Search in sources :

Example 61 with Keyspace

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

the class SSTableScannerTest method testSingleDataRange.

@Test
public void testSingleDataRange() throws IOException {
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore store = keyspace.getColumnFamilyStore(TABLE);
    store.clearUnsafe();
    // disable compaction while flushing
    store.disableAutoCompaction();
    for (int i = 2; i < 10; i++) insertRowWithKey(store.metadata(), i);
    store.forceBlockingFlush();
    assertEquals(1, store.getLiveSSTables().size());
    SSTableReader sstable = store.getLiveSSTables().iterator().next();
    // full range scan
    ISSTableScanner scanner = sstable.getScanner();
    for (int i = 2; i < 10; i++) assertEquals(toKey(i), new String(scanner.next().partitionKey().getKey().array()));
    scanner.close();
    // a simple read of a chunk in the middle
    assertScanMatches(sstable, 3, 6, 3, 6);
    // start of range edge conditions
    assertScanMatches(sstable, 1, 9, 2, 9);
    assertScanMatches(sstable, 2, 9, 2, 9);
    assertScanMatches(sstable, 3, 9, 3, 9);
    // end of range edge conditions
    assertScanMatches(sstable, 1, 8, 2, 8);
    assertScanMatches(sstable, 1, 9, 2, 9);
    assertScanMatches(sstable, 1, 9, 2, 9);
    // single item ranges
    assertScanMatches(sstable, 2, 2, 2, 2);
    assertScanMatches(sstable, 5, 5, 5, 5);
    assertScanMatches(sstable, 9, 9, 9, 9);
    // empty ranges
    assertScanEmpty(sstable, 0, 1);
    assertScanEmpty(sstable, 10, 11);
    // wrapping, starts in middle
    assertScanMatches(sstable, 5, 3, 2, 3, 5, 9);
    assertScanMatches(sstable, 5, 2, 2, 2, 5, 9);
    assertScanMatches(sstable, 5, 1, 5, 9);
    assertScanMatches(sstable, 5, Integer.MIN_VALUE, 5, 9);
    // wrapping, starts at end
    assertScanMatches(sstable, 9, 8, 2, 8, 9, 9);
    assertScanMatches(sstable, 9, 3, 2, 3, 9, 9);
    assertScanMatches(sstable, 9, 2, 2, 2, 9, 9);
    assertScanMatches(sstable, 9, 1, 9, 9);
    assertScanMatches(sstable, 9, Integer.MIN_VALUE, 9, 9);
    assertScanMatches(sstable, 8, 3, 2, 3, 8, 9);
    assertScanMatches(sstable, 8, 2, 2, 2, 8, 9);
    assertScanMatches(sstable, 8, 1, 8, 9);
    assertScanMatches(sstable, 8, Integer.MIN_VALUE, 8, 9);
    // wrapping, starts past end
    assertScanMatches(sstable, 10, 9, 2, 9);
    assertScanMatches(sstable, 10, 5, 2, 5);
    assertScanMatches(sstable, 10, 2, 2, 2);
    assertScanEmpty(sstable, 10, 1);
    assertScanEmpty(sstable, 10, Integer.MIN_VALUE);
    assertScanMatches(sstable, 11, 10, 2, 9);
    assertScanMatches(sstable, 11, 9, 2, 9);
    assertScanMatches(sstable, 11, 5, 2, 5);
    assertScanMatches(sstable, 11, 2, 2, 2);
    assertScanEmpty(sstable, 11, 1);
    assertScanEmpty(sstable, 11, Integer.MIN_VALUE);
    // wrapping, starts at start
    assertScanMatches(sstable, 3, 1, 3, 9);
    assertScanMatches(sstable, 3, Integer.MIN_VALUE, 3, 9);
    assertScanMatches(sstable, 2, 1, 2, 9);
    assertScanMatches(sstable, 2, Integer.MIN_VALUE, 2, 9);
    assertScanMatches(sstable, 1, 0, 2, 9);
    assertScanMatches(sstable, 1, Integer.MIN_VALUE, 2, 9);
    // wrapping, starts before
    assertScanMatches(sstable, 1, -1, 2, 9);
    assertScanMatches(sstable, 1, Integer.MIN_VALUE, 2, 9);
    assertScanMatches(sstable, 1, 0, 2, 9);
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Example 62 with Keyspace

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

the class SSTableScannerTest method testSingleKeyMultipleRanges.

@Test
public void testSingleKeyMultipleRanges() throws IOException {
    Keyspace keyspace = Keyspace.open(KEYSPACE);
    ColumnFamilyStore store = keyspace.getColumnFamilyStore(TABLE);
    store.clearUnsafe();
    // disable compaction while flushing
    store.disableAutoCompaction();
    insertRowWithKey(store.metadata(), 205);
    store.forceBlockingFlush();
    assertEquals(1, store.getLiveSSTables().size());
    SSTableReader sstable = store.getLiveSSTables().iterator().next();
    // full range scan
    ISSTableScanner fullScanner = sstable.getScanner();
    assertScanContainsRanges(fullScanner, 205, 205);
    // scan three ranges separately
    ISSTableScanner scanner = sstable.getScanner(makeRanges(101, 109, 201, 209));
    // this will currently fail
    assertScanContainsRanges(scanner, 205, 205);
}
Also used : SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Test(org.junit.Test)

Example 63 with Keyspace

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

the class ActiveRepairServiceTest method prepareColumnFamilyStore.

private ColumnFamilyStore prepareColumnFamilyStore() {
    Keyspace keyspace = Keyspace.open(KEYSPACE5);
    ColumnFamilyStore store = keyspace.getColumnFamilyStore(CF_STANDARD1);
    store.truncateBlocking();
    store.disableAutoCompaction();
    createSSTables(store, 10);
    return store;
}
Also used : Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore)

