Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support re-start of HazelcastClient (after HazelcastInstanceNotActiveException) #4363

Closed
mzapletal opened this issue Dec 24, 2014 · 7 comments
Assignees
Labels
Milestone

Comments

@mzapletal
Copy link

Currently, clients may run into HazelcastInstanceNotActiveException and are shutting down in cases where this is not desired (e.g., reboot of the cluster, changing IP addresses ( #4349),...).
In such cases, it may be desirable to get the client from a shutdown state back to a connecting state.

While it is easy in plain Java applications to just re-create the Hazelcast client, this gets almost impossible (or needs a lot of workarounds) when working with dependency injection. For example, when working with Spring, one creates a HazelcastInstance (shielding a HazelcastClientProxy) in the container during startup of the application. When the HazelcastInstance bean then gets into the shutdown state it is virtually destroyed (even it would be possible to reconnect later on). As a consequence, the whole application is not able to recover from this error without a restart.

To overcome this issue, I would propose to support the restart of HazelcastClientProxy instances.

@gurbuzali
Copy link
Contributor

an hackish way to restart client:

public static void restartHazelcastClient(HazelcastInstance clientInstance) {
        clientInstance.shutdown();
        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(HazelcastClient.class.getClassLoader());
            HazelcastClientProxy proxy =  ((HazelcastClientProxy) clientInstance);
            ClientConfig clientConfig = proxy.client.getClientConfig();
            HazelcastClientInstanceImpl client = new HazelcastClientInstanceImpl(clientConfig);
            client.start();
            OutOfMemoryErrorDispatcher.registerClient(client);
            proxy.client = client;
        } finally {
            Thread.currentThread().setContextClassLoader(tccl);
        }
    }

@mzapletal
Copy link
Author

Ali, many thanks for the quick response and the provided code. We will try it out in our integration tests.

Any chance that such functionality will make it into the client?

@bwzhang2011
Copy link

@gurbuzali, is it the same way to restart the hazelcast itself. as one we shutdown the hazelcast server(instance) we should find way to restart that.

@bwzhang2011
Copy link

@gurbuzali, would you mind leaving more message for that ? If I were wrong to shutdown the hazelcastInstance from any kind of way. e.g from the press button of hazelcast mancenter, how could I restart such as I could catch the hazelcastInstanceNotActiveException and I could get the original configuration of hazelcast. how could I restart that or replace the bean in spring(if I use hazelcast-spring to create hazelcast instance). If you have any free time, would you mind leaving us more detail on how to restart hazelcast. for example, for the config hacked way to get from hazelcastInstanceProxy like this:
public static Config getHazelcastBoundConfig(HazelcastInstance hzInstance) {
if (hzInstance instanceof HazelcastInstanceProxy) {
HazelcastInstanceProxy hzInstanceProxy = (HazelcastInstanceProxy) hzInstance;

        Object originalHzInstance = BeanOperateHelper.getBeanDeclaredPropertySilent(hzInstanceProxy, "original");

        if (originalHzInstance instanceof HazelcastInstanceImpl) {
            HazelcastInstanceImpl hzInstanceImpl = (HazelcastInstanceImpl) originalHzInstance;

            return hzInstanceImpl.getConfig();
        }
    }

    return new Config();
}

but I was failed that I could not directly replace the bean associated with hazelcastInstance though I could get the config and try to new the hazelcastInstance again. I need such help and instructions from hz team.

@enesakar enesakar removed the PENDING label Nov 2, 2015
@sancar sancar added this to the Backlog milestone Nov 20, 2015
@simpleusr
Copy link

simpleusr commented Nov 29, 2017

@gurbuzali your provided solution does not seem to work at least with 3.8.3 release. Once a client switches to inactive state , client from HazelcastClientProxy is null

@burakcelebi burakcelebi added Source: Community PR or issue was opened by a community user Priority: Medium and removed Source: Community PR or issue was opened by a community user labels Jan 3, 2018
@sancar sancar self-assigned this Jan 29, 2018
@sancar
Copy link
Contributor

sancar commented Jan 30, 2018

Hi @mzapletal , @simpleusr , @bwzhang2011
I would propose a different approach to the problem. Once a client is shutdown, we don't want to restart it. The shutdown state is a well-defined state for both hazelcast server&client, once it is shutdown there should be no resource/thread in use.

Instead of restarting the client, I would propose that configure it so that it will never shutdown. And always be re-usable.
This can be achieved by setting following properties
This will be the interval for a client to check if cluster is there once it is disconnected from cluster.
http://docs.hazelcast.org/docs/3.9/manual/html-single/index.html#setting-connection-timeout

And following will be the property to decide when to give up. If you want, you can set this practically infinite (Integer.MAX), or you can set a smaller value so that client will be closed for example after a week.
http://docs.hazelcast.org/docs/3.9/manual/html-single/index.html#setting-connection-attempt-limit

This proposal was not practical before 3.9. Because all operations on a disconnected client would block. But In 3.9, we have introduced async reconnect mode to give flexible behaviour in disconnected period.
For details you can see this doc.
http://docs.hazelcast.org/docs/3.9/manual/html-single/index.html#configuring-client-connection-strategy

@burakcelebi
Copy link
Member

This feature is already available as explained by @sancar.
Please re-open the issue if you need further help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants