Skip to content

Commit

Permalink
Improve cleanup of tables and types in CQL unit tests
Browse files Browse the repository at this point in the history
Patch by Benjamin Lerer; reviewed by Tyler Hobbs for CASSANDRA-8427
  • Loading branch information
blerer authored and thobbs committed Dec 19, 2014
1 parent d9f5702 commit 9775a9c
Showing 1 changed file with 46 additions and 26 deletions.
72 changes: 46 additions & 26 deletions test/unit/org/apache/cassandra/cql3/CQLTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -79,13 +79,10 @@ public static void tearDownClass()
@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()
Expand All @@ -94,10 +91,11 @@ public void run()
{
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
Expand All @@ -114,7 +112,7 @@ public void run()
});
latch.await(2, TimeUnit.SECONDS);

removeAllSSTables(KEYSPACE, tableToDrop);
removeAllSSTables(KEYSPACE, tablesToDrop);
}
catch (Exception e)
{
Expand All @@ -124,10 +122,20 @@ public void run()
});
}

/**
* 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();
}
Expand All @@ -146,47 +154,59 @@ public boolean usePrepared()
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;
}

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;
}

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);
}

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
Expand All @@ -201,14 +221,14 @@ protected void createTableMayThrow(String query) throws Throwable

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
{
Expand All @@ -222,21 +242,21 @@ protected void alterTableMayThrow(String query) throws Throwable

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
{
Expand Down Expand Up @@ -270,14 +290,14 @@ private static void schemaChange(String query)

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)
Expand Down

0 comments on commit 9775a9c

Please sign in to comment.