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 java.util.List;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.zookeeper.KeeperException;
25  
26  /**
27   * Tracks the master Maintenance Mode via ZK.
28   */
29  @InterfaceAudience.Private
30  public class MasterMaintenanceModeTracker extends ZooKeeperListener {
31    private boolean hasChildren;
32  
33    public MasterMaintenanceModeTracker(ZooKeeperWatcher watcher) {
34      super(watcher);
35      hasChildren = false;
36    }
37  
38    public boolean isInMaintenanceMode() {
39      return hasChildren;
40    }
41  
42    private void update(String path) {
43      if (path.startsWith(ZooKeeperWatcher.masterMaintZNode)) {
44        update();
45      }
46    }
47  
48    private void update() {
49      try {
50        List<String> children =
51            ZKUtil.listChildrenAndWatchForNewChildren(watcher, ZooKeeperWatcher.masterMaintZNode);
52        hasChildren = (children != null && children.size() > 0);
53      } catch (KeeperException e) {
54        // Ignore the ZK keeper exception
55        hasChildren = false;
56      }
57    }
58  
59    /**
60     * Starts the tracking of whether master is in Maintenance Mode.
61     */
62    public void start() {
63      watcher.registerListener(this);
64      update();
65    }
66  
67    @Override
68    public void nodeCreated(String path) {
69      update(path);
70    }
71  
72    @Override
73    public void nodeDeleted(String path) {
74      update(path);
75    }
76  
77    @Override
78    public void nodeChildrenChanged(String path) {
79      update(path);
80    }
81  }