Search in sources :

Example 26 with RowUpdateBuilder

use of org.apache.cassandra.db.RowUpdateBuilder in project cassandra by apache.

the class TimeWindowCompactionStrategyTest method testPrepBucket.

@Test
public void testPrepBucket() {
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF_STANDARD1);
    cfs.truncateBlocking();
    cfs.disableAutoCompaction();
    ByteBuffer value = ByteBuffer.wrap(new byte[100]);
    Long tstamp = System.currentTimeMillis();
    Long tstamp2 = tstamp - (2L * 3600L * 1000L);
    // create 5 sstables
    for (int r = 0; r < 3; r++) {
        DecoratedKey key = Util.dk(String.valueOf(r));
        new RowUpdateBuilder(cfs.metadata(), r, key.getKey()).clustering("column").add("val", value).build().applyUnsafe();
        cfs.forceBlockingFlush();
    }
    // Decrement the timestamp to simulate a timestamp in the past hour
    for (int r = 3; r < 5; r++) {
        // And add progressively more cells into each sstable
        DecoratedKey key = Util.dk(String.valueOf(r));
        new RowUpdateBuilder(cfs.metadata(), r, key.getKey()).clustering("column").add("val", value).build().applyUnsafe();
        cfs.forceBlockingFlush();
    }
    cfs.forceBlockingFlush();
    HashMultimap<Long, SSTableReader> buckets = HashMultimap.create();
    List<SSTableReader> sstrs = new ArrayList<>(cfs.getLiveSSTables());
    // We'll put 3 sstables into the newest bucket
    for (int i = 0; i < 3; i++) {
        Pair<Long, Long> bounds = getWindowBoundsInMillis(TimeUnit.HOURS, 1, tstamp);
        buckets.put(bounds.left, sstrs.get(i));
    }
    List<SSTableReader> newBucket = newestBucket(buckets, 4, 32, new SizeTieredCompactionStrategyOptions(), getWindowBoundsInMillis(TimeUnit.HOURS, 1, System.currentTimeMillis()).left);
    assertTrue("incoming bucket should not be accepted when it has below the min threshold SSTables", newBucket.isEmpty());
    newBucket = newestBucket(buckets, 2, 32, new SizeTieredCompactionStrategyOptions(), getWindowBoundsInMillis(TimeUnit.HOURS, 1, System.currentTimeMillis()).left);
    assertTrue("incoming bucket should be accepted when it is larger than the min threshold SSTables", !newBucket.isEmpty());
    // And 2 into the second bucket (1 hour back)
    for (int i = 3; i < 5; i++) {
        Pair<Long, Long> bounds = getWindowBoundsInMillis(TimeUnit.HOURS, 1, tstamp2);
        buckets.put(bounds.left, sstrs.get(i));
    }
    assertEquals("an sstable with a single value should have equal min/max timestamps", sstrs.get(0).getMinTimestamp(), sstrs.get(0).getMaxTimestamp());
    assertEquals("an sstable with a single value should have equal min/max timestamps", sstrs.get(1).getMinTimestamp(), sstrs.get(1).getMaxTimestamp());
    assertEquals("an sstable with a single value should have equal min/max timestamps", sstrs.get(2).getMinTimestamp(), sstrs.get(2).getMaxTimestamp());
    // Test trim
    int numSSTables = 40;
    for (int r = 5; r < numSSTables; r++) {
        DecoratedKey key = Util.dk(String.valueOf(r));
        for (int i = 0; i < r; i++) {
            new RowUpdateBuilder(cfs.metadata(), tstamp + r, key.getKey()).clustering("column").add("val", value).build().applyUnsafe();
        }
        cfs.forceBlockingFlush();
    }
    // Reset the buckets, overfill it now
    sstrs = new ArrayList<>(cfs.getLiveSSTables());
    for (int i = 0; i < 40; i++) {
        Pair<Long, Long> bounds = getWindowBoundsInMillis(TimeUnit.HOURS, 1, sstrs.get(i).getMaxTimestamp());
        buckets.put(bounds.left, sstrs.get(i));
    }
    newBucket = newestBucket(buckets, 4, 32, new SizeTieredCompactionStrategyOptions(), getWindowBoundsInMillis(TimeUnit.HOURS, 1, System.currentTimeMillis()).left);
    assertEquals("new bucket should be trimmed to max threshold of 32", newBucket.size(), 32);
}
Also used : DecoratedKey(org.apache.cassandra.db.DecoratedKey) ArrayList(java.util.ArrayList) ByteBuffer(java.nio.ByteBuffer) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Example 27 with RowUpdateBuilder

use of org.apache.cassandra.db.RowUpdateBuilder in project cassandra by apache.

the class TimeWindowCompactionStrategyTest method testDropExpiredSSTables.

