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 protected final String memberName;
68
69
70
71
72
73
74
75
76
77
78
79
80 public ZKProcedureUtil(ZooKeeperWatcher watcher, String procDescription,
81 String memberName) throws KeeperException {
82 super(watcher);
83 this.memberName = memberName;
84
85 watcher.registerListener(this);
86
87 this.baseZNode = ZKUtil.joinZNode(watcher.baseZNode, procDescription);
88 acquiredZnode = ZKUtil.joinZNode(baseZNode, ACQUIRED_BARRIER_ZNODE_DEFAULT);
89 reachedZnode = ZKUtil.joinZNode(baseZNode, REACHED_BARRIER_ZNODE_DEFAULT);
90 abortZnode = ZKUtil.joinZNode(baseZNode, ABORT_ZNODE_DEFAULT);
91
92
93
94 ZKUtil.createWithParents(watcher, acquiredZnode);
95
96 ZKUtil.createAndFailSilent(watcher, reachedZnode);
97 ZKUtil.createAndFailSilent(watcher, abortZnode);
98 }
99
100 @Override
101 public void close() throws IOException {
102
103
104 }
105
106 public String getAcquiredBarrierNode(String opInstanceName) {
107 return ZKProcedureUtil.getAcquireBarrierNode(this, opInstanceName);
108 }
109
110 public String getReachedBarrierNode(String opInstanceName) {
111 return ZKProcedureUtil.getReachedBarrierNode(this, opInstanceName);
112 }
113
114 public String getAbortZNode(String opInstanceName) {
115 return ZKProcedureUtil.getAbortNode(this, opInstanceName);
116 }
117
118 public String getAbortZnode() {
119 return abortZnode;
120 }
121
122 public String getBaseZnode() {
123 return baseZNode;
124 }
125
126 public String getAcquiredBarrier() {
127 return acquiredZnode;
128 }
129
130 public String getMemberName() {
131 return memberName;
132 }
133
134
135
136
137
138
139
140
141 public static String getAcquireBarrierNode(ZKProcedureUtil controller,
142 String opInstanceName) {
143 return ZKUtil.joinZNode(controller.acquiredZnode, opInstanceName);
144 }
145
146
147
148
149
150
151
152
153 public static String getReachedBarrierNode(ZKProcedureUtil controller,
154 String opInstanceName) {
155 return ZKUtil.joinZNode(controller.reachedZnode, opInstanceName);
156 }
157
158
159
160
161
162
163
164
165 public static String getAbortNode(ZKProcedureUtil controller, String opInstanceName) {
166 return ZKUtil.joinZNode(controller.abortZnode, opInstanceName);
167 }
168
169 public ZooKeeperWatcher getWatcher() {
170 return watcher;
171 }
172
173
174
175
176
177
178
179
180
181 boolean isInProcedurePath(String path) {
182 return path.startsWith(baseZNode);
183 }
184
185
186
187
188 boolean isAcquiredNode(String path) {
189 return path.equals(acquiredZnode);
190 }
191
192
193
194
195
196 boolean isAcquiredPathNode(String path) {
197 return path.startsWith(this.acquiredZnode) && !path.equals(acquiredZnode);
198 }
199
200
201
202
203 boolean isReachedNode(String path) {
204 return path.equals(reachedZnode);
205 }
206
207
208
209
210 boolean isReachedPathNode(String path) {
211 return path.startsWith(this.reachedZnode) && !path.equals(reachedZnode);
212 }
213
214
215
216
217
218 boolean isAbortNode(String path) {
219 return path.equals(abortZnode);
220 }
221
222
223
224
225 public boolean isAbortPathNode(String path) {
226 return path.startsWith(this.abortZnode) && !path.equals(abortZnode);
227 }
228
229
230
231
232
233
234
235
236
237 void logZKTree(String root) {
238 if (!LOG.isDebugEnabled()) return;
239 LOG.debug("Current zk system:");
240 String prefix = "|-";
241 LOG.debug(prefix + root);
242 try {
243 logZKTree(root, prefix);
244 } catch (KeeperException e) {
245 throw new RuntimeException(e);
246 }
247 }
248
249
250
251
252
253
254 protected void logZKTree(String root, String prefix) throws KeeperException {
255 List<String> children = ZKUtil.listChildrenNoWatch(watcher, root);
256 if (children == null) return;
257 for (String child : children) {
258 LOG.debug(prefix + child);
259 String node = ZKUtil.joinZNode(root.equals("/") ? "" : root, child);
260 logZKTree(node, prefix + "---");
261 }
262 }
263
264 public void clearChildZNodes() throws KeeperException {
265
266 LOG.info("Clearing all procedure znodes: " + acquiredZnode + " " + reachedZnode + " "
267 + abortZnode);
268
269
270
271
272 ZKUtil.deleteChildrenRecursively(watcher, acquiredZnode);
273 ZKUtil.deleteChildrenRecursively(watcher, reachedZnode);
274 ZKUtil.deleteChildrenRecursively(watcher, abortZnode);
275 }
276
277 public void clearZNodes(String procedureName) throws KeeperException {
278
279 LOG.info("Clearing all znodes for procedure " + procedureName + "including nodes "
280 + acquiredZnode + " " + reachedZnode + " " + abortZnode);
281 ZKUtil.deleteNodeRecursively(watcher, getAcquiredBarrierNode(procedureName));
282 ZKUtil.deleteNodeRecursively(watcher, getReachedBarrierNode(procedureName));
283 ZKUtil.deleteNodeRecursively(watcher, getAbortZNode(procedureName));
284 }
285 }