Skip to content

Commit

Permalink
replaced interruptible interface with an explicit restriction interru…
Browse files Browse the repository at this point in the history
…pter class
  • Loading branch information
dedocibula committed Dec 9, 2024
1 parent d7153fa commit d252d3d
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.ChildPartition;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.ChildPartitionsRecord;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionMetadata;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.Interruptible;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.RestrictionInterrupter;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.TimestampRange;
import org.apache.beam.sdk.transforms.DoFn.ProcessContinuation;
import org.apache.beam.sdk.transforms.splittabledofn.ManualWatermarkEstimator;
Expand Down Expand Up @@ -106,6 +106,7 @@ public Optional<ProcessContinuation> run(
PartitionMetadata partition,
ChildPartitionsRecord record,
RestrictionTracker<TimestampRange, Timestamp> tracker,
RestrictionInterrupter<Timestamp> interrupter,
ManualWatermarkEstimator<Instant> watermarkEstimator) {

final String token = partition.getPartitionToken();
Expand All @@ -114,8 +115,7 @@ public Optional<ProcessContinuation> run(

final Timestamp startTimestamp = record.getStartTimestamp();
final Instant startInstant = new Instant(startTimestamp.toSqlTimestamp().getTime());
if (tracker instanceof Interruptible
&& !((Interruptible) tracker).shouldContinue(startTimestamp)) {
if (interrupter.tryInterrupt(startTimestamp)) {
LOG.debug(
"[{}] Soft deadline reached with child partitions record at {}, rescheduling",
token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.ChildPartitionsRecord;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.DataChangeRecord;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionMetadata;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.Interruptible;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.RestrictionInterrupter;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.TimestampRange;
import org.apache.beam.sdk.transforms.DoFn.OutputReceiver;
import org.apache.beam.sdk.transforms.DoFn.ProcessContinuation;
Expand Down Expand Up @@ -83,6 +83,7 @@ public Optional<ProcessContinuation> run(
PartitionMetadata partition,
DataChangeRecord record,
RestrictionTracker<TimestampRange, Timestamp> tracker,
RestrictionInterrupter<Timestamp> interrupter,
OutputReceiver<DataChangeRecord> outputReceiver,
ManualWatermarkEstimator<Instant> watermarkEstimator) {

Expand All @@ -91,8 +92,7 @@ public Optional<ProcessContinuation> run(

final Timestamp commitTimestamp = record.getCommitTimestamp();
final Instant commitInstant = new Instant(commitTimestamp.toSqlTimestamp().getTime());
if (tracker instanceof Interruptible
&& !((Interruptible) tracker).shouldContinue(commitTimestamp)) {
if (interrupter.tryInterrupt(commitTimestamp)) {
LOG.debug(
"[{}] Soft deadline reached with data change record at {}, rescheduling",
token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.beam.sdk.io.gcp.spanner.changestreams.ChangeStreamMetrics;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.HeartbeatRecord;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionMetadata;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.Interruptible;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.RestrictionInterrupter;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.TimestampRange;
import org.apache.beam.sdk.transforms.DoFn.ProcessContinuation;
import org.apache.beam.sdk.transforms.splittabledofn.ManualWatermarkEstimator;
Expand Down Expand Up @@ -73,14 +73,15 @@ public Optional<ProcessContinuation> run(
PartitionMetadata partition,
HeartbeatRecord record,
RestrictionTracker<TimestampRange, Timestamp> tracker,
RestrictionInterrupter<Timestamp> interrupter,
ManualWatermarkEstimator<Instant> watermarkEstimator) {

final String token = partition.getPartitionToken();
LOG.debug("[{}] Processing heartbeat record {}", token, record);

final Timestamp timestamp = record.getTimestamp();
final Instant timestampInstant = new Instant(timestamp.toSqlTimestamp().getTime());
if (tracker instanceof Interruptible && !((Interruptible) tracker).shouldContinue(timestamp)) {
if (interrupter.tryInterrupt(timestamp)) {
LOG.debug(
"[{}] Soft deadline reached with heartbeat record at {}, rescheduling", token, timestamp);
return Optional.of(ProcessContinuation.resume());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.DataChangeRecord;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.HeartbeatRecord;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionMetadata;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.Interruptible;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.RestrictionInterrupter;
import org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.TimestampRange;
import org.apache.beam.sdk.transforms.DoFn.BundleFinalizer;
import org.apache.beam.sdk.transforms.DoFn.OutputReceiver;
Expand Down Expand Up @@ -166,10 +166,9 @@ public ProcessContinuation run(
new IllegalStateException(
"Partition " + token + " not found in metadata table"));

// Set the soft timeout to commit the work if any records have been processed.
if (tracker instanceof Interruptible) {
((Interruptible) tracker).setSoftTimeout(RESTRICTION_TRACKER_TIMEOUT);
}
// Interrupter with soft timeout to commit the work if any records have been processed.
RestrictionInterrupter<Timestamp> interrupter =
RestrictionInterrupter.withSoftTimeout(RESTRICTION_TRACKER_TIMEOUT);

try (ChangeStreamResultSet resultSet =
changeStreamDao.changeStreamQuery(
Expand All @@ -189,16 +188,25 @@ public ProcessContinuation run(
updatedPartition,
(DataChangeRecord) record,
tracker,
interrupter,
receiver,
watermarkEstimator);
} else if (record instanceof HeartbeatRecord) {
maybeContinuation =
heartbeatRecordAction.run(
updatedPartition, (HeartbeatRecord) record, tracker, watermarkEstimator);
updatedPartition,
(HeartbeatRecord) record,
tracker,
interrupter,
watermarkEstimator);
} else if (record instanceof ChildPartitionsRecord) {
maybeContinuation =
childPartitionsRecordAction.run(
updatedPartition, (ChildPartitionsRecord) record, tracker, watermarkEstimator);
updatedPartition,
(ChildPartitionsRecord) record,
tracker,
interrupter,
watermarkEstimator);
} else {
LOG.error("[{}] Unknown record type {}", token, record.getClass());
throw new IllegalArgumentException("Unknown record type " + record.getClass());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@
@SuppressWarnings({
"nullness" // TODO(https://github.com/apache/beam/issues/20497)
})
public class ReadChangeStreamPartitionRangeTracker extends TimestampRangeTracker
implements Interruptible {
public class ReadChangeStreamPartitionRangeTracker extends TimestampRangeTracker {

private final PartitionMetadata partition;
private Instant softDeadline;
private boolean continueProcessing = true;

/**
* Receives the partition that will be queried and the timestamp range that belongs to it.
Expand All @@ -54,48 +51,6 @@ public ReadChangeStreamPartitionRangeTracker(PartitionMetadata partition, Timest
this.partition = partition;
}

/**
* Sets a soft timeout from now for processing new positions. After the timeout the shouldContinue
* will start returning false indicating an early exit from processing.
*/
@Override
public void setSoftTimeout(Duration duration) {
softDeadline = new Instant(timeSupplier.get().toSqlTimestamp()).plus(duration);
continueProcessing = true;
}

/**
* Returns true if the restriction tracker can claim new positions.
*
* <p>If soft timeout isn't set always returns true. Otherwise:
*
* <ol>
* <li>If soft deadline hasn't been reached always returns true.
* <li>If soft deadline has been reached but we haven't processed any positions returns true.
* <li>If soft deadline has been reached but the new position is the same as the last attempted
* position returns true.
* <li>If soft deadline has been reached and the new position differs from the last attempted
* position returns false.
* </ol>
*
* @return {@code true} if the position processing should continue, {@code false} if the soft
* deadline has been reached and we have fully processed the previous position.
*/
@Override
public boolean shouldContinue(Timestamp position) {
if (!continueProcessing) {
return false;
}
if (softDeadline == null || lastAttemptedPosition == null) {
return true;
}

continueProcessing &=
new Instant(timeSupplier.get().toSqlTimestamp()).isBefore(softDeadline)
|| position.equals(lastAttemptedPosition);
return continueProcessing;
}

/**
* Attempts to claim the given position.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.io.gcp.spanner.changestreams.restriction;

import java.util.function.Supplier;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.annotations.VisibleForTesting;
import org.joda.time.Duration;
import org.joda.time.Instant;

/** An interrupter for restriction tracker of type T. */
@SuppressWarnings({
"nullness" // TODO(https://github.com/apache/beam/issues/20497)
})
public class RestrictionInterrupter<T> {
private T lastAttemptedPosition;

private Supplier<Instant> timeSupplier;
private Instant softDeadline;
private boolean hasInterrupted = true;

/**
* Sets a soft timeout from now for processing new positions. After the timeout the tryInterrupt
* will start returning true indicating an early exit from processing.
*/
public static <T> RestrictionInterrupter<T> withSoftTimeout(Duration timeout) {
return new RestrictionInterrupter<T>(() -> Instant.now(), timeout);
}

RestrictionInterrupter(Supplier<Instant> timeSupplier, Duration timeout) {
this.timeSupplier = timeSupplier;
this.softDeadline = this.timeSupplier.get().plus(timeout);
hasInterrupted = false;
}

@VisibleForTesting
void setTimeSupplier(Supplier<Instant> timeSupplier) {
this.timeSupplier = timeSupplier;
}

/**
* Returns true if the restriction tracker should be interrupted in claiming new positions.
*
* <ol>
* <li>If soft deadline hasn't been reached always returns false.
* <li>If soft deadline has been reached but we haven't processed any positions returns false.
* <li>If soft deadline has been reached but the new position is the same as the last attempted
* position returns false.
* <li>If soft deadline has been reached and the new position differs from the last attempted
* position returns true.
* </ol>
*
* @return {@code true} if the position processing should continue, {@code false} if the soft
* deadline has been reached and we have fully processed the previous position.
*/
public boolean tryInterrupt(T position) {
if (hasInterrupted) {
return true;
}
if (lastAttemptedPosition == null) {
lastAttemptedPosition = position;
return false;
}

hasInterrupted |=
timeSupplier.get().isAfter(softDeadline) && !position.equals(lastAttemptedPosition);
lastAttemptedPosition = position;
return hasInterrupted;
}
}
Loading

0 comments on commit d252d3d

Please sign in to comment.