001/* 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase.replication; 020 021import static org.junit.Assert.assertArrayEquals; 022import static org.junit.Assert.fail; 023 024import org.apache.hadoop.hbase.HBaseClassTestRule; 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 058 // enabling and shutdown the peer 059 admin.enablePeer("2"); 060 utility2.shutdownMiniHBaseCluster(); 061 062 byte[] rowkey = Bytes.toBytes("disable inactive peer"); 063 Put put = new Put(rowkey); 064 put.addColumn(famName, row, row); 065 htable1.put(put); 066 067 // wait for the sleep interval of the master cluster to become long 068 Thread.sleep(SLEEP_TIME * NB_RETRIES); 069 070 // disable and start the peer 071 admin.disablePeer("2"); 072 utility2.startMiniHBaseCluster(1, 2); 073 Get get = new Get(rowkey); 074 for (int i = 0; i < NB_RETRIES; i++) { 075 Result res = htable2.get(get); 076 if (res.size() >= 1) { 077 fail("Replication wasn't disabled"); 078 } else { 079 LOG.info("Row not replicated, let's wait a bit more..."); 080 Thread.sleep(SLEEP_TIME); 081 } 082 } 083 084 // Test enable replication 085 admin.enablePeer("2"); 086 // wait since the sleep interval would be long 087 Thread.sleep(SLEEP_TIME * NB_RETRIES); 088 for (int i = 0; i < NB_RETRIES; i++) { 089 Result res = htable2.get(get); 090 if (res.isEmpty()) { 091 LOG.info("Row not available"); 092 Thread.sleep(SLEEP_TIME * NB_RETRIES); 093 } else { 094 assertArrayEquals(row, res.value()); 095 return; 096 } 097 } 098 fail("Waited too much time for put replication"); 099 } 100}