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.master;
019
020import java.util.concurrent.TimeUnit;
021import org.apache.hadoop.hbase.util.Threads;
022import org.apache.yetus.audience.InterfaceAudience;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025
026/**
027 * Protection against zombie master. Started once Master accepts active responsibility and starts
028 * taking over responsibilities. Allows a finite time window before giving up ownership.
029 */
030@InterfaceAudience.Private
031class MasterInitializationMonitor extends Thread {
032
033  private static final Logger LOG = LoggerFactory.getLogger(MasterInitializationMonitor.class);
034
035  /** The amount of time in milliseconds to sleep before checking initialization status. */
036  public static final String TIMEOUT_KEY = "hbase.master.initializationmonitor.timeout";
037  public static final long TIMEOUT_DEFAULT = TimeUnit.MILLISECONDS.convert(15, TimeUnit.MINUTES);
038
039  /**
040   * When timeout expired and initialization has not complete, call {@link System#exit(int)} when
041   * true, do nothing otherwise.
042   */
043  public static final String HALT_KEY = "hbase.master.initializationmonitor.haltontimeout";
044  public static final boolean HALT_DEFAULT = false;
045
046  private final HMaster master;
047  private final long timeout;
048  private final boolean haltOnTimeout;
049
050  /** Creates a Thread that monitors the {@link #isInitialized()} state. */
051  MasterInitializationMonitor(HMaster master) {
052    super("MasterInitializationMonitor");
053    this.master = master;
054    this.timeout = master.getConfiguration().getLong(TIMEOUT_KEY, TIMEOUT_DEFAULT);
055    this.haltOnTimeout = master.getConfiguration().getBoolean(HALT_KEY, HALT_DEFAULT);
056    this.setDaemon(true);
057  }
058
059  @Override
060  public void run() {
061    try {
062      while (!master.isStopped() && master.isActiveMaster()) {
063        Thread.sleep(timeout);
064        if (master.isInitialized()) {
065          LOG.debug("Initialization completed within allotted tolerance. Monitor exiting.");
066        } else {
067          LOG.error("Master failed to complete initialization after " + timeout + "ms. Please"
068            + " consider submitting a bug report including a thread dump of this process.");
069          if (haltOnTimeout) {
070            LOG.error("Zombie Master exiting. Thread dump to stdout");
071            Threads.printThreadInfo(System.out, "Zombie HMaster");
072            System.exit(-1);
073          }
074        }
075      }
076    } catch (InterruptedException ie) {
077      LOG.trace("InitMonitor thread interrupted. Existing.");
078    }
079  }
080}