Search in sources :

Example 1 with UpdateBuilder

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));
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) SSTableRepairStatusChanged(org.apache.cassandra.notifications.SSTableRepairStatusChanged) Random(java.util.Random) UpdateBuilder(org.apache.cassandra.UpdateBuilder) List(java.util.List) ArrayList(java.util.ArrayList) SSTableAddedNotification(org.apache.cassandra.notifications.SSTableAddedNotification) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 2 with UpdateBuilder

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]);
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) UpdateBuilder(org.apache.cassandra.UpdateBuilder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with UpdateBuilder

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));
}
Also used : ISSTableScanner(org.apache.cassandra.io.sstable.ISSTableScanner) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Random(java.util.Random) UpdateBuilder(org.apache.cassandra.UpdateBuilder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with UpdateBuilder

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();
}
Also used : UpdateBuilder(org.apache.cassandra.UpdateBuilder)

Example 5 with UpdateBuilder

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());
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) StubIndex(org.apache.cassandra.index.StubIndex) java.util(java.util) CompactionManager(org.apache.cassandra.db.compaction.CompactionManager) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) BeforeClass(org.junit.BeforeClass) IndexTarget(org.apache.cassandra.cql3.statements.schema.IndexTarget) ByteBuffer(java.nio.ByteBuffer) Iterators(com.google.common.collect.Iterators) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) org.apache.cassandra.db.rows(org.apache.cassandra.db.rows) Int32Type(org.apache.cassandra.db.marshal.Int32Type) UTF8Type(org.apache.cassandra.db.marshal.UTF8Type) Future(java.util.concurrent.Future) MigrationManager(org.apache.cassandra.schema.MigrationManager) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) ColumnFilter(org.apache.cassandra.db.filter.ColumnFilter) UpdateBuilder(org.apache.cassandra.UpdateBuilder) org.apache.cassandra.db.partitions(org.apache.cassandra.db.partitions) SchemaLoader.standardCFMD(org.apache.cassandra.SchemaLoader.standardCFMD) FBUtilities(org.apache.cassandra.utils.FBUtilities) IndexMetadata(org.apache.cassandra.schema.IndexMetadata) ImmutableMap(com.google.common.collect.ImmutableMap) Util(org.apache.cassandra.Util) ByteBufferUtil(org.apache.cassandra.utils.ByteBufferUtil) KeyspaceParams(org.apache.cassandra.schema.KeyspaceParams) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ExecutionException(java.util.concurrent.ExecutionException) SchemaLoader(org.apache.cassandra.SchemaLoader) Assert.assertFalse(org.junit.Assert.assertFalse) TableMetadata(org.apache.cassandra.schema.TableMetadata) StatsMetadata(org.apache.cassandra.io.sstable.metadata.StatsMetadata) StubIndex(org.apache.cassandra.index.StubIndex) Assert.assertEquals(org.junit.Assert.assertEquals) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) IndexTarget(org.apache.cassandra.cql3.statements.schema.IndexTarget) UpdateBuilder(org.apache.cassandra.UpdateBuilder) ByteBuffer(java.nio.ByteBuffer) IndexMetadata(org.apache.cassandra.schema.IndexMetadata) Test(org.junit.Test)

Aggregations

UpdateBuilder (org.apache.cassandra.UpdateBuilder)25 Test (org.junit.Test)20 ByteBuffer (java.nio.ByteBuffer)12 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)12 Random (java.util.Random)6 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)4 TableMetadata (org.apache.cassandra.schema.TableMetadata)3 ArrayList (java.util.ArrayList)2 Range (org.apache.cassandra.dht.Range)2 Token (org.apache.cassandra.dht.Token)2 ConfigurationException (org.apache.cassandra.exceptions.ConfigurationException)2 ISSTableScanner (org.apache.cassandra.io.sstable.ISSTableScanner)2 File (org.apache.cassandra.io.util.File)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Iterators (com.google.common.collect.Iterators)1 java.util (java.util)1 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 UUID (java.util.UUID)1