Elffuss BraidHub

kikocis/java-driver public

9e8f5a07be4b · 2.6 KiBRenderedRawPermalink
1## Query timestamps
2
3In Cassandra, each mutation has a microsecond-precision timestamp, which
4is used to order operations relative to each other.
5
6There are various ways to assign it:
7
8### CQL `USING TIMESTAMP`
9
10You can explicitly provide the timestamp in your CQL query:
11
12```java
13session.execute("INSERT INTO my_table(c1, c2) values (1, 1) " +
14 "USING TIMESTAMP 1432815430948040");
15```
16
17### Client-side generation
18
19This is enabled by default if you're using the driver 3.0 and a version
20of Cassandra that supports [native protocol](../native_protocol) v3 or
21above.
22
23A client timestamp will be sent by each query. It is generated by a
24[TimestampGenerator][tsg]. The default implementation is
25[AtomicMonotonicTimestampGenerator][amtsg].
26
27You can specify another generator at initialization:
28
29```java
30Cluster.builder().addContactPoint("127.0.0.1")
31 .withTimestampGenerator(new MyCustomTimestampGenerator())
32 .build();
33```
34
35In addition, you can also override the default timestamp on a
36per-statement basis:
37
38```java
39Statement statement = session.newSimpleStatement(
40 "UPDATE users SET email = 'x@y.com' where id = 1");
41statement.setDefaultTimestamp(1234567890);
42session.execute(statement);
43```
44
45[tsg]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/TimestampGenerator.html
46[amtsg]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/AtomicMonotonicTimestampGenerator.html
47
48
49### Server-side generation
50
51This is the "legacy" behavior if you're connected to a Cassandra version
52that only supports protocol v2 or below. The server will assign a
53timestamp based on the time it receives the query.
54
55This can be a problem when the order of the writes matter: with unlucky
56timing (different coordinators, network latency, etc.), two successive
57requests from the same client might be processed in a different order
58server-side, and end up with out-of-order timestamps. If protocol v3 is
59not an option, the only workaround is to add `USING TIMESTAMP` in your
60queries.
61
62
63### Summary
64
65As shown in the previous sections, there are multiple ways to provide a
66timestamp, some of which overlap. The order of precedence is the
67following:
68
691. if there is a `USING TIMESTAMP` clause in the CQL string, use that
70 over anything else;
712. otherwise, if a default timestamp was set on the statement and is
72 different from `Long.MIN_VALUE`, use it;
733. otherwise, if a generator is specified, invoke it and use its result
74 if it is different from `Long.MIN_VALUE`;
754. otherwise, let the server assign the timestamp.
76
77Steps 2 and 3 only apply if native protocol v3 or above is in use.