Search in sources :

Example 86 with Keyspace

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

the class RangeCommandIteratorTest method testRangeQueried.

@Test
public void testRangeQueried() {
    List<Token> tokens = setTokens(100, 200, 300, 400);
    // n tokens divide token ring into n+1 ranges
    int vnodeCount = tokens.size() + 1;
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(CF_STANDARD1);
    cfs.clearUnsafe();
    int rows = 100;
    for (int i = 0; i < rows; ++i) {
        RowUpdateBuilder builder = new RowUpdateBuilder(cfs.metadata(), 10, String.valueOf(i));
        builder.clustering("c");
        builder.add("val", String.valueOf(i));
        builder.build().applyUnsafe();
    }
    Util.flush(cfs);
    PartitionRangeReadCommand command = (PartitionRangeReadCommand) Util.cmd(cfs).build();
    AbstractBounds<PartitionPosition> keyRange = command.dataRange().keyRange();
    // without range merger, there will be 2 batches requested: 1st batch with 1 range and 2nd batch with remaining ranges
    CloseableIterator<ReplicaPlan.ForRangeRead> replicaPlans = replicaPlanIterator(keyRange, keyspace, false);
    RangeCommandIterator data = new RangeCommandIterator(replicaPlans, command, 1, 1000, vnodeCount, nanoTime());
    verifyRangeCommandIterator(data, rows, 2, vnodeCount);
    // without range merger and initial cf=5, there will be 1 batches requested: 5 vnode ranges for 1st batch
    replicaPlans = replicaPlanIterator(keyRange, keyspace, false);
    data = new RangeCommandIterator(replicaPlans, command, vnodeCount, 1000, vnodeCount, nanoTime());
    verifyRangeCommandIterator(data, rows, 1, vnodeCount);
    // without range merger and max cf=1, there will be 5 batches requested: 1 vnode range per batch
    replicaPlans = replicaPlanIterator(keyRange, keyspace, false);
    data = new RangeCommandIterator(replicaPlans, command, 1, 1, vnodeCount, nanoTime());
    verifyRangeCommandIterator(data, rows, vnodeCount, vnodeCount);
    // with range merger, there will be only 1 batch requested, as all ranges share the same replica - localhost
    replicaPlans = replicaPlanIterator(keyRange, keyspace, true);
    data = new RangeCommandIterator(replicaPlans, command, 1, 1000, vnodeCount, nanoTime());
    verifyRangeCommandIterator(data, rows, 1, vnodeCount);
    // with range merger and max cf=1, there will be only 1 batch requested, as all ranges share the same replica - localhost
    replicaPlans = replicaPlanIterator(keyRange, keyspace, true);
    data = new RangeCommandIterator(replicaPlans, command, 1, 1, vnodeCount, nanoTime());
    verifyRangeCommandIterator(data, rows, 1, vnodeCount);
}
Also used : RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) PartitionRangeReadCommand(org.apache.cassandra.db.PartitionRangeReadCommand) PartitionPosition(org.apache.cassandra.db.PartitionPosition) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Token(org.apache.cassandra.dht.Token) Test(org.junit.Test)

Example 87 with Keyspace

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

the class RangeCommandsTest method testEstimateResultsPerRange.

private void testEstimateResultsPerRange(int rf) {
    String ks = createKeyspace(String.format("CREATE KEYSPACE %%s WITH replication={'class':'SimpleStrategy', 'replication_factor':%s}", rf));
    String table = createTable(ks, "CREATE TABLE %s (k int PRIMARY KEY, v int)");
    createIndex(String.format("CREATE CUSTOM INDEX ON %s.%s(v) USING '%s'", ks, table, MockedIndex.class.getName()));
    Keyspace keyspace = Keyspace.open(ks);
    ColumnFamilyStore cfs = Keyspace.open(ks).getColumnFamilyStore(table);
    setNumTokens(1);
    testEstimateResultsPerRange(keyspace, cfs, rf, 0, null, 0);
    testEstimateResultsPerRange(keyspace, cfs, rf, 1, null, 1);
    testEstimateResultsPerRange(keyspace, cfs, rf, 10, null, 10);
    testEstimateResultsPerRange(keyspace, cfs, rf, 100, null, 100);
    testEstimateResultsPerRange(keyspace, cfs, rf, 0, 0, 0);
    testEstimateResultsPerRange(keyspace, cfs, rf, 1, 0, 0);
    testEstimateResultsPerRange(keyspace, cfs, rf, 10, 0, 0);
    testEstimateResultsPerRange(keyspace, cfs, rf, 100, 0, 0);
    testEstimateResultsPerRange(keyspace, cfs, rf, 0, 1000, 1000);
    testEstimateResultsPerRange(keyspace, cfs, rf, 1, 1000, 1000);
    testEstimateResultsPerRange(keyspace, cfs, rf, 10, 1000, 1000);
    testEstimateResultsPerRange(keyspace, cfs, rf, 100, 1000, 1000);
    setNumTokens(5);
    testEstimateResultsPerRange(keyspace, cfs, rf, 0, null, 0);
    testEstimateResultsPerRange(keyspace, cfs, rf, 1, null, 0.2f);
    testEstimateResultsPerRange(keyspace, cfs, rf, 10, null, 2);
    testEstimateResultsPerRange(keyspace, cfs, rf, 100, null, 20);
    testEstimateResultsPerRange(keyspace, cfs, rf, 0, 0, 0);
    testEstimateResultsPerRange(keyspace, cfs, rf, 1, 0, 0);
    testEstimateResultsPerRange(keyspace, cfs, rf, 10, 0, 0);
    testEstimateResultsPerRange(keyspace, cfs, rf, 100, 0, 0);
    testEstimateResultsPerRange(keyspace, cfs, rf, 0, 1000, 200);
    testEstimateResultsPerRange(keyspace, cfs, rf, 1, 1000, 200);
    testEstimateResultsPerRange(keyspace, cfs, rf, 10, 1000, 200);
    testEstimateResultsPerRange(keyspace, cfs, rf, 100, 1000, 200);
}
Also used : Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore)

Example 88 with Keyspace

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

the class RangeCommandsTest method tesConcurrencyFactor.

@Test
public void tesConcurrencyFactor() {
    new TokenUpdater().withTokens("127.0.0.1", 1, 2).withTokens("127.0.0.2", 3, 4).update();
    String table = createTable("CREATE TABLE %s (k int PRIMARY KEY, v int)");
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore cfs = keyspace.getColumnFamilyStore(table);
    // verify that a low concurrency factor is not capped by the max concurrency factor
    PartitionRangeReadCommand command = command(cfs, 50, 50);
    try (RangeCommandIterator partitions = RangeCommands.rangeCommandIterator(command, ONE, nanoTime());
        ReplicaPlanIterator ranges = new ReplicaPlanIterator(command.dataRange().keyRange(), keyspace, ONE)) {
        assertEquals(2, partitions.concurrencyFactor());
        assertEquals(MAX_CONCURRENCY_FACTOR, partitions.maxConcurrencyFactor());
        assertEquals(5, ranges.size());
    }
    // verify that a high concurrency factor is capped by the max concurrency factor
    command = command(cfs, 1000, 50);
    try (RangeCommandIterator partitions = RangeCommands.rangeCommandIterator(command, ONE, nanoTime());
        ReplicaPlanIterator ranges = new ReplicaPlanIterator(command.dataRange().keyRange(), keyspace, ONE)) {
        assertEquals(MAX_CONCURRENCY_FACTOR, partitions.concurrencyFactor());
        assertEquals(MAX_CONCURRENCY_FACTOR, partitions.maxConcurrencyFactor());
        assertEquals(5, ranges.size());
    }
    // with 0 estimated results per range the concurrency factor should be 1
    command = command(cfs, 1000, 0);
    try (RangeCommandIterator partitions = RangeCommands.rangeCommandIterator(command, ONE, nanoTime());
        ReplicaPlanIterator ranges = new ReplicaPlanIterator(command.dataRange().keyRange(), keyspace, ONE)) {
        assertEquals(1, partitions.concurrencyFactor());
        assertEquals(MAX_CONCURRENCY_FACTOR, partitions.maxConcurrencyFactor());
        assertEquals(5, ranges.size());
    }
}
Also used : PartitionRangeReadCommand(org.apache.cassandra.db.PartitionRangeReadCommand) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Example 89 with Keyspace

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

