From ee2f30d127dcf93e7f8de2eecaf7263ae52f68d6 Mon Sep 17 00:00:00 2001 From: ihsan demir Date: Mon, 31 Jul 2017 17:43:43 +0300 Subject: [PATCH] The fix makes sure that the cluster connect retries are obeying the configured connection attempt period in milliseconds resolution. Fixes #322 --- .../hazelcast/client/spi/ClusterService.cpp | 18 ++++++++++++------ hazelcast/test/src/cluster/ClusterTest.cpp | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/hazelcast/src/hazelcast/client/spi/ClusterService.cpp b/hazelcast/src/hazelcast/client/spi/ClusterService.cpp index b009a7760b..f7cd80989d 100644 --- a/hazelcast/src/hazelcast/client/spi/ClusterService.cpp +++ b/hazelcast/src/hazelcast/client/spi/ClusterService.cpp @@ -202,7 +202,7 @@ namespace hazelcast { util::ILogger::getLogger().finest(message.str()); } - time_t tryStartTime = std::time(NULL); + int64_t tryStartTime = util::currentTimeMillis(); std::vector
addresses = findServerAddressesToConnect(previousConnectionAddr); for (std::vector
::const_iterator it = addresses.begin(); it != addresses.end(); ++it) { try { @@ -221,19 +221,25 @@ namespace hazelcast { } } - if (++attempt >= connectionAttemptLimit) { + if (++attempt > connectionAttemptLimit) { break; } - const double remainingTime = clientContext.getClientConfig().getAttemptPeriod() - - std::difftime(std::time(NULL), tryStartTime); + + int64_t remainingTime = clientContext.getClientConfig().getAttemptPeriod() - + (util::currentTimeMillis() - tryStartTime); + + if (remainingTime < 0) { + remainingTime = 0; + } + using namespace std; std::ostringstream errorStream; - errorStream << "Unable to get alive cluster connection, try in " << max(0.0, remainingTime) + errorStream << "Unable to get alive cluster connection, try in " << remainingTime << " ms later, attempt " << attempt << " of " << connectionAttemptLimit << "."; util::ILogger::getLogger().warning(errorStream.str()); if (remainingTime > 0) { - util::sleep((unsigned) remainingTime / 1000);//MTODO + util::sleepmillis(remainingTime); } } throw exception::IllegalStateException("ClusterService", diff --git a/hazelcast/test/src/cluster/ClusterTest.cpp b/hazelcast/test/src/cluster/ClusterTest.cpp index 941ca071b9..ede81aa6cf 100644 --- a/hazelcast/test/src/cluster/ClusterTest.cpp +++ b/hazelcast/test/src/cluster/ClusterTest.cpp @@ -347,6 +347,20 @@ namespace hazelcast { ASSERT_TRUE(shutdownLatch.await(10)); } + TEST_P(ClusterTest, testConnectionAttemptPeriod) { + ClientConfig clientConfig; + clientConfig.setAttemptPeriod(900); + clientConfig.setConnectionAttemptLimit(3); + + int64_t startTimeMillis = util::currentTimeMillis(); + try { + HazelcastClient client(clientConfig); + } catch (exception::IllegalStateException &e) { + // this is expected + } + ASSERT_GE(util::currentTimeMillis() - startTimeMillis, 3 * 900); + } + TEST_P(ClusterTest, testAllClientStatesWhenUserShutdown) { HazelcastServer instance(*g_srvFactory);