Search in sources :

Example 1 with EtcdKeysResponse

use of mousio.etcd4j.responses.EtcdKeysResponse in project camel by apache.

the class EtcdWatchConsumer method onResponse.

@Override
public void onResponse(ResponsePromise<EtcdKeysResponse> promise) {
    if (!isRunAllowed()) {
        return;
    }
    Exchange exchange = null;
    Throwable throwable = promise.getException();
    if (throwable != null && throwable instanceof EtcdException) {
        EtcdException exception = (EtcdException) throwable;
        // So we set the index to the one returned by the exception + 1
        if (EtcdHelper.isOutdatedIndexException(exception)) {
            LOGGER.debug("Outdated index, key: {}, cause={}", getPath(), exception.etcdCause);
            // We set the index to the one returned by the exception + 1.
            index.set(exception.index + 1);
            // Clean-up the exception so it is not rethrown/handled
            throwable = null;
        }
    } else {
        try {
            EtcdKeysResponse response = promise.get();
            exchange = endpoint.createExchange();
            exchange.getIn().setHeader(EtcdConstants.ETCD_NAMESPACE, getNamespace());
            exchange.getIn().setHeader(EtcdConstants.ETCD_PATH, response.node.key);
            exchange.getIn().setBody(response);
            // Watch from the modifiedIndex + 1 of the node we got for ensuring
            // no events are missed between watch commands
            index.set(response.node.modifiedIndex + 1);
        } catch (TimeoutException e) {
            LOGGER.debug("Timeout watching for {}", getPath());
            if (configuration.isSendEmptyExchangeOnTimeout()) {
                exchange = endpoint.createExchange();
                exchange.getIn().setHeader(EtcdConstants.ETCD_NAMESPACE, getNamespace());
                exchange.getIn().setHeader(EtcdConstants.ETCD_TIMEOUT, true);
                exchange.getIn().setHeader(EtcdConstants.ETCD_PATH, getPath());
                exchange.getIn().setBody(null);
            }
            throwable = null;
        } catch (Exception e1) {
            throwable = e1;
        }
        if (exchange != null) {
            try {
                throwable = null;
                getProcessor().process(exchange);
            } catch (Exception e) {
                getExceptionHandler().handleException("Error processing exchange", exchange, e);
            }
        }
    }
    if (throwable != null) {
        handleException("Error processing etcd response", throwable);
    }
    try {
        watch();
    } catch (Exception e) {
        handleException("Error watching key " + getPath(), e);
    }
}
Also used : Exchange(org.apache.camel.Exchange) EtcdKeysResponse(mousio.etcd4j.responses.EtcdKeysResponse) EtcdException(mousio.etcd4j.responses.EtcdException) EtcdException(mousio.etcd4j.responses.EtcdException) TimeoutException(java.util.concurrent.TimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with EtcdKeysResponse

use of mousio.etcd4j.responses.EtcdKeysResponse in project camel by apache.

the class EtcdKeysTest method testKeys.

@Test(expected = EtcdException.class)
public void testKeys() throws Exception {
    final String path = "/camel/" + UUID.randomUUID().toString();
    final String value = UUID.randomUUID().toString();
    final EtcdClient client = getClient();
    final Map<String, Object> headers = new HashMap<>();
    // *******************************************
    // SET
    // *******************************************
    headers.clear();
    headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_SET);
    headers.put(EtcdConstants.ETCD_PATH, path);
    sendBody("direct:keys-set", value, headers);
    MockEndpoint mockSet = getMockEndpoint("mock:result-set");
    mockSet.expectedMinimumMessageCount(1);
    mockSet.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.keys.name());
    mockSet.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);
    mockSet.assertIsSatisfied();
    // *******************************************
    // GET
    // *******************************************
    headers.clear();
    headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_GET);
    headers.put(EtcdConstants.ETCD_PATH, path);
    sendBody("direct:keys-get", value, headers);
    MockEndpoint mockGet = getMockEndpoint("mock:result-get");
    mockGet.expectedMinimumMessageCount(1);
    mockSet.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.keys.name());
    mockGet.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);
    mockGet.expectedMessagesMatches(new Predicate() {

        @Override
        public boolean matches(Exchange exchange) {
            EtcdKeysResponse keysResponse = exchange.getIn().getBody(EtcdKeysResponse.class);
            assertNotNull(keysResponse);
            assertNotNull(keysResponse.node);
            assertNotNull(keysResponse.node.value);
            return keysResponse.node.value.equals(value);
        }
    });
    mockGet.assertIsSatisfied();
    // *******************************************
    // DELETE
    // *******************************************
    headers.clear();
    headers.put(EtcdConstants.ETCD_ACTION, EtcdConstants.ETCD_KEYS_ACTION_DELETE);
    headers.put(EtcdConstants.ETCD_PATH, path);
    sendBody("direct:keys-del", "value", headers);
    MockEndpoint mockDel = getMockEndpoint("mock:result-del");
    mockDel.expectedMinimumMessageCount(1);
    mockSet.expectedHeaderReceived(EtcdConstants.ETCD_NAMESPACE, EtcdNamespace.keys.name());
    mockDel.expectedHeaderReceived(EtcdConstants.ETCD_PATH, path);
    mockDel.assertIsSatisfied();
    // *******************************************
    // VALIDATION
    // *******************************************
    client.get(path).send().get();
    fail("EtcdException should have been thrown");
}
Also used : Exchange(org.apache.camel.Exchange) EtcdClient(mousio.etcd4j.EtcdClient) HashMap(java.util.HashMap) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) EtcdKeysResponse(mousio.etcd4j.responses.EtcdKeysResponse) Predicate(org.apache.camel.Predicate) Test(org.junit.Test)

