| 1 | ## Upgrade guide |
| 2 | |
| 3 | The purpose of this guide is to detail changes made by successive |
| 4 | versions of the Java driver. |
| 5 | |
| 6 | ### 2.0.11 |
| 7 | |
| 8 | 2.0.11 preserves binary compatibility with previous versions. There are a few |
| 9 | changes in the driver's behavior: |
| 10 | |
| 11 | 1. The `DefaultRetryPolicy`'s behaviour has changed in the case of an Unavailable |
| 12 | exception received from a request. The new behaviour will cause the driver to |
| 13 | process a Retry on a different node at most once, otherwise an exception will |
| 14 | be thrown. This change makes sense in the case where the node tried initially |
| 15 | for the request happens to be isolated from the rest of the cluster (e.g. |
| 16 | because of a network partition) but can still answer to the client normally. |
| 17 | In this case, trying another node has a chance of success. |
| 18 | The previous behaviour was to always throw an exception. |
| 19 | |
| 20 | 2. A `BuiltStatement` is now considered non-idempotent whenever a `fcall()` |
| 21 | or `raw()` is used to build a value to be inserted in the database. |
| 22 | If you know that the CQL functions or expressions are safe, use |
| 23 | `setIdempotent(true)` on the statement. |
| 24 | |
| 25 | 3. The list of contact points provided at startup is now shuffled before trying |
| 26 | to open the control connection, so that multiple clients with the same contact |
| 27 | points don't all pick the same control host. As a result, you can't assume that |
| 28 | the driver will try contact points in a deterministic order. In particular, if |
| 29 | you use the `DCAwareRoundRobinPolicy` without specifying a primary datacenter |
| 30 | name, make sure that you only provide local hosts as contact points. |
| 31 | |
| 32 | |
| 33 | ### 2.0.x to 2.0.10 |
| 34 | |
| 35 | We try to avoid breaking changes within a branch (2.0.x to 2.0.y), but |
| 36 | 2.0.10 saw a lot of new features and internal improvements. There is one |
| 37 | breaking change: |
| 38 | |
| 39 | 1. `LatencyTracker#update` now has a different signature and takes two new |
| 40 | parameters: the statement that has been executed (never null), and the exception |
| 41 | thrown while executing the query (or null, if the query executed successfully). |
| 42 | Existing implementations of this interface, once upgraded to the new method |
| 43 | signature, should continue to work as before. |
| 44 | |
| 45 | The following might also be of interest: |
| 46 | |
| 47 | 2. `SocketOptions#getTcpNoDelay()` is now TRUE by default (it was previously undefined). |
| 48 | This reflects the new behavior of Netty (which was upgraded from version 3.9.0 to |
| 49 | 4.0.27): `TCP_NODELAY` is now turned on by default, instead of depending on the OS |
| 50 | default like in previous versions. |
| 51 | |
| 52 | 3. Netty is not shaded anymore in the default Maven artifact. However we publish a |
| 53 | [shaded artifact](../features/shaded_jar/) under a different classifier. |
| 54 | |
| 55 | 4. The internal initialization sequence of the Cluster object has been slightly changed: |
| 56 | some fields that were previously initialized in the constructor are now set when |
| 57 | the `init()` method is called. In particular, `Cluster#getMetrics()` will return |
| 58 | `null` until the cluster is initialized. |
| 59 | |
| 60 | ### 1.0 to 2.0 |
| 61 | |
| 62 | We used the opportunity of a major version bump to incorporate your feedback |
| 63 | and improve the API, to fix a number of inconsistencies and remove cruft. |
| 64 | Unfortunately this means there are some breaking changes, but the new API should |
| 65 | be both simpler and more complete. |
| 66 | |
| 67 | The following describes the changes for 2.0 that are breaking changes of the |
| 68 | 1.0 API. For ease of use, we distinguish two categories of API changes: the "main" |
| 69 | ones and the "other" ones. |
| 70 | |
| 71 | The "main" API changes are the ones that are either |
| 72 | likely to affect most upgraded apps or are incompatible changes that, even if minor, |
| 73 | will not be detected at compile time. Upgraders are highly encouraged to check |
| 74 | this list of "main" changes while upgrading their application to 2.0 (even |
| 75 | though most applications are likely to be affected by only a handful of |
| 76 | changes). |
| 77 | |
| 78 | The "other" list is, well, other changes: those that are likely to |
| 79 | affect a minor number of applications and will be detected by compile time |
| 80 | errors anyway. It is ok to skip those initially and only come back to them if |
| 81 | you have trouble compiling your application after an upgrade. |
| 82 | |
| 83 | #### Main API changes |
| 84 | |
| 85 | 1. The `Query` class has been renamed into `Statement` (it was confusing |
| 86 | to some that the `BoundStatement` was not a `Statement`). To allow |
| 87 | this, the old `Statement` class has been renamed to `RegularStatement`. |
| 88 | |
| 89 | 2. The `Cluster` and `Session` shutdown API has changed. There is now a |
| 90 | `closeAsync` that is asynchronous but returns a `Future` on the |
| 91 | completion of the shutdown process. There is also a `close` shortcut |
| 92 | that does the same but blocks. Also, `close` now waits for ongoing |
| 93 | queries to complete by default (but you can force the closing of all |
| 94 | connections if you want to). |
| 95 | |
| 96 | 3. `NoHostAvailableException#getErrors` now returns the full exception objects for |
| 97 | each node instead of just a message. In other words, it returns a |
| 98 | `Map<InetAddress, Throwable>` instead of a `Map<InetAddress, String>`. |
| 99 | |
| 100 | 4. `Statement#getConsistencyLevel` (previously `Query#getConsistencyLevel`, see |
| 101 | first point) will now return `null` by default (instead of `CL.ONE`), with the |
| 102 | meaning of "use the default consistency level". |
| 103 | The default consistency level can now be configured through the new `QueryOptions` |
| 104 | object in the cluster `Configuration`. |
| 105 | |
| 106 | 5. The `Metrics` class now uses the Codahale metrics library version 3 (version 2 was |
| 107 | used previously). This new major version of the library has many API changes |
| 108 | compared to its version 2 (see the [release notes](https://dropwizard.github.io/metrics/3.1.0/about/release-notes/) for details), |
| 109 | which can thus impact consumers of the Metrics class. |
| 110 | Furthermore, the default `JmxReporter` now includes a name specific to the |
| 111 | cluster instance (to avoid conflicts when multiple Cluster instances are created |
| 112 | in the same JVM). As a result, tools that were polling JMX info will |
| 113 | have to be updated accordingly. |
| 114 | |
| 115 | 6. The `QueryBuilder#in` method now has the following special case: using |
| 116 | `QueryBuilder.in(QueryBuilder.bindMarker())` will generate the string `IN ?`, |
| 117 | not `IN (?)` as was the case in 1.0. The reasoning being that the former |
| 118 | syntax, made valid by [CASSANDRA-4210](https://issues.apache.org/jira/browse/CASSANDRA-4210) |
| 119 | is a lot more useful than `IN (?)`, as the latter can more simply use an |
| 120 | equality. |
| 121 | Note that if you really want to output `IN (?)` with the query |
| 122 | builder, you can use `QueryBuilder.in(QueryBuilder.raw("?"))`. |
| 123 | |
| 124 | 7. When binding values by name in `BoundStatement` (i.e. using the |
| 125 | `setX(String, X)` methods), if more than one variable have the same name, |
| 126 | then all values corresponding to that variable |
| 127 | name are set instead of just the first occurrence. |
| 128 | |
| 129 | 8. The `QueryBuilder#raw` method does not automatically add quotes anymore, but |
| 130 | rather output its result without any change (as the raw name implies). |
| 131 | This means for instance that `eq("x", raw(foo))` will output `x = foo`, |
| 132 | not `x = 'foo'` (you don't need the raw method to output the latter string). |
| 133 | |
| 134 | 9. The `QueryBuilder` will now sometimes use the new ability to send value as |
| 135 | bytes instead of serializing everything to string. In general the QueryBuilder |
| 136 | will do the right thing, but if you were calling the `getQueryString()` method |
| 137 | on a Statement created with a QueryBuilder (for other reasons than to prepare a query) |
| 138 | then the returned string may contain bind markers in place of some of the values |
| 139 | provided (and in that case, `getValues()` will contain the values corresponding |
| 140 | to those markers). If need be, it is possible to force the old behavior by |
| 141 | using the new `setForceNoValues()` method. |
| 142 | |
| 143 | #### Other API Changes |
| 144 | |
| 145 | 1. Creating a Cluster instance (through `Cluster#buildFrom` or the |
| 146 | `Cluster.Builder#build` method) **does not create any connection right away |
| 147 | anymore** (and thus cannot throw a `NoHostAvailableException` or an |
| 148 | `AuthenticationException`). Instead, the initial contact points are checked |
| 149 | the first time a call to `Cluster#connect` is done. If for some reason you |
| 150 | want to emulate the previous behavior, you can use the new method |
| 151 | `Cluster#init`: `Cluster.builder().build()` in 1.0 is equivalent to |
| 152 | `Cluster.builder().build().init()` in 2.0. |
| 153 | |
| 154 | 2. Methods from `Metadata`, `KeyspaceMetadata` and `TableMetadata` now use by default |
| 155 | case insensitive identifiers (for keyspace, table and column names in |
| 156 | parameter). You can double-quote an identifier if you want it to be a |
| 157 | case sensitive one (as you would do in CQL) and there is a `Metadata.quote` |
| 158 | helper method for that. |
| 159 | |
| 160 | 3. The `TableMetadata#getClusteringKey` method has been renamed |
| 161 | `TableMetadata#getClusteringColumns` to match the "official" vocabulary. |
| 162 | |
| 163 | 4. The `UnavailableException#getConsistency` method has been renamed to |
| 164 | `UnavailableException#getConsistencyLevel` for consistency with the method of |
| 165 | `QueryTimeoutException`. |
| 166 | |
| 167 | 5. The `RegularStatement` class (ex-`Statement` class, see above) must now |
| 168 | implement two additional methods: `RegularStatement#getKeyspace` and |
| 169 | `RegularStatement#getValues`. If you had extended this class, you will have to |
| 170 | implement those new methods, but both can return null if they are not useful |
| 171 | in your case. |
| 172 | |
| 173 | 6. The `Cluster.Initializer` interface should now implement 2 new methods: |
| 174 | `Cluster.Initializer#getInitialListeners` (which can return an empty |
| 175 | collection) and `Cluster.Initializer#getClusterName` (which can return null). |
| 176 | |
| 177 | 7. The `Metadata#getReplicas` method now takes 2 arguments. On top of the |
| 178 | partition key, you must now provide the keyspace too. The previous behavior |
| 179 | was buggy: it's impossible to properly return the full list of replica for a |
| 180 | partition key without knowing the keyspace since replication may depend on |
| 181 | the keyspace). |
| 182 | |
| 183 | 8. The method `LoadBalancingPolicy#newQueryPlan()` method now takes the currently |
| 184 | logged keyspace as 2nd argument. This information is necessary to do proper |
| 185 | token aware balancing (see preceding point). |
| 186 | |
| 187 | 9. The `ResultSetFuture#set` and `ResultSetFuture#setException` methods have been |
| 188 | removed (from the public API at least). They were never meant to be exposed |
| 189 | publicly: a `resultSetFuture` is always set by the driver itself and should |
| 190 | not be set manually. |
| 191 | |
| 192 | 10. The deprecated since 1.0.2 `Host.HealthMonitor` class has been removed. You |
| 193 | will now need to use `Host#isUp` and `Cluster#register` if you were using that |
| 194 | class. |
| 195 | |
| 196 | #### Features available only with Cassandra 2.0 |
| 197 | |
| 198 | This section details the biggest additions to 2.0 API wise. It is not an |
| 199 | exhaustive list of new features in 2.0. |
| 200 | |
| 201 | 1. The new `BatchStatement` class allows to group any type of insert Statements |
| 202 | (`BoundStatement` or `RegularStatement`) for execution as a batch. For instance, |
| 203 | you can do something like: |
| 204 | |
| 205 | ```java |
| 206 | List<String> values = ...; |
| 207 | PreparedStatement ps = session.prepare("INSERT INTO myTable(value) VALUES (?)"); |
| 208 | BatchStatement bs = new BatchStatement(); |
| 209 | for (String value : values) |
| 210 | bs.add(ps.bind(value)); |
| 211 | session.execute(bs); |
| 212 | ``` |
| 213 | |
| 214 | 2. `SimpleStatement` can now take a list of values in addition to the query. This |
| 215 | allows to do the equivalent of a prepare+execute but with only one round-trip |
| 216 | to the server and without keeping the prepared statement after the |
| 217 | execution. |
| 218 | |
| 219 | This is typically useful if a given query should be executed only |
| 220 | once (i.e. you don't want to prepare it) but you also don't want to |
| 221 | serialize all values into strings. Shortcut `Session#execute()` and |
| 222 | `Session#executeAsync()` methods are also provided so you that you can do: |
| 223 | |
| 224 | ```java |
| 225 | String imgName = ...; |
| 226 | ByteBuffer imgBytes = ...; |
| 227 | session.execute("INSERT INTO images(name, bytes) VALUES (?, ?)", imgName, imgBytes); |
| 228 | ``` |
| 229 | |
| 230 | 3. SELECT queries are now "paged" under the hood. In other words, if a query |
| 231 | yields a very large result, only the beginning of the `ResultSet` will be fetched |
| 232 | initially, the rest being fetched "on-demand". In practice, this means that: |
| 233 | |
| 234 | ```java |
| 235 | for (Row r : session.execute("SELECT * FROM mytable")) |
| 236 | ... process r ... |
| 237 | ``` |
| 238 | |
| 239 | should not timeout or OOM the server anymore even if "mytable" contains a lot |
| 240 | of data. In general paging should be transparent for the application (as in |
| 241 | the example above), but the implementation provides a number of knobs to |
| 242 | fine tune the behavior of that paging: |
| 243 | |
| 244 | * the size of each "page" can be set per-query (`Statement#setFetchSize()`) |
| 245 | * the `ResultSet` object provides 2 methods to check the state of paging |
| 246 | (`ResultSet#getAvailableWithoutFetching` and `ResultSet#isFullyFetched`) |
| 247 | as well as a mean to force the pre-fetching of the next page (`ResultSet#fetchMoreResults`). |