Java 当我现在只使用一个节点时,这些东西肯定不会有帮助

Java 当我现在只使用一个节点时,这些东西肯定不会有帮助,java,cassandra,datastax-java-driver,astyanax,Java,Cassandra,Datastax Java Driver,Astyanax,我的本地Cassandra版本是2.1.15(支持本机协议V3),但我们生产环境中的机器运行的是Cassandra 2.0.12.156(只支持V2) 请记住,这是针对一个具有多个节点和多个数据中心的环境的,这就是为什么我以我这样的方式进行设置(实际值是从配置文件中设置的),即使我知道在这个测试中,我可以跳过使用dcawarerroundrobinpolicy之类的东西 任何帮助都将不胜感激!我也可以发布使用Astyanax的代码,我只是想首先应该确保我的新代码没有明显的错误。 谢谢 使用Dat

我的本地Cassandra版本是2.1.15(支持本机协议V3),但我们生产环境中的机器运行的是Cassandra 2.0.12.156(只支持V2)

请记住,这是针对一个具有多个节点和多个数据中心的环境的,这就是为什么我以我这样的方式进行设置(实际值是从配置文件中设置的),即使我知道在这个测试中,我可以跳过使用dcawarerroundrobinpolicy之类的东西

任何帮助都将不胜感激!我也可以发布使用Astyanax的代码,我只是想首先应该确保我的新代码没有明显的错误。 谢谢

使用DataStax驱动程序进行10000次写入+读取的测试大约需要30秒,而使用Astyanax,则需要15-20秒的时间

我将测试计数增加到100000,以查看DataStax驱动程序是否存在一些开销,这些开销在启动时仅消耗约10秒,之后它们的性能可能会更相似。但即使有100000次读/写:

AstyanaxCassandra 100000次插入+读取耗时156593毫秒


DataStaxCassandra 100000次插入+读取耗时294340毫秒

虽然您使用的是executeAsync,但您正在调用。请在之后立即获取。因此,您的程序执行时没有并发性(一次一个查询)。这是你的意图吗?是的,我这样做是因为我在这里读到的--(“限制总体查询时间”部分)此外,我还尝试用对session.execute(语句)的调用来完全替换execute()方法。此外,由于我在“异步分页”部分--。您能分享一些数字来帮助理解您看到的性能差异吗?同样,共享相应的astyanax实现也会有所帮助。astyanax使用datastax驱动程序,因此它可能只是来自您非异步地使用异步api。在2.0和2.1中,在同时使用的情况下,CQL比thrift快,因此即使您使用thrift api astyanax也不应该更快。我的猜测是,如果使用thrift,来自astyanax的池允许并发请求,而上面的代码不会并发发出请求。虽然您使用的是executeAsync,但您正在调用。请立即获取。因此,您的程序执行时没有并发性(一次一个查询)。这是你的意图吗?是的,我这样做是因为我在这里读到的--(“限制总体查询时间”部分)此外,我还尝试用对session.execute(语句)的调用来完全替换execute()方法。此外,由于我在“异步分页”部分--。您能分享一些数字来帮助理解您看到的性能差异吗?同样,共享相应的astyanax实现也会有所帮助。astyanax使用datastax驱动程序,因此它可能只是来自您非异步地使用异步api。在2.0和2.1中,在同时使用的情况下,CQL比thrift快,因此即使您使用thrift api astyanax也不应该更快。我的猜测是,如果使用thrift,来自astyanax的池允许并发请求,而上面的代码不会并发请求。
class DataStaxCassandra
{
    final Session session;
    final PreparedStatement preparedIDWriteCmd;
    final PreparedStatement preparedIDReadCmd;

