Search in sources :

Example 6 with Cassandra

use of com.github.nosan.embedded.cassandra.Cassandra in project esop by instaclustr.

the class BaseListingRemovalTest method testListingAndBackup.

@Test
public void testListingAndBackup() throws Exception {
    Cassandra cassandra = getCassandra(cassandraDir, CASSANDRA_VERSION);
    cassandra.start();
    waitForCql();
    String[][] arguments = hardlinkingArguments(CASSANDRA_VERSION);
    try (CqlSession session = CqlSession.builder().build()) {
        createTable(session, KEYSPACE, TABLE);
        createTable(session, KEYSPACE_2, TABLE_2);
        // stefansnapshot-1
        insertAndCallBackupCLI(2, session, arguments[0]);
        // stefansnapshot-2
        insertAndCallBackupCLI(2, session, arguments[1]);
        // stefansnapshot-2 in different storage location
        insertAndCallBackupCLI(2, session, arguments[6]);
        try {
            logger.info("Executing the first restoration phase - download {}", asList(arguments[2]));
            Esop.mainWithoutExit(arguments[2]);
            logger.info("Executing the second restoration phase - truncate {}", asList(arguments[3]));
            Esop.mainWithoutExit(arguments[3]);
            logger.info("Executing the third restoration phase - import {}", asList(arguments[4]));
            Esop.mainWithoutExit(arguments[4]);
            logger.info("Executing the fourth restoration phase - cleanup {}", asList(arguments[5]));
            Esop.mainWithoutExit(arguments[5]);
            // we expect 4 records to be there as 2 were there before the first backup and the second 2 before the second backup
            dumpTableAndAssertRowCount(session, KEYSPACE, TABLE, 4);
            dumpTableAndAssertRowCount(session, KEYSPACE_2, TABLE_2, 4);
            // listing
            final Path jsonComplexFile = createTempFile("esop-backup-json-complex", null);
            final Path jsonSimpleFile = createTempFile("esop-backup-json-simple", null);
            final Path tableComplexFile = createTempFile("esop-backup-table-complex", null);
            final Path tableSimpleFile = createTempFile("esop-backup-table-simple", null);
            final String[] jsonComplex = new String[] { "list", "--storage-location=" + getStorageLocation(), "--human-units", "--json", "--to-file=" + jsonComplexFile.toAbsolutePath(), "--cache-dir=" + target(".esop") };
            final String[] jsonSimple = new String[] { "list", "--storage-location=" + getStorageLocation(), "--human-units", "--json", "--simple-format", "--to-file=" + jsonSimpleFile.toAbsolutePath(), "--cache-dir=" + target(".esop") };
            final String[] tableComplex = new String[] { "list", "--storage-location=" + getStorageLocation(), "--human-units", "--to-file=" + tableComplexFile.toAbsolutePath(), "--cache-dir=" + target(".esop") };
            final String[] tableSimple = new String[] { "list", "--storage-location=" + getStorageLocation(), "--human-units", "--simple-format", "--to-file=" + tableSimpleFile.toAbsolutePath(), "--cache-dir=" + target(".esop") };
            logger.info("Executing listing of json complex format: " + asList(jsonComplex));
            Esop.mainWithoutExit(jsonComplex);
            logger.info("Executing listing of json simple format: " + asList(jsonSimple));
            Esop.mainWithoutExit(jsonSimple);
            logger.info("Executing listing of table complex format: " + asList(tableComplex));
            Esop.mainWithoutExit(tableComplex);
            logger.info("Executing listing of table simple format: " + asList(tableSimple));
            Esop.mainWithoutExit(tableSimple);
            Manifest.AllManifestsReport report = objectMapper.readValue(Files.readAllBytes(jsonComplexFile), Manifest.AllManifestsReport.class);
            Optional<Manifest.ManifestReporter.ManifestReport> oldest = report.getOldest();
            Optional<Manifest.ManifestReporter.ManifestReport> latest = report.getLatest();
            if (!oldest.isPresent()) {
                Assert.fail("Not found the oldest report!");
            }
            if (!latest.isPresent()) {
                Assert.fail("Not found the latest report!");
            }
            final String oldestBackupName = oldest.get().name;
            final String latestBackupName = latest.get().name;
            final String[] delete1 = new String[] { "remove-backup", "--storage-location=" + getStorageLocation(), "--backup-name=" + oldestBackupName, "--cache-dir=" + target(".esop") };
            logger.info("Executing the first delete - {}", asList(delete1));
            // delete oldest
            Esop.mainWithoutExit(delete1);
            final String[] delete2 = new String[] { "remove-backup", "--storage-location=" + getStorageLocation(), "--backup-name=" + latestBackupName, "--cache-dir=" + target(".esop") };
            logger.info("Executing the second delete - {}", asList(delete2));
            // delete latest
            Esop.mainWithoutExit(delete2);
            // we basically deleted everything in the first storage location by deleting first two backups
            // assertEquals(Files.list(Paths.get(getStorageLocation().replaceAll(protocol(), ""), "data")).count(), 0);
            String s = getStorageLocation().replaceAll(protocol(), "");
            Path manifests = Paths.get(target(".esop")).resolve(s).resolve("manifests");
            assertEquals(0, Files.list(manifests).count());
        } finally {
            cassandra.stop();
        }
    }
}
Also used : Path(java.nio.file.Path) Cassandra(com.github.nosan.embedded.cassandra.Cassandra) Manifest(com.instaclustr.esop.impl.Manifest) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.testng.annotations.Test)

