Search in sources :

Example 1 with SnapshotManifest

use of org.apache.cassandra.service.snapshot.SnapshotManifest in project cassandra by apache.

the class Directories method listSnapshots.

public Map<String, TableSnapshot> listSnapshots() {
    Map<String, Set<File>> snapshotDirsByTag = listSnapshotDirsByTag();
    Map<String, TableSnapshot> snapshots = Maps.newHashMapWithExpectedSize(snapshotDirsByTag.size());
    for (Map.Entry<String, Set<File>> entry : snapshotDirsByTag.entrySet()) {
        String tag = entry.getKey();
        Set<File> snapshotDirs = entry.getValue();
        SnapshotManifest manifest = maybeLoadManifest(metadata.keyspace, metadata.name, tag, snapshotDirs);
        snapshots.put(tag, buildSnapshot(tag, manifest, snapshotDirs));
    }
    return snapshots;
}
Also used : SnapshotManifest(org.apache.cassandra.service.snapshot.SnapshotManifest) TableSnapshot(org.apache.cassandra.service.snapshot.TableSnapshot) ImmutableMap(com.google.common.collect.ImmutableMap) File(org.apache.cassandra.io.util.File)

Example 2 with SnapshotManifest

use of org.apache.cassandra.service.snapshot.SnapshotManifest in project cassandra by apache.

the class ColumnFamilyStoreTest method testSnapshotWithoutFlushWithSecondaryIndexes.

@Test
public void testSnapshotWithoutFlushWithSecondaryIndexes() throws Exception {
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF_INDEX1);
    cfs.truncateBlocking();
    UpdateBuilder builder = UpdateBuilder.create(cfs.metadata.get(), "key").newRow().add("birthdate", 1L).add("notbirthdate", 2L);
    new Mutation(builder.build()).applyUnsafe();
    cfs.forceBlockingFlush();
    String snapshotName = "newSnapshot";
    cfs.snapshotWithoutFlush(snapshotName);
    File snapshotManifestFile = cfs.getDirectories().getSnapshotManifestFile(snapshotName);
    SnapshotManifest manifest = SnapshotManifest.deserializeFromJsonFile(snapshotManifestFile);
    // Keyspace1-Indexed1 and the corresponding index
    assertThat(manifest.getFiles()).hasSize(2);
    // Snapshot of the secondary index is stored in the subfolder with the same file name
    String baseTableFile = manifest.getFiles().get(0);
    String indexTableFile = manifest.getFiles().get(1);
    assertThat(baseTableFile).isNotEqualTo(indexTableFile);
    assertThat(Directories.isSecondaryIndexFolder(new File(indexTableFile).parent())).isTrue();
    assertThat(indexTableFile).endsWith(baseTableFile);
}
Also used : SnapshotManifest(org.apache.cassandra.service.snapshot.SnapshotManifest) File(org.apache.cassandra.io.util.File) Test(org.junit.Test)

Example 3 with SnapshotManifest

use of org.apache.cassandra.service.snapshot.SnapshotManifest in project cassandra by apache.

the class SASIIndexTest method testSASIComponentsAddedToSnapshot.

