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

GH-512: Add ping method to facilitate connection healthcheck between client and server #515

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -2292,6 +2292,11 @@ public <T> Map<Long, Set<T>> navigate(String key, String ccl,
Timestamp.class).with(key, ccl, timestamp);
}

@Override
public boolean ping() {
return invoke("ping").with();
}

@Override
public Map<Long, Boolean> ping(Collection<Long> records) {
return invoke("ping", Collection.class).with(records);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5123,6 +5123,18 @@ public final <T> Map<Long, Set<T>> navigate(String key, Long record,
public abstract <T> Map<Long, Set<T>> navigate(String key, String ccl,
Timestamp timestamp);

/**
* Test the connection to the server.
* <p>
* Check if this client is still connected to the server by sending an echo
* request and awaiting a response. This can be used to ensure subsequent
* communication with the server is possible. If the connection is broken,
* it will be necessary to establish a new connection.
*
* @return boolean - {@code true} if the connection is still alive
*/
public abstract boolean ping();

/**
* Atomically check to see if each of the {@code records} currently contains
* any data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.cinchapi.concourse;

import java.net.SocketException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -69,6 +70,7 @@
import com.cinchapi.concourse.util.PrettyLinkedHashMap;
import com.cinchapi.concourse.util.PrettyLinkedTableMap;
import com.cinchapi.concourse.util.Transformers;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
Expand Down Expand Up @@ -98,6 +100,12 @@ class ConcourseThriftDriver extends Concourse {
ENVIRONMENT = config.getEnvironment();
}

/**
* Exceptions that indicate the connection to the server has failed.
*/
private static final Set<Class<? extends Throwable>> FAILED_CONNECTION_EXCEPTION_TYPES = ImmutableSet
.of(TTransportException.class, SocketException.class);

/**
* The thrift client that actually handles aggregation RPC communication.
*/
Expand Down Expand Up @@ -2472,6 +2480,24 @@ public <T> Map<Long, Set<T>> navigate(final String key, final String ccl,
});
}

@Override
public boolean ping() {
try {
return execute(() -> core.ping(creds, transaction, environment));
}
catch (Exception e) {
Set<Class<?>> causes = Throwables.getCausalChain(e).stream()
.map(Object::getClass).collect(Collectors.toSet());
if(!Sets.intersection(causes, FAILED_CONNECTION_EXCEPTION_TYPES)
.isEmpty()) {
return false;
}
else {
throw e;
}
}
}

@Override
public Map<Long, Boolean> ping(Collection<Long> records) {
return execute(() -> core.pingRecords(Collections.toLongList(records),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,11 @@ public <T> Map<Long, Set<T>> navigate(String key, String ccl,
return concourse.navigate(key, ccl, timestamp);
}

@Override
public boolean ping() {
return concourse.ping();
}

@Override
public Map<Long, Boolean> ping(Collection<Long> records) {
return concourse.ping(records);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,11 @@ public <T> Map<Long, Set<T>> navigate(String key, String ccl,
throw new UnsupportedOperationException();
}

@Override
public boolean ping() {
throw new UnsupportedOperationException();
}

@Override
public Map<Long, Boolean> ping(Collection<Long> records) {
throw new UnsupportedOperationException();
Expand Down
Loading