@Test
public void testDropExpiredSSTables() throws InterruptedException {
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF_STANDARD1);
    cfs.truncateBlocking();
    cfs.disableAutoCompaction();
    ByteBuffer value = ByteBuffer.wrap(new byte[100]);
    // create 2 sstables
    DecoratedKey key = Util.dk(String.valueOf("expired"));
    new RowUpdateBuilder(cfs.metadata(), System.currentTimeMillis(), 1, key.getKey()).clustering("column").add("val", value).build().applyUnsafe();
    cfs.forceBlockingFlush();
    SSTableReader expiredSSTable = cfs.getLiveSSTables().iterator().next();
    Thread.sleep(10);
    key = Util.dk(String.valueOf("nonexpired"));
    new RowUpdateBuilder(cfs.metadata(), System.currentTimeMillis(), key.getKey()).clustering("column").add("val", value).build().applyUnsafe();
    cfs.forceBlockingFlush();
    assertEquals(cfs.getLiveSSTables().size(), 2);
    Map<String, String> options = new HashMap<>();
    options.put(TimeWindowCompactionStrategyOptions.COMPACTION_WINDOW_SIZE_KEY, "30");
    options.put(TimeWindowCompactionStrategyOptions.COMPACTION_WINDOW_UNIT_KEY, "SECONDS");
    options.put(TimeWindowCompactionStrategyOptions.TIMESTAMP_RESOLUTION_KEY, "MILLISECONDS");
    options.put(TimeWindowCompactionStrategyOptions.EXPIRED_SSTABLE_CHECK_FREQUENCY_SECONDS_KEY, "0");
    TimeWindowCompactionStrategy twcs = new TimeWindowCompactionStrategy(cfs, options);
    for (SSTableReader sstable : cfs.getLiveSSTables()) twcs.addSSTable(sstable);
    twcs.startup();
    assertNull(twcs.getNextBackgroundTask((int) (System.currentTimeMillis() / 1000)));
    Thread.sleep(2000);
    AbstractCompactionTask t = twcs.getNextBackgroundTask((int) (System.currentTimeMillis() / 1000));
    assertNotNull(t);
    assertEquals(1, Iterables.size(t.transaction.originals()));
    SSTableReader sstable = t.transaction.originals().iterator().next();
    assertEquals(sstable, expiredSSTable);
    t.transaction.abort();
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) HashMap(java.util.HashMap) Keyspace(org.apache.cassandra.db.Keyspace) DecoratedKey(org.apache.cassandra.db.DecoratedKey) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 28 with RowUpdateBuilder

use of org.apache.cassandra.db.RowUpdateBuilder in project cassandra by apache.

the class HintMessageTest method testSerializer.

@Test
public void testSerializer() throws IOException {
    SchemaLoader.prepareServer();
    SchemaLoader.createKeyspace(KEYSPACE, KeyspaceParams.simple(1), SchemaLoader.standardCFMD(KEYSPACE, TABLE));
    UUID hostId = UUID.randomUUID();
    long now = FBUtilities.timestampMicros();
    TableMetadata table = Schema.instance.getTableMetadata(KEYSPACE, TABLE);
    Mutation mutation = new RowUpdateBuilder(table, now, bytes("key")).clustering("column").add("val", "val" + 1234).build();
    Hint hint = Hint.create(mutation, now / 1000);
    HintMessage message = new HintMessage(hostId, hint);
    // serialize
    int serializedSize = (int) HintMessage.serializer.serializedSize(message, MessagingService.current_version);
    DataOutputBuffer dob = new DataOutputBuffer();
    HintMessage.serializer.serialize(message, dob, MessagingService.current_version);
    assertEquals(serializedSize, dob.getLength());
    // deserialize
    DataInputPlus di = new DataInputBuffer(dob.buffer(), true);
    HintMessage deserializedMessage = HintMessage.serializer.deserialize(di, MessagingService.current_version);
    // compare before/after
    assertEquals(hostId, deserializedMessage.hostId);
    assertHintsEqual(message.hint, deserializedMessage.hint);
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) DataInputBuffer(org.apache.cassandra.io.util.DataInputBuffer) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) DataOutputBuffer(org.apache.cassandra.io.util.DataOutputBuffer) DataInputPlus(org.apache.cassandra.io.util.DataInputPlus) Mutation(org.apache.cassandra.db.Mutation) UUID(java.util.UUID) Test(org.junit.Test)

Example 29 with RowUpdateBuilder

use of org.apache.cassandra.db.RowUpdateBuilder in project cassandra by apache.

the class IndexSummaryManagerTest method testJMXFunctions.

