简体   繁体   中英

Cassandra consistency level and IF NOT EXISTS clause issue

Cassandra 3.1 NodeJS driver: cassandra-driver 3.0.0

Keyspace is created using the following statement:

CREATE KEYSPACE IF NOT EXISTS xxxxxx WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 2 };

Even though the replication factor is 2 I am running only one instance for development purpose with consistency level set to ONE. Here is the issue when I try to insert any record with IF NOT EXISTS clause I am getting consistency level error. But statements without IF NOT EXISTS work just fine. Here is the log from cqlsh:

cqlsh:xxxxxx> CONSISTENCY;
Current consistency level is ONE.
cqlsh:xxxxxx> insert into accounts(account_id) values('test');
cqlsh:xxxxxx> insert into accounts(account_id) values('test1') if not exists;
Traceback (most recent call last): File "/usr/bin/cqlsh.py", line 1258, in perform_simple_statement result = future.result() File "/usr/share/cassandra/lib/cassandra-driver-internal-only-3.0.0-6af642d.zip/cassandra-driver-3.0.0-6af642d/cassandra/cluster.py", line 3122, in result raise self._final_exception Unavailable: code=1000 [Unavailable exception] message="Cannot achieve consistency level QUORUM" info={'required_replicas': 2, 'alive_replicas': 1, 'consistency': 'QUORUM'}

The "if not exists" clause for paxos uses a different consistency setting of "SERIAL CONSISTENCY", which can have the values SERIAL or LOCAL_SERIAL.

The SERIAL setting means it will need a quorum across all replicas, while the LOCAL_SERIAL means it will need a quorum in the local data center. With a replication factor of 2, a quorum is 2 replicas.

Unfortunately there is no LOCAL_ONE or ONE setting for "SERIAL CONSISTENCY", so in your case you would need two replicas to be alive for your insert if not exists to succeed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM