Search in sources :

Example 6 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 KB 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))) 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 = 0;
    for (List<SSTableReader> level : unrepaired.manifest.generations) sstableCount += level.size();
    // we only have unrepaired sstables:
    assertEquals(sstableCount, cfs.getLiveSSTables().size());
    SSTableReader sstable1 = unrepaired.manifest.generations[2].get(0);
    SSTableReader sstable2 = unrepaired.manifest.generations[1].get(0);
    sstable1.descriptor.getMetadataSerializer().mutateRepaired(sstable1.descriptor, System.currentTimeMillis(), null);
    sstable1.reloadSSTableMetadata();
    assertTrue(sstable1.isRepaired());
    manager.handleNotification(new SSTableRepairStatusChanged(Arrays.asList(sstable1)), this);
    int repairedSSTableCount = 0;
    for (List<SSTableReader> level : repaired.manifest.generations) repairedSSTableCount += level.size();
    assertEquals(1, repairedSSTableCount);
    // make sure the repaired sstable ends up in the same level in the repaired manifest:
    assertTrue(repaired.manifest.generations[2].contains(sstable1));
    // and that it is gone from unrepaired
    assertFalse(unrepaired.manifest.generations[2].contains(sstable1));
    unrepaired.removeSSTable(sstable2);
    manager.handleNotification(new SSTableAddedNotification(singleton(sstable2)), this);
    assertTrue(unrepaired.manifest.getLevel(1).contains(sstable2));
    assertFalse(repaired.manifest.getLevel(1).contains(sstable2));
}
Also used : UpdateBuilder(org.apache.cassandra.UpdateBuilder) SSTableAddedNotification(org.apache.cassandra.notifications.SSTableAddedNotification) ByteBuffer(java.nio.ByteBuffer) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) SSTableRepairStatusChanged(org.apache.cassandra.notifications.SSTableRepairStatusChanged) Random(java.util.Random) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 7 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 KB 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.add(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 8 with UpdateBuilder

use of org.apache.cassandra.UpdateBuilder in project cassandra by apache.

the class PartitionUpdateTest method testOperationCount.

@Test
public void testOperationCount() {
    createTable("CREATE TABLE %s (key text, clustering int, a int, s int static, PRIMARY KEY(key, clustering))");
    TableMetadata cfm = currentTableMetadata();
    UpdateBuilder builder = UpdateBuilder.create(cfm, "key0");
    Assert.assertEquals(0, builder.build().operationCount());
    Assert.assertEquals(1, builder.newRow(1).add("a", 1).build().operationCount());
    builder = UpdateBuilder.create(cfm, "key0");
    Assert.assertEquals(1, builder.newRow().add("s", 1).build().operationCount());
    builder = UpdateBuilder.create(cfm, "key0");
    builder.newRow().add("s", 1);
    builder.newRow(1).add("a", 1);
    Assert.assertEquals(2, builder.build().operationCount());
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) UpdateBuilder(org.apache.cassandra.UpdateBuilder) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Test(org.junit.Test)

Example 9 with UpdateBuilder

use of org.apache.cassandra.UpdateBuilder in project cassandra by apache.

the class LongCompactionsTest method testCompaction.

protected void testCompaction(int sstableCount, int partitionsPerSSTable, int rowsPerPartition) throws Exception {
    CompactionManager.instance.disableAutoCompaction();
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore store = keyspace.getColumnFamilyStore("Standard1");
    ArrayList<SSTableReader> sstables = new ArrayList<>();
    for (int k = 0; k < sstableCount; k++) {
        SortedMap<String, PartitionUpdate> rows = new TreeMap<>();
        for (int j = 0; j < partitionsPerSSTable; j++) {
            String key = String.valueOf(j);
            // last sstable has highest timestamps
            UpdateBuilder builder = UpdateBuilder.create(store.metadata(), String.valueOf(j)).withTimestamp(k);
            for (int i = 0; i < rowsPerPartition; i++) builder.newRow(String.valueOf(i)).add("val", String.valueOf(i));
            rows.put(key, builder.build());
        }
        Collection<SSTableReader> readers = SSTableUtils.prepare().write(rows);
        sstables.addAll(readers);
        store.addSSTables(readers);
    }
    // give garbage collection a bit of time to catch up
    Thread.sleep(1000);
    long start = System.nanoTime();
    final int gcBefore = (int) (System.currentTimeMillis() / 1000) - Schema.instance.getTableMetadata(KEYSPACE1, "Standard1").params.gcGraceSeconds;
    try (LifecycleTransaction txn = store.getTracker().tryModify(sstables, OperationType.COMPACTION)) {
        assert txn != null : "Cannot markCompacting all sstables";
        new CompactionTask(store, txn, gcBefore).execute(null);
    }
    System.out.println(String.format("%s: sstables=%d rowsper=%d colsper=%d: %d ms", this.getClass().getName(), sstableCount, partitionsPerSSTable, rowsPerPartition, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)));
}
Also used : LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) UpdateBuilder(org.apache.cassandra.UpdateBuilder) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader)

