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.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.regionserver.HRegionServer;
25  import org.apache.hadoop.hbase.regionserver.handler.FinishRegionRecoveringHandler;
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    @Override
51    public void nodeDeleted(String path) {
52      if (this.server.isStopped() || this.server.isStopping()) {
53        return;
54      }
55  
56      String parentPath = path.substring(0, path.lastIndexOf('/'));
57      if (!this.watcher.recoveringRegionsZNode.equalsIgnoreCase(parentPath)) {
58        return;
59      }
60  
61      String regionName = path.substring(parentPath.length() + 1);
62  
63      server.getExecutorService().submit(new FinishRegionRecoveringHandler(server, regionName, path));
64    }
65  
66    @Override
67    public void nodeDataChanged(String path) {
68      registerWatcher(path);
69    }
70  
71    @Override
72    public void nodeChildrenChanged(String path) {
73      registerWatcher(path);
74    }
75  
76    /**
77     * Reinstall watcher because watcher only fire once though we're only interested in nodeDeleted
78     * event we need to register the watcher in case other event happens
79     */
80    private void registerWatcher(String path) {
81      String parentPath = path.substring(0, path.lastIndexOf('/'));
82      if (!this.watcher.recoveringRegionsZNode.equalsIgnoreCase(parentPath)) {
83        return;
84      }
85  
86      try {
87        ZKUtil.getDataAndWatch(watcher, path);
88      } catch (KeeperException e) {
89        LOG.warn("Can't register watcher on znode " + path, e);
90      }
91    }
92  }