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