【问题标题】:Astyanax session not initialized when using a keyspace built with CqlFamilyFactory使用使用 CqlFamilyFactory 构建的键空间时未初始化 Astyanax 会话
【发布时间】:2015-02-04 04:31:47
【问题描述】:

我正在针对 Astyanax 的 EmbeddedCassandra 编写测试用例。 我尝试使用CqlFamilyFactory 构建上下文,并在尝试执行dropKeyspace 时获得NPE:

这是初始化代码:

    ConnectionPoolConfiguration cpConfig = new ConnectionPoolConfigurationImpl("cassandra connection pool")
        .setPort(9171)
        .setSeeds("127.0.0.1:9171")
        .setMaxConnsPerHost(4);

    m_context = new AstyanaxContext.Builder()
        .forCluster("test-cluster")
        .forKeyspace("testkeyspace")
        .withAstyanaxConfiguration(new AstyanaxConfigurationImpl().setDiscoveryType(NodeDiscoveryType.NONE))
        .withConnectionPoolConfiguration(cpConfig)
        .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
        .buildKeyspace(CqlFamilyFactory.getInstance());

    m_context.start();

    m_context.getClient().dropKeyspace();

这将导致 NPE,因为 sessionCqlKeyspaceImpl:276 (astyanax-cql-2.0.1) 处为空:

return new CqlOperationResultImpl<SchemaChangeResult>(session.execute("DROP KEYSPACE " + keyspaceName), null);

我猜我没有指定上下文创建会话所需的内容,但我找不到很多使用 CqlFamilyFactory 的示例。

代码在使用ThriftFamilyFactory 构建键空间时有效。

【问题讨论】:

    标签: cassandra astyanax


    【解决方案1】:

    CqlFamilyFactory初始化session需要调用ConnectionPoolProxy.setHosts,可以通过NodeDiscoveryImpl.update调用。它需要 hostSupplier 可用。

    以下是适用于 TOKEN_AWARE 或 DISCOVERY_SERVICE 的示例代码:

    Supplier<List<Host>> hostSupplier = new Supplier<List<Host>>() {
                @Override
                public List<Host> get() {
                    List<Host> hosts = new ArrayList<>();
                    for (HostAndPort hostAndPort : properties.getSeeds()) {
                        hosts.add(new Host(hostAndPort.toString(), 9160));
                    }
                    return hosts;
                }
            };
    AstyanaxConfiguration astyanaxConfiguration = new AstyanaxConfigurationImpl()
                    .setDiscoveryType(NodeDiscoveryType.TOKEN_AWARE)   
                    .setCqlVersion(properties.getCQLVersion())
                    .setTargetCassandraVersion(properties.getVersion());
    ConnectionPoolConfiguration poolConfiguration = new ConnectionPoolConfigurationImpl("MY CONNECTION POOL")
                    .setSeeds(Joiner.on(",").join(properties.getSeeds()))
                    .setPort(properties.getPort())
                    .setMaxConnsPerHost(3)
                    .setAuthenticationCredentials(new SimpleAuthenticationCredentials(properties.getUsername(), properties.getPassword()));
    context = new AstyanaxContext.Builder()
                    .forCluster(properties.getClusterName())
                    .forKeyspace(properties.getKeyspace())
                    .withHostSupplier(hostSupplier)
                    .withAstyanaxConfiguration(astyanaxConfiguration)
                    .withConnectionPoolConfiguration(poolConfiguration)
                    .withConnectionPoolMonitor(new CountingConnectionPoolMonitor())
                    .buildKeyspace(CqlFamilyFactory.getInstance());
    

    更多示例代码可见https://github.com/Netflix/astyanax/blob/master/astyanax-test/src/main/java/com/netflix/astyanax/cql/test/utils/AstyanaxContextFactory.java

    【讨论】:

      猜你喜欢
      • 2014-12-19
      • 2013-08-13
      • 2013-10-14
      • 1970-01-01
      • 2019-07-16
      • 2013-01-11
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      相关资源
      最近更新 更多