use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.
the class BatchlogManagerTest method testTruncatedReplay.
@Test
public void testTruncatedReplay() throws InterruptedException, ExecutionException {
TableMetadata cf2 = Schema.instance.getTableMetadata(KEYSPACE1, CF_STANDARD2);
TableMetadata cf3 = Schema.instance.getTableMetadata(KEYSPACE1, CF_STANDARD3);
// In the middle of the process, 'truncate' Standard2.
for (int i = 0; i < 1000; i++) {
Mutation mutation1 = new RowUpdateBuilder(cf2, FBUtilities.timestampMicros(), ByteBufferUtil.bytes(i)).clustering("name" + i).add("val", "val" + i).build();
Mutation mutation2 = new RowUpdateBuilder(cf3, FBUtilities.timestampMicros(), ByteBufferUtil.bytes(i)).clustering("name" + i).add("val", "val" + i).build();
List<Mutation> mutations = Lists.newArrayList(mutation1, mutation2);
// Make sure it's ready to be replayed, so adjust the timestamp.
long timestamp = System.currentTimeMillis() - BatchlogManager.getBatchlogTimeout();
if (i == 500)
SystemKeyspace.saveTruncationRecord(Keyspace.open(KEYSPACE1).getColumnFamilyStore(CF_STANDARD2), timestamp, CommitLogPosition.NONE);
// Adjust the timestamp (slightly) to make the test deterministic.
if (i >= 500)
timestamp++;
else
timestamp--;
BatchlogManager.store(Batch.createLocal(UUIDGen.getTimeUUID(timestamp, i), FBUtilities.timestampMicros(), mutations));
}
// Flush the batchlog to disk (see CASSANDRA-6822).
Keyspace.open(SchemaConstants.SYSTEM_KEYSPACE_NAME).getColumnFamilyStore(SystemKeyspace.BATCHES).forceBlockingFlush();
// Force batchlog replay and wait for it to complete.
BatchlogManager.instance.startBatchlogReplay().get();
// We should see half of Standard2-targeted mutations written after the replay and all of Standard3 mutations applied.
for (int i = 0; i < 1000; i++) {
UntypedResultSet result = executeInternal(String.format("SELECT * FROM \"%s\".\"%s\" WHERE key = intAsBlob(%d)", KEYSPACE1, CF_STANDARD2, i));
assertNotNull(result);
if (i >= 500) {
assertEquals(ByteBufferUtil.bytes(i), result.one().getBytes("key"));
assertEquals("name" + i, result.one().getString("name"));
assertEquals("val" + i, result.one().getString("val"));
} else {
assertTrue(result.isEmpty());
}
}
for (int i = 0; i < 1000; i++) {
UntypedResultSet result = executeInternal(String.format("SELECT * FROM \"%s\".\"%s\" WHERE key = intAsBlob(%d)", KEYSPACE1, CF_STANDARD3, i));
assertNotNull(result);
assertEquals(ByteBufferUtil.bytes(i), result.one().getBytes("key"));
assertEquals("name" + i, result.one().getString("name"));
assertEquals("val" + i, result.one().getString("val"));
}
}
use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.
the class BatchlogManagerTest method testReplay.
@Test
@SuppressWarnings("deprecation")
public void testReplay() throws Exception {
long initialAllBatches = BatchlogManager.instance.countAllBatches();
long initialReplayedBatches = BatchlogManager.instance.getTotalBatchesReplayed();
TableMetadata cfm = Keyspace.open(KEYSPACE1).getColumnFamilyStore(CF_STANDARD1).metadata();
// Half batches (50) ready to be replayed, half not.
for (int i = 0; i < 100; i++) {
List<Mutation> mutations = new ArrayList<>(10);
for (int j = 0; j < 10; j++) {
mutations.add(new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), ByteBufferUtil.bytes(i)).clustering("name" + j).add("val", "val" + j).build());
}
long timestamp = i < 50 ? (System.currentTimeMillis() - BatchlogManager.getBatchlogTimeout()) : (System.currentTimeMillis() + BatchlogManager.getBatchlogTimeout());
BatchlogManager.store(Batch.createLocal(UUIDGen.getTimeUUID(timestamp, i), timestamp * 1000, mutations));
}
// Flush the batchlog to disk (see CASSANDRA-6822).
Keyspace.open(SchemaConstants.SYSTEM_KEYSPACE_NAME).getColumnFamilyStore(SystemKeyspace.BATCHES).forceBlockingFlush();
assertEquals(100, BatchlogManager.instance.countAllBatches() - initialAllBatches);
assertEquals(0, BatchlogManager.instance.getTotalBatchesReplayed() - initialReplayedBatches);
// Force batchlog replay and wait for it to complete.
BatchlogManager.instance.startBatchlogReplay().get();
// Ensure that the first half, and only the first half, got replayed.
assertEquals(50, BatchlogManager.instance.countAllBatches() - initialAllBatches);
assertEquals(50, BatchlogManager.instance.getTotalBatchesReplayed() - initialReplayedBatches);
for (int i = 0; i < 100; i++) {
String query = String.format("SELECT * FROM \"%s\".\"%s\" WHERE key = intAsBlob(%d)", KEYSPACE1, CF_STANDARD1, i);
UntypedResultSet result = executeInternal(query);
assertNotNull(result);
if (i < 50) {
Iterator<UntypedResultSet.Row> it = result.iterator();
assertNotNull(it);
for (int j = 0; j < 10; j++) {
assertTrue(it.hasNext());
UntypedResultSet.Row row = it.next();
assertEquals(ByteBufferUtil.bytes(i), row.getBytes("key"));
assertEquals("name" + j, row.getString("name"));
assertEquals("val" + j, row.getString("val"));
}
assertFalse(it.hasNext());
} else {
assertTrue(result.isEmpty());
}
}
// Ensure that no stray mutations got somehow applied.
UntypedResultSet result = executeInternal(String.format("SELECT count(*) FROM \"%s\".\"%s\"", KEYSPACE1, CF_STANDARD1));
assertNotNull(result);
assertEquals(500, result.one().getLong("count"));
}
use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.
the class SecondaryIndexOnMapEntriesTest method updateMapInSimpleTable.
private Object[] updateMapInSimpleTable(Object[] row, String mapKey, Integer mapValue) throws Throwable {
execute("UPDATE %s SET v[?] = ? WHERE k = ?", mapKey, mapValue, row[0]);
UntypedResultSet rawResults = execute("SELECT * FROM %s WHERE k = ?", row[0]);
Map<Object, Object> value = (Map<Object, Object>) row[1];
if (mapValue == null) {
value.remove(mapKey);
} else {
value.put(mapKey, mapValue);
}
return row;
}
use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.
the class TypeTest method testNowToUUIDCompatibility.
@Test
public void testNowToUUIDCompatibility() throws Throwable {
createTable("CREATE TABLE %s (a int, b uuid, PRIMARY KEY (a, b))");
execute("INSERT INTO %s (a, b) VALUES (0, now())");
UntypedResultSet results = execute("SELECT * FROM %s WHERE a=0 AND b < now()");
assertEquals(1, results.size());
}
use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.
the class TypeTest method testReversedTypeCompatibility.
@Test
public void testReversedTypeCompatibility() throws Throwable {
createTable("CREATE TABLE %s (a int, b timeuuid, PRIMARY KEY (a, b)) WITH CLUSTERING ORDER BY (b DESC)");
execute("INSERT INTO %s (a, b) VALUES (0, now())");
UntypedResultSet results = execute("SELECT * FROM %s WHERE a=0 AND b < now()");
assertEquals(1, results.size());
}
Aggregations