ndimiduk commented on code in PR #6947:
URL: https://github.com/apache/hbase/pull/6947#discussion_r4710552242


##########
hbase-server/src/test/java/org/apache/hadoop/hbase/snapshot/TestSnapshotRegionSizeCalculator.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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.snapshot;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import java.util.Map;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hbase.HBaseTestingUtil;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.Admin;
+import org.apache.hadoop.hbase.testclassification.SmallTests;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.hadoop.hbase.util.CommonFSUtils;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos;
+
+@Tag(SmallTests.TAG)
+public class TestSnapshotRegionSizeCalculator {
+
+  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
+  private static FileSystem fs;
+  private static Path rootDir;
+  private static Path snapshotDir;
+  private static SnapshotProtos.SnapshotDescription snapshotDesc;
+  private static SnapshotManifest manifest;
+  private static Admin admin;
+  private static Configuration conf;
+
+  @BeforeAll
+  public static void setupCluster() throws Exception {
+    TEST_UTIL.startMiniCluster(3);
+    conf = TEST_UTIL.getConfiguration();
+    fs = TEST_UTIL.getTestFileSystem();
+    rootDir = TEST_UTIL.getDataTestDir("TestSnapshotRegionSizeCalculator");
+    CommonFSUtils.setRootDir(conf, rootDir);
+    admin = TEST_UTIL.getAdmin();
+  }
+
+  @AfterAll
+  public static void tearDown() throws Exception {
+    fs.delete(rootDir, true);
+    TEST_UTIL.shutdownMiniCluster();
+  }
+
+  @Test
+  public void testCalculateRegionSize() throws IOException {
+    TableName tableName = TableName.valueOf("test_table");
+    String snapshotName = "test_snapshot";
+
+    // table has no data
+    TEST_UTIL.createTable(tableName, Bytes.toBytes("info"));
+    admin = TEST_UTIL.getConnection().getAdmin();
+    admin.snapshot(snapshotName, tableName);
+    Path snapshotDir = 
SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName,
+      TEST_UTIL.getDefaultRootDirPath());
+    SnapshotProtos.SnapshotDescription snapshotDesc =
+      SnapshotDescriptionUtils.readSnapshotInfo(TEST_UTIL.getTestFileSystem(), 
snapshotDir);
+    SnapshotManifest manifest = 
SnapshotManifest.open(TEST_UTIL.getConfiguration(),
+      TEST_UTIL.getTestFileSystem(), snapshotDir, snapshotDesc);
+
+    RegionSizes regionSizes =
+      
SnapshotRegionSizeCalculator.calculateRegionSizes(TEST_UTIL.getConfiguration(), 
manifest);

Review Comment:
   I think that this points `hbase.rootdir` at an empty test directory after 
the minicluster has started. `calculateRegionSizes` resolves the snapshot 
through `SnapshotInfo.getSnapshotStats(conf, ...)`, which rebuilds the snapshot 
path from `hbase.rootdir`. It finds no manifest there: the test output logs `No 
manifest files present: 
.../TestSnapshotRegionSizeCalculator/.hbase-snapshot/test_snapshot` for both 
snapshots. So it returns an empty map, both assertion loops iterate over zero 
entries, and the test passes whatever sizes are computed.
   
   Even with the right root dir, the empty-table phase would iterate an empty 
map, because a region only gets an entry when it has a store file. Nothing else 
in the repo asserts `getLength()` on splits from `TableSnapshotInputFormat`, so 
as written no test fails if HBASE-29272 comes back.
   
   Also, `SmallTests` has a 3-minute class timeout, and this starts a 3-RS 
minicluster.
   
   (LLM assisted)



##########
hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotInfo.java:
##########
@@ -182,6 +182,14 @@ String getStateToString() {
       this.fs = fs;
     }
 
+    /**
+     * Returns the map containing region sizes.
+     * @return A map where keys are region names and values are their 
corresponding sizes.
+     */
+    public Map<String, Long> getRegionSizeMap() {

Review Comment:
   `SnapshotInfo` is `@InterfaceAudience.Public` and `SnapshotStats` has no 
narrower annotation, so this getter becomes public API on every branch it lands 
on. Its only caller is the IA.Private `SnapshotRegionSizeCalculator`. Let's not 
leak this into the public API space.
   
   As written:
   - it returns the internal `ConcurrentHashMap` itself;
   - the keys are encoded region names, not region names as the javadoc says;
   - the values include reference files, which `getStoreFilesSize()` excludes, 
so the two disagree on any snapshot that has references.
   
   Can callers use `getStoreFilesSize()` instead?
   
   (LLM assisted)



##########
hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/TableSnapshotInputFormat.java:
##########
@@ -95,10 +95,22 @@ public 
TableSnapshotRegionSplit(TableSnapshotInputFormatImpl.InputSplit delegate
       this.delegate = delegate;
     }
 
+    /**
+     * Creates a TableSnapshotRegionSplit with the given region information 
and the estimated length
+     * of the region in bytes. The length is used by the MapReduce framework 
to balance the
+     * distribution of splits; a value of 0 may cause the framework to skip 
the split.
+     * @param htd the table descriptor
+     * @param regionInfo the region info
+     * @param locations the locations of the region
+     * @param scan the scan to use
+     * @param restoreDir the directory to restore the snapshot to
+     * @param length the estimated size of the region in bytes
+     * @see <a 
href="https://issues.apache.org/jira/browse/HBASE-29272";>HBASE-29272</a>
+     */
     public TableSnapshotRegionSplit(TableDescriptor htd, RegionInfo regionInfo,
-      List<String> locations, Scan scan, Path restoreDir) {
+      List<String> locations, Scan scan, Path restoreDir, long length) {

Review Comment:
   Two things on this new public constructor; the same applies to 
`mapred/TableSnapshotInputFormat.java` L59-L72.
   
   - Nothing in the repo calls it. Every split is built through the 
`InputSplit` delegate constructor (L211 here, mapred L151, and both 
`MultiTableSnapshotInputFormat`s). So this adds a public signature to an 
IA.Public class that would need its own deprecation cycle to change. Is it 
needed?
   - Hadoop defines `getLength()` as the number of bytes in the split, and 
`getSplits` passes `regionSize / numSplits` when a region is cut into several 
splits. "the estimated size of the region" would lead a caller to over-report 
each sub-split. Also, MapReduce sorts splits by length but doesn't skip 
zero-length ones; it's Spark (`spark.hadoopRDD.ignoreEmptySplits`, on by 
default since 3.2) that drops them.
   
   (LLM assisted)



##########
hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotRegionSizeCalculator.java:
##########
@@ -0,0 +1,49 @@
+/*
+ * 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.snapshot;
+
+import java.io.IOException;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.hbase.snapshot.SnapshotInfo.SnapshotStats;
+import org.apache.hadoop.hbase.util.CommonFSUtils;
+import org.apache.yetus.audience.InterfaceAudience;
+
+import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos;
+
+/**
+ * Utility class to calculate the size of each region in a snapshot.
+ */
[email protected]
+public class SnapshotRegionSizeCalculator {
+
+  /**
+   * Calculate the size of each region in the snapshot.
+   * @return A map of region encoded names to their total size in bytes.
+   * @throws IOException If an error occurs during calculation.
+   */
+  public static RegionSizes calculateRegionSizes(Configuration conf,
+    SnapshotManifest manifest) throws IOException {
+    FileSystem fs = FileSystem.get(CommonFSUtils.getRootDir(conf).toUri(), 
conf);
+    SnapshotProtos.SnapshotDescription snapshot =
+      SnapshotDescriptionUtils.readSnapshotInfo(fs, manifest.getSnapshotDir());
+    SnapshotStats stats = SnapshotInfo.getSnapshotStats(conf, snapshot, null);
+    return new RegionSizes(stats.getRegionSizeMap());

Review Comment:
   `manifest` is only used for `getSnapshotDir()`. From there this re-reads 
`.snapshotinfo`, re-opens the manifest from `hbase.rootdir`, and runs the full 
`SnapshotStats` visitor. That visitor calls `exists`/`getFileStatus` against 
the archive, data and MOB locations for each store file, and 
`MultiTableSnapshotInputFormatImpl` repeats the whole pass once per scan. The 
value that ends up in the map is the manifest's own `StoreFile#getFileSize()`, 
so none of those filesystem calls affect the result.
   
   This runs on the client during `getSplits`, and there's no way to turn it 
off. With locality disabled, which is the typical setup for a Spark job reading 
a snapshot from outside the HBase cluster, split planning previously made no 
per-file filesystem calls; with this change it makes 2-3 NameNode or 
object-store calls per store file. The sizes also come from whatever 
`hbase.rootdir` the conf points at, not from the manifest the caller passed in.
   
   (LLM assisted)



##########
hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/RegionSizes.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * 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.snapshot;
+
+import java.util.Collections;
+import java.util.Map;
+import org.apache.yetus.audience.InterfaceAudience;
+
+/**
+ * Holds the size of each region in a snapshot.
+ */
[email protected]
+public class RegionSizes {
+
+  private final Map<String, Long> regionSizeMap;
+
+  RegionSizes(Map<String, Long> regionSizeMap) {
+    this.regionSizeMap = regionSizeMap;
+  }
+
+  /**
+   * Returns the total size of the given region in bytes.
+   * @param encodedRegionName the encoded region name
+   * @return the size of the region, or 0 if the region is not present in the 
snapshot
+   */
+  public long getRegionSize(String encodedRegionName) {

Review Comment:
   Both callers hold a `RegionInfo`, and `getOrDefault(..., 0L)` turns a key 
mismatch into a length of 0, which Spark then drops silently. Would taking a 
`RegionInfo` here make that mismatch impossible?
   
   (LLM assisted)



##########
hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/TableSnapshotInputFormatImpl.java:
##########
@@ -235,6 +240,12 @@ public void readFields(DataInput in) throws IOException {
 
       this.scan = Bytes.toString(Bytes.readByteArray(in));
       this.restoreDir = Bytes.toString(Bytes.readByteArray(in));
+      try {
+        this.length = in.readLong();
+      } catch (EOFException e) {
+        // Older serialized splits do not carry length and keep the previous 
behavior
+        this.length = 0L;
+      }

Review Comment:
   Hadoop writes all of a job's splits back-to-back into one `job.split` file, 
and each task deserializes its split from an open stream at that split's 
offset. For an old-format split that isn't the last one in the file, 
`readLong()` reads 8 bytes of the next split's header instead of hitting EOF, 
so the fallback only holds for the last split. The job's hbase-mapreduce jar 
ships with the job, so old writer / new reader shouldn't come up at all. Is 
there a case this fallback is meant to cover? If compatibility does matter, the 
`TableSnapshotRegionSplit` protobuf written just above can carry an optional 
field.
   
   (LLM assisted)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to