Search in sources :

Example 1 with DurationSpec

use of org.apache.cassandra.config.DurationSpec in project cassandra by apache.

the class StorageService method takeSnapshot.

/**
 * Takes the snapshot of a multiple column family from different keyspaces. A snapshot name must be specified.
 *
 * @param tag
 *            the tag given to the snapshot; may not be null or empty
 * @param options
 *            Map of options (skipFlush is the only supported option for now)
 * @param entities
 *            list of keyspaces / tables in the form of empty | ks1 ks2 ... | ks1.cf1,ks2.cf2,...
 */
@Override
public void takeSnapshot(String tag, Map<String, String> options, String... entities) throws IOException {
    DurationSpec ttl = options.containsKey("ttl") ? new DurationSpec(options.get("ttl")) : null;
    if (ttl != null) {
        int minAllowedTtlSecs = CassandraRelevantProperties.SNAPSHOT_MIN_ALLOWED_TTL_SECONDS.getInt();
        if (ttl.toSeconds() < minAllowedTtlSecs)
            throw new IllegalArgumentException(String.format("ttl for snapshot must be at least %d seconds", minAllowedTtlSecs));
    }
    boolean skipFlush = Boolean.parseBoolean(options.getOrDefault("skipFlush", "false"));
    if (entities != null && entities.length > 0 && entities[0].contains(".")) {
        takeMultipleTableSnapshot(tag, skipFlush, ttl, entities);
    } else {
        takeSnapshot(tag, skipFlush, ttl, entities);
    }
}
Also used : DurationSpec(org.apache.cassandra.config.DurationSpec)

Example 2 with DurationSpec

use of org.apache.cassandra.config.DurationSpec in project cassandra by apache.

the class SnapshotManifestTest method testSerializeAndDeserialize.

@Test
public void testSerializeAndDeserialize() throws Exception {
    SnapshotManifest manifest = new SnapshotManifest(Arrays.asList("db1", "db2", "db3"), new DurationSpec("2m"), Instant.ofEpochMilli(currentTimeMillis()));
    File manifestFile = new File(tempFolder.newFile("manifest.json"));
    manifest.serializeToJsonFile(manifestFile);
    manifest = SnapshotManifest.deserializeFromJsonFile(manifestFile);
    assertThat(manifest.getExpiresAt()).isNotNull();
    assertThat(manifest.getCreatedAt()).isNotNull();
    assertThat(manifest.getFiles()).contains("db1").contains("db2").contains("db3").hasSize(3);
}
Also used : DurationSpec(org.apache.cassandra.config.DurationSpec) File(org.apache.cassandra.io.util.File) Test(org.junit.Test)

Example 3 with DurationSpec

use of org.apache.cassandra.config.DurationSpec in project cassandra by apache.

the class DirectoriesTest method createFakeSnapshot.

public FakeSnapshot createFakeSnapshot(TableMetadata table, String tag, boolean createManifest) throws IOException {
    File tableDir = cfDir(table);
    tableDir.tryCreateDirectories();
    File snapshotDir = new File(tableDir, Directories.SNAPSHOT_SUBDIR + File.pathSeparator() + tag);
    snapshotDir.tryCreateDirectories();
    Descriptor sstableDesc = new Descriptor(snapshotDir, KS, table.name, 1, SSTableFormat.Type.BIG);
    createFakeSSTable(sstableDesc);
    SnapshotManifest manifest = null;
    if (createManifest) {
        File manifestFile = Directories.getSnapshotManifestFile(snapshotDir);
        manifest = new SnapshotManifest(Collections.singletonList(sstableDesc.filenameFor(Component.DATA)), new DurationSpec("1m"), Instant.now());
        manifest.serializeToJsonFile(manifestFile);
    }
    return new FakeSnapshot(table, tag, snapshotDir, manifest);
}
Also used : SnapshotManifest(org.apache.cassandra.service.snapshot.SnapshotManifest) DurationSpec(org.apache.cassandra.config.DurationSpec) Descriptor(org.apache.cassandra.io.sstable.Descriptor) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) File(org.apache.cassandra.io.util.File)

Example 4 with DurationSpec

use of org.apache.cassandra.config.DurationSpec in project cassandra by apache.

