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