Disruptor 学习

Disruptor 是什么

Disruptor 是一种解决并发编程难题的通用机制

Disruptor 结合使用

应用场景:

做埋点上报记录,埋点上报的特点: 多线程同时操作,而且并发量较大。

Disruptor分为生产者和消费者,生产者向环形数组中添加消息,消费者从环形数组中取消息

生产者写法:

java 复制代码
@Component
@Slf4j
public class FillingPointGatewayImpl implements FillingPointGateway {

    private Disruptor<FillingPointEvent> disruptor;
    private RingBuffer<FillingPointEvent> ringBuffer;
    private ExecutorService executorService;

    @Resource
    private SystemValueConfig systemValueConfig;

    @Resource
    private FillingPointEventHandler fillingPointEventHandler;

    @PostConstruct
    public void initAddFillPointCmdExe() {
        FillingPointEventFactory factory = new FillingPointEventFactory();
        int bufferSize = 1024;

        executorService = Executors.newCachedThreadPool();
        disruptor = new Disruptor<>(factory, bufferSize, executorService);
        disruptor.handleEventsWith(fillingPointEventHandler);
        disruptor.start();

        ringBuffer = disruptor.getRingBuffer();
    }

    @Override
    public boolean add(FillingPointCmd fillingPointCmd) {
        log.info("AddFillPointCmdExe execute");
        long sequence = ringBuffer.next();
        try {
            FillingPointEvent event = ringBuffer.get(sequence);
            event.setCmd(fillingPointCmd);
        } finally {
            ringBuffer.publish(sequence);
        }
        return true;
    }

    @PreDestroy
    public void shutdown() {
        log.info("Shutting down Disruptor");
        disruptor.shutdown();
        fillingPointEventHandler.flushCache();
        executorService.shutdown();
    }
}

消费者写法

java 复制代码
@Slf4j
@Component
public class FillingPointEventHandler implements EventHandler<FillingPointEvent> {

    private final List<FillingPointCmd> batch = new ArrayList<>();

    @Resource
    private SystemValueConfig systemValueConfig;

    @Resource
    private PetFillingPointMapper petFillingPointMapper;

    @PostConstruct
    public void init() {
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        scheduler.scheduleAtFixedRate(this::flushCache, 120, systemValueConfig.getFillPointFlushInterval(), TimeUnit.SECONDS);
    }

    @Override
    public void onEvent(FillingPointEvent event, long sequence, boolean endOfBatch) {
        log.info("FillingPointEventHandler onEvent event:{}", JsonUtil.of(event.getCmd()));
        batch.add(event.getCmd());
        if (batch.size() >= systemValueConfig.getFillPointBatchSize()) {
            log.info("batch size:{} try to flush cache ", batch.size());
            flushCache();
        }
    }

    public void flushCache() {
        if (!batch.isEmpty()) {
            // 将批量数据写入数据库
            insertBatch(batch);
            log.info("Flushing cache with {} items", batch.size());
            batch.clear();
        }
    }

    private void insertBatch(List<FillingPointCmd> fillingPointCmds) {
        List<PetFillingPointDO> petFillingPointDOs = FillingPointConvertorMapper.INSTANCE.toDOList(fillingPointCmds);
        petFillingPointMapper.batchInsert(petFillingPointDOs);
    }
}

为什么要用 Disruptor

对比于传统 Java 的线程安全的内存队列

这些队列要么加锁有界,要么不加锁无界,如果想要一个不加锁使用 CAS 操作的有界队列,是没有的,这时候就可以用Disruptor

该图是 Lmax 官方对于 Disruptor 和 ArrayBlockingQueue 的性能对比,上面表示,Disruptor能在很低的 ns(纳秒)延迟下处理很多事件,而ArrayBlockingQueue需要在比较高的延迟下才能处理完相关事件。

Disruptor 其他支持的功能

  1. 生成Disruptor对象的时候可以指定单生产者和多生产者,也就是单线程和多线程的生产者。
  2. 支持多消费者,并且多消费者可以有顺序

Disruptor 为什么这么快

Disruptor 原理图:

