本文介绍了LMAX 的颠覆者模式是如何运作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解破坏者模式.我看过 InfoQ 的视频并尝试阅读他们的论文.我知道涉及到一个环形缓冲区,它被初始化为一个非常大的数组,以利用缓存局部性,消除新内存的分配.

I am trying to understand the disruptor pattern. I have watched the InfoQ video and tried to read their paper. I understand there is a ring buffer involved, that it is initialized as an extremely large array to take advantage of cache locality, eliminate allocation of new memory.

听起来好像有一个或多个原子整数可以跟踪位置.每个事件"似乎都有一个唯一的 id,它在环中的位置是通过找到它与环大小等相关的模数来找到的.

It sounds like there are one or more atomic integers which keep track of positions. Each 'event' seems to get a unique id and it's position in the ring is found by finding its modulus with respect to the size of the ring, etc., etc.

不幸的是,我对它的工作原理没有直观的认识.我做过很多交易应用,研究过actor模型,看过SEDA等

Unfortunately, I don't have an intuitive sense of how it works. I have done many trading applications and studied the actor model, looked at SEDA, etc.

在他们的介绍中,他们提到这种模式基本上就是路由器的工作方式;但是我也没有找到关于路由器如何工作的任何好的描述.

In their presentation they mentioned that this pattern is basically how routers work; however I haven't found any good descriptions of how routers work either.

是否有更好的解释?

推荐答案

Google Code 项目参考一篇关于环形缓冲区实现的技术论文,但是对于想要了解其工作原理的人来说,这有点枯燥、学术和艰难.然而,有一些博客文章已经开始以更易读的方式解释内部结构.有一个对环形缓冲区的解释,即破坏者模式的核心,一个描述消费者障碍(与阅读破坏者相关的部分)和一些有关处理多个生产者的信息 可用.

The Google Code project does reference a technical paper on the implementation of the ring buffer, however it is a bit dry, academic and tough going for someone wanting to learn how it works. However there are some blog posts that have started to explain the internals in a more readable way. There is an explanation of ring buffer that is the core of the disruptor pattern, a description of the consumer barriers (the part related to reading from the disruptor) and some information on handling multiple producers available.

Disruptor 最简单的描述是:它是一种以尽可能最有效的方式在线程之间发送消息的方式.它可以用作队列的替代品,但它也与 SEDA 和 Actors 共享许多功能.

The simplest description of the Disruptor is: It is a way of sending messages between threads in the most efficient manner possible. It can be used as an alternative to a queue, but it also shares a number of features with SEDA and Actors.

与队列相比:

Disruptor 提供了将消息传递到另一个线程的能力,并在需要时唤醒它(类似于 BlockingQueue).但是,有 3 个明显的区别.

The Disruptor provides the ability to pass a message onto another threads, waking it up if required (similar to a BlockingQueue). However, there are 3 distinct differences.

  1. Disruptor 的用户通过扩展 Entry 类并提供工厂来进行预分配来定义消息的存储方式.这允许内存重用(复制)或条目可以包含对另一个对象的引用.
  2. 将消息放入 Disruptor 是一个 2 阶段的过程,首先在环形缓冲区中声明一个时隙,它为用户提供可以填充适当数据的条目.然后必须提交条目,这种两阶段方法是必要的,以允许上述内存的灵活使用.正是提交使消息对消费者线程可见.
  3. 消费者有责任跟踪已从环形缓冲区中消费的消息.将这一职责从环形缓冲区本身移开有助于减少写入争用的数量,因为每个线程都维护自己的计数器.

与演员相比

Actor 模型比大多数其他编程模型更接近 Disruptor,尤其是当您使用提供的 BatchConsumer/BatchHandler 类时.这些类隐藏了维护消耗序列号的所有复杂性,并在发生重要事件时提供了一组简单的回调.但是,有一些细微的差异.

The Actor model is closer the Disruptor than most other programming models, especially if you use the BatchConsumer/BatchHandler classes that are provided. These classes hide all of the complexities of maintaining the consumed sequence numbers and provide a set of simple callbacks when important events occur. However, there are a couple of subtle differences.

  1. Disruptor 使用 1 线程 - 1 消费者模型,其中 Actor 使用 N:M 模型,即您可以拥有任意数量的 Actor,并且它们将分布在固定数量的线程上(通常每个内核 1 个).
  2. BatchHandler 接口提供了一个额外的(也是非常重要的)回调 onEndOfBatch().这允许缓慢的消费者,例如那些执行 I/O 以将事件批处理在一起以提高吞吐量的人.可以在其他 Actor 框架中进行批处理,但是由于几乎所有其他框架都没有在批处理结束时提供回调,因此您需要使用超时来确定批处理结束,从而导致延迟不佳.
  1. The Disruptor uses a 1 thread - 1 consumer model, where Actors use an N:M model i.e. you can have as many actors as you like and they will be distributed across a fixed numbers of threads (generally 1 per core).
  2. The BatchHandler interface provides an additional (and very important) callback onEndOfBatch(). This allows for slow consumers, e.g. those doing I/O to batch events together to improve throughput. It is possible to do batching in other Actor frameworks, however as nearly all other frameworks don't provide a callback at the end of the batch you need to use a timeout to determine the end of the batch, resulting in poor latency.

与 SEDA 相比

LMAX 构建了 Disruptor 模式来取代基于 SEDA 的方法.

LMAX built the Disruptor pattern to replace a SEDA based approach.

  1. 与 SEDA 相比,它提供的主要改进是并行工作的能力.为此,Disruptor 支持向多个消费者多播相同的消息(以相同的顺序).这避免了在管道中分叉阶段的需要.
  2. 我们还允许消费者等待其他消费者的结果,而无需在他们之间放置另一个排队阶段.消费者可以简单地观察它所依赖的消费者的序列号.这避免了在管道中加入阶段的需要.

与内存障碍相比

另一种思考方式是将其视为结构化、有序的内存屏障.其中生产者屏障形成写屏障,消费者屏障是读屏障.

Another way to think about it is as a structured, ordered memory barrier. Where the producer barrier forms the write barrier and the consumer barrier is the read barrier.

这篇关于LMAX 的颠覆者模式是如何运作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 21:43
查看更多