thiagotnunes commented on code in PR #17222: URL: https://github.com/apache/beam/pull/17222#discussion_r869860314
########## 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: This comment is outdated ########## 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, + * (4) WAIT_FOR_PARENT_PARTITIONS, (5) DELETE_PARTITION, (6) 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)); + } + + public Progress getProgress( Review Comment: This is not up to date? Take a look at how we are calculation progress currently in https://github.com/apache/beam/blob/df67c817184acafc4adbc56a725eed4985053171/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/restriction/TimestampRangeTracker.java#L196-L235 ########## sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/restriction/PartitionRestrictionTracker.java: ########## @@ -0,0 +1,170 @@ +/* + * 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 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 String token = + Optional.ofNullable(restriction.getMetadata()) + .map(PartitionRestrictionMetadata::getPartitionToken) + .orElse(""); + final SplitResult<PartitionRestriction> splitResult = + splitter.trySplit(fractionOfRemainder, lastClaimedPosition, restriction); + LOG.debug( Review Comment: This is already printed in the partition splitter. ########## sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/util/TimestampGenerator.java: ########## @@ -24,7 +24,7 @@ import java.util.concurrent.TimeUnit; public class TimestampGenerator extends Generator<Timestamp> { - private static final long MIN_SECONDS = Timestamp.MIN_VALUE.getSeconds(); + private static final long MIN_SECONDS = 0; Review Comment: Why do we need to do this? I think that the generator itself should produce values that are valid within its domain (MIN, MAX). If we need to restrict it, we can configure our test to only allow for values where seconds >= 0. This way the generator is general and can be used for other use cases that need to test this. ########## sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/restriction/PartitionRestrictionTracker.java: ########## @@ -0,0 +1,170 @@ +/* + * 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 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 String token = + Optional.ofNullable(restriction.getMetadata()) + .map(PartitionRestrictionMetadata::getPartitionToken) + .orElse(""); + final SplitResult<PartitionRestriction> splitResult = + splitter.trySplit(fractionOfRemainder, lastClaimedPosition, restriction); + LOG.debug( + "[" + + token + + "] Try split " + + lastClaimedPosition + + ", " + + restriction + + ": " + + splitResult); + 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 { Review Comment: I think this is a bit more nuanced, especially for the QUERY_CHANGE_STREAM mode: https://github.com/apache/beam/blob/df67c817184acafc4adbc56a725eed4985053171/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/restriction/TimestampRangeTracker.java#L173-L194 For the other modes, I am not sure if this logic is valid. We will have to test it (my fear is that a primary split is trying to finish, but it can't because this throws an exception). ########## sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/restriction/PartitionRestrictionTracker.java: ########## @@ -0,0 +1,170 @@ +/* + * 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 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) { Review Comment: We should also exempt the initial partition from splitting in the query change stream mode: https://github.com/apache/beam/blob/df67c817184acafc4adbc56a725eed4985053171/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/changestreams/restriction/ReadChangeStreamPartitionRangeTracker.java#L77-L84 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
