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.apache.hadoop.hbase.replication.ReplicationPeerConfigTestUtil.assertConfigEquals;
021import static org.apache.hadoop.hbase.replication.ReplicationPeerConfigTestUtil.getConfig;
022import static org.junit.Assert.assertEquals;
023
024import java.util.List;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseZKTestingUtility;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.apache.hadoop.hbase.testclassification.ReplicationTests;
031import org.apache.hadoop.hbase.util.CommonFSUtils;
032import org.apache.hadoop.util.ToolRunner;
033import org.junit.AfterClass;
034import org.junit.BeforeClass;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039@Category({ ReplicationTests.class, MediumTests.class })
040public class TestCopyReplicationPeers {
041
042  @ClassRule
043  public static final HBaseClassTestRule CLASS_RULE =
044    HBaseClassTestRule.forClass(TestCopyReplicationPeers.class);
045
046  private static final HBaseZKTestingUtility UTIL = new HBaseZKTestingUtility();
047
048  private static FileSystem FS;
049
050  private static Path DIR;
051
052  private static ReplicationPeerStorage SRC;
053
054  private static ReplicationPeerStorage DST;
055
056  @BeforeClass
057  public static void setUp() throws Exception {
058    DIR = UTIL.getDataTestDir("test_peer_migration");
059    CommonFSUtils.setRootDir(UTIL.getConfiguration(), DIR);
060    FS = FileSystem.get(UTIL.getConfiguration());
061    UTIL.startMiniZKCluster();
062    SRC = new ZKReplicationPeerStorage(UTIL.getZooKeeperWatcher(), UTIL.getConfiguration());
063    DST = new FSReplicationPeerStorage(FS, UTIL.getConfiguration());
064  }
065
066  @AfterClass
067  public static void tearDown() throws Exception {
068    UTIL.shutdownMiniZKCluster();
069    UTIL.cleanupTestDir();
070  }
071
072  @Test
073  public void testMigrate() throws Exception {
074    // invalid args
075    assertEquals(-1,
076      ToolRunner.run(new CopyReplicationPeers(UTIL.getConfiguration()), new String[] {}));
077    int peerCount = 10;
078    for (int i = 0; i < peerCount; i++) {
079      SRC.addPeer(Integer.toString(i), getConfig(i), i % 2 == 0);
080    }
081    // migrate
082    assertEquals(0, ToolRunner.run(new CopyReplicationPeers(UTIL.getConfiguration()),
083      new String[] { SRC.getClass().getName(), DST.getClass().getName() }));
084    // verify the replication peer data in dst storage
085    List<String> peerIds = DST.listPeerIds();
086    assertEquals(peerCount, peerIds.size());
087    for (String peerId : peerIds) {
088      int seed = Integer.parseInt(peerId);
089      assertConfigEquals(getConfig(seed), DST.getPeerConfig(peerId));
090    }
091  }
092}