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.snapshot;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.io.IOException;
024import java.util.Optional;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.Admin;
028import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
029import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
030import org.apache.hadoop.hbase.client.SnapshotDescription;
031import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
032import org.apache.hadoop.hbase.snapshot.SnapshotInfo;
033import org.apache.hadoop.hbase.testclassification.LargeTests;
034import org.apache.hadoop.hbase.testclassification.MasterTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.junit.jupiter.api.AfterEach;
037import org.junit.jupiter.api.BeforeEach;
038import org.junit.jupiter.api.Tag;
039import org.junit.jupiter.api.Test;
040import org.junit.jupiter.api.TestInfo;
041
042@Tag(MasterTests.TAG)
043@Tag(LargeTests.TAG)
044public class TestSnapshotStats {
045  private final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
046  private Admin admin;
047  private String currentTestName;
048
049  @BeforeEach
050  public void setup(TestInfo testInfo) throws Exception {
051    currentTestName = testInfo.getTestMethod().get().getName();
052    TEST_UTIL.startMiniCluster(1);
053    admin = TEST_UTIL.getAdmin();
054  }
055
056  @AfterEach
057  public void tearDown() throws IOException {
058    admin.close();
059    TEST_UTIL.shutdownMiniCluster();
060    TEST_UTIL.getTestFileSystem().delete(TEST_UTIL.getDataTestDir(), true);
061  }
062
063  @Test
064  public void testSnapshotTableWithoutAnyData() throws IOException {
065    TableName tableName = TableName.valueOf(currentTestName);
066    TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
067    ColumnFamilyDescriptor columnFamilyDescriptor =
068      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).build();
069    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
070    admin.createTable(tableDescriptorBuilder.build());
071    assertTrue(admin.tableExists(tableName));
072
073    String snapshotName = "snapshot_" + currentTestName;
074    admin.snapshot(snapshotName, tableName);
075
076    Optional<SnapshotDescription> optional =
077      admin.listSnapshots().stream().filter(s -> snapshotName.equals(s.getName())).findFirst();
078    SnapshotDescription snapshotDescription = optional.orElseThrow();
079
080    SnapshotInfo.SnapshotStats snapshotStats =
081      SnapshotInfo.getSnapshotStats(TEST_UTIL.getConfiguration(), snapshotDescription);
082    assertEquals(0L, snapshotStats.getStoreFilesSize());
083    assertEquals(0, snapshotStats.getSharedStoreFilePercentage(), 0);
084
085    admin.deleteSnapshot(snapshotName);
086    admin.disableTable(tableName);
087    admin.deleteTable(tableName);
088  }
089
090  @Test
091  public void testSnapshotMobTableWithoutAnyData() throws IOException {
092    // Create a MOB table without any data
093    TableName tableName = TableName.valueOf(currentTestName);
094    TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
095    ColumnFamilyDescriptor columnFamilyDescriptor =
096      ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).setMobEnabled(true).build();
097    tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
098    admin.createTable(tableDescriptorBuilder.build());
099    assertTrue(admin.tableExists(tableName));
100
101    String snapshotName = "snapshot_" + currentTestName;
102    admin.snapshot(snapshotName, tableName);
103
104    Optional<SnapshotDescription> optional =
105      admin.listSnapshots().stream().filter(s -> snapshotName.equals(s.getName())).findFirst();
106    SnapshotDescription snapshotDescription = optional.orElseThrow();
107
108    SnapshotInfo.SnapshotStats snapshotStats =
109      SnapshotInfo.getSnapshotStats(TEST_UTIL.getConfiguration(), snapshotDescription);
110    assertEquals(0L, snapshotStats.getStoreFilesSize());
111    assertEquals(0, snapshotStats.getSharedStoreFilePercentage(), 0);
112    assertEquals(0, snapshotStats.getMobStoreFilePercentage(), 0);
113
114    admin.deleteSnapshot(snapshotName);
115    admin.disableTable(tableName);
116    admin.deleteTable(tableName);
117  }
118}