    void DataStaxCassandra()
    {
        final PoolingOptions poolingOptions = new PoolingOptions()
            .setConnectionsPerHost(HostDistance.LOCAL, 1, 2)
            .setConnectionsPerHost(HostDistance.REMOTE, 1, 1)
            .setMaxRequestsPerConnection(HostDistance.LOCAL, 128)
            .setMaxRequestsPerConnection(HostDistance.REMOTE, 128)
            .setPoolTimeoutMillis(0); // Don't ever wait for a connection to one host.

        final QueryOptions queryOptions = new QueryOptions()
            .setConsistencyLevel(ConsistencyLevel.LOCAL_ONE)
            .setPrepareOnAllHosts(true)
            .setReprepareOnUp(true);

        final LoadBalancingPolicy dcAwareRRPolicy = DCAwareRoundRobinPolicy.builder()
            .withLocalDc("my_laptop")
            .withUsedHostsPerRemoteDc(0)
            .build();

        final LoadBalancingPolicy loadBalancingPolicy = new TokenAwarePolicy(dcAwareRRPolicy);

        final SocketOptions socketOptions = new SocketOptions()
        .setConnectTimeoutMillis(1000)
        .setReadTimeoutMillis(1000);

        final RetryPolicy retryPolicy = new LoggingRetryPolicy(DefaultRetryPolicy.INSTANCE);

        Cluster.Builder clusterBuilder = Cluster.builder()
            .withClusterName("test cluster")
            .withPort(9042)
            .addContactPoints("127.0.0.1")
            .withPoolingOptions(poolingOptions)
            .withQueryOptions(queryOptions)
            .withLoadBalancingPolicy(loadBalancingPolicy)
            .withSocketOptions(socketOptions)
            .withRetryPolicy(retryPolicy);

        // I've tried both V3 and V2, with lower connections/host and higher reqs/connection settings
        // with V3, and it doesn't noticably affect the test performance. Leaving it at V2 because the
        // Astyanax version is using V2.
        clusterBuilder.withProtocolVersion(ProtocolVersion.V2);

        final Cluster cluster = clusterBuilder.build();
        session = cluster.connect();

        preparedIDWriteCmd = session.prepare(
            "INSERT INTO \"mykeyspace\".\"mytable\" (\"uuid\", \"date\") VALUES (?, ?) USING TTL 38880000");

        preparedIDReadCmd = session.prepare(
            "SELECT \"date\" from \"mykeyspace\".\"mytable\" WHERE \"uuid\"=?");
    }

    public List<Row> execute(final Statement statement, final int timeout)
    throws InterruptedException, ExecutionException, TimeoutException
    {
        final ResultSetFuture future = session.executeAsync(statement);

        try
        {
            final ResultSet readRows = future.get(timeout, TimeUnit.MILLISECONDS);
            final List<Row> resultRows = new ArrayList<>();

            // How far we can go without triggering the blocking fetch:
            int remainingInPage = readRows.getAvailableWithoutFetching();
            for (final Row row : readRows)
            {
                resultRows.add(row);
                if (--remainingInPage == 0) break;
            }
            return resultRows;
        }
        catch (final TimeoutException e)
        {
            future.cancel(true);
            throw e;
        }
    }

    private void insertRow(final byte[] id, final int date)
    throws InterruptedException, ExecutionException, TimeoutException
    {
        final ByteBuffer idKey = ByteBuffer.wrap(id);
        final BoundStatement writeCmd = preparedIDWriteCmd.bind(idKey, date);
        writeCmd.setRoutingKey(idKey);
        execute(writeCmd, 1000);
    }

    public int readRow(final byte[] id)
    throws InterruptedException, ExecutionException, TimeoutException
    {
        final ByteBuffer idKey = ByteBuffer.wrap(id);
        final BoundStatement readCmd = preparedIDReadCmd.bind(idKey);
        readCmd.setRoutingKey(idKey);
        final List<Row> idRows = execute(readCmd, 1000);

        if (idRows.isEmpty()) return 0;

        final Row idRow = idRows.get(0);
        return idRow.getInt("date");
    }
}

void perfTest()
{
    final DataStaxCassandra ds = new DataStaxCassandra();
    final int perfTestCount = 10000;

    final long startTime = System.nanoTime();
    for (int i = 0; i < perfTestCount; ++i)
    {
        final String id = UUIDUtils.generateRandomUUIDString();
        final byte[] idBytes = Utils.hexStringToByteArray(id);
        final int date = (int)(System.currentTimeMillis() / 1000);

        try
        {
            ds.insertRow(idBytes, date);
            final int dateRead = ds.readRow(idBytes);
            assert(dateRead == date) : "Inserted ID with date " +date +" but date read is " +dateRead;
        }
        catch (final InterruptedException | ExecutionException | TimeoutException e)
        {
            System.err.println("ERROR reading ID (test " +(i+1) +") - " +e.toString());
        }
    }
    System.out.println(
        perfTestCount +" insert+reads took " +
        TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) +" ms");
}