@Test
public void testSASIComponentsAddedToSnapshot() throws Throwable {
    String snapshotName = "sasi_test";
    Map<String, Pair<String, Integer>> data = new HashMap<>();
    Random r = new Random();
    for (int i = 0; i < 100; i++) data.put(UUID.randomUUID().toString(), Pair.create(UUID.randomUUID().toString(), r.nextInt()));
    ColumnFamilyStore store = loadData(data, true);
    store.forceMajorCompaction();
    Set<SSTableReader> ssTableReaders = store.getLiveSSTables();
    Set<Component> sasiComponents = new HashSet<>();
    for (Index index : store.indexManager.listIndexes()) if (index instanceof SASIIndex)
        sasiComponents.add(((SASIIndex) index).getIndex().getComponent());
    Assert.assertFalse(sasiComponents.isEmpty());
    try {
        store.snapshot(snapshotName);
        SnapshotManifest manifest = SnapshotManifest.deserializeFromJsonFile(store.getDirectories().getSnapshotManifestFile(snapshotName));
        Assert.assertFalse(ssTableReaders.isEmpty());
        Assert.assertFalse(manifest.files.isEmpty());
        Assert.assertEquals(ssTableReaders.size(), manifest.files.size());
        Map<Descriptor, Set<Component>> snapshotSSTables = store.getDirectories().sstableLister(Directories.OnTxnErr.IGNORE).snapshots(snapshotName).list();
        long indexSize = 0;
        long tableSize = 0;
        for (SSTableReader sstable : ssTableReaders) {
            File snapshotDirectory = Directories.getSnapshotDirectory(sstable.descriptor, snapshotName);
            Descriptor snapshotSSTable = new Descriptor(snapshotDirectory, sstable.getKeyspaceName(), sstable.getColumnFamilyName(), sstable.descriptor.generation, sstable.descriptor.formatType);
            Set<Component> components = snapshotSSTables.get(snapshotSSTable);
            Assert.assertNotNull(components);
            Assert.assertTrue(components.containsAll(sasiComponents));
            for (Component c : components) {
                Path componentPath = Paths.get(sstable.descriptor + "-" + c.name);
                long componentSize = Files.size(componentPath);
                if (Component.Type.fromRepresentation(c.name) == Component.Type.SECONDARY_INDEX)
                    indexSize += componentSize;
                else
                    tableSize += componentSize;
            }
        }
        TableSnapshot details = store.listSnapshots().get(snapshotName);
        // check that SASI components are included in the computation of snapshot size
        Assert.assertEquals(details.computeTrueSizeBytes(), tableSize + indexSize);
    } finally {
        store.clearSnapshot(snapshotName);
    }
}
Also used : Path(java.nio.file.Path) SSTableSet(org.apache.cassandra.db.lifecycle.SSTableSet) UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) Index(org.apache.cassandra.index.Index) ColumnIndex(org.apache.cassandra.index.sasi.conf.ColumnIndex) SnapshotManifest(org.apache.cassandra.service.snapshot.SnapshotManifest) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) TableSnapshot(org.apache.cassandra.service.snapshot.TableSnapshot) DatabaseDescriptor(org.apache.cassandra.config.DatabaseDescriptor) Descriptor(org.apache.cassandra.io.sstable.Descriptor) Component(org.apache.cassandra.io.sstable.Component) File(org.apache.cassandra.io.util.File) Pair(org.apache.cassandra.utils.Pair)

Example 4 with SnapshotManifest

use of org.apache.cassandra.service.snapshot.SnapshotManifest in project cassandra by apache.

the class ColumnFamilyStore method createSnapshot.

protected TableSnapshot createSnapshot(String tag, boolean ephemeral, DurationSpec ttl, Set<SSTableReader> sstables, Instant creationTime) {
    Set<File> snapshotDirs = sstables.stream().map(s -> Directories.getSnapshotDirectory(s.descriptor, tag).toAbsolute()).filter(// Remove secondary index subdirectory
    dir -> !Directories.isSecondaryIndexFolder(dir)).collect(Collectors.toCollection(HashSet::new));
    // Create and write snapshot manifest
    SnapshotManifest manifest = new SnapshotManifest(mapToDataFilenames(sstables), ttl, creationTime);
    File manifestFile = getDirectories().getSnapshotManifestFile(tag);
    writeSnapshotManifest(manifest, manifestFile);
    // manifest may create empty snapshot dir
    snapshotDirs.add(manifestFile.parent().toAbsolute());
    // Write snapshot schema
    if (!SchemaConstants.isLocalSystemKeyspace(metadata.keyspace) && !SchemaConstants.isReplicatedSystemKeyspace(metadata.keyspace)) {
        File schemaFile = getDirectories().getSnapshotSchemaFile(tag);
        writeSnapshotSchema(schemaFile);
        // schema may create empty snapshot dir
        snapshotDirs.add(schemaFile.parent().toAbsolute());
    }
    // Maybe create ephemeral marker
    if (ephemeral) {
        File ephemeralSnapshotMarker = getDirectories().getNewEphemeralSnapshotMarkerFile(tag);
        createEphemeralSnapshotMarkerFile(tag, ephemeralSnapshotMarker);
        // marker may create empty snapshot dir
        snapshotDirs.add(ephemeralSnapshotMarker.parent().toAbsolute());
    }
    TableSnapshot snapshot = new TableSnapshot(metadata.keyspace, metadata.name, tag, manifest.createdAt, manifest.expiresAt, snapshotDirs, directories::getTrueAllocatedSizeIn);
    StorageService.instance.addSnapshot(snapshot);
    return snapshot;
}
Also used : CachedPartition(org.apache.cassandra.db.partitions.CachedPartition) Sampler(org.apache.cassandra.metrics.Sampler) TableRepairManager(org.apache.cassandra.repair.TableRepairManager) File(org.apache.cassandra.io.util.File) LoggerFactory(org.slf4j.LoggerFactory) CommitLog(org.apache.cassandra.db.commitlog.CommitLog) TimeoutException(java.util.concurrent.TimeoutException) CommitLogPosition(org.apache.cassandra.db.commitlog.CommitLogPosition) com.google.common.util.concurrent(com.google.common.util.concurrent) org.apache.cassandra.utils(org.apache.cassandra.utils) Global.nanoTime(org.apache.cassandra.utils.Clock.Global.nanoTime) CountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) Throwables.merge(org.apache.cassandra.utils.Throwables.merge) SnapshotManifest(org.apache.cassandra.service.snapshot.SnapshotManifest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileOutputStreamPlus(org.apache.cassandra.io.util.FileOutputStreamPlus) CacheService(org.apache.cassandra.service.CacheService) com.google.common.collect(com.google.common.collect) org.apache.cassandra.db.lifecycle(org.apache.cassandra.db.lifecycle) FSWriteError(org.apache.cassandra.io.FSWriteError) TableViews(org.apache.cassandra.db.view.TableViews) org.apache.cassandra.io.sstable.format(org.apache.cassandra.io.sstable.format) TombstoneOption(org.apache.cassandra.schema.CompactionParams.TombstoneOption) NONE(org.apache.cassandra.db.commitlog.CommitLogPosition.NONE) Throwables.maybeFail(org.apache.cassandra.utils.Throwables.maybeFail) Instant(java.time.Instant) org.apache.cassandra.dht(org.apache.cassandra.dht) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) CommitLog.instance(org.apache.cassandra.db.commitlog.CommitLog.instance) UncheckedInterruptedException(org.apache.cassandra.utils.concurrent.UncheckedInterruptedException) AsyncPromise(org.apache.cassandra.utils.concurrent.AsyncPromise) org.apache.cassandra.cache(org.apache.cassandra.cache) Future(org.apache.cassandra.utils.concurrent.Future) FileUtils(org.apache.cassandra.io.util.FileUtils) org.apache.cassandra.concurrent(org.apache.cassandra.concurrent) DataLimits(org.apache.cassandra.db.filter.DataLimits) Pattern(java.util.regex.Pattern) org.apache.cassandra.schema(org.apache.cassandra.schema) DatabaseDescriptor.getFlushWriters(org.apache.cassandra.config.DatabaseDescriptor.getFlushWriters) org.apache.cassandra.config(org.apache.cassandra.config) java.util(java.util) PartitionUpdate(org.apache.cassandra.db.partitions.PartitionUpdate) PendingStat(org.apache.cassandra.repair.consistent.admin.PendingStat) CassandraStreamManager(org.apache.cassandra.db.streaming.CassandraStreamManager) NANOSECONDS(java.util.concurrent.TimeUnit.NANOSECONDS) Global.currentTimeMillis(org.apache.cassandra.utils.Clock.Global.currentTimeMillis) Range(org.apache.cassandra.dht.Range) Callable(java.util.concurrent.Callable) Constructor(java.lang.reflect.Constructor) AtomicReference(java.util.concurrent.atomic.AtomicReference) OpOrder(org.apache.cassandra.utils.concurrent.OpOrder) ActiveRepairService(org.apache.cassandra.service.ActiveRepairService) Promise(org.apache.cassandra.utils.concurrent.Promise) SSTableMultiWriter(org.apache.cassandra.io.sstable.SSTableMultiWriter) TableMetrics(org.apache.cassandra.metrics.TableMetrics) CountDownLatch.newCountDownLatch(org.apache.cassandra.utils.concurrent.CountDownLatch.newCountDownLatch) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) Component(org.apache.cassandra.io.sstable.Component) UpdateTransaction(org.apache.cassandra.index.transactions.UpdateTransaction) Descriptor(org.apache.cassandra.io.sstable.Descriptor) MetadataCollector(org.apache.cassandra.io.sstable.metadata.MetadataCollector) Refs(org.apache.cassandra.utils.concurrent.Refs) CleanupSummary(org.apache.cassandra.repair.consistent.admin.CleanupSummary) ExecutorService(java.util.concurrent.ExecutorService) Throwables.propagate(com.google.common.base.Throwables.propagate) PrintStream(java.io.PrintStream) Logger(org.slf4j.Logger) Files(java.nio.file.Files) com.google.common.base(com.google.common.base) SecondaryIndexManager(org.apache.cassandra.index.SecondaryIndexManager) MemtableAllocator(org.apache.cassandra.utils.memory.MemtableAllocator) javax.management(javax.management) CassandraTableRepairManager(org.apache.cassandra.db.repair.CassandraTableRepairManager) Throwables(com.google.common.base.Throwables) StorageService(org.apache.cassandra.service.StorageService) IOException(java.io.IOException) javax.management.openmbean(javax.management.openmbean) SamplerType(org.apache.cassandra.metrics.Sampler.SamplerType) org.apache.cassandra.db.compaction(org.apache.cassandra.db.compaction) TableStreamManager(org.apache.cassandra.streaming.TableStreamManager) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CellPath(org.apache.cassandra.db.rows.CellPath) FSReadError(org.apache.cassandra.io.FSReadError) CassandraIndex(org.apache.cassandra.index.internal.CassandraIndex) Sample(org.apache.cassandra.metrics.Sampler.Sample) Global.executorFactory(org.apache.cassandra.concurrent.ExecutorFactory.Global.executorFactory) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ClusteringIndexFilter(org.apache.cassandra.db.filter.ClusteringIndexFilter) TableSnapshot(org.apache.cassandra.service.snapshot.TableSnapshot) StartupException(org.apache.cassandra.exceptions.StartupException) SnapshotManifest(org.apache.cassandra.service.snapshot.SnapshotManifest) TableSnapshot(org.apache.cassandra.service.snapshot.TableSnapshot) File(org.apache.cassandra.io.util.File)

Example 5 with SnapshotManifest

use of org.apache.cassandra.service.snapshot.SnapshotManifest 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)

Aggregations

File (org.apache.cassandra.io.util.File)6 SnapshotManifest (org.apache.cassandra.service.snapshot.SnapshotManifest)6 Descriptor (org.apache.cassandra.io.sstable.Descriptor)4 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)3 DurationSpec (org.apache.cassandra.config.DurationSpec)2 TableSnapshot (org.apache.cassandra.service.snapshot.TableSnapshot)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 com.google.common.base (com.google.common.base)1 Throwables (com.google.common.base.Throwables)1 Throwables.propagate (com.google.common.base.Throwables.propagate)1 com.google.common.collect (com.google.common.collect)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 com.google.common.util.concurrent (com.google.common.util.concurrent)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ByteBuffer (java.nio.ByteBuffer)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1