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.storefiletracker;
019
020import static org.junit.Assert.assertThrows;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
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.snapshot.RestoreSnapshotException;
030import org.apache.hadoop.hbase.testclassification.RegionServerTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037@Category({ RegionServerTests.class, SmallTests.class })
038public class TestStoreFileTrackerValidationUtils {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestStoreFileTrackerValidationUtils.class);
043
044  @Test
045  public void testCheckSFTCompatibility() throws Exception {
046    // checking default value change on different configuration levels
047    Configuration conf = new Configuration();
048    conf.set(StoreFileTrackerFactory.TRACKER_IMPL, "DEFAULT");
049
050    // creating a TD with only TableDescriptor level config
051    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(TableName.valueOf("TableX"));
052    builder.setValue(StoreFileTrackerFactory.TRACKER_IMPL, "FILE");
053    ColumnFamilyDescriptor cf = ColumnFamilyDescriptorBuilder.of("cf");
054    builder.setColumnFamily(cf);
055    TableDescriptor td = builder.build();
056
057    // creating a TD with matching ColumnFamilyDescriptor level setting
058    TableDescriptorBuilder snapBuilder =
059      TableDescriptorBuilder.newBuilder(TableName.valueOf("TableY"));
060    snapBuilder.setValue(StoreFileTrackerFactory.TRACKER_IMPL, "FILE");
061    ColumnFamilyDescriptorBuilder snapCFBuilder =
062      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf"));
063    snapCFBuilder.setValue(StoreFileTrackerFactory.TRACKER_IMPL, "FILE");
064    snapBuilder.setColumnFamily(snapCFBuilder.build());
065    TableDescriptor snapTd = snapBuilder.build();
066
067    // adding a cf config that matches the td config is fine even when it does not match the default
068    StoreFileTrackerValidationUtils.validatePreRestoreSnapshot(td, snapTd, conf);
069    // removing cf level config is fine when it matches the td config
070    StoreFileTrackerValidationUtils.validatePreRestoreSnapshot(snapTd, td, conf);
071
072    TableDescriptorBuilder defaultBuilder =
073      TableDescriptorBuilder.newBuilder(TableName.valueOf("TableY"));
074    defaultBuilder.setValue(StoreFileTrackerFactory.TRACKER_IMPL, "FILE");
075    ColumnFamilyDescriptorBuilder defaultCFBuilder =
076      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf"));
077    defaultCFBuilder.setValue(StoreFileTrackerFactory.TRACKER_IMPL, "DEFAULT");
078    defaultBuilder.setColumnFamily(defaultCFBuilder.build());
079    TableDescriptor defaultTd = defaultBuilder.build();
080
081    assertThrows(RestoreSnapshotException.class, () -> {
082      StoreFileTrackerValidationUtils.validatePreRestoreSnapshot(td, defaultTd, conf);
083    });
084    assertThrows(RestoreSnapshotException.class, () -> {
085      StoreFileTrackerValidationUtils.validatePreRestoreSnapshot(snapTd, defaultTd, conf);
086    });
087  }
088}