Example 7 with Cassandra

use of com.github.nosan.embedded.cassandra.Cassandra in project embedded-cassandra-spring-boot-starter by nosan.

the class EmbeddedCassandraAutoConfigurationTests method configureProperties.

@Test
void configureProperties() {
    this.runner.withUserConfiguration(ExcludeCassandraBeanDefinitionRegistryPostProcessor.class).withPropertyValues("cassandra.embedded.config-file=classpath:cassandra.yaml", "cassandra.embedded.config-properties.start_rpc=true", "cassandra.embedded.environment-variables.JVM_OPTS=-Xmx512m", "cassandra.embedded.jvm-options=-Xmx256m", "cassandra.embedded.logger=MyLogger", "cassandra.embedded.version=3.11.3", "cassandra.embedded.name=MyCassandra", "cassandra.embedded.register-shutdown-hook=false", "cassandra.embedded.system-properties.cassandra.start_rpc=true", "cassandra.embedded.startup-timeout=1m", "cassandra.embedded.working-directory-resources.[conf/cassandra.yaml]=classpath:cassandra.yaml", "cassandra.embedded.working-directory=target/embeddedCassandra").withBean(CassandraBuilderConfigurator.class, () -> (builder) -> builder.addJvmOptions("-Xmx1024m")).run(context -> {
        assertThat(context).hasSingleBean(CassandraBuilder.class);
        Cassandra cassandra = context.getBean(CassandraBuilder.class).build();
        assertThat(cassandra).hasFieldOrPropertyWithValue("databaseFactory.jvmOptions", new LinkedHashSet<>(Arrays.asList("-Xmx256m", "-Xmx1024m")));
        assertThat(cassandra).hasFieldOrPropertyWithValue("logger", Logger.get("MyLogger"));
        assertThat(cassandra).hasFieldOrPropertyWithValue("name", "MyCassandra");
        assertThat(cassandra).hasFieldOrPropertyWithValue("version", Version.parse("3.11.3"));
        assertThat(cassandra).hasFieldOrPropertyWithValue("registerShutdownHook", false);
        assertThat(cassandra).hasFieldOrPropertyWithValue("startupTimeout", Duration.ofMinutes(1));
        assertThat(cassandra).hasFieldOrPropertyWithValue("workingDirectory", Paths.get("target/embeddedCassandra").toAbsolutePath());
        assertThat(cassandra).hasFieldOrPropertyWithValue("databaseFactory.environmentVariables", Collections.singletonMap("JVM_OPTS", "-Xmx512m"));
        Map<String, Object> systemProperties = new LinkedHashMap<>();
        systemProperties.put("cassandra.start_rpc", "true");
        systemProperties.put("cassandra.config", new UrlResource(new ClassPathResource("cassandra.yaml").getURL()));
        assertThat(cassandra).hasFieldOrPropertyWithValue("databaseFactory.systemProperties", systemProperties);
        assertThat((Collection<?>) ReflectionTestUtils.getField(cassandra, "workingDirectoryCustomizers")).hasSize(1);
    });
}
Also used : CassandraBuilder(com.github.nosan.embedded.cassandra.CassandraBuilder) Arrays(java.util.Arrays) DriverConfigLoaderBuilderCustomizer(org.springframework.boot.autoconfigure.cassandra.DriverConfigLoaderBuilderCustomizer) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BeanFactoryUtils(org.springframework.beans.factory.BeanFactoryUtils) ClassPathResource(org.springframework.core.io.ClassPathResource) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) CassandraBuilderConfigurator(com.github.nosan.embedded.cassandra.CassandraBuilderConfigurator) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) Version(com.github.nosan.embedded.cassandra.Version) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) LinkedHashMap(java.util.LinkedHashMap) Cassandra(com.github.nosan.embedded.cassandra.Cassandra) DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) CqlSession(com.datastax.oss.driver.api.core.CqlSession) CqlScript(com.github.nosan.embedded.cassandra.cql.CqlScript) Duration(java.time.Duration) Map(java.util.Map) UrlResource(com.github.nosan.embedded.cassandra.commons.UrlResource) LinkedHashSet(java.util.LinkedHashSet) Logger(com.github.nosan.embedded.cassandra.commons.logging.Logger) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) OS(org.junit.jupiter.api.condition.OS) CassandraAutoConfiguration(org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration) Collection(java.util.Collection) CqlSessionBuilder(com.datastax.oss.driver.api.core.CqlSessionBuilder) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) BeansException(org.springframework.beans.BeansException) BeanDefinitionRegistryPostProcessor(org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor) Test(org.junit.jupiter.api.Test) FilteredClassLoader(org.springframework.boot.test.context.FilteredClassLoader) Configuration(org.springframework.context.annotation.Configuration) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) Paths(java.nio.file.Paths) AssertableApplicationContext(org.springframework.boot.test.context.assertj.AssertableApplicationContext) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) CqlSessionFactoryBean(org.springframework.data.cassandra.config.CqlSessionFactoryBean) Cassandra(com.github.nosan.embedded.cassandra.Cassandra) CassandraBuilder(com.github.nosan.embedded.cassandra.CassandraBuilder) CassandraBuilderConfigurator(com.github.nosan.embedded.cassandra.CassandraBuilderConfigurator) ClassPathResource(org.springframework.core.io.ClassPathResource) LinkedHashMap(java.util.LinkedHashMap) UrlResource(com.github.nosan.embedded.cassandra.commons.UrlResource) Collection(java.util.Collection) Test(org.junit.jupiter.api.Test)

