
com.fasterxml.clustermate.std.JdkClusterStatusAccessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of clustermate-api Show documentation
Show all versions of clustermate-api Show documentation
Data types needed for service abstraction,
used by both server and client components.
The newest version!
package com.fasterxml.clustermate.std;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.fasterxml.clustermate.api.*;
import com.fasterxml.clustermate.api.msg.ClusterStatusMessage;
import com.fasterxml.storemate.shared.IpAndPort;
import com.fasterxml.storemate.shared.util.IOUtil;
/**
* Implementation of {@link ClusterStatusAccessor} that uses JDK
* HTTP access functionality.
*/
public class JdkClusterStatusAccessor extends ClusterStatusAccessor
{
protected final ClusterStatusAccessor.Converter _converter;
protected final String[] _basePath;
protected final RequestPathStrategy> _paths;
public JdkClusterStatusAccessor(ClusterStatusAccessor.Converter converter,
String[] basePath, RequestPathStrategy> paths)
{
_converter = converter;
_basePath = basePath;
_paths = paths;
}
@Override
public ClusterStatusMessage getClusterStatus(IpAndPort ip, long timeoutMsecs)
throws IOException
{
JdkHttpClientPathBuilder pathBuilder = new JdkHttpClientPathBuilder(ip)
.addPathSegments(_basePath);
pathBuilder = _paths.appendNodeStatusPath(pathBuilder);
return _getClusterStatus(pathBuilder.toString(), timeoutMsecs, true);
}
@Override
public ClusterStatusMessage getRemoteStatus(IpAndPort ip, long timeoutMsecs)
throws IOException
{
JdkHttpClientPathBuilder pathBuilder = new JdkHttpClientPathBuilder(ip)
.addPathSegments(_basePath);
pathBuilder = _paths.appendRemoteStatusPath(pathBuilder);
return _getClusterStatus(pathBuilder.toString(), timeoutMsecs, false);
}
@Override
public ClusterStatusMessage getClusterStatus(String endpoint, long timeoutMsecs)
throws IOException
{
return _getClusterStatus(endpoint, timeoutMsecs, true);
}
protected ClusterStatusMessage _getClusterStatus(String endpoint, long timeoutMsecs,
boolean verifyLocalPeers)
throws IOException
{
// first: if we can't spend at least 10 msecs, let's give up:
if (timeoutMsecs < MIN_TIMEOUT_MSECS) {
return null;
}
HttpURLConnection conn;
try {
URL url = new URL(endpoint);
conn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
throw new IOException("Can not access Cluster state using '"+endpoint+"': "+e.getMessage());
}
conn.setRequestMethod("GET");
// not optimal, will have to do:
conn.setConnectTimeout((int) timeoutMsecs);
conn.setReadTimeout((int) timeoutMsecs);
int status = conn.getResponseCode();
if (!IOUtil.isHTTPSuccess(status)) {
// should we read the error message?
throw new IOException("Failed to access Cluster state using '"+endpoint+"': response code "
+status);
}
InputStream in;
try {
in = conn.getInputStream();
} catch (IOException e) {
throw new IOException("Can not access Cluster state using '"+endpoint+"': "+e.getMessage());
}
ClusterStatusMessage result;
try {
result = _converter.fromJSON(in);
} catch (IOException e) {
throw new IOException("Invalid Cluster state returned by '"+endpoint+"', failed to parse JSON: "+e.getMessage());
} finally {
try {
in.close();
} catch (IOException e) { }
}
// validate briefly, just in case:
if (result.local == null) {
throw new IOException("Invalid Cluster state returned by '"+endpoint+"', missing 'local' info");
}
if (result.local.getAddress() == null) {
throw new IOException("Invalid Cluster state returned by '"+endpoint+"', missing 'local.address' info");
}
if (verifyLocalPeers && result.localPeers == null) {
throw new IOException("Invalid Cluster state returned by '"+endpoint+"', missing 'localPeers' info");
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy