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.hbase.TableName;
025import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
026import org.apache.hadoop.hbase.client.TableDescriptor;
027import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
028import org.apache.hadoop.hbase.io.compress.Compression;
029import org.apache.hadoop.hbase.testclassification.RegionServerTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.junit.jupiter.api.Tag;
032import org.junit.jupiter.api.Test;
033
034@Tag(RegionServerTests.TAG)
035@Tag(SmallTests.TAG)
036public class TestTableDescriptorHashComputation {
037
038  @Test
039  public void testHashLength() {
040    TableDescriptor td = TableDescriptorBuilder.newBuilder(TableName.valueOf("testTable"))
041      .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf")).build();
042
043    String hash = td.getDescriptorHash();
044    assertNotNull(hash);
045    assertEquals(8, hash.length());
046  }
047
048  @Test
049  public void testIdenticalDescriptorsProduceSameHash() {
050    TableDescriptor td1 = TableDescriptorBuilder.newBuilder(TableName.valueOf("testTable"))
051      .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf")).build();
052
053    TableDescriptor td2 = TableDescriptorBuilder.newBuilder(TableName.valueOf("testTable"))
054      .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf")).build();
055
056    String hash1 = td1.getDescriptorHash();
057    String hash2 = td2.getDescriptorHash();
058
059    assertEquals(hash1, hash2);
060  }
061
062  @Test
063  public void testDifferentDescriptorsProduceDifferentHashes() {
064    TableDescriptor td1 = TableDescriptorBuilder.newBuilder(TableName.valueOf("testTable"))
065      .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf")).build();
066
067    TableDescriptor td2 = TableDescriptorBuilder.newBuilder(TableName.valueOf("testTable"))
068      .setColumnFamily(
069        ColumnFamilyDescriptorBuilder.newBuilder("cf".getBytes()).setTimeToLive(86400).build())
070      .build();
071
072    String hash1 = td1.getDescriptorHash();
073    String hash2 = td2.getDescriptorHash();
074
075    assertNotEquals(hash1, hash2);
076  }
077
078  @Test
079  public void testDifferentCompressionProducesDifferentHash() {
080    TableDescriptor td1 = TableDescriptorBuilder
081      .newBuilder(TableName.valueOf("testTable")).setColumnFamily(ColumnFamilyDescriptorBuilder
082        .newBuilder("cf".getBytes()).setCompressionType(Compression.Algorithm.NONE).build())
083      .build();
084
085    TableDescriptor td2 = TableDescriptorBuilder
086      .newBuilder(TableName.valueOf("testTable")).setColumnFamily(ColumnFamilyDescriptorBuilder
087        .newBuilder("cf".getBytes()).setCompressionType(Compression.Algorithm.SNAPPY).build())
088      .build();
089
090    String hash1 = td1.getDescriptorHash();
091    String hash2 = td2.getDescriptorHash();
092
093    assertNotEquals(hash1, hash2);
094  }
095
096  @Test
097  public void testMultipleColumnFamilies() {
098    TableDescriptor td1 = TableDescriptorBuilder.newBuilder(TableName.valueOf("testTable"))
099      .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf1"))
100      .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf2")).build();
101
102    TableDescriptor td2 = TableDescriptorBuilder.newBuilder(TableName.valueOf("testTable"))
103      .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf1")).build();
104
105    String hash1 = td1.getDescriptorHash();
106    String hash2 = td2.getDescriptorHash();
107
108    assertNotEquals(hash1, hash2);
109  }
110
111  @Test
112  public void testHashCaching() {
113    TableDescriptor td = TableDescriptorBuilder.newBuilder(TableName.valueOf("testTable"))
114      .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf")).build();
115
116    String hash1 = td.getDescriptorHash();
117    String hash2 = td.getDescriptorHash();
118
119    assertNotNull(hash1);
120    assertEquals(hash1, hash2);
121  }
122}