如何从嵌入式cassandra获取描述性错误消息

时间:2019-05-29 06:53:05

标签: cassandra-3.0

我正在使用embedded cassandra来运行单元测试。我注意到,如果任何cql语句失败,那么我看不到任何描述性的失败原因。例如。我运行以下两条语句失败,因为我试图在不切换到键空间的情况下添加表

val statement1 =
        """
          |CREATE KEYSPACE test
          |  WITH REPLICATION = {
          |   'class' : 'SimpleStrategy',
          |   'replication_factor' : 1
          |  };
        """.stripMargin

      val statement3 =
        """
            |CREATE TABLE users (
            |    bucket int,
            |    email text,
            |    firstname text,
            |    lastname text,
            |    authprovider text,
            |    password text,
            |    confirmed boolean,
            |    id UUID,
            |    hasher text,
            |    salt text,
            |    PRIMARY KEY ((bucket, email), authprovider,firstname, lastname) )
        """.stripMargin

val cqlStatements:CqlStatements = new CqlStatements(statement1,statement3)

")
      val testCassandra = repoTestEnv.testCassandra


      try {
        testCassandra.start()
        testCassandra.executeScripts(cqlStatements)
      } finally testCassandra.stop()

但是我没有看到正确的错误。我看到以下内容并不能确切说明问题所在

[info] c.g.n.e.c.l.WindowsCassandraNode - Apache Cassandra Node '7276' is started
[info] c.g.n.e.c.l.LocalCassandraDatabase - Apache Cassandra '3.11.1' is started (20811 ms)
[warn] c.d.d.c.Connection - /127.0.0.1:9042 did not send an authentication challenge; This is suspicious because the driver expects authentication (configured auth provider = com.datastax.driver.core.PlainTextAuthProvider)
[warn] c.d.d.c.Connection - /127.0.0.1:9042 did not send an authentication challenge; This is suspicious because the driver expects authentication (configured auth provider = com.datastax.driver.core.PlainTextAuthProvider)
[debug] c.g.n.e.c.t.u.CqlUtils - Executing Script: CqlStatements [
CREATE KEYSPACE test
  WITH REPLICATION = {
   'class' : 'SimpleStrategy',
   'replication_factor' : 1
  };
        ,
CREATE TABLE users (
    bucket int,
    email text,
    firstname text,
    lastname text,
    authprovider text,
    password text,
    confirmed boolean,
    id UUID,
    hasher text,
    salt text,
    PRIMARY KEY ((bucket, email), authprovider,firstname, lastname) )
        ]
[debug] c.g.n.e.c.t.u.CqlUtils - Executing Statement: 
CREATE KEYSPACE test
  WITH REPLICATION = {
   'class' : 'SimpleStrategy',
   'replication_factor' : 1
  };

[info] c.g.n.e.c.Cassandra - INFO  [Native-Transport-Requests-1] 2019-05-29 07:50:00,788 MigrationManager.java:310 - Create new Keyspace: KeyspaceMetadata{name=test, params=KeyspaceParams{durable_writes=true, replication=ReplicationParams{class=org.apache.cassandra.locator.SimpleStrategy, replication_factor=1}}, tables=[], views=[], functions=[], types=[]}
[debug] c.g.n.e.c.t.u.CqlUtils - Executing Statement: 
CREATE TABLE users (
    bucket int,
    email text,
    firstname text,
    lastname text,
    authprovider text,
    password text,
    confirmed boolean,
    id UUID,
    hasher text,
    salt text,
    PRIMARY KEY ((bucket, email), authprovider,firstname, lastname) )

