1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.procedure;
19
20 import java.io.Closeable;
21 import java.io.IOException;
22 import java.util.List;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.classification.InterfaceAudience;
27 import org.apache.hadoop.classification.InterfaceStability;
28 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
29 import org.apache.hadoop.hbase.zookeeper.ZooKeeperListener;
30 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
31 import org.apache.zookeeper.KeeperException;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 @InterfaceAudience.Public
52 @InterfaceStability.Evolving
53 public abstract class ZKProcedureUtil
54 extends ZooKeeperListener implements Closeable {
55
56 private static final Log LOG = LogFactory.getLog(ZKProcedureUtil.class);
57
58 public static final String ACQUIRED_BARRIER_ZNODE_DEFAULT = "acquired";
59 public static final String REACHED_BARRIER_ZNODE_DEFAULT = "reached";
60 public static final String ABORT_ZNODE_DEFAULT = "abort";
61
62 public final String baseZNode;
63 protected final String acquiredZnode;
64 protected final String reachedZnode;
65 protected final String abortZnode;
66
67
68
69
70
71
72
73
74
75
76
77 public ZKProcedureUtil(ZooKeeperWatcher watcher, String procDescription)
78 throws KeeperException {
79 super(watcher);
80
81 watcher.registerListener(this);
82
83 this.baseZNode = ZKUtil.joinZNode(watcher.baseZNode, procDescription);
84 acquiredZnode = ZKUtil.joinZNode(baseZNode, ACQUIRED_BARRIER_ZNODE_DEFAULT);
85 reachedZnode = ZKUtil.joinZNode(baseZNode, REACHED_BARRIER_ZNODE_DEFAULT);
86 abortZnode = ZKUtil.joinZNode(baseZNode, ABORT_ZNODE_DEFAULT);
87
88
89
90 ZKUtil.createWithParents(watcher, acquiredZnode);
91
92 ZKUtil.createAndFailSilent(watcher, reachedZnode);
93 ZKUtil.createAndFailSilent(watcher, abortZnode);
94 }
95
96 @Override
97 public void close() throws IOException {
98
99
100 }
101
102 public String getAcquiredBarrierNode(String opInstanceName) {
103 return ZKProcedureUtil.getAcquireBarrierNode(this, opInstanceName);
104 }
105
106 public String getReachedBarrierNode(String opInstanceName) {
107 return ZKProcedureUtil.getReachedBarrierNode(this, opInstanceName);
108 }
109
110 public String getAbortZNode(String opInstanceName) {
111 return ZKProcedureUtil.getAbortNode(this, opInstanceName);
112 }
113
114 public String getAbortZnode() {
115 return abortZnode;
116 }
117
118 public String getBaseZnode() {
119 return baseZNode;
120 }
121
122 public String getAcquiredBarrier() {
123 return acquiredZnode;
124 }
125
126
127
128
129
130
131
132
133 public static String getAcquireBarrierNode(ZKProcedureUtil controller,
134 String opInstanceName) {
135 return ZKUtil.joinZNode(controller.acquiredZnode, opInstanceName);
136 }
137
138
139
140
141
142
143
144
145 public static String getReachedBarrierNode(ZKProcedureUtil controller,
146 String opInstanceName) {
147 return ZKUtil.joinZNode(controller.reachedZnode, opInstanceName);
148 }
149
150
151
152
153
154
155
156
157 public static String getAbortNode(ZKProcedureUtil controller, String opInstanceName) {
158 return ZKUtil.joinZNode(controller.abortZnode, opInstanceName);
159 }
160
161 public ZooKeeperWatcher getWatcher() {
162 return watcher;
163 }
164
165
166
167
168
169
170
171
172
173 boolean isInProcedurePath(String path) {
174 return path.startsWith(baseZNode);
175 }
176
177
178
179
180 boolean isAcquiredNode(String path) {
181 return path.equals(acquiredZnode);
182 }
183
184
185
186
187
188 boolean isAcquiredPathNode(String path) {
189 return path.startsWith(this.acquiredZnode) && !path.equals(acquiredZnode) &&
190 isMemberNode(path, acquiredZnode);
191 }
192
193
194
195
196 boolean isReachedNode(String path) {
197 return path.equals(reachedZnode);
198 }
199
200
201
202
203 boolean isReachedPathNode(String path) {
204 return path.startsWith(this.reachedZnode) && !path.equals(reachedZnode) &&
205 isMemberNode(path, reachedZnode);
206 }
207
208
209
210
211
212
213
214 private boolean isMemberNode(final String path, final String statePath) {
215 int count = 0;
216 for (int i = statePath.length(); i < path.length(); ++i) {
217 count += (path.charAt(i) == ZKUtil.ZNODE_PATH_SEPARATOR) ? 1 : 0;
218 }
219 return count == 2;
220 }
221
222
223
224
225 boolean isAbortNode(String path) {
226 return path.equals(abortZnode);
227 }
228
229
230
231
232 public boolean isAbortPathNode(String path) {
233 return path.startsWith(this.abortZnode) && !path.equals(abortZnode);
234 }
235
236
237
238
239
240
241
242
243
244 void logZKTree(String root) {
245 if (!LOG.isDebugEnabled()) return;
246 LOG.debug("Current zk system:");
247 String prefix = "|-";
248 LOG.debug(prefix + root);
249 try {
250 logZKTree(root, prefix);
251 } catch (KeeperException e) {
252 throw new RuntimeException(e);
253 }
254 }
255
256
257
258
259
260
261 protected void logZKTree(String root, String prefix) throws KeeperException {
262 List<String> children = ZKUtil.listChildrenNoWatch(watcher, root);
263 if (children == null) return;
264 for (String child : children) {
265 LOG.debug(prefix + child);
266 String node = ZKUtil.joinZNode(root.equals("/") ? "" : root, child);
267 logZKTree(node, prefix + "---");
268 }
269 }
270
271 public void clearChildZNodes() throws KeeperException {
272
273 LOG.info("Clearing all procedure znodes: " + acquiredZnode + " " + reachedZnode + " "
274 + abortZnode);
275
276
277
278
279 ZKUtil.deleteChildrenRecursively(watcher, acquiredZnode);
280 ZKUtil.deleteChildrenRecursively(watcher, reachedZnode);
281 ZKUtil.deleteChildrenRecursively(watcher, abortZnode);
282 }
283
284 public void clearZNodes(String procedureName) throws KeeperException {
285
286 LOG.info("Clearing all znodes for procedure " + procedureName + "including nodes "
287 + acquiredZnode + " " + reachedZnode + " " + abortZnode);
288 ZKUtil.deleteNodeRecursively(watcher, getAcquiredBarrierNode(procedureName));
289 ZKUtil.deleteNodeRecursively(watcher, getReachedBarrierNode(procedureName));
290 ZKUtil.deleteNodeRecursively(watcher, getAbortZNode(procedureName));
291 }
292 }