use of org.apache.cassandra.UpdateBuilder in project cassandra by apache.
the class LeveledCompactionStrategyTest method testNewRepairedSSTable.
@Test
public void testNewRepairedSSTable() throws Exception {
byte[] b = new byte[100 * 1024];
new Random().nextBytes(b);
// 100 KiB value, make it easy to have multiple files
ByteBuffer value = ByteBuffer.wrap(b);
// Enough data to have a level 1 and 2
int rows = 40;
int columns = 20;
// Adds enough data to trigger multiple sstable per level
for (int r = 0; r < rows; r++) {
UpdateBuilder update = UpdateBuilder.create(cfs.metadata(), String.valueOf(r));
for (int c = 0; c < columns; c++) update.newRow("column" + c).add("val", value);
update.applyUnsafe();
cfs.forceBlockingFlush();
}
waitForLeveling(cfs);
cfs.disableAutoCompaction();
while (CompactionManager.instance.isCompacting(Arrays.asList(cfs), (sstable) -> true)) Thread.sleep(100);
CompactionStrategyManager manager = cfs.getCompactionStrategyManager();
List<List<AbstractCompactionStrategy>> strategies = manager.getStrategies();
LeveledCompactionStrategy repaired = (LeveledCompactionStrategy) strategies.get(0).get(0);
LeveledCompactionStrategy unrepaired = (LeveledCompactionStrategy) strategies.get(1).get(0);
assertEquals(0, repaired.manifest.getLevelCount());
assertEquals(2, unrepaired.manifest.getLevelCount());
assertTrue(manager.getSSTableCountPerLevel()[1] > 0);
assertTrue(manager.getSSTableCountPerLevel()[2] > 0);
for (SSTableReader sstable : cfs.getLiveSSTables()) assertFalse(sstable.isRepaired());
int sstableCount = unrepaired.manifest.getSSTables().size();
// we only have unrepaired sstables:
assertEquals(sstableCount, cfs.getLiveSSTables().size());
SSTableReader sstable1 = unrepaired.manifest.getLevel(2).iterator().next();
SSTableReader sstable2 = unrepaired.manifest.getLevel(1).iterator().next();
sstable1.descriptor.getMetadataSerializer().mutateRepairMetadata(sstable1.descriptor, System.currentTimeMillis(), null, false);
sstable1.reloadSSTableMetadata();
assertTrue(sstable1.isRepaired());
manager.handleNotification(new SSTableRepairStatusChanged(Arrays.asList(sstable1)), this);
int repairedSSTableCount = repaired.manifest.getSSTables().size();
assertEquals(1, repairedSSTableCount);
// make sure the repaired sstable ends up in the same level in the repaired manifest:
assertTrue(repaired.manifest.getLevel(2).contains(sstable1));
// and that it is gone from unrepaired
assertFalse(unrepaired.manifest.getLevel(2).contains(sstable1));
unrepaired.removeSSTable(sstable2);
manager.handleNotification(new SSTableAddedNotification(singleton(sstable2), null), this);
assertTrue(unrepaired.manifest.getLevel(1).contains(sstable2));
assertFalse(repaired.manifest.getLevel(1).contains(sstable2));
}
use of org.apache.cassandra.UpdateBuilder in project cassandra by apache.
the class LeveledCompactionStrategyTest method testMutateLevel.
@Test
public void testMutateLevel() throws Exception {
cfs.disableAutoCompaction();
// 100 KiB value, make it easy to have multiple files
ByteBuffer value = ByteBuffer.wrap(new byte[100 * 1024]);
// Enough data to have a level 1 and 2
int rows = 40;
int columns = 20;
// Adds enough data to trigger multiple sstable per level
for (int r = 0; r < rows; r++) {
UpdateBuilder update = UpdateBuilder.create(cfs.metadata(), String.valueOf(r));
for (int c = 0; c < columns; c++) update.newRow("column" + c).add("val", value);
update.applyUnsafe();
cfs.forceBlockingFlush();
}
cfs.forceBlockingFlush();
LeveledCompactionStrategy strategy = (LeveledCompactionStrategy) cfs.getCompactionStrategyManager().getStrategies().get(1).get(0);
cfs.forceMajorCompaction();
for (SSTableReader s : cfs.getLiveSSTables()) {
assertTrue(s.getSSTableLevel() != 6 && s.getSSTableLevel() > 0);
strategy.manifest.remove(s);
s.descriptor.getMetadataSerializer().mutateLevel(s.descriptor, 6);
s.reloadSSTableMetadata();
strategy.manifest.addSSTables(Collections.singleton(s));
}
// verify that all sstables in the changed set is level 6
for (SSTableReader s : cfs.getLiveSSTables()) assertEquals(6, s.getSSTableLevel());
int[] levels = strategy.manifest.getAllLevelSize();
// verify that the manifest has correct amount of sstables
assertEquals(cfs.getLiveSSTables().size(), levels[6]);
}
use of org.apache.cassandra.UpdateBuilder in project cassandra by apache.
the class LeveledCompactionStrategyTest method testCompactionProgress.
@Test
public void testCompactionProgress() throws Exception {
// make sure we have SSTables in L1
byte[] b = new byte[100 * 1024];
new Random().nextBytes(b);
ByteBuffer value = ByteBuffer.wrap(b);
int rows = 2;
int columns = 10;
for (int r = 0; r < rows; r++) {
UpdateBuilder update = UpdateBuilder.create(cfs.metadata(), String.valueOf(r));
for (int c = 0; c < columns; c++) update.newRow("column" + c).add("val", value);
update.applyUnsafe();
cfs.forceBlockingFlush();
}
waitForLeveling(cfs);
LeveledCompactionStrategy strategy = (LeveledCompactionStrategy) cfs.getCompactionStrategyManager().getStrategies().get(1).get(0);
assert strategy.getLevelSize(1) > 0;
// get LeveledScanner for level 1 sstables
Collection<SSTableReader> sstables = strategy.manifest.getLevel(1);
List<ISSTableScanner> scanners = strategy.getScanners(sstables).scanners;
// should be one per level
assertEquals(1, scanners.size());
ISSTableScanner scanner = scanners.get(0);
// scan through to the end
while (scanner.hasNext()) scanner.next();
// scanner.getCurrentPosition should be equal to total bytes of L1 sstables
assertEquals(scanner.getCurrentPosition(), SSTableReader.getTotalUncompressedBytes(sstables));
}
use of org.apache.cassandra.UpdateBuilder in project cassandra by apache.
the class ScrubTest method fillIndexCF.
public static void fillIndexCF(ColumnFamilyStore cfs, boolean composite, long... values) {
assertEquals(0, values.length % 2);
for (int i = 0; i < values.length; i += 2) {
UpdateBuilder builder = UpdateBuilder.create(cfs.metadata(), String.valueOf(i));
if (composite) {
builder.newRow("c" + i).add(COL_INDEX, values[i]).add(COL_NON_INDEX, values[i + 1]);
} else {
builder.newRow().add(COL_INDEX, values[i]).add(COL_NON_INDEX, values[i + 1]);
}
new Mutation(builder.build()).applyUnsafe();
}
cfs.forceBlockingFlush();
}
use of org.apache.cassandra.UpdateBuilder in project cassandra by apache.
the class RangeTombstoneTest method testRowWithRangeTombstonesUpdatesSecondaryIndex.
@Test
public void testRowWithRangeTombstonesUpdatesSecondaryIndex() throws Exception {
Keyspace table = Keyspace.open(KSNAME);
ColumnFamilyStore cfs = table.getColumnFamilyStore(CFNAME);
ByteBuffer key = ByteBufferUtil.bytes("k5");
ByteBuffer indexedColumnName = ByteBufferUtil.bytes("val");
cfs.truncateBlocking();
cfs.disableAutoCompaction();
ColumnMetadata cd = cfs.metadata().getColumn(indexedColumnName).copy();
IndexMetadata indexDef = IndexMetadata.fromIndexTargets(Collections.singletonList(new IndexTarget(cd.name, IndexTarget.Type.VALUES)), "test_index", IndexMetadata.Kind.CUSTOM, ImmutableMap.of(IndexTarget.CUSTOM_INDEX_OPTION_NAME, StubIndex.class.getName()));
TableMetadata current = cfs.metadata();
if (!current.indexes.get("test_index").isPresent()) {
TableMetadata updated = current.unbuild().indexes(current.indexes.with(indexDef)).build();
MigrationManager.announceTableUpdate(updated, true);
}
Future<?> rebuild = cfs.indexManager.addIndex(indexDef, false);
// If rebuild there is, wait for the rebuild to finish so it doesn't race with the following insertions
if (rebuild != null)
rebuild.get();
StubIndex index = (StubIndex) cfs.indexManager.listIndexes().stream().filter(i -> "test_index".equals(i.getIndexMetadata().name)).findFirst().orElseThrow(() -> new RuntimeException(new AssertionError("Index not found")));
index.reset();
UpdateBuilder builder = UpdateBuilder.create(cfs.metadata(), key).withTimestamp(0);
for (int i = 0; i < 10; i++) builder.newRow(i).add("val", i);
builder.applyUnsafe();
cfs.forceBlockingFlush();
new RowUpdateBuilder(cfs.metadata(), 0, key).addRangeTombstone(0, 7).build().applyUnsafe();
cfs.forceBlockingFlush();
assertEquals(10, index.rowsInserted.size());
CompactionManager.instance.performMaximal(cfs, false);
// compacted down to single sstable
assertEquals(1, cfs.getLiveSSTables().size());
assertEquals(8, index.rowsDeleted.size());
}
Aggregations