首页 > IT基础架构 > 服务器/存储 > Disruptor在撮合引擎的实践|得物技术
来源:得物技术
目录
一、Disruptor的简介
1. Disruptor的使用场景
2. Disruptor和ArrayBlockingQueue性能对比
3. Disruptor快速接入指南
4. Disruptor消费者等待策略
5. Disruptor灵活的消费者模式
二、Disruptor的核心概念
1. Disruptor内部组件交互图
2. 核心概念
三、Disruptor的特点
1. 环形数组结构
2. 无锁化设计
3. 独占缓存行的方式消除伪共享
4. 预分配内存
四、Disruptor在撮合引擎中的应用
1. 数字货币交易系统的简介
2. 撮合引擎流程图
3. 撮合引擎之Disruptor代码
五、总结
一
Disruptor的简介
Disruptor的使用场景
2010年在QCon的演讲,介绍了基于Disruptor开发的系统单线程能支撑每秒600万订单,由此可见该组件可以大幅提升系统的TPS,所以对于一些需要大幅提升单机应用的吞吐量的场景可以考虑使用Disruptor。
Disruptor和ArrayBlockingQueue性能对比

Figure 1. Unicast: 1P–1C

Figure 2. Three Step Pipeline: 1P–3C

Figure 3. Sequencer: 3P–1C

Figure 4. Multicast: 1P–3C

Figure 5. Diamond: 1P–3C

