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.snapshot;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.util.HashMap;
024import java.util.Map;
025import java.util.Optional;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.SnapshotType;
028import org.apache.hadoop.hbase.util.AbstractHBaseTool;
029import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
030import org.junit.jupiter.api.TestTemplate;
031
032public class ExportSnapshotMiscTestBase extends ExportSnapshotTestBase {
033
034  protected ExportSnapshotMiscTestBase(boolean mob) {
035    super(mob);
036  }
037
038  @TestTemplate
039  public void testExportWithTargetName() throws Exception {
040    final String targetName = "testExportWithTargetName";
041    testExportFileSystemState(tableName, snapshotName, targetName, tableNumFiles);
042  }
043
044  @TestTemplate
045  public void testExportWithResetTtl() throws Exception {
046    String suffix = mob ? methodName + "-mob" : methodName;
047    TableName tableName = TableName.valueOf(suffix);
048    String snapshotName = "snaptb-" + suffix;
049    Long ttl = 100000L;
050    // create Table
051    createTable(tableName);
052    SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 50, FAMILY);
053    int tableNumFiles = admin.getRegions(tableName).size();
054    // take a snapshot with TTL
055    Map<String, Object> props = new HashMap<>();
056    props.put("TTL", ttl);
057    admin.snapshot(snapshotName, tableName, props);
058    Optional<Long> ttlOpt =
059      admin.listSnapshots().stream().filter(s -> s.getName().equals(snapshotName))
060        .map(org.apache.hadoop.hbase.client.SnapshotDescription::getTtl).findAny();
061    assertTrue(ttlOpt.isPresent());
062    assertEquals(ttl, ttlOpt.get());
063
064    testExportFileSystemState(tableName, snapshotName, snapshotName, tableNumFiles,
065      getHdfsDestinationDir(), false, true);
066  }
067
068  @TestTemplate
069  public void testExportExpiredSnapshot() throws Exception {
070    String suffix = mob ? methodName + "-mob" : methodName;
071    TableName tableName = TableName.valueOf(suffix);
072    String snapshotName = "snapshot-" + suffix;
073    createTable(tableName);
074    SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 50, FAMILY);
075    Map<String, Object> properties = new HashMap<>();
076    properties.put("TTL", 10);
077    org.apache.hadoop.hbase.client.SnapshotDescription snapshotDescription =
078      new org.apache.hadoop.hbase.client.SnapshotDescription(snapshotName, tableName,
079        SnapshotType.FLUSH, null, EnvironmentEdgeManager.currentTime(), -1, properties);
080    admin.snapshot(snapshotDescription);
081    boolean isExist =
082      admin.listSnapshots().stream().anyMatch(ele -> snapshotName.equals(ele.getName()));
083    assertTrue(isExist);
084    TEST_UTIL.waitFor(60000,
085      () -> SnapshotDescriptionUtils.isExpiredSnapshot(snapshotDescription.getTtl(),
086        snapshotDescription.getCreationTime(), EnvironmentEdgeManager.currentTime()));
087    boolean isExpiredSnapshot =
088      SnapshotDescriptionUtils.isExpiredSnapshot(snapshotDescription.getTtl(),
089        snapshotDescription.getCreationTime(), EnvironmentEdgeManager.currentTime());
090    assertTrue(isExpiredSnapshot);
091    int res = runExportSnapshot(TEST_UTIL.getConfiguration(), snapshotName, snapshotName,
092      TEST_UTIL.getDefaultRootDirPath(), getHdfsDestinationDir(), false, false, false, true, true);
093    assertEquals(res, AbstractHBaseTool.EXIT_FAILURE);
094  }
095}