Search in sources :

Example 1 with EtcdException

use of mousio.etcd4j.responses.EtcdException 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 EtcdException

use of mousio.etcd4j.responses.EtcdException 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 3 with EtcdException

use of mousio.etcd4j.responses.EtcdException 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)

Example 4 with EtcdException

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

the class EtcdRoutePolicy method tryTakeLeadership.

private boolean tryTakeLeadership() throws Exception {
    boolean result = false;
    try {
        EtcdKeysResponse response = getClient().put(servicePath, serviceName).prevExist(false).ttl(ttl).send().get();
        result = ObjectHelper.equal(serviceName, response.node.value);
        EtcdHelper.setIndex(index, response);
    } catch (EtcdException e) {
        if (!e.isErrorCode(EtcdErrorCode.NodeExist)) {
            throw e;
        }
    }
    return result;
}
Also used : EtcdKeysResponse(mousio.etcd4j.responses.EtcdKeysResponse) EtcdException(mousio.etcd4j.responses.EtcdException)

Example 5 with EtcdException

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

the class EtcdWatchTest method testWatchRecovery.

@Test
public void testWatchRecovery() throws Exception {
    final String key = "/myKeyRecovery";
    final EtcdClient client = getClient();
    try {
        // Delete the key if present
        client.delete(key).send().get();
    } catch (EtcdException e) {
        if (!e.isErrorCode(EtcdErrorCode.KeyNotFound)) {
            throw e;
        }
    }
    // Fill the vent backlog ( > 1000)
    for (int i = 0; i < 2000; i++) {
        client.put(key, "v" + i).send().get();
    }
    context().startRoute("watchRecovery");
    testWatch("mock:watch-recovery", key, 10);
}
Also used : EtcdClient(mousio.etcd4j.EtcdClient) EtcdException(mousio.etcd4j.responses.EtcdException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Test(org.junit.Test)

Aggregations

EtcdException (mousio.etcd4j.responses.EtcdException)5 EtcdKeysResponse (mousio.etcd4j.responses.EtcdKeysResponse)4 TimeoutException (java.util.concurrent.TimeoutException)3 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2 EtcdClient (mousio.etcd4j.EtcdClient)1 Exchange (org.apache.camel.Exchange)1 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)1 Test (org.junit.Test)1