Example 8 with Cassandra

use of com.github.nosan.embedded.cassandra.Cassandra in project esop by instaclustr.

the class AbstractBackupTest method liveBackupRestoreTestRenamedEntities.

public void liveBackupRestoreTestRenamedEntities(final String[][] arguments, final String cassandraVersion, int rounds, boolean crossKeyspaceRestore) throws Exception {
    Cassandra cassandra = getCassandra(cassandraDir, cassandraVersion);
    cassandra.start();
    waitForCql();
    try (CqlSession session = CqlSession.builder().build()) {
        createTable(session, KEYSPACE, TABLE);
        createTable(session, KEYSPACE_2, TABLE_2);
        createTable(session, KEYSPACE_2, TABLE_3);
        // stefansnapshot-1
        insertAndCallBackupCLI(2, session, arguments[0]);
        // stefansnapshot-2
        insertAndCallBackupCLI(2, session, arguments[1]);
        for (int i = 1; i < rounds + 1; ++i) {
            // each phase is executed twice here to check that phases are idempotent / repeatable
            logger.info("Round " + i + " - Executing the first restoration phase - download {}", asList(arguments[2]));
            Esop.mainWithoutExit(arguments[2]);
            logger.info("Round " + i + " - Executing the first restoration phase - download {}", asList(arguments[2]));
            Esop.mainWithoutExit(arguments[2]);
            logger.info("Round " + i + " - Executing the second restoration phase - truncate {}", asList(arguments[3]));
            Esop.mainWithoutExit(arguments[3]);
            logger.info("Round " + i + " - Executing the second restoration phase - truncate {}", asList(arguments[3]));
            Esop.mainWithoutExit(arguments[3]);
            // 
            // this will import backup-ed table2 into table3
            // 
            logger.info("Round " + i + " - Executing the third restoration phase - import {}", asList(arguments[4]));
            Esop.mainWithoutExit(arguments[4]);
            if (!cassandraVersion.startsWith("4")) {
                // second round would not pass for 4 because import deletes files in download
                logger.info("Round " + i + " - Executing the third restoration phase for the second time - import {}", asList(arguments[4]));
                Esop.mainWithoutExit(arguments[4]);
            }
            logger.info("Round " + i + " - Executing the fourth restoration phase - cleanup {}", asList(arguments[5]));
            Esop.mainWithoutExit(arguments[5]);
            logger.info("Round " + i + " - Executing the fourth restoration phase - cleanup {}", asList(arguments[5]));
            Esop.mainWithoutExit(arguments[5]);
            // we expect 4 records to be there as 2 were there before the first backup and the second 2 before the second backup
            dumpTableAndAssertRowCount(session, KEYSPACE, TABLE, 4);
            dumpTableAndAssertRowCount(session, KEYSPACE_2, TABLE_2, 4);
            // here we expect that table3 is same as table2
            if (!crossKeyspaceRestore) {
                dumpTableAndAssertRowCount(session, KEYSPACE_2, TABLE_3, 4);
            }
        }
    } finally {
        cassandra.stop();
        FileUtils.deleteDirectory(cassandraDir);
        deleteDirectory(Paths.get(target("backup1")));
    }
}
Also used : Cassandra(com.github.nosan.embedded.cassandra.Cassandra) CqlSession(com.datastax.oss.driver.api.core.CqlSession)

