Jan
Jan

Reputation: 2833

How to stop and shutdown an entire hazelcast cluster?

How do you stop and shutdown a hazelcast cluster? My observation from testing is that whenever a node ist stopped by HazelcastInstance#shutdown() the cluster tries to re-balance or backup the data. How can I first "stop" the cluster and then shut it down? (Or is my observation wrong?)

Upvotes: 5

Views: 6334

Answers (4)

T. Gawęda
T. Gawęda

Reputation: 16076

It's never too late for an answer :)

If you want to not have migration events fired during shutdown, you can change cluster state to PASSIVE:

hazelcastInstance.getCluster().changeClusterState(PASSIVE);

I recommend doing this inside such if:

if (partitionService.isClusterSafe()) {
    hazelcastInstance.getCluster().changeClusterState(PASSIVE);
    hazelcastInstance.getCluster().shutdown(); 
    // or send exit command to each node, as in mrck answer - remember that then action must be an AllowedDuringPassiveState
}

In Passive and Frozen state, Hazelcast won't do migration and - because of that - you won't have errors when shutting down cluster. If block is helpful to be sure, that changing state and shutdown is safe to do to prevent data loss

Upvotes: 1

mrck
mrck

Reputation: 328

You can use isClusterSafe as in below example :

public class ShutdownCluster {

public static void main(String[] args) throws Exception {

    HazelcastInstance member1 = Hazelcast.newHazelcastInstance();
    HazelcastInstance member2 = Hazelcast.newHazelcastInstance();
    HazelcastInstance member3 = Hazelcast.newHazelcastInstance();

    if(member1.getPartitionService().isClusterSafe()) {
        IExecutorService executorService = member1.getExecutorService(ShutdownCluster.class.getName());
        executorService.executeOnAllMembers(new ShutdownMember());
    }
}

private static class ShutdownMember implements Runnable, HazelcastInstanceAware, Serializable {

    private HazelcastInstance node;

    @Override
    public void run() {
        node.getLifecycleService().shutdown();
    }

    @Override
    public void setHazelcastInstance(HazelcastInstance node) {
        this.node = node;
    }
}
}

Upvotes: 4

Ashish Ratan
Ashish Ratan

Reputation: 2870

Try this.

HazelcastInstance.getLifecycleService().shutdown();

Upvotes: 0

pveentjer
pveentjer

Reputation: 11307

When testing I often use a Hazelcast.shutdownAll()

This will kill all the instances.

Upvotes: 0

Related Questions