-
Notifications
You must be signed in to change notification settings - Fork 136
Example : Implementing a Quorum
When operating under SLA's it may be better to return the result of some processing and meet the SLA than wait until all processing is complete and return the full result.
The block method in SimpleReact facilitates this by accepting a Predicate. The Status entity has the following fields available :
int completed; (successfully completed results)
int errors; (completed with errors) (getAllCompleted = completed + eror
int total; (total expected results)
long elapsedNanos; (elapsed time in nanos - also available in millis)
In the example below we slow the execution of each dataflow down by a different amount. The last result will take over 300 ms to complete, but we set an SLA of 200ms and will short circuit the process once the first two results are in.
List<Integer> result = new SimpleReact()
.<Integer> react(() -> 1, () -> 2, () -> 3)
.then(it -> it * 100)
.then(it -> {
sleep(it);
return it;
})
.block(status -> status.getAllCompleted() >1 && status.getElapsedMillis()>200);
assertThat(result.size(), is(2));
Similar implementation using LazyFutureStream
LazyFutureStream.parallel(1,2,3)
.map(it -> it * 100)
.consume(it -> sleep(it))
.block(status -> status.getAllCompleted() >1 && status.getElapsedMillis()>200);
oops - my bad