|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.db.pipe.source.dataregion.realtime.disruptor; |
| 21 | + |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
| 24 | + |
| 25 | +import java.util.concurrent.ThreadFactory; |
| 26 | + |
| 27 | +/** |
| 28 | + * Simplified Disruptor implementation for IoTDB Pipe |
| 29 | + * |
| 30 | + * <p>This implementation is based on LMAX Disruptor (https://github.com/LMAX-Exchange/disruptor) |
| 31 | + * and simplified for IoTDB's specific use case in the Pipe module. |
| 32 | + * |
| 33 | + * <p>Key simplifications: |
| 34 | + * |
| 35 | + * <ul> |
| 36 | + * <li>Single event handler support (no complex dependency graphs) |
| 37 | + * <li>Simplified lifecycle management |
| 38 | + * <li>Removed wait strategies (using simple sleep-based waiting) |
| 39 | + * </ul> |
| 40 | + * |
| 41 | + * @param <T> event type |
| 42 | + */ |
| 43 | +public class Disruptor<T> { |
| 44 | + private static final Logger LOGGER = LoggerFactory.getLogger(Disruptor.class); |
| 45 | + |
| 46 | + private final RingBuffer<T> ringBuffer; |
| 47 | + private final ThreadFactory threadFactory; |
| 48 | + private BatchEventProcessor<T> processor; |
| 49 | + private Thread processorThread; |
| 50 | + private ExceptionHandler<? super T> exceptionHandler; |
| 51 | + private volatile boolean started = false; |
| 52 | + |
| 53 | + /** |
| 54 | + * Create a Disruptor instance |
| 55 | + * |
| 56 | + * @param eventFactory factory for creating pre-allocated events |
| 57 | + * @param ringBufferSize buffer size (must be power of 2) |
| 58 | + * @param threadFactory factory for creating consumer thread |
| 59 | + */ |
| 60 | + public Disruptor(EventFactory<T> eventFactory, int ringBufferSize, ThreadFactory threadFactory) { |
| 61 | + this.ringBuffer = RingBuffer.createMultiProducer(eventFactory, ringBufferSize); |
| 62 | + this.threadFactory = threadFactory; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Configure event handler for processing events |
| 67 | + * |
| 68 | + * <p>Creates a batch event processor that will run in its own thread |
| 69 | + * |
| 70 | + * @param handler event handler implementation |
| 71 | + * @return this instance for method chaining |
| 72 | + */ |
| 73 | + public Disruptor<T> handleEventsWith(final EventHandler<? super T> handler) { |
| 74 | + SequenceBarrier barrier = ringBuffer.newBarrier(); |
| 75 | + processor = new BatchEventProcessor<>(ringBuffer, barrier, handler); |
| 76 | + |
| 77 | + if (exceptionHandler != null) { |
| 78 | + processor.setExceptionHandler(exceptionHandler); |
| 79 | + } |
| 80 | + |
| 81 | + ringBuffer.addGatingSequences(processor.getSequence()); |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Set exception handler for error handling |
| 87 | + * |
| 88 | + * @param exceptionHandler handler for processing exceptions |
| 89 | + */ |
| 90 | + public void setDefaultExceptionHandler(ExceptionHandler<? super T> exceptionHandler) { |
| 91 | + this.exceptionHandler = exceptionHandler; |
| 92 | + if (processor != null) { |
| 93 | + processor.setExceptionHandler(exceptionHandler); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + public RingBuffer<T> start() { |
| 98 | + if (started) { |
| 99 | + throw new IllegalStateException("Disruptor already started"); |
| 100 | + } |
| 101 | + |
| 102 | + if (processor == null) { |
| 103 | + throw new IllegalStateException("No event handler configured"); |
| 104 | + } |
| 105 | + |
| 106 | + processorThread = threadFactory.newThread(processor); |
| 107 | + processorThread.start(); |
| 108 | + started = true; |
| 109 | + |
| 110 | + LOGGER.info("Disruptor started with buffer size: {}", ringBuffer.getBufferSize()); |
| 111 | + return ringBuffer; |
| 112 | + } |
| 113 | + |
| 114 | + public void shutdown() { |
| 115 | + if (!started) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + if (processor != null) { |
| 120 | + processor.halt(); |
| 121 | + } |
| 122 | + |
| 123 | + if (processorThread != null) { |
| 124 | + try { |
| 125 | + processorThread.join(5000); |
| 126 | + } catch (InterruptedException e) { |
| 127 | + Thread.currentThread().interrupt(); |
| 128 | + LOGGER.warn("Interrupted waiting for processor to stop"); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + started = false; |
| 133 | + LOGGER.info("Disruptor shutdown completed"); |
| 134 | + } |
| 135 | +} |
0 commit comments