Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions hazelcast/src/hazelcast/client/spi/ClusterService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Address> addresses = findServerAddressesToConnect(previousConnectionAddr);
for (std::vector<Address>::const_iterator it = addresses.begin(); it != addresses.end(); ++it) {
try {
Expand All @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions hazelcast/test/src/cluster/ClusterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down