View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * This is a shared ZooKeeper-based znode management utils for distributed procedure.  All znode
35   * operations should go through the provided methods in coordinators and members.
36   *
37   * Layout of nodes in ZK is
38   * /hbase/[op name]/acquired/
39   *                    [op instance] - op data/
40   *                        /[nodes that have acquired]
41   *                 /reached/
42   *                    [op instance]/
43   *                        /[nodes that have completed]
44   *                 /abort/
45   *                    [op instance] - failure data
46   *
47   * NOTE: while acquired and completed are znode dirs, abort is actually just a znode.
48   *
49   * Assumption here that procedure names are unique
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     * Top-level watcher/controller for procedures across the cluster.
71     * <p>
72     * On instantiation, this ensures the procedure znodes exist.  This however requires the passed in
73     *  watcher has been started.
74     * @param watcher watcher for the cluster ZK. Owned by <tt>this</tt> and closed via
75     *          {@link #close()}
76     * @param procDescription name of the znode describing the procedure to run
77     * @param memberName name of the member from which we are interacting with running procedures
78     * @throws KeeperException when the procedure znodes cannot be created
79     */
80    public ZKProcedureUtil(ZooKeeperWatcher watcher, String procDescription,
81        String memberName) throws KeeperException {
82      super(watcher);
83      this.memberName = memberName;
84      // make sure we are listening for events
85      watcher.registerListener(this);
86      // setup paths for the zknodes used in procedures
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      // first make sure all the ZK nodes exist
93      // make sure all the parents exist (sometimes not the case in tests)
94      ZKUtil.createWithParents(watcher, acquiredZnode);
95      // regular create because all the parents exist
96      ZKUtil.createAndFailSilent(watcher, reachedZnode);
97      ZKUtil.createAndFailSilent(watcher, abortZnode);
98    }
99  
100   @Override
101   public void close() throws IOException {
102     // the watcher is passed from either Master or Region Server
103     // watcher.close() will be called by the owner so no need to call close() here
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    * Get the full znode path for the node used by the coordinator to trigger a global barrier
136    * acquire on each subprocedure.
137    * @param controller controller running the procedure
138    * @param opInstanceName name of the running procedure instance (not the procedure description).
139    * @return full znode path to the prepare barrier/start node
140    */
141   public static String getAcquireBarrierNode(ZKProcedureUtil controller,
142       String opInstanceName) {
143     return ZKUtil.joinZNode(controller.acquiredZnode, opInstanceName);
144   }
145 
146   /**
147    * Get the full znode path for the node used by the coordinator to trigger a global barrier
148    * execution and release on each subprocedure.
149    * @param controller controller running the procedure
150    * @param opInstanceName name of the running procedure instance (not the procedure description).
151    * @return full znode path to the commit barrier
152    */
153   public static String getReachedBarrierNode(ZKProcedureUtil controller,
154       String opInstanceName) {
155     return ZKUtil.joinZNode(controller.reachedZnode, opInstanceName);
156   }
157 
158   /**
159    * Get the full znode path for the node used by the coordinator or member to trigger an abort
160    * of the global barrier acquisition or execution in subprocedures.
161    * @param controller controller running the procedure
162    * @param opInstanceName name of the running procedure instance (not the procedure description).
163    * @return full znode path to the abort znode
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    * Is this a procedure related znode path?
175    *
176    * TODO: this is not strict, can return true if had name just starts with same prefix but is
177    * different zdir.
178    *
179    * @return true if starts with baseZnode
180    */
181   boolean isInProcedurePath(String path) {
182     return path.startsWith(baseZNode);
183   }
184 
185   /**
186    * Is this the exact procedure barrier acquired znode
187    */
188   boolean isAcquiredNode(String path) {
189     return path.equals(acquiredZnode);
190   }
191 
192   
193   /**
194    * Is this in the procedure barrier acquired znode path
195    */
196   boolean isAcquiredPathNode(String path) {
197     return path.startsWith(this.acquiredZnode) && !path.equals(acquiredZnode);
198   }
199 
200   /**
201    * Is this the exact procedure barrier reached znode
202    */
203   boolean isReachedNode(String path) {
204     return path.equals(reachedZnode);
205   }
206 
207   /**
208    * Is this in the procedure barrier reached znode path
209    */
210   boolean isReachedPathNode(String path) {
211     return path.startsWith(this.reachedZnode) && !path.equals(reachedZnode);
212   }
213 
214 
215   /**
216    * Is this in the procedure barrier abort znode path
217    */
218   boolean isAbortNode(String path) {
219     return path.equals(abortZnode);
220   }
221 
222   /**
223    * Is this in the procedure barrier abort znode path
224    */
225   public boolean isAbortPathNode(String path) {
226     return path.startsWith(this.abortZnode) && !path.equals(abortZnode);
227   }
228 
229   // --------------------------------------------------------------------------
230   // internal debugging methods
231   // --------------------------------------------------------------------------
232   /**
233    * Recursively print the current state of ZK (non-transactional)
234    * @param root name of the root directory in zk to print
235    * @throws KeeperException
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    * Helper method to print the current state of the ZK tree.
251    * @see #logZKTree(String)
252    * @throws KeeperException if an unexpected exception occurs
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     // TODO This is potentially racy since not atomic. update when we support zk that has multi
266     LOG.info("Clearing all procedure znodes: " + acquiredZnode + " " + reachedZnode + " "
267         + abortZnode);
268 
269     // If the coordinator was shutdown mid-procedure, then we are going to lose
270     // an procedure that was previously started by cleaning out all the previous state. Its much
271     // harder to figure out how to keep an procedure going and the subject of HBASE-5487.
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     // TODO This is potentially racy since not atomic. update when we support zk that has multi
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 }