kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 25b5ffc807a51b10e3380f076d6513ae50a10933c8cd5017da2aa6cc66f195b1 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-03-22 18:25 +01:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2016-03-22 18:25 +01:00 |
| Parents | 83bf2897a22e, 34bc3c2fc8c8 |
9 files changed
modified README.md +0 −12
| @@ -28,18 +28,6 @@ | ||
| 28 | 28 | * [Query builder](manual/statements/built/) |
| 29 | 29 | * [Object mapper](manual/object_mapper/) |
| 30 | 30 | |
| 31 | - | |
| 32 | -[sync]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Session.html#execute-com.datastax.driver.core.Statement- | |
| 33 | -[async]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Session.html#executeAsync-com.datastax.driver.core.Statement- | |
| 34 | -[simple_st]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/SimpleStatement.html | |
| 35 | -[prepared_st]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Session.html#prepare-com.datastax.driver.core.RegularStatement- | |
| 36 | -[batch_st]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/BatchStatement.html | |
| 37 | -[pool]: manual/pooling/ | |
| 38 | -[lbp]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/LoadBalancingPolicy.html | |
| 39 | -[retry_policy]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/RetryPolicy.html | |
| 40 | -[query_builder]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/querybuilder/QueryBuilder.html | |
| 41 | -[mapper]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/mapping/MappingManager.html | |
| 42 | - | |
| 43 | 31 | The driver architecture is based on layers. At the bottom lies the driver core. |
| 44 | 32 | This core handles everything related to the connections to a Cassandra |
| 45 | 33 | cluster (for example, connection pool, discovering new nodes, etc.) and exposes a simple, |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractMonotonicTimestampGenerator.java +5 −1
| @@ -35,7 +35,11 @@ | ||
| 35 | 35 | * Beware that to guarantee monotonicity, if more than one call to {@link #next()} |
| 36 | 36 | * is made within the same microsecond, or in the event of a system clock skew, this generator might |
| 37 | 37 | * return timestamps that drift out in the future. |
| 38 | - * Whe this happens, {@link #onDrift(long, long)} is invoked. | |
| 38 | + * When this happens, {@link #onDrift(long, long)} is invoked. | |
| 39 | + * <p/> | |
| 40 | + * Note: currently, native calls won't work from an OSGI container. This is because JNR artifacts don't include proper | |
| 41 | + * OSGI descriptors. This will be fixed in an upcoming version (see | |
| 42 | + * <a href="https://datastax-oss.atlassian.net/browse/JAVA-1127">JAVA-1127</a>). | |
| 39 | 43 | */ |
| 40 | 44 | public abstract class AbstractMonotonicTimestampGenerator implements TimestampGenerator { |
| 41 | 45 |
modified driver-core/src/main/java/com/datastax/driver/core/utils/UUIDs.java +3 −1
| @@ -52,8 +52,10 @@ | ||
| 52 | 52 | * </ol> |
| 53 | 53 | * </li> |
| 54 | 54 | * </ol> |
| 55 | + * Note: currently, native calls won't work from an OSGI container. This is because JNR artifacts don't include proper | |
| 56 | + * OSGI descriptors. This will be fixed in an upcoming version (see | |
| 57 | + * <a href="https://datastax-oss.atlassian.net/browse/JAVA-1127">JAVA-1127</a>). | |
| 55 | 58 | * |
| 56 | - * @jira_ticket JAVA-444 | |
| 57 | 59 | * @see <a href="http://www.ietf.org/rfc/rfc4122.txt">A Universally Unique IDentifier (UUID) URN Namespace (RFC 4122)</a> |
| 58 | 60 | */ |
| 59 | 61 | public final class UUIDs { |
added manual/idempotence/README.md +132 −0
| @@ -0,0 +1,132 @@ | ||
| 1 | +## Query idempotence | |
| 2 | + | |
| 3 | +A query is *idempotent* if it can be applied multiple times without changing the result of the initial application. For | |
| 4 | +example: | |
| 5 | + | |
| 6 | +* `update my_table set list_col = [1] where pk = 1` is idempotent: no matter how many times it gets executed, `list_col` | |
| 7 | + will always end up with the value `[1]`; | |
| 8 | +* `update my_table set list_col = [1] + list_col where pk = 1` is not idempotent: if `list_col` was initially empty, | |
| 9 | + it will contain `[1]` after the first execution, `[1, 1]` after the second, etc. | |
| 10 | + | |
| 11 | +Idempotence matters for [retries](../retries/) and [speculative query executions](../speculative_execution/). The | |
| 12 | +corresponding policies inspect the [Statement#isIdempotent()][isIdempotent] flag. | |
| 13 | + | |
| 14 | +In most cases, you must set that flag manually. The driver does not parse query strings, so it can't infer it | |
| 15 | +automatically (except for statements coming from the query builder, see below). | |
| 16 | + | |
| 17 | +Statements start out as non-idempotent by default. You can override the flag on each statement: | |
| 18 | + | |
| 19 | +```java | |
| 20 | +Statement s = new SimpleStatement("SELECT * FROM users WHERE id = 1"); | |
| 21 | +s.setIdempotent(true); | |
| 22 | +``` | |
| 23 | + | |
| 24 | +The default is also configurable: if you want all statements to start out as idempotent, do this: | |
| 25 | + | |
| 26 | +```java | |
| 27 | +// Make all statements idempotent by default: | |
| 28 | +cluster.getConfiguration().getQueryOptions().setDefaultIdempotence(true); | |
| 29 | +``` | |
| 30 | + | |
| 31 | +Any statement on which you didn't call `setIdempotent` gets this default value. | |
| 32 | + | |
| 33 | +Bound statements inherit the flag from the prepared statement they were created from: | |
| 34 | + | |
| 35 | +```java | |
| 36 | +PreparedStatement pst = session.prepare("SELECT * FROM users WHERE id = ?"); | |
| 37 | +// This cast is for backward-compatibility reasons. On 3.0+, you can do pst.setIdempotent(true) directly | |
| 38 | +((IdempotenceAwarePreparedStatement) pst).setIdempotent(true); | |
| 39 | + | |
| 40 | +BoundStatement bst = pst.bind(); | |
| 41 | +assert bst.isIdempotent(); | |
| 42 | +``` | |
| 43 | + | |
| 44 | +### Idempotence in the query builder | |
| 45 | + | |
| 46 | +The [QueryBuilder] DSL tries to infer the `isIdempotent` flag on the statements it generates. The following statements | |
| 47 | +will be marked **non-idempotent**: | |
| 48 | + | |
| 49 | +* counter updates: | |
| 50 | + | |
| 51 | + ```java | |
| 52 | + update("mytable").with(incr("c")).where(eq("k", 1)); | |
| 53 | + ``` | |
| 54 | +* prepend, append or deletion operations on lists: | |
| 55 | + | |
| 56 | + ```java | |
| 57 | + update("mytable").with(append("l", 1)).where(eq("k", 1)); | |
| 58 | + delete().listElt("l", 1).from("mytable").where(eq("k", 1)); | |
| 59 | + ``` | |
| 60 | +* queries that insert the result of a function call or a "raw" string in a column (or as an element in a collection | |
| 61 | + column): | |
| 62 | + | |
| 63 | + ```java | |
| 64 | + update("mytable").with(set("v", now())).where(eq("k", 1)); | |
| 65 | + update("mytable").with(set("v", fcall("myCustomFunc"))).where(eq("k", 1)); | |
| 66 | + update("mytable").with(set("v", raw("myCustomFunc()"))).where(eq("k", 1)); | |
| 67 | + ``` | |
| 68 | + | |
| 69 | + This is a conservative approach, since the driver can't guess whether a function is idempotent, or what a raw string | |
| 70 | + contains. It might yield false negatives, that you'll have to fix manually. | |
| 71 | + | |
| 72 | +* lightweight transactions (see the next section for a detailed explanation): | |
| 73 | + | |
| 74 | + ```java | |
| 75 | + insertInto("mytable").value("k", 1).value("v", 2).ifNotExists(); | |
| 76 | + ``` | |
| 77 | + | |
| 78 | +If these rules produce a false negative, you can manually override the flag on the built statement: | |
| 79 | + | |
| 80 | +```java | |
| 81 | +BuiltStatement s = update("mytable").with(set("v", fcall("anIdempotentFunc"))).where(eq("k", 1)); | |
| 82 | + | |
| 83 | +// False negative because the driver can't guess that anIdempotentFunc() is safe | |
| 84 | +assert !s.isIdempotent(); | |
| 85 | + | |
| 86 | +// Fix it | |
| 87 | +s.setIdempotent(true); | |
| 88 | +``` | |
| 89 | + | |
| 90 | + | |
| 91 | +### Idempotence and lightweight transactions | |
| 92 | + | |
| 93 | +As explained in the previous section, the query builder considers lightweight transactions as non-idempotent. This might | |
| 94 | +sound counter-intuitive, as these queries can sometimes be safe to execute multiple times. For example, consider the | |
| 95 | +following query: | |
| 96 | + | |
| 97 | +``` | |
| 98 | +UPDATE mytable SET v = 4 WHERE k = 1 IF v = 1 | |
| 99 | +``` | |
| 100 | + | |
| 101 | +If we execute it twice, the `IF` condition will fail the second time, so the second execution will do nothing and `v` | |
| 102 | +will still have the value 4. | |
| 103 | + | |
| 104 | +However, the problem appears when we consider multiple clients executing the query with retries: | |
| 105 | + | |
| 106 | +1. `v` has the value 1; | |
| 107 | +2. client 1 executes the query above, performing a a CAS (compare and set) from 1 to 4; | |
| 108 | +3. client 1's connection drops, but the query completes successfully. `v` now has the value 4; | |
| 109 | +4. client 2 executes a CAS from 4 to 2; | |
| 110 | +5. client 2's transaction succeeds. `v` now has the value 2; | |
| 111 | +6. since client 1 lost its connection, it considers the query as failed, and transparently retries the CAS from 1 to 4. | |
| 112 | + But since the column now has value 2, it receives a "not applied" response. | |
| 113 | + | |
| 114 | +One important aspect of lightweight transactions is [linearizability]: given a set of concurrent operations on a column | |
| 115 | +from different clients, there must be a way to reorder them to yield a sequential history that is correct. From our | |
| 116 | +clients' point of view, there were two operations: | |
| 117 | + | |
| 118 | +* client 1 executed a CAS from 1 to 4, that was not applied; | |
| 119 | +* client 2 executed a CAS from 4 to 2, that was applied. | |
| 120 | + | |
| 121 | +But overall the column changed from 1 to 2. There is no ordering of the two operations that can explain that change. We | |
| 122 | +broke linearizability by doing a transparent retry at step 6. | |
| 123 | + | |
| 124 | +To avoid this, the driver considers lightweight transactions as non-idempotent, and provides a | |
| 125 | +[retry policy](../retries/) that doesn't retry non-idempotent statements. If linearizability is important for you, you | |
| 126 | +should use that policy, and ensure that lightweight transactions are appropriately flagged. | |
| 127 | + | |
| 128 | +[isIdempotent]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Statement.html#isIdempotent-- | |
| 129 | +[setDefaultIdempotence]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/QueryOptions.html#setDefaultIdempotence-boolean- | |
| 130 | +[QueryBuilder]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/querybuilder/QueryBuilder.html | |
| 131 | + | |
| 132 | +[linearizability]: https://en.wikipedia.org/wiki/Linearizability#Definition_of_linearizability | |
| No newline at the end of the file |
modified manual/logging/README.md +3 −5
| @@ -176,11 +176,9 @@ | ||
| 176 | 176 | |
| 177 | 177 | Currently the `QueryLogger` can be configured to track slow queries using either |
| 178 | 178 | a constant threshold in milliseconds (which is the default behavior), or |
| 179 | -a dynamic threshold based on per-host latency percentiles, as computed by `PerHostPercentileTracker`. | |
| 180 | - | |
| 181 | -**Dynamic thresholds are still a beta feature as of version 2.1.6: they | |
| 182 | -haven't been extensively tested yet, and the API is still subject to | |
| 183 | -change.** | |
| 179 | +a dynamic threshold based on latency percentiles, as computed by a `PercentileTracker` (see the | |
| 180 | +[speculative executions](../speculative_execution/#percentile-speculative-execution-policy) section for more information | |
| 181 | +about percentile trackers). | |
| 184 | 182 | |
| 185 | 183 | Refer to the `QueryLogger` [API docs][query_logger] for an example of usage. |
| 186 | 184 |
modified manual/query_timestamps/README.md +4 −0
| @@ -98,6 +98,10 @@ | ||
| 98 | 98 | INFO com.datastax.driver.core.ClockFactory - Using native clock to generate timestamps |
| 99 | 99 | ``` |
| 100 | 100 | |
| 101 | +Note: currently, native calls won't work from an OSGI container. This is because JNR artifacts don't include proper OSGI | |
| 102 | +descriptors. This will be fixed in an upcoming version (see | |
| 103 | +[JAVA-1127](https://datastax-oss.atlassian.net/browse/JAVA-1127)). | |
| 104 | + | |
| 101 | 105 | [gettimeofday]: http://man7.org/linux/man-pages/man2/settimeofday.2.html |
| 102 | 106 | [JNR]: https://github.com/jnr/jnr-ffi |
| 103 | 107 |
modified manual/retries/README.md +197 −2
| @@ -1,6 +1,201 @@ | ||
| 1 | 1 | ## Retries |
| 2 | 2 | |
| 3 | -*Coming soon... In the meantime, see the javadoc for [RetryPolicy].* | |
| 3 | +When a query fails, it sometimes makes sense to retry it: the error might be temporary, or the query might work on a | |
| 4 | +different host, or with different options. | |
| 4 | 5 | |
| 6 | +The driver uses a configurable set of rules to determine when and how to retry. | |
| 5 | 7 | |
| 6 | -[RetryPolicy]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/RetryPolicy.html | |
| No newline at the end of the file | ||
| 8 | +### Concepts | |
| 9 | + | |
| 10 | +When the driver executes a statement, it first obtains a [query plan] \(a list of hosts) from the load balancing policy. | |
| 11 | +Then it picks the first host and sends it the request; this host acts as the **coordinator** for the query, it will | |
| 12 | +communicate with the rest of the cluster and reply to the client. | |
| 13 | + | |
| 14 | +If the coordinator can't be reached or replies with an error, there are various things that the driver can do; they are | |
| 15 | +expressed as [RetryDecision] objects: | |
| 16 | + | |
| 17 | +* [retry()]: retry the query on the same host. It's possible to retry with a different consistency level than the one | |
| 18 | + that was originally requested; | |
| 19 | +* [tryNextHost()]: retry on the next host in the query plan. Again, it can be with a different CL; | |
| 20 | +* [rethrow()]: rethrow the exception to the user code. This means it will be thrown from the `session.execute` call (or | |
| 21 | + returned as a failed future if `executeAsync` was used); | |
| 22 | +* [ignore()]: mark the request as successful, and return an empty result set. | |
| 23 | + | |
| 24 | +If the driver retries on every host and reaches the end of the query plan, a [NoHostAvailableException] is thrown to the | |
| 25 | +user code. You can use its [getErrors()] method to find out what went wrong on each host. | |
| 26 | + | |
| 27 | + | |
| 28 | +### Retry policy | |
| 29 | + | |
| 30 | +[RetryPolicy] is a pluggable component that determines the retry decisions for various types of errors. It is configured | |
| 31 | +when initializing the cluster: | |
| 32 | + | |
| 33 | +```java | |
| 34 | +Cluster cluster = Cluster.builder() | |
| 35 | + .addContactPoint("127.0.0.1") | |
| 36 | + .withRetryPolicy(new MyCustomPolicy()) | |
| 37 | + .build(); | |
| 38 | +``` | |
| 39 | + | |
| 40 | +Once the cluster has been built, you can't change the policy, but you may inspect it at runtime: | |
| 41 | + | |
| 42 | +```java | |
| 43 | +RetryPolicy policy = cluster.getConfiguration().getPolicies().getRetryPolicy(); | |
| 44 | +``` | |
| 45 | + | |
| 46 | +If you don't explicitly configure it, you get a [DefaultRetryPolicy]. | |
| 47 | + | |
| 48 | +The policy's methods cover different types of errors: | |
| 49 | + | |
| 50 | +#### [onUnavailable] | |
| 51 | + | |
| 52 | +A request reached the coordinator, but there weren't enough live replicas to achieve the requested consistency level. | |
| 53 | +The coordinator replied with an `UNAVAILABLE` error. | |
| 54 | + | |
| 55 | +If the policy rethrows the error, the user code will get an [UnavailableException]. You can inspect the exception's | |
| 56 | +fields to get the amount of replicas that were *known* to be alive when the error was triggered, as well as the amount | |
| 57 | +of replicas that where *required* by the requested consistency level. | |
| 58 | + | |
| 59 | +#### [onReadTimeout] | |
| 60 | + | |
| 61 | +A read request reached the coordinator, which initially believed that there were enough live replicas to process it. | |
| 62 | +But, for some reason, one or several replicas were too slow to answer within the predefined timeout | |
| 63 | +(`read_request_timeout_in_ms` in `cassandra.yaml`), and the coordinator replied to the client with a `READ_TIMEOUT` | |
| 64 | +error. | |
| 65 | + | |
| 66 | +This could be due to a temporary overloading of these replicas, or even | |
| 67 | +that they just failed or were turned off. During reads, Cassandra doesn't request data from every replica to minimize | |
| 68 | +internal network traffic; instead, some replicas are only asked for a checksum of the data. A read timeout may occur | |
| 69 | +even if enough replicas responded to fulfill the consistency level, but only checksum responses were received (the | |
| 70 | +method's `dataRetrieved` parameter allow you to check if you're in that situation). | |
| 71 | + | |
| 72 | +If the policy rethrows the error, the user code will get a [ReadTimeoutException]. | |
| 73 | + | |
| 74 | +Note: do not confuse this error with a [driver read timeout], which happens when the coordinator didn't reply at all to | |
| 75 | +the client. | |
| 76 | + | |
| 77 | +#### [onWriteTimeout] | |
| 78 | + | |
| 79 | +This is similar to `onReadTimeout`, but for write operations. The reason reads and writes are handled separately is | |
| 80 | +because a read is obviously a non mutating operation, whereas a write is likely to be. If a write times out at the | |
| 81 | +coordinator level, there is no way to know whether the mutation was applied or not on the non-answering replica. | |
| 82 | +Policy implementations are usually more conservative in `onWriteTimeout` in case the write was not idempotent. | |
| 83 | + | |
| 84 | +If the policy rethrows the error, the user code will get a [WriteTimeoutException]. | |
| 85 | + | |
| 86 | +#### [onRequestError] | |
| 87 | + | |
| 88 | +This gets called for any other error occurring after the request was sent. | |
| 89 | + | |
| 90 | +Note: this method is specified by [ExtendedRetryPolicy], a child interface of `RetryPolicy`. This is for backward | |
| 91 | +compatibility: `onRequestError` did not exist in previous 2.1 versions, so it wasn't added directly on `RetryPolicy` to | |
| 92 | +avoid breaking legacy implementations. All built-in policies implement `ExtendedRetryPolicy`, and we recommend that you | |
| 93 | +do too. If you pass a policy that only implements `RetryPolicy`, the driver will use a default `onRequestError` | |
| 94 | +implementation that always retries on the next host with the same consistency level (this is consistent with what | |
| 95 | +previous 2.1 versions did). In 3.0+, the two interfaces have been merged back together. | |
| 96 | + | |
| 97 | +The method receives the exception as a parameter, so that implementations can refine their decision based on what | |
| 98 | +happened. The possible exceptions are: | |
| 99 | + | |
| 100 | +* [ServerError]: thrown by the coordinator when an unexpected error occurs. This is generally a Cassandra bug; | |
| 101 | +* [OperationTimedOutException]: thrown by the client when it didn't hear back from the coordinator within the | |
| 102 | + [driver read timeout]; | |
| 103 | +* [ConnectionException]: thrown by the client for any network issue while or after the request was written; | |
| 104 | +* [OverloadedException]: thrown by the coordinator when replicas are down and the number of hinted handoffs gets too | |
| 105 | + high; the coordinator temporarily refuses writes for these replicas (see [hinted handoffs] in the Cassandra | |
| 106 | + documentation). | |
| 107 | + | |
| 108 | +In all these cases, there is no absolute certainty that the request was not applied by a host, so implementations must | |
| 109 | +consider the risk of retrying non-idempotent statements. | |
| 110 | + | |
| 111 | +### Hard-coded rules | |
| 112 | + | |
| 113 | +There are a few cases where retrying is always the right thing to do. These are not covered by `RetryPolicy`, but | |
| 114 | +hard-coded in the driver: | |
| 115 | + | |
| 116 | +* **any error before a network write was attempted**: to send a query, the driver selects a host, borrows a connection | |
| 117 | + from the host's [connection pool], and then writes the message to the connection. | |
| 118 | + Errors can occur before the write was even attempted, for example if the connection pool is saturated, or if the | |
| 119 | + host went down right after we borrowed. In those cases, it is always safe to retry since the request wasn't sent, so | |
| 120 | + the driver will transparently move to the next host in the query plan. | |
| 121 | + | |
| 122 | +* **re-preparing a statement**: when the driver executes a prepared statement, it may find out that the coordinator | |
| 123 | + doesn't know about it, and need to re-prepare it on the fly (this is described in detail [here][prepared]). The query | |
| 124 | + is then retried on the same host. | |
| 125 | + | |
| 126 | +* **trying to communicate with a host that is bootstrapping**: this is a rare edge case, as in practice the driver | |
| 127 | + should never try to communicate with a bootstrapping host (the only way is if it was specified as a contact point). | |
| 128 | + Anyway, it is again safe to assume that the query was not executed at all, so the driver moves to the next host. | |
| 129 | + | |
| 130 | +Similarly, some errors have no chance of being solved by a retry. They will always be rethrown directly to the user. | |
| 131 | +These include: | |
| 132 | + | |
| 133 | +* [QueryValidationException] and any of its subclasses ([InvalidQueryException], [InvalidConfigurationInQueryException], | |
| 134 | + [UnauthorizedException], [SyntaxError], [AlreadyExistsException]); | |
| 135 | +* [TruncateException]. | |
| 136 | + | |
| 137 | + | |
| 138 | +### Retries and idempotence | |
| 139 | + | |
| 140 | +Retry policies should avoid retrying [idempotent queries] when it's not clear whether the query was applied or not. | |
| 141 | + | |
| 142 | +It's always safe to retry in `onReadTimeout`, since by definition we know that the query is a read, which doesn't mutate | |
| 143 | +any data. Similarly, `onUnavailable` is safe: the coordinator is telling us that it didn't find enough replicas, so we | |
| 144 | +know that it didn't try to apply the query. | |
| 145 | + | |
| 146 | +`onWriteTimeout` is not safe. The default retry policy is very conservative (it only retries batch log writes) so it | |
| 147 | +will never cause any issue. Custom implementations should check the statements' idempotent flag. | |
| 148 | + | |
| 149 | +`onRequestError` isn't safe either. | |
| 150 | + | |
| 151 | +**For historical reasons, the built-in retry policy implementations do not check the idempotent flag in | |
| 152 | +`onRequestError`**. This is based on the fact that: | |
| 153 | + | |
| 154 | +* previous versions of the driver (which did `onRequestError` internally) didn't check the flag either; | |
| 155 | +* the majority of queries in an application should be idempotent; | |
| 156 | +* statements start out as non-idempotent by default. | |
| 157 | + | |
| 158 | +If `onRequestError` enforced idempotence strictly, this would cause a lot of queries that were legitimately retried with | |
| 159 | +previous driver versions to not be retried anymore. This would be a big behavioral change, so we decided to prefer | |
| 160 | +consistency with previous versions. | |
| 161 | + | |
| 162 | +If you prefer strict handling of the idempotent flag, you can wrap your retry policy into an | |
| 163 | +[IdempotenceAwareRetryPolicy]. Make sure to position the flag properly on all statements. | |
| 164 | + | |
| 165 | + | |
| 166 | +[RetryDecision]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/RetryPolicy.RetryDecision.html | |
| 167 | +[retry()]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/RetryPolicy.RetryDecision.html#retry-com.datastax.driver.core.ConsistencyLevel- | |
| 168 | +[tryNextHost()]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/RetryPolicy.RetryDecision.html#tryNextHost-com.datastax.driver.core.ConsistencyLevel- | |
| 169 | +[rethrow()]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/RetryPolicy.RetryDecision.html#rethrow-- | |
| 170 | +[ignore()]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/RetryPolicy.RetryDecision.html#ignore-- | |
| 171 | +[NoHostAvailableException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/NoHostAvailableException.html | |
| 172 | +[getErrors()]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/NoHostAvailableException.html#getErrors-- | |
| 173 | +[RetryPolicy]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/RetryPolicy.html | |
| 174 | +[DefaultRetryPolicy]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/DefaultRetryPolicy.html | |
| 175 | +[onReadTimeout]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/DefaultRetryPolicy.html#onReadTimeout-com.datastax.driver.core.Statement-com.datastax.driver.core.ConsistencyLevel-int-int-boolean-int- | |
| 176 | +[onWriteTimeout]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/DefaultRetryPolicy.html#onWriteTimeout-com.datastax.driver.core.Statement-com.datastax.driver.core.ConsistencyLevel-com.datastax.driver.core.WriteType-int-int-int- | |
| 177 | +[onUnavailable]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/DefaultRetryPolicy.html#onUnavailable-com.datastax.driver.core.Statement-com.datastax.driver.core.ConsistencyLevel-int-int-int- | |
| 178 | +[onRequestError]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/ExtendedRetryPolicy.html#onRequestError-com.datastax.driver.core.Statement-com.datastax.driver.core.ConsistencyLevel-java.lang.Exception-int- | |
| 179 | +[ExtendedRetryPolicy]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/ExtendedRetryPolicy.html | |
| 180 | +[UnavailableException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/UnavailableException.html | |
| 181 | +[ReadTimeoutException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/ReadTimeoutException.html | |
| 182 | +[WriteTimeoutException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/WriteTimeoutException.html | |
| 183 | +[OverloadedException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/OverloadedException.html | |
| 184 | +[ServerError]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/ServerError.html | |
| 185 | +[OperationTimedOutException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/OperationTimedOutException.html | |
| 186 | +[ConnectionException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/ConnectionException.html | |
| 187 | +[QueryValidationException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/QueryValidationException.html | |
| 188 | +[InvalidQueryException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/InvalidQueryException.html | |
| 189 | +[InvalidConfigurationInQueryException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/InvalidConfigurationInQueryException.html | |
| 190 | +[UnauthorizedException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/UnauthorizedException.html | |
| 191 | +[SyntaxError]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/SyntaxError.html | |
| 192 | +[AlreadyExistsException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/AlreadyExistsException.html | |
| 193 | +[TruncateException]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/TruncateException.html | |
| 194 | +[IdempotenceAwareRetryPolicy]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/IdempotenceAwareRetryPolicy.html | |
| 195 | + | |
| 196 | +[query plan]: ../load_balancing/#query-plan | |
| 197 | +[connection pool]: ../pooling/ | |
| 198 | +[prepared]: ../statements/prepared/#preparing-on-multiple-nodes | |
| 199 | +[driver read timeout]: ../socket_options/#driver-read-timeout | |
| 200 | +[hinted handoffs]: https://docs.datastax.com/en/cassandra/2.1/cassandra/dml/dml_about_hh_c.html?scroll=concept_ds_ifg_jqx_zj__performance | |
| 201 | +[idempotent queries]: ../idempotence/ | |
| No newline at the end of the file |
modified manual/socket_options/README.md +2 −1
| @@ -93,7 +93,8 @@ | ||
| 93 | 93 | #### Driver read timeout vs. server read timeout |
| 94 | 94 | |
| 95 | 95 | Unfortunately, the term "read timeout" clashes with another concept that is not directly related: a Cassandra node may |
| 96 | -reply with a `Read_timeout` error when it didn't hear back from enough replicas during a read query. | |
| 96 | +reply with a [READ_TIMEOUT](../retries/#on-read-timeout) error when it didn't hear back from enough replicas during a | |
| 97 | +read query. | |
| 97 | 98 | |
| 98 | 99 | To clarify: |
| 99 | 100 |
modified manual/speculative_execution/README.md +23 −79
| @@ -60,57 +60,9 @@ | ||
| 60 | 60 | |
| 61 | 61 | ### Query idempotence |
| 62 | 62 | |
| 63 | -One important aspect to consider is whether queries are idempotent, i.e. | |
| 64 | -whether they can be applied multiple times without changing the result | |
| 65 | -beyond the initial application. **If a query is not idempotent, the | |
| 66 | -driver will never schedule speculative executions for it**, because | |
| 63 | +If a query is [not idempotent](../idempotence/), the driver will never schedule speculative executions for it, because | |
| 67 | 64 | there is no way to guarantee that only one node will apply the mutation. |
| 68 | 65 | |
| 69 | -As of Cassandra 2.1.4, the only queries that are *not* idempotent are: | |
| 70 | - | |
| 71 | -* counter operations; | |
| 72 | -* prepending or appending to a list column; | |
| 73 | -* using non-idempotent CQL functions, like `now()` or `uuid()`. | |
| 74 | - | |
| 75 | -In the driver, this is determined by | |
| 76 | -[Statement#isIdempotent()][isIdempotent]. Unfortunately, the driver | |
| 77 | -doesn't parse query strings, so in most cases it has no information | |
| 78 | -about what the query actually does. Therefore: | |
| 79 | - | |
| 80 | -* **`Statement#isIdempotent()` is only computed automatically for | |
| 81 | - statements built with [QueryBuilder][QueryBuilder]**. | |
| 82 | - Note that the driver takes a rather conservative approach with uses | |
| 83 | - of `fcall()` or `raw()`: whenever they appear in a value to be | |
| 84 | - inserted in the database (like the values of an `Insert` or the | |
| 85 | - right-hand side of an assignment in an `Update`), the statement | |
| 86 | - will be considered non-idempotent by default. If you know that your | |
| 87 | - CQL functions or expressions are safe, force idempotence to `true` | |
| 88 | - on the statement manually (see below); | |
| 89 | -* **for all other types of statements, it defaults to `false`.** You'll | |
| 90 | - need to set it manually, with one of the mechanism described below. | |
| 91 | - | |
| 92 | -You can override the value on each statement: | |
| 93 | - | |
| 94 | -```java | |
| 95 | -Statement s = new SimpleStatement("SELECT * FROM users WHERE id = 1"); | |
| 96 | -s.setIdempotent(true); | |
| 97 | -``` | |
| 98 | - | |
| 99 | -Note that this will also work for built statements (and override the | |
| 100 | -computed value). | |
| 101 | - | |
| 102 | -Additionally, if you know for a fact that your application does not use | |
| 103 | -any of the non-idempotent CQL queries listed above, you can change the | |
| 104 | -default cluster-wide: | |
| 105 | - | |
| 106 | -```java | |
| 107 | -// Make all statements idempotent by default: | |
| 108 | -cluster.getConfiguration().getQueryOptions().setDefaultIdempotence(true); | |
| 109 | -``` | |
| 110 | - | |
| 111 | -[isIdempotent]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Statement.html#isIdempotent-- | |
| 112 | -[QueryBuilder]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/querybuilder/QueryBuilder.html | |
| 113 | - | |
| 114 | 66 | ### Enabling speculative executions |
| 115 | 67 | |
| 116 | 68 | Speculative executions are controlled by an instance of |
| @@ -148,14 +100,11 @@ | ||
| 148 | 100 | |
| 149 | 101 | [csep]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/ConstantSpeculativeExecutionPolicy.html |
| 150 | 102 | |
| 151 | -#### [PercentileSpeculativeExecutionPolicy][psep] | |
| 103 | +#### [PercentileSpeculativeExecutionPolicy] | |
| 152 | 104 | |
| 153 | 105 | This policy sets the threshold at a given latency percentile for the |
| 154 | 106 | current host, based on recent statistics. |
| 155 | 107 | |
| 156 | -**As of 2.1.6, this class is provided as a beta preview: it hasn't been | |
| 157 | -extensively tested yet, and the API is still subject to change.** | |
| 158 | - | |
| 159 | 108 | First and foremost, make sure that the [HdrHistogram][hdr] library (used |
| 160 | 109 | under the hood to collect latencies) is in your classpath. It's defined |
| 161 | 110 | as an optional dependency in the driver's POM, so you'll need to |
| @@ -169,19 +118,26 @@ | ||
| 169 | 118 | </dependency> |
| 170 | 119 | ``` |
| 171 | 120 | |
| 172 | -Then create an instance of [PerHostPercentileTracker][phpt] that will collect | |
| 173 | -latency statistics for your `Cluster`: | |
| 121 | +Then create a [PercentileTracker] that will collect latency statistics for your `Cluster`. Two implementations are | |
| 122 | +provided with the driver: | |
| 123 | + | |
| 124 | +* [PerHostPercentileTracker]: maintains one histogram per host. A given host is only compared to itself, so its | |
| 125 | + latencies will rank in the higher percentiles only if it's slower than its usual performance; | |
| 126 | +* [ClusterWidePercentileTracker]: maintains a single histogram for the whole cluster. Hosts are compared against each | |
| 127 | + other, so a host that is consistently slower than the rest of the cluster will get bad rankings. | |
| 128 | + | |
| 129 | +We recommend trying `ClusterWidePercentileTracker` first, as it has produced the best results in our tests. You may also | |
| 130 | +extend `PercentileTracker` with your own implementation. | |
| 174 | 131 | |
| 175 | 132 | ```java |
| 176 | 133 | // There are more options than shown here, please refer to the API docs |
| 177 | 134 | // for more information |
| 178 | -PerHostPercentileTracker tracker = PerHostPercentileTracker | |
| 179 | - .builderWithHighestTrackableLatencyMillis(15000) | |
| 135 | +PercentileTracker tracker = ClusterWidePercentileTracker | |
| 136 | + .builder(15000) | |
| 180 | 137 | .build(); |
| 181 | 138 | ``` |
| 182 | 139 | |
| 183 | -Create an instance of the policy with the tracker, and pass it to your | |
| 184 | -cluster: | |
| 140 | +Next, create an instance of the policy with the tracker, and pass it to your cluster: | |
| 185 | 141 | |
| 186 | 142 | ```java |
| 187 | 143 | PercentileSpeculativeExecutionPolicy policy = |
| @@ -196,20 +152,16 @@ | ||
| 196 | 152 | .build(); |
| 197 | 153 | ``` |
| 198 | 154 | |
| 199 | -Finally, don't forget to register your tracker with the cluster (the | |
| 200 | -policy does not do this itself): | |
| 201 | - | |
| 202 | -```java | |
| 203 | -cluster.register(tracker); | |
| 204 | -``` | |
| 205 | - | |
| 206 | -Note that `PerHostPercentileTracker` may also be used with a slow query | |
| 207 | -logger (see the [Logging](../logging/) section). In that case, you would | |
| 155 | +Note that `PercentileTracker` may also be used with a slow query | |
| 156 | +logger (see the [Logging](../logging/#constant-vs-dynamic-thresholds) section). In that case, you would | |
| 208 | 157 | create a single tracker object and share it with both components. |
| 209 | 158 | |
| 210 | -[psep]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/PercentileSpeculativeExecutionPolicy.html | |
| 159 | +[PercentileSpeculativeExecutionPolicy]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/PercentileSpeculativeExecutionPolicy.html | |
| 160 | +[PercentileTracker]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/PercentileTracker.html | |
| 161 | +[PerHostPercentileTracker]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/PerHostPercentileTracker.html | |
| 162 | +[ClusterWidePercentileTracker]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/ClusterWidePercentileTracker.html | |
| 163 | + | |
| 211 | 164 | [hdr]: http://hdrhistogram.github.io/HdrHistogram/ |
| 212 | -[phpt]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/PerHostPercentileTracker.html | |
| 213 | 165 | |
| 214 | 166 | #### Using your own |
| 215 | 167 | |
| @@ -218,15 +170,7 @@ | ||
| 218 | 170 | |
| 219 | 171 | ### How speculative executions affect retries |
| 220 | 172 | |
| 221 | -Regardless of speculative executions, the driver has a retry mechanism: | |
| 222 | - | |
| 223 | -* on an internal error, it will try the next host; | |
| 224 | -* when the consistency level could not be reached (`unavailable` error | |
| 225 | - or read or write timeout), it will delegate the decision to | |
| 226 | - [RetryPolicy][retry_policy], which might trigger a retry on the same | |
| 227 | - host. | |
| 228 | - | |
| 229 | -Turning speculative executions on doesn't change this behavior. Each | |
| 173 | +Turning speculative executions on doesn't change the driver's [retry](../retries/) behavior. Each | |
| 230 | 174 | parallel execution will trigger retries independently: |
| 231 | 175 | |
| 232 | 176 | ```ditaa |