Skip to content

Commit 3e64a2d

Browse files
committed
refactor(batch): report errors as CompletionExceptions
1 parent 3adda03 commit 3e64a2d

5 files changed

Lines changed: 45 additions & 49 deletions

File tree

src/main/java/io/weaviate/client6/v1/api/collections/batch/BatchContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
* @see StreamObserver
127127
* @see State
128128
* @see #shutdownNow
129-
* @see TaskHandle#result()
129+
* @see TaskHandle#done()
130130
*
131131
* @author Dyma Solovei
132132
*/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.weaviate.client6.v1.api.collections.batch;
2+
3+
import io.weaviate.client6.v1.api.WeaviateException;
4+
5+
public class BatchServerException extends WeaviateException {
6+
7+
public BatchServerException(String message) {
8+
super(message);
9+
}
10+
}

src/main/java/io/weaviate/client6/v1/api/collections/batch/TaskHandle.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,11 @@ public final class TaskHandle {
3333
/** Flag indicating the task has been ack'ed. */
3434
private final CompletableFuture<Void> acked = new CompletableFuture<>();
3535

36-
public final record Result(Optional<String> error) {
37-
public Result {
38-
requireNonNull(error, "error is null");
39-
}
40-
}
41-
4236
/**
4337
* Task result completes when the client receives {@link Event.Results}
4438
* containing this handle's {@link #id}.
4539
*/
46-
private final CompletableFuture<Result> result = new CompletableFuture<>();
40+
private final CompletableFuture<Void> done = new CompletableFuture<>();
4741

4842
/** The number of times this task has been retried. */
4943
private final int retries;
@@ -84,7 +78,7 @@ private TaskHandle() {
8478

8579
/**
8680
* Creates a new task containing the same data as this task and {@link #retries}
87-
* counter incremented by 1. The {@link #acked} and {@link #result} futures
81+
* counter incremented by 1. The {@link #acked} and {@link #done} futures
8882
* are not copied to the returned task.
8983
*
9084
* @return Task handle.
@@ -111,7 +105,7 @@ void setAcked() {
111105
* {@link #setError} afterwards will have no effect.
112106
*/
113107
void setSuccess() {
114-
setResult(new Result(Optional.empty()));
108+
setResult(null);
115109
}
116110

117111
/**
@@ -124,20 +118,24 @@ void setSuccess() {
124118
* status for the task; prefer {@link #setSuccess} in that case.
125119
*/
126120
void setError(String error) {
127-
setResult(new Result(Optional.ofNullable(error)));
121+
setResult(error);
128122
}
129123

130124
/**
131125
* Set result for this task.
132126
*
133127
* @throws IllegalStateException if the task has not been ack'ed.
134128
*/
135-
private void setResult(Result result) {
129+
private void setResult(String error) {
136130
if (!acked.isDone()) {
137131
// TODO(dyma): can this happen due to us?
138132
throw new IllegalStateException("Result can only be set for an ack'ed task");
139133
}
140-
this.result.complete(result);
134+
if (error == null) {
135+
this.done.complete(null);
136+
} else {
137+
this.done.completeExceptionally(new BatchServerException(error));
138+
}
141139
}
142140

143141
/**
@@ -155,8 +153,8 @@ public CompletableFuture<Void> isAcked() {
155153
* @return A future which completes when the server
156154
* has reported the result for this task.
157155
*/
158-
public CompletableFuture<Result> result() {
159-
return result;
156+
public CompletableFuture<Void> done() {
157+
return done;
160158
}
161159

162160
/**

src/test/java/io/weaviate/client6/v1/api/collections/batch/BatchContextTest.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,9 @@ public void test_sendOneBatch() throws Exception {
220220
closeContext();
221221

222222
// By the time context.close() returns all tasks MUST have results set.
223-
Assertions.assertThat(tasks).extracting(TaskHandle::result)
223+
Assertions.assertThat(tasks).extracting(TaskHandle::done)
224224
.allMatch(CompletableFuture::isDone)
225-
.extracting(CompletableFuture::get).extracting(TaskHandle.Result::error)
226-
.allMatch(Optional::isEmpty);
225+
.noneMatch(CompletableFuture::isCompletedExceptionally);
227226
}
228227

229228
@Test
@@ -256,10 +255,9 @@ public void test_drainOnClose() throws Exception {
256255

257256
closeContext();
258257

259-
Assertions.assertThat(tasks).extracting(TaskHandle::result)
258+
Assertions.assertThat(tasks).extracting(TaskHandle::done)
260259
.allMatch(CompletableFuture::isDone)
261-
.extracting(CompletableFuture::get).extracting(TaskHandle.Result::error)
262-
.allMatch(Optional::isEmpty);
260+
.noneMatch(CompletableFuture::isCompletedExceptionally);
263261
}
264262

265263
@Test
@@ -293,10 +291,9 @@ public void test_backoff() throws Exception {
293291

294292
closeContext();
295293

296-
Assertions.assertThat(tasks).extracting(TaskHandle::result)
294+
Assertions.assertThat(tasks).extracting(TaskHandle::done)
297295
.allMatch(CompletableFuture::isDone)
298-
.extracting(CompletableFuture::get).extracting(TaskHandle.Result::error)
299-
.allMatch(Optional::isEmpty);
296+
.noneMatch(CompletableFuture::isCompletedExceptionally);
300297
}
301298

302299
@Test
@@ -328,10 +325,9 @@ public void test_backoffBacklog() throws Exception {
328325

329326
closeContext();
330327

331-
Assertions.assertThat(tasks).extracting(TaskHandle::result)
328+
Assertions.assertThat(tasks).extracting(TaskHandle::done)
332329
.allMatch(CompletableFuture::isDone)
333-
.extracting(CompletableFuture::get).extracting(TaskHandle.Result::error)
334-
.allMatch(Optional::isEmpty);
330+
.noneMatch(CompletableFuture::isCompletedExceptionally);
335331
}
336332

337333
@Test
@@ -454,10 +450,9 @@ public void test_reconnect_DrainAfterStreamHangup() throws Exception {
454450
closeContext();
455451
backgroundAcks.get();
456452

457-
Assertions.assertThat(tasks).extracting(TaskHandle::result)
453+
Assertions.assertThat(tasks).extracting(TaskHandle::done)
458454
.allMatch(CompletableFuture::isDone)
459-
.extracting(CompletableFuture::get).extracting(TaskHandle.Result::error)
460-
.allMatch(Optional::isEmpty);
455+
.noneMatch(CompletableFuture::isCompletedExceptionally);
461456
}
462457

463458
@Test

src/test/java/io/weaviate/client6/v1/api/collections/batch/TaskHandleTest.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import org.assertj.core.api.AbstractObjectAssert;
88
import org.assertj.core.api.Assertions;
9-
import org.assertj.core.api.InstanceOfAssertFactories;
109
import org.junit.Test;
1110

1211
import com.google.protobuf.GeneratedMessage;
@@ -51,10 +50,8 @@ public void test_newTaskHandle_WeaviateObject_success() {
5150
taskHandle.setSuccess();
5251

5352
assertAcked(taskHandle, true);
54-
assertHasResult(taskHandle, true)
55-
.extracting(future -> future.getNow(null)).isNotNull()
56-
.extracting(TaskHandle.Result::error, InstanceOfAssertFactories.optional(String.class))
57-
.isEmpty();
53+
Assertions.assertThat(taskHandle.done())
54+
.isNotCompletedExceptionally();
5855
}
5956

6057
@Test
@@ -76,10 +73,9 @@ public void test_newTaskHandle_WeaviateObject_error() {
7673
taskHandle.setError("Whaam!");
7774

7875
assertAcked(taskHandle, true);
79-
assertHasResult(taskHandle, true)
80-
.extracting(future -> future.getNow(null)).isNotNull()
81-
.extracting(TaskHandle.Result::error, InstanceOfAssertFactories.optional(String.class))
82-
.get().isEqualTo("Whaam!");
76+
Assertions.assertThat(taskHandle.done())
77+
.isCompletedExceptionally()
78+
.withFailMessage("Whaam!");
8379
}
8480

8581
@Test
@@ -101,10 +97,8 @@ public void test_newTaskHandle_BatchReference_success() {
10197
taskHandle.setSuccess();
10298

10399
assertAcked(taskHandle, true);
104-
assertHasResult(taskHandle, true)
105-
.extracting(future -> future.getNow(null)).isNotNull()
106-
.extracting(TaskHandle.Result::error, InstanceOfAssertFactories.optional(String.class))
107-
.isEmpty();
100+
Assertions.assertThat(taskHandle.done())
101+
.isNotCompletedExceptionally();
108102
}
109103

110104
@Test
@@ -126,10 +120,9 @@ public void test_newTaskHandle_BatchReference_error() {
126120
taskHandle.setError("Whaam!");
127121

128122
assertAcked(taskHandle, true);
129-
assertHasResult(taskHandle, true)
130-
.extracting(future -> future.getNow(null)).isNotNull()
131-
.extracting(TaskHandle.Result::error, InstanceOfAssertFactories.optional(String.class))
132-
.get().isEqualTo("Whaam!");
123+
Assertions.assertThat(taskHandle.done())
124+
.isCompletedExceptionally()
125+
.withFailMessage("Whaam!");
133126
}
134127

135128
@Test
@@ -160,10 +153,10 @@ private void assertAcked(TaskHandle taskHandle, boolean expect) {
160153
.returns(expect, CompletableFuture::isDone);
161154
}
162155

163-
private AbstractObjectAssert<?, CompletableFuture<TaskHandle.Result>> assertHasResult(TaskHandle taskHandle,
156+
private AbstractObjectAssert<?, CompletableFuture<Void>> assertHasResult(TaskHandle taskHandle,
164157
boolean expect) {
165158
return Assertions.assertThat(taskHandle)
166-
.extracting(TaskHandle::result)
159+
.extracting(TaskHandle::done)
167160
.as("expect has result")
168161
.returns(expect, CompletableFuture::isDone);
169162
}

0 commit comments

Comments
 (0)