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