Example 64 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 65 with Keyspace

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

the class SSTableOfflineRelevel method main.

/**
     * @param args a list of sstables whose metadata we are changing
     */
public static void main(String[] args) throws IOException {
    PrintStream out = System.out;
    if (args.length < 2) {
        out.println("This command should be run with Cassandra stopped!");
        out.println("Usage: sstableofflinerelevel [--dry-run] <keyspace> <columnfamily>");
        System.exit(1);
    }
    Util.initDatabaseDescriptor();
    boolean dryRun = args[0].equals("--dry-run");
    String keyspace = args[args.length - 2];
    String columnfamily = args[args.length - 1];
    Schema.instance.loadFromDisk(false);
    if (Schema.instance.getTableMetadataRef(keyspace, columnfamily) == null)
        throw new IllegalArgumentException(String.format("Unknown keyspace/columnFamily %s.%s", keyspace, columnfamily));
    Keyspace ks = Keyspace.openWithoutSSTables(keyspace);
    ColumnFamilyStore cfs = ks.getColumnFamilyStore(columnfamily);
    Directories.SSTableLister lister = cfs.getDirectories().sstableLister(Directories.OnTxnErr.THROW).skipTemporary(true);
    SetMultimap<File, SSTableReader> sstableMultimap = HashMultimap.create();
    for (Map.Entry<Descriptor, Set<Component>> sstable : lister.list().entrySet()) {
        if (sstable.getKey() != null) {
            try {
                SSTableReader reader = SSTableReader.open(sstable.getKey());
                sstableMultimap.put(reader.descriptor.directory, reader);
            } catch (Throwable t) {
                out.println("Couldn't open sstable: " + sstable.getKey().filenameFor(Component.DATA));
                Throwables.propagate(t);
            }
        }
    }
    if (sstableMultimap.isEmpty()) {
        out.println("No sstables to relevel for " + keyspace + "." + columnfamily);
        System.exit(1);
    }
    for (File directory : sstableMultimap.keySet()) {
        if (!sstableMultimap.get(directory).isEmpty()) {
            Relevel rl = new Relevel(sstableMultimap.get(directory));
            out.println("For sstables in " + directory + ":");
            rl.relevel(dryRun);
        }
    }
    System.exit(0);
}
Also used : PrintStream(java.io.PrintStream) Set(java.util.Set) Directories(org.apache.cassandra.db.Directories) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Descriptor(org.apache.cassandra.io.sstable.Descriptor) File(java.io.File) Map(java.util.Map)

Aggregations

Keyspace (org.apache.cassandra.db.Keyspace)69 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)63 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)48 Test (org.junit.Test)42 LifecycleTransaction (org.apache.cassandra.db.lifecycle.LifecycleTransaction)23 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)17 CompactionController (org.apache.cassandra.db.compaction.CompactionController)13 CompactionIterator (org.apache.cassandra.db.compaction.CompactionIterator)12 DecoratedKey (org.apache.cassandra.db.DecoratedKey)11 ByteBuffer (java.nio.ByteBuffer)10 File (java.io.File)5 Directories (org.apache.cassandra.db.Directories)5 SystemKeyspace (org.apache.cassandra.db.SystemKeyspace)5 RestorableMeter (org.apache.cassandra.metrics.RestorableMeter)5 ArrayList (java.util.ArrayList)4 AbstractCompactionStrategy (org.apache.cassandra.db.compaction.AbstractCompactionStrategy)4 Range (org.apache.cassandra.dht.Range)4 Token (org.apache.cassandra.dht.Token)4 Descriptor (org.apache.cassandra.io.sstable.Descriptor)4 PrintStream (java.io.PrintStream)3