the class DirectoriesTest method testMaybeManifestLoading.

@Test
public void testMaybeManifestLoading() throws Exception {
    for (TableMetadata cfm : CFM) {
        String tag = "test";
        Directories directories = new Directories(cfm, toDataDirectories(tempDataDir));
        Descriptor parentDesc = new Descriptor(directories.getDirectoryForNewSSTables(), KS, cfm.name, 0, SSTableFormat.Type.BIG);
        File parentSnapshotDirectory = Directories.getSnapshotDirectory(parentDesc, tag);
        List<String> files = new LinkedList<>();
        files.add(parentSnapshotDirectory.toAbsolute().absolutePath());
        File manifestFile = directories.getSnapshotManifestFile(tag);
        SnapshotManifest manifest = new SnapshotManifest(files, new DurationSpec("1m"), Instant.now());
        manifest.serializeToJsonFile(manifestFile);
        Set<File> dirs = new HashSet<>();
        dirs.add(manifestFile.parent());
        dirs.add(new File("buzz"));
        SnapshotManifest loadedManifest = Directories.maybeLoadManifest(KS, cfm.name, tag, dirs);
        assertEquals(manifest, loadedManifest);
    }
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) DataDirectories(org.apache.cassandra.db.Directories.DataDirectories) SnapshotManifest(org.apache.cassandra.service.snapshot.SnapshotManifest) DurationSpec(org.apache.cassandra.config.DurationSpec) Descriptor(org.apache.cassandra.io.sstable.Descriptor) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) File(org.apache.cassandra.io.util.File) Test(org.junit.Test)

Example 5 with DurationSpec

use of org.apache.cassandra.config.DurationSpec in project cassandra by apache.

the class Snapshot method execute.

@Override
public void execute(NodeProbe probe) {
    PrintStream out = probe.output().out;
    try {
        StringBuilder sb = new StringBuilder();
        sb.append("Requested creating snapshot(s) for ");
        Map<String, String> options = new HashMap<String, String>();
        options.put("skipFlush", Boolean.toString(skipFlush));
        if (null != ttl) {
            DurationSpec d = new DurationSpec(ttl);
            options.put("ttl", d.toString());
        }
        // Create a separate path for kclist to avoid breaking of already existing scripts
        if (null != ktList && !ktList.isEmpty()) {
            ktList = ktList.replace(" ", "");
            if (keyspaces.isEmpty() && null == table)
                sb.append("[").append(ktList).append("]");
            else {
                throw new IOException("When specifying the Keyspace table list (using -kt,--kt-list,-kc,--kc.list), you must not also specify keyspaces to snapshot");
            }
            if (!snapshotName.isEmpty())
                sb.append(" with snapshot name [").append(snapshotName).append("]");
            sb.append(" and options ").append(options.toString());
            out.println(sb.toString());
            probe.takeMultipleTableSnapshot(snapshotName, options, ktList.split(","));
            out.println("Snapshot directory: " + snapshotName);
        } else {
            if (keyspaces.isEmpty())
                sb.append("[all keyspaces]");
            else
                sb.append("[").append(join(keyspaces, ", ")).append("]");
            if (!snapshotName.isEmpty())
                sb.append(" with snapshot name [").append(snapshotName).append("]");
            sb.append(" and options ").append(options.toString());
            out.println(sb.toString());
            probe.takeSnapshot(snapshotName, table, options, toArray(keyspaces, String.class));
            out.println("Snapshot directory: " + snapshotName);
        }
    } catch (IOException e) {
        throw new RuntimeException("Error during taking a snapshot", e);
    }
}
Also used : PrintStream(java.io.PrintStream) DurationSpec(org.apache.cassandra.config.DurationSpec) HashMap(java.util.HashMap) IOException(java.io.IOException)

Aggregations

DurationSpec (org.apache.cassandra.config.DurationSpec)5 File (org.apache.cassandra.io.util.File)3 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)2 Descriptor (org.apache.cassandra.io.sstable.Descriptor)2 SnapshotManifest (org.apache.cassandra.service.snapshot.SnapshotManifest)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 HashMap (java.util.HashMap)1 DataDirectories (org.apache.cassandra.db.Directories.DataDirectories)1 TableMetadata (org.apache.cassandra.schema.TableMetadata)1