View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.replication;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.hbase.ClusterStatus;
28  import org.apache.hadoop.hbase.ServerLoad;
29  import org.apache.hadoop.hbase.ServerName;
30  import org.apache.hadoop.hbase.client.Admin;
31  import org.apache.hadoop.hbase.client.Put;
32  import org.apache.hadoop.hbase.testclassification.MediumTests;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  @Category(MediumTests.class)
38  public class TestReplicationStatus extends TestReplicationBase {
39    private static final Log LOG = LogFactory.getLog(TestReplicationStatus.class);
40    private static final String PEER_ID = "2";
41  
42    /**
43     * Test for HBASE-9531
44     * put a few rows into htable1, which should be replicated to htable2
45     * create a ClusterStatus instance 'status' from HBaseAdmin
46     * test : status.getLoad(server).getReplicationLoadSourceList()
47     * test : status.getLoad(server).getReplicationLoadSink()
48     * * @throws Exception
49     */
50    @Test(timeout = 300000)
51    public void testReplicationStatus() throws Exception {
52      LOG.info("testReplicationStatus");
53  
54      try (Admin hbaseAdmin = utility1.getConnection().getAdmin()) {
55        // disable peer
56        admin.disablePeer(PEER_ID);
57  
58        final byte[] qualName = Bytes.toBytes("q");
59        Put p;
60  
61        for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
62          p = new Put(Bytes.toBytes("row" + i));
63          p.add(famName, qualName, Bytes.toBytes("val" + i));
64          htable1.put(p);
65        }
66  
67        ClusterStatus status = hbaseAdmin.getClusterStatus();
68  
69        for (ServerName server : status.getServers()) {
70          ServerLoad sl = status.getLoad(server);
71          List<ReplicationLoadSource> rLoadSourceList = sl.getReplicationLoadSourceList();
72          ReplicationLoadSink rLoadSink = sl.getReplicationLoadSink();
73  
74          // check SourceList only has one entry, beacuse only has one peer
75          assertTrue("failed to get ReplicationLoadSourceList", (rLoadSourceList.size() == 1));
76          assertEquals(PEER_ID, rLoadSourceList.get(0).getPeerID());
77  
78          // check Sink exist only as it is difficult to verify the value on the fly
79          assertTrue("failed to get ReplicationLoadSink.AgeOfLastShippedOp ",
80            (rLoadSink.getAgeOfLastAppliedOp() >= 0));
81          assertTrue("failed to get ReplicationLoadSink.TimeStampsOfLastAppliedOp ",
82            (rLoadSink.getTimeStampsOfLastAppliedOp() >= 0));
83        }
84  
85        // Stop rs1, then the queue of rs1 will be transfered to rs0
86        utility1.getHBaseCluster().getRegionServer(1).stop("Stop RegionServer");
87        Thread.sleep(10000);
88        status = hbaseAdmin.getClusterStatus();
89        ServerName server = utility1.getHBaseCluster().getRegionServer(0).getServerName();
90        ServerLoad sl = status.getLoad(server);
91        List<ReplicationLoadSource> rLoadSourceList = sl.getReplicationLoadSourceList();
92        // check SourceList still only has one entry
93        assertTrue("failed to get ReplicationLoadSourceList", (rLoadSourceList.size() == 1));
94        assertEquals(PEER_ID, rLoadSourceList.get(0).getPeerID());
95      } finally {
96        admin.enablePeer(PEER_ID);
97        utility1.getHBaseCluster().getRegionServer(1).start();
98      }
99    }
100 }