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