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