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);
}
}
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;
}
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());
}
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"));
}
Aggregations