Search in sources :

Example 41 with Keyspace

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

the class ViewComplexDeletionsTest method complexTimestampWithbasePKColumnsInViewPKDeletionTest.

private void complexTimestampWithbasePKColumnsInViewPKDeletionTest(boolean flush) throws Throwable {
    createTable("create table %s (p1 int, p2 int, v1 int, v2 int, primary key(p1, p2))");
    Keyspace ks = Keyspace.open(keyspace());
    createView("create materialized view %s as select * from %s " + "where p1 is not null and p2 is not null primary key (p2, p1)");
    ks.getColumnFamilyStore(currentView()).disableAutoCompaction();
    // Set initial values TS=1
    updateView("Insert into %s (p1, p2, v1, v2) values (1, 2, 3, 4) using timestamp 1;");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRowsIgnoringOrder(executeView("SELECT v1, v2, WRITETIME(v2) from %s WHERE p1 = ? AND p2 = ?", 1, 2), row(3, 4, 1L));
    // remove row/mv TS=2
    updateView("Delete from %s using timestamp 2 where p1 = 1 and p2 = 2;");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    // view are empty
    assertRowsIgnoringOrder(executeView("SELECT * FROM %s"));
    // insert PK with TS=3
    updateView("Insert into %s (p1, p2) values (1, 2) using timestamp 3;");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    // deleted column in MV remained dead
    assertRowsIgnoringOrder(executeView("SELECT * FROM %s"), row(2, 1, null, null));
    ks.getColumnFamilyStore(currentView()).forceMajorCompaction();
    assertRowsIgnoringOrder(executeView("SELECT * FROM %s"), row(2, 1, null, null));
    // reset values
    updateView("Insert into %s (p1, p2, v1, v2) values (1, 2, 3, 4) using timestamp 10;");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRowsIgnoringOrder(executeView("SELECT v1, v2, WRITETIME(v2) from %s WHERE p1 = ? AND p2 = ?", 1, 2), row(3, 4, 10L));
    updateView("UPDATE %s using timestamp 20 SET v2 = 5 WHERE p1 = 1 and p2 = 2");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRowsIgnoringOrder(executeView("SELECT v1, v2, WRITETIME(v2) from %s WHERE p1 = ? AND p2 = ?", 1, 2), row(3, 5, 20L));
    updateView("DELETE FROM %s using timestamp 10 WHERE p1 = 1 and p2 = 2");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRowsIgnoringOrder(executeView("SELECT v1, v2, WRITETIME(v2) from %s WHERE p1 = ? AND p2 = ?", 1, 2), row(null, 5, 20L));
}
Also used : SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) Keyspace(org.apache.cassandra.db.Keyspace)

Example 42 with Keyspace

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

the class ViewComplexDeletionsTest method testCommutativeRowDeletion.

private void testCommutativeRowDeletion(boolean flush) throws Throwable {
    // CASSANDRA-13409 new update should not resurrect previous deleted data in view
    createTable("create table %s (p int primary key, v1 int, v2 int)");
    Keyspace ks = Keyspace.open(keyspace());
    createView("create materialized view %s as select * from %s " + "where p is not null and v1 is not null primary key (v1, p)");
    ks.getColumnFamilyStore(currentView()).disableAutoCompaction();
    // sstable-1, Set initial values TS=1
    updateView("Insert into %s (p, v1, v2) values (3, 1, 3) using timestamp 1;");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRowsIgnoringOrder(executeView("SELECT v2, WRITETIME(v2) from %s WHERE v1 = ? AND p = ?", 1, 3), row(3, 1L));
    // sstable-2
    updateView("Delete from %s using timestamp 2 where p = 3;");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRowsIgnoringOrder(executeView("SELECT v1, p, v2, WRITETIME(v2) from %s"));
    // sstable-3
    updateView("Insert into %s (p, v1) values (3, 1) using timestamp 3;");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRowsIgnoringOrder(executeView("SELECT v1, p, v2, WRITETIME(v2) from %s"), row(1, 3, null, null));
    // sstable-4
    updateView("UPdate %s using timestamp 4 set v1 = 2 where p = 3;");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRowsIgnoringOrder(executeView("SELECT v1, p, v2, WRITETIME(v2) from %s"), row(2, 3, null, null));
    // sstable-5
    updateView("UPdate %s using timestamp 5 set v1 = 1 where p = 3;");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRowsIgnoringOrder(executeView("SELECT v1, p, v2, WRITETIME(v2) from %s"), row(1, 3, null, null));
    if (flush) {
        // compact sstable 2 and 4, 5;
        ColumnFamilyStore cfs = ks.getColumnFamilyStore(currentView());
        List<String> sstables = cfs.getLiveSSTables().stream().sorted(Comparator.comparingInt(s -> s.descriptor.generation)).map(SSTableReader::getFilename).collect(Collectors.toList());
        String dataFiles = String.join(",", Arrays.asList(sstables.get(1), sstables.get(3), sstables.get(4)));
        CompactionManager.instance.forceUserDefinedCompaction(dataFiles);
        assertEquals(3, cfs.getLiveSSTables().size());
    }
    // regular tombstone should be retained after compaction
    assertRowsIgnoringOrder(executeView("SELECT v1, p, v2, WRITETIME(v2) from %s"), row(1, 3, null, null));
}
Also used : Arrays(java.util.Arrays) CompactionManager(org.apache.cassandra.db.compaction.CompactionManager) FBUtilities(org.apache.cassandra.utils.FBUtilities) Test(org.junit.Test) Collectors(java.util.stream.Collectors) SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) SSTableReader(org.apache.cassandra.io.sstable.format.SSTableReader) List(java.util.List) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore) Comparator(java.util.Comparator) SchemaConstants(org.apache.cassandra.schema.SchemaConstants) Keyspace(org.apache.cassandra.db.Keyspace) Assert.assertEquals(org.junit.Assert.assertEquals) SystemKeyspace(org.apache.cassandra.db.SystemKeyspace) Keyspace(org.apache.cassandra.db.Keyspace) ColumnFamilyStore(org.apache.cassandra.db.ColumnFamilyStore)

Example 43 with Keyspace

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

the class ViewComplexLivenessTest method testUnselectedColumnWithExpiredLivenessInfo.

private void testUnselectedColumnWithExpiredLivenessInfo(boolean flush) throws Throwable {
    createTable("create table %s (k int, c int, a int, b int, PRIMARY KEY(k, c))");
    Keyspace ks = Keyspace.open(keyspace());
    createView("create materialized view %s as select k,c,b from %s " + "where c is not null and k is not null primary key (c, k)");
    ks.getColumnFamilyStore(currentView()).disableAutoCompaction();
    // sstable-1, Set initial values TS=1
    updateViewWithFlush("UPDATE %s SET a = 1 WHERE k = 1 AND c = 1;", flush);
    assertRowsIgnoringOrder(execute("SELECT * from %s WHERE k = 1 AND c = 1;"), row(1, 1, 1, null));
    assertRowsIgnoringOrder(executeView("SELECT k,c,b from %s WHERE k = 1 AND c = 1;"), row(1, 1, null));
    // sstable-2
    updateViewWithFlush("INSERT INTO %s(k,c) VALUES(1,1) USING TTL 5", flush);
    assertRowsIgnoringOrder(execute("SELECT * from %s WHERE k = 1 AND c = 1;"), row(1, 1, 1, null));
    assertRowsIgnoringOrder(executeView("SELECT k,c,b from %s WHERE k = 1 AND c = 1;"), row(1, 1, null));
    Thread.sleep(5001);
    assertRowsIgnoringOrder(execute("SELECT * from %s WHERE k = 1 AND c = 1;"), row(1, 1, 1, null));
    assertRowsIgnoringOrder(executeView("SELECT k,c,b from %s WHERE k = 1 AND c = 1;"), row(1, 1, null));
    // sstable-3
    updateViewWithFlush("Update %s set a = null where k = 1 AND c = 1;", flush);
    assertRowsIgnoringOrder(execute("SELECT * from %s WHERE k = 1 AND c = 1;"));
    assertRowsIgnoringOrder(executeView("SELECT k,c,b from %s WHERE k = 1 AND c = 1;"));
    // sstable-4
    updateViewWithFlush("Update %s USING TIMESTAMP 1 set b = 1 where k = 1 AND c = 1;", flush);
    assertRowsIgnoringOrder(execute("SELECT * from %s WHERE k = 1 AND c = 1;"), row(1, 1, null, 1));
    assertRowsIgnoringOrder(executeView("SELECT k,c,b from %s WHERE k = 1 AND c = 1;"), row(1, 1, 1));
}
Also used : Keyspace(org.apache.cassandra.db.Keyspace)

Example 44 with Keyspace

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

