Elffuss BraidHub

kikocis/java-driver public

b93a2ccc07ca · 15.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.1.2
7
82.1.2 brings important internal changes with native protocol v3 support, but
9the impact on the public API has been kept as low as possible.
10
11#### User API Changes
12
131. The native protocol version is now modelled as an enum: `ProtocolVersion`.
14 Most public methods that take it as an argument have a backward-compatible
15 version that takes an `int` (the exception being `RegularStatement`,
16 described below). For new code, prefer the enum version.
17
18#### Internal API Changes
19
201. `RegularStatement.getValues` now takes the protocol version as a
21 `ProtocolVersion` instead of an `int`. This is transparent for callers
22 since there is a backward-compatible alternative, but if you happened to
23 extend the class you'll need to update your implementation.
24
252. `BatchStatement.setSerialConsistencyLevel` now returns `BatchStatement`
26 instead of `Statement`. Again, this only matters if you extended this
27 class (if so, it might be a good idea to also have a covariant return in
28 your child class).
29
303. The constructor of `UnsupportedFeatureException` now takes a
31 `ProtocolVersion` as a parameter. This should impact few users, as there's
32 hardly any reason to build instances of that class from client code.
33
34#### New features
35
36These features are only active when the native protocol v3 is in use.
37
381. The driver now uses a single connection per host (as opposed to a pool in
39 2.1.1). Most options in `PoolingOptions` are ignored, except for a new one
40 called `maxSimultaneousRequestsPerHostThreshold`. See the class's Javadocs
41 for detailed explanations.
42
432. You can now provide a default timestamp with each query (but it will be
44 ignored if the CQL query string already contains a `USING TIMESTAMP`
45 clause). This can be done on a per-statement basis with
46 `Statement.setDefaultTimestamp`, or automatically with a
47 `TimestampGenerator` specified with
48 `Cluster.Builder.withTimestampGenerator` (two implementations are
49 provided: `ThreadLocalMonotonicTimestampGenerator` and
50 `AtomicMonotonicTimestampGenerator`). If you specify both, the statement's
51 timestamp takes precedence over the generator. By default, the driver has
52 the same behavior as 2.1.1 (no generator, timestamps are assigned by
53 Cassandra unless `USING TIMESTAMP` was specified).
54
553. `BatchStatement.setSerialConsistencyLevel` no longer throws an exception,
56 it will honor the serial consistency level for the batch.
57
58
59### 2.1.1
60
61#### Internal API Changes
62
631. The `ResultSet` interface has a new `wasApplied()` method. This will
64 only affect clients that provide their own implementation of this interface.
65
66
67### 2.1.0
68
69#### User API Changes
70
711. The `getCaching` method of `TableMetadata#Options` now returns a
72 `Map` to account for changes to Cassandra 2.1. Also, the
73 `getIndexInterval` method now returns an `Integer` instead of an `int`
74 which will be `null` when connected to Cassandra 2.1 nodes.
75
762. `BoundStatement` variables that have not been set explicitly will no
77 longer default to `null`. Instead, all variables must be bound explicitly,
78 otherwise the execution of the statement will fail (this also applies to
79 statements inside of a `BatchStatement`). For variables that map to a
80 primitive Java type, a new `setToNull` method has been added.
81 We made this change because the driver might soon distinguish between unset
82 and null variables, so we don't want clients relying on the "leave unset to
83 set to `null`" behavior.
84
85
86#### Internal API Changes
87
88The changes listed in this section should normally not impact end users of the
89driver, but rather third-party frameworks and tools.
90
911. The `serialize` and `deserialize` methods in `DataType` now take an
92 additional parameter: the protocol version. As explained in the javadoc,
93 if unsure, the proper value to use for this parameter is the protocol version
94 in use by the driver, i.e. the value returned by
95 `cluster.getConfiguration().getProtocolOptions().getProtocolVersion()`.
96
972. The `parse` method in `DataType` now returns a Java object, not a
98 `ByteBuffer`. The previous behavior can be obtained by calling the
99 `serialize` method on the returned object.
100
1013. The `getValues` method of `RegularStatement` now takes the protocol
102 version as a parameter. As above, the proper value if unsure is almost surely
103 the protocol version in use
104 (`cluster.getConfiguration().getProtocolOptions().getProtocolVersion()`).
105
106
107### 2.0.x to 2.0.10
108
109We try to avoid breaking changes within a branch (2.0.x to 2.0.y), but
1102.0.10 saw a lot of new features and internal improvements. There is one
111breaking change:
112
1131. `LatencyTracker#update` now has a different signature and takes two new
114 parameters: the statement that has been executed (never null), and the exception
115 thrown while executing the query (or null, if the query executed successfully).
116 Existing implementations of this interface, once upgraded to the new method
117 signature, should continue to work as before.
118
119The following might also be of interest:
120
1212. `SocketOptions#getTcpNoDelay()` is now TRUE by default (it was previously undefined).
122 This reflects the new behavior of Netty (which was upgraded from version 3.9.0 to
123 4.0.27): `TCP_NODELAY` is now turned on by default, instead of depending on the OS
124 default like in previous versions.
125
1263. Netty is not shaded anymore in the default Maven artifact. However we publish a
127 [shaded artifact](../features/shaded_jar/) under a different classifier.
128
1294. The internal initialization sequence of the Cluster object has been slightly changed:
130 some fields that were previously initialized in the constructor are now set when
131 the `init()` method is called. This is unlikely to affect regular driver users.
132
133### 1.0 to 2.0
134
135We used the opportunity of a major version bump to incorporate your feedback
136and improve the API, to fix a number of inconsistencies and remove cruft.
137Unfortunately this means there are some breaking changes, but the new API should
138be both simpler and more complete.
139
140The following describes the changes for 2.0 that are breaking changes of the
1411.0 API. For ease of use, we distinguish two categories of API changes: the "main"
142ones and the "other" ones.
143
144The "main" API changes are the ones that are either
145likely to affect most upgraded apps or are incompatible changes that, even if minor,
146will not be detected at compile time. Upgraders are highly encouraged to check
147this list of "main" changes while upgrading their application to 2.0 (even
148though most applications are likely to be affected by only a handful of
149changes).
150
151The "other" list is, well, other changes: those that are likely to
152affect a minor number of applications and will be detected by compile time
153errors anyway. It is ok to skip those initially and only come back to them if
154you have trouble compiling your application after an upgrade.
155
156#### Main API changes
157
1581. The `Query` class has been renamed into `Statement` (it was confusing
159 to some that the `BoundStatement` was not a `Statement`). To allow
160 this, the old `Statement` class has been renamed to `RegularStatement`.
161
1622. The `Cluster` and `Session` shutdown API has changed. There is now a
163 `closeAsync` that is asynchronous but returns a `Future` on the
164 completion of the shutdown process. There is also a `close` shortcut
165 that does the same but blocks. Also, `close` now waits for ongoing
166 queries to complete by default (but you can force the closing of all
167 connections if you want to).
168
1693. `NoHostAvailableException#getErrors` now returns the full exception objects for
170 each node instead of just a message. In other words, it returns a
171 `Map<InetAddress, Throwable>` instead of a `Map<InetAddress, String>`.
172
1734. `Statement#getConsistencyLevel` (previously `Query#getConsistencyLevel`, see
174 first point) will now return `null` by default (instead of `CL.ONE`), with the
175 meaning of "use the default consistency level".
176 The default consistency level can now be configured through the new `QueryOptions`
177 object in the cluster `Configuration`.
178
1795. The `Metrics` class now uses the Codahale metrics library version 3 (version 2 was
180 used previously). This new major version of the library has many API changes
181 compared to its version 2 (see the [release notes](https://dropwizard.github.io/metrics/3.1.0/about/release-notes/) for details),
182 which can thus impact consumers of the Metrics class.
183 Furthermore, the default `JmxReporter` now includes a name specific to the
184 cluster instance (to avoid conflicts when multiple Cluster instances are created
185 in the same JVM). As a result, tools that were polling JMX info will
186 have to be updated accordingly.
187
1886. The `QueryBuilder#in` method now has the following special case: using
189 `QueryBuilder.in(QueryBuilder.bindMarker())` will generate the string `IN ?`,
190 not `IN (?)` as was the case in 1.0. The reasoning being that the former
191 syntax, made valid by [CASSANDRA-4210](https://issues.apache.org/jira/browse/CASSANDRA-4210)
192 is a lot more useful than `IN (?)`, as the latter can more simply use an
193 equality.
194 Note that if you really want to output `IN (?)` with the query
195 builder, you can use `QueryBuilder.in(QueryBuilder.raw("?"))`.
196
1977. When binding values by name in `BoundStatement` (i.e. using the
198 `setX(String, X)` methods), if more than one variable have the same name,
199 then all values corresponding to that variable
200 name are set instead of just the first occurrence.
201
2028. The `QueryBuilder#raw` method does not automatically add quotes anymore, but
203 rather output its result without any change (as the raw name implies).
204 This means for instance that `eq("x", raw(foo))` will output `x = foo`,
205 not `x = 'foo'` (you don't need the raw method to output the latter string).
206
207
2089. The `QueryBuilder` will now sometimes use the new ability to send value as
209 bytes instead of serializing everything to string. In general the QueryBuilder
210 will do the right thing, but if you were calling the `getQueryString()` method
211 on a Statement created with a QueryBuilder (for other reasons than to prepare a query)
212 then the returned string may contain bind markers in place of some of the values
213 provided (and in that case, `getValues()` will contain the values corresponding
214 to those markers). If need be, it is possible to force the old behavior by
215 using the new `setForceNoValues()` method.
216
217#### Other API Changes
218
2191. Creating a Cluster instance (through `Cluster#buildFrom` or the
220 `Cluster.Builder#build` method) **does not create any connection right away
221 anymore** (and thus cannot throw a `NoHostAvailableException` or an
222 `AuthenticationException`). Instead, the initial contact points are checked
223 the first time a call to `Cluster#connect` is done. If for some reason you
224 want to emulate the previous behavior, you can use the new method
225 `Cluster#init`: `Cluster.builder().build()` in 1.0 is equivalent to
226 `Cluster.builder().build().init()` in 2.0.
227
2282. Methods from `Metadata`, `KeyspaceMetadata` and `TableMetadata` now use by default
229 case insensitive identifiers (for keyspace, table and column names in
230 parameter). You can double-quote an identifier if you want it to be a
231 case sensitive one (as you would do in CQL) and there is a `Metadata.quote`
232 helper method for that.
233
2343. The `TableMetadata#getClusteringKey` method has been renamed
235 `TableMetadata#getClusteringColumns` to match the "official" vocabulary.
236
2374. The `UnavailableException#getConsistency` method has been renamed to
238 `UnavailableException#getConsistencyLevel` for consistency with the method of
239 `QueryTimeoutException`.
240
2415. The `RegularStatement` class (ex-`Statement` class, see above) must now
242 implement two additional methods: `RegularStatement#getKeyspace` and
243 `RegularStatement#getValues`. If you had extended this class, you will have to
244 implement those new methods, but both can return null if they are not useful
245 in your case.
246
2476. The `Cluster.Initializer` interface should now implement 2 new methods:
248 `Cluster.Initializer#getInitialListeners` (which can return an empty
249 collection) and `Cluster.Initializer#getClusterName` (which can return null).
250
2517. The `Metadata#getReplicas` method now takes 2 arguments. On top of the
252 partition key, you must now provide the keyspace too. The previous behavior
253 was buggy: it's impossible to properly return the full list of replica for a
254 partition key without knowing the keyspace since replication may depend on
255 the keyspace).
256
2578. The method `LoadBalancingPolicy#newQueryPlan()` method now takes the currently
258 logged keyspace as 2nd argument. This information is necessary to do proper
259 token aware balancing (see preceding point).
260
2619. The `ResultSetFuture#set` and `ResultSetFuture#setException` methods have been
262 removed (from the public API at least). They were never meant to be exposed
263 publicly: a `resultSetFuture` is always set by the driver itself and should
264 not be set manually.
265
26610. The deprecated since 1.0.2 `Host.HealthMonitor` class has been removed. You
267 will now need to use `Host#isUp` and `Cluster#register` if you were using that
268 class.
269
270#### Features available only with Cassandra 2.0
271
272This section details the biggest additions to 2.0 API wise. It is not an
273exhaustive list of new features in 2.0.
274
2751. The new `BatchStatement` class allows to group any type of insert Statements
276 (`BoundStatement` or `RegularStatement`) for execution as a batch. For instance,
277 you can do something like:
278
279 ```java
280 List<String> values = ...;
281 PreparedStatement ps = session.prepare("INSERT INTO myTable(value) VALUES (?)");
282 BatchStatement bs = new BatchStatement();
283 for (String value : values)
284 bs.add(ps.bind(value));
285 session.execute(bs);
286 ```
287
2882. `SimpleStatement` can now take a list of values in addition to the query. This
289 allows to do the equivalent of a prepare+execute but with only one round-trip
290 to the server and without keeping the prepared statement after the
291 execution.
292
293 This is typically useful if a given query should be executed only
294 once (i.e. you don't want to prepare it) but you also don't want to
295 serialize all values into strings. Shortcut `Session#execute()` and
296 `Session#executeAsync()` methods are also provided so you that you can do:
297
298 ```java
299 String imgName = ...;
300 ByteBuffer imgBytes = ...;
301 session.execute("INSERT INTO images(name, bytes) VALUES (?, ?)", imgName, imgBytes);
302 ```
303
3043. SELECT queries are now "paged" under the hood. In other words, if a query
305 yields a very large result, only the beginning of the `ResultSet` will be fetch
306 initially, the rest being fetch "on-demand". In practice, this means that:
307
308 ```java
309 for (Row r : session.execute("SELECT * FROM mytable"))
310 ... process r ...
311 ```
312
313 should not timeout or OOM the server anymore even if "mytable" contains a lot
314 of data. In general paging should be transparent for the application (as in
315 the example above), but the implementation provides a number of knobs to
316 fine tune the behavior of that paging:
317
318 * the size of each "page" can be set per-query (`Statement#setFetchSize()`)
319 * the `ResultSet` object provides 2 methods to check the state of paging
320 (`ResultSet#getAvailableWithoutFetching` and `ResultSet#isFullyFetched`)
321 as well as a mean to force the pre-fetching of the next page (`ResultSet#fetchMoreResults`).