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 java.util.Map; 021import org.apache.hadoop.fs.Path; 022import org.apache.hadoop.hbase.HBaseClassTestRule; 023import org.apache.hadoop.hbase.client.Admin; 024import org.apache.hadoop.hbase.client.Get; 025import org.apache.hadoop.hbase.client.Put; 026import org.apache.hadoop.hbase.regionserver.HRegionServer; 027import org.apache.hadoop.hbase.replication.regionserver.ReplicationStatus; 028import org.apache.hadoop.hbase.testclassification.MediumTests; 029import org.apache.hadoop.hbase.testclassification.ReplicationTests; 030import org.apache.hadoop.hbase.util.Bytes; 031import org.junit.Assert; 032import org.junit.ClassRule; 033import org.junit.Test; 034import org.junit.experimental.categories.Category; 035 036@Category({ ReplicationTests.class, MediumTests.class }) 037public class TestReplicationMetricsforUI extends TestReplicationBase { 038 @ClassRule 039 public static final HBaseClassTestRule CLASS_RULE = 040 HBaseClassTestRule.forClass(TestReplicationMetricsforUI.class); 041 private static final byte[] qualName = Bytes.toBytes("q"); 042 043 @Test 044 public void testReplicationMetrics() throws Exception { 045 try (Admin hbaseAdmin = UTIL1.getConnection().getAdmin()) { 046 Put p = new Put(Bytes.toBytes("starter")); 047 p.addColumn(famName, qualName, Bytes.toBytes("value help to test replication delay")); 048 htable1.put(p); 049 // make sure replication done 050 while (htable2.get(new Get(Bytes.toBytes("starter"))).size() == 0) { 051 Thread.sleep(500); 052 } 053 // sleep 5 seconds to make sure timePassedAfterLastShippedOp > 2 * ageOfLastShippedOp 054 Thread.sleep(5000); 055 HRegionServer rs = UTIL1.getRSForFirstRegionInTable(tableName); 056 Map<String, ReplicationStatus> metrics = rs.getWalGroupsReplicationStatus(); 057 Assert.assertEquals("metric size ", 1, metrics.size()); 058 long lastPosition = 0; 059 for (Map.Entry<String, ReplicationStatus> metric : metrics.entrySet()) { 060 Assert.assertEquals("peerId", PEER_ID2, metric.getValue().getPeerId()); 061 Assert.assertEquals("queue length", 1, metric.getValue().getQueueSize()); 062 Assert.assertEquals("replication delay", 0, metric.getValue().getReplicationDelay()); 063 Assert.assertTrue("current position >= 0", metric.getValue().getCurrentPosition() >= 0); 064 lastPosition = metric.getValue().getCurrentPosition(); 065 } 066 for (int i = 0; i < NB_ROWS_IN_BATCH; i++) { 067 p = new Put(Bytes.toBytes("" + Integer.toString(i))); 068 p.addColumn(famName, qualName, Bytes.toBytes("value help to test replication delay " + i)); 069 htable1.put(p); 070 } 071 while ( 072 htable2.get(new Get(Bytes.toBytes("" + Integer.toString(NB_ROWS_IN_BATCH - 1)))).size() == 0 073 ) { 074 Thread.sleep(500); 075 } 076 rs = UTIL1.getRSForFirstRegionInTable(tableName); 077 metrics = rs.getWalGroupsReplicationStatus(); 078 Path lastPath = null; 079 for (Map.Entry<String, ReplicationStatus> metric : metrics.entrySet()) { 080 lastPath = metric.getValue().getCurrentPath(); 081 Assert.assertEquals("peerId", PEER_ID2, metric.getValue().getPeerId()); 082 Assert.assertTrue("age of Last Shipped Op should be > 0 ", 083 metric.getValue().getAgeOfLastShippedOp() > 0); 084 Assert.assertTrue("current position should > last position", 085 metric.getValue().getCurrentPosition() - lastPosition > 0); 086 lastPosition = metric.getValue().getCurrentPosition(); 087 } 088 089 hbaseAdmin.rollWALWriter(rs.getServerName()); 090 p = new Put(Bytes.toBytes("trigger")); 091 p.addColumn(famName, qualName, Bytes.toBytes("value help to test replication delay")); 092 htable1.put(p); 093 // make sure replication rolled to a new log 094 while (htable2.get(new Get(Bytes.toBytes("trigger"))).size() == 0) { 095 Thread.sleep(500); 096 } 097 // sleep 5 seconds to make sure timePassedAfterLastShippedOp > 2 * ageOfLastShippedOp 098 Thread.sleep(5000); 099 metrics = rs.getWalGroupsReplicationStatus(); 100 for (Map.Entry<String, ReplicationStatus> metric : metrics.entrySet()) { 101 Assert.assertEquals("replication delay", 0, metric.getValue().getReplicationDelay()); 102 Assert.assertTrue("current position should < last position", 103 metric.getValue().getCurrentPosition() < lastPosition); 104 Assert.assertNotEquals("current path", lastPath, metric.getValue().getCurrentPath()); 105 } 106 } 107 } 108}