Search in sources :

Example 1 with TruncateException

use of com.datastax.driver.core.exceptions.TruncateException in project titus-control-plane by Netflix.

the class CassandraUtils method truncateTableInternal.

private static boolean truncateTableInternal(CommandContext context, String table) {
    PreparedStatement truncateStatement = context.getTargetSession().prepare("TRUNCATE \"" + table + "\"");
    try {
        context.getTargetCassandraExecutor().executeUpdate(truncateStatement.bind()).toBlocking().firstOrDefault(null);
    } catch (TruncateException e) {
        // Check if the table is empty
        logger.info("Couldn't complete the truncate operation. Checking if the table is empty: {}", table);
        Pair<Object, Object> value = readTwoColumnTable(context.getTargetSession(), table).take(1).toBlocking().firstOrDefault(null);
        if (value == null) {
            // Truncate failed, but the table is empty. It is ok to move on.
            logger.info("Truncate deemed as successful, as the table is empty: {}", table);
            return true;
        }
        if (e.getMessage().contains("Cannot achieve consistency level ALL")) {
            logger.warn("Recoverable truncate operations for table {}. Cause: {}", table, e.getMessage());
            return false;
        }
        // Not recoverable error. Re-throw it.
        throw e;
    }
    logger.info("Truncated table {}.{}", truncateStatement.getQueryKeyspace(), table);
    return true;
}
Also used : TruncateException(com.datastax.driver.core.exceptions.TruncateException) PreparedStatement(com.datastax.driver.core.PreparedStatement) Pair(com.netflix.titus.common.util.tuple.Pair)

Aggregations

PreparedStatement (com.datastax.driver.core.PreparedStatement)1 TruncateException (com.datastax.driver.core.exceptions.TruncateException)1 Pair (com.netflix.titus.common.util.tuple.Pair)1