Search in sources :

Example 11 with Pair

use of com.aws.greengrass.util.Pair in project aws-greengrass-nucleus by aws-greengrass.

the class LifecycleIPCEventStreamAgent method sendPreComponentUpdateEvent.

/**
 * Signal components about pending component updates.
 *
 * @param preComponentUpdateEvent event sent to subscribed components
 */
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public List<Future<DeferComponentUpdateRequest>> sendPreComponentUpdateEvent(PreComponentUpdateEvent preComponentUpdateEvent) {
    List<Future<DeferComponentUpdateRequest>> deferUpdateFutures = new ArrayList<>();
    discardDeferComponentUpdateFutures();
    for (Map.Entry<String, Set<StreamEventPublisher<ComponentUpdatePolicyEvents>>> entry : componentUpdateListeners.entrySet()) {
        String serviceName = entry.getKey();
        entry.getValue().forEach(subscribeHandler -> {
            log.atTrace().kv(COMPONENT_NAME, serviceName).log("Sending preComponentUpdate event");
            ComponentUpdatePolicyEvents componentUpdatePolicyEvents = new ComponentUpdatePolicyEvents();
            componentUpdatePolicyEvents.setPreUpdateEvent(preComponentUpdateEvent);
            CompletableFuture<DeferComponentUpdateRequest> deferUpdateFuture = new CompletableFuture<>();
            // If there are multiple pre component events sent to same service, we will store the latest future
            // As the update should be waiting for the latest one to complete.
            Pair<String, String> serviceAndDeployment = new Pair<>(serviceName, preComponentUpdateEvent.getDeploymentId());
            // Save to the map before sending the event so that it will be there if
            // they respond very quickly
            deferUpdateFuturesMap.put(serviceAndDeployment, deferUpdateFuture);
            try {
                subscribeHandler.sendStreamEvent(componentUpdatePolicyEvents).get(DEFAULT_STREAM_MESSAGE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
            } catch (Exception e) {
                log.atError().setCause(e).kv(COMPONENT_NAME, serviceName).log("Failed to send the pre component update on stream");
                deferUpdateFuturesMap.remove(serviceAndDeployment);
                return;
            }
            deferUpdateFutures.add(deferUpdateFuture);
        });
    }
    return deferUpdateFutures;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) DeferComponentUpdateRequest(software.amazon.awssdk.aws.greengrass.model.DeferComponentUpdateRequest) AuthorizationException(com.aws.greengrass.authorization.exceptions.AuthorizationException) ServiceException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceException) ServiceLoadException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException) ComponentUpdatePolicyEvents(software.amazon.awssdk.aws.greengrass.model.ComponentUpdatePolicyEvents) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Pair(com.aws.greengrass.util.Pair)

Example 12 with Pair

use of com.aws.greengrass.util.Pair in project aws-greengrass-nucleus by aws-greengrass.

the class TestUtils method asyncAssertOnBiConsumer.

/**
 * Wraps a given biconsumer function so that once it is called, the completable future can
 * complete with the exception, or with a success.
 *
 * @param bi
 * @param numCalls number of expected calls
 */
public static <A, B> Pair<CompletableFuture<Void>, BiConsumer<A, B>> asyncAssertOnBiConsumer(BiConsumer<A, B> bi, int numCalls) {
    CompletableFuture<Void> f = new CompletableFuture<>();
    AtomicInteger calls = new AtomicInteger(0);
    return new Pair<>(f, (a, b) -> {
        try {
            int callsSoFar = calls.incrementAndGet();
            bi.accept(a, b);
            if (callsSoFar == numCalls) {
                f.complete(null);
            }
        } catch (Throwable ex) {
            f.completeExceptionally(ex);
        }
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Pair(com.aws.greengrass.util.Pair)

Example 13 with Pair

use of com.aws.greengrass.util.Pair in project aws-greengrass-nucleus by aws-greengrass.

the class TestUtils method asyncAssertOnConsumer.

/**
 * Creates a test utility wrapping a given Consumer and returning a new Consumer and Future. Use the Future to
 * validate that the Consumer is called numCalls times without any exceptions.
 *
 * @param c        Consumer to wrap
 * @param numCalls number of expected calls. -1 will ignore the number of expected calls
 */
public static <A> Pair<CompletableFuture<Void>, Consumer<A>> asyncAssertOnConsumer(Consumer<A> c, int numCalls) {
    CompletableFuture<Void> f = new CompletableFuture<>();
    AtomicInteger calls = new AtomicInteger();
    return new Pair<>(f, (a) -> {
        try {
            int callsSoFar = calls.incrementAndGet();
            c.accept(a);
            if (callsSoFar == numCalls || numCalls < 0) {
                f.complete(null);
            } else if (callsSoFar > numCalls) {
                f.obtrudeException(new Exception("Too many invocations! (" + callsSoFar + "), expected " + numCalls));
            }
        } catch (Throwable ex) {
            f.obtrudeException(ex);
        }
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Pair(com.aws.greengrass.util.Pair)

Aggregations

Pair (com.aws.greengrass.util.Pair)13 CompletableFuture (java.util.concurrent.CompletableFuture)7 IOException (java.io.IOException)5 List (java.util.List)4 ChildChanged (com.aws.greengrass.config.ChildChanged)3 Configuration (com.aws.greengrass.config.Configuration)3 Topics (com.aws.greengrass.config.Topics)3 WhatHappened (com.aws.greengrass.config.WhatHappened)3 Context (com.aws.greengrass.dependency.Context)3 DeviceConfiguration (com.aws.greengrass.deployment.DeviceConfiguration)3 DEVICE_MQTT_NAMESPACE (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_MQTT_NAMESPACE)3 DEVICE_PARAM_AWS_REGION (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_AWS_REGION)3 DEVICE_PARAM_CERTIFICATE_FILE_PATH (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_CERTIFICATE_FILE_PATH)3 DEVICE_PARAM_IOT_DATA_ENDPOINT (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_IOT_DATA_ENDPOINT)3 DEVICE_PARAM_PRIVATE_KEY_PATH (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_PRIVATE_KEY_PATH)3 DEVICE_PARAM_ROOT_CA_PATH (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_ROOT_CA_PATH)3 DEVICE_PARAM_THING_NAME (com.aws.greengrass.deployment.DeviceConfiguration.DEVICE_PARAM_THING_NAME)3 DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT (com.aws.greengrass.mqttclient.MqttClient.DEFAULT_MQTT_MAX_OF_PUBLISH_RETRY_COUNT)3 MAX_LENGTH_OF_TOPIC (com.aws.greengrass.mqttclient.MqttClient.MAX_LENGTH_OF_TOPIC)3 MAX_NUMBER_OF_FORWARD_SLASHES (com.aws.greengrass.mqttclient.MqttClient.MAX_NUMBER_OF_FORWARD_SLASHES)3