[
https://issues.apache.org/jira/browse/BEAM-12164?focusedWorklogId=770430&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-770430
]
ASF GitHub Bot logged work on BEAM-12164:
-----------------------------------------
Author: ASF GitHub Bot
Created on: 13/May/22 23:31
Start Date: 13/May/22 23:31
Worklog Time Spent: 10m
Work Description: nancyxu123 commented on code in PR #17222:
URL: https://github.com/apache/beam/pull/17222#discussion_r872891297
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/restriction/PartitionRestrictionTracker.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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 static
org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.PartitionMode.DONE;
+import static
org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.PartitionMode.QUERY_CHANGE_STREAM;
+import static
org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.PartitionMode.STOP;
+import static
org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.TimestampUtils.next;
+import static
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState;
+
+import com.google.cloud.Timestamp;
+import java.math.BigDecimal;
+import java.util.Optional;
+import java.util.function.Supplier;
+import org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker;
+import
org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.HasProgress;
+import org.apache.beam.sdk.transforms.splittabledofn.SplitResult;
+import
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+// TODO: Add java docs
+// TODO: Implement duration waiting for returning false on try claim
+public class PartitionRestrictionTracker
+ extends RestrictionTracker<PartitionRestriction, PartitionPosition>
implements HasProgress {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(PartitionRestrictionTracker.class);
+ private final PartitionRestrictionSplitter splitter;
+ private final PartitionRestrictionClaimer claimer;
+ private final PartitionRestrictionProgressChecker progressChecker;
+ protected PartitionRestriction restriction;
+ private @Nullable PartitionPosition lastClaimedPosition;
+ protected Supplier<Timestamp> timeSupplier;
+
+ public PartitionRestrictionTracker(PartitionRestriction restriction) {
+ this(
+ restriction,
+ new PartitionRestrictionSplitter(),
+ new PartitionRestrictionClaimer(),
+ new PartitionRestrictionProgressChecker());
+ }
+
+ PartitionRestrictionTracker(
+ PartitionRestriction restriction,
+ PartitionRestrictionSplitter splitter,
+ PartitionRestrictionClaimer claimer,
+ PartitionRestrictionProgressChecker progressChecker) {
+ this.splitter = splitter;
+ this.claimer = claimer;
+ this.restriction = restriction;
+ this.progressChecker = progressChecker;
+ this.timeSupplier = () -> Timestamp.now();
+ }
+
+ @VisibleForTesting
+ public void setTimeSupplier(Supplier<Timestamp> timeSupplier) {
+ this.timeSupplier = timeSupplier;
+ }
+
+ @Override
+ public @Nullable SplitResult<PartitionRestriction> trySplit(double
fractionOfRemainder) {
+ final SplitResult<PartitionRestriction> splitResult =
+ splitter.trySplit(fractionOfRemainder, lastClaimedPosition,
restriction);
+ if (splitResult != null) {
+ PartitionRestriction restrictionFromSplit =
+
Optional.ofNullable(splitResult.getPrimary()).orElse(this.restriction);
+ this.restriction = restrictionFromSplit;
+ }
+ return splitResult;
+ }
+
+ @Override
+ public boolean tryClaim(PartitionPosition position) {
+ final boolean canClaim = claimer.tryClaim(restriction,
lastClaimedPosition, position);
+
+ if (canClaim) {
+ this.lastClaimedPosition = position;
+ }
+
+ return canClaim;
+ }
+
+ @Override
+ public Progress getProgress() {
+ final String token =
+ Optional.ofNullable(restriction.getMetadata())
+ .map(PartitionRestrictionMetadata::getPartitionToken)
+ .orElse(null);
+ BigDecimal end;
+ if (restriction.getEndTimestamp().compareTo(Timestamp.MAX_VALUE) == 0) {
+ // When the given end timestamp equals to Timestamp.MAX_VALUE, this
means that
+ // the end timestamp is not specified which should be a streaming job.
So we
+ // use now() as the end timestamp.
+ end = BigDecimal.valueOf(timeSupplier.get().getSeconds());
+ } else {
+ end = BigDecimal.valueOf(restriction.getEndTimestamp().getSeconds());
+ }
+ final Progress progress = progressChecker.getProgress(restriction,
lastClaimedPosition, end);
+ LOG.debug("[" + token + "] Progress is " + progress);
+ return progress;
+ }
+
+ @Override
+ public PartitionRestriction currentRestriction() {
+ return restriction;
+ }
+
+ @Override
+ public void checkDone() throws IllegalStateException {
+ if (restriction.getMode() == STOP) {
+ return;
+ }
+ if (lastClaimedPosition != null) {
+ final PartitionMode currentMode = lastClaimedPosition.getMode();
+ if (currentMode == QUERY_CHANGE_STREAM) {
+ // If the mode is QUERY_CHANGE_STREAM, and the end of the timestamp
range wasn't attempted,
+ // throw an exception
+ final Timestamp nextPosition =
next(lastClaimedPosition.getTimestamp().get());
+ if (nextPosition.compareTo(restriction.getEndTimestamp()) < 0) {
+ throw new IllegalStateException(
+ String.format(
+ "Last attempted key was %s in range %s, claiming work in
[%s, %s) was not"
+ + " attempted",
+ lastClaimedPosition.getTimestamp().get(),
+ restriction,
+ nextPosition,
+ restriction.getEndTimestamp()));
+ }
Review Comment:
No....
For modes STOP/DONE, they should always succeed if we claimed a position
For mode QUERY_CHANGE_STREAM, it should only succeed if we claimed
endTimestamp - 1, for restriction [startTimestamp, endTimestamp)
For all other modes, an error should be thrown, since in the splitter, I
don't believe the primary restriction will ever be one of the other modes after
a split.
Issue Time Tracking
-------------------
Worklog Id: (was: 770430)
Time Spent: 72h 50m (was: 72h 40m)
> SpannerIO Change Stream Connector
> ---------------------------------
>
> Key: BEAM-12164
> URL: https://issues.apache.org/jira/browse/BEAM-12164
> Project: Beam
> Issue Type: New Feature
> Components: sdk-java-core
> Reporter: Thiago Nunes
> Assignee: Thiago Nunes
> Priority: P2
> Fix For: 2.37.0
>
> Time Spent: 72h 50m
> Remaining Estimate: 0h
>
> We would like to augment the existing Google Cloud SpannerIO connector
> ([https://github.com/apache/beam/blob/master/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/SpannerIO.java)]
> with the support for Spanner Change Streams (CDC). CDC support is just being
> implemented in Spanner and it will be exposed through a gRPC API. We will use
> such API to create a new SpannerIO.readChangeStream(...) implementation.
--
This message was sent by Atlassian Jira
(v8.20.7#820007)