boyuanzz commented on a change in pull request #13470: URL: https://github.com/apache/beam/pull/13470#discussion_r543632883
########## File path: sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsublite/OffsetByteRangeTracker.java ########## @@ -0,0 +1,119 @@ +/* + * 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.pubsublite; + +import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument; +import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull; +import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState; + +import com.google.cloud.pubsublite.Offset; +import com.google.cloud.pubsublite.proto.ComputeMessageStatsResponse; +import javax.annotation.Nullable; +import org.apache.beam.sdk.io.range.OffsetRange; +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; + +public class OffsetByteRangeTracker extends RestrictionTracker<OffsetRange, OffsetByteProgress> + implements HasProgress { + private final TopicBacklogReader backlogReader; + private OffsetRange range; + private @Nullable Long lastClaimed; + private long byteCount = 0; + + public OffsetByteRangeTracker(OffsetRange range, TopicBacklogReader backlogReader) { + checkArgument(range.getTo() == Long.MAX_VALUE); + this.backlogReader = backlogReader; + this.range = range; + } + + @Override + public IsBounded isBounded() { + return IsBounded.UNBOUNDED; + } + + @Override + public boolean tryClaim(OffsetByteProgress position) { + long toClaim = position.lastOffset().value(); + checkArgument( + lastClaimed == null || toClaim > lastClaimed, + "Trying to claim offset %s while last attempted was %s", + position.lastOffset().value(), + lastClaimed); + checkArgument( + toClaim >= range.getFrom(), + "Trying to claim offset %s before start of the range %s", + toClaim, + range); + // split() has already been called, truncating this range. No more offsets may be claimed. + if (range.getTo() != Long.MAX_VALUE) { + boolean isRangeEmpty = range.getTo() == range.getFrom(); + boolean isValidClosedRange = nextOffset() == range.getTo(); + checkState( + isRangeEmpty || isValidClosedRange, + "Violated class precondition: offset range improperly split. Please report a beam bug."); + return false; + } + lastClaimed = toClaim; + byteCount += position.batchBytes(); + return true; + } + + @Override + public OffsetRange currentRestriction() { + return range; + } + + private long nextOffset() { + return lastClaimed == null ? currentRestriction().getFrom() : lastClaimed + 1; + } + + @Override + public @Nullable SplitResult<OffsetRange> trySplit(double fractionOfRemainder) { + // Cannot split a bounded range. This should already be completely claimed. + if (range.getTo() != Long.MAX_VALUE) return null; + range = new OffsetRange(currentRestriction().getFrom(), nextOffset()); + return SplitResult.of(this.range, new OffsetRange(nextOffset(), Long.MAX_VALUE)); + } + + @Override + @SuppressWarnings("unboxing.of.nullable") + public void checkDone() throws IllegalStateException { + backlogReader.close(); Review comment: > You know finalize() is deprecated yes? I don't, but good to know. We only have lifecycle management on `DoFn`, thus you should manage the resource of your objects. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
