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.hamcrest.MatcherAssert.assertThat;
021import static org.hamcrest.Matchers.empty;
022import static org.junit.jupiter.api.Assertions.assertEquals;
023import static org.junit.jupiter.api.Assertions.assertNotNull;
024
025import java.io.IOException;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.client.Admin;
029import org.apache.hadoop.hbase.testclassification.LargeTests;
030import org.apache.hadoop.hbase.testclassification.ReplicationTests;
031import org.apache.hadoop.hbase.util.JVMClusterUtil.MasterThread;
032import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
033import org.apache.hadoop.util.ToolRunner;
034import org.junit.jupiter.api.AfterAll;
035import org.junit.jupiter.api.BeforeAll;
036import org.junit.jupiter.api.Tag;
037import org.junit.jupiter.api.Test;
038
039@Tag(ReplicationTests.TAG)
040@Tag(LargeTests.TAG)
041public class TestMigrateRepliationPeerStorageOnline {
042
043  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
044
045  @BeforeAll
046  public static void setUp() throws Exception {
047    // use zookeeper first, and then migrate to filesystem
048    UTIL.getConfiguration().set(ReplicationStorageFactory.REPLICATION_PEER_STORAGE_IMPL,
049      ReplicationPeerStorageType.ZOOKEEPER.name());
050    UTIL.startMiniCluster(1);
051  }
052
053  @AfterAll
054  public static void tearDown() throws IOException {
055    UTIL.shutdownMiniCluster();
056  }
057
058  @Test
059  public void testMigrate() throws Exception {
060    Admin admin = UTIL.getAdmin();
061    ReplicationPeerConfig rpc =
062      ReplicationPeerConfig.newBuilder().setClusterKey(UTIL.getRpcConnnectionURI() + "-test")
063        .setReplicationEndpointImpl(DummyReplicationEndpoint.class.getName()).build();
064    admin.addReplicationPeer("1", rpc);
065
066    // disable peer modification
067    admin.replicationPeerModificationSwitch(false, true);
068
069    // migrate replication peer data
070    Configuration conf = new Configuration(UTIL.getConfiguration());
071    assertEquals(0, ToolRunner.run(conf, new CopyReplicationPeers(conf),
072      new String[] { "zookeeper", "filesystem" }));
073    conf.set(ReplicationStorageFactory.REPLICATION_PEER_STORAGE_IMPL,
074      ReplicationPeerStorageType.FILESYSTEM.name());
075    // confirm that we have copied the data
076    ReplicationPeerStorage fsPeerStorage = ReplicationStorageFactory
077      .getReplicationPeerStorage(UTIL.getTestFileSystem(), UTIL.getZooKeeperWatcher(), conf);
078    assertNotNull(fsPeerStorage.getPeerConfig("1"));
079
080    for (MasterThread mt : UTIL.getMiniHBaseCluster().getMasterThreads()) {
081      Configuration newConf = new Configuration(mt.getMaster().getConfiguration());
082      newConf.set(ReplicationStorageFactory.REPLICATION_PEER_STORAGE_IMPL,
083        ReplicationPeerStorageType.FILESYSTEM.name());
084      mt.getMaster().getConfigurationManager().notifyAllObservers(newConf);
085    }
086    for (RegionServerThread rt : UTIL.getMiniHBaseCluster().getRegionServerThreads()) {
087      Configuration newConf = new Configuration(rt.getRegionServer().getConfiguration());
088      newConf.set(ReplicationStorageFactory.REPLICATION_PEER_STORAGE_IMPL,
089        ReplicationPeerStorageType.FILESYSTEM.name());
090      rt.getRegionServer().getConfigurationManager().notifyAllObservers(newConf);
091    }
092
093    admin.replicationPeerModificationSwitch(true);
094    admin.removeReplicationPeer("1");
095
096    // confirm that we will operation on the new peer storage
097    assertThat(fsPeerStorage.listPeerIds(), empty());
098  }
099}