Example 10 with UpdateBuilder

use of org.apache.cassandra.UpdateBuilder in project cassandra by apache.

the class LongLeveledCompactionStrategyTest method testLeveledScanner.

@Test
public void testLeveledScanner() throws Exception {
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore store = keyspace.getColumnFamilyStore(CF_STANDARDLVL2);
    // 100 KB 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 = 128;
    int columns = 10;
    // Adds enough data to trigger multiple sstable per level
    for (int r = 0; r < rows; r++) {
        DecoratedKey key = Util.dk(String.valueOf(r));
        UpdateBuilder builder = UpdateBuilder.create(store.metadata(), key);
        for (int c = 0; c < columns; c++) builder.newRow("column" + c).add("val", value);
        Mutation rm = new Mutation(builder.build());
        rm.apply();
        store.forceBlockingFlush();
    }
    LeveledCompactionStrategyTest.waitForLeveling(store);
    store.disableAutoCompaction();
    CompactionStrategyManager mgr = store.getCompactionStrategyManager();
    LeveledCompactionStrategy lcs = (LeveledCompactionStrategy) mgr.getStrategies().get(1).get(0);
    // 10 KB value
    value = ByteBuffer.wrap(new byte[10 * 1024]);
    // Adds 10 partitions
    for (int r = 0; r < 10; r++) {
        DecoratedKey key = Util.dk(String.valueOf(r));
        UpdateBuilder builder = UpdateBuilder.create(store.metadata(), key);
        for (int c = 0; c < 10; c++) builder.newRow("column" + c).add("val", value);
        Mutation rm = new Mutation(builder.build());
        rm.apply();
    }
    //Flush sstable
    store.forceBlockingFlush();
    store.runWithCompactionsDisabled(new Callable<Void>() {

        public Void call() throws Exception {
            Iterable<SSTableReader> allSSTables = store.getSSTables(SSTableSet.LIVE);
            for (SSTableReader sstable : allSSTables) {
                if (sstable.getSSTableLevel() == 0) {
                    System.out.println("Mutating L0-SSTABLE level to L1 to simulate a bug: " + sstable.getFilename());
                    sstable.descriptor.getMetadataSerializer().mutateLevel(sstable.descriptor, 1);
                    sstable.reloadSSTableMetadata();
                }
            }
            try (AbstractCompactionStrategy.ScannerList scannerList = lcs.getScanners(Lists.newArrayList(allSSTables))) {
                //Verify that leveled scanners will always iterate in ascending order (CASSANDRA-9935)
                for (ISSTableScanner scanner : scannerList.scanners) {
                    DecoratedKey lastKey = null;
                    while (scanner.hasNext()) {
                        UnfilteredRowIterator row = scanner.next();
                        if (lastKey != null) {
                            assertTrue("row " + row.partitionKey() + " received out of order wrt " + lastKey, row.partitionKey().compareTo(lastKey) >= 0);
                        }
                        lastKey = row.partitionKey();
                    }
                }
            }
            return null;
        }
    }, true, true);
}
Also used : ISSTableScanner(org.apache.cassandra.io.sstable.ISSTableScanner) UnfilteredRowIterator(org.apache.cassandra.db.rows.UnfilteredRowIterator) UpdateBuilder(org.apache.cassandra.UpdateBuilder) ByteBuffer(java.nio.ByteBuffer) ConfigurationException(org.apache.cassandra.exceptions.ConfigurationException) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Test(org.junit.Test)

Aggregations

UpdateBuilder (org.apache.cassandra.UpdateBuilder)20 Test (org.junit.Test)17 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)11 ByteBuffer (java.nio.ByteBuffer)10 Random (java.util.Random)4 File (java.io.File)2 ArrayList (java.util.ArrayList)2 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)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 TableMetadata (org.apache.cassandra.schema.TableMetadata)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 List (java.util.List)1 UUID (java.util.UUID)1 ExecutionException (java.util.concurrent.ExecutionException)1