View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.zookeeper;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.regionserver.HRegion;
25  import org.apache.hadoop.hbase.regionserver.HRegionServer;
26  import org.apache.zookeeper.KeeperException;
27  
28  /**
29   * Watcher used to be notified of the recovering region coming out of recovering state
30   */
31  @InterfaceAudience.Private
32  public class RecoveringRegionWatcher extends ZooKeeperListener {
33    private static final Log LOG = LogFactory.getLog(RecoveringRegionWatcher.class);
34  
35    private HRegionServer server;
36    
37    /**
38     * Construct a ZooKeeper event listener.
39     */
40    public RecoveringRegionWatcher(ZooKeeperWatcher watcher, HRegionServer server) {
41      super(watcher);
42      watcher.registerListener(this);
43      this.server = server;
44    }
45  
46    /**
47     * Called when a node has been deleted
48     * @param path full path of the deleted node
49     */
50    public void nodeDeleted(String path) {
51      if (this.server.isStopped() || this.server.isStopping()) {
52        return;
53      }
54  
55      String parentPath = path.substring(0, path.lastIndexOf('/'));
56      if (!this.watcher.recoveringRegionsZNode.equalsIgnoreCase(parentPath)) {
57        return;
58      }
59  
60      String regionName = path.substring(parentPath.length() + 1);
61      HRegion region = this.server.getRecoveringRegions().remove(regionName);
62      if (region != null) {
63        region.setRecovering(false);
64      }
65  
66      LOG.info(path + " znode deleted. Region: " + regionName + " completes recovery.");
67    }
68  
69    @Override
70    public void nodeDataChanged(String path) {
71      registerWatcher(path);
72    }
73  
74    @Override
75    public void nodeChildrenChanged(String path) {
76      registerWatcher(path);
77    }
78  
79    /**
80     * Reinstall watcher because watcher only fire once though we're only interested in nodeDeleted
81     * event we need to register the watcher in case other event happens
82     */
83    private void registerWatcher(String path) {
84      String parentPath = path.substring(0, path.lastIndexOf('/'));
85      if (!this.watcher.recoveringRegionsZNode.equalsIgnoreCase(parentPath)) {
86        return;
87      }
88  
89      try {
90        ZKUtil.getDataAndWatch(watcher, path);
91      } catch (KeeperException e) {
92        LOG.warn("Can't register watcher on znode " + path, e);
93      }
94    }
95  }