the class ViewFilteringTest method complexRestrictedTimestampUpdateTest.

public void complexRestrictedTimestampUpdateTest(boolean flush) throws Throwable {
    createTable("CREATE TABLE %s (a int, b int, c int, d int, e int, PRIMARY KEY (a, b))");
    execute("USE " + keyspace());
    executeNet(protocolVersion, "USE " + keyspace());
    Keyspace ks = Keyspace.open(keyspace());
    createView("mv", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a IS NOT NULL AND b IS NOT NULL AND c IS NOT NULL AND c = 1 PRIMARY KEY (c, a, b)");
    ks.getColumnFamilyStore("mv").disableAutoCompaction();
    //Set initial values TS=0, matching the restriction and verify view
    executeNet(protocolVersion, "INSERT INTO %s (a, b, c, d) VALUES (0, 0, 1, 0) USING TIMESTAMP 0");
    assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(0));
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    //update c's timestamp TS=2
    executeNet(protocolVersion, "UPDATE %s USING TIMESTAMP 2 SET c = ? WHERE a = ? and b = ? ", 1, 0, 0);
    assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(0));
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    //change c's value and TS=3, tombstones c=1 and adds c=0 record
    executeNet(protocolVersion, "UPDATE %s USING TIMESTAMP 3 SET c = ? WHERE a = ? and b = ? ", 0, 0, 0);
    assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 0, 0, 0));
    if (flush) {
        ks.getColumnFamilyStore("mv").forceMajorCompaction();
        FBUtilities.waitOnFutures(ks.flush());
    }
    //change c's value back to 1 with TS=4, check we can see d
    executeNet(protocolVersion, "UPDATE %s USING TIMESTAMP 4 SET c = ? WHERE a = ? and b = ? ", 1, 0, 0);
    if (flush) {
        ks.getColumnFamilyStore("mv").forceMajorCompaction();
        FBUtilities.waitOnFutures(ks.flush());
    }
    assertRows(execute("SELECT d, e from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(0, null));
    //Add e value @ TS=1
    executeNet(protocolVersion, "UPDATE %s USING TIMESTAMP 1 SET e = ? WHERE a = ? and b = ? ", 1, 0, 0);
    assertRows(execute("SELECT d, e from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(0, 1));
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    //Change d value @ TS=2
    executeNet(protocolVersion, "UPDATE %s USING TIMESTAMP 2 SET d = ? WHERE a = ? and b = ? ", 2, 0, 0);
    assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(2));
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    //Change d value @ TS=3
    executeNet(protocolVersion, "UPDATE %s USING TIMESTAMP 3 SET d = ? WHERE a = ? and b = ? ", 1, 0, 0);
    assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row(1));
    //Tombstone c
    executeNet(protocolVersion, "DELETE FROM %s WHERE a = ? and b = ?", 0, 0);
    assertRows(execute("SELECT d from mv"));
    //Add back without D
    executeNet(protocolVersion, "INSERT INTO %s (a, b, c) VALUES (0, 0, 1)");
    //Make sure D doesn't pop back in.
    assertRows(execute("SELECT d from mv WHERE c = ? and a = ? and b = ?", 1, 0, 0), row((Object) null));
    //New partition
    // insert a row with timestamp 0
    executeNet(protocolVersion, "INSERT INTO %s (a, b, c, d, e) VALUES (?, ?, ?, ?, ?) USING TIMESTAMP 0", 1, 0, 1, 0, 0);
    // overwrite pk and e with timestamp 1, but don't overwrite d
    executeNet(protocolVersion, "INSERT INTO %s (a, b, c, e) VALUES (?, ?, ?, ?) USING TIMESTAMP 1", 1, 0, 1, 0);
    // delete with timestamp 0 (which should only delete d)
    executeNet(protocolVersion, "DELETE FROM %s USING TIMESTAMP 0 WHERE a = ? AND b = ?", 1, 0);
    assertRows(execute("SELECT a, b, c, d, e from mv WHERE c = ? and a = ? and b = ?", 1, 1, 0), row(1, 0, 1, null, 0));
    executeNet(protocolVersion, "UPDATE %s USING TIMESTAMP 2 SET c = ? WHERE a = ? AND b = ?", 1, 1, 1);
    executeNet(protocolVersion, "UPDATE %s USING TIMESTAMP 3 SET c = ? WHERE a = ? AND b = ?", 1, 1, 0);
    assertRows(execute("SELECT a, b, c, d, e from mv WHERE c = ? and a = ? and b = ?", 1, 1, 0), row(1, 0, 1, null, 0));
    executeNet(protocolVersion, "UPDATE %s USING TIMESTAMP 3 SET d = ? WHERE a = ? AND b = ?", 0, 1, 0);
    assertRows(execute("SELECT a, b, c, d, e from mv WHERE c = ? and a = ? and b = ?", 1, 1, 0), row(1, 0, 1, 0, 0));
}
Also used : SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) Keyspace(org.apache.cassandra.db.Keyspace)

