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.master; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertInstanceOf; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023 024import org.apache.hadoop.hbase.HBaseTestingUtil; 025import org.apache.hadoop.hbase.ServerName; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.client.RegionInfo; 028import org.apache.hadoop.hbase.client.Table; 029import org.apache.hadoop.hbase.master.assignment.AssignmentTestingUtil; 030import org.apache.hadoop.hbase.testclassification.MasterTests; 031import org.apache.hadoop.hbase.testclassification.MediumTests; 032import org.apache.hadoop.hbase.util.Bytes; 033import org.apache.hadoop.metrics2.AbstractMetric; 034import org.apache.hadoop.metrics2.MetricsRecord; 035import org.apache.hadoop.metrics2.MetricsSource; 036import org.apache.hadoop.metrics2.impl.MetricsCollectorImpl; 037import org.junit.jupiter.api.AfterAll; 038import org.junit.jupiter.api.BeforeAll; 039import org.junit.jupiter.api.Tag; 040import org.junit.jupiter.api.Test; 041import org.junit.jupiter.api.TestInfo; 042 043@Tag(MasterTests.TAG) 044@Tag(MediumTests.TAG) 045public class TestAssignmentManagerRitDurationMetrics { 046 047 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 048 private static final byte[] FAMILY = Bytes.toBytes("family"); 049 private static final long WAIT_TIMEOUT_MS = 10_000L; 050 051 private static HMaster MASTER; 052 private static final String RIT_DURATION_NUM_OPS_METRIC = "RitDuration_num_ops"; 053 054 @BeforeAll 055 public static void startCluster() throws Exception { 056 TEST_UTIL.startMiniCluster(2); 057 MASTER = TEST_UTIL.getMiniHBaseCluster().getMaster(); 058 } 059 060 @AfterAll 061 public static void after() throws Exception { 062 TEST_UTIL.shutdownMiniCluster(); 063 } 064 065 @Test 066 public void testRitDurationHistogramMetric(TestInfo testInfo) throws Exception { 067 TableName tableName = TableName.valueOf(testInfo.getTestMethod().orElseThrow().getName()); 068 try (Table table = TEST_UTIL.createTable(tableName, FAMILY)) { 069 RegionInfo regionInfo = 070 MASTER.getAssignmentManager().getRegionStates().getRegionsOfTable(tableName).get(0); 071 TEST_UTIL.waitFor(WAIT_TIMEOUT_MS, 072 () -> !AssignmentTestingUtil.isRegionInTransition(regionInfo, MASTER.getAssignmentManager()) 073 && MASTER.getAssignmentManager().getRegionStates().getRegionServerOfRegion(regionInfo) 074 != null); 075 076 MetricsAssignmentManagerSource amSource = 077 MASTER.getAssignmentManager().getAssignmentManagerMetrics().getMetricsProcSource(); 078 long ritDurationNumOps = 079 getMetricValue(snapshotMetrics(amSource), RIT_DURATION_NUM_OPS_METRIC); 080 081 ServerName current = 082 MASTER.getAssignmentManager().getRegionStates().getRegionServerOfRegion(regionInfo); 083 ServerName target = MASTER.getServerManager().getOnlineServersList().stream() 084 .filter(sn -> !sn.equals(current)).findFirst() 085 .orElseThrow(() -> new IllegalStateException("Need at least two regionservers")); 086 087 TEST_UTIL.getAdmin().move(regionInfo.getEncodedNameAsBytes(), target); 088 TEST_UTIL.waitFor(WAIT_TIMEOUT_MS, () -> target 089 .equals(MASTER.getAssignmentManager().getRegionStates().getRegionServerOfRegion(regionInfo)) 090 && !AssignmentTestingUtil.isRegionInTransition(regionInfo, MASTER.getAssignmentManager())); 091 092 // num_ops is cumulative (never reset on snapshot); an increase proves the histogram is now 093 // fed on RIT completion. Use >= not ==: background RIT may also add. _max is not asserted -- 094 // snapshot() resets it on every read, racing the metrics2 sampler. 095 long ritDurationNumOpsAfter = 096 getMetricValue(snapshotMetrics(amSource), RIT_DURATION_NUM_OPS_METRIC); 097 assertTrue(ritDurationNumOpsAfter >= ritDurationNumOps + 1, 098 "RitDuration histogram num_ops should increase after a region transition"); 099 } 100 } 101 102 private MetricsRecord snapshotMetrics(MetricsAssignmentManagerSource amSource) { 103 MetricsCollectorImpl collector = new MetricsCollectorImpl(); 104 assertInstanceOf(MetricsSource.class, amSource, 105 "MetricsAssignmentManagerSource should also implement MetricsSource"); 106 ((MetricsSource) amSource).getMetrics(collector, true); 107 assertEquals(1, collector.getRecords().size()); 108 return collector.getRecords().get(0); 109 } 110 111 private long getMetricValue(MetricsRecord record, String metricName) { 112 for (AbstractMetric metric : record.metrics()) { 113 if (metricName.equals(metric.name())) { 114 return metric.value().longValue(); 115 } 116 } 117 throw new AssertionError("Metric not found: " + metricName); 118 } 119}