Example 3 with EtcdKeysResponse

use of mousio.etcd4j.responses.EtcdKeysResponse in project camel by apache.

the class EtcdServiceDiscovery method getServices.

protected List<ServiceDefinition> getServices(Predicate<EtcdServiceDefinition> filter) {
    List<ServiceDefinition> servers = Collections.emptyList();
    if (isRunAllowed()) {
        try {
            final EtcdConfiguration conf = getConfiguration();
            final EtcdKeyGetRequest request = getClient().get(conf.getServicePath()).recursive();
            if (conf.hasTimeout()) {
                request.timeout(conf.getTimeout(), TimeUnit.SECONDS);
            }
            final EtcdKeysResponse response = request.send().get();
            if (Objects.nonNull(response.node) && !response.node.nodes.isEmpty()) {
                servers = response.node.nodes.stream().map(node -> node.value).filter(ObjectHelper::isNotEmpty).map(this::nodeFromString).filter(Objects::nonNull).filter(filter).sorted(EtcdServiceDefinition.COMPARATOR).collect(Collectors.toList());
            }
        } catch (Exception e) {
            throw new RuntimeCamelException(e);
        }
    }
    return servers;
}
Also used : EtcdKeysResponse(mousio.etcd4j.responses.EtcdKeysResponse) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Logger(org.slf4j.Logger) EtcdClient(mousio.etcd4j.EtcdClient) Predicate(java.util.function.Predicate) ServiceDefinition(org.apache.camel.cloud.ServiceDefinition) EtcdConfiguration(org.apache.camel.component.etcd.EtcdConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EtcdKeyGetRequest(mousio.etcd4j.requests.EtcdKeyGetRequest) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) DefaultServiceDiscovery(org.apache.camel.impl.cloud.DefaultServiceDiscovery) ObjectHelper(org.apache.camel.util.ObjectHelper) Collections(java.util.Collections) EtcdHelper(org.apache.camel.component.etcd.EtcdHelper) ObjectHelper(org.apache.camel.util.ObjectHelper) EtcdKeyGetRequest(mousio.etcd4j.requests.EtcdKeyGetRequest) EtcdConfiguration(org.apache.camel.component.etcd.EtcdConfiguration) Objects(java.util.Objects) EtcdKeysResponse(mousio.etcd4j.responses.EtcdKeysResponse) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ServiceDefinition(org.apache.camel.cloud.ServiceDefinition) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 4 with EtcdKeysResponse

use of mousio.etcd4j.responses.EtcdKeysResponse in project camel by apache.

