Elffuss BraidHub

kikocis/java-driver public

3.1.x · 5.3 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 = new SimpleStatement(
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
63The driver ships with two implementations of `TimestampGenerator`:
64
651. [AtomicMonotonicTimestampGenerator][amtsg], which guarantess monotonicity of timestamps for all threads;
662. [ThreadLocalMonotonicTimestampGenerator][tlmtsg], which guarantees per-thread monotonicity of timestamps.
67
68There is less contention using `ThreadLocalMonotonicTimestampGenerator`, but beware
69that there is a risk of timestamp collision with this generator when accessed by more than one
70thread; only use it when threads are not in direct competition for timestamp ties (i.e., they are executing
71independent statements).
72
73[amtsg]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/AtomicMonotonicTimestampGenerator.html
74[tlmtsg]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/ThreadLocalMonotonicTimestampGenerator.html
75
76#### Accuracy
77
78Both implementations strive to achieve microsecond resolution on a best-effort basis.
79But in practice, the real accuracy of generated timestamps is largely dependent on the
80granularity of the underlying operating system's clock.
81
82For most systems, this minimum granularity is millisecond, and
83the sub-millisecond part of generated timestamps is simply a counter that gets incremented
84until the next clock tick, as provided by `System.currentTimeMillis()`.
85
86On some systems, however, it is possible to have a better granularity by using a [JNR]
87call to [gettimeofday]. This native call will be used when available, unless the system
88property `com.datastax.driver.USE_NATIVE_CLOCK` is explicitly set to `false`.
89
90To check what's available on your system:
91
92* make sure your `Cluster` uses a `TimestampGenerator`;
93* [configure your logging framework](../logging/) to use level `INFO` for the category
94 `com.datastax.driver.core.ClockFactory`;
95* look for one of the following messages at startup:
96
97 ```
98 INFO com.datastax.driver.core.ClockFactory - Using java.lang.System clock to generate timestamps
99 INFO com.datastax.driver.core.ClockFactory - Using native clock to generate timestamps
100 ```
101
102[gettimeofday]: http://man7.org/linux/man-pages/man2/settimeofday.2.html
103[JNR]: https://github.com/jnr/jnr-ffi
104
105#### Monotonicity
106
107The aforementioned implementations also guarantee
108that returned timestamps will always be monotonically increasing, even if multiple updates
109happen under the same millisecond.
110
111Note that to guarantee such monotonicity, if more than one timestamp is generated
112within the same microsecond, or in the event of a system clock skew, _both implementations might
113return timestamps that drift out in the future_.
114
115When this happens, the built-in generators log a periodic warning message in the category
116`com.datastax.driver.core.TimestampGenerator`. See their non-default constructors for ways to control the warning
117interval.
118
119### Summary
120
121As shown in the previous sections, there are multiple ways to provide a
122timestamp, some of which overlap. The order of precedence is the
123following:
124
1251. if there is a `USING TIMESTAMP` clause in the CQL string, use that
126 over anything else;
1272. otherwise, if a default timestamp was set on the statement and is
128 different from `Long.MIN_VALUE`, use it;
1293. otherwise, if a generator is specified, invoke it and use its result
130 if it is different from `Long.MIN_VALUE`;
1314. otherwise, let the server assign the timestamp.
132
133Steps 2 and 3 only apply if native protocol v3 or above is in use.