RingBuffer 是个环形数组

Producter 向里面写入消息

消费者们通过 SequenceBarrier 来消费消息

消费者等待队列消息的时候可以执行 waitFor()的策略

  • Disruptor 是怎么解决之前队列里面性能问题的
  1. 数组要比链表快,内存空间连续,对 CPU 友好

  2. 传统队列中在队列末尾多个生产者争抢一个尾标这种情况下总是需要加锁,但是Disruptor是每个生产者都会有自己的一个 Sequence,在生产者投放消息的过程中,先使用 CAS 申请空间,申请成功后根据返回的最大序列来投放消息。

  3. 伪共享的问题解决

每个线程要处理变量,需要先从主内存读取变量到本地中,CPU 读取变量是以缓存行的形式读取。 假设一个类有两个字段

java 复制代码
{
    int productValue;
    int consumerValue;
}

CPU 加载时这两个字段有可能是加载到一个缓存行,如果生产者更新了productValue,没有更新consumerValue,但是由于在一个缓存行,consumerValue之前缓存的值也会失效。

这种无法充分使用缓存行特性的现象,称为伪共享。

Disruptor在每个字段中添加了多个空数据,这样保证productValue和consumerValue能分配到两个不同的缓存行

java 复制代码
abstract class SingleProducerSequencerPad extends AbstractSequencer
{
    protected long p1, p2, p3, p4, p5, p6, p7;

    SingleProducerSequencerPad(int bufferSize, WaitStrategy waitStrategy)
    {
        super(bufferSize, waitStrategy);
    }
}

abstract class SingleProducerSequencerFields extends SingleProducerSequencerPad
{
    SingleProducerSequencerFields(int bufferSize, WaitStrategy waitStrategy)
    {
        super(bufferSize, waitStrategy);
    }

    /**
     * Set to -1 as sequence starting point
     */
    long nextValue = Sequence.INITIAL_VALUE;
    long cachedValue = Sequence.INITIAL_VALUE;
}

/**
 * <p>Coordinator for claiming sequences for access to a data structure while tracking dependent {@link Sequence}s.
 * Not safe for use from multiple threads as it does not implement any barriers.</p>
 *
 * <p>* Note on {@link Sequencer#getCursor()}:  With this sequencer the cursor value is updated after the call
 * to {@link Sequencer#publish(long)} is made.</p>
 */

public final class SingleProducerSequencer extends SingleProducerSequencerFields
{
    protected long p1, p2, p3, p4, p5, p6, p7;
  • Disruptor 是如何保证投递的数据其他消费者立即可以看到

即使不在 Event 对象上添加 volatile 关键字,生产者投放的消息,其他线程也都能立马看到,是因为Disruptor在写以后添加了写屏障、读之前又添加了读屏障

Disruptor 源码解析

引用文章:juejin.cn/post/722282...

相关推荐
元亓亓亓18 分钟前
Leet热题100--208. 实现 Trie (前缀树)--中等
java·开发语言
拿破轮21 分钟前
不小心在idea中点了add 到版本控制 怎么样恢复?
java·ide·intellij-idea
cynicme6 小时前
力扣3318——计算子数组的 x-sum I(偷懒版)
java·算法·leetcode
青云交7 小时前
Java 大视界 -- Java 大数据在智能教育学习效果评估与教学质量改进实战
java·实时分析·生成式 ai·个性化教学·智能教育·学习效果评估·教学质量改进
崎岖Qiu7 小时前
【设计模式笔记17】:单例模式1-模式分析
java·笔记·单例模式·设计模式
Lei活在当下7 小时前
【现代 Android APP 架构】09. 聊一聊依赖注入在 Android 开发中的应用
java·架构·android jetpack
不穿格子的程序员8 小时前
从零开始刷算法-栈-括号匹配
java·开发语言·
lkbhua莱克瓦248 小时前
Java练习-正则表达式 1
java·笔记·正则表达式·github
yue0088 小时前
C#类继承
java·开发语言·c#
凯芸呢9 小时前
Java中的数组(续)
java·开发语言·数据结构·算法·青少年编程·排序算法·idea