我正在尝试编写一个Java片段,在单独的线程中启动Cassandra,然后使用驱动程序连接并创建一些用户。我们有一个脚本使用基于Python的CQLSH客户端来实现此目的,但是当我们将其中一个服务器升级到Ubuntu 16.04时遇到了问题。显然在Ubuntu上存在Python版本不兼容(适用于RHEL 7,但不适用于Ubuntu 16.04)。我甚至尝试在Ubuntu上降级Python版本,但仍然无法使用CQLSH客户端,所以我在Java中使用了这个。这些是Maven依赖项。请注意,我已尝试使用默认的Guava版本(16.0),但遇到了其他一些问题,所以我用17.0覆盖了它。
[INFO] +- commons-io:commons-io:jar:1.3.2:compile
[INFO] +- org.apache.cassandra:cassandra-all:jar:2.2.4:compile
[INFO] | +- org.xerial.snappy:snappy-java:jar:1.1.1.7:compile
[INFO] | +- net.jpountz.lz4:lz4:jar:1.3.0:compile
[INFO] | +- com.ning:compress-lzf:jar:0.8.4:compile
[INFO] | +- commons-cli:commons-cli:jar:1.1:compile
[INFO] | +- commons-codec:commons-codec:jar:1.9:compile
[INFO] | +- org.apache.commons:commons-lang3:jar:3.3.2:compile
[INFO] | +- org.apache.commons:commons-math3:jar:3.2:compile
[INFO] | +- com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:jar:1.4:compile
[INFO] | +- org.antlr:antlr:jar:3.5.2:compile
[INFO] | | \- org.antlr:ST4:jar:4.0.8:compile
[INFO] | +- org.antlr:antlr-runtime:jar:3.5.2:compile
[INFO] | +- org.slf4j:slf4j-api:jar:1.7.12:compile
[INFO] | +- org.slf4j:log4j-over-slf4j:jar:1.7.7:compile
[INFO] | +- org.slf4j:jcl-over-slf4j:jar:1.7.5:compile
[INFO] | +- org.codehaus.jackson:jackson-core-asl:jar:1.9.2:compile
[INFO] | +- org.codehaus.jackson:jackson-mapper-asl:jar:1.9.2:compile
[INFO] | +- com.googlecode.json-simple:json-simple:jar:1.1:compile
[INFO] | +- com.boundary:high-scale-lib:jar:1.0.6:compile
[INFO] | +- org.yaml:snakeyaml:jar:1.11:compile
[INFO] | +- org.mindrot:jbcrypt:jar:0.3m:compile
[INFO] | +- io.dropwizard.metrics:metrics-core:jar:3.1.0:compile
[INFO] | +- com.addthis.metrics:reporter-config3:jar:3.0.0:compile
[INFO] | | +- com.addthis.metrics:reporter-config-base:jar:3.0.0:compile
[INFO] | | \- org.hibernate:hibernate-validator:jar:4.3.0.Final:compile
[INFO] | | +- javax.validation:validation-api:jar:1.0.0.GA:compile
[INFO] | | \- org.jboss.logging:jboss-logging:jar:3.1.0.CR2:compile
[INFO] | +- com.thinkaurelius.thrift:thrift-server:jar:0.3.7:compile
[INFO] | | \- com.lmax:disruptor:jar:3.0.1:compile
[INFO] | +- com.clearspring.analytics:stream:jar:2.5.2:compile
[INFO] | | \- it.unimi.dsi:fastutil:jar:6.5.7:compile
[INFO] | +- net.sf.supercsv:super-csv:jar:2.1.0:compile
[INFO] | +- ch.qos.logback:logback-core:jar:1.0.6:compile
[INFO] | +- ch.qos.logback:logback-classic:jar:1.0.6:compile
[INFO] | +- org.apache.thrift:libthrift:jar:0.9.2:compile
[INFO] | | +- org.apache.httpcomponents:httpclient:jar:4.2.5:compile
[INFO] | | \- org.apache.httpcomponents:httpcore:jar:4.2.4:compile
[INFO] | +- org.apache.cassandra:cassandra-thrift:jar:2.2.4:compile
[INFO] | +- net.java.dev.jna:jna:jar:4.0.0:compile
[INFO] | +- com.github.jbellis:jamm:jar:0.3.0:compile
[INFO] | +- com.github.tjake:crc32ex:jar:0.1.1:compile
[INFO] | +- io.netty:netty-all:jar:4.0.23.Final:compile
[INFO] | +- joda-time:joda-time:jar:2.4:compile
[INFO] | \- org.fusesource:sigar:jar:1.6.4:compile
[INFO] +- com.google.guava:guava:jar:17.0:test
[INFO] +- com.datastax.cassandra:cassandra-driver-core:jar:3.1.2:compile
[INFO] | +- io.netty:netty-handler:jar:4.0.37.Final:compile
[INFO] | | +- io.netty:netty-buffer:jar:4.0.37.Final:compile
[INFO] | | | \- io.netty:netty-common:jar:4.0.37.Final:compile
[INFO] | | +- io.netty:netty-transport:jar:4.0.37.Final:compile
[INFO] | | \- io.netty:netty-codec:jar:4.0.37.Final:compile
[INFO] | +- com.github.jnr:jnr-ffi:jar:2.0.7:compile
[INFO] | | +- com.github.jnr:jffi:jar:1.2.10:compile
[INFO] | | +- com.github.jnr:jffi:jar:native:1.2.10:runtime
[INFO] | | +- org.ow2.asm:asm:jar:5.0.3:compile
[INFO] | | +- org.ow2.asm:asm-commons:jar:5.0.3:compile
[INFO] | | +- org.ow2.asm:asm-util:jar:5.0.3:compile
[INFO] | | \- com.github.jnr:jnr-x86asm:jar:1.0.2:compile
[INFO] | \- com.github.jnr:jnr-posix:jar:3.0.27:compile
[INFO] | \- com.github.jnr:jnr-constants:jar:0.9.0:compile
[INFO] +- com.cisco.dascode:dascode-cassandra:zip:2.2.4:compile
[INFO] \- junit:junit:jar:4.11:test
[INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test
起初,我正在使用cassandra-unit来启动服务器,但遇到了一大堆其他问题,所以我最终只是从我的代码中启动服务器,就像这样:
System.setProperty("cassandra.config", "file:" + file.getAbsolutePath()); // Path to cassandra.yaml
System.setProperty("cassandra-foreground", "true");
System.setProperty("cassandra.native.epoll.enabled", "false");
...
cassandraDaemon = new CassandraDaemon();
cassandraDaemon.activate();
客户端代码如下所示:
Cluster cluster = new Cluster.Builder().addContactPoints("localhost").withPort(9042).withCredentials("cassandra", "cassandra").build();
Session session = cluster.connect("system_auth");
我已尝试使用和不使用键空间进行connect()调用,并且行为没有区别。当客户端尝试连接时,我得到以下异常:
com.datastax.driver.core.exceptions.AuthenticationException: Authentication error on host localhost/127.0.0.1:9042: Username and/or password are incorrect
at com.datastax.driver.core.Connection$8.apply(Connection.java:393)
at com.datastax.driver.core.Connection$8.apply(Connection.java:362)
有趣的是,如果我只在Mac上本地启动服务器部分,我可以使用CQLSH客户端,用户名/密码设置为“cassandra”/“cassandra”,我没有任何问题认证
我的cassandra.yaml文件如下所示:
cluster_name: 'Test Cluster'
num_tokens: 256
hinted_handoff_enabled: true
hinted_handoff_throttle_in_kb: 1024
max_hints_delivery_threads: 2
batchlog_replay_throttle_in_kb: 1024
authenticator: PasswordAuthenticator
authorizer: CassandraAuthorizer
role_manager: CassandraRoleManager
roles_validity_in_ms: 2000
permissions_validity_in_ms: 2000
partitioner: org.apache.cassandra.dht.Murmur3Partitioner
disk_failure_policy: stop
commit_failure_policy: stop
key_cache_size_in_mb:
key_cache_save_period: 14400
row_cache_size_in_mb: 0
row_cache_save_period: 0
counter_cache_size_in_mb:
counter_cache_save_period: 7200
commitlog_sync: periodic
commitlog_sync_period_in_ms: 10000
commitlog_segment_size_in_mb: 32
seed_provider:
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
- seeds: "127.0.0.1"
concurrent_reads: 32
concurrent_writes: 32
concurrent_counter_writes: 32
memtable_allocation_type: heap_buffers
index_summary_capacity_in_mb:
index_summary_resize_interval_in_minutes: 60
trickle_fsync: false
trickle_fsync_interval_in_kb: 10240
storage_port: 7000
ssl_storage_port: 7001
listen_address: localhost
start_native_transport: true
native_transport_port: 9042
start_rpc: false
rpc_address: localhost
rpc_port: 9160
rpc_keepalive: true
rpc_server_type: sync
thrift_framed_transport_size_in_mb: 15
incremental_backups: false
snapshot_before_compaction: false
auto_snapshot: true
tombstone_warn_threshold: 1000
tombstone_failure_threshold: 100000
column_index_size_in_kb: 64
batch_size_warn_threshold_in_kb: 5
batch_size_fail_threshold_in_kb: 50
compaction_throughput_mb_per_sec: 16
compaction_large_partition_warning_threshold_mb: 100
sstable_preemptive_open_interval_in_mb: 50
对我可能做错的任何提示都表示赞赏。
答案 0 :(得分:2)
我猜这是竞争条件。到客户端尝试连接时,服务器尚未完全初始化。我放了一个Thread.sleep(20000);在尝试连接之前的代码中,这是有效的。有没有更好的方法来验证服务器是否已完全初始化?
答案 1 :(得分:2)
我不确定这是否会有所帮助,但如果您在创建CassandraDaemon
之前添加以下内容,那么cassandra用户可能无法立即显示:<{1}} <\ n / p>
System.setProperty("cassandra.superuser_setup_delay_ms", "0");
这可能会解决您的问题。该值默认为10000(10秒)(source),这解释了为什么睡眠20秒可能解决了您的问题。