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.regionserver.regionreplication;
019
020import static org.junit.Assert.assertNotNull;
021import static org.junit.Assert.assertThrows;
022import static org.junit.Assert.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.ReplicationPeerNotFoundException;
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
030import org.apache.hadoop.hbase.master.HMaster;
031import org.apache.hadoop.hbase.master.procedure.ServerCrashProcedure;
032import org.apache.hadoop.hbase.procedure2.Procedure;
033import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
034import org.apache.hadoop.hbase.replication.ReplicationUtils;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.testclassification.RegionServerTests;
037import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
038import org.apache.hadoop.hbase.util.ServerRegionReplicaUtil;
039import org.junit.AfterClass;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044
045/**
046 * Make sure we could start the cluster with RegionReplicaReplicationEndpoint configured.
047 */
048@Category({ RegionServerTests.class, MediumTests.class })
049public class TestStartupWithLegacyRegionReplicationEndpoint {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestStartupWithLegacyRegionReplicationEndpoint.class);
054
055  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
056
057  @BeforeClass
058  public static void setUp() throws Exception {
059    UTIL.startMiniCluster(1);
060  }
061
062  @AfterClass
063  public static void tearDown() throws IOException {
064    UTIL.shutdownMiniCluster();
065  }
066
067  @Test
068  public void test() throws Exception {
069    ReplicationPeerConfig peerConfig = ReplicationPeerConfig.newBuilder()
070      .setClusterKey("127.0.0.1:2181:/hbase")
071      .setReplicationEndpointImpl(ReplicationUtils.LEGACY_REGION_REPLICATION_ENDPOINT_NAME).build();
072    SingleProcessHBaseCluster cluster = UTIL.getMiniHBaseCluster();
073    HMaster master = cluster.getMaster();
074    // can not use Admin.addPeer as it will fail with ClassNotFound
075    master.getReplicationPeerManager().addPeer("legacy", peerConfig, true);
076    // add a wal file to the queue
077    ServerName rsName = cluster.getRegionServer(0).getServerName();
078    master.getReplicationPeerManager().getQueueStorage().addWAL(rsName,
079      ServerRegionReplicaUtil.REGION_REPLICA_REPLICATION_PEER, "test-wal-file");
080    cluster.stopRegionServer(0);
081    RegionServerThread rst = cluster.startRegionServer();
082    // we should still have this peer
083    assertNotNull(UTIL.getAdmin().getReplicationPeerConfig("legacy"));
084    // but at RS side, we should not have this peer loaded as replication source
085    assertTrue(rst.getRegionServer().getReplicationSourceService().getReplicationManager()
086      .getSources().isEmpty());
087
088    UTIL.shutdownMiniHBaseCluster();
089    UTIL.restartHBaseCluster(1);
090    // now we should have removed the peer
091    assertThrows(ReplicationPeerNotFoundException.class,
092      () -> UTIL.getAdmin().getReplicationPeerConfig("legacy"));
093    // at rs side, we should not have the peer this time, not only for not having replication source
094    assertTrue(UTIL.getMiniHBaseCluster().getRegionServer(0).getReplicationSourceService()
095      .getReplicationManager().getReplicationPeers().getAllPeerIds().isEmpty());
096
097    // make sure that we can finish the SCP and delete the test-wal-file
098    UTIL.waitFor(15000,
099      () -> UTIL.getMiniHBaseCluster().getMaster().getProcedures().stream()
100        .filter(p -> p instanceof ServerCrashProcedure).map(p -> (ServerCrashProcedure) p)
101        .allMatch(Procedure::isSuccess));
102    assertTrue(UTIL.getMiniHBaseCluster().getMaster().getReplicationPeerManager().getQueueStorage()
103      .getAllQueues(rsName).isEmpty());
104  }
105}