Hazelcast asynchronous client - checking 'CLIENT_CONNECTED' state

259 views
Skip to first unread message

Netrunner

unread,
Jul 5, 2019, 11:03:19 AM7/5/19
to Hazelcast
I need to check if Hazelcast client is connected to cluster.

The way I'm trying to do this

ClientConfig clientConfig = new XmlClientConfigBuilder(xmlConfigurationFileName).build();

ClientStateListener clientStateListener = new ClientStateListener(clientConfig);
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);

final boolean connected = clientStateListener.isConnected();
final boolean started = clientStateListener.isStarted();

isStarted method return true, but isConnected always false. Between isConnected checking I'm putting and getting data from server cluster, so client is connected for sure (which I can see also in logs) - also when I'm setting asynchronous on false (async-start="false") then isConnected is returning true.

LifecycleServiceImpl Hazelcast class contains in fireLifecycleEvent method (called in HZ when status is changing) such code

executor.execute(new Runnable() {
    @Override
    public void run() {
        for (LifecycleListener lifecycleListener : lifecycleListeners.values()) {
            lifecycleListener.stateChanged(lifecycleEvent);
        }
    }
});

In synchronous mode run was called each time (twice STARTING->STARTED and STARTED->CLIENT_CONNECTED). In asynchronous only once (STARTING->STARTED).

Hazelcast version 3.12.1.

This is whole configuration (server discovery strategy by tcp-ip)

<?xml version="1.0" encoding="UTF-8"?>

<hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://www.hazelcast.com/schema/client-config
              http://www.hazelcast.com/schema/client-config/hazelcast-client-config-3.12.xsd">

    <group>
        <name>hazelcast-group</name>
    </group>

    <network>
        <cluster-members>
            <address>XXXXXXXXXXXXXXX:XXXX</address>
            <address>XXXXXXXXXXXXXXX:XXXX</address>
            <address>XXXXXXXXXXXXXXX:XXXX</address>
            <address>XXXXXXXXXXXXXXX:XXXX</address>
        </cluster-members>
    </network>

    <connection-strategy async-start="true" reconnect-mode="ASYNC">
        <connection-retry enabled="true">
            <initial-backoff-millis>2000</initial-backoff-millis>
            <max-backoff-millis>60000</max-backoff-millis>
            <multiplier>3</multiplier>
            <fail-on-max-backoff>false</fail-on-max-backoff>
            <jitter>0.5</jitter>
        </connection-retry>
    </connection-strategy>

</hazelcast-client>

All logs from Hazelcast client start:

Jul 02, 2019 2:00:42 PM com.hazelcast.client.HazelcastClient
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] A non-empty group password is configured for the Hazelcast client. Starting with Hazelcast version 3.11, clients with the same group name, but with different group passwords (that do not use authentication) will be accepted to a cluster. The group password configuration will be removed completely in a future release.
Jul 02, 2019 2:00:43 PM com.hazelcast.client.spi.ClientInvocationService
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] Running with 2 response threads, dynamic=false
Jul 02, 2019 2:00:43 PM com.hazelcast.core.LifecycleService
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] HazelcastClient 3.12.1 (20190611 - 0a0ee66) is STARTING
Jul 02, 2019 2:00:43 PM com.hazelcast.core.LifecycleService
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] HazelcastClient 3.12.1 (20190611 - 0a0ee66) is STARTED
Jul 02, 2019 2:00:44 PM com.hazelcast.internal.diagnostics.Diagnostics
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] Diagnostics disabled. To enable add -Dhazelcast.diagnostics.enabled=true to the JVM arguments.
Jul 02, 2019 2:00:44 PM com.hazelcast.client.connection.nio.ClusterConnectorService
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] Trying to connect to cluster with name: hazelcast-cache-group
Jul 02, 2019 2:00:44 PM com.hazelcast.client.connection.nio.ClusterConnectorService
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] Trying to connect to [FFFFFFFFF]:PORTXXXX as owner member
Jul 02, 2019 2:00:44 PM com.hazelcast.client.connection.ClientConnectionManager
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] Setting ClientConnection{alive=true, connectionId=1, channel=NioChannel{/CLIENT-IP:58235->FFFFFFFFF/IPFFF:PORTXXXX}, remoteEndpoint=[FFFFFFFFF]:PORTXXXX, lastReadTime=2019-07-02 14:00:44.898, lastWriteTime=2019-07-02 14:00:44.711, closedTime=never, connected server version=3.12.1} as owner with principal ClientPrincipal{uuid='8090fecb-294f-4a4a-9da7-168bf0a7faff', ownerUuid='TOKEN-FFFFFFFFF'}
Jul 02, 2019 2:00:44 PM com.hazelcast.client.connection.ClientConnectionManager
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] Authenticated with server [FFFFFFFFF]:PORTXXXX, server version:3.12.1 Local address: /CLIENT-IP:58235
Jul 02, 2019 2:00:45 PM com.hazelcast.client.spi.impl.ClientMembershipListener
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] 