[debug] c.g.n.e.c.t.TestCassandra - Stop TestCassandra 3.11.1
[info] c.g.n.e.c.l.LocalCassandraDatabase - Stop Apache Cassandra '3.11.1'
[debug] c.g.n.e.c.l.RunProcess - Execute 'powershell -ExecutionPolicy Unrestricted C:\Users\manu\AppData\Local\Temp\embedded-cassandra\3.11.1\0d155e04-97d5-4927-87ac-d46824a77c32\bin\stop-server.ps1 -p C:\Users\manu\AppData\Local\Temp\embedded-cassandra\3.11.1\0d155e04-97d5-4927-87ac-d46824a77c32\1da63488-2624-4141-a49e-174203b7edc4' within a directory 'C:\Users\manu\AppData\Local\Temp\embedded-cassandra\3.11.1\0d155e04-97d5-4927-87ac-d46824a77c32'
[info] c.g.n.e.c.Cassandra - INFO  [StorageServiceShutdownHook] 2019-05-29 07:50:03,926 HintsService.java:220 - Paused hints dispatch
[info] c.g.n.e.c.Cassandra - INFO  [StorageServiceShutdownHook] 2019-05-29 07:50:03,933 Server.java:176 - Stop listening for CQL clients
[info] c.g.n.e.c.Cassandra - INFO  [StorageServiceShutdownHook] 2019-05-29 07:50:03,934 Gossiper.java:1532 - Announcing shutdown
[info] c.g.n.e.c.Cassandra - INFO  [StorageServiceShutdownHook] 2019-05-29 07:50:03,938 StorageService.java:2268 - Node localhost/127.0.0.1 state jump to shutdown
[info] c.g.n.e.c.Cassandra - INFO  [StorageServiceShutdownHook] 2019-05-29 07:50:05,941 MessagingService.java:984 - Waiting for messaging service to quiesce
[info] c.g.n.e.c.Cassandra - INFO  [ACCEPT-localhost/127.0.0.1] 2019-05-29 07:50:05,948 MessagingService.java:1338 - MessagingService has terminated the accept() thread
[info] c.g.n.e.c.Cassandra - INFO  [StorageServiceShutdownHook] 2019-05-29 07:50:06,076 HintsService.java:220 - Paused hints dispatch
[info] c.g.n.e.c.l.WindowsCassandraNode - Successfully sent ctrl+c to process with id: 7276.
[info] c.g.n.e.c.l.WindowsCassandraNode - Apache Cassandra Node '7276' is stopped
[info] c.g.n.e.c.l.LocalCassandraDatabase - Apache Cassandra '3.11.1' is stopped (3490 ms)
[info] c.g.n.e.c.l.LocalCassandraDatabase - The working directory 'C:\Users\manu\AppData\Local\Temp\embedded-cassandra\3.11.1\0d155e04-97d5-4927-87ac-d46824a77c32' was deleted.
[debug] c.g.n.e.c.t.TestCassandra - TestCassandra 3.11.1 is stopped

Unable to start TestCassandra 3.11.1
com.github.nosan.embedded.cassandra.CassandraException: Unable to start TestCassandra 3.11.1
    at com.github.nosan.embedded.cassandra.test.TestCassandra.start(TestCassandra.java:128)
    at UnitSpecs.RepositorySpecs.UsersRepositorySpecs.$anonfun$new$3(UsersRepositorySpecs.scala:146)
    at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)

理想情况下,我应该得到与使用cqlsh时类似的错误

有没有办法获得更多描述性错误?

3 个答案:

答案 0 :(得分:0)

我试图重现您的问题,但是没有运气。

import com.github.nosan.embedded.cassandra.cql.CqlScript;
import com.github.nosan.embedded.cassandra.test.TestCassandra;

class Scratch {

    public static void main(String[] args) {

        TestCassandra testCassandra = new TestCassandra(CqlScript.statements(createKeyspace(),
                createUserTable()));
        testCassandra.start();
        try {
            System.out.println(testCassandra.getSettings());
        }
        finally {
            testCassandra.stop();
        }
    }

    private static String createUserTable() {
        return "CREATE TABLE users ( bucket int, "
                + "email text, "
                + "firstname text, "
                + "lastname text, "
                + "authprovider text, "
                + "password text, "
                + "confirmed boolean, "
                + "id UUID, hasher text, "
                + "salt text, "
                + "PRIMARY KEY ((bucket, email), authprovider,firstname, lastname) )";
    }

