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.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.util.EnumSet;
024import java.util.List;
025import org.apache.hadoop.hbase.ClusterMetrics.Option;
026import org.apache.hadoop.hbase.ClusterStatus;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.ServerLoad;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.client.Admin;
031import org.apache.hadoop.hbase.client.Put;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.testclassification.ReplicationTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.hadoop.hbase.util.JVMClusterUtil;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042@Category({ReplicationTests.class, MediumTests.class})
043public class TestReplicationStatus extends TestReplicationBase {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047      HBaseClassTestRule.forClass(TestReplicationStatus.class);
048
049  private static final Logger LOG = LoggerFactory.getLogger(TestReplicationStatus.class);
050  private static final String PEER_ID = "2";
051
052  /**
053   * Test for HBASE-9531
054   * put a few rows into htable1, which should be replicated to htable2
055   * create a ClusterStatus instance 'status' from HBaseAdmin
056   * test : status.getLoad(server).getReplicationLoadSourceList()
057   * test : status.getLoad(server).getReplicationLoadSink()
058   * * @throws Exception
059   */
060  @Test
061  public void testReplicationStatus() throws Exception {
062    LOG.info("testReplicationStatus");
063
064    try (Admin hbaseAdmin = utility1.getConnection().getAdmin()) {
065      // disable peer
066      admin.disablePeer(PEER_ID);
067
068      final byte[] qualName = Bytes.toBytes("q");
069      Put p;
070
071      for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
072        p = new Put(Bytes.toBytes("row" + i));
073        p.addColumn(famName, qualName, Bytes.toBytes("val" + i));
074        htable1.put(p);
075      }
076
077      ClusterStatus status = new ClusterStatus(hbaseAdmin.getClusterMetrics(
078        EnumSet.of(Option.LIVE_SERVERS)));
079
080      for (JVMClusterUtil.RegionServerThread thread : utility1.getHBaseCluster()
081          .getRegionServerThreads()) {
082        ServerName server = thread.getRegionServer().getServerName();
083        ServerLoad sl = status.getLoad(server);
084        List<ReplicationLoadSource> rLoadSourceList = sl.getReplicationLoadSourceList();
085        ReplicationLoadSink rLoadSink = sl.getReplicationLoadSink();
086
087        // check SourceList only has one entry, beacuse only has one peer
088        assertTrue("failed to get ReplicationLoadSourceList", (rLoadSourceList.size() == 1));
089        assertEquals(PEER_ID, rLoadSourceList.get(0).getPeerID());
090
091        // check Sink exist only as it is difficult to verify the value on the fly
092        assertTrue("failed to get ReplicationLoadSink.AgeOfLastShippedOp ",
093          (rLoadSink.getAgeOfLastAppliedOp() >= 0));
094        assertTrue("failed to get ReplicationLoadSink.TimeStampsOfLastAppliedOp ",
095          (rLoadSink.getTimestampsOfLastAppliedOp() >= 0));
096      }
097
098      // Stop rs1, then the queue of rs1 will be transfered to rs0
099      utility1.getHBaseCluster().getRegionServer(1).stop("Stop RegionServer");
100      Thread.sleep(10000);
101      status = new ClusterStatus(hbaseAdmin.getClusterMetrics(EnumSet.of(Option.LIVE_SERVERS)));
102      ServerName server = utility1.getHBaseCluster().getRegionServer(0).getServerName();
103      ServerLoad sl = status.getLoad(server);
104      List<ReplicationLoadSource> rLoadSourceList = sl.getReplicationLoadSourceList();
105      // check SourceList still only has one entry
106      assertTrue("failed to get ReplicationLoadSourceList", (rLoadSourceList.size() == 1));
107      assertEquals(PEER_ID, rLoadSourceList.get(0).getPeerID());
108    } finally {
109      admin.enablePeer(PEER_ID);
110      utility1.getHBaseCluster().getRegionServer(1).start();
111    }
112  }
113}