Hazelcast: Merge two hazelcast instances
Asked Answered
W

1

6

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?

Warp answered 2/3, 2016 at 7:41 Comment(0)
H
6

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

Hydrosol answered 2/3, 2016 at 7:55 Comment(4)
Hi Ernest, thanks for detailed explanation. I am initializing the firstInstance from spring config and the other using the newHazelcastInstance(). The idea is to get the updated data and save it in the secondInstace whilst the firstInstance is being used by other services. Once the secondInstance is updated, I wanted to remove all data from the firstInstace and load it from the secondInstance.Warp
But what I understood from your comment is that both these instances are tied up and any data addition/removal operation on any instance will be mirrored on the other instance. If that is the case then how do I achieve this scenario (given that both these instances will be from the same cluster group) ?Warp
@Rahul: If both members are part of the same cluster group and have discovered each other, then hazelcast's replication (unless you've customized it) will cause the nodes to hold copies of all the data, without any manual copy in your code. If spring-based member dies, then second node will still have the data. However, if you 'remove' map entries, then the data will be removed from the cluster (neither of the members will have it afterwards). Key point is that both members belong to the same cluster and share the replicated data, in which case you don't need to copy.Hydrosol
If your intent is to rather make only one of these members keep the data, then you can consider starting the spring-based node in client mode (of course, you'd then have to start the member from your java code first). If not, then you don't need to manually transfer data. You can read more about hazelcast's clustering here: docs.hazelcast.org/docs/3.6/manual/html-single/…Hydrosol

© 2022 - 2024 — McMap. All rights reserved.