Members [6] {
    Member [AAAAAAAAA]:PORTXXXX - TOKEN-AAAAAAAAA
    Member [BBBBBBBBB]:PORTXXXX - TOKEN-BBBBBBBBB
    Member [CCCCCCCCC]:PORTXXXX - TOKEN-CCCCCCCCC
    Member [DDDDDDDDD]:PORTXXXX - TOKEN-DDDDDDDDD
    Member [EEEEEEEEE]:PORTXXXX - TOKEN-EEEEEEEEE
    Member [FFFFFFFFF]:PORTXXXX - TOKEN-FFFFFFFFF
}

Jul 02, 2019 2:00:45 PM com.hazelcast.core.LifecycleService
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] HazelcastClient 3.12.1 (20190611 - 0a0ee66) is CLIENT_CONNECTED
Jul 02, 2019 2:00:45 PM com.hazelcast.client.connection.ClientConnectionManager
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] Authenticated with server [AAAAAAAAA]:PORTXXXX, server version:3.12.1 Local address: /CLIENT-IP:58238
Jul 02, 2019 2:00:45 PM com.hazelcast.client.connection.ClientConnectionManager
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] Authenticated with server [BBBBBBBBB]:PORTXXXX, server version:3.12.1 Local address: /CLIENT-IP:58236
Jul 02, 2019 2:00:45 PM com.hazelcast.client.connection.ClientConnectionManager
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] Authenticated with server [CCCCCCCCC]:PORTXXXX, server version:3.12.1 Local address: /CLIENT-IP:58237
Jul 02, 2019 2:00:45 PM com.hazelcast.client.connection.ClientConnectionManager
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] Authenticated with server [EEEEEEEEE]:PORTXXXX, server version:3.12.1 Local address: /CLIENT-IP:58240
Jul 02, 2019 2:00:45 PM com.hazelcast.client.connection.ClientConnectionManager
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] Authenticated with server [DDDDDDDDD]:PORTXXXX, server version:3.12.1 Local address: /CLIENT-IP:58239

What should be done to force statusChange for CLIENT_CONNECTED state ?

İhsan Demir

unread,
Jul 8, 2019, 4:59:40 AM7/8/19
to Hazelcast
I see from your logs that the client actually connects:

```
Jul 02, 2019 2:00:45 PM com.hazelcast.core.LifecycleService
INFO: hz.client_0 [hazelcast-cache-group] [3.12.1] HazelcastClient 3.12.1 (20190611 - 0a0ee66) is CLIENT_CONNECTED
```

Furthermore, I do not see any client disconnect in the logs, hence you definitely should see clientStateListener.isConnected(); method return true.

May be you are not waiting enough to do the check. Jut keep in mind that the connected event delivery is async via the event executor. We have working tests of this here: https://github.com/hazelcast/hazelcast/blob/master/hazelcast-client/src/test/java/com/hazelcast/client/util/ClientStateListenerTest.java#L82

