You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@curator.apache.org by Gabriel Menegatti <ga...@s1mbi0se.com.br> on 2018/12/13 18:11:45 UTC

How to properly close curator

Hello,

I kindly ask your help to solve this connection problem.

I'm trying to implement a fallback logic on my connection to Zookeeper
using Apache Curator, basically I have two sets of connection strings and
if I receive a LOST state on my state listener I try to reconnect my
curator client on the another set of connection strings. I could simple put
all machines on the same connection string but I want to connect on
fallback only when all machines for the default cluster are offline.

The problem is that I can't close the previous curator client when I try to
change to the fallback cluster, I keep receiving the LOG message saying
that curator is trying to reconnect, even after I connect on the fallback
set of zookeepers. Below you can find a code example of what I'm trying to
do:

---
final ConnectionStateListener listener = (client1, state) -> {
    if (state == ConnectionState.LOST) {
        reconnect();
    }
};
---

And the reconnect method (will change the lastHost to the fallback cluster):

---
if (client != null) {
    client.close();
}
...
client = CuratorFrameworkFactory.newClient(
        lastHost,
        sessionTimeout,
        connectionTimeout,
        retryPolicy);
...
client.start()
---

I can successfully connect on the new set of connection strings (fallback)
but the problem is that the previous client keep trying to connect on the
previous connection strings.

Looking at the close() method I saw that curator only close things if the
State of the client is STARTED, I think that's why curator keep trying to
connect on the previous cluster.

Is there a way to close() the curator client without having the STARTED
state on it?

If not is there another way to implement this logic (fallback zookeeper
servers) on curator?

This question was also posted on StackOverflow:
https://stackoverflow.com/questions/53764456/java-apache-curator-how-to-properly-close-curator

Thanks in advance.

Regards,
Gabriel.

Re: How to properly close curator

Posted by Cameron McKenzie <mc...@gmail.com>.
The state you're looking at in the close method in the
CuratorFrameworkState (which is either LATENT, STARTED or STOPPED), not the
ConnectionState (LOST, SUSPENDED etc.). So, the close() method should work
fine, even if your CuratorFramework instance is in a LOST state.

If you haven't actually called start() on your CuratorFramework instance,
then it won't have attempted to establish a connection to ZK, so you
shouldn't need to call close on it.
cheers

On Fri, Dec 14, 2018 at 5:12 AM Gabriel Menegatti <ga...@s1mbi0se.com.br>
wrote:

> Hello,
>
> I kindly ask your help to solve this connection problem.
>
> I'm trying to implement a fallback logic on my connection to Zookeeper
> using Apache Curator, basically I have two sets of connection strings and
> if I receive a LOST state on my state listener I try to reconnect my
> curator client on the another set of connection strings. I could simple put
> all machines on the same connection string but I want to connect on
> fallback only when all machines for the default cluster are offline.
>
> The problem is that I can't close the previous curator client when I try
> to change to the fallback cluster, I keep receiving the LOG message saying
> that curator is trying to reconnect, even after I connect on the fallback
> set of zookeepers. Below you can find a code example of what I'm trying to
> do:
>
> ---
> final ConnectionStateListener listener = (client1, state) -> {
>     if (state == ConnectionState.LOST) {
>         reconnect();
>     }
> };
> ---
>
> And the reconnect method (will change the lastHost to the fallback
> cluster):
>
> ---
> if (client != null) {
>     client.close();
> }
> ...
> client = CuratorFrameworkFactory.newClient(
>         lastHost,
>         sessionTimeout,
>         connectionTimeout,
>         retryPolicy);
> ...
> client.start()
> ---
>
> I can successfully connect on the new set of connection strings (fallback)
> but the problem is that the previous client keep trying to connect on the
> previous connection strings.
>
> Looking at the close() method I saw that curator only close things if the
> State of the client is STARTED, I think that's why curator keep trying to
> connect on the previous cluster.
>
> Is there a way to close() the curator client without having the STARTED
> state on it?
>
> If not is there another way to implement this logic (fallback zookeeper
> servers) on curator?
>
> This question was also posted on StackOverflow:
>
> https://stackoverflow.com/questions/53764456/java-apache-curator-how-to-properly-close-curator
>
> Thanks in advance.
>
> Regards,
> Gabriel.
>