Elffuss BraidHub

kikocis/java-driver public

37f021e9afe6 · 20.3 KiBRenderedRawPermalink
1## Upgrade guide
2
3The purpose of this guide is to detail changes made by successive
4versions of the Java driver.
5
6### 2.2.0
7
8This version brings parity with Cassandra 2.2. We've also seized the
9opportunity to clean up the API.
10
111. All methods that took the protocol version as an `int` or assumed a
12 default version have been removed (they were already deprecated in
13 2.1):
14 * `AbstractGettableData(int)`
15 * `Cluster.Builder#withProtocolVersion(int)`
16 * in `DataType`:
17 * `serialize(Object, int)`
18 * `serialize(Object)`
19 * `deserialize(ByteBuffer)`
20 * `serializeValue(Object, int)`
21 * `serializeValue(Object)`
22 * in `ProtocolOptions`:
23 * `NEWEST_SUPPORTED_PROTOCOL_VERSION` (replaced by
24 `ProtocolVersion#NEWEST_SUPPORTED`)
25 * `int getProtocolVersion()`
26 * in `RegularStatement`:
27 * `getValues(int)`
28 * `getValues()`
29
30 There are now variants of these methods using the `ProtocolVersion`
31 enum. In addition, `ProtocolOptions#getProtocolVersionEnum` has been
32 renamed to `ProtocolOptions#getProtocolVersion`.
33
342. All methods related to the "suspected" host state have been removed
35 (they had been deprecated in 2.1.6 when the suspicion mechanism was
36 removed):
37 * `Host.StateListener#onSuspected()` (was inherited by
38 `LoadBalancingPolicy`)
39 * `Host#getInitialReconnectionAttemptFuture()`
40
413. `PoolingOptions#setMinSimultaneousRequestsPerConnectionThreshold(HostDistance,
42 int)` has been removed. The new connection pool resizing algorithm introduced by
43 [JAVA-419](https://datastax-oss.atlassian.net/browse/JAVA-419) does not need this
44 threshold anymore.
45
464. `AddressTranslater` has been renamed to `AddressTranslator`. All
47 related methods and classes have also been renamed.
48
49 In addition, the `close()` method has been pulled up into
50 `AddressTranslator`, and `CloseableAddressTranslator` has been removed.
51 Existing third-party `AddressTranslator` implementations only need
52 to add an empty `close()` method.
53
545. The `close()` method has been pulled up into `LoadBalancingPolicy`,
55 and `CloseableLoadBalancingPolicy` has been removed. Existing third-party
56 `LoadBalancingPolicy` implementations only need to add an empty
57 `close()` method.
58
596. All pluggable components now have callbacks to detect when they get
60 associated with a `Cluster` instance:
61 * `ReconnectionPolicy`, `RetryPolicy`, `AddressTranslator`,
62 and `TimestampGenerator`:
63 * `init(Cluster)`
64 * `close()`
65 * `Host.StateListener` and `LatencyTracker`:
66 * `onRegister(Cluster)`
67 * `onUnregister(Cluster)`
68
69 This gives these components the opportunity to perform
70 initialization / cleanup tasks. Existing third-party implementations
71 only need to add empty methods.
72
737. `LoadBalancingPolicy` does not extend `Host.StateListener` anymore:
74 callback methods (`onUp`, `onDown`, etc.) have been duplicated. This
75 is unlikely to affect clients.
76
778. [Client-side timestamp generation](../features/query_timestamps/) is
78 now the default (provided that [native
79 protocol](../features/native_protocol) v3 or higher is in use). The
80 generator used is `AtomicMonotonicTimestampGenerator`.
81
829. If a DNS name resolves to multiple A-records,
83 `Cluster.Builder#addContactPoint(String)` will now use all of these
84 addresses as contact points. This gives you the possibility of
85 maintaining contact points in DNS configuration, and having a single,
86 static contact point in your Java code.
87
8810. The following methods were added for [Custom payloads](../features/custom_payloads):
89 * in `PreparedStatement`: `getIncomingPayload()`,
90 `getOutgoingPayload()` and
91 `setOutgoingPayload(Map<String,ByteBuffer>)`
92 * `AbstractSession#prepareAsync(String, Map<String,ByteBuffer>)`
93
94 Also, not that `AbstractSession#prepareAsync(Statement)` does not
95 call `AbstractSession#prepareAsync(String)` anymore, they now both
96 delegate to a private method.
97
98 This breaks binary compatibility for these two classes; if you have
99 custom implementations, you will have to adapt them accordingly.
100
10111. Getters and setters have been added to "data-container" classes for
102 new CQL types:
103 * `getByte`/`setByte` for the `TINYINT` type
104 * `getShort`/`setShort` for the `SMALLINT` type
105 * `getTime`/`setTime` for the `TIME` type
106 * `getDate`/`setDate` for the `DATE` type
107
108 The methods for the `TIMESTAMP` CQL type have been renamed to
109 `getTimestamp` and `setTimestamp`.
110
111 This affects `Row`, `BoundStatement`, `TupleValue` and `UDTValue`.
112
11312. New exception types have been added to handle additional server-side
114 errors introduced in Cassandra 2.2:
115 * `ReadFailureException`
116 * `WriteFailureException`
117 * `FunctionExecutionException`
118
119 This is not a breaking change since all driver exceptions are
120 unchecked; but clients might decide to handle these errors in a specific
121 way.
122
123 In addition, `QueryTimeoutException` has been renamed to
124 `QueryExecutionException` (this is an intermediary class in our
125 exception hierarchy, it now has new child classes that are not
126 related to timeouts).
127
128### 2.1.2
129
1302.1.2 brings important internal changes with native protocol v3 support, but
131the impact on the public API has been kept as low as possible.
132
133#### User API Changes
134
1351. The native protocol version is now modelled as an enum: `ProtocolVersion`.
136 Most public methods that take it as an argument have a backward-compatible
137 version that takes an `int` (the exception being `RegularStatement`,
138 described below). For new code, prefer the enum version.
139
140#### Internal API Changes
141
1421. `RegularStatement.getValues` now takes the protocol version as a
143 `ProtocolVersion` instead of an `int`. This is transparent for callers
144 since there is a backward-compatible alternative, but if you happened to
145 extend the class you'll need to update your implementation.
146
1472. `BatchStatement.setSerialConsistencyLevel` now returns `BatchStatement`
148 instead of `Statement`. Again, this only matters if you extended this
149 class (if so, it might be a good idea to also have a covariant return in
150 your child class).
151
1523. The constructor of `UnsupportedFeatureException` now takes a
153 `ProtocolVersion` as a parameter. This should impact few users, as there's
154 hardly any reason to build instances of that class from client code.
155
156#### New features
157
158These features are only active when the native protocol v3 is in use.
159
1601. The driver now uses a single connection per host (as opposed to a pool in
161 2.1.1). Most options in `PoolingOptions` are ignored, except for a new one
162 called `maxSimultaneousRequestsPerHostThreshold`. See the class's Javadocs
163 for detailed explanations.
164
1652. You can now provide a default timestamp with each query (but it will be
166 ignored if the CQL query string already contains a `USING TIMESTAMP`
167 clause). This can be done on a per-statement basis with
168 `Statement.setDefaultTimestamp`, or automatically with a
169 `TimestampGenerator` specified with
170 `Cluster.Builder.withTimestampGenerator` (two implementations are
171 provided: `ThreadLocalMonotonicTimestampGenerator` and
172 `AtomicMonotonicTimestampGenerator`). If you specify both, the statement's
173 timestamp takes precedence over the generator. By default, the driver has
174 the same behavior as 2.1.1 (no generator, timestamps are assigned by
175 Cassandra unless `USING TIMESTAMP` was specified).
176
1773. `BatchStatement.setSerialConsistencyLevel` no longer throws an exception,
178 it will honor the serial consistency level for the batch.
179
180
181### 2.1.1
182
183#### Internal API Changes
184
1851. The `ResultSet` interface has a new `wasApplied()` method. This will
186 only affect clients that provide their own implementation of this interface.
187
188
189### 2.1.0
190
191#### User API Changes
192
1931. The `getCaching` method of `TableMetadata#Options` now returns a
194 `Map` to account for changes to Cassandra 2.1. Also, the
195 `getIndexInterval` method now returns an `Integer` instead of an `int`
196 which will be `null` when connected to Cassandra 2.1 nodes.
197
1982. `BoundStatement` variables that have not been set explicitly will no
199 longer default to `null`. Instead, all variables must be bound explicitly,
200 otherwise the execution of the statement will fail (this also applies to
201 statements inside of a `BatchStatement`). For variables that map to a
202 primitive Java type, a new `setToNull` method has been added.
203 We made this change because the driver might soon distinguish between unset
204 and null variables, so we don't want clients relying on the "leave unset to
205 set to `null`" behavior.
206
207
208#### Internal API Changes
209
210The changes listed in this section should normally not impact end users of the
211driver, but rather third-party frameworks and tools.
212
2131. The `serialize` and `deserialize` methods in `DataType` now take an
214 additional parameter: the protocol version. As explained in the javadoc,
215 if unsure, the proper value to use for this parameter is the protocol version
216 in use by the driver, i.e. the value returned by
217 `cluster.getConfiguration().getProtocolOptions().getProtocolVersion()`.
218
2192. The `parse` method in `DataType` now returns a Java object, not a
220 `ByteBuffer`. The previous behavior can be obtained by calling the
221 `serialize` method on the returned object.
222
2233. The `getValues` method of `RegularStatement` now takes the protocol
224 version as a parameter. As above, the proper value if unsure is almost surely
225 the protocol version in use
226 (`cluster.getConfiguration().getProtocolOptions().getProtocolVersion()`).
227
228
229### 2.0.x to 2.0.10
230
231We try to avoid breaking changes within a branch (2.0.x to 2.0.y), but
2322.0.10 saw a lot of new features and internal improvements. There is one
233breaking change:
234
2351. `LatencyTracker#update` now has a different signature and takes two new
236 parameters: the statement that has been executed (never null), and the exception
237 thrown while executing the query (or null, if the query executed successfully).
238 Existing implementations of this interface, once upgraded to the new method
239 signature, should continue to work as before.
240
241The following might also be of interest:
242
2432. `SocketOptions#getTcpNoDelay()` is now TRUE by default (it was previously undefined).
244 This reflects the new behavior of Netty (which was upgraded from version 3.9.0 to
245 4.0.27): `TCP_NODELAY` is now turned on by default, instead of depending on the OS
246 default like in previous versions.
247
2483. Netty is not shaded anymore in the default Maven artifact. However we publish a
249 [shaded artifact](../features/shaded_jar/) under a different classifier.
250
2514. The internal initialization sequence of the Cluster object has been slightly changed:
252 some fields that were previously initialized in the constructor are now set when
253 the `init()` method is called. This is unlikely to affect regular driver users.
254
255### 1.0 to 2.0
256
257We used the opportunity of a major version bump to incorporate your feedback
258and improve the API, to fix a number of inconsistencies and remove cruft.
259Unfortunately this means there are some breaking changes, but the new API should
260be both simpler and more complete.
261
262The following describes the changes for 2.0 that are breaking changes of the
2631.0 API. For ease of use, we distinguish two categories of API changes: the "main"
264ones and the "other" ones.
265
266The "main" API changes are the ones that are either
267likely to affect most upgraded apps or are incompatible changes that, even if minor,
268will not be detected at compile time. Upgraders are highly encouraged to check
269this list of "main" changes while upgrading their application to 2.0 (even
270though most applications are likely to be affected by only a handful of
271changes).
272
273The "other" list is, well, other changes: those that are likely to
274affect a minor number of applications and will be detected by compile time
275errors anyway. It is ok to skip those initially and only come back to them if
276you have trouble compiling your application after an upgrade.
277
278#### Main API changes
279
2801. The `Query` class has been renamed into `Statement` (it was confusing
281 to some that the `BoundStatement` was not a `Statement`). To allow
282 this, the old `Statement` class has been renamed to `RegularStatement`.
283
2842. The `Cluster` and `Session` shutdown API has changed. There is now a
285 `closeAsync` that is asynchronous but returns a `Future` on the
286 completion of the shutdown process. There is also a `close` shortcut
287 that does the same but blocks. Also, `close` now waits for ongoing
288 queries to complete by default (but you can force the closing of all
289 connections if you want to).
290
2913. `NoHostAvailableException#getErrors` now returns the full exception objects for
292 each node instead of just a message. In other words, it returns a
293 `Map<InetAddress, Throwable>` instead of a `Map<InetAddress, String>`.
294
2954. `Statement#getConsistencyLevel` (previously `Query#getConsistencyLevel`, see
296 first point) will now return `null` by default (instead of `CL.ONE`), with the
297 meaning of "use the default consistency level".
298 The default consistency level can now be configured through the new `QueryOptions`
299 object in the cluster `Configuration`.
300
3015. The `Metrics` class now uses the Codahale metrics library version 3 (version 2 was
302 used previously). This new major version of the library has many API changes
303 compared to its version 2 (see the [release notes](https://dropwizard.github.io/metrics/3.1.0/about/release-notes/) for details),
304 which can thus impact consumers of the Metrics class.
305 Furthermore, the default `JmxReporter` now includes a name specific to the
306 cluster instance (to avoid conflicts when multiple Cluster instances are created
307 in the same JVM). As a result, tools that were polling JMX info will
308 have to be updated accordingly.
309
3106. The `QueryBuilder#in` method now has the following special case: using
311 `QueryBuilder.in(QueryBuilder.bindMarker())` will generate the string `IN ?`,
312 not `IN (?)` as was the case in 1.0. The reasoning being that the former
313 syntax, made valid by [CASSANDRA-4210](https://issues.apache.org/jira/browse/CASSANDRA-4210)
314 is a lot more useful than `IN (?)`, as the latter can more simply use an
315 equality.
316 Note that if you really want to output `IN (?)` with the query
317 builder, you can use `QueryBuilder.in(QueryBuilder.raw("?"))`.
318
3197. When binding values by name in `BoundStatement` (i.e. using the
320 `setX(String, X)` methods), if more than one variable have the same name,
321 then all values corresponding to that variable
322 name are set instead of just the first occurrence.
323
3248. The `QueryBuilder#raw` method does not automatically add quotes anymore, but
325 rather output its result without any change (as the raw name implies).
326 This means for instance that `eq("x", raw(foo))` will output `x = foo`,
327 not `x = 'foo'` (you don't need the raw method to output the latter string).
328
329
3309. The `QueryBuilder` will now sometimes use the new ability to send value as
331 bytes instead of serializing everything to string. In general the QueryBuilder
332 will do the right thing, but if you were calling the `getQueryString()` method
333 on a Statement created with a QueryBuilder (for other reasons than to prepare a query)
334 then the returned string may contain bind markers in place of some of the values
335 provided (and in that case, `getValues()` will contain the values corresponding
336 to those markers). If need be, it is possible to force the old behavior by
337 using the new `setForceNoValues()` method.
338
339#### Other API Changes
340
3411. Creating a Cluster instance (through `Cluster#buildFrom` or the
342 `Cluster.Builder#build` method) **does not create any connection right away
343 anymore** (and thus cannot throw a `NoHostAvailableException` or an
344 `AuthenticationException`). Instead, the initial contact points are checked
345 the first time a call to `Cluster#connect` is done. If for some reason you
346 want to emulate the previous behavior, you can use the new method
347 `Cluster#init`: `Cluster.builder().build()` in 1.0 is equivalent to
348 `Cluster.builder().build().init()` in 2.0.
349
3502. Methods from `Metadata`, `KeyspaceMetadata` and `TableMetadata` now use by default
351 case insensitive identifiers (for keyspace, table and column names in
352 parameter). You can double-quote an identifier if you want it to be a
353 case sensitive one (as you would do in CQL) and there is a `Metadata.quote`
354 helper method for that.
355
3563. The `TableMetadata#getClusteringKey` method has been renamed
357 `TableMetadata#getClusteringColumns` to match the "official" vocabulary.
358
3594. The `UnavailableException#getConsistency` method has been renamed to
360 `UnavailableException#getConsistencyLevel` for consistency with the method of
361 `QueryTimeoutException`.
362
3635. The `RegularStatement` class (ex-`Statement` class, see above) must now
364 implement two additional methods: `RegularStatement#getKeyspace` and
365 `RegularStatement#getValues`. If you had extended this class, you will have to
366 implement those new methods, but both can return null if they are not useful
367 in your case.
368
3696. The `Cluster.Initializer` interface should now implement 2 new methods:
370 `Cluster.Initializer#getInitialListeners` (which can return an empty
371 collection) and `Cluster.Initializer#getClusterName` (which can return null).
372
3737. The `Metadata#getReplicas` method now takes 2 arguments. On top of the
374 partition key, you must now provide the keyspace too. The previous behavior
375 was buggy: it's impossible to properly return the full list of replica for a
376 partition key without knowing the keyspace since replication may depend on
377 the keyspace).
378
3798. The method `LoadBalancingPolicy#newQueryPlan()` method now takes the currently
380 logged keyspace as 2nd argument. This information is necessary to do proper
381 token aware balancing (see preceding point).
382
3839. The `ResultSetFuture#set` and `ResultSetFuture#setException` methods have been
384 removed (from the public API at least). They were never meant to be exposed
385 publicly: a `resultSetFuture` is always set by the driver itself and should
386 not be set manually.
387
38810. The deprecated since 1.0.2 `Host.HealthMonitor` class has been removed. You
389 will now need to use `Host#isUp` and `Cluster#register` if you were using that
390 class.
391
392#### Features available only with Cassandra 2.0
393
394This section details the biggest additions to 2.0 API wise. It is not an
395exhaustive list of new features in 2.0.
396
3971. The new `BatchStatement` class allows to group any type of insert Statements
398 (`BoundStatement` or `RegularStatement`) for execution as a batch. For instance,
399 you can do something like:
400
401 ```java
402 List<String> values = ...;
403 PreparedStatement ps = session.prepare("INSERT INTO myTable(value) VALUES (?)");
404 BatchStatement bs = new BatchStatement();
405 for (String value : values)
406 bs.add(ps.bind(value));
407 session.execute(bs);
408 ```
409
4102. `SimpleStatement` can now take a list of values in addition to the query. This
411 allows to do the equivalent of a prepare+execute but with only one round-trip
412 to the server and without keeping the prepared statement after the
413 execution.
414
415 This is typically useful if a given query should be executed only
416 once (i.e. you don't want to prepare it) but you also don't want to
417 serialize all values into strings. Shortcut `Session#execute()` and
418 `Session#executeAsync()` methods are also provided so you that you can do:
419
420 ```java
421 String imgName = ...;
422 ByteBuffer imgBytes = ...;
423 session.execute("INSERT INTO images(name, bytes) VALUES (?, ?)", imgName, imgBytes);
424 ```
425
4263. SELECT queries are now "paged" under the hood. In other words, if a query
427 yields a very large result, only the beginning of the `ResultSet` will be fetch
428 initially, the rest being fetch "on-demand". In practice, this means that:
429
430 ```java
431 for (Row r : session.execute("SELECT * FROM mytable"))
432 ... process r ...
433 ```
434
435 should not timeout or OOM the server anymore even if "mytable" contains a lot
436 of data. In general paging should be transparent for the application (as in
437 the example above), but the implementation provides a number of knobs to
438 fine tune the behavior of that paging:
439 * the size of each "page" can be set per-query (`Statement#setFetchSize()`)
440 * the `ResultSet` object provides 2 methods to check the state of paging
441 (`ResultSet#getAvailableWithoutFetching` and
442 `ResultSet#isFullyFetched`) as well as a mean to force the pre-fetching
443 of the next page (`ResultSet#fetchMoreResults`).