mosche commented on code in PR #26946: URL: https://github.com/apache/beam/pull/26946#discussion_r1223592159
########## sdks/java/io/amazon-web-services2/src/main/java/org/apache/beam/sdk/io/aws2/common/AsyncBatchWriteHandler.java: ########## @@ -0,0 +1,403 @@ +/* + * 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.aws2.common; + +import static java.util.function.Function.identity; +import static java.util.stream.Collectors.counting; +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.joining; +import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Predicates.notNull; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.BiConsumer; +import java.util.function.BiFunction; +import java.util.function.Function; +import javax.annotation.Nullable; +import javax.annotation.concurrent.NotThreadSafe; +import org.apache.beam.sdk.annotations.Internal; +import org.apache.beam.sdk.util.BackOff; +import org.apache.beam.sdk.util.BackOffUtils; +import org.apache.beam.sdk.util.FluentBackoff; +import org.apache.beam.sdk.util.Sleeper; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Sets; +import org.joda.time.DateTimeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Async handler that automatically retries unprocessed records in case of a partial success. + * + * <p>The handler enforces the provided upper limit of concurrent requests. Once that limit is + * reached any further call to {@link #batchWrite(String, List)} will block until another request + * completed. + * + * <p>The handler is fail fast and won't submit any further request after a failure. Async failures + * can be polled using {@link #checkForAsyncFailure()}. + * + * @param <RecT> Record type in batch + * @param <ResT> Potentially erroneous result that needs to be correlated to a record using {@link + * #failedRecords(List, List)} + */ +@NotThreadSafe +@Internal +public abstract class AsyncBatchWriteHandler<RecT, ResT> { + private static final Logger LOG = LoggerFactory.getLogger(AsyncBatchWriteHandler.class); + private final FluentBackoff backoff; + private final int concurrentRequests; + private final Stats stats; + protected final BiFunction<String, List<RecT>, CompletableFuture<List<ResT>>> submitFn; + protected final Function<ResT, String> errorCodeFn; + + private AtomicBoolean hasErrored; + private AtomicReference<Throwable> asyncFailure; + private Semaphore pendingRequests; Review Comment: 👍 requestsInProgress is much better, thx ... completion is pending, but not the request -- 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]
