Skip to content

Commit e28d1f9

Browse files
committed
docs(batch): document BatchContext
1 parent 5d7160a commit e28d1f9

3 files changed

Lines changed: 64 additions & 3 deletions

File tree

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
* edge-case
4141
*
4242
*
43-
* <h2>Synchronization policy</h2>
44-
*
4543
* @see #inFlight
4644
* @see #isFull
4745
* @see #clear

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,63 @@
6969
*
7070
* <h2>State</h2>
7171
*
72+
* BatchContext organized client-side work using the
73+
* <a href= "https://refactoring.guru/design-patterns/state">State</a>
74+
* pattern. These states are defined:
75+
*
76+
* <ul>
77+
* <li>{@code null} -- context hasn't been {@link #start}ed yet. The context
78+
* SHOULD NOT be used in this state, as it will likely result in an NPE.
79+
* <li>AwaitStarted -- client's opened the stream, sent Start,
80+
* and is now awaiting for the server to respond with Started.
81+
* <li>Active -- the server is ready to accept the next Data message.
82+
* <li>InFlight -- the latest batch has been sent, awaiting Acks.
83+
* <li>OOM -- server has OOM'ed and will not accept any more data.
84+
* <li>ServerShuttingDown -- server's begun a graceful shutdown.
85+
* <li>Reconnecting -- server's closed it's half of the stream; the client
86+
* will try to reconnect to another instance up to {@link #maxReconnectRetries}
87+
* times.
88+
* </ul>
89+
*
7290
* <h2>Cancellation policy</h2>
91+
* BatchContext does not rely on timing heuristics advance its state.
92+
* Threads coordinate via {@link #stateChanged} conditional variable
93+
* and interrupts, when appropriate.
94+
*
95+
* <h3>Graceful shutdown</h3>
96+
* When {@link #close()} is called, the context will stop accepting
97+
* new items and start draining the remaining items in the {@link #queue}
98+
* and {@link #batch} backlog. The client will then continue processing
99+
* server-side events until stream's EOF. By the time context is closed
100+
* all submitted tasks are expected to be completed successfully or otherwise.
101+
*
102+
* <br>
103+
* N.B.: This may take an arbitrarily long amount time, as the client will
104+
* continue to re-connect to other instances and re-submit WIP tasks in
105+
* case the current stream is hung up or the server shuts down prematurely.
106+
*
107+
* <h3>Abrupt termination</h3>
108+
* In the event of an internal client error (e.g. in the "sender" or "recv"
109+
* threads), the client's half of the stream is closed immediately, and the
110+
* "sender" processed is cancelled. A subsequent call to {@link #close()} will
111+
* re-throw the causing exception as {@link IOException}. The stream can be
112+
* terminated at any time, including during a graceful shutdown.
113+
* In case the context if terminated <i>before</i> a graceful shutdown begins,
114+
* the parent thread is also interrupted to prevent {@link #add()} from blocking
115+
* indefinitely, "sender" will not be there to pop items from the task queue).
116+
*
117+
* <p>
118+
* To prevent data loss, re-submit all incomplete tasks
119+
* to the next batch context.
73120
*
74121
* @param <PropertiesT> the shape of properties for inserted objects.
75122
*
76123
* @see StreamObserver
124+
* @see State
125+
* @see shutdownNow
126+
* @see TaskHandle#result()
127+
*
128+
* @author Dyma Solovei
77129
*/
78130
public final class BatchContext<PropertiesT> implements Closeable {
79131
private final int maxReconnectRetries;
@@ -218,6 +270,10 @@ public TaskHandle add(BatchReference reference) throws InterruptedException {
218270
}
219271

220272
void start() {
273+
if (closed) {
274+
throw new IllegalStateException("context is closed");
275+
}
276+
221277
workers = new CountDownLatch(2);
222278

223279
messages = streamFactory.createStream(new Recv());
@@ -415,7 +471,7 @@ private void onEvent(Event event) {
415471

416472
private TaskHandle add(final TaskHandle taskHandle) throws InterruptedException {
417473
if (closed) {
418-
throw new IllegalStateException("BatchContext is closed");
474+
throw new IllegalStateException("context is closed");
419475
}
420476

421477
TaskHandle existing = wip.get(taskHandle.id());

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,13 @@ public void test_add_closed() throws Exception {
452452
context.add(WeaviateObject.of(o -> o.properties(Map.of())));
453453
}
454454

455+
@Test(expected = IllegalStateException.class)
456+
public void test_startAfterClose() throws Exception {
457+
in.expectMessage(WeaviateProtoBatch.BatchStreamRequest.MessageCase.START);
458+
context.close();
459+
context.start();
460+
}
461+
455462
/**
456463
* Read the next Data message from the stream and ACK it.
457464
* This method does not wait for the server to process the Acks.

0 commit comments

Comments
 (0)