use of com.aws.greengrass.config.WhatHappened in project aws-greengrass-nucleus by aws-greengrass.
the class GenericExternalServiceIntegTest method GIVEN_running_service_WHEN_pause_resume_requested_THEN_pause_resume_Service_and_freeze_thaw_cgroup.
void GIVEN_running_service_WHEN_pause_resume_requested_THEN_pause_resume_Service_and_freeze_thaw_cgroup(ExtensionContext context) throws Exception {
ignoreExceptionOfType(context, FileSystemException.class);
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, getClass().getResource("long_running_services.yaml"));
kernel.launch();
CountDownLatch mainRunningLatch = new CountDownLatch(1);
kernel.getMain().addStateSubscriber((WhatHappened what, Topic t) -> {
if (Coerce.toEnum(State.class, t).isRunning()) {
mainRunningLatch.countDown();
}
});
// wait for main to run
assertTrue(mainRunningLatch.await(60, TimeUnit.SECONDS), "main running");
GenericExternalService component = (GenericExternalService) kernel.locate("sleeperA");
assertThat(component.getState(), is(State.RUNNING));
component.pause();
assertTrue(component.isPaused());
assertThat(getCgroupFreezerState(component.getServiceName()), anyOf(is(LinuxSystemResourceController.CgroupFreezerState.FROZEN), is(LinuxSystemResourceController.CgroupFreezerState.FREEZING)));
component.resume();
assertFalse(component.isPaused());
assertThat(getCgroupFreezerState(component.getServiceName()), is(LinuxSystemResourceController.CgroupFreezerState.THAWED));
}
use of com.aws.greengrass.config.WhatHappened in project aws-greengrass-nucleus by aws-greengrass.
the class KernelShutdownTest method WHEN_kernel_shutdown_THEN_services_are_shutdown_in_reverse_dependency_order.
@Test
void WHEN_kernel_shutdown_THEN_services_are_shutdown_in_reverse_dependency_order() throws InterruptedException {
AtomicBoolean mainClosed = new AtomicBoolean(false);
AtomicBoolean sleeperAClosed = new AtomicBoolean(false);
AtomicBoolean sleeperBClosed = new AtomicBoolean(false);
kernel.getContext().addGlobalStateChangeListener((service, oldState, newState) -> {
if ("main".equals(service.getName()) && newState.isClosable()) {
mainClosed.set(true);
}
// Only count main as started if its dependency (new_service) has already been started
if (mainClosed.get() && "sleeperA".equals(service.getName()) && newState.isClosable()) {
sleeperAClosed.set(true);
}
if (sleeperAClosed.get() && "sleeperB".equals(service.getName()) && newState.isClosable()) {
sleeperBClosed.set(true);
}
});
CountDownLatch mainRunningLatch = new CountDownLatch(1);
kernel.getMain().addStateSubscriber((WhatHappened what, Topic t) -> {
if (Coerce.toEnum(State.class, t).isRunning()) {
mainRunningLatch.countDown();
}
});
// wait for main to run
assertTrue(mainRunningLatch.await(60, TimeUnit.SECONDS));
kernel.shutdown(60);
assertTrue(sleeperBClosed.get());
}
use of com.aws.greengrass.config.WhatHappened in project aws-greengrass-nucleus by aws-greengrass.
the class GenericExternalServiceIntegTest method GIVEN_service_with_dynamically_loaded_config_WHEN_dynamic_config_changes_THEN_service_does_not_restart.
@Test
void GIVEN_service_with_dynamically_loaded_config_WHEN_dynamic_config_changes_THEN_service_does_not_restart() throws Exception {
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, getClass().getResource("service_with_dynamic_config.yaml"));
CountDownLatch mainRunning = new CountDownLatch(1);
kernel.getContext().addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals("main") && newState.equals(State.RUNNING)) {
mainRunning.countDown();
}
});
kernel.launch();
assertTrue(mainRunning.await(5, TimeUnit.SECONDS));
GenericExternalService service = spy((GenericExternalService) kernel.locate("service_with_dynamic_config"));
assertEquals(State.RUNNING, service.getState());
CompletableFuture<Void> topicUpdateProcessedFuture = new CompletableFuture<>();
Subscriber customConfigWatcher = (WhatHappened what, Topic t) -> {
topicUpdateProcessedFuture.complete(null);
};
Topic customConfigTopic = service.getServiceConfig().find(CONFIGURATION_CONFIG_KEY, "my_custom_key");
customConfigTopic.subscribe(customConfigWatcher);
customConfigTopic.withValue("my_custom_initial_value");
topicUpdateProcessedFuture.get();
assertEquals(State.RUNNING, service.getState());
verify(service, times(0)).requestReinstall();
verify(service, times(0)).requestRestart();
}
use of com.aws.greengrass.config.WhatHappened in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentConfigMergingTest method GIVEN_a_running_service_is_not_disruptable_WHEN_deployed_THEN_deployment_waits.
@Test
@SuppressWarnings({ "PMD.CloseResource", "PMD.AvoidCatchingGenericException" })
void GIVEN_a_running_service_is_not_disruptable_WHEN_deployed_THEN_deployment_waits() throws Throwable {
// GIVEN
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, getClass().getResource("non_disruptable_service.yaml"));
kernel.launch();
CountDownLatch mainFinished = new CountDownLatch(1);
kernel.getMain().addStateSubscriber((WhatHappened what, Topic t) -> {
if (Coerce.toEnum(State.class, t).equals(State.FINISHED)) {
mainFinished.countDown();
}
});
// wait for main to finish
assertTrue(mainFinished.await(10, TimeUnit.SECONDS));
AtomicInteger deferCount = new AtomicInteger(0);
AtomicInteger preComponentUpdateCount = new AtomicInteger(0);
CountDownLatch postComponentUpdateRecieved = new CountDownLatch(1);
String authToken = IPCTestUtils.getAuthTokeForService(kernel, "nondisruptable");
final EventStreamRPCConnection clientConnection = IPCTestUtils.connectToGGCOverEventStreamIPC(socketOptions, authToken, kernel);
GreengrassCoreIPCClient greengrassCoreIPCClient = new GreengrassCoreIPCClient(clientConnection);
SubscribeToComponentUpdatesRequest subscribeToComponentUpdatesRequest = new SubscribeToComponentUpdatesRequest();
CompletableFuture<SubscribeToComponentUpdatesResponse> fut = greengrassCoreIPCClient.subscribeToComponentUpdates(subscribeToComponentUpdatesRequest, Optional.of(new StreamResponseHandler<ComponentUpdatePolicyEvents>() {
@Override
public void onStreamEvent(ComponentUpdatePolicyEvents streamEvent) {
if (streamEvent.getPreUpdateEvent() != null) {
preComponentUpdateCount.getAndIncrement();
if (deferCount.get() < 1) {
DeferComponentUpdateRequest deferComponentUpdateRequest = new DeferComponentUpdateRequest();
deferComponentUpdateRequest.setRecheckAfterMs(Duration.ofSeconds(7).toMillis());
deferComponentUpdateRequest.setMessage("Test");
deferComponentUpdateRequest.setDeploymentId(streamEvent.getPreUpdateEvent().getDeploymentId());
greengrassCoreIPCClient.deferComponentUpdate(deferComponentUpdateRequest, Optional.empty());
deferCount.getAndIncrement();
}
}
if (streamEvent.getPostUpdateEvent() != null) {
postComponentUpdateRecieved.countDown();
clientConnection.disconnect();
}
}
@Override
public boolean onStreamError(Throwable error) {
logger.atError().setCause(error).log("Caught an error on the stream");
return false;
}
@Override
public void onStreamClosed() {
logger.atWarn().log("Stream closed by the server");
}
})).getResponse();
try {
fut.get(30, TimeUnit.SECONDS);
} catch (Exception e) {
logger.atError().setCause(e).log("Error when subscribing to component updates");
fail("Caught exception when subscribing to component updates");
}
Map<String, Object> currentConfig = new HashMap<>(kernel.getConfig().toPOJO());
Future<DeploymentResult> future = deploymentConfigMerger.mergeInNewConfig(testDeployment(), currentConfig);
// update should be deferred for 5 seconds
assertThrows(TimeoutException.class, () -> future.get(5, TimeUnit.SECONDS), "Merge should not happen within 5 seconds");
assertTrue(postComponentUpdateRecieved.await(15, TimeUnit.SECONDS));
assertEquals(2, preComponentUpdateCount.get());
}
use of com.aws.greengrass.config.WhatHappened in project aws-greengrass-nucleus by aws-greengrass.
the class DeploymentConfigMergingTest method GIVEN_kernel_running_services_WHEN_merge_removes_service_THEN_removed_service_is_closed.
@Test
void GIVEN_kernel_running_services_WHEN_merge_removes_service_THEN_removed_service_is_closed() throws Throwable {
// GIVEN
ConfigPlatformResolver.initKernelWithMultiPlatformConfig(kernel, getClass().getResource("long_running_services.yaml"));
kernel.launch();
CountDownLatch mainRunningLatch = new CountDownLatch(1);
kernel.getMain().addStateSubscriber((WhatHappened what, Topic t) -> {
if (Coerce.toEnum(State.class, t).isRunning()) {
mainRunningLatch.countDown();
}
});
// wait for main to run
assertTrue(mainRunningLatch.await(60, TimeUnit.SECONDS), "main running");
Map<String, Object> currentConfig = new HashMap<>(kernel.getConfig().toPOJO());
Map<String, Map> servicesConfig = (Map<String, Map>) currentConfig.get(SERVICES_NAMESPACE_TOPIC);
// removing all services in the current Nucleus config except sleeperB, main, and nucleus
servicesConfig.keySet().removeIf(serviceName -> !"sleeperB".equals(serviceName) && !"main".equals(serviceName) && !DEFAULT_NUCLEUS_COMPONENT_NAME.equalsIgnoreCase(serviceName));
List<String> dependencies = new ArrayList<>((List<String>) servicesConfig.get("main").get(SERVICE_DEPENDENCIES_NAMESPACE_TOPIC));
// removing main's dependency on sleeperA, Now sleeperA is an unused dependency
dependencies.removeIf(s -> s.contains("sleeperA"));
servicesConfig.get("main").put(SERVICE_DEPENDENCIES_NAMESPACE_TOPIC, dependencies);
// updating service B's run
Map lifecycle = (Map) servicesConfig.get("sleeperB").get(SERVICE_LIFECYCLE_NAMESPACE_TOPIC);
lifecycle.put(LIFECYCLE_RUN_NAMESPACE_TOPIC, ((String) lifecycle.get(LIFECYCLE_RUN_NAMESPACE_TOPIC)).replace("5", "10"));
Future<DeploymentResult> deploymentFuture = deploymentConfigMerger.mergeInNewConfig(testDeployment(), currentConfig);
DeploymentResult deploymentResult = deploymentFuture.get(30, TimeUnit.SECONDS);
assertEquals(SUCCESSFUL, deploymentResult.getDeploymentStatus());
GreengrassService main = kernel.locate("main");
assertThat(main::getState, eventuallyEval(is(State.RUNNING), Duration.ofSeconds(30)));
GreengrassService sleeperB = kernel.locate("sleeperB");
assertEquals(State.RUNNING, sleeperB.getState());
// ensure context finish all tasks
kernel.getContext().waitForPublishQueueToClear();
// ensuring config value for sleeperA is removed
assertFalse(kernel.getConfig().findTopics(SERVICES_NAMESPACE_TOPIC).children.containsKey("sleeperA"), "sleeperA removed");
// ensure Nucleus no longer holds a reference of sleeperA
assertThrows(ServiceLoadException.class, () -> kernel.locate("sleeperA"));
List<String> orderedDependencies = kernel.orderedDependencies().stream().filter(greengrassService -> greengrassService instanceof GenericExternalService).map(GreengrassService::getName).collect(Collectors.toList());
assertThat(orderedDependencies, containsInAnyOrder("sleeperB", DEFAULT_NUCLEUS_COMPONENT_NAME, "main"));
// sleeperB and nucleus can be in any order, but must be before main
assertThat(orderedDependencies, containsInRelativeOrder("sleeperB", "main"));
assertThat(orderedDependencies, containsInRelativeOrder(DEFAULT_NUCLEUS_COMPONENT_NAME, "main"));
}
Aggregations