Skip to content

Commit

Permalink
v2.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
arnett, stu committed Aug 12, 2015
1 parent ea256e8 commit e83f4d8
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 11 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ description = 'Smart REST Client - JAX-RS (Jersey) REST client that provides cli
ext.githubProjectName = 'smart-client-java'

buildscript {
ext.commonBuildVersion = '1.3.2'
ext.commonBuildVersion = '1.3.3'
ext.commonBuildDir = "https://raw.githubusercontent.com/emcvipr/ecs-common-build/v$commonBuildVersion"
apply from: "$commonBuildDir/ecs-publish.buildscript.gradle", to: buildscript
}
Expand All @@ -39,7 +39,7 @@ apply from: "$commonBuildDir/ecs-publish.gradle"
dependencies {
compile 'com.sun.jersey:jersey-client:1.19',
'com.sun.jersey.contribs:jersey-apache-client4:1.19',
'org.apache.httpcomponents:httpclient:4.5',
'org.apache.httpcomponents:httpclient:4.2.6',
'log4j:log4j:1.2.17'
testCompile 'junit:junit:4.12'
}
9 changes: 9 additions & 0 deletions src/main/java/com/emc/rest/smart/HostListProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,13 @@ public interface HostListProvider {
* (<code>host.setHealthy(false)</code> is called).
*/
void runHealthCheck(Host host);

/**
* Destroy this provider. Any system resources associated with the provider
* will be cleaned up.
* <p/>
* The provider must not be reused after this method is called otherwise
* undefined behavior will occur.
*/
void destroy();
}
16 changes: 11 additions & 5 deletions src/main/java/com/emc/rest/smart/LoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,16 @@ public LoadBalancer(List<Host> initialHosts) {
* Returns the host with the lowest response index.
*/
public Host getTopHost(Map<String, Object> requestProperties) {
Host topHost = null;
Host topHost = null, topHealthyHost = null;

long lowestIndex = Long.MAX_VALUE;
long lowestIndex = Long.MAX_VALUE, lowestHealthyIndex = Long.MAX_VALUE;

synchronized (hosts) {
for (Host host : hosts) {

// apply any veto rules
if (shouldVeto(host, requestProperties)) continue;

// if the host is unhealthy/down, ignore it
if (!host.isHealthy()) continue;

// get response index for a host
long hostIndex = host.getResponseIndex();

Expand All @@ -63,8 +60,17 @@ public Host getTopHost(Map<String, Object> requestProperties) {
topHost = host;
lowestIndex = hostIndex;
}

// also keep track of the top *healthy* host
if (host.isHealthy() && hostIndex < lowestHealthyIndex) {
topHealthyHost = host;
lowestHealthyIndex = hostIndex;
}
}

// if there are no healthy hosts, we still need a host to contact
if (topHealthyHost != null) topHost = topHealthyHost;

// move the top host to the end of the host list as an extra tie-breaker
hosts.remove(topHost);
hosts.add(topHost);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/emc/rest/smart/PollingDaemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,12 @@ public void run() {
public void terminate() {
running = false;
}

public SmartConfig getSmartConfig() {
return smartConfig;
}

public boolean isRunning() {
return running;
}
}
41 changes: 38 additions & 3 deletions src/main/java/com/emc/rest/smart/SmartClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@
import com.sun.jersey.core.impl.provider.entity.ByteArrayProvider;
import com.sun.jersey.core.impl.provider.entity.FileProvider;
import com.sun.jersey.core.impl.provider.entity.InputStreamProvider;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.log4j.Logger;

public final class SmartClientFactory {
private static final Logger l4j = Logger.getLogger(SmartClientFactory.class);

public static final String DISABLE_APACHE_RETRY = "com.emc.rest.smart.disableApacheRetry";

public static Client createSmartClient(SmartConfig smartConfig) {
return createSmartClient(smartConfig, createApacheClientHandler(smartConfig));
}
Expand Down Expand Up @@ -96,9 +103,31 @@ public static Client createStandardClient(SmartConfig smartConfig,
clientConfig.getClasses().add(InputStreamProvider.class);

// build Jersey client
Client client = new Client(clientHandler, clientConfig);
return new Client(clientHandler, clientConfig);
}

return client;
/**
* Destroy this client. Any system resources associated with the client
* will be cleaned up.
* <p/>
* This method must be called when there are not responses pending otherwise
* undefined behavior will occur.
* <p/>
* The client must not be reused after this method is called otherwise
* undefined behavior will occur.
*/
public static void destroy(Client client) {
PollingDaemon pollingDaemon = (PollingDaemon) client.getProperties().get(PollingDaemon.PROPERTY_KEY);
if (pollingDaemon != null) {
l4j.debug("terminating polling daemon");
pollingDaemon.terminate();
if (pollingDaemon.getSmartConfig().getHostListProvider() != null) {
l4j.debug("destroying host list provider");
pollingDaemon.getSmartConfig().getHostListProvider().destroy();
}
}
l4j.debug("destroying Jersey client");
client.destroy();
}

static ApacheHttpClient4Handler createApacheClientHandler(SmartConfig smartConfig) {
Expand All @@ -119,7 +148,13 @@ static ApacheHttpClient4Handler createApacheClientHandler(SmartConfig smartConfi
if (smartConfig.getProxyPass() != null)
clientConfig.getProperties().put(ApacheHttpClient4Config.PROPERTY_PROXY_PASSWORD, smartConfig.getProxyPass());

return ApacheHttpClient4.create(clientConfig).getClientHandler();
ApacheHttpClient4Handler handler = ApacheHttpClient4.create(clientConfig).getClientHandler();

// disable the retry handler if necessary
if (smartConfig.getProperty(DISABLE_APACHE_RETRY) != null)
((AbstractHttpClient) handler.getHttpClient()).setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0, false));

return handler;
}

private SmartClientFactory() {
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/com/emc/rest/smart/SmartConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public Object getProperty(String propName) {
/**
* Allows custom Jersey client properties to be set. These will be passed on in the Jersey ClientConfig
*/
public void withProperty(String propName, Object value) {
public void setProperty(String propName, Object value) {
properties.put(propName, value);
}

Expand Down Expand Up @@ -184,4 +184,9 @@ public SmartConfig withHealthCheckEnabled(boolean healthCheckEnabled) {
setHealthCheckEnabled(healthCheckEnabled);
return this;
}

public SmartConfig withProperty(String propName, Object value) {
setProperty(propName, value);
return this;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/emc/rest/smart/ecs/EcsHostListProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public void runHealthCheck(Host host) {
client.resource(getRequestUri(host, "/?ping")).header("x-emc-namespace", "x").get(String.class);
}

@Override
public void destroy() {
client.destroy();
}

protected List<Host> getDataNodes(Host host) {
String path = "/?endpoint";
URI uri = getRequestUri(host, path);
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/com/emc/rest/smart/TestHealthCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public TestHostListProvider(Host host, boolean healthy) {
this.healthy = healthy;
}

@Override
public void destroy() {
}

@Override
public List<Host> getHostList() {
throw new RuntimeException("no host update");
Expand Down

0 comments on commit e83f4d8

Please sign in to comment.