Search in sources :

Example 11 with DatabaseEntities

use of com.instaclustr.esop.impl.DatabaseEntities in project esop by instaclustr.

the class ManifestComponentsTest method testManifestFilterNoRestoreSystemKeyspaceNoNewCluster.

/**
 * ks1
 *  ks1t1
 *  ks1t2
 * ks2
 *  ks2t1
 *  ks2t2
 * system_auth
 * system
 *
 * @throws Exception
 */
@Test
public void testManifestFilterNoRestoreSystemKeyspaceNoNewCluster() throws Exception {
    Manifest manifest = parseManifest();
    manifest.enrichManifestEntries();
    List<ManifestEntry> manifestFiles = manifest.getManifestFiles(new DatabaseEntities(), // restoreSystemKeyspace
    false, // restoreSystemAuth
    false, // newCluster
    false, // withSchemas
    false);
    Assert.assertTrue(manifestFiles.stream().noneMatch(entry -> KeyspaceTable.isSystemKeyspace(entry.keyspaceTable.keyspace)));
}
Also used : DatabaseEntities(com.instaclustr.esop.impl.DatabaseEntities) ManifestEntry(com.instaclustr.esop.impl.ManifestEntry) Module(com.google.inject.Module) Inject(com.google.inject.Inject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BeforeMethod(org.testng.annotations.BeforeMethod) Keyspace(com.instaclustr.esop.impl.Snapshots.Snapshot.Keyspace) Type(com.instaclustr.esop.impl.ManifestEntry.Type) JacksonModule(com.instaclustr.jackson.JacksonModule) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) Manifest(com.instaclustr.esop.impl.Manifest) Snapshot(com.instaclustr.esop.impl.Snapshots.Snapshot) ArrayList(java.util.ArrayList) Injector(com.google.inject.Injector) DatabaseEntities(com.instaclustr.esop.impl.DatabaseEntities) List(java.util.List) Assert(org.testng.Assert) Paths(java.nio.file.Paths) Table(com.instaclustr.esop.impl.Snapshots.Snapshot.Keyspace.Table) Guice(com.google.inject.Guice) KeyspaceTable(com.instaclustr.esop.impl.KeyspaceTable) Path(java.nio.file.Path) ManifestEntry(com.instaclustr.esop.impl.ManifestEntry) Manifest(com.instaclustr.esop.impl.Manifest) Test(org.testng.annotations.Test)

Example 12 with DatabaseEntities

use of com.instaclustr.esop.impl.DatabaseEntities in project esop by instaclustr.

the class ManifestTest method testJsonManifest.