the class ViewComplexTTLTest method testUpdateColumnInViewPKWithTTL.

private void testUpdateColumnInViewPKWithTTL(boolean flush) throws Throwable {
    // CASSANDRA-13657 if base column used in view pk is ttled, then view row is considered dead
    createTable("create table %s (k int primary key, a int, b int)");
    Keyspace ks = Keyspace.open(keyspace());
    createView("CREATE MATERIALIZED VIEW %s AS SELECT * FROM %s " + "WHERE k IS NOT NULL AND a IS NOT NULL PRIMARY KEY (a, k)");
    ks.getColumnFamilyStore(currentView()).disableAutoCompaction();
    updateView("UPDATE %s SET a = 1 WHERE k = 1;");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRows(execute("SELECT * from %s"), row(1, 1, null));
    assertRows(executeView("SELECT * from %s"), row(1, 1, null));
    updateView("DELETE a FROM %s WHERE k = 1");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRows(execute("SELECT * from %s"));
    assertEmpty(executeView("SELECT * from %s"));
    updateView("INSERT INTO %s (k) VALUES (1);");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRows(execute("SELECT * from %s"), row(1, null, null));
    assertEmpty(executeView("SELECT * from %s"));
    updateView("UPDATE %s USING TTL 5 SET a = 10 WHERE k = 1;");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRows(execute("SELECT * from %s"), row(1, 10, null));
    assertRows(executeView("SELECT * from %s"), row(10, 1, null));
    updateView("UPDATE %s SET b = 100 WHERE k = 1;");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRows(execute("SELECT * from %s"), row(1, 10, 100));
    assertRows(executeView("SELECT * from %s"), row(10, 1, 100));
    Thread.sleep(5000);
    // 'a' is TTL of 5 and removed.
    assertRows(execute("SELECT * from %s"), row(1, null, 100));
    assertEmpty(executeView("SELECT * from %s"));
    assertEmpty(executeView("SELECT * from %s WHERE k = ? AND a = ?", 1, 10));
    updateView("DELETE b FROM %s WHERE k=1");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertRows(execute("SELECT * from %s"), row(1, null, null));
    assertEmpty(executeView("SELECT * from %s"));
    updateView("DELETE FROM %s WHERE k=1;");
    if (flush)
        FBUtilities.waitOnFutures(ks.flush());
    assertEmpty(execute("SELECT * from %s"));
    assertEmpty(executeView("SELECT * from %s"));
}
Also used : Keyspace(org.apache.cassandra.db.Keyspace)

Example 45 with Keyspace

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

the class ViewComplexTest method testMVWithDifferentColumns.

