1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
46 while (htable2.get(new Get(Bytes.toBytes("starter"))).size() == 0) {
47 Thread.sleep(500);
48 }
49
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
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
91 while (htable2.get(new Get(Bytes.toBytes("trigger"))).size() == 0) {
92 Thread.sleep(500);
93 }
94
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 }