Last active
August 30, 2020 14:58
-
-
Save alecco/ba55b605cf91d23a1828591388e7200e to your computer and use it in GitHub Desktop.
Cassandra and ScyllaDB LWT error handling dtest example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dtest import Tester, debug | |
from cassandra import ConsistencyLevel | |
class LWTExample(Tester): | |
def test_lwt(self): | |
cluster = self.cluster | |
cluster.populate(4) | |
cluster.start(wait_other_notice=True, wait_for_binary_proto=True) | |
node = cluster.nodelist()[0] | |
session = self.patient_cql_connection(node) | |
session.execute("CREATE KEYSPACE ks WITH replication={'class':'SimpleStrategy', 'replication_factor': 3}") | |
session.execute("CREATE TABLE ks.users (id INT PRIMARY KEY, name TEXT)") | |
# The IF NOT EXIST part makes the query LWT | |
# query runs in 2 rounds | |
# 1. to check the condition | |
# 2. tries to apply the change with Paxos | |
insert_stmt = session.prepare("INSERT INTO ks.users (id, name) VALUES (1, 'foo') IF NOT EXISTS") | |
#https://docs.datastax.com/en/developer/python-driver/3.24/api/cassandra/query/#cassandra.query.Statement.consistency_level | |
insert_stmt.consistency_level = ConsistencyLevel.QUORUM # First read round CL | |
# https://docs.datastax.com/en/developer/python-driver/3.24/api/cassandra/query/#cassandra.query.Statement.serial_consistency_level | |
# https://docs.datastax.com/en/ddac/doc/datastax_enterprise/dbInternals/dbIntConfigSerialConsistency.html | |
insert_stmt.serial_consistency_level = ConsistencyLevel.SERIAL # Second Paxos round CL | |
try: | |
res = session.execute(insert_stmt) | |
except Exception as exc: | |
debug(f"LWT INSERT query error {exc}") | |
else: | |
if not res[0].applied: | |
debug(f"LWT INSERT query failed (user exists)") | |
else: | |
debug("Success") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment