lukecwik commented on a change in pull request #11715:
URL: https://github.com/apache/beam/pull/11715#discussion_r427400011



##########
File path: 
sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/splittabledofn/GrowableOffsetRangeTrackerTest.java
##########
@@ -0,0 +1,231 @@
+/*
+ * 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.beam.sdk.transforms.splittabledofn;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.beam.sdk.io.range.OffsetRange;
+import 
org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.Progress;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link GrowableOffsetRangeTracker}. */
+@RunWith(JUnit4.class)
+public class GrowableOffsetRangeTrackerTest {
+  private static class SimplePoller implements 
GrowableOffsetRangeTracker.RangeEndEstimator {
+    private long estimateRangeEnd = 0;
+
+    @Override
+    public long estimate() {
+      return estimateRangeEnd;
+    }
+
+    public void setEstimateRangeEnd(long offset) {
+      estimateRangeEnd = offset;
+    }
+  }
+
+  @Rule public final ExpectedException expected = ExpectedException.none();
+
+  @Test
+  public void testIllegalInitialization() throws Exception {
+    expected.expect(NullPointerException.class);
+    GrowableOffsetRangeTracker tracker = new GrowableOffsetRangeTracker(0L, 
null);
+  }
+
+  @Test
+  public void testTryClaim() throws Exception {
+    GrowableOffsetRangeTracker tracker = new GrowableOffsetRangeTracker(0L, 
new SimplePoller());
+    assertTrue(tracker.tryClaim(10L));
+    assertTrue(tracker.tryClaim(100L));
+    assertFalse(tracker.tryClaim(Long.MAX_VALUE));
+    tracker.checkDone();
+  }
+
+  @Test
+  public void testCheckpointBeforeStart() throws Exception {
+    SimplePoller poller = new SimplePoller();
+    GrowableOffsetRangeTracker tracker = new GrowableOffsetRangeTracker(0L, 
poller);
+    poller.setEstimateRangeEnd(10);
+    SplitResult res = tracker.trySplit(0);
+    tracker.checkDone();
+    assertEquals(new OffsetRange(0, 0), res.getPrimary());
+    assertEquals(new OffsetRange(0, 0), tracker.currentRestriction());
+    assertEquals(new OffsetRange(0, Long.MAX_VALUE), res.getResidual());
+  }
+
+  @Test
+  public void testCheckpointJustStarted() throws Exception {
+    SimplePoller poller = new SimplePoller();
+    GrowableOffsetRangeTracker tracker = new GrowableOffsetRangeTracker(0L, 
poller);
+    assertTrue(tracker.tryClaim(5L));
+    poller.setEstimateRangeEnd(0L);
+    SplitResult res = tracker.trySplit(0);
+    tracker.checkDone();
+    assertEquals(new OffsetRange(0, 6), res.getPrimary());
+    assertEquals(new OffsetRange(0, 6), tracker.currentRestriction());
+    assertEquals(new OffsetRange(6, Long.MAX_VALUE), res.getResidual());
+
+    tracker = new GrowableOffsetRangeTracker(0L, poller);
+    assertTrue(tracker.tryClaim(5L));
+    poller.setEstimateRangeEnd(20L);
+    res = tracker.trySplit(0);
+    tracker.checkDone();
+    assertEquals(new OffsetRange(0, 6), res.getPrimary());
+    assertEquals(new OffsetRange(6, Long.MAX_VALUE), res.getResidual());
+  }
+
+  @Test
+  public void testCheckpointAfterAllProcessed() throws Exception {
+    SimplePoller poller = new SimplePoller();
+    GrowableOffsetRangeTracker tracker = new GrowableOffsetRangeTracker(0L, 
poller);
+    assertFalse(tracker.tryClaim(Long.MAX_VALUE));
+    tracker.checkDone();
+    assertNull(tracker.trySplit(0));
+  }
+
+  @Test
+  public void testCheckpointAtEmptyRange() throws Exception {
+    GrowableOffsetRangeTracker tracker =
+        new GrowableOffsetRangeTracker(Long.MAX_VALUE, new SimplePoller());
+    tracker.checkDone();
+    assertNull(tracker.trySplit(0));
+  }
+
+  @Test
+  public void testSplit() throws Exception {
+    SimplePoller poller = new SimplePoller();
+    GrowableOffsetRangeTracker tracker = new GrowableOffsetRangeTracker(0L, 
poller);
+    assertTrue(tracker.tryClaim(0L));
+
+    poller.setEstimateRangeEnd(16L);
+    // The split of infinite range results in one finite range and on infinite 
range.

Review comment:
       ```suggestion
       // The split of infinite range results in one finite range and one 
infinite range.
   ```

##########
File path: 
sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/splittabledofn/GrowableOffsetRangeTrackerTest.java
##########
@@ -0,0 +1,231 @@
+/*
+ * 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.beam.sdk.transforms.splittabledofn;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.beam.sdk.io.range.OffsetRange;
+import 
org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.Progress;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link GrowableOffsetRangeTracker}. */
+@RunWith(JUnit4.class)
+public class GrowableOffsetRangeTrackerTest {
+  private static class SimplePoller implements 
GrowableOffsetRangeTracker.RangeEndEstimator {

Review comment:
       nit: SimplePoller -> SimpleEstimator

##########
File path: 
sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/splittabledofn/GrowableOffsetRangeTrackerTest.java
##########
@@ -0,0 +1,231 @@
+/*
+ * 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.beam.sdk.transforms.splittabledofn;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.beam.sdk.io.range.OffsetRange;
+import 
org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.Progress;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Tests for {@link GrowableOffsetRangeTracker}. */
+@RunWith(JUnit4.class)
+public class GrowableOffsetRangeTrackerTest {
+  private static class SimplePoller implements 
GrowableOffsetRangeTracker.RangeEndEstimator {
+    private long estimateRangeEnd = 0;
+
+    @Override
+    public long estimate() {
+      return estimateRangeEnd;
+    }
+
+    public void setEstimateRangeEnd(long offset) {
+      estimateRangeEnd = offset;
+    }
+  }
+
+  @Rule public final ExpectedException expected = ExpectedException.none();
+
+  @Test
+  public void testIllegalInitialization() throws Exception {
+    expected.expect(NullPointerException.class);
+    GrowableOffsetRangeTracker tracker = new GrowableOffsetRangeTracker(0L, 
null);
+  }
+
+  @Test
+  public void testTryClaim() throws Exception {
+    GrowableOffsetRangeTracker tracker = new GrowableOffsetRangeTracker(0L, 
new SimplePoller());
+    assertTrue(tracker.tryClaim(10L));
+    assertTrue(tracker.tryClaim(100L));
+    assertFalse(tracker.tryClaim(Long.MAX_VALUE));
+    tracker.checkDone();
+  }
+
+  @Test
+  public void testCheckpointBeforeStart() throws Exception {
+    SimplePoller poller = new SimplePoller();

Review comment:
       nit: poller -> estimator
   
   here and elsewhere in this test

##########
File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/splittabledofn/GrowableOffsetRangeTracker.java
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.beam.sdk.transforms.splittabledofn;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.io.range.OffsetRange;
+
+/**
+ * An {@link OffsetRangeTracker} for tracking a growable offset range. {@code 
Long.MAX_VALUE} is
+ * used as end range to indicate the possibility of infinity.
+ *
+ * <p>An offset range is considered growable when the end offset could grow 
(or change) during
+ * execution time (e.g., Kafka topic partition offset, appended file, ...).
+ *
+ * <p>The growable range is marked as done by claiming {@code Long.MAX_VALUE}.
+ */
+@Experimental(Kind.SPLITTABLE_DO_FN)
+public class GrowableOffsetRangeTracker extends OffsetRangeTracker {
+  /**
+   * An interface that should be implemented to fetch estimated end offset of 
the range.
+   *
+   * <p>{@code estimateRangeEnd} is called to give the end offset when {@code 
trySplit} or {@code
+   * getProgress} is invoked. The end offset is exclusive for the range. The 
estimated end is not
+   * necessary to increase monotonically as it will only be taken into 
computation when the estimate
+   * end is larger than the current position. When returning {@code 
Long.MAX_VALUE} as estimate, it
+   * means the largest possible position for the range is {@code 
Long.MAX_VALUE - 1}. If there is
+   * not an estimate yet, {@code Long.MIN_VALUE} should be returned, where 
estimated end will not
+   * effect progress and split.
+   *
+   * <p>Having a good estimate is important for providing a good signal of 
progress and splitting at
+   * a proper position.
+   *
+   * <p>If {@code estimate()} is expensive to call, please consider wrapping 
the implementation with
+   * {@code Suppliers.memoizeWithExpiration} as an optimization.

Review comment:
       ```suggestion
      * Provides the estimated end offset of the range.
      *
      * <p>{@link #estimate} is called to give the end offset when {@link 
#trySplit} or {@link
      * #getProgress} is invoked. The end offset is exclusive for the range. 
The estimated end is not
      * required to monotonically increase as it will only be taken into 
consideration when the estimated
      * end offset is larger than the current position. Returning {@code 
Long.MAX_VALUE} as the estimate
      * implies the largest possible position for the range is {@code 
Long.MAX_VALUE - 1}. Return {@code Long.MIN_VALUE} if an estimate can not be 
provided.
      *
      * <p>Providing a good estimate is important for an accurate progress 
signal and will impact splitting decisions by
      * the runner.
      *
      * <p>If {@link #estimate} is expensive to compute, consider wrapping the 
implementation with
      * {@link Suppliers.memoizeWithExpiration} or equivalent as an 
optimization.
   ```

##########
File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/splittabledofn/GrowableOffsetRangeTracker.java
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.beam.sdk.transforms.splittabledofn;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.io.range.OffsetRange;
+
+/**
+ * An {@link OffsetRangeTracker} for tracking a growable offset range. {@code 
Long.MAX_VALUE} is
+ * used as end range to indicate the possibility of infinity.
+ *
+ * <p>An offset range is considered growable when the end offset could grow 
(or change) during
+ * execution time (e.g., Kafka topic partition offset, appended file, ...).
+ *
+ * <p>The growable range is marked as done by claiming {@code Long.MAX_VALUE}.
+ */
+@Experimental(Kind.SPLITTABLE_DO_FN)
+public class GrowableOffsetRangeTracker extends OffsetRangeTracker {
+  /**
+   * An interface that should be implemented to fetch estimated end offset of 
the range.
+   *
+   * <p>{@code estimateRangeEnd} is called to give the end offset when {@code 
trySplit} or {@code
+   * getProgress} is invoked. The end offset is exclusive for the range. The 
estimated end is not
+   * necessary to increase monotonically as it will only be taken into 
computation when the estimate
+   * end is larger than the current position. When returning {@code 
Long.MAX_VALUE} as estimate, it
+   * means the largest possible position for the range is {@code 
Long.MAX_VALUE - 1}. If there is
+   * not an estimate yet, {@code Long.MIN_VALUE} should be returned, where 
estimated end will not
+   * effect progress and split.
+   *
+   * <p>Having a good estimate is important for providing a good signal of 
progress and splitting at
+   * a proper position.
+   *
+   * <p>If {@code estimate()} is expensive to call, please consider wrapping 
the implementation with
+   * {@code Suppliers.memoizeWithExpiration} as an optimization.
+   */
+  @FunctionalInterface
+  public interface RangeEndEstimator {
+    long estimate();
+  }
+
+  private final RangeEndEstimator rangeEndEstimator;
+
+  public GrowableOffsetRangeTracker(long start, RangeEndEstimator 
rangeEndEstimator) {
+    super(new OffsetRange(start, Long.MAX_VALUE));
+    this.rangeEndEstimator = checkNotNull(rangeEndEstimator);
+  }
+
+  @Override
+  public SplitResult<OffsetRange> trySplit(double fractionOfRemainder) {
+    // If current tracking range is no longer growable, split it as a normal 
range.
+    if (range.getTo() != Long.MAX_VALUE || range.getTo() == range.getFrom()) {
+      return super.trySplit(fractionOfRemainder);
+    }
+    // If current range has been done, there is no more space to split.
+    if (lastAttemptedOffset != null && lastAttemptedOffset == Long.MAX_VALUE) {
+      return null;
+    }
+    double cur =
+        (lastAttemptedOffset == null) ? (double) range.getFrom() - 1 : 
(double) lastAttemptedOffset;
+
+    // Fetch the estimated end offset. If the estimated end is smaller than 
the next offset, use
+    // the next offset as end.
+    double estimateRangeEnd = Math.max(rangeEndEstimator.estimate(), cur + 1);
+
+    // Convert to double in computation to prevent overflow, which may result 
in lost of some
+    // accuracy.
+    double splitPos = cur + Math.max(1L, (estimateRangeEnd - cur) * 
fractionOfRemainder);
+    long split = Double.valueOf(splitPos).longValue();
+    if (split > Double.valueOf(estimateRangeEnd).longValue()) {
+      return null;
+    }
+    OffsetRange res = new OffsetRange(split, range.getTo());
+    this.range = new OffsetRange(range.getFrom(), split);
+    return SplitResult.of(range, res);
+  }
+
+  @Override
+  public Progress getProgress() {
+    // If current tracking range is no longer growable, get progress as a 
normal range.
+    if (range.getTo() != Long.MAX_VALUE || range.getTo() == range.getFrom()) {
+      return super.getProgress();
+    }
+
+    // Convert to double in computation to prevent overflow, which may result 
in lost of some
+    // accuracy.

Review comment:
       ```suggestion
       // Convert to double in computation to prevent overflow, which may 
result in lost of
       // precision.
   ```

##########
File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/splittabledofn/GrowableOffsetRangeTracker.java
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.beam.sdk.transforms.splittabledofn;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.io.range.OffsetRange;
+
+/**
+ * An {@link OffsetRangeTracker} for tracking a growable offset range. {@code 
Long.MAX_VALUE} is
+ * used as end range to indicate the possibility of infinity.

Review comment:
       ```suggestion
    * used as the end of the range to indicate infinity.
   ```

##########
File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/splittabledofn/GrowableOffsetRangeTracker.java
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.beam.sdk.transforms.splittabledofn;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.io.range.OffsetRange;
+
+/**
+ * A special {@link OffsetRangeTracker} for tracking a growable offset range. 
The Long.MAX_VALUE is
+ * used as end range to indicate the possibility of infinity.
+ *
+ * <p>A offset range is considered as growable when the end offset could 
grow(or change) during
+ * execution time(e.g., Kafka backlog, appended file).
+ */
+@Experimental(Kind.SPLITTABLE_DO_FN)
+public class GrowableOffsetRangeTracker extends OffsetRangeTracker {
+  /**
+   * An interface that should be implemented to fetch estimated end offset of 
range.
+   *
+   * <p>{@code estimateRangeEnd} is called to give te end offset when {@code 
trySplit} or {@code
+   * getProgress} is invoked. The end offset is exclusive for the range. It's 
not necessary to
+   * increase monotonically but it's only taken into computation when it's 
larger than the current
+   * position. When returning Long.MAX_VALUE as estimate, it means the largest 
possible position for
+   * the range is Long.MAX_VALUE - 1. Having a good estimate is important for 
providing a good
+   * signal of progress and splitting at a proper position.
+   */
+  public interface OffsetPoller {
+    long estimateRangeEnd();
+  }
+
+  private final OffsetPoller poller;
+
+  public GrowableOffsetRangeTracker(long start, OffsetPoller offsetPoller) {
+    super(new OffsetRange(start, Long.MAX_VALUE));
+    this.poller = checkNotNull(offsetPoller);
+  }
+
+  @Override
+  public SplitResult<OffsetRange> trySplit(double fractionOfRemainder) {
+    // If current tracking range is no longer growable, split it as a normal 
range.
+    if (range.getTo() != Long.MAX_VALUE || range.getTo() == range.getFrom()) {

Review comment:
       Sorry for the confusion, I misread the if statement earlier.

##########
File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/splittabledofn/OffsetRangeTracker.java
##########
@@ -50,16 +53,17 @@ public OffsetRange currentRestriction() {
 
   @Override
   public SplitResult<OffsetRange> trySplit(double fractionOfRemainder) {
-    long cur = (lastAttemptedOffset == null) ? range.getFrom() - 1 : 
lastAttemptedOffset;
-    long splitPos =
-        cur
-            + Math.max(
-                1L, (Double.valueOf((range.getTo() - cur) * 
fractionOfRemainder)).longValue());
-    if (splitPos >= range.getTo()) {
+    // Convert to double in computation to prevent overflow, which may result 
in lost of some
+    // accuracy.

Review comment:
       ```suggestion
       // Convert to double in computation to prevent overflow, which may 
result in loss of
       // precision.
   ```

##########
File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/splittabledofn/GrowableOffsetRangeTracker.java
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.beam.sdk.transforms.splittabledofn;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.io.range.OffsetRange;
+
+/**
+ * An {@link OffsetRangeTracker} for tracking a growable offset range. {@code 
Long.MAX_VALUE} is
+ * used as end range to indicate the possibility of infinity.
+ *
+ * <p>An offset range is considered growable when the end offset could grow 
(or change) during
+ * execution time (e.g., Kafka topic partition offset, appended file, ...).
+ *
+ * <p>The growable range is marked as done by claiming {@code Long.MAX_VALUE}.
+ */
+@Experimental(Kind.SPLITTABLE_DO_FN)
+public class GrowableOffsetRangeTracker extends OffsetRangeTracker {
+  /**
+   * An interface that should be implemented to fetch estimated end offset of 
the range.
+   *
+   * <p>{@code estimateRangeEnd} is called to give the end offset when {@code 
trySplit} or {@code
+   * getProgress} is invoked. The end offset is exclusive for the range. The 
estimated end is not
+   * necessary to increase monotonically as it will only be taken into 
computation when the estimate
+   * end is larger than the current position. When returning {@code 
Long.MAX_VALUE} as estimate, it
+   * means the largest possible position for the range is {@code 
Long.MAX_VALUE - 1}. If there is
+   * not an estimate yet, {@code Long.MIN_VALUE} should be returned, where 
estimated end will not
+   * effect progress and split.
+   *
+   * <p>Having a good estimate is important for providing a good signal of 
progress and splitting at
+   * a proper position.
+   *
+   * <p>If {@code estimate()} is expensive to call, please consider wrapping 
the implementation with
+   * {@code Suppliers.memoizeWithExpiration} as an optimization.
+   */
+  @FunctionalInterface
+  public interface RangeEndEstimator {
+    long estimate();
+  }
+
+  private final RangeEndEstimator rangeEndEstimator;
+
+  public GrowableOffsetRangeTracker(long start, RangeEndEstimator 
rangeEndEstimator) {
+    super(new OffsetRange(start, Long.MAX_VALUE));
+    this.rangeEndEstimator = checkNotNull(rangeEndEstimator);
+  }
+
+  @Override
+  public SplitResult<OffsetRange> trySplit(double fractionOfRemainder) {
+    // If current tracking range is no longer growable, split it as a normal 
range.
+    if (range.getTo() != Long.MAX_VALUE || range.getTo() == range.getFrom()) {
+      return super.trySplit(fractionOfRemainder);
+    }
+    // If current range has been done, there is no more space to split.
+    if (lastAttemptedOffset != null && lastAttemptedOffset == Long.MAX_VALUE) {
+      return null;
+    }
+    double cur =
+        (lastAttemptedOffset == null) ? (double) range.getFrom() - 1 : 
(double) lastAttemptedOffset;
+
+    // Fetch the estimated end offset. If the estimated end is smaller than 
the next offset, use
+    // the next offset as end.
+    double estimateRangeEnd = Math.max(rangeEndEstimator.estimate(), cur + 1);
+
+    // Convert to double in computation to prevent overflow, which may result 
in lost of some
+    // accuracy.

Review comment:
       ```suggestion
       // Convert to double in computation to prevent overflow, which may 
result in loss of
       // precision.
   ```

##########
File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/splittabledofn/GrowableOffsetRangeTracker.java
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.beam.sdk.transforms.splittabledofn;
+
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull;
+
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.io.range.OffsetRange;
+
+/**
+ * An {@link OffsetRangeTracker} for tracking a growable offset range. {@code 
Long.MAX_VALUE} is
+ * used as end range to indicate the possibility of infinity.
+ *
+ * <p>An offset range is considered growable when the end offset could grow 
(or change) during
+ * execution time (e.g., Kafka topic partition offset, appended file, ...).
+ *
+ * <p>The growable range is marked as done by claiming {@code Long.MAX_VALUE}.
+ */
+@Experimental(Kind.SPLITTABLE_DO_FN)
+public class GrowableOffsetRangeTracker extends OffsetRangeTracker {
+  /**
+   * An interface that should be implemented to fetch estimated end offset of 
the range.
+   *
+   * <p>{@code estimateRangeEnd} is called to give the end offset when {@code 
trySplit} or {@code
+   * getProgress} is invoked. The end offset is exclusive for the range. The 
estimated end is not
+   * necessary to increase monotonically as it will only be taken into 
computation when the estimate
+   * end is larger than the current position. When returning {@code 
Long.MAX_VALUE} as estimate, it
+   * means the largest possible position for the range is {@code 
Long.MAX_VALUE - 1}. If there is
+   * not an estimate yet, {@code Long.MIN_VALUE} should be returned, where 
estimated end will not
+   * effect progress and split.
+   *
+   * <p>Having a good estimate is important for providing a good signal of 
progress and splitting at
+   * a proper position.
+   *
+   * <p>If {@code estimate()} is expensive to call, please consider wrapping 
the implementation with
+   * {@code Suppliers.memoizeWithExpiration} as an optimization.
+   */
+  @FunctionalInterface
+  public interface RangeEndEstimator {
+    long estimate();
+  }
+
+  private final RangeEndEstimator rangeEndEstimator;
+
+  public GrowableOffsetRangeTracker(long start, RangeEndEstimator 
rangeEndEstimator) {
+    super(new OffsetRange(start, Long.MAX_VALUE));
+    this.rangeEndEstimator = checkNotNull(rangeEndEstimator);
+  }
+
+  @Override
+  public SplitResult<OffsetRange> trySplit(double fractionOfRemainder) {
+    // If current tracking range is no longer growable, split it as a normal 
range.
+    if (range.getTo() != Long.MAX_VALUE || range.getTo() == range.getFrom()) {
+      return super.trySplit(fractionOfRemainder);
+    }
+    // If current range has been done, there is no more space to split.
+    if (lastAttemptedOffset != null && lastAttemptedOffset == Long.MAX_VALUE) {
+      return null;
+    }
+    double cur =

Review comment:
       We should only use doubles if there would be an issue with overflow. For 
really large long values we can have the math be incorrect which can lead to 
the splitPos being before from or after to even though with higher precision we 
would get something that works. For example:
   ```
       long a = 123456789012345677L;
       long b = 123456789012345679L;
       long c = 123456789012345680L;
       double ad = a;
       double bd = b;
       double cd = c;
       System.out.println((long)ad + " " + (long)bd + " " + (long)cd);
   ```
   prints
   ```
   123456789012345680 123456789012345680 123456789012345680
   ```
   Worthwhile to have a test which exercises a case where `from = 
123456789012345677L` and `to = 123456789012345679L` and we can have a split in 
between. Also worthwhile to test `from = 123456789012345681L` and `to = 
123456789012345683L`. All these values (and the values in between) when 
converted to double and back to long round to `123456789012345680L`.
   
   We can solve this by having two ways to calculate this or using BigDecimal. 
The two ways to calculate method would use longs when (estimateRangeEnd - cur) 
won't overflow and doubles otherwise. Using BigDecimal with a MathContext with 
enough precision wouldn't run into this either and would only require one 
implemention (I believe DECIMAL128 should have enough precision to not run into 
this issue).

##########
File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/splittabledofn/OffsetRangeTracker.java
##########
@@ -120,13 +124,16 @@ public String toString() {
   public Progress getProgress() {
     // If we have never attempted an offset, we return the length of the 
entire range as work
     // remaining.
+    // Convert to double in computation to prevent overflow, which may result 
in lost of some
+    // accuracy.

Review comment:
       ```suggestion
       // Convert to double in computation to prevent overflow, which may 
result in loss of
       // precision.
   ```




----------------------------------------------------------------
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.

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


Reply via email to