1 /** 2 * Copyright 2010 The Apache Software Foundation 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package org.apache.hadoop.hbase.zookeeper; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.hadoop.hbase.Abortable; 25 import org.apache.hadoop.hbase.ClusterStatus; 26 import org.apache.hadoop.hbase.util.Bytes; 27 import org.apache.zookeeper.KeeperException; 28 29 /** 30 * Tracker on cluster settings up in zookeeper. 31 * This is not related to {@link ClusterStatus}. That class is a data structure 32 * that holds snapshot of current view on cluster. This class is about tracking 33 * cluster attributes up in zookeeper. 34 * 35 */ 36 public class ClusterStatusTracker extends ZooKeeperNodeTracker { 37 private static final Log LOG = LogFactory.getLog(ClusterStatusTracker.class); 38 39 /** 40 * Creates a cluster status tracker. 41 * 42 * <p>After construction, use {@link #start} to kick off tracking. 43 * 44 * @param watcher 45 * @param abortable 46 */ 47 public ClusterStatusTracker(ZooKeeperWatcher watcher, Abortable abortable) { 48 super(watcher, watcher.clusterStateZNode, abortable); 49 } 50 51 /** 52 * Checks if cluster is up. 53 * @return true if root region location is available, false if not 54 */ 55 public boolean isClusterUp() { 56 return super.getData() != null; 57 } 58 59 /** 60 * Sets the cluster as up. 61 * @throws KeeperException unexpected zk exception 62 */ 63 public void setClusterUp() 64 throws KeeperException { 65 byte [] upData = Bytes.toBytes(new java.util.Date().toString()); 66 try { 67 ZKUtil.createAndWatch(watcher, watcher.clusterStateZNode, upData); 68 } catch(KeeperException.NodeExistsException nee) { 69 ZKUtil.setData(watcher, watcher.clusterStateZNode, upData); 70 } 71 } 72 73 /** 74 * Sets the cluster as down by deleting the znode. 75 * @throws KeeperException unexpected zk exception 76 */ 77 public void setClusterDown() 78 throws KeeperException { 79 try { 80 ZKUtil.deleteNode(watcher, watcher.clusterStateZNode); 81 } catch(KeeperException.NoNodeException nne) { 82 LOG.warn("Attempted to set cluster as down but already down, cluster " + 83 "state node (" + watcher.clusterStateZNode + ") not found"); 84 } 85 } 86 }