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