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