This is an automated email from the ASF dual-hosted git repository. zhangduo pushed a commit to branch branch-2.6 in repository https://gitbox.apache.org/repos/asf/hbase.git
commit 7f553240be760610772d2f6468db23fbec6b5bc1 Author: Peng Lu <[email protected]> AuthorDate: Sun Feb 2 23:08:16 2025 +0800 HBASE-29088 The denominator cannot be 0 when calculating percentages in SnapshotStats (#6624) Signed-off-by: Pankaj Kumar<[email protected]> Signed-off-by: Duo Zhang <[email protected]> (cherry picked from commit 7660aa8222f0e15c425faf8ac228e1a25dc23801) --- .../apache/hadoop/hbase/snapshot/SnapshotInfo.java | 8 +- .../hbase/master/snapshot/TestSnapshotStats.java | 128 +++++++++++++++++++++ 2 files changed, 134 insertions(+), 2 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotInfo.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotInfo.java index 25e7f3ebd33..fb48b03d69a 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotInfo.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotInfo.java @@ -253,12 +253,16 @@ public final class SnapshotInfo extends AbstractHBaseTool { /** Returns the percentage of the shared store files */ public float getSharedStoreFilePercentage() { - return ((float) hfilesSize.get() / (getStoreFilesSize())) * 100; + return getStoreFilesSize() == 0 + ? 0 + : ((float) hfilesSize.get() / (getStoreFilesSize())) * 100; } /** Returns the percentage of the mob store files */ public float getMobStoreFilePercentage() { - return ((float) hfilesMobSize.get() / (getStoreFilesSize())) * 100; + return getStoreFilesSize() == 0 + ? 0 + : ((float) hfilesMobSize.get() / (getStoreFilesSize())) * 100; } /** Returns the total log size */ diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/snapshot/TestSnapshotStats.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/snapshot/TestSnapshotStats.java new file mode 100644 index 00000000000..57a99f5990b --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/snapshot/TestSnapshotStats.java @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.master.snapshot; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.Optional; +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.SnapshotDescription; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.snapshot.SnapshotInfo; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.MasterTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.After; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.TestName; + +@Category({ MasterTests.class, LargeTests.class }) +public class TestSnapshotStats { + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestSnapshotStats.class); + + private final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + private Admin admin; + + @Rule + public TestName name = new TestName(); + + @Before + public void setup() throws Exception { + TEST_UTIL.startMiniCluster(1); + admin = TEST_UTIL.getAdmin(); + } + + @After + public void tearDown() throws IOException { + admin.close(); + TEST_UTIL.shutdownMiniCluster(); + TEST_UTIL.getTestFileSystem().delete(TEST_UTIL.getDataTestDir(), true); + } + + @Test + public void testSnapshotTableWithoutAnyData() throws IOException { + // Create a table without any data + TableName tableName = TableName.valueOf(name.getMethodName()); + TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName); + ColumnFamilyDescriptor columnFamilyDescriptor = + ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).build(); + tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor); + admin.createTable(tableDescriptorBuilder.build()); + assertTrue(admin.tableExists(tableName)); + + String snapshotName = "snapshot_" + name.getMethodName(); + admin.snapshot(snapshotName, tableName); + + Optional<SnapshotDescription> optional = + admin.listSnapshots().stream().filter(s -> snapshotName.equals(s.getName())).findFirst(); + assertTrue(optional.isPresent()); + SnapshotDescription snapshotDescription = optional.get(); + + SnapshotInfo.SnapshotStats snapshotStats = + SnapshotInfo.getSnapshotStats(TEST_UTIL.getConfiguration(), snapshotDescription); + assertEquals(0L, snapshotStats.getStoreFilesSize()); + assertEquals(0, snapshotStats.getSharedStoreFilePercentage(), 0); + + admin.deleteSnapshot(snapshotName); + admin.disableTable(tableName); + admin.deleteTable(tableName); + } + + @Test + public void testSnapshotMobTableWithoutAnyData() throws IOException { + // Create a MOB table without any data + TableName tableName = TableName.valueOf(name.getMethodName()); + TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName); + ColumnFamilyDescriptor columnFamilyDescriptor = + ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info")).setMobEnabled(true).build(); + tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor); + admin.createTable(tableDescriptorBuilder.build()); + assertTrue(admin.tableExists(tableName)); + + String snapshotName = "snapshot_" + name.getMethodName(); + admin.snapshot(snapshotName, tableName); + + Optional<SnapshotDescription> optional = + admin.listSnapshots().stream().filter(s -> snapshotName.equals(s.getName())).findFirst(); + assertTrue(optional.isPresent()); + SnapshotDescription snapshotDescription = optional.get(); + + SnapshotInfo.SnapshotStats snapshotStats = + SnapshotInfo.getSnapshotStats(TEST_UTIL.getConfiguration(), snapshotDescription); + assertEquals(0L, snapshotStats.getStoreFilesSize()); + assertEquals(0, snapshotStats.getSharedStoreFilePercentage(), 0); + assertEquals(0, snapshotStats.getMobStoreFilePercentage(), 0); + + admin.deleteSnapshot(snapshotName); + admin.disableTable(tableName); + admin.deleteTable(tableName); + } +}
