use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.
the class ImportTest method testCorruptHelper.
private void testCorruptHelper(boolean verify, boolean copy) throws Throwable {
createTable("create table %s (id int primary key, d int)");
for (int i = 0; i < 10; i++) execute("insert into %s (id, d) values (?, ?)", i, i);
getCurrentColumnFamilyStore().forceBlockingFlush();
SSTableReader sstableToCorrupt = getCurrentColumnFamilyStore().getLiveSSTables().iterator().next();
for (int i = 0; i < 10; i++) execute("insert into %s (id, d) values (?, ?)", i + 10, i);
getCurrentColumnFamilyStore().forceBlockingFlush();
Set<SSTableReader> sstables = getCurrentColumnFamilyStore().getLiveSSTables();
getCurrentColumnFamilyStore().clearUnsafe();
String filenameToCorrupt = sstableToCorrupt.descriptor.filenameFor(Component.STATS);
try (FileChannel fileChannel = new File(filenameToCorrupt).newReadWriteChannel()) {
fileChannel.position(0);
fileChannel.write(ByteBufferUtil.bytes(StringUtils.repeat('z', 2)));
}
File backupdir = moveToBackupDir(sstables);
// now move a correct sstable to another directory to make sure that directory gets properly imported
for (int i = 100; i < 130; i++) execute("insert into %s (id, d) values (?, ?)", i, i);
getCurrentColumnFamilyStore().forceBlockingFlush();
Set<SSTableReader> correctSSTables = getCurrentColumnFamilyStore().getLiveSSTables();
getCurrentColumnFamilyStore().clearUnsafe();
File backupdirCorrect = moveToBackupDir(correctSSTables);
Set<File> beforeImport = Sets.newHashSet(backupdir.tryList());
// first we moved out 2 sstables, one correct and one corrupt in to a single directory (backupdir)
// then we moved out 1 sstable, a correct one (in backupdirCorrect).
// now import should fail import on backupdir, but import the one in backupdirCorrect.
SSTableImporter.Options options = SSTableImporter.Options.options(Sets.newHashSet(backupdir.toString(), backupdirCorrect.toString())).copyData(copy).verifySSTables(verify).build();
SSTableImporter importer = new SSTableImporter(getCurrentColumnFamilyStore());
List<String> failedDirectories = importer.importNewSSTables(options);
assertEquals(Collections.singletonList(backupdir.toString()), failedDirectories);
UntypedResultSet res = execute("SELECT * FROM %s");
for (UntypedResultSet.Row r : res) {
int pk = r.getInt("id");
assertTrue("pk = " + pk, pk >= 100 && pk < 130);
}
assertEquals("Data dir should contain one file", 1, countFiles(getCurrentColumnFamilyStore().getDirectories().getDirectoryForNewSSTables()));
assertEquals("backupdir contained 2 files before import, should still contain 2 after failing to import it", beforeImport, Sets.newHashSet(backupdir.tryList()));
if (copy) {
assertEquals("backupdirCorrect contained 1 file before import, should contain 1 after import too", 1, countFiles(backupdirCorrect));
} else {
assertEquals("backupdirCorrect contained 1 file before import, should be empty after import", 0, countFiles(backupdirCorrect));
}
}
use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.
the class BatchlogManagerTest method testAddBatch.
@Test
public void testAddBatch() {
long initialAllBatches = BatchlogManager.instance.countAllBatches();
TableMetadata cfm = Keyspace.open(KEYSPACE1).getColumnFamilyStore(CF_STANDARD5).metadata();
long timestamp = (currentTimeMillis() - DatabaseDescriptor.getWriteRpcTimeout(MILLISECONDS) * 2) * 1000;
UUID uuid = UUIDGen.getTimeUUID();
// Add a batch with 10 mutations
List<Mutation> mutations = new ArrayList<>(10);
for (int j = 0; j < 10; j++) {
mutations.add(new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), ByteBufferUtil.bytes(j)).clustering("name" + j).add("val", "val" + j).build());
}
BatchlogManager.store(Batch.createLocal(uuid, timestamp, mutations));
Assert.assertEquals(initialAllBatches + 1, BatchlogManager.instance.countAllBatches());
String query = String.format("SELECT count(*) FROM %s.%s where id = %s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.BATCHES, uuid);
UntypedResultSet result = executeInternal(query);
assertNotNull(result);
assertEquals(1L, result.one().getLong("count"));
}
use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.
the class BatchlogManagerTest method testRemoveBatch.
@Test
public void testRemoveBatch() {
long initialAllBatches = BatchlogManager.instance.countAllBatches();
TableMetadata cfm = Keyspace.open(KEYSPACE1).getColumnFamilyStore(CF_STANDARD5).metadata();
long timestamp = (currentTimeMillis() - DatabaseDescriptor.getWriteRpcTimeout(MILLISECONDS) * 2) * 1000;
UUID uuid = UUIDGen.getTimeUUID();
// Add a batch with 10 mutations
List<Mutation> mutations = new ArrayList<>(10);
for (int j = 0; j < 10; j++) {
mutations.add(new RowUpdateBuilder(cfm, FBUtilities.timestampMicros(), ByteBufferUtil.bytes(j)).clustering("name" + j).add("val", "val" + j).build());
}
// Store the batch
BatchlogManager.store(Batch.createLocal(uuid, timestamp, mutations));
Assert.assertEquals(initialAllBatches + 1, BatchlogManager.instance.countAllBatches());
// Remove the batch
BatchlogManager.remove(uuid);
assertEquals(initialAllBatches, BatchlogManager.instance.countAllBatches());
String query = String.format("SELECT count(*) FROM %s.%s where id = %s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.BATCHES, uuid);
UntypedResultSet result = executeInternal(query);
assertNotNull(result);
assertEquals(0L, result.one().getLong("count"));
}
use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.
the class AntiCompactionBytemanTest method testRedundantTransitions.
@Test
@BMRules(rules = { @BMRule(name = "Insert delay after first prepareToCommit", targetClass = "CompactionManager", targetMethod = "antiCompactGroup", condition = "not flagged(\"done\")", targetLocation = "AFTER INVOKE prepareToCommit", action = "Thread.sleep(2000);") })
public void testRedundantTransitions() throws Throwable {
createTable("create table %s (id int primary key, i int)");
execute("insert into %s (id, i) values (1, 1)");
execute("insert into %s (id, i) values (2, 1)");
execute("insert into %s (id, i) values (3, 1)");
getCurrentColumnFamilyStore().forceBlockingFlush();
UntypedResultSet res = execute("select token(id) as tok from %s");
Iterator<UntypedResultSet.Row> it = res.iterator();
List<Long> tokens = new ArrayList<>();
while (it.hasNext()) {
UntypedResultSet.Row r = it.next();
tokens.add(r.getLong("tok"));
}
tokens.sort(Long::compareTo);
long first = tokens.get(0) - 10;
long last = tokens.get(0) + 10;
Range<Token> toRepair = new Range<>(new Murmur3Partitioner.LongToken(first), new Murmur3Partitioner.LongToken(last));
first = tokens.get(1) - 10;
last = tokens.get(1) + 10;
Range<Token> pending = new Range<>(new Murmur3Partitioner.LongToken(first), new Murmur3Partitioner.LongToken(last));
RangesAtEndpoint ranges = new RangesAtEndpoint.Builder(FBUtilities.getBroadcastAddressAndPort()).add(Replica.fullReplica(FBUtilities.getBroadcastAddressAndPort(), toRepair)).add(Replica.transientReplica(InetAddressAndPort.getByName("127.0.0.1"), pending)).build();
AtomicBoolean failed = new AtomicBoolean(false);
AtomicBoolean finished = new AtomicBoolean(false);
Thread t = new Thread(() -> {
while (!finished.get()) {
UntypedResultSet result = null;
try {
result = execute("select id from %s");
} catch (Throwable throwable) {
failed.set(true);
throw new RuntimeException(throwable);
}
Iterator<UntypedResultSet.Row> rowIter = result.iterator();
Set<Integer> ids = new HashSet<>();
while (rowIter.hasNext()) {
UntypedResultSet.Row r = rowIter.next();
ids.add(r.getInt("id"));
}
if (!Sets.newHashSet(1, 2, 3).equals(ids)) {
failed.set(true);
return;
}
Uninterruptibles.sleepUninterruptibly(10, TimeUnit.MILLISECONDS);
}
});
t.start();
assertEquals(1, getCurrentColumnFamilyStore().getLiveSSTables().size());
SSTableReader sstableBefore = getCurrentColumnFamilyStore().getLiveSSTables().iterator().next();
try (LifecycleTransaction txn = getCurrentColumnFamilyStore().getTracker().tryModify(getCurrentColumnFamilyStore().getLiveSSTables(), OperationType.ANTICOMPACTION)) {
CompactionManager.instance.antiCompactGroup(getCurrentColumnFamilyStore(), ranges, txn, UUID.randomUUID(), () -> false);
}
finished.set(true);
t.join();
assertFalse(failed.get());
assertFalse(getCurrentColumnFamilyStore().getLiveSSTables().contains(sstableBefore));
Util.assertOnDiskState(getCurrentColumnFamilyStore(), 3);
}
use of org.apache.cassandra.cql3.UntypedResultSet in project cassandra by apache.
the class SSTableReverseIteratorTest method emptyBlockTolerance.
/**
* SSTRI shouldn't bail out if it encounters empty blocks (due to dropped columns)
*/
@Test
public void emptyBlockTolerance() {
String table = "empty_block_tolerance";
QueryProcessor.executeInternal(String.format("CREATE TABLE %s.%s (k INT, c int, v1 blob, v2 blob, primary key (k, c))", KEYSPACE, table));
ColumnFamilyStore tbl = Keyspace.open(KEYSPACE).getColumnFamilyStore(table);
assert tbl != null;
int key = 100;
QueryProcessor.executeInternal(String.format("UPDATE %s.%s SET v1=?, v2=? WHERE k=? AND c=?", KEYSPACE, table), bytes(8), bytes(8), key, 0);
QueryProcessor.executeInternal(String.format("UPDATE %s.%s SET v1=? WHERE k=? AND c=?", KEYSPACE, table), bytes(0x20000), key, 1);
QueryProcessor.executeInternal(String.format("UPDATE %s.%s SET v1=? WHERE k=? AND c=?", KEYSPACE, table), bytes(0x20000), key, 2);
QueryProcessor.executeInternal(String.format("UPDATE %s.%s SET v1=? WHERE k=? AND c=?", KEYSPACE, table), bytes(0x20000), key, 3);
tbl.forceBlockingFlush();
SSTableReader sstable = Iterables.getOnlyElement(tbl.getLiveSSTables());
DecoratedKey dk = tbl.getPartitioner().decorateKey(Int32Type.instance.decompose(key));
RowIndexEntry indexEntry = sstable.getPosition(dk, SSTableReader.Operator.EQ);
Assert.assertTrue(indexEntry.isIndexed());
Assert.assertTrue(indexEntry.columnsIndexCount() > 2);
// drop v1 so the first 2 index blocks only contain empty unfiltereds
QueryProcessor.executeInternal(String.format("ALTER TABLE %s.%s DROP v1", KEYSPACE, table));
UntypedResultSet result = QueryProcessor.executeInternal(String.format("SELECT v2 FROM %s.%s WHERE k=? ORDER BY c DESC", KEYSPACE, table), key);
Assert.assertEquals(1, result.size());
}
Aggregations