|
34 | 34 | import java.util.concurrent.CompletableFuture;
|
35 | 35 | import java.util.concurrent.LinkedBlockingQueue;
|
36 | 36 |
|
| 37 | +/** |
| 38 | + * Processes events from the Streams rebalance protocol. |
| 39 | + * <p> |
| 40 | + * The Streams rebalance processor receives events from the background thread of the async consumer, more precisely |
| 41 | + * from the Streams membership manager and handles them. |
| 42 | + * For example, events are requests for invoking the task assignment and task revocation callbacks. |
| 43 | + * Results of the event handling are passed back to the background thread. |
| 44 | + */ |
37 | 45 | public class StreamsRebalanceEventsProcessor {
|
38 | 46 |
|
39 | 47 | private final BlockingQueue<BackgroundEvent> onCallbackRequests = new LinkedBlockingQueue<>();
|
40 | 48 | private ApplicationEventHandler applicationEventHandler = null;
|
41 | 49 | private final StreamsGroupRebalanceCallbacks rebalanceCallbacks;
|
42 | 50 | private final StreamsRebalanceData streamsRebalanceData;
|
43 | 51 |
|
| 52 | + /** |
| 53 | + * Constructs the Streams rebalance processor. |
| 54 | + * |
| 55 | + * @param streamsRebalanceData |
| 56 | + * @param rebalanceCallbacks |
| 57 | + */ |
44 | 58 | public StreamsRebalanceEventsProcessor(StreamsRebalanceData streamsRebalanceData,
|
45 | 59 | StreamsGroupRebalanceCallbacks rebalanceCallbacks) {
|
46 | 60 | this.streamsRebalanceData = streamsRebalanceData;
|
47 | 61 | this.rebalanceCallbacks = rebalanceCallbacks;
|
48 | 62 | }
|
49 | 63 |
|
| 64 | + /** |
| 65 | + * Requests the invocation of the task assignment callback. |
| 66 | + * |
| 67 | + * @param assignment The tasks to be assigned to the member of the Streams group. |
| 68 | + * @return A future that will be completed when the callback has been invoked. |
| 69 | + */ |
50 | 70 | public CompletableFuture<Void> requestOnTasksAssignedCallbackInvocation(final StreamsRebalanceData.Assignment assignment) {
|
51 | 71 | final StreamsOnTasksAssignedCallbackNeededEvent onTasksAssignedCallbackNeededEvent = new StreamsOnTasksAssignedCallbackNeededEvent(assignment);
|
52 | 72 | onCallbackRequests.add(onTasksAssignedCallbackNeededEvent);
|
53 | 73 | return onTasksAssignedCallbackNeededEvent.future();
|
54 | 74 | }
|
55 | 75 |
|
| 76 | + /** |
| 77 | + * Requests the invocation of the task revocation callback. |
| 78 | + * |
| 79 | + * @param activeTasksToRevoke The tasks to revoke from the member of the Streams group |
| 80 | + * @return A future that will be completed when the callback has been invoked. |
| 81 | + */ |
56 | 82 | public CompletableFuture<Void> requestOnTasksRevokedCallbackInvocation(final Set<StreamsRebalanceData.TaskId> activeTasksToRevoke) {
|
57 | 83 | final StreamsOnTasksRevokedCallbackNeededEvent onTasksRevokedCallbackNeededEvent = new StreamsOnTasksRevokedCallbackNeededEvent(activeTasksToRevoke);
|
58 | 84 | onCallbackRequests.add(onTasksRevokedCallbackNeededEvent);
|
59 | 85 | return onTasksRevokedCallbackNeededEvent.future();
|
60 | 86 | }
|
61 | 87 |
|
| 88 | + /** |
| 89 | + * Requests the invocation of the all tasks lost callback. |
| 90 | + * |
| 91 | + * @return A future that will be completed when the callback has been invoked. |
| 92 | + */ |
62 | 93 | public CompletableFuture<Void> requestOnAllTasksLostCallbackInvocation() {
|
63 | 94 | final StreamsOnAllTasksLostCallbackNeededEvent onAllTasksLostCallbackNeededEvent = new StreamsOnAllTasksLostCallbackNeededEvent();
|
64 | 95 | onCallbackRequests.add(onAllTasksLostCallbackNeededEvent);
|
65 | 96 | return onAllTasksLostCallbackNeededEvent.future();
|
66 | 97 | }
|
67 | 98 |
|
| 99 | + /** |
| 100 | + * Sets the application event handler. |
| 101 | + * |
| 102 | + * The application handler sends the results of the callbacks to the background thread. |
| 103 | + * |
| 104 | + * @param applicationEventHandler The application handler. |
| 105 | + */ |
68 | 106 | public void setApplicationEventHandler(final ApplicationEventHandler applicationEventHandler) {
|
69 | 107 | this.applicationEventHandler = applicationEventHandler;
|
70 | 108 | }
|
@@ -153,6 +191,9 @@ private StreamsOnAllTasksLostCallbackCompletedEvent invokeOnAllTasksLostCallback
|
153 | 191 | return new StreamsOnAllTasksLostCallbackCompletedEvent(future, error);
|
154 | 192 | }
|
155 | 193 |
|
| 194 | + /** |
| 195 | + * Processes all events received from the background thread so far. |
| 196 | + */ |
156 | 197 | public void process() {
|
157 | 198 | LinkedList<BackgroundEvent> events = new LinkedList<>();
|
158 | 199 | onCallbackRequests.drainTo(events);
|
|
0 commit comments