Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat]: Support using server time for CQLSH connections #718

Open
millerjp opened this issue Feb 4, 2025 · 0 comments
Open

[feat]: Support using server time for CQLSH connections #718

millerjp opened this issue Feb 4, 2025 · 0 comments
Assignees
Labels
Milestone

Comments

@millerjp
Copy link
Contributor

millerjp commented Feb 4, 2025

When you do an insert into cassandra using the Python driver it by default uses the driver time as the time.

You can also tell it use the Cassandra server time as the default time. This is managed via how the CQLSH cluster connection is instantiated:
https://github.com/axonops/axonops-workbench-cqlsh/blob/ae3fa132b55187419c4f6f2f763d443d363b8e95/cqlsh/cqlshlib/cqlshmain.py#L415

We should add a configuration option on the connections when they are being added/edited to allow users to decide which time to use.

Add an option to choose desktop time or server time as the time option.

We will need to update the CQLSH code to add this extra option. Expose it as a connection option and add it to our cqlshrc file

from cassandra.cluster import Cluster

# Create a Cluster instance with server-side timestamps
cluster = Cluster(['127.0.0.1'], timestamp_generator=None)

# Create a session
session = cluster.connect()

# Execute queries without client-side timestamps
session.execute("INSERT INTO my_table (id, value) VALUES (1, 'example')")

By default, the Python Cassandra driver uses the MonotonicTimestampGenerator to generate client-side timestamps for write operations. This generator ensures that timestamps are monotonically increasing, even if the system clock drifts backward or if multiple writes occur in rapid succession.

Details of the Default Behavior

  1. MonotonicTimestampGenerator:

    • This is the default timestamp generator for the Python Cassandra driver.
    • It generates timestamps in microseconds based on the system clock (time.time()), ensuring that they are always increasing.
    • If the system clock moves backward or multiple requests are made within a very short time, it artificially increments timestamps to maintain monotonicity.
    • It also logs warnings if significant clock drift is detected.
  2. Why Client-Side Timestamps Are Default:

    • Client-side timestamps ensure predictable ordering of operations from the perspective of the client application.
    • This avoids issues caused by potential clock skew between Cassandra nodes in a distributed system, where server-assigned timestamps may not always reflect the actual order of operations.
  3. Configuration:

    • You can explicitly configure or replace the default timestamp generator when creating a Cluster instance. For example:
      from cassandra.cluster import Cluster
      from cassandra.timestamps import MonotonicTimestampGenerator
      
      cluster = Cluster(
          ['127.0.0.1'],
          timestamp_generator=MonotonicTimestampGenerator()
      )
      session = cluster.connect()

If you want to disable this behavior and use server-side timestamps, set timestamp_generator=None when initializing the Cluster instance.

Citations:
[1] https://datastax.github.io/cpp-driver/api/struct.CassTimestampGen/
[2] https://docs.datastax.com/en/developer/python-driver/3.8/api/cassandra/timestamps/index.html
[3] https://stackoverflow.com/questions/22759843/python-cassandra-cql-insert-timestamp-and-blob
[4] https://groups.google.com/a/lists.datastax.com/g/python-driver-user/c/6MgM8mCzeZQ
[5] https://docs.datastax.com/en/datastax-drivers/developing/query-timestamps.html
[6] https://datastax-oss.atlassian.net/browse/NODEJS-322
[7] https://datastax-oss.atlassian.net/browse/JAVA-467
[8] https://python-driver.docs.scylladb.com/stable/api/cassandra/cqlengine/query.html
[9] https://packagehub.suse.com/packages/python-cassandra-driver/3_20_2-bp152_1_8/
[10] https://datastax.github.io/python-driver/_modules/cassandra/cluster.html

@millerjp millerjp added cql console enhancement New feature or request labels Feb 4, 2025
@millerjp millerjp added this to the RightClick milestone Feb 4, 2025
@millerjp millerjp assigned millerjp and mhmdkrmabd and unassigned millerjp Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants