Search in sources :

Example 1 with KernelLifecycle

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()));
    }
}
Also used : Context(com.aws.greengrass.dependency.Context) Path(java.nio.file.Path) ConfigurationWriter(com.aws.greengrass.config.ConfigurationWriter) Configuration(com.aws.greengrass.config.Configuration) KernelLifecycle(com.aws.greengrass.lifecyclemanager.KernelLifecycle) Topic(com.aws.greengrass.config.Topic) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with KernelLifecycle

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());
}
Also used : Context(com.aws.greengrass.dependency.Context) Path(java.nio.file.Path) Configuration(com.aws.greengrass.config.Configuration) KernelLifecycle(com.aws.greengrass.lifecyclemanager.KernelLifecycle) Topic(com.aws.greengrass.config.Topic) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with KernelLifecycle

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);
    }
}
Also used : Path(java.nio.file.Path) KernelLifecycle(com.aws.greengrass.lifecyclemanager.KernelLifecycle) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) IOException(java.io.IOException) ServiceUpdateException(com.aws.greengrass.deployment.exceptions.ServiceUpdateException)

Aggregations

KernelLifecycle (com.aws.greengrass.lifecyclemanager.KernelLifecycle)3 Path (java.nio.file.Path)3 Configuration (com.aws.greengrass.config.Configuration)2 Topic (com.aws.greengrass.config.Topic)2 Context (com.aws.greengrass.dependency.Context)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ConfigurationWriter (com.aws.greengrass.config.ConfigurationWriter)1 ServiceUpdateException (com.aws.greengrass.deployment.exceptions.ServiceUpdateException)1 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)1 DeploymentResult (com.aws.greengrass.deployment.model.DeploymentResult)1 IOException (java.io.IOException)1