Repository: cassandra Updated Branches: refs/heads/cassandra-2.2 5a54d9a62 -> 5c59d5af7
Add unit tests for enable/disable autocompaction Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/1a2c1bcd Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/1a2c1bcd Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/1a2c1bcd Branch: refs/heads/cassandra-2.2 Commit: 1a2c1bcdc7267abec9b19d77726aedbb045d79a8 Parents: b724d1e Author: Marcus Eriksson <[email protected]> Authored: Tue Aug 4 09:51:55 2015 +0200 Committer: Marcus Eriksson <[email protected]> Committed: Tue Aug 4 09:51:55 2015 +0200 ---------------------------------------------------------------------- .../db/compaction/CompactionsCQLTest.java | 118 ++++++++++++++++++- 1 file changed, 113 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/1a2c1bcd/test/unit/org/apache/cassandra/db/compaction/CompactionsCQLTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/db/compaction/CompactionsCQLTest.java b/test/unit/org/apache/cassandra/db/compaction/CompactionsCQLTest.java index 76e3b88..58fc062 100644 --- a/test/unit/org/apache/cassandra/db/compaction/CompactionsCQLTest.java +++ b/test/unit/org/apache/cassandra/db/compaction/CompactionsCQLTest.java @@ -21,32 +21,140 @@ import org.junit.Test; import org.apache.cassandra.cql3.CQLTester; import org.apache.cassandra.cql3.UntypedResultSet; +import org.apache.cassandra.db.ColumnFamilyStore; import org.apache.cassandra.db.Keyspace; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class CompactionsCQLTest extends CQLTester { @Test - public void testTriggerMinorCompaction() throws Throwable + public void testTriggerMinorCompactionSTCS() throws Throwable { - createTable("CREATE TABLE %s (id text PRIMARY KEY);"); - assertTrue(Keyspace.open(KEYSPACE).getColumnFamilyStore(currentTable()).getCompactionStrategy().isEnabled()); + createTable("CREATE TABLE %s (id text PRIMARY KEY) WITH compaction = {'class':'SizeTieredCompactionStrategy', 'min_threshold':2};"); + assertTrue(getCurrentColumnFamilyStore().getCompactionStrategy().isEnabled()); execute("insert into %s (id) values ('1')"); flush(); execute("insert into %s (id) values ('1')"); flush(); + Thread.sleep(1000); + assertTrue(minorWasTriggered(KEYSPACE, currentTable())); + } + + @Test + public void testTriggerMinorCompactionLCS() throws Throwable + { + createTable("CREATE TABLE %s (id text PRIMARY KEY) WITH compaction = {'class':'LeveledCompactionStrategy', 'sstable_size_in_mb':1};"); + assertTrue(getCurrentColumnFamilyStore().getCompactionStrategy().isEnabled()); + execute("insert into %s (id) values ('1')"); + flush(); + execute("insert into %s (id) values ('1')"); + flush(); + Thread.sleep(1000); + assertTrue(minorWasTriggered(KEYSPACE, currentTable())); + } + + + @Test + public void testTriggerMinorCompactionDTCS() throws Throwable + { + createTable("CREATE TABLE %s (id text PRIMARY KEY) WITH compaction = {'class':'DateTieredCompactionStrategy', 'min_threshold':2};"); + assertTrue(getCurrentColumnFamilyStore().getCompactionStrategy().isEnabled()); + execute("insert into %s (id) values ('1')"); + flush(); + execute("insert into %s (id) values ('1')"); + flush(); + Thread.sleep(1000); + assertTrue(minorWasTriggered(KEYSPACE, currentTable())); + } + + @Test + public void testTriggerNoMinorCompactionSTCSDisabled() throws Throwable + { + createTable("CREATE TABLE %s (id text PRIMARY KEY) WITH compaction = {'class':'SizeTieredCompactionStrategy', 'min_threshold':2, 'enabled':false};"); + assertFalse(getCurrentColumnFamilyStore().getCompactionStrategy().isEnabled()); + execute("insert into %s (id) values ('1')"); + flush(); + execute("insert into %s (id) values ('1')"); + flush(); + Thread.sleep(1000); + assertFalse(minorWasTriggered(KEYSPACE, currentTable())); + } + + @Test + public void testTriggerMinorCompactionSTCSNodetoolEnabled() throws Throwable + { + createTable("CREATE TABLE %s (id text PRIMARY KEY) WITH compaction = {'class':'SizeTieredCompactionStrategy', 'min_threshold':2, 'enabled':false};"); + assertFalse(getCurrentColumnFamilyStore().getCompactionStrategy().isEnabled()); + getCurrentColumnFamilyStore().enableAutoCompaction(); + assertTrue(getCurrentColumnFamilyStore().getCompactionStrategy().isEnabled()); execute("insert into %s (id) values ('1')"); flush(); execute("insert into %s (id) values ('1')"); flush(); Thread.sleep(1000); + assertTrue(minorWasTriggered(KEYSPACE, currentTable())); + } + + @Test + public void testTriggerNoMinorCompactionSTCSNodetoolDisabled() throws Throwable + { + createTable("CREATE TABLE %s (id text PRIMARY KEY) WITH compaction = {'class':'SizeTieredCompactionStrategy', 'min_threshold':2, 'enabled':true};"); + assertTrue(getCurrentColumnFamilyStore().getCompactionStrategy().isEnabled()); + getCurrentColumnFamilyStore().disableAutoCompaction(); + assertFalse(getCurrentColumnFamilyStore().getCompactionStrategy().isEnabled()); + execute("insert into %s (id) values ('1')"); + flush(); + execute("insert into %s (id) values ('1')"); + flush(); + Thread.sleep(1000); + assertFalse(minorWasTriggered(KEYSPACE, currentTable())); + } + + @Test + public void testTriggerNoMinorCompactionSTCSAlterTable() throws Throwable + { + createTable("CREATE TABLE %s (id text PRIMARY KEY) WITH compaction = {'class':'SizeTieredCompactionStrategy', 'min_threshold':2, 'enabled':true};"); + assertTrue(getCurrentColumnFamilyStore().getCompactionStrategy().isEnabled()); + execute("ALTER TABLE %s WITH compaction = {'class': 'SizeTieredCompactionStrategy', 'enabled': false}"); + assertFalse(getCurrentColumnFamilyStore().getCompactionStrategy().isEnabled()); + execute("insert into %s (id) values ('1')"); + flush(); + execute("insert into %s (id) values ('1')"); + flush(); + Thread.sleep(1000); + assertFalse(minorWasTriggered(KEYSPACE, currentTable())); + } + + @Test + public void testTriggerMinorCompactionSTCSAlterTable() throws Throwable + { + createTable("CREATE TABLE %s (id text PRIMARY KEY) WITH compaction = {'class':'SizeTieredCompactionStrategy', 'min_threshold':2, 'enabled':false};"); + assertFalse(getCurrentColumnFamilyStore().getCompactionStrategy().isEnabled()); + execute("ALTER TABLE %s WITH compaction = {'class': 'SizeTieredCompactionStrategy', 'min_threshold': 2, 'enabled': true}"); + assertTrue(getCurrentColumnFamilyStore().getCompactionStrategy().isEnabled()); + execute("insert into %s (id) values ('1')"); + flush(); + execute("insert into %s (id) values ('1')"); + flush(); + Thread.sleep(1000); + assertTrue(minorWasTriggered(KEYSPACE, currentTable())); + } + + private ColumnFamilyStore getCurrentColumnFamilyStore() + { + return Keyspace.open(KEYSPACE).getColumnFamilyStore(currentTable()); + } + + public boolean minorWasTriggered(String keyspace, String cf) throws Throwable + { UntypedResultSet res = execute("SELECT * FROM system.compaction_history"); boolean minorWasTriggered = false; for (UntypedResultSet.Row r : res) { - if (r.getString("keyspace_name").equals(KEYSPACE) && r.getString("columnfamily_name").equals(currentTable())) + if (r.getString("keyspace_name").equals(keyspace) && r.getString("columnfamily_name").equals(cf)) minorWasTriggered = true; } - assertTrue(minorWasTriggered); + return minorWasTriggered; } }
