Question on LMAX unmarshaler

754 views
Skip to first unread message

Min Zhou

unread,
Feb 2, 2012, 4:14:01 AM2/2/12
to Disruptor
Hi all,

From Martin's post, it refer that
" The unmarshaler turns the event data from the wire into a java
object that can be used to invoke behavior on the Business Logic
Processor. Therefore, unlike the other consumers, it needs to modify
the data in the ring buffer so it can store this unmarshaled object.
The rule here is that consumers are permitted to write to the ring
buffer, but each writable field can only have one parallel consumer
that's allowed to write to it. This preserves the principle of only
having a single writer."

Unmarshaler is the other writer of that ring buffer except the
receiver(event publisher). In order to accord the "single-write
principle", the unmarshaler should write to another field of the
original Event. For example, if we define the event type like below

public class DisruptorMessageEvent {

private Object field1;
private Object field2
public void setField1(Object o) {
this.field1 = o;
}

public void setField2(Object o) {
this.field2 = o;
}

public final static EventFactory<DisruptorMessageEvent>
EVENT_FACTORY =
new EventFactory<DisruptorMessageEvent>() {
public DisruptorMessageEvent newInstance() {
return new DisruptorMessageEvent();
}
};

and field1 is set by an EventPubliser.
long sequence = ringBuffer.next();
ringBuffer.get(sequence).setField1(object1);
ringBuffer.publish(sequence);

How to modify that event from the unmarshaler? Is there any similar
example on the web or in the source code of disruptor?

Thanks,
Min

Dave Farley

unread,
Feb 2, 2012, 1:36:05 PM2/2/12
to lmax-di...@googlegroups.com
Your example here is pretty much it.

In our (LMAX's) case the producer writes the incoming bytes that represent a raw event into one field in a "message" object stored in the ring buffer. The unmarshalling event handler reads these bytes and translates them into a more usable form for the business logic, which it writes back to another, different, field (or fields) in the same "message" object.

When the marshaller, and other event handlers are finished, the business-logic event handler will call the appropriate method with the arguments. So the single-writer principle is preserved.

Dave Farley

Min Zhou

unread,
Feb 2, 2012, 9:19:49 PM2/2/12
to lmax-di...@googlegroups.com
Hi Dave, 

Thank you for your reply. Actually, network unmarshalling is more complex than what we discussed here,  where  the packets of a stream-based transport unfortunately can be fragmented and reassembled during transmission even in a LAN environment.For example, let us assume that the TCP/IP stack of an operating system has received three packets:
 +-----+-----+-----+
 | ABC | DEF | GHI |
 +-----+-----+-----+
Because of the packet fragmentation, a server can receive them like the following:

 +----+-------+---+---+
 | AB | CDEFG | H | I |
 +----+-------+---+---+

Therefore, the number of bytes buffers published by the producer is very likely  not equal than the number of the logical frames which could be easily understood by the subsequent business logic.  So just setting different field(s) in the same event seems not helps here. 

How do you deal with such issue?

Thanks,
Min
--
My research interests are distributed systems, parallel computing and bytecode based virtual machine.

My profile:
http://www.linkedin.com/in/coderplay
My blog:
http://coderplay.javaeye.com

Michael Barker

unread,
Feb 3, 2012, 4:10:30 PM2/3/12
to lmax-di...@googlegroups.com
Hi Min,

The flippant answer: "Use a messaging bus". We use Ultra Messaging
from Informatica, I've also heard good things about ØMQ. We let the
messaging bus deal with ordering and fragmentation. We add our own
reliability and late join support using the disruptor. The basic
premise is to sequence all of the packets coming across the network
and re-request any that are missed. We use the ring buffer as a store
for messages on the publishing side. If we miss a message we can
rewind the event handler to the missed message and redeliver.

Mike.

Adnan Selimovic

unread,
Feb 8, 2012, 4:23:33 AM2/8/12
to lmax-di...@googlegroups.com
Hi Dave,

How are you creating internal business objects in the unmarshaller? Are you using some kind of object pool or always creating new objects for every event?
In case of the pool how are you dealing with the concurrency problems and in case of the new objects how does the GC pressure looks like?

Thanks,
Adnan. 

Michael Barker

unread,
Feb 8, 2012, 5:27:53 AM2/8/12
to lmax-di...@googlegroups.com
> How are you creating internal business objects in the unmarshaller? Are you
> using some kind of object pool or always creating new objects for every
> event?

We create a new object for each event. Object pooling is a technique
that I'd only apply with the utmost care. It can create more problems
that it fixes. Note that the information in Martin Fowler's article
is now a bit out of date. We un-marshal on the same thread as the
business logic. It's simpler and has very little performance impact.
It also gives the JVM a chance to apply escape analysis and
potentially allocate the business event on the stack. After a couple
of conversations with experts in the VM area it was suggested that
this is friendlier to the GC than allocating an object on one thread
passing it to another and nulling it out quickly afterward.

However, I don't consider our solution to be the most optimal. There
is a zero garbage approach that I demonstrated at Devoxx this year.
I'm going to put up some example code as soon as I get it packaged
cleanly.

Mike.

Adnan Selimovic

unread,
Feb 8, 2012, 6:44:15 AM2/8/12
to lmax-di...@googlegroups.com
Hi Mike,

Thank for the answer.
Is there any change that I could get your slides from the Devoxx presentation?

Thanks,
Adnan.

Michael Barker

unread,
Feb 10, 2012, 3:05:32 AM2/10/12
to lmax-di...@googlegroups.com
> Is there any change that I could get your slides from the Devoxx
> presentation?

No problem, I've posted my most recent slides from Devoxx and JAX on my blog:

http://bad-concurrency.blogspot.com/2012/02/slides-from-recent-presentations.html

Paul

unread,
Feb 16, 2012, 5:22:41 AM2/16/12
to Disruptor
Mike,

How do you rewind the Event Processor?

Thanks, Paul
> > On Fri, Feb 3, 2012 at 2:36 AM, Dave Farley <davefarle...@gmail.com> wrote:
>
> >> Your example here is pretty much it.
>
> >> In our (LMAX's) case the producer writes the incoming bytes that represent
> >> a raw event into one field in a "message" object stored in the ring buffer.
> >> The unmarshalling event handler reads these bytes and translates them into a
> >> more usable form for the business logic, which it writes back to another,
> >> different, field (or fields) in the same "message" object.
>
> >> When the marshaller, and other event handlers are finished, the
> >> business-logic event handler will call the appropriate method with the
> >> arguments. So the single-writer principle is preserved.
>
> >> Dave Farley
>

Michael Barker

unread,
Feb 16, 2012, 6:33:22 AM2/16/12
to lmax-di...@googlegroups.com
We have a custom event processor that can respond to a rewind request
and set it's sequence to a lower value (within a specified threshold).

Mike.

Andrei

unread,
Feb 22, 2012, 11:12:34 AM2/22/12
to Disruptor
Hi Mike,

It's an interesting approach. I presume you're using UDP ?

Can you please give more details about the messaging layer and how the
services are communicating between each other (pub/sub request/
reply) ? For instance, rewind/redeliver is it a separate request to
upstream feed ?

Thanks,
Andrei.
> > On Fri, Feb 3, 2012 at 2:36 AM, Dave Farley <davefarle...@gmail.com> wrote:
>
> >> Your example here is pretty much it.
>
> >> In our (LMAX's) case the producer writes the incoming bytes that represent
> >> a raw event into one field in a "message" object stored in the ring buffer.
> >> The unmarshalling event handler reads these bytes and translates them into a
> >> more usable form for the business logic, which it writes back to another,
> >> different, field (or fields) in the same "message" object.
>
> >> When the marshaller, and other event handlers are finished, the
> >> business-logic event handler will call the appropriate method with the
> >> arguments. So the single-writer principle is preserved.
>
> >> Dave Farley
>

Michael Barker

unread,
Feb 25, 2012, 8:58:46 AM2/25/12
to lmax-di...@googlegroups.com
> Can you please give more details about the messaging layer and how the
> services are communicating  between each other (pub/sub request/
> reply) ? For instance, rewind/redeliver is it a separate request to
> upstream feed ?

Our messaging is topic based, i.e. pub/sub. Topics that we have
designated as reliable, will have a second associated topic, suffixed
with the string "_nak". A separate listener to the nak topic will
perform the rewind activity as required.

Mike.

Daniel Cegiełka

unread,
Feb 25, 2012, 9:32:27 AM2/25/12
to lmax-di...@googlegroups.com
Quite well described.



best regards,
Daniel



2012/2/25 Michael Barker <mik...@gmail.com>

Andrei

unread,
Feb 29, 2012, 11:32:09 PM2/29/12
to lmax-di...@googlegroups.com
Thanks for reply., Mike.

Do you keep (queue) messages in the same java process or delegate to a 3rd party (middleware) ?

Andrei

unread,
Feb 29, 2012, 11:44:08 PM2/29/12
to lmax-di...@googlegroups.com
Hi Daniel,

Thanks for informative article. Do you think zeroMQ is an overkill in the context of socket to socket connections when no reliable delivery is required ? I'm thinking  how would it compare to netty/MINA frameworks (we don't have more than 50 subscribers per process).

Regards.


On Saturday, February 25, 2012 9:32:27 AM UTC-5, danice wrote:
2012/2/25 Michael Barker
> Can you please give more details about the messaging layer and how the

Michael Barker

unread,
Mar 1, 2012, 4:28:35 PM3/1/12
to lmax-di...@googlegroups.com
Thanks for reply., Mike.

Do you keep (queue) messages in the same java process or delegate to a 3rd party (middleware) ?

The same process.  Informatica UME and ZeroMQ both use a nothing in a middle architecture, so there is no separate message broker process. 

Mike.

Dmitry Shilovich

unread,
Dec 7, 2015, 4:02:14 PM12/7/15
to Disruptor
Hi Mike. 
Is there any example of such event processor?

I watched this presentation by Sam Adams on the LMAX architecture. On 58:30 he talked about NAK. Can you pls provide more information how you handle this NAK(rewind) request?
Thank you.
Reply all
Reply to author
Forward
0 new messages