This is an automated email from the ASF dual-hosted git repository.
JNSimba pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris-kafka-connector.git
The following commit(s) were added to refs/heads/master by this push:
new 17b7015 [fix] With enable.combine.flush, retry the failed async load
before failing the task (#113)
17b7015 is described below
commit 17b7015e258216aab602c37483866f38378340bb
Author: stinger1206 <[email protected]>
AuthorDate: Tue Dec 1 10:56:55 2026 +0200
[fix] With enable.combine.flush, retry the failed async load before failing
the task (#113)
* [fix] Keep failing after a failed async stream load so offsets cannot
advance past it
With enable.combine.flush, a failed background load dropped its buffer from
the flush queue, and DorisSinkTask.put() reset the failure when it restarted
the executor via sink.init(); Kafka Connect re-delivered only the current
batch, and the next preCommit returned the last seen offsets past the lost
records.
AsyncDorisStreamLoad now latches the failure for the lifetime of the object:
checkFlushException() keeps throwing after start() restarts the executor, so
put() exhausts its retries and the task fails. While the task is still
running, every commit attempt is skipped and rewound to the last committed
offsets; once the task is restarted, new writers are created and consumption
resumes from the last committed offsets, replaying the dropped records.
---
.../writer/load/AsyncDorisStreamLoad.java | 20 +-
.../service/TestCombineFlushFailedLoadCommits.java | 333 ++++++++
.../load/TestAsyncStreamLoadFailureLatch.java | 881 +++++++++++++++++++++
3 files changed, 1232 insertions(+), 2 deletions(-)
diff --git
a/src/main/java/org/apache/doris/kafka/connector/writer/load/AsyncDorisStreamLoad.java
b/src/main/java/org/apache/doris/kafka/connector/writer/load/AsyncDorisStreamLoad.java
index 900e45b..dbbc6f0 100644
---
a/src/main/java/org/apache/doris/kafka/connector/writer/load/AsyncDorisStreamLoad.java
+++
b/src/main/java/org/apache/doris/kafka/connector/writer/load/AsyncDorisStreamLoad.java
@@ -126,7 +126,6 @@ public class AsyncDorisStreamLoad extends DataLoad {
public void start() {
if (!loadThreadAlive) {
this.loadExecutorService.execute(loadAsyncExecutor);
- this.exception.set(null);
}
}
@@ -204,7 +203,7 @@ public class AsyncDorisStreamLoad extends DataLoad {
// When the label is empty, it is the eof buffer for
checkpoint flush.
continue;
}
- load(buffer.getLabel(), buffer);
+ loadWithRetry(buffer);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
@@ -226,6 +225,23 @@ public class AsyncDorisStreamLoad extends DataLoad {
loadThreadAlive = false;
}
+ private void loadWithRetry(RecordBuffer buffer) throws IOException,
InterruptedException {
+ String label = buffer.getLabel();
+ for (int retry = 0; retry <= dorisOptions.getMaxRetries();
retry++) {
+ try {
+ load(label, buffer);
+ return;
+ } catch (Exception e) {
+ if (retry == dorisOptions.getMaxRetries()) {
+ throw e;
+ }
+ LOG.warn("stream load failed for label {}, will retry",
label, e);
+ label = buffer.getLabel() + "_" + (retry + 1);
+ Thread.sleep(dorisOptions.getRetryIntervalMs());
+ }
+ }
+ }
+
/** execute stream load. */
public void load(String label, RecordBuffer buffer) throws IOException
{
if (enableGroupCommit) {
diff --git
a/src/test/java/org/apache/doris/kafka/connector/service/TestCombineFlushFailedLoadCommits.java
b/src/test/java/org/apache/doris/kafka/connector/service/TestCombineFlushFailedLoadCommits.java
new file mode 100644
index 0000000..e3fdbbd
--- /dev/null
+++
b/src/test/java/org/apache/doris/kafka/connector/service/TestCombineFlushFailedLoadCommits.java
@@ -0,0 +1,333 @@
+/*
+ * 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.doris.kafka.connector.service;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Field;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+import org.apache.doris.kafka.connector.cfg.DorisOptions;
+import org.apache.doris.kafka.connector.cfg.DorisSinkConnectorConfig;
+import org.apache.doris.kafka.connector.exception.DorisException;
+import org.apache.doris.kafka.connector.testutil.RecordingHttpClient;
+import org.apache.doris.kafka.connector.utils.BackendUtils;
+import org.apache.doris.kafka.connector.writer.AsyncStreamLoadWriter;
+import org.apache.doris.kafka.connector.writer.load.AsyncDorisStreamLoad;
+import org.apache.kafka.clients.consumer.OffsetAndMetadata;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.connect.data.Schema;
+import org.apache.kafka.connect.errors.RetriableException;
+import org.apache.kafka.connect.sink.SinkRecord;
+import org.apache.kafka.connect.sink.SinkTaskContext;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.MockedStatic;
+
+/**
+ * Regression tests for combined-flush mode: when a background stream load
fails, the failed buffer
+ * is dropped from the flush queue, so neither {@code put()} redelivery nor
{@code preCommit()} may
+ * advance offsets past the dropped records. The task must keep failing until
it is restarted; on
+ * restart, consumption resumes from the last committed offsets.
+ */
+public class TestCombineFlushFailedLoadCommits {
+ private static final String TOPIC = "orders-topic";
+ private static final String TABLE = "orders";
+ private static final TopicPartition PARTITION = new TopicPartition(TOPIC,
0);
+
+ private MockedStatic<RestService> mockRestService;
+ private Map<String, String> config;
+ private SinkTaskContext context;
+
+ @Before
+ public void init() throws IOException {
+ mockRestService = mockStatic(RestService.class);
+ config = combineFlushConfig();
+ context = mock(SinkTaskContext.class);
+ when(context.assignment())
+ .thenReturn(
+ new java.util.HashSet<>(
+ java.util.Arrays.asList(PARTITION, new
TopicPartition(TOPIC, 1))));
+ }
+
+ @After
+ public void close() {
+ mockRestService.close();
+ }
+
+ private static Map<String, String> combineFlushConfig() throws IOException
{
+ InputStream stream =
+ TestCombineFlushFailedLoadCommits.class
+ .getClassLoader()
+
.getResourceAsStream("doris-connector-sink.properties");
+ Properties properties = new Properties();
+ properties.load(stream);
+ DorisSinkConnectorConfig.setDefaultValues((Map) properties);
+ properties.put("task_id", "7");
+ properties.put(DorisSinkConnectorConfig.NAME,
"combine-flush-failed-load-test");
+ properties.put(DorisSinkConnectorConfig.DORIS_DATABASE, "demo");
+ properties.put(DorisSinkConnectorConfig.JMX_OPT, "false");
+ properties.put(DorisSinkConnectorConfig.TOPICS_TABLES_MAP, TOPIC + ":"
+ TABLE);
+ properties.put(DorisSinkConnectorConfig.LOAD_MODEL, "stream_load");
+ properties.put(DorisSinkConnectorConfig.ENABLE_COMBINE_FLUSH, "true");
+ properties.put(DorisSinkConnectorConfig.DELIVERY_GUARANTEE,
"at_least_once");
+ properties.put(DorisSinkConnectorConfig.BUFFER_FLUSH_TIME_SEC, "3600");
+ // fast, bounded retries for the executor's load retry loop
+ properties.put(DorisSinkConnectorConfig.MAX_RETRIES, "2");
+ properties.put(DorisSinkConnectorConfig.RETRY_INTERVAL_MS, "10");
+ return (Map<String, String>) (Map) properties;
+ }
+
+ private static SinkRecord record(long offset, String value) {
+ return new SinkRecord(
+ TOPIC,
+ 0,
+ Schema.OPTIONAL_STRING_SCHEMA,
+ "key",
+ Schema.OPTIONAL_STRING_SCHEMA,
+ value,
+ offset);
+ }
+
+ @Test
+ public void multiPartitionCombinedBufferRecoversAfterTransientFailure()
throws Exception {
+ DorisCombinedSinkService service = new
DorisCombinedSinkService(config, context);
+ DorisOptions options = new DorisOptions(config);
+ service.startTask(TABLE, PARTITION);
+ RecordingHttpClient client = new RecordingHttpClient();
+ injectRecordingClient(service, options, client);
+
+ // both partitions of the topic feed one combined writer and one buffer
+ service.insert(Collections.singleton(record(100, "{\"id\":100}")));
+ service.insert(
+ Collections.singleton(
+ new SinkRecord(
+ TOPIC,
+ 1,
+ Schema.OPTIONAL_STRING_SCHEMA,
+ "key",
+ Schema.OPTIONAL_STRING_SCHEMA,
+ "{\"id\":101}",
+ 0)));
+ TopicPartition partition1 = new TopicPartition(TOPIC, 1);
+
+ // the combined load fails once, then the executor retry recovers
automatically
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"BE is
restarting\"}");
+ client.addResponse(200, "{\"Status\":\"Success\",\"Message\":\"OK\"}");
+ Map<TopicPartition, OffsetAndMetadata> offsets = new HashMap<>();
+ offsets.put(PARTITION, new OffsetAndMetadata(101));
+ offsets.put(partition1, new OffsetAndMetadata(1));
+ // commit() blocks through the failed attempt and the successful retry
+ service.commit(offsets);
+
+ // only after the durable load may both partitions' offsets be reported
+ Assert.assertEquals(100, service.getOffset(PARTITION));
+ Assert.assertEquals(0, service.getOffset(partition1));
+ service.close();
+ }
+
+ @Test
+ public void taskRestartReplaysFromLastCommittedOffsets() throws Exception {
+ Map<String, String> taskConfig = new HashMap<>(combineFlushConfig());
+ taskConfig.put(DorisSinkConnectorConfig.MAX_RETRIES, "2");
+ taskConfig.put(DorisSinkConnectorConfig.RETRY_INTERVAL_MS, "0");
+ org.apache.doris.kafka.connector.DorisSinkTask failedTask =
+ new org.apache.doris.kafka.connector.DorisSinkTask();
+ failedTask.initialize(context);
+ failedTask.start(taskConfig);
+ failedTask.open(Collections.singleton(PARTITION));
+
+ Field sinkField =
+
org.apache.doris.kafka.connector.DorisSinkTask.class.getDeclaredField("sink");
+ sinkField.setAccessible(true);
+ DorisCombinedSinkService failedService =
+ (DorisCombinedSinkService) sinkField.get(failedTask);
+ RecordingHttpClient failingClient = new RecordingHttpClient();
+ injectRecordingClient(failedService, new DorisOptions(taskConfig),
failingClient);
+
+ // offset 100 buffers, the load fails and the retries are exhausted:
the task fails
+ failedTask.put(Collections.singleton(record(100, "{\"id\":100}")));
+ failingClient.addResponse(200,
"{\"Status\":\"Failed\",\"Message\":\"BE is restarting\"}");
+ Map<TopicPartition, OffsetAndMetadata> offsets =
+ Collections.singletonMap(PARTITION, new
OffsetAndMetadata(101));
+ Assert.assertThrows(DorisException.class, () ->
failedTask.preCommit(offsets));
+ // re-delivered put()s stay retriable until the task's retries are
exhausted
+ Assert.assertThrows(
+ RetriableException.class,
+ () -> failedTask.put(Collections.singleton(record(100,
"{\"id\":100}"))));
+ Assert.assertThrows(
+ RetriableException.class,
+ () -> failedTask.put(Collections.singleton(record(100,
"{\"id\":100}"))));
+ Assert.assertThrows(
+ DorisException.class,
+ () -> failedTask.put(Collections.singleton(record(100,
"{\"id\":100}"))));
+ failedTask.stop();
+
+ // simulate the external task restart: a new task instance with fresh
writers
+ // replays the redelivered records from the last committed offset and
commits
+ // successfully
+ org.apache.doris.kafka.connector.DorisSinkTask restartedTask =
+ new org.apache.doris.kafka.connector.DorisSinkTask();
+ restartedTask.initialize(context);
+ restartedTask.start(taskConfig);
+ restartedTask.open(Collections.singleton(PARTITION));
+ DorisCombinedSinkService restartedService =
+ (DorisCombinedSinkService) sinkField.get(restartedTask);
+ RecordingHttpClient recoveringClient = new RecordingHttpClient();
+ injectRecordingClient(restartedService, new DorisOptions(taskConfig),
recoveringClient);
+
+ recoveringClient.addResponse(200,
"{\"Status\":\"Success\",\"Message\":\"OK\"}");
+ restartedTask.put(Collections.singleton(record(100, "{\"id\":100}")));
+ Map<TopicPartition, OffsetAndMetadata> replayed =
+ Collections.singletonMap(PARTITION, new
OffsetAndMetadata(101));
+ // preCommit must return the durable offsets, not just not throw
+ Map<TopicPartition, OffsetAndMetadata> committed =
restartedTask.preCommit(replayed);
+
+ Assert.assertNotNull(committed.get(PARTITION));
+ Assert.assertEquals(100, committed.get(PARTITION).offset());
+ Assert.assertEquals(100, restartedService.getOffset(PARTITION));
+ restartedTask.stop();
+ }
+
+ /**
+ * Replaces the writer's internal {@link AsyncDorisStreamLoad} with one
backed by the recording
+ * HTTP client, so the background load can be made to fail.
+ */
+ private static AsyncDorisStreamLoad injectRecordingClient(
+ DorisCombinedSinkService service, DorisOptions options,
RecordingHttpClient client)
+ throws Exception {
+ AsyncStreamLoadWriter writer =
+ (AsyncStreamLoadWriter)
service.writer.values().iterator().next();
+ Field field =
AsyncStreamLoadWriter.class.getDeclaredField("dorisStreamLoad");
+ field.setAccessible(true);
+ AsyncDorisStreamLoad original = (AsyncDorisStreamLoad)
field.get(writer);
+ original.close();
+ BackendUtils backendUtils = mock(BackendUtils.class);
+ when(backendUtils.getAvailableBackend()).thenReturn("be:8040");
+ AsyncDorisStreamLoad replacement =
+ new AsyncDorisStreamLoad(backendUtils, options, TOPIC, TABLE,
client);
+ awaitLoadThread(replacement);
+ field.set(writer, replacement);
+ return replacement;
+ }
+
+ private static void awaitLoadThread(AsyncDorisStreamLoad streamLoad) {
+ try {
+ Field alive =
AsyncDorisStreamLoad.class.getDeclaredField("loadThreadAlive");
+ alive.setAccessible(true);
+ long deadline = System.currentTimeMillis() + 10_000;
+ while (!(boolean) alive.get(streamLoad)) {
+ Assert.assertTrue(
+ "load executor thread did not start",
+ System.currentTimeMillis() < deadline);
+ Thread.sleep(10);
+ }
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Test
+ public void failedLoadFailsInsertAndCommitAcrossRestarts() throws
Exception {
+ DorisCombinedSinkService service = new
DorisCombinedSinkService(config, context);
+ DorisOptions options = new DorisOptions(config);
+ service.startTask(TABLE, PARTITION);
+ RecordingHttpClient client = new RecordingHttpClient();
+ injectRecordingClient(service, options, client);
+
+ // two puts buffer records in the writer
+ service.insert(Collections.singleton(record(100, "{\"id\":100}")));
+ service.insert(Collections.singleton(record(101, "{\"id\":101}")));
+
+ // preCommit first calls commit(): the flush must fail and propagate,
so preCommit
+ // cannot return the (unsafe) offsets from topicPartitionOffset
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"insert
rows exceed limit\"}");
+ Map<TopicPartition, OffsetAndMetadata> offsets =
+ Collections.singletonMap(PARTITION, new
OffsetAndMetadata(102));
+ Assert.assertThrows(DorisException.class, () ->
service.commit(offsets));
+
+ // DorisSinkTask.put() catches the failure and calls sink.init(),
which restarts the
+ // async executor; both insert() and commit() must keep failing
afterwards
+ service.init();
+ Assert.assertThrows(
+ DorisException.class,
+ () -> service.insert(Collections.singleton(record(102,
"{\"id\":102}"))));
+ Assert.assertThrows(DorisException.class, () ->
service.commit(offsets));
+
+ // getOffset() itself still reports the last seen offset, but
preCommit never reaches
+ // it: commit() runs first and throws
+ Assert.assertEquals(102, service.getOffset(PARTITION));
+ service.close();
+ }
+
+ @Test
+ public void taskKeepsFailingUntilRetriesExhaustedThenNeverCommits() throws
Exception {
+ Map<String, String> taskConfig = new HashMap<>(combineFlushConfig());
+ taskConfig.put(DorisSinkConnectorConfig.MAX_RETRIES, "2");
+ taskConfig.put(DorisSinkConnectorConfig.RETRY_INTERVAL_MS, "0");
+ org.apache.doris.kafka.connector.DorisSinkTask task =
+ new org.apache.doris.kafka.connector.DorisSinkTask();
+ task.initialize(context);
+ task.start(taskConfig);
+ task.open(Collections.singleton(PARTITION));
+
+ Field sinkField =
+
org.apache.doris.kafka.connector.DorisSinkTask.class.getDeclaredField("sink");
+ sinkField.setAccessible(true);
+ DorisCombinedSinkService service = (DorisCombinedSinkService)
sinkField.get(task);
+ RecordingHttpClient client = new RecordingHttpClient();
+ injectRecordingClient(service, new DorisOptions(taskConfig), client);
+
+ // put #1 buffers records; the task stays healthy
+ task.put(Collections.singleton(record(100, "{\"id\":100}")));
+
+ // preCommit flushes and the background load fails: no offsets are
returned
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"insert
rows exceed limit\"}");
+ Map<TopicPartition, OffsetAndMetadata> offsets =
+ Collections.singletonMap(PARTITION, new
OffsetAndMetadata(101));
+ Assert.assertThrows(DorisException.class, () ->
task.preCommit(offsets));
+
+ // Kafka Connect re-delivers only the current put() batch; each
redelivery keeps
+ // failing retriable until the retries are exhausted, then the
original exception
+ // escapes and the task fails (it stays FAILED until restarted; on
restart,
+ // consumption resumes from the last committed offsets)
+ Assert.assertThrows(
+ RetriableException.class,
+ () -> task.put(Collections.singleton(record(101,
"{\"id\":101}"))));
+ Assert.assertThrows(
+ RetriableException.class,
+ () -> task.put(Collections.singleton(record(101,
"{\"id\":101}"))));
+ Assert.assertThrows(
+ DorisException.class,
+ () -> task.put(Collections.singleton(record(101,
"{\"id\":101}"))));
+
+ // and preCommit still never returns offsets past the failed load
+ Assert.assertThrows(DorisException.class, () ->
task.preCommit(offsets));
+ task.stop();
+ }
+}
diff --git
a/src/test/java/org/apache/doris/kafka/connector/writer/load/TestAsyncStreamLoadFailureLatch.java
b/src/test/java/org/apache/doris/kafka/connector/writer/load/TestAsyncStreamLoadFailureLatch.java
new file mode 100644
index 0000000..be77416
--- /dev/null
+++
b/src/test/java/org/apache/doris/kafka/connector/writer/load/TestAsyncStreamLoadFailureLatch.java
@@ -0,0 +1,881 @@
+/*
+ * 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.doris.kafka.connector.writer.load;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.Field;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicReference;
+import org.apache.doris.kafka.connector.cfg.DorisOptions;
+import org.apache.doris.kafka.connector.cfg.DorisSinkConnectorConfig;
+import org.apache.doris.kafka.connector.exception.DorisException;
+import org.apache.doris.kafka.connector.testutil.RecordingHttpClient;
+import org.apache.doris.kafka.connector.utils.BackendUtils;
+import org.apache.doris.kafka.connector.writer.RecordBuffer;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.params.HttpParams;
+import org.apache.http.protocol.HttpContext;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Regression tests for the bounded retry and sticky terminal failure of {@link
+ * AsyncDorisStreamLoad}.
+ */
+public class TestAsyncStreamLoadFailureLatch {
+ private DorisOptions options;
+
+ @Before
+ public void init() throws IOException {
+ InputStream stream =
+
getClass().getClassLoader().getResourceAsStream("doris-connector-sink.properties");
+ Properties props = new Properties();
+ props.load(stream);
+ DorisSinkConnectorConfig.setDefaultValues((Map) props);
+ props.put("task_id", "0");
+ props.put("name", "async-failure-latch-test");
+ // fast, bounded retries for the executor's load retry loop
+ props.put(DorisSinkConnectorConfig.MAX_RETRIES, "2");
+ props.put(DorisSinkConnectorConfig.RETRY_INTERVAL_MS, "10");
+ // keep request payloads readable for the payload-preservation
assertions
+ props.put("sink.properties.compress_type", "");
+ options = new DorisOptions((Map) props);
+ }
+
+ private AsyncDorisStreamLoad newStreamLoad(CloseableHttpClient client) {
+ return newStreamLoad(client, options);
+ }
+
+ private AsyncDorisStreamLoad newStreamLoad(CloseableHttpClient client,
DorisOptions options) {
+ BackendUtils backendUtils = mock(BackendUtils.class);
+ when(backendUtils.getAvailableBackend()).thenReturn("be:8040");
+ AsyncDorisStreamLoad streamLoad =
+ new AsyncDorisStreamLoad(backendUtils, options,
"orders-topic", "orders", client);
+ awaitLoadThread(streamLoad);
+ return streamLoad;
+ }
+
+ private AsyncDorisStreamLoad newStreamLoad(
+ CloseableHttpClient client, int maxRetries, long retryIntervalMs)
throws IOException {
+ InputStream stream =
+
getClass().getClassLoader().getResourceAsStream("doris-connector-sink.properties");
+ Properties props = new Properties();
+ props.load(stream);
+ DorisSinkConnectorConfig.setDefaultValues((Map) props);
+ props.put("task_id", "0");
+ props.put("name", "retry-config-test");
+ props.put(DorisSinkConnectorConfig.MAX_RETRIES,
String.valueOf(maxRetries));
+ props.put(DorisSinkConnectorConfig.RETRY_INTERVAL_MS,
String.valueOf(retryIntervalMs));
+ props.put("sink.properties.compress_type", "");
+ return newStreamLoad(client, new DorisOptions((Map) props));
+ }
+
+ /**
+ * The executor thread sets its alive flag as its first statement; wait
for that so the first
+ * flush() cannot lose the race against thread startup.
+ */
+ private static void awaitLoadThread(AsyncDorisStreamLoad streamLoad) {
+ awaitLoadThreadState(streamLoad, true);
+ }
+
+ private static void awaitLoadThreadExit(AsyncDorisStreamLoad streamLoad) {
+ awaitLoadThreadState(streamLoad, false);
+ }
+
+ private static void awaitLoadThreadState(AsyncDorisStreamLoad streamLoad,
boolean wanted) {
+ try {
+ Field field =
AsyncDorisStreamLoad.class.getDeclaredField("loadThreadAlive");
+ field.setAccessible(true);
+ long deadline = System.currentTimeMillis() + 10_000;
+ while ((boolean) field.get(streamLoad) != wanted) {
+ Assert.assertTrue(
+ "load executor thread did not reach alive=" + wanted,
+ System.currentTimeMillis() < deadline);
+ Thread.sleep(10);
+ }
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private static RecordBuffer dataBuffer() {
+ RecordBuffer buffer = new RecordBuffer();
+ buffer.insert("{\"id\":1}");
+ return buffer;
+ }
+
+ @Test
+ public void failedLoadKeepsFailingAfterExecutorRestart() {
+ RecordingHttpClient client = new RecordingHttpClient();
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"insert
rows exceed limit\"}");
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client);
+
+ streamLoad.flush("label-1", dataBuffer());
+ Assert.assertThrows(DorisException.class, streamLoad::forceFlush);
+ // wait for the failed executor to exit, so start() deterministically
restarts it
+ awaitLoadThreadExit(streamLoad);
+
+ // the executor restart from DorisSinkTask.put() -> sink.init() ->
start() must not
+ // clear the terminal failure
+ streamLoad.start();
+ awaitLoadThread(streamLoad);
+ Assert.assertThrows(DorisException.class, streamLoad::checkException);
+ streamLoad.start();
+ Assert.assertThrows(DorisException.class, streamLoad::checkException);
+ Assert.assertThrows(DorisException.class, () ->
streamLoad.flush("label-2", dataBuffer()));
+ Assert.assertThrows(DorisException.class, streamLoad::forceFlush);
+ streamLoad.close();
+ }
+
+ @Test
+ public void connectionFailureKeepsFailingAfterExecutorRestart() {
+ // no response configured -> the client throws IOException, like a
connection
+ // refused while the BE restarts
+ RecordingHttpClient client = new RecordingHttpClient();
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client);
+
+ streamLoad.flush("label-1", dataBuffer());
+ Assert.assertThrows(DorisException.class, streamLoad::forceFlush);
+
+ streamLoad.start();
+ Assert.assertThrows(DorisException.class, streamLoad::checkException);
+ Assert.assertThrows(DorisException.class, () ->
streamLoad.flush("label-2", dataBuffer()));
+ Assert.assertThrows(DorisException.class, streamLoad::forceFlush);
+ streamLoad.close();
+ }
+
+ @Test
+ public void successfulLoadsDoNotLatch() {
+ RecordingHttpClient client = new RecordingHttpClient();
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client);
+
+ client.addResponse(200, "{\"Status\":\"Success\",\"Message\":\"OK\"}");
+ streamLoad.flush("label-1", dataBuffer());
+ streamLoad.forceFlush();
+
+ client.addResponse(200, "{\"Status\":\"Success\",\"Message\":\"OK\"}");
+ streamLoad.flush("label-2", dataBuffer());
+ streamLoad.forceFlush();
+
+ streamLoad.start();
+ streamLoad.checkException();
+ streamLoad.close();
+ }
+
+ /**
+ * The preCommit path calls forceFlush() while a load may still be in
flight on the executor
+ * thread. forceFlush() must not return before that load has finished: if
the load then failed,
+ * the failure (exception + latch) must be observable to the caller.
+ *
+ * <p>The barrier is structural: waitAsyncLoadFinish() enqueues two eof
markers into the
+ * capacity-1 flush queue, and the single sequential executor thread can
only poll the second
+ * marker after load() has returned. If the load failed, the exception is
set (and the queue
+ * cleared) before the blocked marker put unblocks, so the post-put
checkFlushException()
+ * throws.
+ */
+ @Test
+ public void forceFlushWaitsForInFlightLoadAndSurfacesItsFailure() throws
Exception {
+ BlockingFailHttpClient client = new BlockingFailHttpClient();
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client);
+
+ // the executor dequeues the buffer and the HTTP request is now in
progress
+ streamLoad.flush("label-1", dataBuffer());
+ Assert.assertTrue(
+ "http request did not start", client.awaitRequestStarted(10,
TimeUnit.SECONDS));
+
+ // forceFlush() on a second thread, as preCommit would
+ AtomicBoolean returned = new AtomicBoolean(false);
+ AtomicReference<Throwable> thrown = new AtomicReference<>();
+ Thread flusher =
+ new Thread(
+ () -> {
+ try {
+ streamLoad.forceFlush();
+ returned.set(true);
+ } catch (Throwable t) {
+ thrown.set(t);
+ }
+ },
+ "force-flush-thread");
+ flusher.start();
+ // give forceFlush() every chance to return early; the barrier must
hold while the
+ // request is in flight
+ Thread.sleep(500);
+ Assert.assertFalse(
+ "forceFlush returned while the load was still in flight",
returned.get());
+ Assert.assertNull(thrown.get());
+
+ // let the in-flight request fail; forceFlush must surface the
failure, not return
+ client.failRequest();
+ flusher.join(10_000);
+ Assert.assertFalse("forceFlush returned normally despite a failed
load", returned.get());
+ Assert.assertTrue(
+ "expected DorisException from forceFlush, got " + thrown.get(),
+ thrown.get() instanceof DorisException);
+
+ // and the failure stays latched across the put()-retry executor
restart
+ streamLoad.start();
+ Assert.assertThrows(DorisException.class, streamLoad::checkException);
+ streamLoad.close();
+ }
+
+ @Test
+ public void closeDuringIdlePollDoesNotLatch() throws Exception {
+ RecordingHttpClient client = new RecordingHttpClient();
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client);
+ // the executor is idle, blocked in flushQueue.poll(2000ms)
+
+ streamLoad.close();
+ awaitLoadThreadExit(streamLoad);
+
+ // the shutdown interrupt must not have recorded any failure (a task
restart
+ // creates fresh writers, so a closed instance is never restarted in
production)
+ streamLoad.checkException();
+ }
+
+ @Test
+ public void transientFailureRecoversWithoutLatch() {
+ RecordingHttpClient client = new RecordingHttpClient();
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"BE is
restarting\"}");
+ client.addResponse(200, "{\"Status\":\"Success\",\"Message\":\"OK\"}");
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client);
+
+ streamLoad.flush("label-transient", dataBuffer());
+ // blocks through the failed attempt and its successful retry
+ streamLoad.forceFlush();
+
+ streamLoad.checkException();
+ streamLoad.start();
+ streamLoad.checkException();
+ streamLoad.close();
+ }
+
+ @Test
+ public void retryUsesDistinctLabelsDerivedFromBase() throws Exception {
+ // the first attempt uses the buffer's original label, every retry a
distinct
+ // label derived from it
+ ScriptedHttpClient client = new ScriptedHttpClient();
+ for (int i = 0; i < 13; i++) {
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"BE
is restarting\"}");
+ }
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client, 12, 10L);
+
+ RecordBuffer multiRecord = new RecordBuffer();
+ multiRecord.insert("{\"id\":\"a1\"}");
+ multiRecord.insert("{\"id\":\"a2\"}");
+ streamLoad.flush("label-reuse", multiRecord);
+ Assert.assertThrows(DorisException.class, streamLoad::forceFlush);
+
+ java.util.List<String> expected = new java.util.ArrayList<>();
+ expected.add("label-reuse");
+ for (int attempt = 1; attempt <= 12; attempt++) {
+ expected.add("label-reuse_" + attempt);
+ }
+ Assert.assertEquals(expected, client.requestLabels());
+ Assert.assertEquals(13, new
java.util.HashSet<>(client.requestLabels()).size());
+ // every attempt transmits the identical, complete payload of the
buffer
+ Assert.assertEquals(13, client.requestBodies().size());
+ for (byte[] body : client.requestBodies()) {
+ Assert.assertArrayEquals(client.requestBodies().get(0), body);
+ String text = new String(body,
java.nio.charset.StandardCharsets.UTF_8);
+ Assert.assertTrue("payload must keep all records: " + text,
text.contains("a1"));
+ Assert.assertTrue("payload must keep all records: " + text,
text.contains("a2"));
+ }
+ streamLoad.close();
+ }
+
+ @Test
+ public void duplicateLabelRejectionRetriesThenLatches() {
+ // a duplicate-label rejection is an ordinary failure for a
changed-label retry
+ ScriptedHttpClient client = new ScriptedHttpClient();
+ client.addIoError("connection reset");
+ client.addResponse(200, duplicateLabelBody("label-ambiguous_1"));
+ client.addResponse(200, duplicateLabelBody("label-ambiguous_2"));
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client);
+
+ streamLoad.flush("label-ambiguous", dataBuffer());
+ Assert.assertThrows(DorisException.class, streamLoad::forceFlush);
+
+ Assert.assertEquals(
+ java.util.Arrays.asList(
+ "label-ambiguous", "label-ambiguous_1",
"label-ambiguous_2"),
+ client.requestLabels());
+
+ // the terminal failure survives the executor restart (the put() retry
path)
+ streamLoad.start();
+ Assert.assertThrows(DorisException.class, streamLoad::checkException);
+ streamLoad.close();
+ }
+
+ @Test
+ public void lostResponseRetriesWithNewLabelAndMayResubmit() {
+ // a lost response may hide a committed load: the retry re-sends the
identical
+ // payload under a new label, so the batch may exist twice
(at-least-once)
+ ScriptedHttpClient client = new ScriptedHttpClient();
+ client.addIoError("connection reset while the response was in flight");
+ client.addResponse(200, "{\"Status\":\"Success\",\"Message\":\"OK\"}");
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client);
+
+ RecordBuffer multiRecord = new RecordBuffer();
+ multiRecord.insert("{\"id\":\"d1\"}");
+ multiRecord.insert("{\"id\":\"d2\"}");
+ streamLoad.flush("label-lost-response", multiRecord);
+ // blocks through the lost attempt and its successful retry
+ streamLoad.forceFlush();
+
+ streamLoad.checkException();
+ Assert.assertEquals(
+ java.util.Arrays.asList("label-lost-response",
"label-lost-response_1"),
+ client.requestLabels());
+ Assert.assertEquals(2, client.requestBodies().size());
+ Assert.assertArrayEquals(client.requestBodies().get(0),
client.requestBodies().get(1));
+ streamLoad.close();
+ }
+
+ @Test
+ public void connectionFailureRecoversWithoutLatch() {
+ ScriptedHttpClient client = new ScriptedHttpClient();
+ client.addIoError("connection refused");
+ client.addResponse(200, "{\"Status\":\"Success\",\"Message\":\"OK\"}");
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client);
+
+ streamLoad.flush("label-conn", dataBuffer());
+ streamLoad.forceFlush();
+ streamLoad.checkException();
+ streamLoad.close();
+ }
+
+ @Test
+ public void retryReselectsBackendBetweenAttempts() {
+ // each attempt re-selects the backend (refreshLoadUrl ->
getAvailableBackend per
+ // load call), so a retry can land on a healthy backend after the
first one died
+ ScriptedHttpClient client = new ScriptedHttpClient();
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"BE is
restarting\"}");
+ client.addResponse(200, "{\"Status\":\"Success\",\"Message\":\"OK\"}");
+ BackendUtils backendUtils = mock(BackendUtils.class);
+ when(backendUtils.getAvailableBackend()).thenReturn("be1:8040",
"be2:8040");
+ AsyncDorisStreamLoad streamLoad =
+ new AsyncDorisStreamLoad(backendUtils, options,
"orders-topic", "orders", client);
+ awaitLoadThread(streamLoad);
+
+ streamLoad.flush("label-failover", dataBuffer());
+ streamLoad.forceFlush();
+ streamLoad.checkException();
+
+ Assert.assertEquals(java.util.Arrays.asList("be1:8040", "be2:8040"),
client.requestHosts());
+ streamLoad.close();
+ }
+
+ @Test
+ public void closeDuringRetryBackoffInterruptsPromptly() throws Exception {
+ // close() while the executor sleeps in the retry backoff must
interrupt the sleep
+ // and shut down promptly, without latching (the interrupt exit checks
`started`)
+ ScriptedHttpClient client = new ScriptedHttpClient();
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"BE is
restarting\"}");
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client, 2, 60_000L);
+
+ streamLoad.flush("label-backoff", dataBuffer());
+ // the first attempt fails immediately; the executor then sleeps for
60s
+ awaitLoadThreadBusyInBackoff(client);
+ long before = System.currentTimeMillis();
+ streamLoad.close();
+ awaitLoadThreadExit(streamLoad);
+ Assert.assertTrue(
+ "close must interrupt the backoff promptly",
+ System.currentTimeMillis() - before < 10_000);
+
+ // a single served request proves the close interrupted the backoff
before a second
+ // attempt, and the interrupt while shutting down must not have
latched the writer
+ Assert.assertEquals(1, client.requestLabels().size());
+ streamLoad.checkException();
+ }
+
+ @Test
+ public void groupCommitFailureRetriesThenLatches() throws Exception {
+ // group commit sends no client-controlled label: every retry is a
fresh
+ // server-labelled submission
+ ScriptedHttpClient client = new ScriptedHttpClient();
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"BE is
restarting\"}");
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"BE is
restarting\"}");
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"BE is
restarting\"}");
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client,
groupCommitOptions());
+
+ streamLoad.flush("label-gc", dataBuffer());
+ Assert.assertThrows(DorisException.class, streamLoad::forceFlush);
+
+ // every attempt carries no client-controlled label, and the terminal
failure
+ // sticks across the executor restart
+ Assert.assertEquals(3, client.requestLabels().size());
+ for (String label : client.requestLabels()) {
+ Assert.assertNull(label);
+ }
+ streamLoad.start();
+ Assert.assertThrows(DorisException.class, streamLoad::checkException);
+ streamLoad.close();
+ }
+
+ @Test
+ public void groupCommitTransientFailureRecovers() throws Exception {
+ ScriptedHttpClient client = new ScriptedHttpClient();
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"BE is
restarting\"}");
+ client.addResponse(200, "{\"Status\":\"Success\",\"Message\":\"OK\"}");
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client,
groupCommitOptions());
+
+ RecordBuffer multiRecord = new RecordBuffer();
+ multiRecord.insert("{\"id\":\"g1\"}");
+ multiRecord.insert("{\"id\":\"g2\"}");
+ streamLoad.flush("label-gc-recover", multiRecord);
+ // blocks through the failed attempt and its successful retry
+ streamLoad.forceFlush();
+
+ streamLoad.checkException();
+ Assert.assertEquals(2, client.requestLabels().size());
+ for (String label : client.requestLabels()) {
+ Assert.assertNull(label);
+ }
+ Assert.assertArrayEquals(client.requestBodies().get(0),
client.requestBodies().get(1));
+ streamLoad.close();
+ }
+
+ /** The documented duplicate-label response body. */
+ private static String duplicateLabelBody(String label) {
+ return String.format(
+ "{\"Status\": \"Label Already Exists\", \"Label\": \"%s\","
+ + " \"Message\": \"label [%s] already exist\"}",
+ label, label);
+ }
+
+ /** Options with group commit enabled (stream_load_prop.group_commit). */
+ private DorisOptions groupCommitOptions() throws IOException {
+ InputStream stream =
+
getClass().getClassLoader().getResourceAsStream("doris-connector-sink.properties");
+ Properties props = new Properties();
+ props.load(stream);
+ DorisSinkConnectorConfig.setDefaultValues((Map) props);
+ props.put("task_id", "0");
+ props.put("name", "group-commit-test");
+ props.put(DorisSinkConnectorConfig.MAX_RETRIES, "2");
+ props.put(DorisSinkConnectorConfig.RETRY_INTERVAL_MS, "10");
+ props.put("sink.properties.compress_type", "");
+ props.put("sink.properties.group_commit", "async_mode");
+ return new DorisOptions((Map) props);
+ }
+
+ /**
+ * Waits until the executor is deterministically inside the retry backoff:
the first scripted
+ * response has been handed to the executor thread, and that same thread
is in TIMED_WAITING —
+ * inside {@code loadWithRetry} the only timed wait after a served
response is the backoff
+ * sleep.
+ */
+ private static void awaitLoadThreadBusyInBackoff(ScriptedHttpClient
client) {
+ long deadline = System.currentTimeMillis() + 10_000;
+ Thread executor;
+ do {
+ executor = client.firstServingThread();
+ Assert.assertTrue(
+ "first load attempt did not start",
System.currentTimeMillis() < deadline);
+ try {
+ Thread.sleep(10);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
+ } while (executor == null || executor.getState() !=
Thread.State.TIMED_WAITING);
+ }
+
+ @Test
+ public void gzipDefaultPayloadPreservedAcrossRetries() throws Exception {
+ // with the default compress_type=gz, each attempt transmits a gzip
entity: the
+ // recorded (still compressed) payloads must still be identical across
attempts,
+ // and decompressing them must yield the complete original records
+ ScriptedHttpClient client = new ScriptedHttpClient();
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"BE is
restarting\"}");
+ client.addResponse(200, "{\"Status\":\"Failed\",\"Message\":\"BE is
restarting\"}");
+ client.addResponse(200, "{\"Status\":\"Success\",\"Message\":\"OK\"}");
+
+ InputStream stream =
+
getClass().getClassLoader().getResourceAsStream("doris-connector-sink.properties");
+ Properties gzipProps = new Properties();
+ gzipProps.load(stream);
+ DorisSinkConnectorConfig.setDefaultValues((Map) gzipProps);
+ gzipProps.put("task_id", "0");
+ gzipProps.put("name", "gzip-payload-test");
+ gzipProps.put(DorisSinkConnectorConfig.MAX_RETRIES, "2");
+ gzipProps.put(DorisSinkConnectorConfig.RETRY_INTERVAL_MS, "10");
+ // no compress_type override: the default gz applies
+
+ BackendUtils backendUtils = mock(BackendUtils.class);
+ when(backendUtils.getAvailableBackend()).thenReturn("be:8040");
+ AsyncDorisStreamLoad streamLoad =
+ new AsyncDorisStreamLoad(
+ backendUtils,
+ new DorisOptions((Map) gzipProps),
+ "orders-topic",
+ "orders",
+ client);
+ awaitLoadThread(streamLoad);
+
+ RecordBuffer buffer = new RecordBuffer();
+ buffer.insert("{\"id\":\"g1\"}");
+ buffer.insert("{\"id\":\"g2\"}");
+ streamLoad.flush("label-gzip", buffer);
+ streamLoad.forceFlush();
+ streamLoad.checkException();
+
+ Assert.assertEquals(3, client.requestBodies().size());
+ String decompressed = gunzip(client.requestBodies().get(0));
+ for (byte[] body : client.requestBodies()) {
+ Assert.assertArrayEquals(client.requestBodies().get(0), body);
+ Assert.assertEquals(decompressed, gunzip(body));
+ }
+ Assert.assertTrue(
+ "payload must keep all records: " + decompressed,
+ decompressed.contains("g1") && decompressed.contains("g2"));
+ streamLoad.close();
+ }
+
+ private static String gunzip(byte[] gzipped) throws java.io.IOException {
+ java.io.ByteArrayInputStream bytes = new
java.io.ByteArrayInputStream(gzipped);
+ try (java.util.zip.GZIPInputStream gzip = new
java.util.zip.GZIPInputStream(bytes)) {
+ java.io.ByteArrayOutputStream out = new
java.io.ByteArrayOutputStream();
+ byte[] chunk = new byte[1024];
+ int n;
+ while ((n = gzip.read(chunk)) > 0) {
+ out.write(chunk, 0, n);
+ }
+ return new String(out.toByteArray(),
java.nio.charset.StandardCharsets.UTF_8);
+ }
+ }
+
+ @Test
+ public void inFlightFailureRetainsPendingQueuedBuffer() throws Exception {
+ // buffer A is in flight when buffer B is enqueued behind it; A's
first attempt fails,
+ // its retry succeeds, and B — still queued throughout — is loaded
afterwards
+ ScriptedHttpClient client = new ScriptedHttpClient();
+ ScriptedHttpClient.BlockingStep blockedA = client.addBlockingStep();
+ client.addResponse(200, "{\"Status\":\"Success\",\"Message\":\"OK\"}");
+ client.addResponse(200, "{\"Status\":\"Success\",\"Message\":\"OK\"}");
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client);
+
+ RecordBuffer bufferA = new RecordBuffer();
+ bufferA.insert("{\"id\":\"a1\"}");
+ bufferA.insert("{\"id\":\"a2\"}");
+ RecordBuffer bufferB = new RecordBuffer();
+ bufferB.insert("{\"id\":\"b1\"}");
+ streamLoad.flush("label-a", bufferA);
+ Assert.assertTrue(blockedA.awaitStarted(10, TimeUnit.SECONDS));
+ // A is in flight (polled out of the capacity-1 queue): B enters the
queue behind it
+ streamLoad.flush("label-b", bufferB);
+ blockedA.release();
+
+ // the forceFlush barrier waits for A's retry and B's load to complete
+ streamLoad.forceFlush();
+ streamLoad.checkException();
+
+ Assert.assertEquals(
+ java.util.Arrays.asList("label-a", "label-a_1", "label-b"),
client.requestLabels());
+ // A's two attempts carry the identical complete payload; B's is
distinct and intact
+ Assert.assertEquals(3, client.requestBodies().size());
+ Assert.assertArrayEquals(client.requestBodies().get(0),
client.requestBodies().get(1));
+ Assert.assertFalse(
+ java.util.Arrays.equals(
+ client.requestBodies().get(0),
client.requestBodies().get(2)));
+ Assert.assertTrue(
+ new String(client.requestBodies().get(2),
java.nio.charset.StandardCharsets.UTF_8)
+ .contains("b1"));
+ streamLoad.close();
+ }
+
+ @Test
+ public void forceFlushWaitsForInFlightLoadThenSucceeds() throws Exception {
+ // the preCommit path may run while a load is still in flight;
forceFlush must not
+ // return before the load has finished, and after a successful load it
returns
+ // normally (no latch)
+ ScriptedHttpClient client = new ScriptedHttpClient();
+ ScriptedHttpClient.BlockingStep blocked = client.addBlockingStep();
+ client.addResponse(200, "{\"Status\":\"Success\",\"Message\":\"OK\"}");
+ AsyncDorisStreamLoad streamLoad = newStreamLoad(client);
+
+ streamLoad.flush("label-blocking", dataBuffer());
+ Assert.assertTrue(blocked.awaitStarted(10, TimeUnit.SECONDS));
+
+ AtomicBoolean returned = new AtomicBoolean(false);
+ AtomicReference<Throwable> thrown = new AtomicReference<>();
+ Thread flusher =
+ new Thread(
+ () -> {
+ try {
+ streamLoad.forceFlush();
+ returned.set(true);
+ } catch (Throwable t) {
+ thrown.set(t);
+ }
+ },
+ "force-flush-success-thread");
+ flusher.start();
+ Thread.sleep(300);
+ Assert.assertFalse(
+ "forceFlush returned while the load was still in flight",
returned.get());
+ Assert.assertNull(thrown.get());
+
+ blocked.release();
+ flusher.join(10_000);
+ Assert.assertTrue(
+ "forceFlush must return normally after a successful load",
returned.get());
+ Assert.assertNull(thrown.get());
+ streamLoad.checkException();
+ streamLoad.close();
+ }
+
+ /**
+ * HTTP client whose single request blocks until the test releases it,
then fails with an
+ * IOException — a connection reset while the request is in flight.
+ */
+ private static final class BlockingFailHttpClient extends
CloseableHttpClient {
+ private final CountDownLatch requestStarted = new CountDownLatch(1);
+ private final CountDownLatch releaseRequest = new CountDownLatch(1);
+
+ boolean awaitRequestStarted(long timeout, TimeUnit unit) throws
InterruptedException {
+ return requestStarted.await(timeout, unit);
+ }
+
+ void failRequest() {
+ releaseRequest.countDown();
+ }
+
+ @Override
+ protected CloseableHttpResponse doExecute(
+ HttpHost target, HttpRequest request, HttpContext context)
throws IOException {
+ requestStarted.countDown();
+ try {
+ releaseRequest.await();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ throw new IOException("connection reset during load");
+ }
+
+ @Override
+ @Deprecated
+ public HttpParams getParams() {
+ return null;
+ }
+
+ @Override
+ @Deprecated
+ public ClientConnectionManager getConnectionManager() {
+ return null;
+ }
+
+ @Override
+ public void close() {}
+ }
+ /**
+ * Scripted HTTP client for the retry tests: every executed request
records its stream load
+ * label header, and each request consumes one scripted step — either an
HTTP response or an
+ * IOException. A blocking step parks the request until the test releases
it, simulating a load
+ * that is still in flight.
+ */
+ private static final class ScriptedHttpClient extends CloseableHttpClient {
+ private final java.util.List<String> labels =
+ java.util.Collections.synchronizedList(new
java.util.ArrayList<>());
+ private final java.util.List<byte[]> bodies =
+ java.util.Collections.synchronizedList(new
java.util.ArrayList<>());
+ private final java.util.List<String> hosts =
+ java.util.Collections.synchronizedList(new
java.util.ArrayList<>());
+ private final java.util.List<Thread> servingThreads =
+ java.util.Collections.synchronizedList(new
java.util.ArrayList<>());
+ private final java.util.Queue<Step> steps =
+ new java.util.concurrent.ConcurrentLinkedQueue<>();
+
+ Step addResponse(int status, String body) {
+ Step step = new ResponseStep(status, body);
+ steps.add(step);
+ return step;
+ }
+
+ Step addIoError(String message) {
+ Step step = new IoErrorStep(message);
+ steps.add(step);
+ return step;
+ }
+
+ BlockingStep addBlockingStep() {
+ BlockingStep step = new BlockingStep();
+ steps.add(step);
+ return step;
+ }
+
+ java.util.List<String> requestLabels() {
+ synchronized (labels) {
+ return new java.util.ArrayList<>(labels);
+ }
+ }
+
+ java.util.List<byte[]> requestBodies() {
+ synchronized (bodies) {
+ return new java.util.ArrayList<>(bodies);
+ }
+ }
+
+ java.util.List<String> requestHosts() {
+ synchronized (hosts) {
+ return new java.util.ArrayList<>(hosts);
+ }
+ }
+
+ Thread firstServingThread() {
+ synchronized (servingThreads) {
+ return servingThreads.isEmpty() ? null : servingThreads.get(0);
+ }
+ }
+
+ @Override
+ protected CloseableHttpResponse doExecute(
+ HttpHost target, HttpRequest request, HttpContext context)
throws IOException {
+ labels.add(
+ request.getLastHeader("label") == null
+ ? null
+ : request.getLastHeader("label").getValue());
+ hosts.add(target == null ? null : target.toHostString());
+ servingThreads.add(Thread.currentThread());
+ if (request instanceof org.apache.http.HttpEntityEnclosingRequest)
{
+ org.apache.http.HttpEntity entity =
+ ((org.apache.http.HttpEntityEnclosingRequest)
request).getEntity();
+ if (entity == null) {
+ bodies.add(null);
+ } else {
+ // writeTo works for plain and gzip entities alike;
getContent does not
+ java.io.ByteArrayOutputStream out = new
java.io.ByteArrayOutputStream();
+ try {
+ entity.writeTo(out);
+ } catch (java.io.IOException e) {
+ throw new RuntimeException(e);
+ }
+ bodies.add(out.toByteArray());
+ }
+ } else {
+ bodies.add(null);
+ }
+ Step step = steps.poll();
+ if (step == null) {
+ throw new IOException("no scripted step left");
+ }
+ return step.run();
+ }
+
+ @Override
+ @Deprecated
+ public HttpParams getParams() {
+ return null;
+ }
+
+ @Override
+ @Deprecated
+ public ClientConnectionManager getConnectionManager() {
+ return null;
+ }
+
+ @Override
+ public void close() {}
+
+ interface Step {
+ CloseableHttpResponse run() throws IOException;
+ }
+
+ private static final class ResponseStep implements Step {
+ private final int status;
+ private final String body;
+
+ ResponseStep(int status, String body) {
+ this.status = status;
+ this.body = body;
+ }
+
+ @Override
+ public CloseableHttpResponse run() {
+ CloseableHttpResponse response =
+ org.mockito.Mockito.mock(CloseableHttpResponse.class);
+ org.mockito.Mockito.when(response.getStatusLine())
+ .thenReturn(
+ new org.apache.http.message.BasicStatusLine(
+ new
org.apache.http.ProtocolVersion("HTTP", 1, 1),
+ status,
+ ""));
+ if (body != null) {
+ org.mockito.Mockito.when(response.getEntity())
+ .thenReturn(
+ new org.apache.http.entity.StringEntity(
+ body,
java.nio.charset.StandardCharsets.UTF_8));
+ }
+ return response;
+ }
+ }
+
+ private static final class IoErrorStep implements Step {
+ private final String message;
+
+ IoErrorStep(String message) {
+ this.message = message;
+ }
+
+ @Override
+ public CloseableHttpResponse run() throws IOException {
+ throw new IOException(message);
+ }
+ }
+
+ static final class BlockingStep implements Step {
+ private final CountDownLatch started = new CountDownLatch(1);
+ private final CountDownLatch release = new CountDownLatch(1);
+
+ void release() {
+ release.countDown();
+ }
+
+ boolean awaitStarted(long timeout, TimeUnit unit) throws
InterruptedException {
+ return started.await(timeout, unit);
+ }
+
+ @Override
+ public CloseableHttpResponse run() throws IOException {
+ started.countDown();
+ try {
+ release.await();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ }
+ throw new IOException("connection reset during load");
+ }
+ }
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]