Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hazelcast: Merge two hazelcast instances

Tags:

java

hazelcast

Lets say we have two instances of hazelcast:

HazelcastInstance firstInstance = Hazelcast.newHazelcastInstance(new Config());
HazelcastInstance secondInstance = Hazelcast.newHazelcastInstance(new Config());
// Add entries to firstInstance
// Add entries to secondInstance

Now I am trying to delete everything from firstInstance and then add everything from secondInstance to firstInstance.

Is there a way to achieve this?

like image 925
RDM Avatar asked Mar 02 '16 07:03

RDM


People also ask

How to create multiple nodes in Hazelcast cluster?

To create multiple nodes we can start the multiple instances of ServerNode application. As a result, Hazelcast will automatically create and add a new member to the cluster. For example, if we run the ServerNode application again, we'll see the following log in the console which says that there are two members in the cluster.

What is the best use case for Hazelcast?

Given that Hazelcast is a distributed IMDG and typically is set up on multiple machines, it requires access to the internal/external network. The most important use-case being discovery of Hazelcast nodes within a cluster. 1 inbound port to receive pings/data from other Hazelcast nodes/clients

How many ports does Hazelcast try to bind?

By default, Hazelcast will try 100 ports to bind. In the example above, if we set the value of port as 5701 and limit the port count to 20, as members are joining to the cluster, Hazelcast tries to find ports between 5701 and 5721.

How do Hazelcast members join together?

Create a Hazelcast Member Members (also called nodes) automatically join together to form a cluster. This automatic joining takes place with various discovery mechanisms that the members use to find each other. Let's create a member that stores data in a Hazelcast distributed map:


1 Answers

First, as per that initialization of the two instances shown in your code, both are cluster members belonging to the same cluster group and, given the default configuration, they will both contain all of the shared data. In other words, you wouldn't need to 'transfer' information.

If the above is true, by the time you're done deleting from the first instance, you won't have any copy of the data (other than their respective source).

If, however, the instances are initialized with configurations tying them to different cluster groups (remember, your code in the question isn't doing so), it's easy enough to just 'copy' using the Java Map/Collections API (which Hazelcast shared data structure types implement):

secondInstance.getMap("myMap").putAll(firstInstance.getMap("myMap"));
firstInstance.getMap("myMap").clear(); //please confirm this.

Distributed lists can be treated in a similar manner. Also, be careful with such 'bulk copies' as your member can hit an out of memory error (of course, that depends on the size of your data).

More about this can be read here: http://docs.hazelcast.org/docs/3.6/manual/html-single/index.html#preventing-out-of-memory-exceptions

like image 177
ernest_k Avatar answered Oct 27 '22 18:10

ernest_k