@Test
public void testJsonManifest() throws Exception {
    try {
        final List<String> tokens = new CassandraTokens(jmx).act();
        DatabaseEntities databaseEntities = DatabaseEntities.empty();
        // first table
        createTable("ks1", "ks1t1");
        disableAutocompaction("ks1");
        // #1 insert and flush & take snapshot
        insertDataIntoTable("ks1", "ks1t1");
        flush("ks1");
        insertDataIntoTable("ks1", "ks1t1");
        flush("ks1");
        waitForOperation(new TakeSnapshotOperation(jmx, new TakeSnapshotOperationRequest(databaseEntities, "snapshot1"), cassandraVersionProvider));
        // #2 insert and flush & take snapshot
        insertDataIntoTable("ks1", "ks1t1");
        flush("ks1");
        insertDataIntoTable("ks1", "ks1t1");
        flush("ks1");
        waitForOperation(new TakeSnapshotOperation(jmx, new TakeSnapshotOperationRequest(databaseEntities, "snapshot2"), cassandraVersionProvider));
        // second table
        createTable("ks2", "ks2t1");
        disableAutocompaction("ks2");
        // #1 insert and flush & take snapshot
        insertDataIntoTable("ks2", "ks2t1");
        flush("ks2");
        insertDataIntoTable("ks2", "ks2t1");
        flush("ks2");
        waitForOperation(new TakeSnapshotOperation(jmx, new TakeSnapshotOperationRequest(databaseEntities, "snapshot3"), cassandraVersionProvider));
        // parse
        final Snapshots snapshots = Snapshots.parse(cassandraDataDir);
        assertNotNull(snapshots);
        assertFalse(snapshots.isEmpty());
        assertEquals(3, snapshots.size());
        assertTrue(snapshots.get("snapshot1").isPresent());
        assertTrue(snapshots.get("snapshot2").isPresent());
        assertTrue(snapshots.get("snapshot3").isPresent());
        Manifest manifest = new Manifest(snapshots.get("snapshot3").get());
        // manifest itself, but it wont be serialised
        final Path localManifestPath = getLocalManifestPath("snapshot1");
        manifest.setManifest(getManifestAsManifestEntry(localManifestPath));
        // tokens
        manifest.setTokens(tokens);
        final String schemaVersion = new CassandraSchemaVersion(jmx).act();
        manifest.setSchemaVersion(schemaVersion);
        String writtenManifestAsJson = Manifest.write(manifest, objectMapper);
        logger.info(writtenManifestAsJson);
        assertNotNull(writtenManifestAsJson);
        Snapshot snapshot3 = snapshots.get("snapshot3").get();
        Optional<Keyspace> ks2 = snapshot3.getKeyspace("ks2");
        assertTrue(ks2.isPresent());
        assertTrue(ks2.get().containsTable("ks2t1"));
        List<ManifestEntry> ks2t1 = ks2.get().getManifestEntries("ks2t1");
        assertFalse(ks2t1.isEmpty());
        Manifest readManifest = Manifest.read(writtenManifestAsJson, objectMapper);
        assertNotNull(readManifest);
        HashMultimap<String, String> ksAndTables = readManifest.getSnapshot().getKeyspacesAndTables();
        // also system
        assertTrue(ksAndTables.size() > 2);
        assertTrue(ksAndTables.containsEntry("ks1", "ks1t1"));
        assertTrue(ksAndTables.containsEntry("ks2", "ks2t1"));
        HashMultimap<String, String> ksAndTablesWithoutSystem = readManifest.getSnapshot().getKeyspacesAndTables(false);
        assertEquals(2, ksAndTablesWithoutSystem.size());
        assertTrue(ksAndTables.containsEntry("ks1", "ks1t1"));
        assertTrue(ksAndTables.containsEntry("ks2", "ks2t1"));
        snapshots.clear();
        assertTrue(snapshots.isEmpty());
        assertEquals(0, snapshots.size());
    } finally {
        try {
            waitForOperation(new ClearSnapshotOperation(jmx, new ClearSnapshotOperationRequest("snapshot1")));
            waitForOperation(new ClearSnapshotOperation(jmx, new ClearSnapshotOperationRequest("snapshot2")));
            waitForOperation(new ClearSnapshotOperation(jmx, new ClearSnapshotOperationRequest("snapshot3")));
        } catch (final Exception ex) {
            logger.error("Unable to clear snapshots", ex);
        }
    }
}
Also used : DatabaseEntities(com.instaclustr.esop.impl.DatabaseEntities) Manifest.getLocalManifestPath(com.instaclustr.esop.impl.Manifest.getLocalManifestPath) Path(java.nio.file.Path) ClearSnapshotOperation(com.instaclustr.esop.impl.backup.coordination.ClearSnapshotOperation) ClearSnapshotOperationRequest(com.instaclustr.esop.impl.backup.coordination.ClearSnapshotOperation.ClearSnapshotOperationRequest) TakeSnapshotOperation(com.instaclustr.esop.impl.backup.coordination.TakeSnapshotOperation) TakeSnapshotOperationRequest(com.instaclustr.esop.impl.backup.coordination.TakeSnapshotOperation.TakeSnapshotOperationRequest) Manifest(com.instaclustr.esop.impl.Manifest) CassandraSchemaVersion(com.instaclustr.esop.impl.interaction.CassandraSchemaVersion) Snapshot(com.instaclustr.esop.impl.Snapshots.Snapshot) ManifestEntry(com.instaclustr.esop.impl.ManifestEntry) Manifest.getManifestAsManifestEntry(com.instaclustr.esop.impl.Manifest.getManifestAsManifestEntry) Keyspace(com.instaclustr.esop.impl.Snapshots.Snapshot.Keyspace) SchemaBuilder.createKeyspace(com.datastax.oss.driver.api.querybuilder.SchemaBuilder.createKeyspace) CassandraTokens(com.instaclustr.esop.impl.interaction.CassandraTokens) Snapshots(com.instaclustr.esop.impl.Snapshots) Test(org.testng.annotations.Test)

