Repository: cassandra Updated Branches: refs/heads/trunk 4bfba6409 -> f45a1941c
Improve cleanup of tables and types in CQL unit tests Patch by Benjamin Lerer; reviewed by Tyler Hobbs for CASSANDRA-8427 Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/9775a9c6 Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/9775a9c6 Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/9775a9c6 Branch: refs/heads/trunk Commit: 9775a9c6808b8ed3cf37ca04d3b701284b459893 Parents: d9f5702 Author: blerer <b_le...@hotmail.com> Authored: Fri Dec 19 12:04:23 2014 +0100 Committer: Tyler Hobbs <tylerho...@apache.org> Committed: Fri Dec 19 13:03:15 2014 -0600 ---------------------------------------------------------------------- .../org/apache/cassandra/cql3/CQLTester.java | 72 +++++++++++++------- 1 file changed, 46 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/9775a9c6/test/unit/org/apache/cassandra/cql3/CQLTester.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/cql3/CQLTester.java b/test/unit/org/apache/cassandra/cql3/CQLTester.java index 2226153..bdd761d 100644 --- a/test/unit/org/apache/cassandra/cql3/CQLTester.java +++ b/test/unit/org/apache/cassandra/cql3/CQLTester.java @@ -62,8 +62,8 @@ public abstract class CQLTester SchemaLoader.prepareServer(); } - private String currentTable; - private final Set<String> currentTypes = new HashSet<>(); + private List<String> tables = new ArrayList<>(); + private List<String> types = new ArrayList<>(); @BeforeClass public static void setUpClass() throws Throwable @@ -79,13 +79,10 @@ public abstract class CQLTester @After public void afterTest() throws Throwable { - if (currentTable == null) - return; - - final String tableToDrop = currentTable; - final Set<String> typesToDrop = currentTypes.isEmpty() ? Collections.emptySet() : new HashSet(currentTypes); - currentTable = null; - currentTypes.clear(); + final List<String> tablesToDrop = copy(tables); + final List<String> typesToDrop = copy(types); + tables = null; + types = null; // We want to clean up after the test, but dropping a table is rather long so just do that asynchronously ScheduledExecutors.optionalTasks.execute(new Runnable() @@ -94,10 +91,11 @@ public abstract class CQLTester { try { - schemaChange(String.format("DROP TABLE IF EXISTS %s.%s", KEYSPACE, tableToDrop)); + for (int i = tablesToDrop.size() - 1; i >=0; i--) + schemaChange(String.format("DROP TABLE IF EXISTS %s.%s", KEYSPACE, tablesToDrop.get(i))); - for (String typeName : typesToDrop) - schemaChange(String.format("DROP TYPE IF EXISTS %s.%s", KEYSPACE, typeName)); + for (int i = typesToDrop.size() - 1; i >=0; i--) + schemaChange(String.format("DROP TYPE IF EXISTS %s.%s", KEYSPACE, typesToDrop.get(i))); // Dropping doesn't delete the sstables. It's not a huge deal but it's cleaner to cleanup after us // Thas said, we shouldn't delete blindly before the SSTableDeletingTask for the table we drop @@ -114,7 +112,7 @@ public abstract class CQLTester }); latch.await(2, TimeUnit.SECONDS); - removeAllSSTables(KEYSPACE, tableToDrop); + removeAllSSTables(KEYSPACE, tablesToDrop); } catch (Exception e) { @@ -124,10 +122,20 @@ public abstract class CQLTester }); } + /** + * Returns a copy of the specified list. + * @return a copy of the specified list. + */ + private static List<String> copy(List<String> list) + { + return list.isEmpty() ? Collections.<String>emptyList() : new ArrayList<>(list); + } + public void flush() { try { + String currentTable = currentTable(); if (currentTable != null) Keyspace.open(KEYSPACE).getColumnFamilyStore(currentTable).forceFlush().get(); } @@ -146,16 +154,24 @@ public abstract class CQLTester return USE_PREPARED_VALUES; } - private static void removeAllSSTables(String ks, String table) + private static void removeAllSSTables(String ks, List<String> tables) { // clean up data directory which are stored as data directory/keyspace/data files for (File d : Directories.getKSChildDirectories(ks)) { - if (d.exists() && d.getName().contains(table)) + if (d.exists() && containsAny(d.getName(), tables)) FileUtils.deleteRecursive(d); } } + private static boolean containsAny(String filename, List<String> tables) + { + for (int i = 0, m = tables.size(); i < m; i++) + if (filename.contains(tables.get(i))) + return true; + return false; + } + protected String keyspace() { return KEYSPACE; @@ -163,14 +179,16 @@ public abstract class CQLTester protected String currentTable() { - return currentTable; + if (tables.isEmpty()) + return null; + return tables.get(tables.size() - 1); } protected String createType(String query) { String typeName = "type_" + seqNumber.getAndIncrement(); String fullQuery = String.format(query, KEYSPACE + "." + typeName); - currentTypes.add(typeName); + types.add(typeName); logger.info(fullQuery); schemaChange(fullQuery); return typeName; @@ -178,7 +196,8 @@ public abstract class CQLTester protected void createTable(String query) { - currentTable = "table_" + seqNumber.getAndIncrement(); + String currentTable = "table_" + seqNumber.getAndIncrement(); + tables.add(currentTable); String fullQuery = String.format(query, KEYSPACE + "." + currentTable); logger.info(fullQuery); schemaChange(fullQuery); @@ -186,7 +205,8 @@ public abstract class CQLTester protected void createTableMayThrow(String query) throws Throwable { - currentTable = "table_" + seqNumber.getAndIncrement(); + String currentTable = "table_" + seqNumber.getAndIncrement(); + tables.add(currentTable); String fullQuery = String.format(query, KEYSPACE + "." + currentTable); logger.info(fullQuery); try @@ -201,14 +221,14 @@ public abstract class CQLTester protected void alterTable(String query) { - String fullQuery = String.format(query, KEYSPACE + "." + currentTable); + String fullQuery = String.format(query, KEYSPACE + "." + currentTable()); logger.info(fullQuery); schemaChange(fullQuery); } protected void alterTableMayThrow(String query) throws Throwable { - String fullQuery = String.format(query, KEYSPACE + "." + currentTable); + String fullQuery = String.format(query, KEYSPACE + "." + currentTable()); logger.info(fullQuery); try { @@ -222,21 +242,21 @@ public abstract class CQLTester protected void dropTable(String query) { - String fullQuery = String.format(query, KEYSPACE + "." + currentTable); + String fullQuery = String.format(query, KEYSPACE + "." + currentTable()); logger.info(fullQuery); schemaChange(fullQuery); } protected void createIndex(String query) { - String fullQuery = String.format(query, KEYSPACE + "." + currentTable); + String fullQuery = String.format(query, KEYSPACE + "." + currentTable()); logger.info(fullQuery); schemaChange(fullQuery); } protected void createIndexMayThrow(String query) throws Throwable { - String fullQuery = String.format(query, KEYSPACE + "." + currentTable); + String fullQuery = String.format(query, KEYSPACE + "." + currentTable()); logger.info(fullQuery); try { @@ -270,14 +290,14 @@ public abstract class CQLTester protected CFMetaData currentTableMetadata() { - return Schema.instance.getCFMetaData(KEYSPACE, currentTable); + return Schema.instance.getCFMetaData(KEYSPACE, currentTable()); } protected UntypedResultSet execute(String query, Object... values) throws Throwable { try { - query = currentTable == null ? query : String.format(query, KEYSPACE + "." + currentTable); + query = currentTable() == null ? query : String.format(query, KEYSPACE + "." + currentTable()); UntypedResultSet rs; if (USE_PREPARED_VALUES)