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.HBaseTestingUtil; 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 @Override 048 protected String getClusterKey(HBaseTestingUtil util) throws Exception { 049 // in this test we will restart the peer cluster, and the master address will be changed, so we 050 // need to use zk based connection uri 051 return util.getZkConnectionURI(); 052 } 053 054 /** 055 * Test disabling an inactive peer. Add a peer which is inactive, trying to insert, disable the 056 * peer, then activate the peer and make sure nothing is replicated. In Addition, enable the peer 057 * and check the updates are replicated. 058 */ 059 @Test 060 public void testDisableInactivePeer() throws Exception { 061 UTIL2.shutdownMiniHBaseCluster(); 062 063 byte[] rowkey = Bytes.toBytes("disable inactive peer"); 064 Put put = new Put(rowkey); 065 put.addColumn(famName, row, row); 066 htable1.put(put); 067 068 // wait for the sleep interval of the master cluster to become long 069 Thread.sleep(SLEEP_TIME * NB_RETRIES); 070 071 // disable and start the peer 072 hbaseAdmin.disableReplicationPeer("2"); 073 restartTargetHBaseCluster(2); 074 Get get = new Get(rowkey); 075 for (int i = 0; i < NB_RETRIES; i++) { 076 Result res = htable2.get(get); 077 if (res.size() >= 1) { 078 fail("Replication wasn't disabled"); 079 } else { 080 LOG.info("Row not replicated, let's wait a bit more..."); 081 Thread.sleep(SLEEP_TIME); 082 } 083 } 084 085 // Test enable replication 086 hbaseAdmin.enableReplicationPeer("2"); 087 // wait since the sleep interval would be long 088 Thread.sleep(SLEEP_TIME * NB_RETRIES); 089 for (int i = 0; i < NB_RETRIES; i++) { 090 Result res = htable2.get(get); 091 if (res.isEmpty()) { 092 LOG.info("Row not available"); 093 Thread.sleep(SLEEP_TIME * NB_RETRIES); 094 } else { 095 assertArrayEquals(row, res.value()); 096 return; 097 } 098 } 099 fail("Waited too much time for put replication"); 100 } 101}