Example 13 with DatabaseEntities

use of com.instaclustr.esop.impl.DatabaseEntities in project esop by instaclustr.

the class DataSynchronizator method execute.

public DataSynchronizator execute() {
    final DatabaseEntities filteredManifestDatabaseEntities = manifest.getDatabaseEntities(true).filter(request.entities);
    logger.info("filtered entities from manifest: " + filteredManifestDatabaseEntities.toString());
    final List<ManifestEntry> entriesFromManifest = manifest.getManifestFiles(filteredManifestDatabaseEntities, request.restoreSystemKeyspace, request.restoreSystemAuth, request.newCluster, true);
    // 3. Build a list of all SSTables files currently present (that are candidates for deleting)
    final Set<Path> localDataFiles = CassandraData.getLocalDataFiles(request.dirs.data());
    logger.info("Restoring to existing cluster: {}", localDataFiles.size() > 0);
    for (final ManifestEntry entryFromManifest : entriesFromManifest) {
        // do not download schemas
        if (entryFromManifest.type == ManifestEntry.Type.CQL_SCHEMA) {
            continue;
        }
        if (CassandraData.containsFile(localDataFiles, entryFromManifest.localFile)) {
            // this file exists on a local disk as well as in manifest, there is nothing to download nor remove
            logger.info(String.format("%s found locally, not downloading", entryFromManifest.localFile));
        } else {
            // if it does not exist locally, we have to download it
            entriesToDownload.add(entryFromManifest);
        }
    }
    for (final Path localExistingFile : localDataFiles) {
        final Optional<ManifestEntry> first = entriesFromManifest.stream().filter(me -> localExistingFile.endsWith(me.localFile)).findFirst();
        if (first.isPresent()) {
            // if it exists, hash has to be same, otherwise delete it
            if (!SSTableUtils.isExistingSStable(localExistingFile, first.get().objectKey.getName(SSTableUtils.isSecondaryIndexManifest(first.get().objectKey) ? 4 : 3).toString())) {
                filesToDelete.add(localExistingFile);
            }
        } else {
            filesToDelete.add(localExistingFile);
        }
    }
    return this;
}
Also used : DatabaseEntities(com.instaclustr.esop.impl.DatabaseEntities) Path(java.nio.file.Path) ManifestEntry(com.instaclustr.esop.impl.ManifestEntry) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) Function(java.util.function.Function) SSTableUtils(com.instaclustr.esop.impl.SSTableUtils) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) DatabaseEntities(com.instaclustr.esop.impl.DatabaseEntities) Matcher(java.util.regex.Matcher) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) FileUtils.cleanDirectory(com.instaclustr.io.FileUtils.cleanDirectory) BiConsumer(java.util.function.BiConsumer) CassandraData(com.instaclustr.esop.impl.CassandraData) Path(java.nio.file.Path) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Set(java.util.Set) Manifest(com.instaclustr.esop.impl.Manifest) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Snapshots(com.instaclustr.esop.impl.Snapshots) RestoreOperationRequest(com.instaclustr.esop.impl.restore.RestoreOperationRequest) Function.identity(java.util.function.Function.identity) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) ManifestEntry(com.instaclustr.esop.impl.ManifestEntry)

Aggregations

DatabaseEntities (com.instaclustr.esop.impl.DatabaseEntities)13 Test (org.testng.annotations.Test)12 List (java.util.List)6 CassandraData (com.instaclustr.esop.impl.CassandraData)5 Manifest (com.instaclustr.esop.impl.Manifest)5 ManifestEntry (com.instaclustr.esop.impl.ManifestEntry)5 BeforeTest (org.testng.annotations.BeforeTest)5 Path (java.nio.file.Path)4 Pair (org.apache.commons.lang3.tuple.Pair)4 KeyspaceTables (com.instaclustr.esop.impl.KeyspaceTable.KeyspaceTables)3 Snapshot (com.instaclustr.esop.impl.Snapshots.Snapshot)3 Keyspace (com.instaclustr.esop.impl.Snapshots.Snapshot.Keyspace)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Multimap (com.google.common.collect.Multimap)2 Guice (com.google.inject.Guice)2 Inject (com.google.inject.Inject)2 Injector (com.google.inject.Injector)2 Module (com.google.inject.Module)2