| 1 | ## Query timestamps |
| 2 | |
| 3 | In Cassandra, each mutation has a microsecond-precision timestamp, which |
| 4 | is used to order operations relative to each other. |
| 5 | |
| 6 | There are various ways to assign it: |
| 7 | |
| 8 | ### CQL `USING TIMESTAMP` |
| 9 | |
| 10 | You can explicitly provide the timestamp in your CQL query: |
| 11 | |
| 12 | ```java |
| 13 | session.execute("INSERT INTO my_table(c1, c2) values (1, 1) " + |
| 14 | "USING TIMESTAMP 1432815430948040"); |
| 15 | ``` |
| 16 | |
| 17 | ### Client-side generation |
| 18 | |
| 19 | This is enabled by default if you're using the driver 3.0+ and a version |
| 20 | of Cassandra that supports [native protocol](../native_protocol) v3 or |
| 21 | above. |
| 22 | |
| 23 | A client timestamp will be sent by each query. It is generated by a |
| 24 | [TimestampGenerator][tsg]. The default implementation is |
| 25 | [AtomicMonotonicTimestampGenerator][amtsg]. |
| 26 | |
| 27 | You can specify another generator at initialization: |
| 28 | |
| 29 | ```java |
| 30 | Cluster.builder().addContactPoint("127.0.0.1") |
| 31 | .withTimestampGenerator(new MyCustomTimestampGenerator()) |
| 32 | .build(); |
| 33 | ``` |
| 34 | |
| 35 | In addition, you can also override the default timestamp on a |
| 36 | per-statement basis: |
| 37 | |
| 38 | ```java |
| 39 | Statement statement = new SimpleStatement( |
| 40 | "UPDATE users SET email = 'x@y.com' where id = 1"); |
| 41 | statement.setDefaultTimestamp(1234567890); |
| 42 | session.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 | |
| 51 | This is the "legacy" behavior if you're connected to a Cassandra version |
| 52 | that only supports protocol v2 or below. The server will assign a |
| 53 | timestamp based on the time it receives the query. |
| 54 | |
| 55 | This can be a problem when the order of the writes matter: with unlucky |
| 56 | timing (different coordinators, network latency, etc.), two successive |
| 57 | requests from the same client might be processed in a different order |
| 58 | server-side, and end up with out-of-order timestamps. If protocol v3 is |
| 59 | not an option, the only workaround is to add `USING TIMESTAMP` in your |
| 60 | queries. |
| 61 | |
| 62 | |
| 63 | The driver ships with two implementations of `TimestampGenerator`: |
| 64 | |
| 65 | 1. [AtomicMonotonicTimestampGenerator][amtsg], which guarantess monotonicity of timestamps for all threads; |
| 66 | 2. [ThreadLocalMonotonicTimestampGenerator][tlmtsg], which guarantees per-thread monotonicity of timestamps. |
| 67 | |
| 68 | There is less contention using `ThreadLocalMonotonicTimestampGenerator`, but beware |
| 69 | that there is a risk of timestamp collision with this generator when accessed by more than one |
| 70 | thread; only use it when threads are not in direct competition for timestamp ties (i.e., they are executing |
| 71 | independent 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 | |
| 78 | Both implementations strive to achieve microsecond resolution on a best-effort basis. |
| 79 | But in practice, the real accuracy of generated timestamps is largely dependent on the |
| 80 | granularity of the underlying operating system's clock. |
| 81 | |
| 82 | For most systems, this minimum granularity is millisecond, and |
| 83 | the sub-millisecond part of generated timestamps is simply a counter that gets incremented |
| 84 | until the next clock tick, as provided by `System.currentTimeMillis()`. |
| 85 | |
| 86 | On some systems, however, it is possible to have a better granularity by using a [JNR] |
| 87 | call to [gettimeofday]. This native call will be used when available, unless the system |
| 88 | property `com.datastax.driver.USE_NATIVE_CLOCK` is explicitly set to `false`. |
| 89 | |
| 90 | To 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 | |
| 107 | The aforementioned implementations also guarantee |
| 108 | that returned timestamps will always be monotonically increasing, even if multiple updates |
| 109 | happen under the same millisecond. |
| 110 | |
| 111 | Note that to guarantee such monotonicity, if more than one timestamp is generated |
| 112 | within the same microsecond, or in the event of a system clock skew, _both implementations might |
| 113 | return timestamps that drift out in the future_. |
| 114 | |
| 115 | When 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 |
| 117 | interval. |
| 118 | |
| 119 | ### Summary |
| 120 | |
| 121 | As shown in the previous sections, there are multiple ways to provide a |
| 122 | timestamp, some of which overlap. The order of precedence is the |
| 123 | following: |
| 124 | |
| 125 | 1. if there is a `USING TIMESTAMP` clause in the CQL string, use that |
| 126 | over anything else; |
| 127 | 2. otherwise, if a default timestamp was set on the statement and is |
| 128 | different from `Long.MIN_VALUE`, use it; |
| 129 | 3. otherwise, if a generator is specified, invoke it and use its result |
| 130 | if it is different from `Long.MIN_VALUE`; |
| 131 | 4. otherwise, let the server assign the timestamp. |
| 132 | |
| 133 | Steps 2 and 3 only apply if native protocol v3 or above is in use. |