Search in sources :

Example 6 with TableSnapshot

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

use of org.apache.cassandra.service.snapshot.TableSnapshot 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 8 with TableSnapshot

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

the class DirectoriesTest method testListSnapshots.

@Test
public void testListSnapshots() throws Exception {
    // Initial state
    TableMetadata fakeTable = createFakeTable(TABLE_NAME);
    Directories directories = new Directories(fakeTable, toDataDirectories(tempDataDir));
    assertThat(directories.listSnapshots()).isEmpty();
    // Create snapshot with and without manifest
    FakeSnapshot snapshot1 = createFakeSnapshot(fakeTable, SNAPSHOT1, true);
    FakeSnapshot snapshot2 = createFakeSnapshot(fakeTable, SNAPSHOT2, false);
    // Both snapshots should be present
    Map<String, TableSnapshot> snapshots = directories.listSnapshots();
    assertThat(snapshots.keySet()).isEqualTo(Sets.newHashSet(SNAPSHOT1, SNAPSHOT2));
    assertThat(snapshots.get(SNAPSHOT1)).isEqualTo(snapshot1.asTableSnapshot());
    assertThat(snapshots.get(SNAPSHOT2)).isEqualTo(snapshot2.asTableSnapshot());
    // Now remove snapshot1
    FileUtils.deleteRecursive(snapshot1.snapshotDir);
    // Only snapshot 2 should be present
    snapshots = directories.listSnapshots();
    assertThat(snapshots.keySet()).isEqualTo(Sets.newHashSet(SNAPSHOT2));
    assertThat(snapshots.get(SNAPSHOT2)).isEqualTo(snapshot2.asTableSnapshot());
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) DataDirectories(org.apache.cassandra.db.Directories.DataDirectories) TableSnapshot(org.apache.cassandra.service.snapshot.TableSnapshot) Test(org.junit.Test)

Example 9 with TableSnapshot

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

the class ColumnFamilyStoreTest method createSnapshotAndDelete.

private void createSnapshotAndDelete(String ks, String table, boolean writeData) {
    ColumnFamilyStore cfs = Keyspace.open(ks).getColumnFamilyStore(table);
    if (writeData) {
        writeData(cfs);
    }
    TableSnapshot snapshot = cfs.snapshot("basic");
    assertThat(snapshot.exists()).isTrue();
    assertThat(cfs.listSnapshots().containsKey("basic")).isTrue();
    assertThat(cfs.listSnapshots().get("basic")).isEqualTo(snapshot);
    snapshot.getDirectories().forEach(FileUtils::deleteRecursive);
    assertThat(snapshot.exists()).isFalse();
    assertFalse(cfs.listSnapshots().containsKey("basic"));
}
Also used : FileUtils(org.apache.cassandra.io.util.FileUtils) TableSnapshot(org.apache.cassandra.service.snapshot.TableSnapshot)

Aggregations

TableSnapshot (org.apache.cassandra.service.snapshot.TableSnapshot)9 File (org.apache.cassandra.io.util.File)4 Instant (java.time.Instant)3 Descriptor (org.apache.cassandra.io.sstable.Descriptor)3 SnapshotManifest (org.apache.cassandra.service.snapshot.SnapshotManifest)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 com.google.common.base (com.google.common.base)2 Throwables (com.google.common.base.Throwables)2 Throwables.propagate (com.google.common.base.Throwables.propagate)2 com.google.common.collect (com.google.common.collect)2 com.google.common.util.concurrent (com.google.common.util.concurrent)2 IOException (java.io.IOException)2 PrintStream (java.io.PrintStream)2 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ByteBuffer (java.nio.ByteBuffer)2 Files (java.nio.file.Files)2 java.util (java.util)2 Objects (java.util.Objects)2 Callable (java.util.concurrent.Callable)2