// 核心是一个循环缓冲区。我们的循环缓冲区是一个LMAX Disruptor。当多个线程在单个WAL竞争append和sync时,它试图最小化同步与volatile写。 // Disruptor配置为处理多个生产者和仅有一个消费者(HBase中的生产者是调用append、sync的IPC Handlers)。单一的消费者从环形缓冲区中 // 拉去append和sync。
/** * RingBufferTruck为事件--Event * RingBufferTruck.EVENT_FACTORY为事件工厂--EventFactory * preallocatedEventCount为RingBuffer大小,必须是2的N次方,默认为1024*16 * appendExecutor为事件处理的线程池--newSingleThreadExecutor * ProducerType.MULTI为生产者类型,意思为多个生产者 * BlockingWaitStrategy为指定的等待策略 */ this.disruptor = new Disruptor<RingBufferTruck>(RingBufferTruck.EVENT_FACTORY, preallocatedEventCount, this.appendExecutor, ProducerType.MULTI, new BlockingWaitStrategy()); // Advance the ring buffer sequence so that it starts from 1 instead of 0, // because SyncFuture.NOT_DONE = 0. // 请求下一个事件序号 // 让ring buffer sequence从1开始,而不是从0开始 this.disruptor.getRingBuffer().next(); // 构造事件处理器实例 this.ringBufferEventHandler = new RingBufferEventHandler(conf.getInt("hbase.regionserver.hlog.syncer.count", 5), maxHandlersCount); // 添加异常处理器 this.disruptor.handleExceptionsWith(new RingBufferExceptionHandler()); // 添加事件处理器 this.disruptor.handleEventsWith(new RingBufferEventHandler [] {this.ringBufferEventHandler}); // Presize our map of SyncFutures by handler objects. this.syncFuturesByHandler = new ConcurrentHashMap<Thread, SyncFuture>(maxHandlersCount); // Starting up threads in constructor is a no no; Interface should have an init call. // 启动disruptor this.disruptor.start();