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 * Unused. Used to be set by hbck to prevent concurrent splits/merges, but those use PV2 now and
030 * HBCK2 uses it's own service, so no longer an issue. Left in, in case we need to use this for
031 * the incomplete parts of HBCK2...
032 */
033@InterfaceAudience.Private
034public class MasterMaintenanceModeTracker extends ZKListener {
035  private boolean hasChildren;
036
037  public MasterMaintenanceModeTracker(ZKWatcher watcher) {
038    super(watcher);
039    hasChildren = false;
040  }
041
042  public boolean isInMaintenanceMode() {
043    return hasChildren;
044  }
045
046  private void update(String path) {
047    if (path.startsWith(watcher.getZNodePaths().masterMaintZNode)) {
048      update();
049    }
050  }
051
052  private void update() {
053    try {
054      List<String> children =
055          ZKUtil.listChildrenAndWatchForNewChildren(watcher,
056                  watcher.getZNodePaths().masterMaintZNode);
057      hasChildren = (children != null && children.size() > 0);
058    } catch (KeeperException e) {
059      // Ignore the ZK keeper exception
060      hasChildren = false;
061    }
062  }
063
064  /**
065   * Starts the tracking of whether master is in Maintenance Mode.
066   */
067  public void start() {
068    watcher.registerListener(this);
069    update();
070  }
071
072  @Override
073  public void nodeCreated(String path) {
074    update(path);
075  }
076
077  @Override
078  public void nodeDeleted(String path) {
079    update(path);
080  }
081
082  @Override
083  public void nodeChildrenChanged(String path) {
084    update(path);
085  }
086}