Example 90 with Keyspace

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

the class SnapshotDeletingTest method testCompactionHook.

@Test
public void testCompactionHook() throws Exception {
    Assume.assumeTrue(FBUtilities.isWindows);
    Keyspace keyspace = Keyspace.open(KEYSPACE1);
    ColumnFamilyStore store = keyspace.getColumnFamilyStore(CF_STANDARD1);
    store.clearUnsafe();
    populate(10000);
    store.snapshot("snapshot1");
    // Confirm snapshot deletion fails. Sleep for a bit just to make sure the SnapshotDeletingTask has
    // time to run and fail.
    Thread.sleep(500);
    store.clearSnapshot("snapshot1");
    assertEquals(1, SnapshotDeletingTask.pendingDeletionCount());
    // Compact the cf and confirm that the executor's after hook calls rescheduleDeletion
    populate(20000);
    store.forceBlockingFlush();
    store.forceMajorCompaction();
    long start = System.currentTimeMillis();
    while (System.currentTimeMillis() - start < 1000 && SnapshotDeletingTask.pendingDeletionCount() > 0) {
        Thread.yield();
    }
    assertEquals(0, SnapshotDeletingTask.pendingDeletionCount());
}
Also used : Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Aggregations

Keyspace (org.apache.cassandra.db.Keyspace)173 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)107 Test (org.junit.Test)77 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)66 Token (org.apache.cassandra.dht.Token)35 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)29 LifecycleTransaction (org.apache.cassandra.db.lifecycle.LifecycleTransaction)27 DecoratedKey (org.apache.cassandra.db.DecoratedKey)23 TableMetadata (org.apache.cassandra.schema.TableMetadata)21 Range (org.apache.cassandra.dht.Range)19 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)19 ArrayList (java.util.ArrayList)18 SystemKeyspace (org.apache.cassandra.db.SystemKeyspace)18 Set (java.util.Set)17 HashMap (java.util.HashMap)16 List (java.util.List)16 Map (java.util.Map)16 DatabaseDescriptor (org.apache.cassandra.config.DatabaseDescriptor)16 Collectors (java.util.stream.Collectors)15 TableId (org.apache.cassandra.schema.TableId)15