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 java.util.Map;
21  import org.apache.hadoop.fs.Path;
22  import org.apache.hadoop.hbase.client.Admin;
23  import org.apache.hadoop.hbase.client.Get;
24  import org.apache.hadoop.hbase.client.Put;
25  import org.apache.hadoop.hbase.regionserver.HRegionServer;
26  import org.apache.hadoop.hbase.replication.regionserver.ReplicationStatus;
27  import org.apache.hadoop.hbase.testclassification.MediumTests;
28  import org.apache.hadoop.hbase.testclassification.ReplicationTests;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.junit.Assert;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  @Category({ ReplicationTests.class, MediumTests.class })
35  public class TestReplicationMetricsforUI extends TestReplicationBase {
36  
37    private static final byte[] qualName = Bytes.toBytes("q");
38  
39    @Test
40    public void testReplicationMetrics() throws Exception {
41      try (Admin hbaseAdmin = utility1.getConnection().getAdmin()) {
42        Put p = new Put(Bytes.toBytes("starter"));
43        p.addColumn(famName, qualName, Bytes.toBytes("value help to test replication delay"));
44        htable1.put(p);
45        // make sure replication done
46        while (htable2.get(new Get(Bytes.toBytes("starter"))).size() == 0) {
47          Thread.sleep(500);
48        }
49        // sleep 5 seconds to make sure timePassedAfterLastShippedOp > 2 * ageOfLastShippedOp
50        Thread.sleep(5000);
51        HRegionServer rs = utility1.getRSForFirstRegionInTable(tableName);
52        Map<String, ReplicationStatus> metrics = rs.getWalGroupsReplicationStatus();
53        Assert.assertEquals("metric size ", 1, metrics.size());
54        for (Map.Entry<String, ReplicationStatus> metric : metrics.entrySet()) {
55          Assert.assertEquals("peerId", PEER_ID2, metric.getValue().getPeerId());
56          Assert.assertEquals("queue length", 1, metric.getValue().getQueueSize());
57          Assert.assertEquals("replication delay", 0, metric.getValue().getReplicationDelay());
58          long pos = metric.getValue().getCurrentPosition();
59          // Semantics are a bit different in branch-1: If not started, pos will be -1
60          if (pos == -1) {
61            pos = 0;
62          }
63          Assert.assertTrue("current position should be >= 0, is " + pos, pos >= 0);
64        }
65        for (int i = 0; i < NB_ROWS_IN_BATCH; i++) {
66          p = new Put(Bytes.toBytes("" + Integer.toString(i)));
67          p.addColumn(famName, qualName, Bytes.toBytes("value help to test replication delay " + i));
68          htable1.put(p);
69        }
70        while (htable2.get(new Get(Bytes.toBytes("" + Integer.toString(NB_ROWS_IN_BATCH - 1))))
71            .size() == 0) {
72          Thread.sleep(500);
73        }
74        rs = utility1.getRSForFirstRegionInTable(tableName);
75        metrics = rs.getWalGroupsReplicationStatus();
76        Path lastPath = null;
77        for (Map.Entry<String, ReplicationStatus> metric : metrics.entrySet()) {
78          lastPath = metric.getValue().getCurrentPath();
79          Assert.assertEquals("peerId", PEER_ID2, metric.getValue().getPeerId());
80          Assert.assertTrue("age of Last Shipped Op should be > 0 ",
81            metric.getValue().getAgeOfLastShippedOp() > 0);
82          long pos = metric.getValue().getCurrentPosition();
83          Assert.assertTrue("current position should be >= 0, is " + pos, pos >= 0);
84        }
85  
86        hbaseAdmin.rollWALWriter(rs.getServerName());
87        p = new Put(Bytes.toBytes("trigger"));
88        p.addColumn(famName, qualName, Bytes.toBytes("value help to test replication delay"));
89        htable1.put(p);
90        // make sure replication rolled to a new log
91        while (htable2.get(new Get(Bytes.toBytes("trigger"))).size() == 0) {
92          Thread.sleep(500);
93        }
94        // sleep 5 seconds to make sure timePassedAfterLastShippedOp > 2 * ageOfLastShippedOp
95        Thread.sleep(5000);
96        metrics = rs.getWalGroupsReplicationStatus();
97        for (Map.Entry<String, ReplicationStatus> metric : metrics.entrySet()) {
98          Assert.assertEquals("replication delay", 0, metric.getValue().getReplicationDelay());
99          long pos = metric.getValue().getCurrentPosition();
100         Assert.assertTrue("current position should be >= 0, is " + pos, pos >= 0);
101         Assert.assertNotEquals("current path", lastPath, metric.getValue().getCurrentPath());
102       }
103     }
104   }
105 }