Skip to content

Commit 1a063ba

Browse files
committed
refactor: handle all events in BaseState
1 parent a500693 commit 1a063ba

2 files changed

Lines changed: 46 additions & 62 deletions

File tree

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

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -641,40 +641,11 @@ public void onError(Throwable t) {
641641
}
642642
}
643643

644-
private final State AWAIT_STARTED = new BaseState("AWAIT_STARTED", BaseState.Action.PREPARE_NEXT) {
645-
@Override
646-
public void onEvent(Event event) {
647-
if (event == Event.STARTED) {
648-
setState(ACTIVE);
649-
} else {
650-
super.onEvent(event);
651-
}
652-
}
653-
};
644+
private final State AWAIT_STARTED = new BaseState("AWAIT_STARTED", BaseState.Action.PREPARE_NEXT);
654645
private final State ACTIVE = new BaseState("ACTIVE", BaseState.Action.PREPARE_NEXT, BaseState.Action.SEND);
655-
private final State IN_FLIGHT = new BaseState("IN_FLIGHT") {
656-
@Override
657-
public void onEvent(Event event) {
658-
if (event instanceof Event.Acks acks) {
659-
Collection<String> removed = batch.clear();
660-
if (!acks.acked().containsAll(removed)) {
661-
throw ProtocolViolationException.incompleteAcks(List.copyOf(removed));
662-
}
663-
acks.acked().forEach(id -> {
664-
TaskHandle task = wip.get(id);
665-
if (task != null) {
666-
task.setAcked();
667-
}
668-
});
669-
setState(ACTIVE);
670-
} else if (event instanceof Event.Oom oom) {
671-
setState(new Oom(oom.delaySeconds()));
672-
} else {
673-
super.onEvent(event);
674-
}
675-
}
676-
};
646+
private final State IN_FLIGHT = new BaseState("IN_FLIGHT");
677647

