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...

相关推荐
纪莫23 分钟前
A公司一面:类加载的过程是怎么样的? 双亲委派的优点和缺点? 产生fullGC的情况有哪些? spring的动态代理有哪些?区别是什么? 如何排查CPU使用率过高?
java·java面试⑧股
JavaGuide1 小时前
JDK 25(长期支持版) 发布,新特性解读!
java·后端
用户3721574261351 小时前
Java 轻松批量替换 Word 文档文字内容
java
白鲸开源1 小时前
教你数分钟内创建并运行一个 DolphinScheduler Workflow!
java
Java中文社群2 小时前
有点意思!Java8后最有用新特性排行榜!
java·后端·面试
代码匠心2 小时前
从零开始学Flink:数据源
java·大数据·后端·flink
间彧2 小时前
Spring Boot项目中如何自定义线程池
java
间彧2 小时前
Java线程池详解与实战指南
java
用户298698530143 小时前
Java 使用 Spire.PDF 将PDF文档转换为Word格式
java·后端
渣哥3 小时前
ConcurrentHashMap 1.7 vs 1.8:分段锁到 CAS+红黑树的演进与性能差异
java