Disruptor快速接入指南
引入Maven依赖
<dependency>
<groupld>com.lmax</groupld>
<artifactld>disruptor</artifactld>
<version>4.0.0</version>
</dependency>
自定义事件和事件工厂
public class LongEvent {private long value;public void set(long value) {this.value = value;}public String toString() {return "LongEvent{" + "value=" + value + '}';}}public class LongEventFactory implements EventFactory<LongEvent> {public LongEvent newInstance() {return new LongEvent();}}
定义事件处理器,即消费者
public class LongEventHandler implements EventHandler<LongEvent> { @Override public void onEvent(LongEvent event, long sequence, boolean endOfBatch) { System.out.println("Event: " + event); }}定义事件生产者
import com.lmax.disruptor.RingBuffer;import com.lmax.disruptor.examples.longevent.LongEvent;import java.nio.ByteBuffer;public class LongEventProducer {private final RingBuffer<LongEvent> ringBuffer;public LongEventProducer(RingBuffer<LongEvent> ringBuffer) {this.ringBuffer = ringBuffer;}public void onData(ByteBuffer bb) {long sequence = ringBuffer.next();try {LongEvent event = ringBuffer.get(sequence);event.set(bb.getLong(0));}finally {ringBuffer.publish(sequence);}}}
编写启动类
public class LongEventMain { public static void main(String[] args) throws InterruptedException { // 消费者线程池 Executor executor = Executors.newCachedThreadPool(); // 事件工厂 LongEventFactory eventFactory = new LongEventFactory(); // 指定RingBuffer大小 int bufferSize = 1024; // 构造事件分发器 Disruptor<LongEvent> disruptor = new Disruptor<>(eventFactory , bufferSize , executor , ProducerType.SINGLE // 1.ProducerType.SINGLE 单生产者模式 2.ProducerType.MULTI 多生产者模式 , new YieldingWaitStrategy());//消费者等待策略 // 注册消费者 disruptor.handleEventsWith(new LongEventHandler()); // 启动事件分发 disruptor.start(); // 获取RingBuffer 用于生产事件 RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer(); LongEventProducer producer = new LongEventProducer(ringBuffer); ByteBuffer bb = ByteBuffer.allocate(8); for (long i=0;true; i++) { bb.putLong(0, i); // 发送事件 producer.onData(bb); Thread.sleep(1000); } }}Disruptor消费者等待策略

Disruptor灵活的消费者模式
支持单生产者和多生产者
单消费者
//注册单个消费者disruptor.handleEventsWith(new LongEventHandler());
多消费者:并行的、广播模式
//注册多个消费者disruptor.handleEventsWith(new LongEventHandler(), new LongEventHandler1(), new LongEventHandler2());
多消费者:并行的、消费者组模式
public class LongWorkHandler implements WorkHandler<LongEvent> {public void onEvent(LongEvent longEvent) throws Exception {System.out.println("Event: " + logEvent);}}public class OtherWorkHandler implements WorkHandler<LongEvent> {public void onEvent(LongEvent longEvent) throws Exception {System.out.println("Event: " + logEvent);}}//注册消费者组disruptor.handleEventsWithWorkerPool(new LongWorkHandler(), new LongWorkHandler(), new LongWorkHandler());

//注册消费者组1disruptor.handleEventsWithWorkerPool(new LongWorkHandler() , new LongWorkHandler() , new LongWorkHandler());//注册消费者组2disruptor.handleEventsWithWorkerPool(new OtherWorkHandler() , new OtherWorkHandler() , new OtherWorkHandler());

//注册消费者disruptor.handleEventsWithWorkerPool(new LongWorkHandler(), new LongWorkHandler(), new LongWorkHandler()) .thenHandleEventsWithWorkerPool(new OtherWorkHandler(), new OtherWorkHandler(), new OtherWorkHandler());
多消费者:链式、菱形、六边形执行模式

//链式disruptor.handleEventsWith(new LongEventHandler11()).then(new LongEventHandler12());disruptor.handleEventsWith(new LongEventHandler21()).then(new LongEventHandler22());//菱形disruptor.handleEventsWith(new LongEventHandler1(), new LongEventHandler2()).then(new LongEventHandler3());//六边形LongEventHandler handler11 = new LongEventHandler();LongEventHandler handler12 = new LongEventHandler();LongEventHandler handler21 = new LongEventHandler();LongEventHandler handler22 = new LongEventHandler();LongEventHandler handler3 = new LongEventHandler();disruptor.handleEventsWith(handler11, handler21);disruptor.after(handler11).handleEventsWith(handler12);disruptor.after(handler21).handleEventsWith(handler22);disruptor.after(handler12, handler22).handleEventsWith(handler3);
二
Disruptor的核心概念
Disruptor内部组件交互图

核心概念
Sequence
Sequencer
SequenceBarrier(序列屏障)

三
Disruptor的特点
环形数组结构
无锁化设计
独占缓存行的方式消除伪共享
什么是伪共享
Disruptor是如何解决伪共享的

预分配内存
见com.lmax.disruptor.RingBuffer.fill(EventFactoryeventFactory)

四
Disruptor在撮合引擎中的应用
数字货币交易系统的简介
背景&价值
C端核心界面


以上截图仅用于技术展示,不构成投资建议
交易系统简化交互图

撮合应用的特点
应用启动时加载数据库未处理订单、写日志、撮合成功发送消息到MQ会涉及IO操作。
正因为应用是有状态的,所以需要通过Disruptor提升单机的性能和吞吐量。
为什么撮合应用不设计成无状态的?
撮合引擎流程图

撮合引擎之Disruptor代码
定义事件:用户交易单
public class DisruptorEvent implements Serializable {private static final long serialVersionUID = -5886259612924517631L;//成交单private EntrustOrder entrustOrder;}
定义事件处理器:对用户买单和卖单进行撮合匹配
//撮合事件处理器public class ResultsHandler implements EventHandler<DisruptorEvent> {private final Set<Integer> symbolIdSet = new HashSet<>();private int workerQueueSize;public ResultsHandler(Set<Integer> symbolIdSet, int queueSize) {this.symbolIdSet.addAll(symbolIdSet);this.workerQueueSize = queueSize;}public void onEvent(DisruptorEvent disruptorEvent, long sequence, boolean endOfBatch) {try {//获取订单EntrustOrder entrustOrder = disruptorEvent.getEntrustOrder();//常规的撮合,正常撤单,异常撤单if (OperationTypeEnum.MATCH.getCode() == entrustOrder.getOperationType() ||OperationTypeEnum.CANCEL.getCode() == entrustOrder.getOperationType()) {// 取消订单需要在引擎内处理if (Objects.equals(entrustOrder.getOperationType(), OperationTypeEnum.MATCH.getCode())) {//更新为处理中OrderBook.addToOrderBook(entrustOrder.getOrderId(), MatchStatusEnum.MATCH_ING);} else if (Objects.equals(entrustOrder.getOperationType(), OperationTypeEnum.CANCEL.getCode())) {//更新为处理中if (OrderBook.getByOrderId(entrustOrder.getOrderId()) != null) {OrderBook.addToOrderBook(entrustOrder.getOrderId(), MatchStatusEnum.CANCEL_ING);}}// 执行撮合this.doMatch(entrustOrder);}} catch (Exception e) {log.error("match disruptor event handler error:{}", e.getMessage(), e);}}/*** 根据规则选择不同的撮合策略算法,进行撮合处理* @param takerOrder*/public void doMatch(EntrustOrder takerOrder) {SideEnum sideEnum = SideEnum.getSideEnum(takerOrder.getSide());OrderTypeEnum orderTypeEnum = OrderTypeEnum.getOrderTypeEnum(takerOrder.getOrderType());//选择撮合策略MatchService matchService = MatchStrategy.router(orderTypeEnum, sideEnum);MatchContext matchContext = MatchContext.getContext();matchContext.setTakerOrder(takerOrder);//执行撮合matchService.start(matchContext);//撮合完成matchService.stop(matchContext);}}
事件生产者:构建Disruptor、生产事件
/*** disruptor启动入口类,当系统读取到当前机器需要为哪些交易对提供服务的时候,* 我们需要为这些交易对进行分组服务,哪些交易对放到同一个disruptor中* 通过分组,一方面确保了活跃度高的交易对能够最大程度的利用资源,另一方面活跃度低的交易对能够有效处理,* 同时降低了cpu暴涨的风险*/4jpublic class ExchangeLauncher {private static int BUFFER_SIZE = 1024 * 16;private LimitBuyMatchService limitBuyMatchService;private LimitSellMatchService limitSellMatchService;private MarketBuyMatchService marketBuyMatchService;private MarketSellMatchService marketSellMatchService;private MatchClusterConfiguration matchClusterConfiguration;("${match.worker-queue-size:5}")private int workSize;//一个交易对对应一个disruptorprivate Map<Integer, ExchangeCore> exchangeCoreMap = new ConcurrentHashMap<>();private List<ExchangeCore> exchangeCoreList = new CopyOnWriteArrayList<>();public void start() {try {//init order bookOrderBook.init();Set<Integer> symbolIdListSet = matchClusterConfiguration.getMasterSymbolIdSet();if (CollectionUtils.isNotEmpty(symbolIdListSet)) {List<Integer> allSymbolIds = new ArrayList<>(symbolIdListSet);List<List<Integer>> pageList = ListUtils.partition(allSymbolIds, workSize);pageList.forEach(symbolIds -> {ResultsHandler handler = new ResultsHandler(new HashSet<>(symbolIds), workSize);ExchangeCore exchangeCore = new ExchangeCore(handler, BUFFER_SIZE, new NamedThreadFactory("match", false));exchangeCore.start();exchangeCoreList.add(exchangeCore);symbolIds.forEach(symbolId -> exchangeCoreMap.put(symbolId, exchangeCore));});}// 注册matchService子类registerMatchServices();} catch (Exception e) {log.error("exchangeLauncher start error:{}", e.getMessage(), e);}}private void registerMatchServices() {MatchStrategy.register(OrderTypeEnum.LIMIT, SideEnum.BUY, limitBuyMatchService);MatchStrategy.register(OrderTypeEnum.LIMIT, SideEnum.SELL, limitSellMatchService);MatchStrategy.register(OrderTypeEnum.MARKET, SideEnum.BUY, marketBuyMatchService);MatchStrategy.register(OrderTypeEnum.MARKET, SideEnum.SELL, marketSellMatchService);}}
public class ExchangeCore extends AbstractLifeCycle {private final Disruptor<DisruptorEvent> disruptor;private MatchEventPublisher publisher;private ResultsHandler eventHandler;public ExchangeCore(ResultsHandler matchHandler, int ringBufferSize, ThreadFactory threadFactory) {EventFactory eventFactory = () -> new DisruptorEvent();this.disruptor = new Disruptor<>(eventFactory, ringBufferSize, threadFactory);publisher = new MatchEventPublisher(this.disruptor);disruptor.setDefaultExceptionHandler(new DisruptorExceptionHandler());this.eventHandler = matchHandler;disruptor.handleEventsWith(eventHandler);disruptor.start();}public void start() {super.start();}public void stop() {super.stop();disruptor.shutdown();}public BaseResponse doMatch(EntrustOrder taker) {// 前置处理----startif (OrderTypeEnum.getOrderTypeEnum(taker.getOrderType()) == null || SideEnum.getSideEnum(taker.getSide()) == null) {log.error("{} - parameter error:{} or {}", taker.getTraceId(), "orderType", "side");return BaseResponse.error(TradingMatchCodeEnum.PARAMETER_ERROR);}MatchStatusEnum matchStatusEnum = OrderBook.getByOrderId(taker.getOrderId());MetricService metricService = SpringContextUtil.getBean(MetricService.class);MatchClusterConfiguration configuration = SpringContextUtil.getBean(MatchClusterConfiguration.class);// 撮合防重校验,并发存在问题。但是消费的时候,是单线程,做了校验,不存在重复撮合的问题。if (OperationTypeEnum.MATCH.getCode() == taker.getOperationType()) {if (matchStatusEnum != null) {//短时间内重复撮合log.error("{} - match repeat ,orderId :{}", taker.getTraceId(), taker.getOrderId());return BaseResponse.error(TradingMatchCodeEnum.REPEAT_REQUEST);}//构造对象进入等待队列OrderBook.addToOrderBook(taker.getOrderId(), MatchStatusEnum.WAIT_ING);metricService.count(MetricNames.ORDER_TYPE_NUM, "type", "match", "group", configuration.getClusterName());} else if (OperationTypeEnum.CANCEL.getCode() == taker.getOperationType()) {int cancelType = taker.getCancelType();/**异常单-产生情况:收单服务 调用撮合 出现异常,不知道成功没,没有明确响应 开始进行异常撤单*/if (CancelTypeEnum.NORMAL_CANCEL.getCode() == cancelType) {if (matchStatusEnum == null) {// 数据有可能在请求队列中被逐出,需要继续走逻辑//} else {if (MatchStatusEnum.MATCH_END == matchStatusEnum) {//重复撤销,深度盘已经没有数据,没必要继续往下,不走disruptor 和撮合直接返回log.error("{} - cancel failed, match end ,orderId :{}", taker.getTraceId(), taker.getOrderId());return BaseResponse.error(TradingMatchCodeEnum.REPEAT_REQUEST);}OrderBook.addToOrderBook(taker.getOrderId(), MatchStatusEnum.WAIT_CANCEL);}} else {// reload异常撤单,要加入内存OrderBook.addToOrderBook(taker.getOrderId(), MatchStatusEnum.WAIT_CANCEL);}} else {log.warn("--------can not find the operationType[{}]", taker.getOperationType());throw new TradingMatchException("can not find the operationType[" + taker.getOperationType() + "]");}// 前置处理----end//Disruptor开始发布事件publisher.publish(taker);return BaseResponse.success();}public Disruptor<DisruptorEvent> getDisruptor() {return disruptor;}}
public class MatchEventPublisher {private Disruptor<DisruptorEvent> disruptor;public MatchEventPublisher(Disruptor<DisruptorEvent> disruptor) {this.disruptor = disruptor;}private static final EventTranslatorOneArg<DisruptorEvent, EntrustOrder> TRANSLATOR =(event, sequence, entrustOrder) -> {event.setEntrustOrder(entrustOrder);};public void publish(EntrustOrder taker) {RingBuffer<DisruptorEvent> ringBuffer = disruptor.getRingBuffer();taker.setSequence(ringBuffer.getCursor());taker.setArriveTime(System.currentTimeMillis());ringBuffer.publishEvent(TRANSLATOR, taker);// ...}}
五
总结
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70027824/viewspace-3021791/ ,如需转载,请注明出处,否则将追究法律责任。