use of com.aws.greengrass.lifecyclemanager.KernelLifecycle in project aws-greengrass-nucleus by aws-greengrass.
the class KernelTest method GIVEN_kernel_running_WHEN_truncate_tlog_and_shutdown_THEN_tlog_consistent_with_non_truncated_tlog.
@SuppressWarnings("PMD.CloseResource")
@Test
void GIVEN_kernel_running_WHEN_truncate_tlog_and_shutdown_THEN_tlog_consistent_with_non_truncated_tlog() throws Exception {
Configuration config = kernel.getConfig();
Context context = kernel.getContext();
Path configPath = tempRootDir.resolve("config");
Files.createDirectories(configPath);
kernel.writeEffectiveConfigAsTransactionLog(configPath.resolve("full.tlog"));
try (ConfigurationWriter configurationWriter = ConfigurationWriter.logTransactionsTo(config, configPath.resolve("full.tlog")).flushImmediately(true)) {
kernel.parseArgs().launch();
Topic testTopic = config.lookup("testTopic").withValue("initial");
KernelLifecycle kernelLifecycle = context.get(KernelLifecycle.class);
context.runOnPublishQueueAndWait(() -> {
// make truncate run by setting a small limit
kernelLifecycle.getTlog().withMaxEntries(1);
testTopic.withValue("triggering truncate");
// immediately queue a task to increase max size to prevent repeated truncation
context.runOnPublishQueue(() -> kernelLifecycle.getTlog().withMaxEntries(10000));
});
// wait for things to complete
CountDownLatch startupCdl = new CountDownLatch(1);
context.addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals("main") && newState.equals(State.FINISHED)) {
startupCdl.countDown();
}
});
startupCdl.await(30, TimeUnit.SECONDS);
// shutdown to stop config/tlog changes
kernel.shutdown();
Configuration fullConfig = ConfigurationReader.createFromTLog(context, configPath.resolve("full.tlog"));
Configuration compressedConfig = ConfigurationReader.createFromTLog(context, configPath.resolve("config.tlog"));
assertEquals("triggering truncate", compressedConfig.find("testTopic").getOnce());
assertThat(fullConfig.toPOJO(), is(compressedConfig.toPOJO()));
}
}
use of com.aws.greengrass.lifecyclemanager.KernelLifecycle in project aws-greengrass-nucleus by aws-greengrass.
the class KernelTest method GIVEN_kernel_running_WHEN_truncate_tlog_and_shutdown_THEN_tlog_consistent_with_config.
@SuppressWarnings("PMD.CloseResource")
@Test
void GIVEN_kernel_running_WHEN_truncate_tlog_and_shutdown_THEN_tlog_consistent_with_config() throws Exception {
kernel.parseArgs().launch();
Configuration config = kernel.getConfig();
Context context = kernel.getContext();
Path configPath = tempRootDir.resolve("config");
Topic testTopic = config.lookup("testTopic").withValue("initial");
KernelLifecycle kernelLifecycle = context.get(KernelLifecycle.class);
context.runOnPublishQueueAndWait(() -> {
// make truncate run by setting a small limit
kernelLifecycle.getTlog().withMaxEntries(1);
testTopic.withValue("triggering truncate");
// immediately queue a task to increase max size to prevent repeated truncation
context.runOnPublishQueue(() -> kernelLifecycle.getTlog().withMaxEntries(10000));
});
// wait for things to complete
CountDownLatch startupCdl = new CountDownLatch(1);
context.addGlobalStateChangeListener((service, oldState, newState) -> {
if (service.getName().equals("main") && newState.equals(State.FINISHED)) {
startupCdl.countDown();
}
});
startupCdl.await(30, TimeUnit.SECONDS);
// shutdown to stop config/tlog changes
kernel.shutdown();
Configuration tlogConfig = ConfigurationReader.createFromTLog(context, configPath.resolve("config.tlog"));
assertEquals("triggering truncate", tlogConfig.find("testTopic").getOnce());
// data type may be different when recreating tlog
// using this to coerce to string for comparison
assertEqualsDeepMap(config.toPOJO(), tlogConfig.toPOJO());
}
use of com.aws.greengrass.lifecyclemanager.KernelLifecycle in project aws-greengrass-nucleus by aws-greengrass.
the class KernelUpdateActivator method activate.
@Override
public void activate(Map<String, Object> newConfig, Deployment deployment, CompletableFuture<DeploymentResult> totallyCompleteFuture) {
if (!takeConfigSnapshot(totallyCompleteFuture)) {
return;
}
if (!kernelAlternatives.isLaunchDirSetup()) {
totallyCompleteFuture.complete(new DeploymentResult(DeploymentResult.DeploymentStatus.FAILED_NO_STATE_CHANGE, new UnsupportedOperationException("Unable to process deployment. Greengrass launch directory is not set up or Greengrass " + "is not set up as a system service")));
return;
}
DeploymentDocument deploymentDocument = deployment.getDeploymentDocumentObj();
KernelLifecycle lifecycle = kernel.getContext().get(KernelLifecycle.class);
// Preserve tlog state before launch directory is updated to reflect ongoing deployment.
// Wait for all services to close.
lifecycle.softShutdown(30);
updateConfiguration(deploymentDocument.getTimestamp(), newConfig);
Path bootstrapTaskFilePath;
try {
bootstrapTaskFilePath = deploymentDirectoryManager.getBootstrapTaskFilePath();
deploymentDirectoryManager.takeConfigSnapshot(deploymentDirectoryManager.getTargetConfigFilePath());
bootstrapManager.persistBootstrapTaskList(bootstrapTaskFilePath);
kernelAlternatives.prepareBootstrap(deploymentDocument.getDeploymentId());
} catch (IOException e) {
rollback(deployment, e);
return;
}
try {
int exitCode = bootstrapManager.executeAllBootstrapTasksSequentially(bootstrapTaskFilePath);
if (!bootstrapManager.hasNext()) {
logger.atInfo().log("Completed all bootstrap tasks. Continue to activate deployment changes");
}
// If exitCode is 0, which happens when all bootstrap tasks are completed, restart in new launch
// directories and verify handover is complete. As a result, exit code 0 is treated as 100 here.
logger.atInfo().log((exitCode == REQUEST_REBOOT ? "device reboot" : "Nucleus restart") + " requested to complete bootstrap task");
kernel.shutdown(30, exitCode == REQUEST_REBOOT ? REQUEST_REBOOT : REQUEST_RESTART);
} catch (ServiceUpdateException | IOException e) {
rollback(deployment, e);
}
}
Aggregations