Elffuss BraidHub

kikocis/java-driver public

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