Search in sources :

Example 1 with AdapterInstanceStatus

use of org.eclipse.hono.util.AdapterInstanceStatus in project hono by eclipse.

the class CacheBasedDeviceConnectionInfo method checkAdapterInstanceId.

private String checkAdapterInstanceId(final String adapterInstanceId, final String tenantId, final String deviceId, final Span span) {
    if (adapterInstanceId != null) {
        final AdapterInstanceStatus status = adapterInstanceStatusProvider.getStatus(adapterInstanceId);
        if (status == AdapterInstanceStatus.DEAD) {
            LOG.debug("ignoring found adapter instance id, belongs to already terminated container [tenant: {}, device-id: {}, adapter-instance-id: {}]", tenantId, deviceId, adapterInstanceId);
            span.log("ignoring found adapter instance id [" + adapterInstanceId + "], belongs to already terminated container");
            cache.remove(getAdapterInstanceEntryKey(tenantId, deviceId), adapterInstanceId).onSuccess(removed -> {
                if (removed) {
                    LOG.debug("removed entry with obsolete adapter instance id '{}' [tenant: {}, device-id: {}]", adapterInstanceId, tenantId, deviceId);
                }
            }).onFailure(thr -> LOG.debug("error removing entry with obsolete adapter instance id '{}' [tenant: {}, device-id: {}]", adapterInstanceId, tenantId, deviceId, thr));
            return null;
        } else if (status == AdapterInstanceStatus.SUSPECTED_DEAD) {
            LOG.debug("ignoring found adapter instance id, belongs to container with state 'SUSPECTED_DEAD' [tenant: {}, device-id: {}, adapter-instance-id: {}]", tenantId, deviceId, adapterInstanceId);
            span.log("ignoring found adapter instance id [" + adapterInstanceId + "], belongs to container with state 'SUSPECTED_DEAD'");
            return null;
        }
    }
    return adapterInstanceId;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Lifecycle(org.eclipse.hono.util.Lifecycle) LoggerFactory(org.slf4j.LoggerFactory) AdapterInstanceStatus(org.eclipse.hono.util.AdapterInstanceStatus) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Status(io.vertx.ext.healthchecks.Status) HealthCheckHandler(io.vertx.ext.healthchecks.HealthCheckHandler) Duration(java.time.Duration) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) DeviceConnectionConstants(org.eclipse.hono.util.DeviceConnectionConstants) Promise(io.vertx.core.Promise) Set(java.util.Set) ServerErrorException(org.eclipse.hono.client.ServerErrorException) MessageHelper(org.eclipse.hono.util.MessageHelper) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) JsonArray(io.vertx.core.json.JsonArray) ServiceClient(org.eclipse.hono.client.util.ServiceClient) Optional(java.util.Optional) Span(io.opentracing.Span) AdapterInstanceStatus(org.eclipse.hono.util.AdapterInstanceStatus)

Example 2 with AdapterInstanceStatus

use of org.eclipse.hono.util.AdapterInstanceStatus in project hono by eclipse.

the class KubernetesBasedAdapterInstanceStatusService method getDeadAdapterInstances.

@Override
public Future<Set<String>> getDeadAdapterInstances(final Collection<String> adapterInstanceIds) {
    Objects.requireNonNull(adapterInstanceIds);
    if (active.get() != 1) {
        return Future.failedFuture("service not active");
    }
    boolean needPodListRefresh = false;
    final Set<String> resultSet = new HashSet<>();
    for (final String adapterInstanceId : adapterInstanceIds) {
        final AdapterInstanceStatus status = getStatus(adapterInstanceId);
        if (status == AdapterInstanceStatus.DEAD) {
            resultSet.add(adapterInstanceId);
        } else if (status == AdapterInstanceStatus.SUSPECTED_DEAD) {
            needPodListRefresh = true;
        // don't break out of loop here - let getStatus() be invoked for all, filling suspectedContainerIds if needed
        }
    }
    if (needPodListRefresh) {
        final Promise<Set<String>> resultPromise = Promise.promise();
        final Handler<Promise<Set<String>>> resultProvider = promise -> {
            try {
                // get current pod list so that SUSPECTED_DEAD entries can be resolved
                refreshContainerLists(client.pods().inNamespace(namespace).list().getItems());
                final Set<String> deadAdapterInstances = adapterInstanceIds.stream().filter(id -> getStatus(id) == AdapterInstanceStatus.DEAD).collect(Collectors.toSet());
                promise.complete(deadAdapterInstances);
            } catch (final Exception e) {
                promise.fail(e);
            }
        };
        Optional.ofNullable(Vertx.currentContext()).ifPresentOrElse(ctx -> ctx.executeBlocking(resultProvider, false, resultPromise), () -> resultProvider.handle(resultPromise));
        return resultPromise.future();
    } else {
        return Future.succeededFuture(resultSet);
    }
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) Watcher(io.fabric8.kubernetes.client.Watcher) AdapterInstanceStatus(org.eclipse.hono.util.AdapterInstanceStatus) Watch(io.fabric8.kubernetes.client.Watch) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) WatcherException(io.fabric8.kubernetes.client.WatcherException) Matcher(java.util.regex.Matcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ContainerStatus(io.fabric8.kubernetes.api.model.ContainerStatus) CommandConstants(org.eclipse.hono.util.CommandConstants) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Logger(org.slf4j.Logger) Collection(java.util.Collection) Promise(io.vertx.core.Promise) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Vertx(io.vertx.core.Vertx) Set(java.util.Set) Pod(io.fabric8.kubernetes.api.model.Pod) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Optional(java.util.Optional) Handler(io.vertx.core.Handler) Collections(java.util.Collections) AdapterInstanceStatusService(org.eclipse.hono.commandrouter.AdapterInstanceStatusService) Promise(io.vertx.core.Promise) HashSet(java.util.HashSet) Set(java.util.Set) AdapterInstanceStatus(org.eclipse.hono.util.AdapterInstanceStatus) WatcherException(io.fabric8.kubernetes.client.WatcherException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) HashSet(java.util.HashSet)

Aggregations

Future (io.vertx.core.Future)2 Promise (io.vertx.core.Promise)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 AdapterInstanceStatus (org.eclipse.hono.util.AdapterInstanceStatus)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ContainerStatus (io.fabric8.kubernetes.api.model.ContainerStatus)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1 Watch (io.fabric8.kubernetes.client.Watch)1 Watcher (io.fabric8.kubernetes.client.Watcher)1 WatcherException (io.fabric8.kubernetes.client.WatcherException)1 Span (io.opentracing.Span)1