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