Search in sources :

Example 66 with UntypedResultSet

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));
    }
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) FileChannel(java.nio.channels.FileChannel) File(org.apache.cassandra.io.util.File)

Example 67 with UntypedResultSet

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"));
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Mutation(org.apache.cassandra.db.Mutation)

Example 68 with UntypedResultSet

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"));
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) Mutation(org.apache.cassandra.db.Mutation)

Example 69 with UntypedResultSet

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);
}
Also used : RangesAtEndpoint(org.apache.cassandra.locator.RangesAtEndpoint) ArrayList(java.util.ArrayList) LifecycleTransaction(org.apache.cassandra.db.lifecycle.LifecycleTransaction) Token(org.apache.cassandra.dht.Token) Range(org.apache.cassandra.dht.Range) Murmur3Partitioner(org.apache.cassandra.dht.Murmur3Partitioner) UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) HashSet(java.util.HashSet) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 70 with UntypedResultSet

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());
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) DecoratedKey(org.apache.cassandra.db.DecoratedKey) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) RowIndexEntry(org.apache.cassandra.db.RowIndexEntry) Test(org.junit.Test)

Aggregations

UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)107 Test (org.junit.Test)35 UUID (java.util.UUID)8 ByteBuffer (java.nio.ByteBuffer)6 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)6 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)6 HashSet (java.util.HashSet)5 Mutation (org.apache.cassandra.db.Mutation)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 HashMap (java.util.HashMap)4 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)4 TableMetadata (org.apache.cassandra.schema.TableMetadata)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)3 ResultMessage (org.apache.cassandra.transport.messages.ResultMessage)3 Predicate (com.google.common.base.Predicate)2 IOException (java.io.IOException)2 InetAddress (java.net.InetAddress)2 ArrayList (java.util.ArrayList)2