1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
39
40 public RecoveringRegionWatcher(ZooKeeperWatcher watcher, HRegionServer server) {
41 super(watcher);
42 watcher.registerListener(this);
43 this.server = server;
44 }
45
46
47
48
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
81
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 }