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 static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertNotEquals; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023 024import java.util.Map; 025import org.apache.hadoop.fs.Path; 026import org.apache.hadoop.hbase.client.Admin; 027import org.apache.hadoop.hbase.client.Get; 028import org.apache.hadoop.hbase.client.Put; 029import org.apache.hadoop.hbase.regionserver.HRegionServer; 030import org.apache.hadoop.hbase.replication.regionserver.ReplicationStatus; 031import org.apache.hadoop.hbase.testclassification.MediumTests; 032import org.apache.hadoop.hbase.testclassification.ReplicationTests; 033import org.apache.hadoop.hbase.util.Bytes; 034import org.junit.jupiter.api.Tag; 035import org.junit.jupiter.api.Test; 036 037@Tag(ReplicationTests.TAG) 038@Tag(MediumTests.TAG) 039public class TestReplicationMetricsforUI extends TestReplicationBase { 040 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 assertEquals(1, metrics.size(), "metric size"); 058 long lastPosition = 0; 059 for (Map.Entry<String, ReplicationStatus> metric : metrics.entrySet()) { 060 assertEquals(PEER_ID2, metric.getValue().getPeerId(), "peerId"); 061 assertEquals(1, metric.getValue().getQueueSize(), "queue length"); 062 assertEquals(0, metric.getValue().getReplicationDelay(), "replication delay"); 063 assertTrue(metric.getValue().getCurrentPosition() >= 0, "current position >= 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 assertEquals(PEER_ID2, metric.getValue().getPeerId(), "peerId"); 082 assertTrue(metric.getValue().getAgeOfLastShippedOp() > 0, 083 "age of Last Shipped Op should be > 0"); 084 assertTrue(metric.getValue().getCurrentPosition() - lastPosition > 0, 085 "current position should > last position"); 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 assertEquals(0, metric.getValue().getReplicationDelay(), "replication delay"); 102 assertTrue(metric.getValue().getCurrentPosition() < lastPosition, 103 "current position should < last position"); 104 assertNotEquals(lastPath, metric.getValue().getCurrentPath(), "current path"); 105 } 106 } 107 } 108}