Example 9 with Cassandra

use of com.github.nosan.embedded.cassandra.Cassandra in project esop by instaclustr.

the class AbstractBackupTest method inPlaceBackupRestoreTest.

public void inPlaceBackupRestoreTest(final String[][] arguments) throws Exception {
    Cassandra cassandra = null;
    try {
        cassandra = getCassandra(cassandraDir, CASSANDRA_VERSION);
        cassandra.start();
        List<Long> insertionTimes;
        try (CqlSession session = CqlSession.builder().build()) {
            insertionTimes = populateDatabaseWithBackup(session, arguments);
            assertEquals(insertionTimes.size(), NUMBER_OF_INSERTED_ROWS);
        }
        logger.info("Executing backup of commit logs {}", asList(arguments[3]));
        Esop.mainWithoutExit(arguments[3]);
        cassandra.stop();
        // RESTORE VERIFICATION
        cassandra = getCassandra(cassandraRestoredDir, CASSANDRA_VERSION, (workingDirectory, version) -> {
            try {
                FileUtils.createDirectory(workingDirectory.resolve("data").resolve("data"));
                FileUtils.createDirectory(workingDirectory.resolve("data").resolve("data2"));
                FileUtils.createDirectory(workingDirectory.resolve("data").resolve("data3"));
            } catch (final Exception ex) {
            // ignore
            }
            restoreOnStoppedNode(insertionTimes, arguments);
        });
        cassandra.start();
        waitForCql();
        try (CqlSession session = CqlSession.builder().build()) {
            dumpTableAndAssertRowCount(session, KEYSPACE, TABLE, NUMBER_OF_ROWS_AFTER_RESTORATION);
        }
    } finally {
        if (cassandra != null) {
            cassandra.stop();
        }
        deleteDirectory(Paths.get(target("commitlog_download_dir")));
        FileUtils.deleteDirectory(cassandraDir);
        FileUtils.deleteDirectory(cassandraRestoredDir);
    }
}
Also used : QueryBuilder.insertInto(com.datastax.oss.driver.api.querybuilder.QueryBuilder.insertInto) CassandraBuilder(com.github.nosan.embedded.cassandra.CassandraBuilder) OperationsModule(com.instaclustr.operations.OperationsModule) TIMEUUID(com.datastax.oss.driver.api.core.type.DataTypes.TIMEUUID) KEYSPACE_2(com.instaclustr.esop.backup.embedded.TestEntity2.KEYSPACE_2) IntStream.range(java.util.stream.IntStream.range) HashSpec(com.instaclustr.esop.impl.hash.HashSpec) Date(java.util.Date) HashModule(com.instaclustr.esop.impl.hash.HashModule) LoggerFactory(org.slf4j.LoggerFactory) KEYSPACE(com.instaclustr.esop.backup.embedded.TestEntity.KEYSPACE) Arrays.asList(java.util.Arrays.asList) DATE(com.instaclustr.esop.backup.embedded.TestEntity.DATE) Path(java.nio.file.Path) CassandraModule(com.instaclustr.cassandra.CassandraModule) Awaitility.await(org.awaitility.Awaitility.await) Collection(java.util.Collection) ExecutorsModule(com.instaclustr.threading.ExecutorsModule) CassandraJMXServiceImpl(jmx.org.apache.cassandra.service.CassandraJMXServiceImpl) QueryBuilder.selectFrom(com.datastax.oss.driver.api.querybuilder.QueryBuilder.selectFrom) UUID(java.util.UUID) ImmutableMap.of(com.google.common.collect.ImmutableMap.of) String.format(java.lang.String.format) SchemaBuilder(com.datastax.oss.driver.api.querybuilder.SchemaBuilder) RestorationStrategyType(com.instaclustr.esop.impl.restore.RestorationStrategy.RestorationStrategyType) List(java.util.List) QueryBuilder.literal(com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal) TABLE_3(com.instaclustr.esop.backup.embedded.TestEntity3.TABLE_3) TEXT(com.datastax.oss.driver.api.core.type.DataTypes.TEXT) CassandraJMXConnectionInfo(jmx.org.apache.cassandra.CassandraJMXConnectionInfo) TABLE(com.instaclustr.esop.backup.embedded.TestEntity.TABLE) ApiException(io.kubernetes.client.ApiException) Assert.assertEquals(org.testng.Assert.assertEquals) MINUTES(java.util.concurrent.TimeUnit.MINUTES) HashMap(java.util.HashMap) TABLE_2(com.instaclustr.esop.backup.embedded.TestEntity2.TABLE_2) ListModule(com.instaclustr.esop.impl.list.ListModule) Version(com.github.nosan.embedded.cassandra.Version) WorkingDirectoryCustomizer(com.github.nosan.embedded.cassandra.WorkingDirectoryCustomizer) ArrayList(java.util.ArrayList) BackupModule(com.instaclustr.esop.impl.backup.BackupModules.BackupModule) Cassandra(com.github.nosan.embedded.cassandra.Cassandra) KEYSPACE_3(com.instaclustr.esop.backup.embedded.TestEntity3.KEYSPACE_3) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Objects.requireNonNull(java.util.Objects.requireNonNull) Row(com.datastax.oss.driver.api.core.cql.Row) UploadingModule(com.instaclustr.esop.impl.backup.BackupModules.UploadingModule) FileUtils(com.instaclustr.io.FileUtils) ID(com.instaclustr.esop.backup.embedded.TestEntity.ID) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) CassandraSchemaVersion(com.instaclustr.esop.impl.interaction.CassandraSchemaVersion) RestorationStrategyModule(com.instaclustr.esop.impl.restore.RestoreModules.RestorationStrategyModule) Logger(org.slf4j.Logger) Esop(com.instaclustr.esop.cli.Esop) FileUtils.deleteDirectory(com.instaclustr.io.FileUtils.deleteDirectory) DataType(com.datastax.oss.driver.api.core.type.DataType) DownloadingModule(com.instaclustr.esop.impl.restore.RestoreModules.DownloadingModule) IOException(java.io.IOException) Uuids.timeBased(com.datastax.oss.driver.api.core.uuid.Uuids.timeBased) File(java.io.File) RemoveBackupModule(com.instaclustr.esop.impl.remove.RemoveBackupModule) Collectors.toList(java.util.stream.Collectors.toList) NAME(com.instaclustr.esop.backup.embedded.TestEntity.NAME) Paths(java.nio.file.Paths) RestoreModule(com.instaclustr.esop.impl.restore.RestoreModules.RestoreModule) SECONDS(java.util.concurrent.TimeUnit.SECONDS) AbstractModule(com.google.inject.AbstractModule) Cassandra(com.github.nosan.embedded.cassandra.Cassandra) CqlSession(com.datastax.oss.driver.api.core.CqlSession) ApiException(io.kubernetes.client.ApiException) IOException(java.io.IOException)

