Elffuss BraidHub

kikocis/java-driver public

3.1.x · 9.8 KiBRenderedRawPermalink
1# Configuration
2
3## How Configuring the Java driver works
4
5The two basic components in the Java driver are the `Cluster` and the `Session`.
6The `Cluster` is the object to create first, and on to which all global configurations
7apply. Connecting to the `Cluster` creates a `Session`. Queries are executed
8through the `Session`.
9
10The `Cluster` object then is to be viewed as the equivalent of the `AstyanaxContext`
11object. "Starting" an `AstyanaxContext` object typically returns a `Keyspace`
12object, the `Keyspace` object is the equivalent of the *Java driver*’s `Session`.
13
14Configuring a `Cluster` works with the *Builder* pattern. The `Builder` takes all
15the configurations into account before building the `Cluster`.
16
17Following are some examples of the most important configurations that were
18possible with *Astyanax* and how to translate them into *DataStax Java driver*
19configurations. Please note that the Java driver has been optimized to handle most use
20cases at best and even though the following sections show how to tune some various
21options, the driver should provide the best performances with the default configurations
22and these options should not be changed unless there is a good reason to.
23
24## Connection pools
25
26Configuration of connection pools in *Astyanax* are made through the
27`ConnectionPoolConfigurationImpl`. This object gathers important configurations
28that the *Java driver* has categorized in multiple *Option* and *Policy* kinds.
29
30### Connections pools internals
31Everything concerning the internal pools of connections to the *Cassandra nodes*
32will be gathered in the Java driver in the [`PoolingOptions`](../../../manual/pooling):
33
34*Astyanax*:
35
36```java
37ConnectionPoolConfigurationImpl cpool =
38 new ConnectionPoolConfigurationImpl("myConnectionPool")
39 .setInitConnsPerHost(2)
40 .setMaxConnsPerHost(3)
41```
42
43*Java driver*:
44
45```java
46PoolingOptions poolingOptions =
47 new PoolingOptions()
48 .setConnectionsPerHost(HostDistance.LOCAL, 2, 3)
49```
50The first number is the initial number of connections, the second is the maximum number
51of connections the driver is allowed to create for each host.
52
53Note that the *Java driver* allows multiple simultaneous requests on one single
54connection, as it is based upon the [*Native protocol*](../../../manual/native_protocol),
55an asynchronous binary protocol that can handle up to 32768 simultaneous requests on a
56single connection. The Java driver is able to manage and distribute simultaneous requests
57by itself even under high contention, and changing the default `PoolingOptions` is not
58necessary most of the time except for very [specific use cases](../../../manual/pooling/#tuning-protocol-v3-for-very-high-throughputs).
59
60### Timeouts
61
62Timeouts concerning requests, or connections will be part of the `SocketOptions`.
63
64*Astyanax*:
65
66```java
67ConnectionPoolConfigurationImpl cpool =
68 new ConnectionPoolConfigurationImpl("myConnectionPool")
69 .setSocketTimeout(3000)
70 .setConnectTimeout(3000)
71```
72
73*Java Driver:*
74
75```java
76SocketOptions so =
77 new SocketOptions()
78 .setReadTimeoutMillis(3000)
79 .setConnectTimeoutMillis(3000);
80```
81
82Changing the client timeout options might have more impacts than expected, **please make
83sure to properly document before changing these options.**
84
85## Load Balancing
86Both *Astyanax* and the *Java driver* connect to multiple nodes of the *Cassandra*
87cluster. Distributing requests through all the nodes plays an important role in
88the good operation of the `Cluster` and for best performances. With *Astyanax*,
89requests (or “operations”) can be sent directly to replicas that have a copy of
90the data targeted by the *“Row key”* specified in the operation. Since the *Thrift* API is
91low-level, it forces the user to provide *Row keys*, known as the `TokenAware`
92connection pool type. This setting is also present in the *Java driver*, however
93the configuration is different and provides more options to tweak.
94
95Load balancing in the *Java driver* is a *Policy*, it is a class that will be
96plugged in the *Java driver*’s code and the Driver will call its methods when it
97needs to. The *Java driver* comes with a preset of specific load balancing policies.
98Here’s an equivalent code:
99
100*Astyanax*:
101
102```java
103final ConnectionPoolType poolType = ConnectionPoolType.TOKEN_AWARE;
104final NodeDiscoveryType discType = NodeDiscoveryType.RING_DESCRIBE;
105ConnectionPoolConfigurationImpl cpool =
106 new ConnectionPoolConfigurationImpl("myConnectionPool")
107 .setLocalDatacenter("myDC")
108AstyanaxConfigurationImpl aconf =
109 new AstyanaxConfigurationImpl()
110 .setConnectionPoolType(poolType)
111 .setDiscoveryType(discType)
112```
113
114*Java driver*:
115
116```java
117LoadBalancingPolicy lbp = new TokenAwarePolicy(
118 DCAwareRoundRobinPolicy.builder()
119 .withLocalDc("myDC")
120 .build()
121);
122```
123
124*By default* the *Java driver* will instantiate the exact Load balancing policy
125shown above, with the `LocalDC` being the DC of the first host the driver connects
126to. So to get the same behaviour than the *TokenAware* pool type of *Astyanax*,
127users shouldn’t need to specify a load balancing policy since the default one
128should cover it.
129
130Important: Note that since *CQL* is an abstraction of the Cassandra’s architecture, a simple
131query needs to have the *Row key* specified explicitly on a `Statement` in order
132to benefit from the *TokenAware* routing (the *Row key* in the *Java driver* is
133referenced as *Routing Key*), unlike the *Astyanax* driver.
134Some differences occur related to the different kinds of `Statements` the *Java
135driver* provides. Please see [this link](../../../manual/load_balancing/#token-aware-policy)
136for specific information.
137
138Custom load balancing policies can easily be implemented by users, and supplied to
139the *Java driver* for specific use cases. All information necessary is available
140in the [Load balaning policies docs](../../../manual/load_balancing).
141
142## Consistency levels
143Consistency levels can be set per-statement, or globally through the `QueryOptions`.
144
145*Astyanax*:
146
147```java
148AstyanaxConfigurationImpl aconf =
149 new AstyanaxConfigurationImpl()
150 .setDefaultReadConsistencyLevel(ConsistencyLevel.CL*ALL)
151 .setDefaultWriteConsistencyLevel(ConsistencyLevel.CL*ALL)
152```
153
154*Java driver*:
155
156```java
157QueryOptions qo = new QueryOptions().setConsistencyLevel(ConsistencyLevel.ALL);
158```
159
160Since the *Java driver* only executes *CQL* statements, which can be either reads
161or writes to *Cassandra*, it is not possible to globally configure the
162Consistency Level for only reads or only writes. To do so, since the Consistency
163Level can be set per-statement, you can either set it on every statement, or use
164`PreparedStatements` (if queries are to be repeated with different values): in
165this case, setting the CL on the `PreparedStatement`, causes the `BoundStatements` to
166inherit the CL from the prepared statements they were prepared from. More
167informations about how `Statement`s work in the *Java driver* are detailed
168in the [“Queries and Results” section](../queries_and_results/).
169
170
171## Authentication
172
173Authentication settings are managed by the `AuthProvider` class in the *Java driver*.
174It can be highly customizable, but also comes with default simple implementations:
175
176*Astyanax*:
177
178```java
179AuthenticationCredentials authCreds = new SimpleAuthenticationCredentials("username", "password");
180ConnectionPoolConfigurationImpl cpool =
181 new ConnectionPoolConfigurationImpl("myConnectionPool")
182 .setAuthenticationCredentials(authCreds)
183```
184
185*Java driver*:
186
187```java
188AuthProvider authProvider = new PlainTextAuthProvider("username", "password");
189```
190
191The class `AuthProvider` can be easily implemented to suit the user’s needs,
192documentation about the classes needed is [available there](../../../manual/auth/).
193
194## Hosts and ports
195
196Setting the “seeds” or first hosts to connect to can be done directly on the
197Cluster configuration Builder:
198
199*Astyanax*:
200
201```java
202ConnectionPoolConfigurationImpl cpool =
203 new ConnectionPoolConfigurationImpl("myConnectionPool")
204 .setSeeds("127.0.0.1")
205 .setPort(9160)
206```
207
208*Java driver*:
209
210```java
211Cluster cluster = Cluster.builder()
212 .addContactPoint("127.0.0.1")
213 .withPort(9042)
214```
215
216The *Java driver* by default connects to port *9042*, hence you can supply only
217host names with the `addContactPoints(String...)` method. Note that the contact
218points are only the entry points to the `Cluster` for the *Automatic discovery
219phase*.
220
221## Building the Cluster
222With all options previously presented, one may configure and create the
223`Cluster` object this way:
224
225*Java driver*:
226
227```java
228Cluster cluster = Cluster.builder()
229 .addContactPoint("127.0.0.1")
230 .withAuthProvider(authProvider)
231 .withLoadBalancingPolicy(lbp)
232 .withSocketOptions(so)
233 .withPoolingOptions(poolingOptions)
234 .withQueryOptions(qo)
235 .build();
236Session session = cluster.connect();
237```
238
239## Best Practices
240
241A few best practices are summed up in [this blog post](http://www.datastax.com/dev/blog/4-simple-rules-when-using-the-datastax-drivers-for-cassandra).
242
243Concerning connection pools, the Java driver’s default settings should allow
244most of the users to get the best out of the driver in terms of throughput,
245they have been thoroughly tested and tweaked to accommodate the users’ needs.
246If one still wishes to change those, first [Monitoring the pools](../../../manual/pooling/#monitoring-and-tuning-the-pool) is
247advised, then a [deep dive in the Pools management mechanism](../../../manual/pooling/) should
248provide enough insight.
249
250A lot more options are available in the different `XxxxOption`s classes, policies are
251also highly customizable since the base Java driver's implementations can easily be
252extended and implement user-specific actions.