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