001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.zookeeper;
019
020import java.util.List;
021import org.apache.yetus.audience.InterfaceAudience;
022import org.apache.zookeeper.KeeperException;
023
024/**
025 * Tracks the master Maintenance Mode via ZK. Unused. Used to be set by hbck to prevent concurrent
026 * splits/merges, but those use PV2 now and HBCK2 uses it's own service, so no longer an issue. Left
027 * in, in case we need to use this for the incomplete parts of HBCK2...
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.getZNodePaths().masterMaintZNode)) {
044      update();
045    }
046  }
047
048  private void update() {
049    try {
050      List<String> children = ZKUtil.listChildrenAndWatchForNewChildren(watcher,
051        watcher.getZNodePaths().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}