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  
41    /**
42     * Test for HBASE-9531
43     * put a few rows into htable1, which should be replicated to htable2
44     * create a ClusterStatus instance 'status' from HBaseAdmin
45     * test : status.getLoad(server).getReplicationLoadSourceList()
46     * test : status.getLoad(server).getReplicationLoadSink()
47     * * @throws Exception
48     */
49    @Test(timeout = 300000)
50    public void testReplicationStatus() throws Exception {
51      LOG.info("testReplicationStatus");
52  
53      try (Admin hbaseAdmin = utility1.getConnection().getAdmin()) {
54        // disable peer
55        admin.disablePeer(PEER_ID);
56  
57        final byte[] qualName = Bytes.toBytes("q");
58        Put p;
59  
60        for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
61          p = new Put(Bytes.toBytes("row" + i));
62          p.add(famName, qualName, Bytes.toBytes("val" + i));
63          htable1.put(p);
64        }
65  
66        ClusterStatus status = hbaseAdmin.getClusterStatus();
67  
68        for (ServerName server : status.getServers()) {
69          ServerLoad sl = status.getLoad(server);
70          List<ReplicationLoadSource> rLoadSourceList = sl.getReplicationLoadSourceList();
71          ReplicationLoadSink rLoadSink = sl.getReplicationLoadSink();
72  
73          // check SourceList only has one entry, beacuse only has one peer
74          assertTrue("failed to get ReplicationLoadSourceList", (rLoadSourceList.size() == 1));
75          assertEquals(PEER_ID, rLoadSourceList.get(0).getPeerID());
76  
77          // check Sink exist only as it is difficult to verify the value on the fly
78          assertTrue("failed to get ReplicationLoadSink.AgeOfLastShippedOp ",
79            (rLoadSink.getAgeOfLastAppliedOp() >= 0));
80          assertTrue("failed to get ReplicationLoadSink.TimeStampsOfLastAppliedOp ",
81            (rLoadSink.getTimeStampsOfLastAppliedOp() >= 0));
82        }
83  
84        // Stop rs1, then the queue of rs1 will be transfered to rs0
85        utility1.getHBaseCluster().getRegionServer(1).stop("Stop RegionServer");
86        Thread.sleep(10000);
87        status = hbaseAdmin.getClusterStatus();
88        ServerName server = utility1.getHBaseCluster().getRegionServer(0).getServerName();
89        ServerLoad sl = status.getLoad(server);
90        List<ReplicationLoadSource> rLoadSourceList = sl.getReplicationLoadSourceList();
91        // check SourceList still only has one entry
92        assertTrue("failed to get ReplicationLoadSourceList", (rLoadSourceList.size() == 1));
93        assertEquals(PEER_ID, rLoadSourceList.get(0).getPeerID());
94      } finally {
95        admin.enablePeer(PEER_ID);
96        utility1.getHBaseCluster().getRegionServer(1).start();
97      }
98    }
99  }