the class EtcdWatchServiceDiscovery method onResponse.

// *************************************************************************
// Watch
// *************************************************************************
@Override
public void onResponse(ResponsePromise<EtcdKeysResponse> promise) {
    if (!isRunAllowed()) {
        return;
    }
    Throwable throwable = promise.getException();
    if (throwable != null && throwable instanceof EtcdException) {
        EtcdException exception = (EtcdException) throwable;
        if (EtcdHelper.isOutdatedIndexException(exception)) {
            LOGGER.debug("Outdated index, key={}, cause={}", servicePath, exception.etcdCause);
            index.set(exception.index + 1);
        }
    } else {
        try {
            EtcdKeysResponse response = promise.get();
            EtcdHelper.setIndex(index, response);
            serversRef.set(getServices());
        } catch (TimeoutException e) {
            LOGGER.debug("Timeout watching for {}", getConfiguration().getServicePath());
            throwable = null;
        } catch (Exception e) {
            throwable = e;
        }
    }
    if (throwable == null) {
        watch();
    } else {
        throw new RuntimeCamelException(throwable);
    }
}
Also used : EtcdKeysResponse(mousio.etcd4j.responses.EtcdKeysResponse) RuntimeCamelException(org.apache.camel.RuntimeCamelException) EtcdException(mousio.etcd4j.responses.EtcdException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) TimeoutException(java.util.concurrent.TimeoutException) EtcdException(mousio.etcd4j.responses.EtcdException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with EtcdKeysResponse

use of mousio.etcd4j.responses.EtcdKeysResponse in project camel by apache.

the class EtcdRoutePolicy method onResponse.

// *************************************************************************
// Watch
// *************************************************************************
@Override
public void onResponse(ResponsePromise<EtcdKeysResponse> promise) {
    if (!isRunAllowed()) {
        return;
    }
    Throwable throwable = promise.getException();
    if (throwable != null && throwable instanceof EtcdException) {
        EtcdException exception = (EtcdException) throwable;
        if (EtcdHelper.isOutdatedIndexException(exception)) {
            LOGGER.debug("Outdated index, key={}, cause={}", servicePath, exception.etcdCause);
            index.set(exception.index + 1);
            throwable = null;
        }
    } else {
        try {
            EtcdKeysResponse response = promise.get();
            EtcdHelper.setIndex(index, response);
            if (response.node.value == null) {
                setLeader(tryTakeLeadership());
            } else if (!ObjectHelper.equal(serviceName, response.node.value) && leader.get()) {
                // Looks like I've lost leadership
                setLeader(false);
            }
        } catch (TimeoutException e) {
            LOGGER.debug("Timeout watching for {}", servicePath);
            throwable = null;
        } catch (Exception e1) {
            throwable = e1;
        }
    }
    if (throwable == null) {
        watch();
    } else {
        throw new RuntimeCamelException(throwable);
    }
}
Also used : EtcdKeysResponse(mousio.etcd4j.responses.EtcdKeysResponse) RuntimeCamelException(org.apache.camel.RuntimeCamelException) EtcdException(mousio.etcd4j.responses.EtcdException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) TimeoutException(java.util.concurrent.TimeoutException) EtcdException(mousio.etcd4j.responses.EtcdException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

EtcdKeysResponse (mousio.etcd4j.responses.EtcdKeysResponse)6 EtcdException (mousio.etcd4j.responses.EtcdException)4 TimeoutException (java.util.concurrent.TimeoutException)3 RuntimeCamelException (org.apache.camel.RuntimeCamelException)3 EtcdClient (mousio.etcd4j.EtcdClient)2 Exchange (org.apache.camel.Exchange)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Objects (java.util.Objects)1 TimeUnit (java.util.concurrent.TimeUnit)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 EtcdKeyGetRequest (mousio.etcd4j.requests.EtcdKeyGetRequest)1 Predicate (org.apache.camel.Predicate)1 ServiceDefinition (org.apache.camel.cloud.ServiceDefinition)1 EtcdConfiguration (org.apache.camel.component.etcd.EtcdConfiguration)1 EtcdHelper (org.apache.camel.component.etcd.EtcdHelper)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1