    private static String createKeyspace() {
        return "CREATE KEYSPACE test WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1}";
    }

}

输出:

Exception in thread "main" com.github.nosan.embedded.cassandra.CassandraException: Unable to start TestCassandra 3.11.4
    at com.github.nosan.embedded.cassandra.test.TestCassandra.start(TestCassandra.java:156)
    at com.github.nosan.embedded.cassandra.Scratch.main(Scratch.java:27)
Caused by: com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: No keyspace has been specified. USE a keyspace, or explicitly specify keyspace.tablename
    at com.datastax.oss.driver.api.core.servererrors.InvalidQueryException.copy(InvalidQueryException.java:48)
    at com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures.getUninterruptibly(CompletableFutures.java:113)
    at com.datastax.oss.driver.internal.core.cql.CqlRequestSyncProcessor.process(CqlRequestSyncProcessor.java:53)
    at com.datastax.oss.driver.internal.core.cql.CqlRequestSyncProcessor.process(CqlRequestSyncProcessor.java:30)
    at com.datastax.oss.driver.internal.core.session.DefaultSession.execute(DefaultSession.java:207)
    at com.datastax.oss.driver.api.core.CqlSession.execute(CqlSession.java:47)
    at com.datastax.oss.driver.api.core.CqlSession.execute(CqlSession.java:56)
    at com.github.nosan.embedded.cassandra.test.util.CqlUtils.execute(CqlUtils.java:68)
    at com.github.nosan.embedded.cassandra.test.util.CqlUtils.execute(CqlUtils.java:47)
    at com.github.nosan.embedded.cassandra.test.util.CqlSessionUtils.execute(CqlSessionUtils.java:43)
    at com.github.nosan.embedded.cassandra.test.CqlSessionConnection.execute(CqlSessionConnection.java:60)
    at com.github.nosan.embedded.cassandra.test.DefaultConnection.execute(DefaultConnection.java:53)
    at com.github.nosan.embedded.cassandra.test.TestCassandra.executeScripts(TestCassandra.java:256)
    at com.github.nosan.embedded.cassandra.test.TestCassandra.doStart(TestCassandra.java:285)
    at com.github.nosan.embedded.cassandra.test.TestCassandra.start(TestCassandra.java:147)

答案 1 :(得分:0)

我无法找到未打印的Caused,但是找到了解决方法

  try {
    testCassandra.start()
    println(s"cassandra state is ${testCassandra.getState}")
    testCassandra.executeScripts(cqlStatements)
    //println(s"result of execution is ${result}")
    //val settings = testCassandra.getSettings
    //println(s"settings are ${settings}")
  } catch {
    case e:Exception => {
      println(s"exception ${e} caused by ${e.getCause}")
      //println(s"caused by ${e.getCause()}")
     fail( new Throwable(e.getCause))
    }
  }finally {
    testCassandra.stop()

  }

上面的照片

org.scalatest.exceptions.TestFailedException was thrown.
ScalaTestFailureLocation: UnitSpecs.RepositorySpecs.UsersRepositorySpecs at (UsersRepositorySpecs.scala:157)
...
Caused by: java.lang.Throwable: com.datastax.driver.core.exceptions.InvalidQueryException: No keyspace has been specified. USE a keyspace, or explicitly specify keyspace.tablename

答案 2 :(得分:0)

我找到了原因。看来我没有正确使用TestCassandra。我没有意识到,如果我创建TestCassandra并在实例化时还指定了cql语句,那么start方法也会运行查询。在我的代码中,我如下创建TestCassandra

new TestCassandra(factory,cqlStatements)})

并同时呼叫startexecuteScripts

testCassandra.start()
testCassandra.executeScripts(cqlStatements)

我评论了executeScripts行,现在我同时看到了ExceptionCaused

我认为,如果API明确提到start也具有执行语句的副作用,那会更好。