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?
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.
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
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.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With