Regards,
ihsan

Tomasz Jagodziński

unread,
Jul 8, 2019, 6:18:11 AM7/8/19
to haze...@googlegroups.com
Hi,

Thanks for your response. I know that client is connected for sure because I've tried to put and get Map elements successfully. I've made it in a scheduled executor that putting to map, getting from map and finally write to logs state of isConnected from time to time. The effect is as I have wrote before - element are put and get successfully and isConnected state is 'false' anyway - no matter how much time I will wait. I've also debug an internal code of Hazelcast and please check in my post text about "LifecycleServiceImpl Hazelcast class contains in fireLifecycleEvent" - in async mode stateChanged is never called for CLIENT_CONNECTED, but when I change mode to sync (default) then stateChange works as expected.

--
You received this message because you are subscribed to a topic in the Google Groups "Hazelcast" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/hazelcast/1Uvn5kfIRAE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to hazelcast+...@googlegroups.com.
To post to this group, send email to haze...@googlegroups.com.
Visit this group at https://groups.google.com/group/hazelcast.
To view this discussion on the web visit https://groups.google.com/d/msgid/hazelcast/d067a18b-8ac0-4237-8fc7-6dbf0e0ce781%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tomasz Jagodziński

unread,
Jul 8, 2019, 9:04:34 AM7/8/19
to haze...@googlegroups.com
Can you reproduce the problem ?

Tomasz Jagodziński

unread,
Jul 8, 2019, 9:13:11 AM7/8/19
to haze...@googlegroups.com
Sorry for question - you said that you have tested it. But can you do this exactly the same way as I have ? By xml configuration. I will send you soon my client classes and server configuration (server was downloaded in mentioned version - only configuration has changed - for tcp-ip discovery strategy with 6 members and with 6 backups for maps, the group has also changed - nothing unusual).

İhsan Demir

unread,
Jul 9, 2019, 4:02:41 AM7/9/19
to haze...@googlegroups.com
Hello Tomasz, yes I tried the code and config that you sent and it works, i wrote it as a test and it passes. I think in your code, you forgot this line:
```
assertTrue(clientStateListener.awaitConnected());
```
You can wait for async connection this way.

My test code:
@Test
public void testClientASYNCStartXML() throws InterruptedException, IOException {
// start server
hazelcastFactory.newHazelcastInstance();

URL resource = getClass().getClassLoader().getResource("state-listener-config.xml");
ClientConfig clientConfig = new XmlClientConfigBuilder(resource).build();

ClientStateListener clientStateListener = new ClientStateListener(clientConfig);
    hazelcastFactory.newHazelcastClient(clientConfig);

assertTrue(clientStateListener.awaitConnected());

assertTrue(clientStateListener.isConnected());
assertTrue(clientStateListener.isStarted());
}
My xml:
<?xml version="1.0" encoding="UTF-8"?>

<hazelcast-client xmlns="http://www.hazelcast.com/schema/client-config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/client-config
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+...@googlegroups.com.

To post to this group, send email to haze...@googlegroups.com.
Visit this group at https://groups.google.com/group/hazelcast.

For more options, visit https://groups.google.com/d/optout.


--

Ihsan Demir
Software Engineer, Clients Team
Mahir İz Cad. No:35, Altunizade, İstanbul 
ih...@hazelcast.com
skype: idemir

Tomasz Jagodziński

unread,
Jul 12, 2019, 4:38:27 AM7/12/19
to haze...@googlegroups.com
Hi,

I have a solution.

I was tried awaitConnected before and it was hang forever because it probably base on isConnected state. A have my own awaitConnected and the isConnected for async was always false - but when I changing xml config to SYNC, then isConnected returning true. 

The problem lies in strange behavior of IDE - now it works. It totally threw me off the scent, because everything point to Hazelcast problem (bug or my wrong configuration) and everything else was working correctly. 

Thanks for interest in this problem.

Reply all
Reply to author
Forward
0 new messages