关闭EmbeddedCassandraServerHelper时获取CancellationException

时间:2013-06-08 21:07:28

标签: java unit-testing cassandra

我正在编写一个使用cassandra-unit的商店单元测试。

public class StoreTest {

  public void before() throws Exception {
    EmbeddedCassandraServerHelper.startEmbeddedCassandra();
    DataLoader dataLoader = new DataLoader("mydata", "127.0.0.1:9171");
    dataLoader.load(new ClassPathJsonDataSet("cassandra.json"));
  }

  @After
  public void after() throws Exception {
    EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
  }

  @Test
  public void testSomething() {
    // test code
  }
}

测试运行正常,能够连接到内存中的cassandra实例,并返回合理的结果。但是,当我关闭cassandra时,我在日志中遇到以下异常。

17:05:48.666 [StorageServiceShutdownHook] INFO  o.a.cassandra.thrift.ThriftServer - Stop listening to thrift clients
17:05:48.667 [StorageServiceShutdownHook] INFO  org.apache.cassandra.gms.Gossiper - Announcing shutdown
17:05:48.782 [GossipTasks:1] DEBUG o.a.c.c.DebuggableThreadPoolExecutor - Task cancelled
java.util.concurrent.CancellationException: null
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:250) ~[na:1.7.0_21]
    at java.util.concurrent.FutureTask.get(FutureTask.java:111) ~[na:1.7.0_21]
    at org.apache.cassandra.concurrent.DebuggableThreadPoolExecutor.extractThrowable(DebuggableThreadPoolExecutor.java:230) [cassandra-all-1.2.0.jar:1.2.0]
    at org.apache.cassandra.concurrent.DebuggableThreadPoolExecutor.logExceptionsAfterExecute(DebuggableThreadPoolExecutor.java:194) [cassandra-all-1.2.0.jar:1.2.0]
    at org.apache.cassandra.concurrent.DebuggableScheduledThreadPoolExecutor.afterExecute(DebuggableScheduledThreadPoolExecutor.java:46) [cassandra-all-1.2.0.jar:1.2.0]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153) [na:1.7.0_21]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_21]
    at java.lang.Thread.run(Thread.java:722) [na:1.7.0_21]
17:05:49.667 [StorageServiceShutdownHook] INFO  o.a.cassandra.net.MessagingService - Waiting for messaging service to quiesce
17:05:49.668 [ACCEPT-/27.0.0.1] INFO  o.a.cassandra.net.MessagingService - MessagingService shutting down server thread.

这是从哪里来的,如何防止它发生?

2 个答案:

答案 0 :(得分:0)

看起来停止方法存在问题。我的猜测是,仍然关闭的系统的一部分工作线程在完成之前会被中断。 latest EmbeddedCassandraServerHelper代码弃用了stopEmbeddedCassandra方法,并注释它不能完全正常工作。

我们使用自己的代码在我们的单元测试中嵌入了cassandra。我们不使用activate()方法作为EmbeddedCassandraServerHelper类,而是使用CassandraDaemon的init()和start()方法。然后stop()方法似乎有效。 Cassandra并没有完美地嵌入,因为它期望在jsvc下执行。

如果除了令人讨厌的日志消息之外没有任何问题,我只会配置日志记录以禁止该类。否则,尝试使用init()/ start()方法替换EmbeddedCassandraServerHelper中的active()方法调用。

答案 1 :(得分:0)

问题出在DebuggableThreadPoolExecutor.extractThrowable方法中。源代码是:

 public static Throwable extractThrowable(Runnable runnable)
{
    // Check for exceptions wrapped by FutureTask.  We do this by calling get(), which will
    // cause it to throw any saved exception.
    //
    // Complicating things, calling get() on a ScheduledFutureTask will block until the task
    // is cancelled.  Hence, the extra isDone check beforehand.
    if ((runnable instanceof Future<?>) && ((Future<?>) runnable).isDone())
    {
        try
        {
            ((Future<?>) runnable).get();
        }
        catch (InterruptedException e)
        {
            throw new AssertionError(e);
        }
        catch (CancellationException e)
        {
            logger.debug("Task cancelled", e);
        }
        catch (ExecutionException e)
        {
            return e.getCause();
        }
    }

    return null;
}

正如人们可以看到它使用调试警告记录任务的取消。我所做的是将行logger.debug("Task cancelled", e);更改为logger.debug("Task cancelled (see DebuggableThreadPoolExecutor.extractThrowable)");。通过这种方式记录事件,但不会使用引起注意的堆栈跟踪污染日志。