jinhyukify commented on code in PR #8134:
URL: https://github.com/apache/hbase/pull/8134#discussion_r3139181900
##########
hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/TableRecordReaderImpl.java:
##########
@@ -141,9 +143,61 @@ public void initialize(InputSplit inputsplit,
TaskAttemptContext context)
if (context != null) {
this.context = context;
}
+ initProgressBounds();
restart(scan.getStartRow());
}
+ /**
+ * Resolve the start/stop row keys used for progress estimation. The
TableInputFormat splitter
+ * sets start and stop row keys from region boundaries, so they are only
empty for the table's
+ * very first region (empty start) or last region (empty stop). In those
cases, probe the table to
+ * discover the actual first or last row key as an approximation.
+ */
+ private void initProgressBounds() {
+ byte[] startRow = scan.getStartRow();
+ byte[] stopRow = scan.getStopRow();
+ if (startRow == null || startRow.length == 0) {
+ startRow = probeFirstRow();
+ }
+ if (stopRow == null || stopRow.length == 0) {
+ stopRow = probeLastRow();
+ }
+ Configuration conf = context.getConfiguration();
+ Class<? extends RowKeyProgress> progressClass =
conf.getClass(RowKeyProgress.PROGRESS_CLASS_KEY,
+ ByteBasedRowKeyProgress.class, RowKeyProgress.class);
+ rowKeyProgress = ReflectionUtils.newInstance(progressClass, conf);
+ rowKeyProgress.setStartStopRows(startRow, stopRow);
+ }
+
+ private byte[] probeFirstRow() {
+ try {
+ Scan probeScan = new Scan(scan);
+ probeScan.setOneRowLimit();
+ try (ResultScanner probeScanner = htable.getScanner(probeScan)) {
+ Result result = probeScanner.next();
+ return result != null ? result.getRow() : null;
+ }
+ } catch (IOException e) {
+ LOG.warn("Failed to probe first row for progress estimation", e);
+ return null;
Review Comment:
If there are any issues with the scan, this will simply report 0 progress.
##########
hbase-mapreduce/src/main/java/org/apache/hadoop/hbase/mapreduce/ByteBasedRowKeyProgress.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.mapreduce;
+
+import org.apache.yetus.audience.InterfaceAudience;
+
+/**
+ * {@link RowKeyProgress} implementation that treats row keys as raw byte
sequences. Converts the
+ * leading bytes to a big-endian unsigned numeric value and computes progress
as a linear fraction
+ * of the key space.
+ */
[email protected]
+public class ByteBasedRowKeyProgress implements RowKeyProgress {
+ private static final int BYTES_FOR_PROGRESS = Double.BYTES;
+
+ private double start;
+ private double stop;
+
+ @Override
+ public void setStartStopRows(byte[] startRow, byte[] stopRow) {
+ this.start = rowKeyToDouble(startRow);
+ this.stop = rowKeyToDouble(stopRow);
+ }
+
+ @Override
+ public float getProgress(byte[] currentRow) {
+ if (currentRow == null || stop <= start) {
+ return 0.0f;
+ }
+ double current = rowKeyToDouble(currentRow);
+ float progress = (float) ((current - start) / (stop - start));
+ return Math.min(1.0f, Math.max(0.0f, progress));
+ }
+
+ /**
+ * Interpret the leading bytes of a row key as an unsigned big-endian value.
Keys shorter than
+ * {@link #BYTES_FOR_PROGRESS} bytes are treated as if right-padded with
zeros.
+ */
+ private static double rowKeyToDouble(byte[] row) {
+ if (row == null) {
+ return 0;
+ }
+ double d = 0;
Review Comment:
double avoids unsigned arithmetic issues. Java has no unsigned long, so
interpreting row key bytes as a raw long would be cumbersome. With double, all
values are naturally non-negative and standard arithmetic just works.
--
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]