001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.zookeeper;
020
021import java.util.List;
022
023import org.apache.yetus.audience.InterfaceAudience;
024import org.apache.zookeeper.KeeperException;
025
026/**
027 * Tracks the master Maintenance Mode via ZK.
028 */
029@InterfaceAudience.Private
030public class MasterMaintenanceModeTracker extends ZKListener {
031  private boolean hasChildren;
032
033  public MasterMaintenanceModeTracker(ZKWatcher watcher) {
034    super(watcher);
035    hasChildren = false;
036  }
037
038  public boolean isInMaintenanceMode() {
039    return hasChildren;
040  }
041
042  private void update(String path) {
043    if (path.startsWith(watcher.znodePaths.masterMaintZNode)) {
044      update();
045    }
046  }
047
048  private void update() {
049    try {
050      List<String> children =
051          ZKUtil.listChildrenAndWatchForNewChildren(watcher, watcher.znodePaths.masterMaintZNode);
052      hasChildren = (children != null && children.size() > 0);
053    } catch (KeeperException e) {
054      // Ignore the ZK keeper exception
055      hasChildren = false;
056    }
057  }
058
059  /**
060   * Starts the tracking of whether master is in Maintenance Mode.
061   */
062  public void start() {
063    watcher.registerListener(this);
064    update();
065  }
066
067  @Override
068  public void nodeCreated(String path) {
069    update(path);
070  }
071
072  @Override
073  public void nodeDeleted(String path) {
074    update(path);
075  }
076
077  @Override
078  public void nodeChildrenChanged(String path) {
079    update(path);
080  }
081}