Aggregations

CqlSession (com.datastax.oss.driver.api.core.CqlSession)9 Cassandra (com.github.nosan.embedded.cassandra.Cassandra)9 CassandraBuilder (com.github.nosan.embedded.cassandra.CassandraBuilder)3 Version (com.github.nosan.embedded.cassandra.Version)2 CassandraSchemaVersion (com.instaclustr.esop.impl.interaction.CassandraSchemaVersion)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2 Collection (java.util.Collection)2 CassandraJMXConnectionInfo (jmx.org.apache.cassandra.CassandraJMXConnectionInfo)2 CassandraJMXServiceImpl (jmx.org.apache.cassandra.service.CassandraJMXServiceImpl)2 Test (org.testng.annotations.Test)2 CqlSessionBuilder (com.datastax.oss.driver.api.core.CqlSessionBuilder)1 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)1 Row (com.datastax.oss.driver.api.core.cql.Row)1 DataType (com.datastax.oss.driver.api.core.type.DataType)1 TEXT (com.datastax.oss.driver.api.core.type.DataTypes.TEXT)1 TIMEUUID (com.datastax.oss.driver.api.core.type.DataTypes.TIMEUUID)1 Uuids.timeBased (com.datastax.oss.driver.api.core.uuid.Uuids.timeBased)1 QueryBuilder.insertInto (com.datastax.oss.driver.api.querybuilder.QueryBuilder.insertInto)1