开发者

Ordering of Mnesia events

We’re developing an application where multiple processes on different nodes in a distributed system subscribe to mnesia events. The table is written to from one single process on one of the nodes.

However uncertainty has arose about if we can be sure to receive the events in the same order as operations on the table.

E.g:

mnesia:delete(tab1, SomeRe开发者_JAVA百科c),
mnesia:write(tab1, SomeOtherRec)

If we sometimes get the delete event after the write event our design would not work and we would have to create some other kind of notification mechanism.

Also, how about operations on different tables (from the same process)?

mnesia:write(tab1, SomeRec),
mnesia:write(tab2, SomeOtherRec)

Can we be sure to always get the event from tab1 before the one from tab2? On all processes and all nodes?


For Erlang as a whole, the sending of messages from a process A to a process B is guaranteed to always be in order.

However, for messages between more than two processes, you can not guarantee that messages sent from A to B will arrive before messages sent from C to B, even if A's messages were globally sent first. The scheduler, network latency or network problems (especially if A and C aren't on the same node) might be good examples of why such guarantees are hard to give.

If all your events are sent from the same process, ordering can be a sure thing. Otherwise, you can't trust the events' order.

As for mnesia events, They're all managed in mnesia_subscr.erl, which is a single gen_server taking charge of forwarding all events for a node, no matter the table. This should thus adhere to the A to B principle and guarantee ordered events.


I don't know whether mnesia will do what you want by default, but assuming that it doesn't then perhaps you need to start looking at the use of distributed consensus algorithms such as Paxos? There is an implementation in the form of lib_paxos, a GPL licensed open source library.

Needless to say, this will impact your performance but ensure consistency.


Paxos is the solution you are looking for. Make the selection of accepted values is a Max of earlier accepted proposals. This will create a sequence you can use to order your instructions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