JVM heap replication between two machines

2020-05-19 04:42发布

问题:

What are the basic principles of how two separable computers connected within the same network running the same Java application maintain the same state by syncing their heap between each other?

I believe Terracotta does this task but I have no idea how would some pseudo code look like that would describe its core functions.

I'm just looking for understanding of this technology.

回答1:

Terracotta DSO works by manipulating the byte code of your classes (and the JDK's classes etc). The instructions on how and when to do this are part of the Terracotta configuration file.

The bytecode modification looks for certain byte codes such as a field read or write or a monitor enter or exit. Whenever those instructions occur, code is added around that location that does the appropriate action in the distributed store. For example when a monitor is obtained due to synchronization, a distributed lock is obtained as well (whether it is a read or write lock is dependent on the configuration). If a field in a shared object is written, the distributed system must verify that a write lock is being held and then send the data value is sent to the clustered server, which stores it on disk or shares it over the network as appropriate.

Note that Terracotta does not share the entire heap, only the graph of objects indicated by the configuration. In general, there would be little point in sharing an entire heap. It is better instead for the application to describe the domain objects needed across the distributed application.

There are many optimizations employed to make the operations above efficient: only field deltas are sent over the wire and in a form much more efficient than Java serialization, many deltas can be bundled and sent in batches, locks are actually "checked out" to a particular client so that if the application data is partitioned across clients, most distributed locks are actually a local operation not involving a network call, etc.



回答2:

Terracotta can indeed handle that if you tell it to - see the description of its DSO - Distributed Shared Objects.

It sounds cool but I would prefer something like EHcache (can be backed by Terracotta again) which functions on a bit more high level.



回答3:

One emerging technology that somehow tackles this problem is Distributed Software Transactional Memory. You get strong data consistency guarantees (i.e. 1-copy serializability) and a powerful concurrency control mechanism: transactions.

AFAIK, there is no mature solution out there, but it is promising.



回答4:

I would recommend that you investigate http://www.jboss.org/infinispan and see if it will fulfill your needs.