Class TimestampRangeTracker
- java.lang.Object
-
- org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker<TimestampRange,com.google.cloud.Timestamp>
-
- org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.TimestampRangeTracker
-
- All Implemented Interfaces:
org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.HasProgress
- Direct Known Subclasses:
DetectNewPartitionsRangeTracker,ReadChangeStreamPartitionRangeTracker
public class TimestampRangeTracker extends org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker<TimestampRange,com.google.cloud.Timestamp> implements org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.HasProgress
ARestrictionTrackerfor claiming positions in aTimestampRangein a monotonically increasing fashion.The smallest position is
Timestamp.MIN_VALUEand the largest position isTimestamp.MAX_VALUE - 1 nanosecond.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker
org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.HasProgress, org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.IsBounded, org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.Progress, org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.TruncateResult<RestrictionT extends java.lang.Object>
-
-
Field Summary
Fields Modifier and Type Field Description protected @Nullable com.google.cloud.TimestamplastAttemptedPositionprotected @Nullable com.google.cloud.TimestamplastClaimedPositionprotected TimestampRangerangeprotected java.util.function.Supplier<com.google.cloud.Timestamp>timeSupplier
-
Constructor Summary
Constructors Constructor Description TimestampRangeTracker(TimestampRange range)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcheckDone()Checks if the restriction has been processed successfully.TimestampRangecurrentRestriction()org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.ProgressgetProgress()Returns the progress made within the restriction so far.org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.IsBoundedisBounded()voidsetTimeSupplier(java.util.function.Supplier<com.google.cloud.Timestamp> timeSupplier)booleantryClaim(com.google.cloud.Timestamp position)Attempts to claim the given position.booleantryClaim(com.google.cloud.Timestamp position, PartitionMetadata partitionMetadata)@Nullable org.apache.beam.sdk.transforms.splittabledofn.SplitResult<TimestampRange>trySplit(double fractionOfRemainder)Splits the restriction through the following algorithm:
-
-
-
Field Detail
-
range
protected TimestampRange range
-
lastAttemptedPosition
protected @Nullable com.google.cloud.Timestamp lastAttemptedPosition
-
lastClaimedPosition
protected @Nullable com.google.cloud.Timestamp lastClaimedPosition
-
timeSupplier
protected java.util.function.Supplier<com.google.cloud.Timestamp> timeSupplier
-
-
Constructor Detail
-
TimestampRangeTracker
public TimestampRangeTracker(TimestampRange range)
-
-
Method Detail
-
setTimeSupplier
public void setTimeSupplier(java.util.function.Supplier<com.google.cloud.Timestamp> timeSupplier)
-
tryClaim
public boolean tryClaim(com.google.cloud.Timestamp position)
Attempts to claim the given position. Depending on the position following outcomes are possible:- If the position is less than or equal to a previous attempted one, an
IllegalArgumentExceptionwill be thrown - If the position is less than the restriction range start, an
IllegalArgumentExceptionwill be thrown - If the position is greater than or equal to the range end, the position will not be claimed
- If the position is greater or equal to the range start and less than the range end, the position will be claimed
lastAttemptedPosition.- Specified by:
tryClaimin classorg.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker<TimestampRange,com.google.cloud.Timestamp>- Returns:
trueif the position was successfully claimed,falseotherwise
- If the position is less than or equal to a previous attempted one, an
-
tryClaim
public boolean tryClaim(com.google.cloud.Timestamp position, PartitionMetadata partitionMetadata)
-
trySplit
public @Nullable org.apache.beam.sdk.transforms.splittabledofn.SplitResult<TimestampRange> trySplit(double fractionOfRemainder)
Splits the restriction through the following algorithm:currentPosition = lastAttemptedPosition == null ? (from - 1ns) : lastAttemptedPosition splitPosition = currentPosition + max(1, (range.to - currentPosition) * fractionOfRemainder) primary = [range.from, splitPosition) residual = [splitPosition, range.to) this.range = primaryIf thesplitPositionis greater than therange.to,nullwill be returned. For checkpoints thefractionOfRemainderwill always be zero.- Specified by:
trySplitin classorg.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker<TimestampRange,com.google.cloud.Timestamp>- Returns:
- a
SplitResultif a split was possible ornullif thesplitPositionis beyond the end of the range.
-
checkDone
public void checkDone() throws java.lang.IllegalStateExceptionChecks if the restriction has been processed successfully. If not, throws anIllegalStateException.The restriction is considered processed successfully if:
- The range is empty (
range.from == range.to) - The
lastAttemptedPosition + 1ns >= range.to
- No position claim was attempted for a non-empty range
- The
lastAttemptedPosition + 1ns < range.to
- Specified by:
checkDonein classorg.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker<TimestampRange,com.google.cloud.Timestamp>- Throws:
java.lang.IllegalStateException
- The range is empty (
-
getProgress
public org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.Progress getProgress()
Returns the progress made within the restriction so far. If lastAttemptedPosition is null, the start of the range is used as the completed work; otherwise, lastAttemptedPosition will be used. The time gap between lastAttemptedPosition and now is used as the remaining work. In this way, when the time gap becomes large, we will have more backlog to process and we should add more resources.- Specified by:
getProgressin interfaceorg.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.HasProgress- Returns:
- work completed and work remaining in seconds.
-
currentRestriction
public TimestampRange currentRestriction()
- Specified by:
currentRestrictionin classorg.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker<TimestampRange,com.google.cloud.Timestamp>
-
isBounded
public org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.IsBounded isBounded()
- Specified by:
isBoundedin classorg.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker<TimestampRange,com.google.cloud.Timestamp>
-
-