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