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.master.procedure;
019
020import static org.junit.Assert.assertEquals;
021import static org.mockito.Mockito.mock;
022import static org.mockito.Mockito.when;
023
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
029import org.apache.hadoop.hbase.client.TableDescriptor;
030import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
031import org.apache.hadoop.hbase.testclassification.MasterTests;
032import org.apache.hadoop.hbase.testclassification.SmallTests;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037@Category({ MasterTests.class, SmallTests.class })
038public class TestRecoverySnapshotUtils {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042    HBaseClassTestRule.forClass(TestRecoverySnapshotUtils.class);
043
044  @Test
045  public void testRecoverySnapshotTtlNoDescriptor() {
046    // Create a mock MasterProcedureEnv with a known site configuration TTL
047    long siteLevelTtl = 7200; // 2 hours
048    Configuration conf = new Configuration();
049    conf.setLong(HConstants.SNAPSHOT_BEFORE_DESTRUCTIVE_ACTION_TTL_KEY, siteLevelTtl);
050
051    MasterProcedureEnv env = mock(MasterProcedureEnv.class);
052    when(env.getMasterConfiguration()).thenReturn(conf);
053
054    // Test with null table descriptor - should return site configuration
055    long actualTtl = RecoverySnapshotUtils.getRecoverySnapshotTtl(env, null);
056    assertEquals("Should return site-level TTL when no table descriptor provided", siteLevelTtl,
057      actualTtl);
058  }
059
060  @Test
061  public void testRecoverySnapshotTtlWithDescriptor() {
062    // Create a mock MasterProcedureEnv with a known site configuration TTL
063    long siteLevelTtl = 7200; // 2 hours
064    Configuration conf = new Configuration();
065    conf.setLong(HConstants.SNAPSHOT_BEFORE_DESTRUCTIVE_ACTION_TTL_KEY, siteLevelTtl);
066
067    MasterProcedureEnv env = mock(MasterProcedureEnv.class);
068    when(env.getMasterConfiguration()).thenReturn(conf);
069
070    // Create a table descriptor with a different TTL override
071    long tableLevelTtl = 3600; // 1 hour
072    TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(TableName.valueOf("test"))
073      .setColumnFamily(ColumnFamilyDescriptorBuilder.of("cf"))
074      .setValue(HConstants.TABLE_RECOVERY_SNAPSHOT_TTL_KEY, String.valueOf(tableLevelTtl)).build();
075
076    // Test with table descriptor override - should return table-level TTL
077    long actualTtl = RecoverySnapshotUtils.getRecoverySnapshotTtl(env, tableDescriptor);
078    assertEquals("Should return table-level TTL when table descriptor provides override",
079      tableLevelTtl, actualTtl);
080  }
081
082  @Test
083  public void testRecoverySnapshotTtlUsesDefault() {
084    // Create a mock MasterProcedureEnv with default configuration (no explicit TTL set)
085    Configuration conf = new Configuration();
086    // Don't set the TTL key, so it should use the default
087
088    MasterProcedureEnv env = mock(MasterProcedureEnv.class);
089    when(env.getMasterConfiguration()).thenReturn(conf);
090
091    // Test with null table descriptor - should return default TTL
092    long actualTtl = RecoverySnapshotUtils.getRecoverySnapshotTtl(env, null);
093    assertEquals("Should return default TTL when no site configuration provided",
094      HConstants.DEFAULT_SNAPSHOT_BEFORE_DESTRUCTIVE_ACTION_TTL, actualTtl);
095  }
096}