648+
/** BaseState implements default handlers for all {@link Event} subclasses. */
678649
private class BaseState implements State {
679650
/** State's display name for logging. */
680651
private final String name;
@@ -717,24 +688,15 @@ public boolean canPrepareNext() {
717688
return permitted.contains(Action.PREPARE_NEXT);
718689
}
719690

720-
/**
721-
* Handle events which may arrive at any moment without violating the protocol.
722-
*
723-
* <ul>
724-
* <li>{@link Event.Results} -- update tasks in {@link #wip} and remove them.
725-
* <li>{@link Event.Backoff} -- adjust batch size.
726-
* <li>{@link Event#SHUTTING_DOWN} -- transition into
727-
* {@link ServerShuttingDown}.
728-
* <li>{@link Event.StreamHangup -- transition into {@link Reconnecting} state.
729-
* <li>{@link Event.ClientError -- shutdown the service immediately.
730-
* </ul>
731-
*
732-
* @throws ProtocolViolationException If event cannot be handled in this state.
733-
* @see BatchContext#shutdownNow
734-
*/
735691
@Override
736692
public void onEvent(Event event) {
737-
if (event instanceof Event.Results results) {
693+
if (event == Event.STARTED) {
694+
onStarted();
695+
} else if (event instanceof Event.Acks acks) {
696+
onAcks(acks);
697+
} else if (event instanceof Event.Oom oom) {
698+
onOom(oom);
699+
} else if (event instanceof Event.Results results) {
738700
onResults(results);
739701
} else if (event instanceof Event.Backoff backoff) {
740702
onBackoff(backoff);
@@ -745,10 +707,28 @@ public void onEvent(Event event) {
745707
} else if (event instanceof Event.ClientError error) {
746708
onClientError(error);
747709
} else {
748-
throw ProtocolViolationException.illegalStateTransition(this, event);
710+
throw new AssertionError("unreachable with event " + event);
749711
}
750712
}
751713

714+
private void onStarted() {
715+
setState(ACTIVE);
716+
}
717+
718+
private void onAcks(Event.Acks acks) {
719+
Collection<String> removed = batch.clear();
720+
if (!acks.acked().containsAll(removed)) {
721+
throw ProtocolViolationException.incompleteAcks(List.copyOf(removed));
722+
}
723+
acks.acked().forEach(id -> {
724+
TaskHandle task = wip.get(id);
725+
if (task != null) {
726+
task.setAcked();
727+
}
728+
});
729+
setState(ACTIVE);
730+
}
731+
752732
private void onResults(Event.Results results) {
753733
results.successful().forEach(id -> wip.remove(id).setSuccess());
754734
results.errors().forEach((id, error) -> wip.remove(id).setError(error));
@@ -758,6 +738,10 @@ private void onBackoff(Event.Backoff backoff) {
758738
batch.setMaxSize(backoff.maxSize());
759739
}
760740

741+
private void onOom(Event.Oom oom) {
742+
setState(new Oom(oom.delaySeconds()));
743+
}
744+
761745
private void onShuttingDown() {
762746
setState(new ServerShuttingDown(this));
763747
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public synchronized void reset() throws Exception {
119119
}
120120

121121
@AfterClass
122-
public static void closeExecutor() throws Exception {
122+
public static void shutdownExecutors() throws Exception {
123123
boolean terminated;
124124

125125
BACKGROUND.shutdown();
@@ -136,7 +136,7 @@ public static void closeExecutor() throws Exception {
136136
private static final WeaviateProtoBatch.BatchStreamRequest.MessageCase DATA = WeaviateProtoBatch.BatchStreamRequest.MessageCase.DATA;
137137

138138
@Test
139-
public void test_sendOneBatch() throws Exception {
139+
public synchronized void test_sendOneBatch() throws Exception {
140140
in.expectMessage(START);
141141
out.emitEvent(Event.STARTED);
142142

@@ -166,7 +166,7 @@ public void test_sendOneBatch() throws Exception {
166166
}
167167

168168
@Test
169-
public void test_drainOnClose() throws Exception {
169+
public synchronized void test_drainOnClose() throws Exception {
170170
in.expectMessage(START);
171171
out.emitEvent(Event.STARTED);
172172

@@ -202,7 +202,7 @@ public void test_drainOnClose() throws Exception {
202202
}
203203

204204
@Test
205-
public void test_backoff() throws Exception {
205+
public synchronized void test_backoff() throws Exception {
206206
in.expectMessage(START);
207207
out.emitEvent(Event.STARTED);
208208

@@ -242,7 +242,7 @@ public void test_backoff() throws Exception {
242242
}
243243

244244
@Test
245-
public void test_backoffBacklog() throws Exception {
245+
public synchronized void test_backoffBacklog() throws Exception {
246246
in.expectMessage(START);
247247
out.emitEvent(Event.STARTED);
248248

@@ -279,7 +279,7 @@ public void test_backoffBacklog() throws Exception {
279279
}
280280

281281
@Test
282-
public void test_reconnect_onShutdown() throws Exception {
282+
public synchronized void test_reconnect_onShutdown() throws Exception {
283283
in.expectMessage(START);
284284
out.emitEvent(Event.STARTED);
285285

@@ -293,7 +293,7 @@ public void test_reconnect_onShutdown() throws Exception {
293293
}
294294

295295
@Test
296-
public void test_reconnect_onOom() throws Exception {
296+
public synchronized void test_reconnect_onOom() throws Exception {
297297
in.expectMessage(START);
298298
out.emitEvent(Event.STARTED);
299299

@@ -321,7 +321,7 @@ public void test_reconnect_onOom() throws Exception {
321321
}
322322

323323
@Test
324-
public void test_reconnect_onStreamHangup() throws Exception {
324+
public synchronized void test_reconnect_onStreamHangup() throws Exception {
325325
in.expectMessage(START);
326326
out.emitEvent(Event.STARTED);
327327

@@ -364,7 +364,7 @@ public void test_reconnect_onStreamHangup() throws Exception {
364364
}
365365

366366
@Test
367-
public void test_reconnect_DrainAfterStreamHangup() throws Exception {
367+
public synchronized void test_reconnect_DrainAfterStreamHangup() throws Exception {
368368
in.expectMessage(START);
369369
out.emitEvent(Event.STARTED);
370370

@@ -417,15 +417,15 @@ public void test_reconnect_DrainAfterStreamHangup() throws Exception {
417417
}
418418

419419
@Test
420-
public void test_closeAfterStreamHangup() throws Exception {
420+
public synchronized void test_closeAfterStreamHangup() throws Exception {
421421
in.expectMessage(START);
422422
out.emitEvent(Event.STARTED);
423423

424424
out.hangup();
425425
}
426426

427427
@Test
428-
public void test_maxReconnectRetries() throws Exception {
428+
public synchronized void test_maxReconnectRetries() throws Exception {
429429
in.expectMessage(START);
430430

431431
// Drop the connection several times until the client
@@ -450,14 +450,14 @@ public void test_maxReconnectRetries() throws Exception {
450450
}
451451

452452
@Test(expected = IllegalStateException.class)
453-
public void test_add_closed() throws Exception {
453+
public synchronized void test_add_closed() throws Exception {
454454
in.expectMessage(START);
455455
context.close();
456456
context.add(WeaviateObject.of(o -> o.properties(Map.of())));
457457
}
458458

459459
@Test(expected = IllegalStateException.class)
460-
public void test_startAfterClose() throws Exception {
460+
public synchronized void test_startAfterClose() throws Exception {
461461
in.expectMessage(START);
462462
context.close();
463463
context.start();

0 commit comments

Comments
 (0)