[ 
https://issues.apache.org/jira/browse/BEAM-12164?focusedWorklogId=769368&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-769368
 ]

ASF GitHub Bot logged work on BEAM-12164:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 12/May/22 00:31
            Start Date: 12/May/22 00:31
    Worklog Time Spent: 10m 
      Work Description: nancyxu123 commented on code in PR #17222:
URL: https://github.com/apache/beam/pull/17222#discussion_r870850918


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/restriction/PartitionRestrictionTracker.java:
##########
@@ -0,0 +1,152 @@
+/*
+ * 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.STOP;
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState;
+
+import java.util.Optional;
+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

Review Comment:
   Hmm I think that would require significant refactoring of this PR, which 
might delay it even further. I think I already tried to make the 
QUERY_CHANGE_STREAM behavior of this new restriction tracker very similar to 
ReadChangeStreamPartitionRangeTracker?



##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/restriction/PartitionRestrictionClaimer.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.PartitionMode.UPDATE_STATE;
+import static 
org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.PartitionMode.WAIT_FOR_CHILD_PARTITIONS;
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import com.google.cloud.Timestamp;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Sets;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** The PartitionRestrictionClaimer class. */
+public class PartitionRestrictionClaimer {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(PartitionRestrictionClaimer.class);
+
+  private final Map<PartitionMode, Set<PartitionMode>> allowedTransitions;
+
+  public PartitionRestrictionClaimer() {
+    this.allowedTransitions = new HashMap<>();
+    allowedTransitions.put(UPDATE_STATE, Sets.newHashSet(UPDATE_STATE, 
QUERY_CHANGE_STREAM));
+    allowedTransitions.put(
+        QUERY_CHANGE_STREAM, Sets.newHashSet(QUERY_CHANGE_STREAM, 
WAIT_FOR_CHILD_PARTITIONS));
+    allowedTransitions.put(
+        WAIT_FOR_CHILD_PARTITIONS, Sets.newHashSet(WAIT_FOR_CHILD_PARTITIONS, 
DONE));
+  }
+
+  public boolean tryClaim(
+      PartitionRestriction restriction,
+      @Nullable PartitionPosition lastClaimedPosition,
+      PartitionPosition position) {
+    final PartitionMode fromMode =
+        Optional.ofNullable(lastClaimedPosition)
+            .map(PartitionPosition::getMode)
+            .orElse(restriction.getMode());
+    final PartitionMode toMode = position.getMode();
+    final String token =
+        Optional.ofNullable(restriction.getMetadata())
+            .map(PartitionRestrictionMetadata::getPartitionToken)
+            .orElse("");
+
+    if (fromMode == STOP) {
+      LOG.debug(
+          "["
+              + token
+              + "] Try claim from ("
+              + restriction
+              + ","
+              + lastClaimedPosition
+              + ", "
+              + position
+              + ") is false");
+      return false;
+    }
+
+    checkArgument(
+        allowedTransitions.getOrDefault(fromMode, 
Collections.emptySet()).contains(toMode),
+        "Invalid partition mode transition from %s to %s",
+        fromMode,
+        toMode);
+    checkArgument(
+        toMode != QUERY_CHANGE_STREAM || position.getTimestamp().isPresent(),
+        "%s mode must specify a timestamp (no value sent)",
+        toMode);
+
+    boolean tryClaimResult;
+    switch (toMode) {
+      case QUERY_CHANGE_STREAM:
+        final Timestamp attemptedTimestamp = position.getTimestamp().get();
+        final Timestamp endTimestamp =
+            
Optional.ofNullable(restriction.getEndTimestamp()).orElse(Timestamp.MAX_VALUE);
+
+        checkArgument(
+            lastClaimedPosition == null
+                || !lastClaimedPosition.getTimestamp().isPresent()
+                || 
attemptedTimestamp.compareTo(lastClaimedPosition.getTimestamp().get()) >= 0,
+            "Trying to claim offset %s while last attempted was %s",
+            position,
+            lastClaimedPosition);
+        checkArgument(
+            attemptedTimestamp.compareTo(restriction.getStartTimestamp()) >= 0,
+            "Trying to claim offset %s before the start timestamp %s",
+            position,
+            restriction.getStartTimestamp().toString());
+
+        tryClaimResult = attemptedTimestamp.compareTo(endTimestamp) < 0;
+        break;
+      case UPDATE_STATE:
+      case WAIT_FOR_CHILD_PARTITIONS:
+      case DONE:
+        tryClaimResult = true;
+        break;
+      case STOP:
+        throw new IllegalArgumentException("Trying to claim STOP state is 
invalid");
+      default:
+        throw new IllegalArgumentException("Unknown mode " + fromMode);

Review Comment:
   Done



##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/restriction/PartitionRestrictionProgressChecker.java:
##########
@@ -0,0 +1,100 @@
+/*
+ * 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 java.math.MathContext.DECIMAL128;
+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.PartitionMode.UPDATE_STATE;
+import static 
org.apache.beam.sdk.io.gcp.spanner.changestreams.restriction.PartitionMode.WAIT_FOR_CHILD_PARTITIONS;
+
+import com.google.cloud.Timestamp;
+import java.math.BigDecimal;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import 
org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.Progress;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+public class PartitionRestrictionProgressChecker {
+
+  private static final BigDecimal TOTAL_MODE_TRANSITIONS = 
BigDecimal.valueOf(3L);
+
+  /**
+   * Indicates how many mode transitions have been completed for the current 
mode. The transitions
+   * are as follows: * (1) QUERY_CHANGE_STREAM, (2) WAIT_FOR_CHILD_PARTITIONS, 
(3) FINISH_PARTITION,

Review Comment:
   DOne





Issue Time Tracking
-------------------

    Worklog Id:     (was: 769368)
    Time Spent: 72h  (was: 71h 50m)

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

Reply via email to