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