Search in sources :

Example 1 with ConfigurationWriter

use of com.aws.greengrass.config.ConfigurationWriter 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)

Aggregations

Configuration (com.aws.greengrass.config.Configuration)1 ConfigurationWriter (com.aws.greengrass.config.ConfigurationWriter)1 Topic (com.aws.greengrass.config.Topic)1 Context (com.aws.greengrass.dependency.Context)1 KernelLifecycle (com.aws.greengrass.lifecyclemanager.KernelLifecycle)1 Path (java.nio.file.Path)1 CountDownLatch (java.util.concurrent.CountDownLatch)1