|
| 1 | +package io.numaproj.numaflow.examples.sink.forkjoin; |
| 2 | + |
| 3 | +import io.numaproj.numaflow.sinker.Datum; |
| 4 | +import io.numaproj.numaflow.sinker.DatumIterator; |
| 5 | +import io.numaproj.numaflow.sinker.Response; |
| 6 | +import io.numaproj.numaflow.sinker.ResponseList; |
| 7 | +import io.numaproj.numaflow.sinker.Server; |
| 8 | +import io.numaproj.numaflow.sinker.Sinker; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.concurrent.*; |
| 14 | + |
| 15 | +/** |
| 16 | + * ConcurrentSink demonstrates concurrent processing of incoming messages using ThreadPoolExecutor. |
| 17 | + * This example shows how to process messages in parallel using a thread pool for |
| 18 | + * CPU-intensive operations where parallel processing can improve performance. |
| 19 | + * |
| 20 | + * Key features: |
| 21 | + * - Uses ThreadPoolExecutor for parallel execution |
| 22 | + * - Processes each message independently in parallel |
| 23 | + * - Demonstrates concurrent data transformation |
| 24 | + * - Handles exceptions gracefully in parallel processing |
| 25 | + * - Shows how to aggregate results from multiple threads |
| 26 | + */ |
| 27 | +@Slf4j |
| 28 | +public class ConcurrentSink extends Sinker { |
| 29 | + |
| 30 | + private static final int DEFAULT_THREAD_POOL_SIZE = Runtime.getRuntime().availableProcessors(); |
| 31 | + |
| 32 | + private final ThreadPoolExecutor threadPool; |
| 33 | + |
| 34 | + public ConcurrentSink() { |
| 35 | + this(DEFAULT_THREAD_POOL_SIZE); |
| 36 | + } |
| 37 | + |
| 38 | + public ConcurrentSink(int threadPoolSize) { |
| 39 | + this.threadPool = new ThreadPoolExecutor( |
| 40 | + threadPoolSize, |
| 41 | + threadPoolSize, |
| 42 | + 60L, |
| 43 | + TimeUnit.SECONDS, |
| 44 | + new LinkedBlockingQueue<>(), |
| 45 | + new ThreadFactory() { |
| 46 | + private int counter = 0; |
| 47 | + @Override |
| 48 | + public Thread newThread(Runnable r) { |
| 49 | + return new Thread(r, "ConcurrentSink-Worker-" + (++counter)); |
| 50 | + } |
| 51 | + } |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public static void main(String[] args) throws Exception { |
| 56 | + ConcurrentSink concurrentSink = new ConcurrentSink(); |
| 57 | + |
| 58 | + Server server = new Server(concurrentSink); |
| 59 | + server.start(); |
| 60 | + server.awaitTermination(); |
| 61 | + server.stop(); |
| 62 | + |
| 63 | + concurrentSink.shutdown(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public ResponseList processMessages(DatumIterator datumIterator) { |
| 68 | + log.info("Starting concurrent processing with thread pool size: {}", |
| 69 | + threadPool.getCorePoolSize()); |
| 70 | + |
| 71 | + List<Datum> messages = new ArrayList<>(); |
| 72 | + while (true) { |
| 73 | + Datum datum; |
| 74 | + try { |
| 75 | + datum = datumIterator.next(); |
| 76 | + } catch (InterruptedException e) { |
| 77 | + Thread.currentThread().interrupt(); |
| 78 | + continue; |
| 79 | + } |
| 80 | + if (datum == null) { |
| 81 | + break; |
| 82 | + } |
| 83 | + messages.add(datum); |
| 84 | + } |
| 85 | + |
| 86 | + log.info("Collected {} messages for concurrent processing", messages.size()); |
| 87 | + |
| 88 | + if (messages.isEmpty()) { |
| 89 | + return ResponseList.newBuilder().build(); |
| 90 | + } |
| 91 | + |
| 92 | + List<Response> allResponses = processInParallel(messages); |
| 93 | + |
| 94 | + log.info("Completed concurrent processing, generated {} responses", allResponses.size()); |
| 95 | + |
| 96 | + ResponseList.ResponseListBuilder responseListBuilder = ResponseList.newBuilder(); |
| 97 | + for (Response response : allResponses) { |
| 98 | + responseListBuilder.addResponse(response); |
| 99 | + } |
| 100 | + |
| 101 | + return responseListBuilder.build(); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Processes messages in parallel using ThreadPoolExecutor. |
| 106 | + * Each message is processed independently in a separate thread. |
| 107 | + */ |
| 108 | + private List<Response> processInParallel(List<Datum> messages) { |
| 109 | + List<Future<Response>> futures = new ArrayList<>(); |
| 110 | + |
| 111 | + for (Datum message : messages) { |
| 112 | + Future<Response> future = threadPool.submit(new MessageProcessingTask(message)); |
| 113 | + futures.add(future); |
| 114 | + } |
| 115 | + |
| 116 | + List<Response> allResponses = new ArrayList<>(); |
| 117 | + for (Future<Response> future : futures) { |
| 118 | + try { |
| 119 | + Response response = future.get(30, TimeUnit.SECONDS); |
| 120 | + allResponses.add(response); |
| 121 | + } catch (InterruptedException e) { |
| 122 | + Thread.currentThread().interrupt(); |
| 123 | + log.error("Interrupted while waiting for message processing", e); |
| 124 | + } catch (ExecutionException e) { |
| 125 | + log.error("Error during message processing", e.getCause()); |
| 126 | + } catch (TimeoutException e) { |
| 127 | + log.error("Timeout waiting for message processing", e); |
| 128 | + future.cancel(true); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return allResponses; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Task that processes a single message in a thread. |
| 137 | + * This is where the actual CPU-intensive work would be done. |
| 138 | + */ |
| 139 | + private static class MessageProcessingTask implements Callable<Response> { |
| 140 | + private final Datum datum; |
| 141 | + |
| 142 | + public MessageProcessingTask(Datum datum) { |
| 143 | + this.datum = datum; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public Response call() { |
| 148 | + try { |
| 149 | + String message = new String(datum.getValue()); |
| 150 | + String processedMessage = processMessage(message); |
| 151 | + |
| 152 | + log.debug("Processed message {} -> {}", message, processedMessage); |
| 153 | + return Response.responseOK(datum.getId()); |
| 154 | + |
| 155 | + } catch (Exception e) { |
| 156 | + log.error("Error processing message with ID: {}", datum.getId(), e); |
| 157 | + return Response.responseFailure(datum.getId(), e.getMessage()); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Simulates CPU-intensive message processing. |
| 163 | + * In a real-world scenario, this could be data transformation, validation, |
| 164 | + * encryption, compression, or any other compute-intensive operation. |
| 165 | + */ |
| 166 | + private String processMessage(String message) { |
| 167 | + StringBuilder processed = new StringBuilder(); |
| 168 | + processed.append("PROCESSED[") |
| 169 | + .append(new StringBuilder(message).reverse()) |
| 170 | + .append("]-") |
| 171 | + .append(Thread.currentThread().getName()) |
| 172 | + .append("-") |
| 173 | + .append(System.currentTimeMillis() % 1000); |
| 174 | + |
| 175 | + for (int i = 0; i < 100; i++) { |
| 176 | + Math.sqrt(i * message.hashCode()); |
| 177 | + } |
| 178 | + |
| 179 | + return processed.toString(); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Shutdown the thread pool gracefully. |
| 185 | + * This should be called when the sink is no longer needed. |
| 186 | + */ |
| 187 | + public void shutdown() { |
| 188 | + log.info("Shutting down concurrent sink thread pool"); |
| 189 | + threadPool.shutdown(); |
| 190 | + try { |
| 191 | + if (!threadPool.awaitTermination(10, TimeUnit.SECONDS)) { |
| 192 | + threadPool.shutdownNow(); |
| 193 | + } |
| 194 | + } catch (InterruptedException e) { |
| 195 | + threadPool.shutdownNow(); |
| 196 | + Thread.currentThread().interrupt(); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Get current thread pool statistics for monitoring. |
| 202 | + */ |
| 203 | + public String getThreadPoolStats() { |
| 204 | + return String.format("ThreadPool[active=%d, completed=%d, queued=%d, pool=%d/%d]", |
| 205 | + threadPool.getActiveCount(), |
| 206 | + threadPool.getCompletedTaskCount(), |
| 207 | + threadPool.getQueue().size(), |
| 208 | + threadPool.getPoolSize(), |
| 209 | + threadPool.getMaximumPoolSize()); |
| 210 | + } |
| 211 | +} |
0 commit comments