Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Adds HealthCheckCache for health api /v1/health/state/<state> #134

Merged
merged 2 commits into from
May 31, 2016
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: 18 additions & 0 deletions src/main/java/com/orbitz/consul/HealthClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ public ConsulResponse<List<HealthCheck>> getChecksByState(State state, CatalogOp
return extractConsulResponse(api.getChecksByState(state.getName(), Options.from(catalogOptions, queryOptions)));
}

/**
* Asynchronously retrieves the healthchecks for a state in a given datacenter with {@link com.orbitz.consul.option.QueryOptions}.
*
* GET /v1/health/state/{state}?dc={datacenter}
*
* @param state The state to query.
* @param catalogOptions The catalog specific options to use.
* @param queryOptions The Query Options to use.
* @param callback Callback implemented by callee to handle results.
* @return A {@link com.orbitz.consul.model.ConsulResponse} containing a list of
* {@link com.orbitz.consul.model.health.HealthCheck} objects.
*/
public void getChecksByState(State state, CatalogOptions catalogOptions,
QueryOptions queryOptions,
ConsulResponseCallback<List<HealthCheck>> callback) {
extractConsulResponse(api.getChecksByState(state.getName(), Options.from(catalogOptions, queryOptions)), callback);
}

/**
* Retrieves the healthchecks for all healthy service instances.
*
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/com/orbitz/consul/cache/HealthCheckCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.orbitz.consul.cache;

import com.google.common.base.Function;
import com.google.common.net.HostAndPort;
import com.orbitz.consul.HealthClient;
import com.orbitz.consul.async.ConsulResponseCallback;
import com.orbitz.consul.model.health.HealthCheck;
import com.orbitz.consul.model.health.ServiceHealth;
import com.orbitz.consul.option.CatalogOptions;

import java.math.BigInteger;
import java.util.List;

public class HealthCheckCache extends ConsulCache<String, HealthCheck> {

private HealthCheckCache(Function<HealthCheck, String> keyConversion, CallbackConsumer<HealthCheck> callbackConsumer) {
super(keyConversion, callbackConsumer);
}

/**
* Factory method to construct a string/{@link HealthCheck} map for a particular {@link com.orbitz.consul.model.State}.
* <p/>
* Keys will be the {@link HealthCheck#getCheckId()}.
*
* @param healthClient the {@link HealthClient}
* @param state the state fo filter checks
* @return a cache object
*/
public static HealthCheckCache newCache(
final HealthClient healthClient,
final com.orbitz.consul.model.State state,
final CatalogOptions catalogOptions,
final int watchSeconds) {
Function<HealthCheck, String> keyExtractor = new Function<HealthCheck, String>() {
@Override
public String apply(HealthCheck input) {
return input.getCheckId();
}
};

CallbackConsumer<HealthCheck> callbackConsumer = new CallbackConsumer<HealthCheck>() {
@Override
public void consume(BigInteger index, ConsulResponseCallback<List<HealthCheck>> callback) {
healthClient.getChecksByState(state, catalogOptions, watchParams(index, watchSeconds), callback);
}
};

return new HealthCheckCache(keyExtractor, callbackConsumer);

}

public static HealthCheckCache newCache(final HealthClient healthClient, final com.orbitz.consul.model.State state) {
return newCache(healthClient, state, CatalogOptions.BLANK, 10);
}

}
30 changes: 30 additions & 0 deletions src/test/java/com/orbitz/consul/cache/ConsulCacheTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import com.orbitz.consul.HealthClient;
import com.orbitz.consul.KeyValueClient;
import com.orbitz.consul.model.ConsulResponse;
import com.orbitz.consul.model.State;
import com.orbitz.consul.model.agent.Agent;
import com.orbitz.consul.model.agent.Check;
import com.orbitz.consul.model.health.HealthCheck;
import com.orbitz.consul.model.health.Service;
import com.orbitz.consul.model.health.ServiceHealth;
import com.orbitz.consul.model.kv.Value;
import org.junit.Test;
Expand All @@ -28,6 +32,32 @@
public class ConsulCacheTest {


@Test
public void nodeCacheHealthCheckTest() throws Exception {
Consul client = Consul.newClient();
HealthClient healthClient = client.healthClient();
String checkName = UUID.randomUUID().toString();
String checkId = UUID.randomUUID().toString();

client.agentClient().registerCheck(checkId, checkName, 20L);
client.agentClient().passCheck(checkId);
Thread.sleep(100);

HealthCheckCache hCheck = HealthCheckCache.newCache(healthClient, State.PASS);

hCheck.start();
hCheck.awaitInitialized(3, TimeUnit.SECONDS);

HealthCheck check = hCheck.getMap().get(checkId);
assertEquals(checkId, check.getCheckId());

client.agentClient().failCheck(checkId);
Thread.sleep(100);

check = hCheck.getMap().get(checkId);
assertNull(check);
}

@Test
public void nodeCacheServicePassingTest() throws Exception {
Consul client = Consul.newClient();
Expand Down