You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "vahmed-hamdy (via GitHub)" <gi...@apache.org> on 2024/05/24 11:52:51 UTC
[PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
vahmed-hamdy opened a new pull request, #24839:
URL: https://github.com/apache/flink/pull/24839
<!--
*Thank you very much for contributing to Apache Flink - we are happy that you want to help us improve Flink. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
*Please understand that we do not do this to make contributions to Flink a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
## Contribution Checklist
- Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
- Name the pull request in the form "[FLINK-XXXX] [component] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number. Skip *component* if you are unsure about which is the best component.
Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] [docs] Fix typo in event time introduction` or `[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
- Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
- Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes. You can set up Azure Pipelines CI to do that following [this guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
- Each pull request should address only one issue, not mix up code from multiple issues.
- Each commit in the pull request has a meaningful commit message (including the JIRA id)
- Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
**(The sections below can be removed for hotfixes of typos)**
-->
## What is the purpose of the change
Implementation of [FLIP-451](https://cwiki.apache.org/confluence/display/FLINK/FLIP-451%3A+Introduce+timeout+configuration+to+AsyncSink+API) introducing Timeout Configuration to Async Sink.
## Brief change log
- Add `ResultHandler` class to be used by Sink implementers
- Add `AsyncSinkWriterResultHandler` implementation that supports timeout
- Add `requestTimeoutMs` and `failOnTimeout` configuration to `AsyncSinkWriterConfiguration` and to `AsyncSinkWriterConfigurationBuilder`
- Add default values to `requestTimeoutMs` and `failOnTimeout` as suggested in FLIP-451
- Add needed unit tests and refactored existing tests
## Verifying this change
This change added tests and can be verified as follows:
- Added unit tests
- Performed Sanity testing and benchmarks on Kinesis Implementation as described in the [Ticket](https://issues.apache.org/jira/browse/FLINK-35435).
## Does this pull request potentially affect one of the following parts:
- Dependencies (does it add or upgrade a dependency): no
- The public API, i.e., is any changed class annotated with `@Public(Evolving)`: yes
- The serializers: no
- The runtime per-record code paths (performance sensitive): no
- Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: no
- The S3 file system connector: no
## Documentation
- Does this pull request introduce a new feature? yes
- If yes, how is the feature documented? JavaDocs + [Follow Up Ticket
](https://issues.apache.org/jira/browse/FLINK-35445)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on PR #24839:
URL: https://github.com/apache/flink/pull/24839#issuecomment-2132249502
@flinkbot run azure
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on PR #24839:
URL: https://github.com/apache/flink/pull/24839#issuecomment-2132141416
@dannycranmer thanks for the review, Could you have another pass?
The CI is failing due to a bug in Architecture tests FLINK-35454,
Could you take a look at this [PR](https://github.com/apache/flink/pull/24843/commits/7dd3dc90fcb9c37fd0beccba69cf19586efe59f6) and we can rebase this one after.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1614911177
##########
flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriterTimeoutTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.flink.connector.base.sink.writer;
+
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.connector.base.sink.writer.config.AsyncSinkWriterConfiguration;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/** Test for timeout functionalities of {@link AsyncSinkWriter}. */
+public class AsyncSinkWriterTimeoutTest {
+ private final List<Long> destination = new ArrayList<>();
+
+ @Test
+ void writerShouldNotRetryIfRequestIsProcessedBeforeTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(10);
+ writer.deliverMessage();
+ tpts.setCurrentTime(120);
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldRetryOnTimeoutIfFailOnErrorIsFalse() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ // element should be requeued back after timeout
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout
+ writer.deliverMessage();
+ // flush outstanding mailbox messages containing resubmission of the element
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L, 1L);
+ }
+
+ @Test
+ void writerShouldFailOnTimeoutIfFailOnErrorIsTrue() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withCauseInstanceOf(TimeoutException.class)
+ .havingCause()
+ .withMessage("Request timed out after 100ms with failOnTimeout set to true.");
+ }
+
+ @Test
+ void writerShouldDiscardRetriedEntriesOnTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ writer.setShouldFailRequest(true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout, message request would fail but elements should be
+ // discarded as timeout already occurred
+ writer.deliverMessage();
+ // flush outstanding mailbox messages to resubmit timed out elements
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldFailOnFatalError() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.setFatalError(new FlinkRuntimeException("Fatal error"));
+ writer.write("1", null);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withMessage("Fatal error");
Review Comment:
Why? we explicitly set the complete message in test, right?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "liuml07 (via GitHub)" <gi...@apache.org>.
liuml07 commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1628815201
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriter.java:
##########
@@ -533,4 +607,77 @@ private int getNextBatchSizeLimit() {
protected Consumer<Exception> getFatalExceptionCons() {
return fatalExceptionCons;
}
+
+ /** An implementation of {@link ResultHandler} that supports timeout. */
+ private class AsyncSinkWriterResultHandler implements ResultHandler<RequestEntryT> {
+ private final ScheduledFuture<?> scheduledFuture;
+ private final long requestTimestamp;
+ private final int batchSize;
+ private final AtomicBoolean isCompleted = new AtomicBoolean(false);
+ private final List<RequestEntryT> batchEntries;
+
+ public AsyncSinkWriterResultHandler(
+ long requestTimestamp, List<RequestEntryT> batchEntries, RequestInfo requestInfo) {
+ this.scheduledFuture =
+ timeService.registerTimer(
+ timeService.getCurrentProcessingTime() + requestTimeoutMS,
+ instant -> this.timeout());
+ this.requestTimestamp = requestTimestamp;
+ this.batchEntries = batchEntries;
+ this.batchSize = requestInfo.getBatchSize();
+ }
+
+ @Override
+ public void complete() {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> completeRequest(Collections.emptyList(), batchSize, requestTimestamp),
+ "Mark in-flight request as completed successfully",
+ batchSize);
+ }
+ }
+
+ @Override
+ public void completeExceptionally(Exception e) {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> getFatalExceptionCons().accept(e),
+ "Mark in-flight request as failed with fatal exception",
Review Comment:
having %s for exception message?
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriter.java:
##########
@@ -533,4 +607,77 @@ private int getNextBatchSizeLimit() {
protected Consumer<Exception> getFatalExceptionCons() {
return fatalExceptionCons;
}
+
+ /** An implementation of {@link ResultHandler} that supports timeout. */
+ private class AsyncSinkWriterResultHandler implements ResultHandler<RequestEntryT> {
+ private final ScheduledFuture<?> scheduledFuture;
+ private final long requestTimestamp;
+ private final int batchSize;
+ private final AtomicBoolean isCompleted = new AtomicBoolean(false);
+ private final List<RequestEntryT> batchEntries;
+
+ public AsyncSinkWriterResultHandler(
+ long requestTimestamp, List<RequestEntryT> batchEntries, RequestInfo requestInfo) {
+ this.scheduledFuture =
+ timeService.registerTimer(
+ timeService.getCurrentProcessingTime() + requestTimeoutMS,
+ instant -> this.timeout());
+ this.requestTimestamp = requestTimestamp;
+ this.batchEntries = batchEntries;
+ this.batchSize = requestInfo.getBatchSize();
+ }
+
+ @Override
+ public void complete() {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> completeRequest(Collections.emptyList(), batchSize, requestTimestamp),
+ "Mark in-flight request as completed successfully",
Review Comment:
having %d for the `batchSize`?
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriter.java:
##########
@@ -181,15 +187,88 @@ public abstract class AsyncSinkWriter<InputT, RequestEntryT extends Serializable
* <p>During checkpointing, the sink needs to ensure that there are no outstanding in-flight
* requests.
*
+ * <p>This method is deprecated in favor of {@code submitRequestEntries(List<RequestEntryT>
Review Comment:
nit: use `@deprecated` in Javadoc for this?
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriter.java:
##########
@@ -533,4 +607,77 @@ private int getNextBatchSizeLimit() {
protected Consumer<Exception> getFatalExceptionCons() {
return fatalExceptionCons;
}
+
+ /** An implementation of {@link ResultHandler} that supports timeout. */
+ private class AsyncSinkWriterResultHandler implements ResultHandler<RequestEntryT> {
+ private final ScheduledFuture<?> scheduledFuture;
+ private final long requestTimestamp;
+ private final int batchSize;
+ private final AtomicBoolean isCompleted = new AtomicBoolean(false);
+ private final List<RequestEntryT> batchEntries;
+
+ public AsyncSinkWriterResultHandler(
+ long requestTimestamp, List<RequestEntryT> batchEntries, RequestInfo requestInfo) {
+ this.scheduledFuture =
+ timeService.registerTimer(
+ timeService.getCurrentProcessingTime() + requestTimeoutMS,
+ instant -> this.timeout());
+ this.requestTimestamp = requestTimestamp;
+ this.batchEntries = batchEntries;
+ this.batchSize = requestInfo.getBatchSize();
+ }
+
+ @Override
+ public void complete() {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> completeRequest(Collections.emptyList(), batchSize, requestTimestamp),
+ "Mark in-flight request as completed successfully",
+ batchSize);
+ }
+ }
+
+ @Override
+ public void completeExceptionally(Exception e) {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> getFatalExceptionCons().accept(e),
+ "Mark in-flight request as failed with fatal exception",
+ e.getMessage());
+ }
+ }
+
+ @Override
+ public void retryForEntries(List<RequestEntryT> requestEntriesToRetry) {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> completeRequest(requestEntriesToRetry, batchSize, requestTimestamp),
+ "Mark in-flight request as completed with %d failed request entries",
+ requestEntriesToRetry.size());
+ }
+ }
+
+ public void timeout() {
+ if (isCompleted.compareAndSet(false, true)) {
+ mailboxExecutor.execute(
+ () -> {
+ if (failOnTimeout) {
+ getFatalExceptionCons()
+ .accept(
+ new TimeoutException(
+ "Request timed out after "
+ + requestTimeoutMS
+ + "ms with failOnTimeout set to true."));
+ } else {
+ // Retry the request on timeout
+ completeRequest(batchEntries, batchSize, requestTimestamp);
+ }
+ },
+ "Mark in-flight request as completed with timeout");
Review Comment:
nit: add `requestTimeoutMS` to the description?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1614911032
##########
flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriterTimeoutTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.flink.connector.base.sink.writer;
+
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.connector.base.sink.writer.config.AsyncSinkWriterConfiguration;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/** Test for timeout functionalities of {@link AsyncSinkWriter}. */
+public class AsyncSinkWriterTimeoutTest {
+ private final List<Long> destination = new ArrayList<>();
+
+ @Test
+ void writerShouldNotRetryIfRequestIsProcessedBeforeTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(10);
+ writer.deliverMessage();
+ tpts.setCurrentTime(120);
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldRetryOnTimeoutIfFailOnErrorIsFalse() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ // element should be requeued back after timeout
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout
+ writer.deliverMessage();
+ // flush outstanding mailbox messages containing resubmission of the element
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L, 1L);
+ }
+
+ @Test
+ void writerShouldFailOnTimeoutIfFailOnErrorIsTrue() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withCauseInstanceOf(TimeoutException.class)
+ .havingCause()
+ .withMessage("Request timed out after 100ms with failOnTimeout set to true.");
+ }
+
+ @Test
+ void writerShouldDiscardRetriedEntriesOnTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ writer.setShouldFailRequest(true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout, message request would fail but elements should be
+ // discarded as timeout already occurred
+ writer.deliverMessage();
+ // flush outstanding mailbox messages to resubmit timed out elements
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldFailOnFatalError() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.setFatalError(new FlinkRuntimeException("Fatal error"));
+ writer.write("1", null);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withMessage("Fatal error");
+ }
+
+ /** Test that the writer can handle a timeout. */
+ private class TimeoutWriter extends AsyncSinkWriter<String, Long> {
+ private Exception fatalError;
+ private final CountDownLatch completionLatch;
+ private Thread submitThread;
+
+ private boolean shouldFailRequest = false;
Review Comment:
It is only used in scope of tests, we also guarantee no concurrent modifications since it is set by main test thread before latch is triggered to execute the request. I am happy to change but seems to detailed for a mock replacement class
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1614903605
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/AsyncSinkBase.java:
##########
@@ -73,6 +97,8 @@ protected AsyncSinkBase(
this.maxBatchSizeInBytes = maxBatchSizeInBytes;
this.maxTimeInBufferMS = maxTimeInBufferMS;
this.maxRecordSizeInBytes = maxRecordSizeInBytes;
+ this.requestTimeoutMS = requestTimeoutMS;
Review Comment:
replaced with primitives
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "dannycranmer (via GitHub)" <gi...@apache.org>.
dannycranmer merged PR #24839:
URL: https://github.com/apache/flink/pull/24839
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "jeyhunkarimov (via GitHub)" <gi...@apache.org>.
jeyhunkarimov commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1629840434
##########
flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriterTimeoutTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.flink.connector.base.sink.writer;
+
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.connector.base.sink.writer.config.AsyncSinkWriterConfiguration;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/** Test for timeout functionalities of {@link AsyncSinkWriter}. */
+public class AsyncSinkWriterTimeoutTest {
+ private final List<Long> destination = new ArrayList<>();
+
+ @Test
+ void writerShouldNotRetryIfRequestIsProcessedBeforeTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(10);
+ writer.deliverMessage();
+ tpts.setCurrentTime(120);
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldRetryOnTimeoutIfFailOnErrorIsFalse() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ // element should be requeued back after timeout
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout
+ writer.deliverMessage();
+ // flush outstanding mailbox messages containing resubmission of the element
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L, 1L);
+ }
+
+ @Test
+ void writerShouldFailOnTimeoutIfFailOnErrorIsTrue() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withCauseInstanceOf(TimeoutException.class)
+ .havingCause()
+ .withMessage("Request timed out after 100ms with failOnTimeout set to true.");
+ }
+
+ @Test
+ void writerShouldDiscardRetriedEntriesOnTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ writer.setShouldFailRequest(true);
Review Comment:
Not an issue anymore
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1629436707
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriter.java:
##########
@@ -533,4 +607,77 @@ private int getNextBatchSizeLimit() {
protected Consumer<Exception> getFatalExceptionCons() {
return fatalExceptionCons;
}
+
+ /** An implementation of {@link ResultHandler} that supports timeout. */
+ private class AsyncSinkWriterResultHandler implements ResultHandler<RequestEntryT> {
+ private final ScheduledFuture<?> scheduledFuture;
+ private final long requestTimestamp;
+ private final int batchSize;
+ private final AtomicBoolean isCompleted = new AtomicBoolean(false);
+ private final List<RequestEntryT> batchEntries;
+
+ public AsyncSinkWriterResultHandler(
+ long requestTimestamp, List<RequestEntryT> batchEntries, RequestInfo requestInfo) {
+ this.scheduledFuture =
+ timeService.registerTimer(
+ timeService.getCurrentProcessingTime() + requestTimeoutMS,
+ instant -> this.timeout());
+ this.requestTimestamp = requestTimestamp;
+ this.batchEntries = batchEntries;
+ this.batchSize = requestInfo.getBatchSize();
+ }
+
+ @Override
+ public void complete() {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> completeRequest(Collections.emptyList(), batchSize, requestTimestamp),
+ "Mark in-flight request as completed successfully",
+ batchSize);
+ }
+ }
+
+ @Override
+ public void completeExceptionally(Exception e) {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> getFatalExceptionCons().accept(e),
+ "Mark in-flight request as failed with fatal exception",
Review Comment:
The exception is propagated to task runner and should be visible as Error, I don't find a value of logging it in debug logs
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1614904850
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriter.java:
##########
@@ -533,4 +607,77 @@ private int getNextBatchSizeLimit() {
protected Consumer<Exception> getFatalExceptionCons() {
return fatalExceptionCons;
}
+
+ /** An implementation of {@link ResultHandler} that supports timeout. */
+ private class AsyncSinkWriterResultHandler implements ResultHandler<RequestEntryT> {
+ private final ScheduledFuture<?> scheduledFuture;
+ private final long requestTimestamp;
+ private final int batchSize;
+ private final AtomicBoolean isCompleted = new AtomicBoolean(false);
+ private final List<RequestEntryT> batchEntries;
+
+ public AsyncSinkWriterResultHandler(
+ long requestTimestamp, List<RequestEntryT> batchEntries, RequestInfo requestInfo) {
+ this.scheduledFuture =
+ timeService.registerTimer(
+ timeService.getCurrentProcessingTime() + requestTimeoutMS,
+ instant -> this.timeout());
+ this.requestTimestamp = requestTimestamp;
+ this.batchEntries = batchEntries;
+ this.batchSize = requestInfo.getBatchSize();
+ }
+
+ @Override
+ public void complete() {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> completeRequest(Collections.emptyList(), batchSize, requestTimestamp),
+ "Mark in-flight request as completed successfully",
+ batchSize);
+ }
+ }
+
+ @Override
+ public void completeExceptionally(Exception e) {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
Review Comment:
It is insignificant whether the cancellation was successful or not as we mainly rely on `iscompleted` boolean to avoid race conditions, in other words we would probably do nothing in case the future failed to cancel anyway.
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "jeyhunkarimov (via GitHub)" <gi...@apache.org>.
jeyhunkarimov commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1614421157
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriter.java:
##########
@@ -181,15 +187,88 @@ public abstract class AsyncSinkWriter<InputT, RequestEntryT extends Serializable
* <p>During checkpointing, the sink needs to ensure that there are no outstanding in-flight
* requests.
*
+ * <p>This method is deprecated in favor of {@code submitRequestEntries( List<RequestEntryT>
+ * requestEntries, ResultHandler<RequestEntryT> resultHandler)}
Review Comment:
`<p>This method is deprecated in favor of {@code submitRequestEntries(List<RequestEntryT>
* requestEntries, ResultHandler<RequestEntryT> resultHandler)}`
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriter.java:
##########
@@ -533,4 +607,77 @@ private int getNextBatchSizeLimit() {
protected Consumer<Exception> getFatalExceptionCons() {
return fatalExceptionCons;
}
+
+ /** An implementation of {@link ResultHandler} that supports timeout. */
+ private class AsyncSinkWriterResultHandler implements ResultHandler<RequestEntryT> {
+ private final ScheduledFuture<?> scheduledFuture;
+ private final long requestTimestamp;
+ private final int batchSize;
+ private final AtomicBoolean isCompleted = new AtomicBoolean(false);
+ private final List<RequestEntryT> batchEntries;
+
+ public AsyncSinkWriterResultHandler(
+ long requestTimestamp, List<RequestEntryT> batchEntries, RequestInfo requestInfo) {
+ this.scheduledFuture =
+ timeService.registerTimer(
+ timeService.getCurrentProcessingTime() + requestTimeoutMS,
+ instant -> this.timeout());
+ this.requestTimestamp = requestTimestamp;
+ this.batchEntries = batchEntries;
+ this.batchSize = requestInfo.getBatchSize();
+ }
+
+ @Override
+ public void complete() {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> completeRequest(Collections.emptyList(), batchSize, requestTimestamp),
+ "Mark in-flight request as completed successfully",
+ batchSize);
+ }
+ }
+
+ @Override
+ public void completeExceptionally(Exception e) {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
Review Comment:
Why dont we check the return value of `scheduledFuture.cancel(...)`?
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/AsyncSinkBase.java:
##########
@@ -73,6 +97,8 @@ protected AsyncSinkBase(
this.maxBatchSizeInBytes = maxBatchSizeInBytes;
this.maxTimeInBufferMS = maxTimeInBufferMS;
this.maxRecordSizeInBytes = maxRecordSizeInBytes;
+ this.requestTimeoutMS = requestTimeoutMS;
Review Comment:
`requireNonNull(requestTimeoutMS)` ?
##########
flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriterTimeoutTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.flink.connector.base.sink.writer;
+
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.connector.base.sink.writer.config.AsyncSinkWriterConfiguration;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/** Test for timeout functionalities of {@link AsyncSinkWriter}. */
+public class AsyncSinkWriterTimeoutTest {
+ private final List<Long> destination = new ArrayList<>();
+
+ @Test
+ void writerShouldNotRetryIfRequestIsProcessedBeforeTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(10);
+ writer.deliverMessage();
+ tpts.setCurrentTime(120);
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldRetryOnTimeoutIfFailOnErrorIsFalse() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ // element should be requeued back after timeout
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout
+ writer.deliverMessage();
+ // flush outstanding mailbox messages containing resubmission of the element
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L, 1L);
+ }
+
+ @Test
+ void writerShouldFailOnTimeoutIfFailOnErrorIsTrue() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withCauseInstanceOf(TimeoutException.class)
+ .havingCause()
+ .withMessage("Request timed out after 100ms with failOnTimeout set to true.");
+ }
+
+ @Test
+ void writerShouldDiscardRetriedEntriesOnTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ writer.setShouldFailRequest(true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout, message request would fail but elements should be
+ // discarded as timeout already occurred
+ writer.deliverMessage();
+ // flush outstanding mailbox messages to resubmit timed out elements
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldFailOnFatalError() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.setFatalError(new FlinkRuntimeException("Fatal error"));
+ writer.write("1", null);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withMessage("Fatal error");
+ }
+
+ /** Test that the writer can handle a timeout. */
+ private class TimeoutWriter extends AsyncSinkWriter<String, Long> {
+ private Exception fatalError;
+ private final CountDownLatch completionLatch;
+ private Thread submitThread;
+
+ private boolean shouldFailRequest = false;
+
+ public TimeoutWriter(
+ WriterInitContext writerInitContext,
+ int maxBatchSize,
+ long maximumTimeInBufferMs,
+ long requestTimeout,
+ boolean failOnTimeout) {
+ super(
+ (ElementConverter<String, Long>) (element, context) -> Long.parseLong(element),
+ writerInitContext,
+ AsyncSinkWriterConfiguration.builder()
+ .setMaxBatchSize(maxBatchSize)
+ .setMaxBatchSizeInBytes(Long.MAX_VALUE)
+ .setMaxInFlightRequests(Integer.MAX_VALUE)
+ .setMaxBufferedRequests(Integer.MAX_VALUE)
+ .setMaxTimeInBufferMS(maximumTimeInBufferMs)
+ .setMaxRecordSizeInBytes(Long.MAX_VALUE)
+ .setRequestTimeoutMS(requestTimeout)
+ .setFailOnTimeout(failOnTimeout)
+ .build(),
+ Collections.emptyList());
+ this.completionLatch = new CountDownLatch(1);
+ }
+
+ @Override
+ protected void submitRequestEntries(
+ List<Long> requestEntries, ResultHandler<Long> resultHandler) {
+ submitThread =
Review Comment:
Maybe use threadpool, instead of creating and destroying threads for each test?
##########
flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriterTimeoutTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.flink.connector.base.sink.writer;
+
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.connector.base.sink.writer.config.AsyncSinkWriterConfiguration;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/** Test for timeout functionalities of {@link AsyncSinkWriter}. */
+public class AsyncSinkWriterTimeoutTest {
+ private final List<Long> destination = new ArrayList<>();
+
+ @Test
+ void writerShouldNotRetryIfRequestIsProcessedBeforeTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(10);
+ writer.deliverMessage();
+ tpts.setCurrentTime(120);
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldRetryOnTimeoutIfFailOnErrorIsFalse() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ // element should be requeued back after timeout
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout
+ writer.deliverMessage();
+ // flush outstanding mailbox messages containing resubmission of the element
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L, 1L);
+ }
+
+ @Test
+ void writerShouldFailOnTimeoutIfFailOnErrorIsTrue() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withCauseInstanceOf(TimeoutException.class)
+ .havingCause()
+ .withMessage("Request timed out after 100ms with failOnTimeout set to true.");
Review Comment:
`hasMessageContaining` ?
##########
flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriterTimeoutTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.flink.connector.base.sink.writer;
+
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.connector.base.sink.writer.config.AsyncSinkWriterConfiguration;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/** Test for timeout functionalities of {@link AsyncSinkWriter}. */
+public class AsyncSinkWriterTimeoutTest {
+ private final List<Long> destination = new ArrayList<>();
+
+ @Test
+ void writerShouldNotRetryIfRequestIsProcessedBeforeTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(10);
+ writer.deliverMessage();
+ tpts.setCurrentTime(120);
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldRetryOnTimeoutIfFailOnErrorIsFalse() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ // element should be requeued back after timeout
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout
+ writer.deliverMessage();
+ // flush outstanding mailbox messages containing resubmission of the element
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L, 1L);
+ }
+
+ @Test
+ void writerShouldFailOnTimeoutIfFailOnErrorIsTrue() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withCauseInstanceOf(TimeoutException.class)
+ .havingCause()
+ .withMessage("Request timed out after 100ms with failOnTimeout set to true.");
+ }
+
+ @Test
+ void writerShouldDiscardRetriedEntriesOnTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ writer.setShouldFailRequest(true);
Review Comment:
Why dont you parametrize `shouldFailRequest` for each test? In this case, you might need to clean up the state of `shouldfailRequest` manually for each test no? (e.g., for this test there is no cleanup)
##########
flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriterTimeoutTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.flink.connector.base.sink.writer;
+
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.connector.base.sink.writer.config.AsyncSinkWriterConfiguration;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/** Test for timeout functionalities of {@link AsyncSinkWriter}. */
+public class AsyncSinkWriterTimeoutTest {
+ private final List<Long> destination = new ArrayList<>();
+
+ @Test
+ void writerShouldNotRetryIfRequestIsProcessedBeforeTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(10);
+ writer.deliverMessage();
+ tpts.setCurrentTime(120);
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldRetryOnTimeoutIfFailOnErrorIsFalse() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ // element should be requeued back after timeout
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout
+ writer.deliverMessage();
+ // flush outstanding mailbox messages containing resubmission of the element
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L, 1L);
+ }
+
+ @Test
+ void writerShouldFailOnTimeoutIfFailOnErrorIsTrue() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withCauseInstanceOf(TimeoutException.class)
+ .havingCause()
+ .withMessage("Request timed out after 100ms with failOnTimeout set to true.");
+ }
+
+ @Test
+ void writerShouldDiscardRetriedEntriesOnTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ writer.setShouldFailRequest(true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout, message request would fail but elements should be
+ // discarded as timeout already occurred
+ writer.deliverMessage();
+ // flush outstanding mailbox messages to resubmit timed out elements
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldFailOnFatalError() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.setFatalError(new FlinkRuntimeException("Fatal error"));
+ writer.write("1", null);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withMessage("Fatal error");
Review Comment:
`hasMessageContaining` ?
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriter.java:
##########
@@ -533,4 +607,77 @@ private int getNextBatchSizeLimit() {
protected Consumer<Exception> getFatalExceptionCons() {
return fatalExceptionCons;
}
+
+ /** An implementation of {@link ResultHandler} that supports timeout. */
+ private class AsyncSinkWriterResultHandler implements ResultHandler<RequestEntryT> {
+ private final ScheduledFuture<?> scheduledFuture;
+ private final long requestTimestamp;
+ private final int batchSize;
+ private final AtomicBoolean isCompleted = new AtomicBoolean(false);
+ private final List<RequestEntryT> batchEntries;
+
+ public AsyncSinkWriterResultHandler(
+ long requestTimestamp, List<RequestEntryT> batchEntries, RequestInfo requestInfo) {
+ this.scheduledFuture =
+ timeService.registerTimer(
+ timeService.getCurrentProcessingTime() + requestTimeoutMS,
+ instant -> this.timeout());
+ this.requestTimestamp = requestTimestamp;
+ this.batchEntries = batchEntries;
+ this.batchSize = requestInfo.getBatchSize();
+ }
+
+ @Override
+ public void complete() {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> completeRequest(Collections.emptyList(), batchSize, requestTimestamp),
+ "Mark in-flight request as completed successfully",
+ batchSize);
+ }
+ }
+
+ @Override
+ public void completeExceptionally(Exception e) {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> getFatalExceptionCons().accept(e),
+ "Mark in-flight request as failed with fatal exception",
+ e.getMessage());
+ }
+ }
+
+ @Override
+ public void retryForEntries(List<RequestEntryT> requestEntriesToRetry) {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> completeRequest(requestEntriesToRetry, batchSize, requestTimestamp),
+ "Mark in-flight request as completed with %d failed request entries",
Review Comment:
You wanted to use `String.format` here?
##########
flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriterTimeoutTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.flink.connector.base.sink.writer;
+
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.connector.base.sink.writer.config.AsyncSinkWriterConfiguration;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/** Test for timeout functionalities of {@link AsyncSinkWriter}. */
+public class AsyncSinkWriterTimeoutTest {
+ private final List<Long> destination = new ArrayList<>();
+
+ @Test
+ void writerShouldNotRetryIfRequestIsProcessedBeforeTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(10);
+ writer.deliverMessage();
+ tpts.setCurrentTime(120);
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldRetryOnTimeoutIfFailOnErrorIsFalse() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ // element should be requeued back after timeout
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout
+ writer.deliverMessage();
+ // flush outstanding mailbox messages containing resubmission of the element
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L, 1L);
+ }
+
+ @Test
+ void writerShouldFailOnTimeoutIfFailOnErrorIsTrue() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withCauseInstanceOf(TimeoutException.class)
+ .havingCause()
+ .withMessage("Request timed out after 100ms with failOnTimeout set to true.");
+ }
+
+ @Test
+ void writerShouldDiscardRetriedEntriesOnTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ writer.setShouldFailRequest(true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout, message request would fail but elements should be
+ // discarded as timeout already occurred
+ writer.deliverMessage();
+ // flush outstanding mailbox messages to resubmit timed out elements
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldFailOnFatalError() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.setFatalError(new FlinkRuntimeException("Fatal error"));
+ writer.write("1", null);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withMessage("Fatal error");
+ }
+
+ /** Test that the writer can handle a timeout. */
+ private class TimeoutWriter extends AsyncSinkWriter<String, Long> {
+ private Exception fatalError;
+ private final CountDownLatch completionLatch;
+ private Thread submitThread;
+
+ private boolean shouldFailRequest = false;
+
+ public TimeoutWriter(
+ WriterInitContext writerInitContext,
+ int maxBatchSize,
+ long maximumTimeInBufferMs,
+ long requestTimeout,
+ boolean failOnTimeout) {
+ super(
+ (ElementConverter<String, Long>) (element, context) -> Long.parseLong(element),
+ writerInitContext,
+ AsyncSinkWriterConfiguration.builder()
+ .setMaxBatchSize(maxBatchSize)
+ .setMaxBatchSizeInBytes(Long.MAX_VALUE)
+ .setMaxInFlightRequests(Integer.MAX_VALUE)
+ .setMaxBufferedRequests(Integer.MAX_VALUE)
+ .setMaxTimeInBufferMS(maximumTimeInBufferMs)
+ .setMaxRecordSizeInBytes(Long.MAX_VALUE)
+ .setRequestTimeoutMS(requestTimeout)
+ .setFailOnTimeout(failOnTimeout)
+ .build(),
+ Collections.emptyList());
+ this.completionLatch = new CountDownLatch(1);
+ }
+
+ @Override
+ protected void submitRequestEntries(
+ List<Long> requestEntries, ResultHandler<Long> resultHandler) {
+ submitThread =
+ new Thread(
+ () -> {
+ while (completionLatch.getCount() > 0) {
+ try {
+ completionLatch.await();
+ } catch (InterruptedException e) {
+ fail("Submission thread must not be interrupted.");
+ }
+ }
+ submitRequestEntriesSync(requestEntries, resultHandler);
+ });
+
+ submitThread.start();
+ }
+
+ private void submitRequestEntriesSync(
+ List<Long> requestEntries, ResultHandler<Long> resultHandler) {
+ if (fatalError != null) {
+ resultHandler.completeExceptionally(fatalError);
+ } else if (shouldFailRequest) {
+ shouldFailRequest = false;
+ resultHandler.retryForEntries(requestEntries);
+ } else {
+ destination.addAll(requestEntries);
+ resultHandler.complete();
+ }
+ }
+
+ @Override
+ protected long getSizeInBytes(Long requestEntry) {
+ return 8;
+ }
+
+ public void setFatalError(Exception fatalError) {
Review Comment:
Should be ensured thread-safe modification
##########
flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriterTimeoutTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.flink.connector.base.sink.writer;
+
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.connector.base.sink.writer.config.AsyncSinkWriterConfiguration;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/** Test for timeout functionalities of {@link AsyncSinkWriter}. */
+public class AsyncSinkWriterTimeoutTest {
+ private final List<Long> destination = new ArrayList<>();
+
+ @Test
+ void writerShouldNotRetryIfRequestIsProcessedBeforeTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(10);
+ writer.deliverMessage();
+ tpts.setCurrentTime(120);
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldRetryOnTimeoutIfFailOnErrorIsFalse() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ // element should be requeued back after timeout
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout
+ writer.deliverMessage();
+ // flush outstanding mailbox messages containing resubmission of the element
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L, 1L);
+ }
+
+ @Test
+ void writerShouldFailOnTimeoutIfFailOnErrorIsTrue() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withCauseInstanceOf(TimeoutException.class)
+ .havingCause()
+ .withMessage("Request timed out after 100ms with failOnTimeout set to true.");
+ }
+
+ @Test
+ void writerShouldDiscardRetriedEntriesOnTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ writer.setShouldFailRequest(true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout, message request would fail but elements should be
+ // discarded as timeout already occurred
+ writer.deliverMessage();
+ // flush outstanding mailbox messages to resubmit timed out elements
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldFailOnFatalError() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.setFatalError(new FlinkRuntimeException("Fatal error"));
+ writer.write("1", null);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withMessage("Fatal error");
+ }
+
+ /** Test that the writer can handle a timeout. */
+ private class TimeoutWriter extends AsyncSinkWriter<String, Long> {
+ private Exception fatalError;
+ private final CountDownLatch completionLatch;
+ private Thread submitThread;
+
+ private boolean shouldFailRequest = false;
Review Comment:
Should be ensured thread-safe modification
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1614910580
##########
flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriterTimeoutTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.flink.connector.base.sink.writer;
+
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.connector.base.sink.writer.config.AsyncSinkWriterConfiguration;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/** Test for timeout functionalities of {@link AsyncSinkWriter}. */
+public class AsyncSinkWriterTimeoutTest {
+ private final List<Long> destination = new ArrayList<>();
+
+ @Test
+ void writerShouldNotRetryIfRequestIsProcessedBeforeTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(10);
+ writer.deliverMessage();
+ tpts.setCurrentTime(120);
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldRetryOnTimeoutIfFailOnErrorIsFalse() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ // element should be requeued back after timeout
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout
+ writer.deliverMessage();
+ // flush outstanding mailbox messages containing resubmission of the element
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L, 1L);
+ }
+
+ @Test
+ void writerShouldFailOnTimeoutIfFailOnErrorIsTrue() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withCauseInstanceOf(TimeoutException.class)
+ .havingCause()
+ .withMessage("Request timed out after 100ms with failOnTimeout set to true.");
+ }
+
+ @Test
+ void writerShouldDiscardRetriedEntriesOnTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ writer.setShouldFailRequest(true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout, message request would fail but elements should be
+ // discarded as timeout already occurred
+ writer.deliverMessage();
+ // flush outstanding mailbox messages to resubmit timed out elements
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldFailOnFatalError() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.setFatalError(new FlinkRuntimeException("Fatal error"));
+ writer.write("1", null);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withMessage("Fatal error");
+ }
+
+ /** Test that the writer can handle a timeout. */
+ private class TimeoutWriter extends AsyncSinkWriter<String, Long> {
+ private Exception fatalError;
+ private final CountDownLatch completionLatch;
+ private Thread submitThread;
+
+ private boolean shouldFailRequest = false;
+
+ public TimeoutWriter(
+ WriterInitContext writerInitContext,
+ int maxBatchSize,
+ long maximumTimeInBufferMs,
+ long requestTimeout,
+ boolean failOnTimeout) {
+ super(
+ (ElementConverter<String, Long>) (element, context) -> Long.parseLong(element),
+ writerInitContext,
+ AsyncSinkWriterConfiguration.builder()
+ .setMaxBatchSize(maxBatchSize)
+ .setMaxBatchSizeInBytes(Long.MAX_VALUE)
+ .setMaxInFlightRequests(Integer.MAX_VALUE)
+ .setMaxBufferedRequests(Integer.MAX_VALUE)
+ .setMaxTimeInBufferMS(maximumTimeInBufferMs)
+ .setMaxRecordSizeInBytes(Long.MAX_VALUE)
+ .setRequestTimeoutMS(requestTimeout)
+ .setFailOnTimeout(failOnTimeout)
+ .build(),
+ Collections.emptyList());
+ this.completionLatch = new CountDownLatch(1);
+ }
+
+ @Override
+ protected void submitRequestEntries(
+ List<Long> requestEntries, ResultHandler<Long> resultHandler) {
+ submitThread =
+ new Thread(
+ () -> {
+ while (completionLatch.getCount() > 0) {
+ try {
+ completionLatch.await();
+ } catch (InterruptedException e) {
+ fail("Submission thread must not be interrupted.");
+ }
+ }
+ submitRequestEntriesSync(requestEntries, resultHandler);
+ });
+
+ submitThread.start();
+ }
+
+ private void submitRequestEntriesSync(
+ List<Long> requestEntries, ResultHandler<Long> resultHandler) {
+ if (fatalError != null) {
+ resultHandler.completeExceptionally(fatalError);
+ } else if (shouldFailRequest) {
+ shouldFailRequest = false;
+ resultHandler.retryForEntries(requestEntries);
+ } else {
+ destination.addAll(requestEntries);
+ resultHandler.complete();
+ }
+ }
+
+ @Override
+ protected long getSizeInBytes(Long requestEntry) {
+ return 8;
+ }
+
+ public void setFatalError(Exception fatalError) {
Review Comment:
Only modified by test thread
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1614907240
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/AsyncSinkBase.java:
##########
@@ -54,6 +54,8 @@ public abstract class AsyncSinkBase<InputT, RequestEntryT extends Serializable>
private final long maxBatchSizeInBytes;
private final long maxTimeInBufferMS;
private final long maxRecordSizeInBytes;
+ private Long requestTimeoutMS;
+ private Boolean failOnTimeout;
Review Comment:
Agreed, I used default values instead
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1629436707
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriter.java:
##########
@@ -533,4 +607,77 @@ private int getNextBatchSizeLimit() {
protected Consumer<Exception> getFatalExceptionCons() {
return fatalExceptionCons;
}
+
+ /** An implementation of {@link ResultHandler} that supports timeout. */
+ private class AsyncSinkWriterResultHandler implements ResultHandler<RequestEntryT> {
+ private final ScheduledFuture<?> scheduledFuture;
+ private final long requestTimestamp;
+ private final int batchSize;
+ private final AtomicBoolean isCompleted = new AtomicBoolean(false);
+ private final List<RequestEntryT> batchEntries;
+
+ public AsyncSinkWriterResultHandler(
+ long requestTimestamp, List<RequestEntryT> batchEntries, RequestInfo requestInfo) {
+ this.scheduledFuture =
+ timeService.registerTimer(
+ timeService.getCurrentProcessingTime() + requestTimeoutMS,
+ instant -> this.timeout());
+ this.requestTimestamp = requestTimestamp;
+ this.batchEntries = batchEntries;
+ this.batchSize = requestInfo.getBatchSize();
+ }
+
+ @Override
+ public void complete() {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> completeRequest(Collections.emptyList(), batchSize, requestTimestamp),
+ "Mark in-flight request as completed successfully",
+ batchSize);
+ }
+ }
+
+ @Override
+ public void completeExceptionally(Exception e) {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> getFatalExceptionCons().accept(e),
+ "Mark in-flight request as failed with fatal exception",
Review Comment:
The exception is propagated to task runner and should be visible as Error, I don't find a value of logging it in debug logs
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1614905336
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriter.java:
##########
@@ -533,4 +607,77 @@ private int getNextBatchSizeLimit() {
protected Consumer<Exception> getFatalExceptionCons() {
return fatalExceptionCons;
}
+
+ /** An implementation of {@link ResultHandler} that supports timeout. */
+ private class AsyncSinkWriterResultHandler implements ResultHandler<RequestEntryT> {
+ private final ScheduledFuture<?> scheduledFuture;
+ private final long requestTimestamp;
+ private final int batchSize;
+ private final AtomicBoolean isCompleted = new AtomicBoolean(false);
+ private final List<RequestEntryT> batchEntries;
+
+ public AsyncSinkWriterResultHandler(
+ long requestTimestamp, List<RequestEntryT> batchEntries, RequestInfo requestInfo) {
+ this.scheduledFuture =
+ timeService.registerTimer(
+ timeService.getCurrentProcessingTime() + requestTimeoutMS,
+ instant -> this.timeout());
+ this.requestTimestamp = requestTimestamp;
+ this.batchEntries = batchEntries;
+ this.batchSize = requestInfo.getBatchSize();
+ }
+
+ @Override
+ public void complete() {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> completeRequest(Collections.emptyList(), batchSize, requestTimestamp),
+ "Mark in-flight request as completed successfully",
+ batchSize);
+ }
+ }
+
+ @Override
+ public void completeExceptionally(Exception e) {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> getFatalExceptionCons().accept(e),
+ "Mark in-flight request as failed with fatal exception",
+ e.getMessage());
+ }
+ }
+
+ @Override
+ public void retryForEntries(List<RequestEntryT> requestEntriesToRetry) {
+ if (isCompleted.compareAndSet(false, true)) {
+ scheduledFuture.cancel(false);
+ mailboxExecutor.execute(
+ () -> completeRequest(requestEntriesToRetry, batchSize, requestTimestamp),
+ "Mark in-flight request as completed with %d failed request entries",
Review Comment:
I think it is already supported by [mailbox executor](https://github.com/apache/flink/blob/5b535e1410782a2d42127e00d77815d6ab7068ed/flink-core/src/main/java/org/apache/flink/api/common/operators/MailboxExecutor.java#L120)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "dannycranmer (via GitHub)" <gi...@apache.org>.
dannycranmer commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1613883830
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/AsyncSinkBase.java:
##########
@@ -54,6 +54,8 @@ public abstract class AsyncSinkBase<InputT, RequestEntryT extends Serializable>
private final long maxBatchSizeInBytes;
private final long maxTimeInBufferMS;
private final long maxRecordSizeInBytes;
+ private Long requestTimeoutMS;
+ private Boolean failOnTimeout;
Review Comment:
Why `Boolean` and not `boolean` ?
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/AsyncSinkBase.java:
##########
@@ -54,6 +54,8 @@ public abstract class AsyncSinkBase<InputT, RequestEntryT extends Serializable>
private final long maxBatchSizeInBytes;
private final long maxTimeInBufferMS;
private final long maxRecordSizeInBytes;
+ private Long requestTimeoutMS;
+ private Boolean failOnTimeout;
Review Comment:
Also, to avoid null checks could default request timeout ms to -1 for off
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/AsyncSinkBase.java:
##########
@@ -54,6 +54,8 @@ public abstract class AsyncSinkBase<InputT, RequestEntryT extends Serializable>
private final long maxBatchSizeInBytes;
private final long maxTimeInBufferMS;
private final long maxRecordSizeInBytes;
+ private Long requestTimeoutMS;
+ private Boolean failOnTimeout;
Review Comment:
Make `final`
##########
flink-connectors/flink-connector-base/src/main/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriter.java:
##########
@@ -181,15 +187,88 @@ public abstract class AsyncSinkWriter<InputT, RequestEntryT extends Serializable
* <p>During checkpointing, the sink needs to ensure that there are no outstanding in-flight
* requests.
*
+ * <p>This method is deprecated in favor of {@code submitRequestEntries( List<RequestEntryT>
+ * requestEntries, ResultHandler<RequestEntryT> resultHandler)}
+ *
* @param requestEntries a set of request entries that should be sent to the destination
* @param requestToRetry the {@code accept} method should be called on this Consumer once the
* processing of the {@code requestEntries} are complete. Any entries that encountered
* difficulties in persisting should be re-queued through {@code requestToRetry} by
* including that element in the collection of {@code RequestEntryT}s passed to the {@code
* accept} method. All other elements are assumed to have been successfully persisted.
*/
- protected abstract void submitRequestEntries(
- List<RequestEntryT> requestEntries, Consumer<List<RequestEntryT>> requestToRetry);
+ @Deprecated
+ protected void submitRequestEntries(
+ List<RequestEntryT> requestEntries, Consumer<List<RequestEntryT>> requestToRetry) {
+ throw new UnsupportedOperationException(
+ "This method is deprecated. Please override the method that accepts a ResultHandler.");
+ }
+
+ /**
+ * This method specifies how to persist buffered request entries into the destination. It is
+ * implemented when support for a new destination is added.
+ *
+ * <p>The method is invoked with a set of request entries according to the buffering hints (and
+ * the valid limits of the destination). The logic then needs to create and execute the request
+ * asynchronously against the destination (ideally by batching together multiple request entries
+ * to increase efficiency). The logic also needs to identify individual request entries that
+ * were not persisted successfully and resubmit them using the {@code requestToRetry} callback.
+ *
+ * <p>From a threading perspective, the mailbox thread will call this method and initiate the
+ * asynchronous request to persist the {@code requestEntries}. NOTE: The client must support
Review Comment:
Well... You could spin up a thread pool in the sink, and not necessarily in the client
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on PR #24839:
URL: https://github.com/apache/flink/pull/24839#issuecomment-2147156792
@flinkbot run azure
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1614908849
##########
flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriterTimeoutTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.flink.connector.base.sink.writer;
+
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.connector.base.sink.writer.config.AsyncSinkWriterConfiguration;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/** Test for timeout functionalities of {@link AsyncSinkWriter}. */
+public class AsyncSinkWriterTimeoutTest {
+ private final List<Long> destination = new ArrayList<>();
+
+ @Test
+ void writerShouldNotRetryIfRequestIsProcessedBeforeTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(10);
+ writer.deliverMessage();
+ tpts.setCurrentTime(120);
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldRetryOnTimeoutIfFailOnErrorIsFalse() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ // element should be requeued back after timeout
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout
+ writer.deliverMessage();
+ // flush outstanding mailbox messages containing resubmission of the element
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L, 1L);
+ }
+
+ @Test
+ void writerShouldFailOnTimeoutIfFailOnErrorIsTrue() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withCauseInstanceOf(TimeoutException.class)
+ .havingCause()
+ .withMessage("Request timed out after 100ms with failOnTimeout set to true.");
+ }
+
+ @Test
+ void writerShouldDiscardRetriedEntriesOnTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ writer.setShouldFailRequest(true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout, message request would fail but elements should be
+ // discarded as timeout already occurred
+ writer.deliverMessage();
+ // flush outstanding mailbox messages to resubmit timed out elements
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldFailOnFatalError() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.setFatalError(new FlinkRuntimeException("Fatal error"));
+ writer.write("1", null);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withMessage("Fatal error");
+ }
+
+ /** Test that the writer can handle a timeout. */
+ private class TimeoutWriter extends AsyncSinkWriter<String, Long> {
+ private Exception fatalError;
+ private final CountDownLatch completionLatch;
+ private Thread submitThread;
+
+ private boolean shouldFailRequest = false;
+
+ public TimeoutWriter(
+ WriterInitContext writerInitContext,
+ int maxBatchSize,
+ long maximumTimeInBufferMs,
+ long requestTimeout,
+ boolean failOnTimeout) {
+ super(
+ (ElementConverter<String, Long>) (element, context) -> Long.parseLong(element),
+ writerInitContext,
+ AsyncSinkWriterConfiguration.builder()
+ .setMaxBatchSize(maxBatchSize)
+ .setMaxBatchSizeInBytes(Long.MAX_VALUE)
+ .setMaxInFlightRequests(Integer.MAX_VALUE)
+ .setMaxBufferedRequests(Integer.MAX_VALUE)
+ .setMaxTimeInBufferMS(maximumTimeInBufferMs)
+ .setMaxRecordSizeInBytes(Long.MAX_VALUE)
+ .setRequestTimeoutMS(requestTimeout)
+ .setFailOnTimeout(failOnTimeout)
+ .build(),
+ Collections.emptyList());
+ this.completionLatch = new CountDownLatch(1);
+ }
+
+ @Override
+ protected void submitRequestEntries(
+ List<Long> requestEntries, ResultHandler<Long> resultHandler) {
+ submitThread =
Review Comment:
good suggestion
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "vahmed-hamdy (via GitHub)" <gi...@apache.org>.
vahmed-hamdy commented on code in PR #24839:
URL: https://github.com/apache/flink/pull/24839#discussion_r1614909440
##########
flink-connectors/flink-connector-base/src/test/java/org/apache/flink/connector/base/sink/writer/AsyncSinkWriterTimeoutTest.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.flink.connector.base.sink.writer;
+
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.connector.base.sink.writer.config.AsyncSinkWriterConfiguration;
+import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService;
+import org.apache.flink.util.FlinkRuntimeException;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeoutException;
+
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+/** Test for timeout functionalities of {@link AsyncSinkWriter}. */
+public class AsyncSinkWriterTimeoutTest {
+ private final List<Long> destination = new ArrayList<>();
+
+ @Test
+ void writerShouldNotRetryIfRequestIsProcessedBeforeTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(10);
+ writer.deliverMessage();
+ tpts.setCurrentTime(120);
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L);
+ }
+
+ @Test
+ void writerShouldRetryOnTimeoutIfFailOnErrorIsFalse() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ // element should be requeued back after timeout
+ tpts.setCurrentTime(110);
+ // deliver initial message after timeout
+ writer.deliverMessage();
+ // flush outstanding mailbox messages containing resubmission of the element
+ writer.flush(false);
+ assertThat(destination).containsExactly(1L, 1L);
+ }
+
+ @Test
+ void writerShouldFailOnTimeoutIfFailOnErrorIsTrue() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, true);
+ tpts.setCurrentTime(0);
+ writer.write("1", null);
+ tpts.setCurrentTime(110);
+ writer.deliverMessage();
+ assertThatExceptionOfType(FlinkRuntimeException.class)
+ .isThrownBy(() -> writer.flush(false))
+ .withCauseInstanceOf(TimeoutException.class)
+ .havingCause()
+ .withMessage("Request timed out after 100ms with failOnTimeout set to true.");
+ }
+
+ @Test
+ void writerShouldDiscardRetriedEntriesOnTimeout() throws Exception {
+ TestSinkInitContextAnyThreadMailbox context = new TestSinkInitContextAnyThreadMailbox();
+ TestProcessingTimeService tpts = context.getTestProcessingTimeService();
+ TimeoutWriter writer = new TimeoutWriter(context, 1, 10, 100, false);
+ writer.setShouldFailRequest(true);
Review Comment:
I am not sure I follow, could you explain more?
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [FLINK-35435] Add timeout Configuration to Async Sink [flink]
Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #24839:
URL: https://github.com/apache/flink/pull/24839#issuecomment-2129354320
<!--
Meta data
{
"version" : 1,
"metaDataEntries" : [ {
"hash" : "2b91a9c92af7d97d492a9c83d43d7c544f85d355",
"status" : "UNKNOWN",
"url" : "TBD",
"triggerID" : "2b91a9c92af7d97d492a9c83d43d7c544f85d355",
"triggerType" : "PUSH"
} ]
}-->
## CI report:
* 2b91a9c92af7d97d492a9c83d43d7c544f85d355 UNKNOWN
<details>
<summary>Bot commands</summary>
The @flinkbot bot supports the following commands:
- `@flinkbot run azure` re-run the last Azure build
</details>
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org