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.client.replication;
019
020import java.io.IOException;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HBaseTestingUtility;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.client.Admin;
026import org.apache.hadoop.hbase.client.Connection;
027import org.apache.hadoop.hbase.client.ConnectionFactory;
028import org.apache.hadoop.hbase.replication.ReplicationPeerConfig;
029import org.apache.hadoop.hbase.replication.ReplicationPeerConfigBuilder;
030import org.apache.hadoop.hbase.testclassification.ClientTests;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.junit.AfterClass;
033import org.junit.BeforeClass;
034import org.junit.ClassRule;
035import org.junit.Rule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.junit.rules.TestName;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042@Category({ MediumTests.class, ClientTests.class })
043public class TestBadReplicationPeer {
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestBadReplicationPeer.class);
047  private static final Logger LOG = LoggerFactory.getLogger(TestBadReplicationPeer.class);
048  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
049  private static Configuration conf;
050
051  @Rule
052  public TestName name = new TestName();
053
054  @BeforeClass
055  public static void setUpBeforeClass() throws Exception {
056    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
057    TEST_UTIL.getConfiguration().setBoolean("replication.source.regionserver.abort", false);
058    TEST_UTIL.startMiniCluster();
059    conf = TEST_UTIL.getConfiguration();
060  }
061
062  @AfterClass
063  public static void tearDownAfterClass() throws Exception {
064    TEST_UTIL.shutdownMiniCluster();
065  }
066
067  /*
068   * Add dummy peer and make sure that we are able to remove that peer.
069   */
070  @Test
071  public void testRemovePeerSucceeds() throws IOException {
072    String peerId = "dummypeer_1";
073    try (Connection connection = ConnectionFactory.createConnection(conf);
074      Admin admin = connection.getAdmin()) {
075      ReplicationPeerConfigBuilder rpcBuilder = ReplicationPeerConfig.newBuilder();
076      String quorum = TEST_UTIL.getHBaseCluster().getMaster().getZooKeeper().getQuorum();
077      rpcBuilder.setClusterKey(quorum + ":/1");
078      ReplicationPeerConfig rpc = rpcBuilder.build();
079      admin.addReplicationPeer(peerId, rpc);
080      LOG.info("Added replication peer with peer id: {}", peerId);
081    } finally {
082      LOG.info("Removing replication peer with peer id: {}", peerId);
083      cleanPeer(peerId);
084    }
085  }
086
087  private static void cleanPeer(String peerId) throws IOException {
088    try (Connection connection = ConnectionFactory.createConnection(conf);
089      Admin admin = connection.getAdmin()) {
090      admin.removeReplicationPeer(peerId);
091    }
092  }
093}