@Test
public void testJMXFunctions() throws IOException {
    IndexSummaryManager manager = IndexSummaryManager.instance;
    // resize interval
    manager.setResizeIntervalInMinutes(-1);
    assertNull(manager.getTimeToNextResize(TimeUnit.MINUTES));
    manager.setResizeIntervalInMinutes(10);
    assertEquals(10, manager.getResizeIntervalInMinutes());
    assertEquals(10, manager.getTimeToNextResize(TimeUnit.MINUTES), 1);
    manager.setResizeIntervalInMinutes(15);
    assertEquals(15, manager.getResizeIntervalInMinutes());
    assertEquals(15, manager.getTimeToNextResize(TimeUnit.MINUTES), 2);
    // memory pool capacity
    assertTrue(manager.getMemoryPoolCapacityInMB() >= 0);
    manager.setMemoryPoolCapacityInMB(10);
    assertEquals(10, manager.getMemoryPoolCapacityInMB());
    String ksname = KEYSPACE1;
    // index interval of 8, no key caching
    String cfname = CF_STANDARDLOWiINTERVAL;
    Keyspace keyspace = Keyspace.open(ksname);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(cfname);
    cfs.truncateBlocking();
    cfs.disableAutoCompaction();
    ByteBuffer value = ByteBuffer.wrap(new byte[100]);
    int numSSTables = 2;
    int numRows = 10;
    for (int sstable = 0; sstable < numSSTables; sstable++) {
        for (int row = 0; row < numRows; row++) {
            String key = String.format("%3d", row);
            new RowUpdateBuilder(cfs.metadata(), 0, key).clustering("column").add("val", value).build().applyUnsafe();
        }
        cfs.forceBlockingFlush();
    }
    assertTrue(manager.getAverageIndexInterval() >= cfs.metadata().params.minIndexInterval);
    Map<String, Integer> intervals = manager.getIndexIntervals();
    for (Map.Entry<String, Integer> entry : intervals.entrySet()) if (entry.getKey().contains(CF_STANDARDLOWiINTERVAL))
        assertEquals(cfs.metadata().params.minIndexInterval, entry.getValue(), 0.001);
    manager.setMemoryPoolCapacityInMB(0);
    manager.redistributeSummaries();
    assertTrue(manager.getAverageIndexInterval() > cfs.metadata().params.minIndexInterval);
    intervals = manager.getIndexIntervals();
    for (Map.Entry<String, Integer> entry : intervals.entrySet()) {
        if (entry.getKey().contains(CF_STANDARDLOWiINTERVAL))
            assertTrue(entry.getValue() >= cfs.metadata().params.minIndexInterval);
    }
}
Also used : RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 30 with RowUpdateBuilder

use of org.apache.cassandra.db.RowUpdateBuilder in project cassandra by apache.

the class SSTableMetadataTest method trackMaxMinColNames.

@Test
public void trackMaxMinColNames() throws CharacterCodingException, ExecutionException, InterruptedException {
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore store = keyspace.getColumnFamilyStore("Standard3");
    for (int j = 0; j < 8; j++) {
        String key = "row" + j;
        for (int i = 100; i < 150; i++) {
            new RowUpdateBuilder(store.metadata(), System.currentTimeMillis(), key).clustering(j + "col" + i).add("val", ByteBufferUtil.EMPTY_BYTE_BUFFER).build().applyUnsafe();
        }
    }
    store.forceBlockingFlush();
    assertEquals(1, store.getLiveSSTables().size());
    for (SSTableReader sstable : store.getLiveSSTables()) {
        assertEquals(ByteBufferUtil.string(sstable.getSSTableMetadata().minClusteringValues.get(0)), "0col100");
        assertEquals(ByteBufferUtil.string(sstable.getSSTableMetadata().maxClusteringValues.get(0)), "7col149");
    }
    String key = "row2";
    for (int i = 101; i < 299; i++) {
        new RowUpdateBuilder(store.metadata(), System.currentTimeMillis(), key).clustering(9 + "col" + i).add("val", ByteBufferUtil.EMPTY_BYTE_BUFFER).build().applyUnsafe();
    }
    store.forceBlockingFlush();
    store.forceMajorCompaction();
    assertEquals(1, store.getLiveSSTables().size());
    for (SSTableReader sstable : store.getLiveSSTables()) {
        assertEquals(ByteBufferUtil.string(sstable.getSSTableMetadata().minClusteringValues.get(0)), "0col100");
        assertEquals(ByteBufferUtil.string(sstable.getSSTableMetadata().maxClusteringValues.get(0)), "9col298");
    }
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Aggregations

RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)34 Test (org.junit.Test)22 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)19 Keyspace (org.apache.cassandra.db.Keyspace)17 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)14 ByteBuffer (java.nio.ByteBuffer)12 TableMetadata (org.apache.cassandra.schema.TableMetadata)12 DecoratedKey (org.apache.cassandra.db.DecoratedKey)10 Mutation (org.apache.cassandra.db.Mutation)9 ArrayList (java.util.ArrayList)4 UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)4 File (java.io.File)3 LifecycleTransaction (org.apache.cassandra.db.lifecycle.LifecycleTransaction)3 UUID (java.util.UUID)2 CompactionController (org.apache.cassandra.db.compaction.CompactionController)2 CompactionIterator (org.apache.cassandra.db.compaction.CompactionIterator)2 Row (org.apache.cassandra.db.rows.Row)2 WriteTimeoutException (org.apache.cassandra.exceptions.WriteTimeoutException)2 DataInputBuffer (org.apache.cassandra.io.util.DataInputBuffer)2 DataInputPlus (org.apache.cassandra.io.util.DataInputPlus)2