private void testMVWithDifferentColumns(boolean flush) throws Throwable {
    createTable("CREATE TABLE %s (a int, b int, c int, d int, e int, f int, PRIMARY KEY(a, b))");
    List<String> viewNames = new ArrayList<>();
    List<String> mvStatements = Arrays.asList(// all selected
    "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %s WHERE a IS NOT NULL AND b IS NOT NULL PRIMARY KEY (a,b)", // unselected e,f
    "CREATE MATERIALIZED VIEW %s AS SELECT a,b,c,d FROM %s WHERE a IS NOT NULL AND b IS NOT NULL PRIMARY KEY (a,b)", // no selected
    "CREATE MATERIALIZED VIEW %s AS SELECT a,b FROM %s WHERE a IS NOT NULL AND b IS NOT NULL PRIMARY KEY (a,b)", // all selected, re-order keys
    "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %s WHERE a IS NOT NULL AND b IS NOT NULL PRIMARY KEY (b,a)", // unselected e,f, re-order keys
    "CREATE MATERIALIZED VIEW %s AS SELECT a,b,c,d FROM %s WHERE a IS NOT NULL AND b IS NOT NULL PRIMARY KEY (b,a)", // no selected, re-order keys
    "CREATE MATERIALIZED VIEW %s AS SELECT a,b FROM %s WHERE a IS NOT NULL AND b IS NOT NULL PRIMARY KEY (b,a)");
    Keyspace ks = Keyspace.open(keyspace());
    for (int i = 0; i < mvStatements.size(); i++) {
        String name = createView(mvStatements.get(i));
        viewNames.add(name);
        ks.getColumnFamilyStore(name).disableAutoCompaction();
    }
    // insert
    updateViewWithFlush("INSERT INTO %s (a,b,c,d,e,f) VALUES(1,1,1,1,1,1) using timestamp 1", flush);
    assertBaseViews(row(1, 1, 1, 1, 1, 1), viewNames);
    updateViewWithFlush("UPDATE %s using timestamp 2 SET c=0, d=0 WHERE a=1 AND b=1", flush);
    assertBaseViews(row(1, 1, 0, 0, 1, 1), viewNames);
    updateViewWithFlush("UPDATE %s using timestamp 2 SET e=0, f=0 WHERE a=1 AND b=1", flush);
    assertBaseViews(row(1, 1, 0, 0, 0, 0), viewNames);
    updateViewWithFlush("DELETE FROM %s using timestamp 2 WHERE a=1 AND b=1", flush);
    assertBaseViews(null, viewNames);
    // partial update unselected, selected
    updateViewWithFlush("UPDATE %s using timestamp 3 SET f=1 WHERE a=1 AND b=1", flush);
    assertBaseViews(row(1, 1, null, null, null, 1), viewNames);
    updateViewWithFlush("UPDATE %s using timestamp 4 SET e = 1, f=null WHERE a=1 AND b=1", flush);
    assertBaseViews(row(1, 1, null, null, 1, null), viewNames);
    updateViewWithFlush("UPDATE %s using timestamp 4 SET e = null WHERE a=1 AND b=1", flush);
    assertBaseViews(null, viewNames);
    updateViewWithFlush("UPDATE %s using timestamp 5 SET c = 1 WHERE a=1 AND b=1", flush);
    assertBaseViews(row(1, 1, 1, null, null, null), viewNames);
    updateViewWithFlush("UPDATE %s using timestamp 5 SET c = null WHERE a=1 AND b=1", flush);
    assertBaseViews(null, viewNames);
    updateViewWithFlush("UPDATE %s using timestamp 6 SET d = 1 WHERE a=1 AND b=1", flush);
    assertBaseViews(row(1, 1, null, 1, null, null), viewNames);
    updateViewWithFlush("UPDATE %s using timestamp 7 SET d = null WHERE a=1 AND b=1", flush);
    assertBaseViews(null, viewNames);
    updateViewWithFlush("UPDATE %s using timestamp 8 SET f = 1 WHERE a=1 AND b=1", flush);
    assertBaseViews(row(1, 1, null, null, null, 1), viewNames);
    updateViewWithFlush("UPDATE %s using timestamp 6 SET c = 1 WHERE a=1 AND b=1", flush);
    assertBaseViews(row(1, 1, 1, null, null, 1), viewNames);
    // view row still alive due to c=1@6
    updateViewWithFlush("UPDATE %s using timestamp 8 SET f = null WHERE a=1 AND b=1", flush);
    assertBaseViews(row(1, 1, 1, null, null, null), viewNames);
    updateViewWithFlush("UPDATE %s using timestamp 6 SET c = null WHERE a=1 AND b=1", flush);
    assertBaseViews(null, viewNames);
}
Also used : Keyspace(org.apache.cassandra.db.Keyspace) ArrayList(java.util.ArrayList)

Aggregations

Keyspace (org.apache.cassandra.db.Keyspace)163 ColumnFamilyStore (org.apache.cassandra.db.ColumnFamilyStore)100 Test (org.junit.Test)73 SSTableReader (org.apache.cassandra.io.sstable.format.SSTableReader)66 RowUpdateBuilder (org.apache.cassandra.db.RowUpdateBuilder)29 Token (org.apache.cassandra.dht.Token)28 LifecycleTransaction (org.apache.cassandra.db.lifecycle.LifecycleTransaction)27 ArrayList (java.util.ArrayList)18 DecoratedKey (org.apache.cassandra.db.DecoratedKey)17 ByteBuffer (java.nio.ByteBuffer)13 SystemKeyspace (org.apache.cassandra.db.SystemKeyspace)13 CompactionController (org.apache.cassandra.db.compaction.CompactionController)13 TableMetadata (org.apache.cassandra.schema.TableMetadata)13 CompactionIterator (org.apache.cassandra.db.compaction.CompactionIterator)12 Range (org.apache.cassandra.dht.Range)12 AbstractReplicationStrategy (org.apache.cassandra.locator.AbstractReplicationStrategy)12 InetAddressAndPort (org.apache.cassandra.locator.InetAddressAndPort)12 IOException (java.io.IOException)11 List (java.util.List)11 Map (java.util.Map)11