Message Replication in ElasticMQ with JGroups
Join the DZone community and get the full member experience.Join For Free
elasticmq is a messaging server, with a scala, java, and an amazon sqs-compatible interface. it supports guaranteed messaging by replicating the messages across a cluster of servers, and message persistence with journalling.
message replication is one of the core features of elasticmq. however if you look at the code, it’s only a handful of classes, the longest having 76 lines (remember that this is scala, though ;) ). that is because elasticmq uses jgroups as the underlying communication library. jgroups is quite old, especially for a java library – the first release was in 1999 (!). but it’s quite far from being outdated and obsolete – it has a nice api, works without problems, has a good community; and as any java library cooperates well with scala.
jgroups has a lot of useful features:
- reliable multicast
- cluster management
- failure detection
- node discovery
many years of performance improvements
which are extensively used for implementing replication in elasticmq. below is a summary of how it’s done.
how does an elasticmq cluster work?
in a single elasticmq cluster one node is always the master. you can only execute operations against this node. the results of each operation are then replicated to the other members. there are a couple of options related to blocking; the replication can be fully asynchronous, or it can wait until at least one or all nodes ack the operation. to make sure that in case of cluster partitions the same messages aren’t received from different partitions, only the partition which has at least half+1 of the nodes operations is active.
the central concept in elasticmq is the message storage. a storage executes commands (such as a send message command, delete message command etc.). the replication layer is just a wrapper around any other storage. note though, that we can only replicate the resulting storage mutations (so after the command is executed), not the original command itself. for example, if the command is “receive a message”, the results of executing it may be different on each machine. hence we replicate only the change of the message visibility (in elasticmq, similarly to amazon sqs , if a message is received it is blocked from a subsequent receive for a specified period of time), if receiving a message succeedes. you can see this basic logic in jgroupsreplicatedstorage .
initializing the cluster
before we get to the replication itself though, the first thing to do is to initialize the cluster; that is done in replicatedstorageconfigurator . as a parameter there, we need a jgroups configuration file, which is a stack of protocols. you don’t really need to know what each of these protocols do and what all those configuration parameters mean. the two most useful are udp.xml and tcp.xml . the first one should be used if you have multicast in your network, the second – if all communication should go through tcp (e.g. on ec2). in the latter case you will also need to provide the list of initial ips. the list doesn’t have to be exhaustive, just a list of seeds.
having a protocol stack, elasticmq creates a jchannel, and connects it, which simply means connecting to the cluster. and that’s in fact all you need to do to create a cluster with jgroups – pretty simple, right? as you can see at the end of replicatedstorageconfigurator , the first thing after connecting is a call to channel.getstate(null, 0) . this will go to the current master node (more on master election later), fetch the state (current queues and messages) and apply it on the current node (see the very simple jgroupsstatetransfermessagelistener – handles both sending and receiving). there are two important things to note here. first is that this transfer doesn’t block the whole cluster from normal operation. second is that if an operation is executed during the state transfer, it will also be replicated. so it may happen that one command is executed twice on the new node. that doesn’t matter though, as each replicated command is idempotent, so may be applied many times. in other scenarios some application-side mechanism would have to be implemented to prevent such situations.
finally we get to the core: replicating the commands. on the sender side, this is handled by jgroupsreplicationmessagesender . again, not a very complicated class. it uses the messagedispatcher “building block” from jgroups which, apart from multicasing messages across the cluster, enables you to wait until a specified number of nodes receive it. on the receiving side, we have jgroupsrequesthandler . again, pretty simple. when a message is received it is simply sent to the storage.
you may have also noticed the setmaster special message. this is needed for the user to be able to read the current master’s node address. master election (deciding which node is the master) is handled entirely with jgroups. there isn’t a specific algorithm in jgroups to elect a master, however we can use the fact that each node has the same cluster view, which is represented by the jgroups view class. all we need to do is simply get the first (or last, or 3rd, etc. – anything as long as it’s the same on all nodes) element from that list, and set it as the master.
cluster view is handled by the last “core” replication class, jgroupsmembershiplistener . two things happen there. the viewaccepted callback is called whenever a new node joins or leaves the cluster; each node with the same (well, equal :) ) instance of the view class. the master broadcasts its address (which is the elasticmq server address, not the internal jgroups cluster-communication address) in a separate thread . it’s a very easy mistake to perform a blocking operation in one of the jgroups callback methods. you should never do that, as the whole stack can then lock. we also need the flush protocol (which is always added during the cluster setup); this protocol makes sure that no new messages are sent when before the new view has been installed by all nodes, and hence we make sure that a new node always receives the master information.
the membership listener also handles cluster merges. again, jgroups gives us a view of the partitions that were merged and the new combined view. in elasticmq, all partitions except the primary parition (which is the largest one) request a state transfer, just as after connecting to the cluster. that way the data is kept in a consistent state.
it’s also worth noting that elasticmq’s replication is fully tested using scalatest. each test creates a cluster of in-memory storages, creating new nodes or simulating node crashes. see the jgroupsreplicatedstoragetest class.
having the mechanisms from jgroups it is pretty straightforward to implement cluster communication. as always, though, you need to remember about some traps when it comes to concurrency (e.g. that there may be cluster activity when a new node joins; that partitions and merges can happen anytime; that there’s no ordering between normal messages and cluster view changes; that messages may be sent during state transfer; etc.). however both the jgroups tutorial and manual is pretty comprehensive and given the additional help from the forums (thanks bela!), you should be good to go.
Published at DZone with permission of Adam Warski, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.