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