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.junit.jupiter.api.Assertions.assertArrayEquals;
021import static org.junit.jupiter.api.Assertions.fail;
022
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.client.Get;
025import org.apache.hadoop.hbase.client.Put;
026import org.apache.hadoop.hbase.client.Result;
027import org.apache.hadoop.hbase.testclassification.LargeTests;
028import org.apache.hadoop.hbase.testclassification.ReplicationTests;
029import org.apache.hadoop.hbase.util.Bytes;
030import org.junit.jupiter.api.Tag;
031import org.junit.jupiter.api.Test;
032import org.slf4j.Logger;
033import org.slf4j.LoggerFactory;
034
035@Tag(ReplicationTests.TAG)
036@Tag(LargeTests.TAG)
037public class TestReplicationDisableInactivePeer extends TestReplicationBase {
038
039  private static final Logger LOG =
040    LoggerFactory.getLogger(TestReplicationDisableInactivePeer.class);
041
042  @Override
043  protected String getClusterKey(HBaseTestingUtil util) throws Exception {
044    // in this test we will restart the peer cluster, and the master address will be changed, so we
045    // need to use zk based connection uri
046    return util.getZkConnectionURI();
047  }
048
049  /**
050   * Test disabling an inactive peer. Add a peer which is inactive, trying to insert, disable the
051   * peer, then activate the peer and make sure nothing is replicated. In Addition, enable the peer
052   * and check the updates are replicated.
053   */
054  @Test
055  public void testDisableInactivePeer() throws Exception {
056    UTIL2.shutdownMiniHBaseCluster();
057
058    byte[] rowkey = Bytes.toBytes("disable inactive peer");
059    Put put = new Put(rowkey);
060    put.addColumn(famName, row, row);
061    htable1.put(put);
062
063    // wait for the sleep interval of the master cluster to become long
064    Thread.sleep(SLEEP_TIME * NB_RETRIES);
065
066    // disable and start the peer
067    hbaseAdmin.disableReplicationPeer("2");
068    restartTargetHBaseCluster(2);
069    Get get = new Get(rowkey);
070    for (int i = 0; i < NB_RETRIES; i++) {
071      Result res = htable2.get(get);
072      if (res.size() >= 1) {
073        fail("Replication wasn't disabled");
074      } else {
075        LOG.info("Row not replicated, let's wait a bit more...");
076        Thread.sleep(SLEEP_TIME);
077      }
078    }
079
080    // Test enable replication
081    hbaseAdmin.enableReplicationPeer("2");
082    // wait since the sleep interval would be long
083    Thread.sleep(SLEEP_TIME * NB_RETRIES);
084    for (int i = 0; i < NB_RETRIES; i++) {
085      Result res = htable2.get(get);
086      if (res.isEmpty()) {
087        LOG.info("Row not available");
088        Thread.sleep(SLEEP_TIME * NB_RETRIES);
089      } else {
090        assertArrayEquals(row, res.value());
091        return;
092      }
093    }
094    fail("Waited too much time for put replication");
095  }
096}