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.Abortable;
25  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
26  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
27  import org.apache.zookeeper.KeeperException;
28  
29  /**
30   * Tracker on cluster settings up in zookeeper.
31   * This is not related to {@link org.apache.hadoop.hbase.ClusterStatus}. That class 
32   * is a data structure that holds snapshot of current view on cluster. This class 
33   * is about tracking cluster attributes up in zookeeper.
34   *
35   */
36  @InterfaceAudience.Private
37  public class ClusterStatusTracker extends ZooKeeperNodeTracker {
38    private static final Log LOG = LogFactory.getLog(ClusterStatusTracker.class);
39  
40    /**
41     * Creates a cluster status tracker.
42     *
43     * <p>After construction, use {@link #start} to kick off tracking.
44     *
45     * @param watcher
46     * @param abortable
47     */
48    public ClusterStatusTracker(ZooKeeperWatcher watcher, Abortable abortable) {
49      super(watcher, watcher.clusterStateZNode, abortable);
50    }
51  
52    /**
53     * Checks if cluster is up.
54     * @return true if the cluster up ('shutdown' is its name up in zk) znode
55     * exists with data, false if not
56     */
57    public boolean isClusterUp() {
58      return super.getData(false) != null;
59    }
60  
61    /**
62     * Sets the cluster as up.
63     * @throws KeeperException unexpected zk exception
64     */
65    public void setClusterUp()
66    throws KeeperException {
67      byte [] upData = toByteArray();
68      try {
69        ZKUtil.createAndWatch(watcher, watcher.clusterStateZNode, upData);
70      } catch(KeeperException.NodeExistsException nee) {
71        ZKUtil.setData(watcher, watcher.clusterStateZNode, upData);
72      }
73    }
74  
75    /**
76     * Sets the cluster as down by deleting the znode.
77     * @throws KeeperException unexpected zk exception
78     */
79    public void setClusterDown()
80    throws KeeperException {
81      try {
82        ZKUtil.deleteNode(watcher, watcher.clusterStateZNode);
83      } catch(KeeperException.NoNodeException nne) {
84        LOG.warn("Attempted to set cluster as down but already down, cluster " +
85            "state node (" + watcher.clusterStateZNode + ") not found");
86      }
87    }
88  
89    /**
90     * @return Content of the clusterup znode as a serialized pb with the pb
91     * magic as prefix.
92     */
93    static byte [] toByteArray() {
94      ZooKeeperProtos.ClusterUp.Builder builder =
95        ZooKeeperProtos.ClusterUp.newBuilder();
96      builder.setStartDate(new java.util.Date().toString());
97      return ProtobufUtil.prependPBMagic(builder.build().toByteArray());
98    }
99  }