HBASE-26416 Implement a new method for region replication instead of using replay...
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / master / AlwaysStandByHMaster.java
blob3d36db71242af8eaaea06db0f9ab8b342dce1a82
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.master;
20 import java.io.IOException;
21 import java.io.InterruptedIOException;
22 import org.apache.hadoop.conf.Configuration;
23 import org.apache.hadoop.hbase.Server;
24 import org.apache.hadoop.hbase.ServerName;
25 import org.apache.hadoop.hbase.monitoring.MonitoredTask;
26 import org.apache.hadoop.hbase.util.Threads;
27 import org.apache.hadoop.hbase.zookeeper.MasterAddressTracker;
28 import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
29 import org.apache.zookeeper.KeeperException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 /**
34 * An implementation of HMaster that always runs as a stand by and never transitions to active.
36 public class AlwaysStandByHMaster extends HMaster {
37 /**
38 * An implementation of ActiveMasterManager that never transitions it's master to active state. It
39 * always remains as a stand by master. With the master registry implementation (HBASE-18095) it
40 * is expected to have at least one active / standby master always running at any point in time
41 * since they serve as the gateway for client connections.
43 * With this implementation, tests can simulate the scenario of not having an active master yet
44 * the client connections to the cluster succeed.
46 private static class AlwaysStandByMasterManager extends ActiveMasterManager {
47 private static final Logger LOG =
48 LoggerFactory.getLogger(AlwaysStandByMasterManager.class);
50 AlwaysStandByMasterManager(ZKWatcher watcher, ServerName sn, Server master)
51 throws InterruptedIOException {
52 super(watcher, sn, master);
55 /**
56 * An implementation that never transitions to an active master.
58 boolean blockUntilBecomingActiveMaster(int checkInterval, MonitoredTask startupStatus) {
59 while (!(master.isAborted() || master.isStopped())) {
60 startupStatus.setStatus("Forever looping to stay as a standby master.");
61 try {
62 activeMasterServerName = null;
63 try {
64 if (MasterAddressTracker.getMasterAddress(watcher) != null) {
65 clusterHasActiveMaster.set(true);
67 } catch (IOException e) {
68 // pass, we will get notified when some other active master creates the znode.
70 Threads.sleepWithoutInterrupt(1000);
71 } catch (KeeperException e) {
72 master.abort("Received an unexpected KeeperException, aborting", e);
73 return false;
75 synchronized (this.clusterHasActiveMaster) {
76 while (clusterHasActiveMaster.get() && !master.isStopped()) {
77 try {
78 clusterHasActiveMaster.wait(checkInterval);
79 } catch (InterruptedException e) {
80 // We expect to be interrupted when a master dies,
81 // will fall out if so
82 LOG.debug("Interrupted waiting for master to die", e);
85 if (clusterShutDown.get()) {
86 this.master.stop(
87 "Cluster went down before this master became active");
91 return false;
95 public AlwaysStandByHMaster(Configuration conf) throws IOException {
96 super(conf);
99 protected ActiveMasterManager createActiveMasterManager(ZKWatcher zk, ServerName sn,
100 org.apache.hadoop.hbase.Server server) throws InterruptedIOException {
101 return new AlwaysStandByMasterManager(zk, sn, server);