[
https://issues.apache.org/jira/browse/BEAM-12164?focusedWorklogId=771138&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-771138
]
ASF GitHub Bot logged work on BEAM-12164:
-----------------------------------------
Author: ASF GitHub Bot
Created on: 17/May/22 04:31
Start Date: 17/May/22 04:31
Worklog Time Spent: 10m
Work Description: hengfengli commented on code in PR #17222:
URL: https://github.com/apache/beam/pull/17222#discussion_r874354953
##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/restriction/PartitionRestrictionProgressChecker.java:
##########
@@ -0,0 +1,117 @@
+/*
+ * 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 java.util.function.Supplier;
+import
org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.Progress;
+import
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+public class PartitionRestrictionProgressChecker {
+
+ private static final BigDecimal TOTAL_MODE_TRANSITIONS =
BigDecimal.valueOf(3L);
+ protected Supplier<Timestamp> timeSupplier;
+
+ /**
+ * Indicates how many mode transitions have been completed for the current
mode. The transitions
+ * are as follows: * (1) UPDATE_STATE, (2) QUERY_CHANGE_STREAM, (3)
WAIT_FOR_CHILD_PARTITIONS, (4)
+ * DONE.
+ *
+ * <p>This is used to calculate the units of work left, meaning that 1
transition = 1 unit of
+ * work.
+ */
+ private final Map<PartitionMode, BigDecimal> modeToTransitionsCompleted;
+
+ public PartitionRestrictionProgressChecker() {
+ modeToTransitionsCompleted = new HashMap<>();
+ modeToTransitionsCompleted.put(UPDATE_STATE, BigDecimal.valueOf(0L));
+ modeToTransitionsCompleted.put(QUERY_CHANGE_STREAM,
BigDecimal.valueOf(1L));
+ modeToTransitionsCompleted.put(WAIT_FOR_CHILD_PARTITIONS,
BigDecimal.valueOf(2L));
+ modeToTransitionsCompleted.put(DONE, BigDecimal.valueOf(3L));
+ this.timeSupplier = () -> Timestamp.now();
+ }
+
+ @VisibleForTesting
+ public void setTimeSupplier(Supplier<Timestamp> timeSupplier) {
+ this.timeSupplier = timeSupplier;
+ }
+
+ public Progress getProgress(
+ PartitionRestriction restriction, @Nullable PartitionPosition
lastClaimedPosition) {
+ BigDecimal totalSeconds;
+ 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.
+ totalSeconds = BigDecimal.valueOf(timeSupplier.get().getSeconds());
+ } else {
+ totalSeconds =
BigDecimal.valueOf(restriction.getEndTimestamp().getSeconds());
+ }
+
+ final PartitionMode currentMode =
+ Optional.ofNullable(lastClaimedPosition)
+ .map(PartitionPosition::getMode)
+ .orElse(restriction.getMode());
+
+ // Total work is the total number of seconds in the timestamp range plus
the total number
+ // of mode transitions.
+ final BigDecimal totalWork = totalSeconds.add(TOTAL_MODE_TRANSITIONS,
DECIMAL128);
Review Comment:
Why do we need to add the total number of mode transitions here?
Issue Time Tracking
-------------------
Worklog Id: (was: 771138)
Time Spent: 73h 20m (was: 73h 10m)
> 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: 73h 20m
> 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)