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.replication;
019
020import static org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022
023import org.apache.hadoop.fs.Path;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.master.MasterFileSystem;
026import org.apache.hadoop.hbase.testclassification.LargeTests;
027import org.apache.hadoop.hbase.testclassification.ReplicationTests;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034@Category({ ReplicationTests.class, LargeTests.class })
035public class TestSyncReplicationStandbyKillMaster extends SyncReplicationTestBase {
036
037  private static final Logger LOG =
038    LoggerFactory.getLogger(TestSyncReplicationStandbyKillMaster.class);
039
040  private final long SLEEP_TIME = 2000;
041
042  private final int COUNT = 1000;
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestSyncReplicationStandbyKillMaster.class);
047
048  @Test
049  public void testStandbyKillMaster() throws Exception {
050    MasterFileSystem mfs = UTIL2.getHBaseCluster().getMaster().getMasterFileSystem();
051    Path remoteWALDir = getRemoteWALDir(mfs, PEER_ID);
052    assertFalse(mfs.getWALFileSystem().exists(remoteWALDir));
053    UTIL2.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
054      SyncReplicationState.STANDBY);
055    assertTrue(mfs.getWALFileSystem().exists(remoteWALDir));
056    UTIL1.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
057      SyncReplicationState.ACTIVE);
058
059    // Disable async replication and write data, then shutdown
060    UTIL1.getAdmin().disableReplicationPeer(PEER_ID);
061    write(UTIL1, 0, COUNT);
062    UTIL1.shutdownMiniCluster();
063
064    Thread t = new Thread(() -> {
065      try {
066        Thread.sleep(SLEEP_TIME);
067        UTIL2.getMiniHBaseCluster().getMaster().stop("Stop master for test");
068      } catch (Exception e) {
069        LOG.error("Failed to stop master", e);
070      }
071    });
072    t.start();
073
074    // Transit standby to DA to replay logs
075    try {
076      UTIL2.getAdmin().transitReplicationPeerSyncReplicationState(PEER_ID,
077        SyncReplicationState.DOWNGRADE_ACTIVE);
078    } catch (Exception e) {
079      LOG.error("Failed to transit standby cluster to " + SyncReplicationState.DOWNGRADE_ACTIVE);
080    }
081
082    while (
083      UTIL2.getAdmin().getReplicationPeerSyncReplicationState(PEER_ID)
084          != SyncReplicationState.DOWNGRADE_ACTIVE
085    ) {
086      Thread.sleep(SLEEP_TIME);
087    }
088    verify(UTIL2, 0, COUNT);
089  }
090}