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.regionserver; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertNotEquals; 022import static org.junit.jupiter.api.Assertions.assertNotNull; 023 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.fs.Path; 026import org.apache.hadoop.hbase.HBaseConfiguration; 027import org.apache.hadoop.hbase.HBaseTestingUtil; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 030import org.apache.hadoop.hbase.client.RegionInfo; 031import org.apache.hadoop.hbase.client.RegionInfoBuilder; 032import org.apache.hadoop.hbase.client.TableDescriptor; 033import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 034import org.apache.hadoop.hbase.testclassification.RegionServerTests; 035import org.apache.hadoop.hbase.testclassification.SmallTests; 036import org.apache.hadoop.hbase.util.Bytes; 037import org.junit.jupiter.api.AfterEach; 038import org.junit.jupiter.api.BeforeEach; 039import org.junit.jupiter.api.Tag; 040import org.junit.jupiter.api.Test; 041 042@Tag(RegionServerTests.TAG) 043@Tag(SmallTests.TAG) 044public class TestMetricsRegionWrapperTableDescriptorHash { 045 046 private HBaseTestingUtil testUtil; 047 private Configuration conf; 048 049 @BeforeEach 050 public void setUp() throws Exception { 051 conf = HBaseConfiguration.create(); 052 testUtil = new HBaseTestingUtil(conf); 053 } 054 055 @AfterEach 056 public void tearDown() throws Exception { 057 if (testUtil != null) { 058 testUtil.cleanupTestDir(); 059 } 060 } 061 062 @Test 063 public void testTableDescriptorHashGeneration() throws Exception { 064 TableName tableName = TableName.valueOf("testTable"); 065 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName) 066 .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf")).build(); 067 068 RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).setStartKey(Bytes.toBytes("a")) 069 .setEndKey(Bytes.toBytes("z")).build(); 070 071 Path testDir = testUtil.getDataTestDir("testTableDescriptorHashGeneration"); 072 HRegion region = 073 HBaseTestingUtil.createRegionAndWAL(regionInfo, testDir, conf, tableDescriptor); 074 075 try (MetricsRegionWrapperImpl wrapper = new MetricsRegionWrapperImpl(region)) { 076 String hash = wrapper.getTableDescriptorHash(); 077 assertNotNull(hash); 078 assertNotEquals("unknown", hash); 079 assertEquals(8, hash.length()); 080 } finally { 081 HBaseTestingUtil.closeRegionAndWAL(region); 082 } 083 } 084 085 @Test 086 public void testHashConsistency() throws Exception { 087 TableName tableName = TableName.valueOf("testTable2"); 088 TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(tableName) 089 .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf")).build(); 090 091 RegionInfo regionInfo1 = RegionInfoBuilder.newBuilder(tableName).setStartKey(Bytes.toBytes("a")) 092 .setEndKey(Bytes.toBytes("m")).build(); 093 RegionInfo regionInfo2 = RegionInfoBuilder.newBuilder(tableName).setStartKey(Bytes.toBytes("m")) 094 .setEndKey(Bytes.toBytes("z")).build(); 095 096 Path testDir1 = testUtil.getDataTestDir("testHashConsistency1"); 097 HRegion region1 = 098 HBaseTestingUtil.createRegionAndWAL(regionInfo1, testDir1, conf, tableDescriptor); 099 100 Path testDir2 = testUtil.getDataTestDir("testHashConsistency2"); 101 HRegion region2 = 102 HBaseTestingUtil.createRegionAndWAL(regionInfo2, testDir2, conf, tableDescriptor); 103 try (MetricsRegionWrapperImpl wrapper1 = new MetricsRegionWrapperImpl(region1); 104 MetricsRegionWrapperImpl wrapper2 = new MetricsRegionWrapperImpl(region2)) { 105 106 String hash1 = wrapper1.getTableDescriptorHash(); 107 String hash2 = wrapper2.getTableDescriptorHash(); 108 109 assertEquals(hash1, hash2); 110 } finally { 111 HBaseTestingUtil.closeRegionAndWAL(region1); 112 HBaseTestingUtil.closeRegionAndWAL(region2); 113 } 114 } 115 116 @Test 117 public void testHashChangeOnDescriptorChange() throws Exception { 118 TableName tableName = TableName.valueOf("testTable3"); 119 TableDescriptor tableDescriptor1 = TableDescriptorBuilder.newBuilder(tableName) 120 .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf")).build(); 121 TableDescriptor tableDescriptor2 = TableDescriptorBuilder.newBuilder(tableName) 122 .setColumnFamily( 123 ColumnFamilyDescriptorBuilder.newBuilder("cf".getBytes()).setTimeToLive(86400).build()) 124 .build(); 125 126 RegionInfo regionInfo1 = RegionInfoBuilder.newBuilder(tableName).setStartKey(Bytes.toBytes("a")) 127 .setEndKey(Bytes.toBytes("m")).build(); 128 RegionInfo regionInfo2 = RegionInfoBuilder.newBuilder(tableName).setStartKey(Bytes.toBytes("m")) 129 .setEndKey(Bytes.toBytes("z")).build(); 130 131 Path testDir1 = testUtil.getDataTestDir("testHashChangeOnDescriptorChange1"); 132 HRegion region1 = 133 HBaseTestingUtil.createRegionAndWAL(regionInfo1, testDir1, conf, tableDescriptor1); 134 135 Path testDir2 = testUtil.getDataTestDir("testHashChangeOnDescriptorChange2"); 136 HRegion region2 = 137 HBaseTestingUtil.createRegionAndWAL(regionInfo2, testDir2, conf, tableDescriptor2); 138 139 try (MetricsRegionWrapperImpl wrapper1 = new MetricsRegionWrapperImpl(region1); 140 MetricsRegionWrapperImpl wrapper2 = new MetricsRegionWrapperImpl(region2)) { 141 String hash1 = wrapper1.getTableDescriptorHash(); 142 String hash2 = wrapper2.getTableDescriptorHash(); 143 144 assertNotEquals(hash1, hash2); 145 } finally { 146 HBaseTestingUtil.closeRegionAndWAL(region1); 147 HBaseTestingUtil.closeRegionAndWAL(region2); 148 } 149 } 150}