Replication in Mammatus
Sunday, December 14th, 2008I have created a way of replicating state which is similar to MySQL.
We have several cases where we want to update a Btree on a central server and then having it replicated across all slave nodes.
Today we serialize a HashMap to disk, rsyncs it and when the slaves understands that the underlying file is changed it initializes itself with that. This works, however it is not a smart way of doing it since it needs to reload the entire state even though just one entry has been added. To solve that you need to add transaction logging and replicate those transactions.
So how does it work ?
* TransactionLogger needs to be initialized on both master and slave.
* You write to the master file.
* The slave polls the master and sends it’s latest sequence number (trx id) called X.
* The master sends the delta entries from X to Y where Y is the latest entry noted on the master when the client initiated the request.
I wrote the transaction loggers as separate modules so you need to wire them up to make the storage synchronized.
On the slave you need a StateChangeListener and on the master you need to wrap the storage engine in a TransactionLoggerCacheStrategy.
Here is a fully working example spring context file.
Example code:
public static void main(String[] args)
{
String[] cfg = {”logManager.xml”};
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(cfg);
Cache cacheMaster = (Cache)ctx.getBean(”masterCache”);
Cache cacheSlave = (Cache)ctx.getBean(”slaveCache”);
cacheMaster.put(”testing”, new Date());
while(true)
{
Date date = (Date)cacheSlave.get(”testing”);
if(date != null)
{
System.out.println(”Huzza!”);
System.exit(0);
}
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}





