kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 296c787d932c0ba04eacf731d34aa5c593be4b6bc4f4a4cab2d1f559678eec35 |
|---|---|
| Author | Kevin Gallardo <kevin.gallardo@datastax.com> · 2015-07-08 11:47 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-08-09 20:19 +02:00 |
| Parents | d4015c5defee |
4 files changed
modified changelog/README.md +1 −0
| @@ -31,6 +31,7 @@ | ||
| 31 | 31 | - [improvement] Allow PlainTextAuthProvider to change its credentials at runtime (JAVA-719) |
| 32 | 32 | - [new feature] Make it possible to register for SchemaChange Events (JAVA-151) |
| 33 | 33 | - [improvement] Downgrade "Asked to rebuild table" log from ERROR to INFO level (JAVA-861) |
| 34 | +- [improvement] Provide an option to prepare statements only on one node (JAVA-797) | |
| 34 | 35 | |
| 35 | 36 | Merged from 2.0.10_fixes branch: |
| 36 | 37 |
modified driver-core/src/main/java/com/datastax/driver/core/QueryOptions.java +42 −0
| @@ -47,6 +47,7 @@ | ||
| 47 | 47 | private volatile int fetchSize = DEFAULT_FETCH_SIZE; |
| 48 | 48 | private volatile boolean defaultIdempotence = DEFAULT_IDEMPOTENCE; |
| 49 | 49 | private volatile Cluster.Manager manager; |
| 50 | + private volatile boolean prepareOnAllHosts = true; | |
| 50 | 51 | |
| 51 | 52 | /** |
| 52 | 53 | * Creates a new {@link QueryOptions} instance using the {@link #DEFAULT_CONSISTENCY_LEVEL}, |
| @@ -169,4 +170,45 @@ | ||
| 169 | 170 | public boolean getDefaultIdempotence() { |
| 170 | 171 | return defaultIdempotence; |
| 171 | 172 | } |
| 173 | + | |
| 174 | + /** | |
| 175 | + * Set whether the driver should prepare statements on all hosts in the cluster. | |
| 176 | + * <p> | |
| 177 | + * A statement is normally prepared in two steps: | |
| 178 | + * <ol> | |
| 179 | + * <li>prepare the query on a single host in the cluster;</li> | |
| 180 | + * <li>if that succeeds, prepare on all other hosts.</li> | |
| 181 | + * </ol> | |
| 182 | + * This option controls whether step 2 is executed. It is enabled by default. | |
| 183 | + * <p> | |
| 184 | + * The reason why you might want to disable it is to optimize network usage if you | |
| 185 | + * have a large number of clients preparing the same set of statements at startup. | |
| 186 | + * If your load balancing policy distributes queries randomly, each client will pick | |
| 187 | + * a different host to prepare its statements, and on the whole each host has a good | |
| 188 | + * chance of having been hit by at least one client for each statement. | |
| 189 | + * <p> | |
| 190 | + * On the other hand, if that assumption turns out to be wrong and one host hasn't | |
| 191 | + * prepared a given statement, you will pay a penalty on the first execution of this | |
| 192 | + * statement on this host (one extra roundtrip to resend the query to prepare, and | |
| 193 | + * another to retry the execution). | |
| 194 | + * | |
| 195 | + * @param prepareOnAllHosts the new value to set to indicate whether to prepare | |
| 196 | + * statements once or on all nodes. | |
| 197 | + * @return this {@code QueryOptions} instance. | |
| 198 | + */ | |
| 199 | + public QueryOptions setPrepareOnAllHosts(boolean prepareOnAllHosts) { | |
| 200 | + this.prepareOnAllHosts = prepareOnAllHosts; | |
| 201 | + return this; | |
| 202 | + } | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * Returns whether the driver should prepare statements on all hosts in the cluster. | |
| 206 | + * | |
| 207 | + * @return the value. | |
| 208 | + * | |
| 209 | + * @see #setPrepareOnAllHosts(boolean) | |
| 210 | + */ | |
| 211 | + public boolean isPrepareOnAllHosts() { | |
| 212 | + return this.prepareOnAllHosts; | |
| 213 | + } | |
| 172 | 214 | } |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +11 −9
| @@ -145,15 +145,17 @@ | ||
| 145 | 145 | Responses.Result.Prepared pmsg = (Responses.Result.Prepared)rm; |
| 146 | 146 | PreparedStatement stmt = DefaultPreparedStatement.fromMessage(pmsg, cluster.getMetadata(), query, poolsState.keyspace); |
| 147 | 147 | stmt = cluster.manager.addPrepared(stmt); |
| 148 | - try { | |
| 149 | - // All Sessions are connected to the same nodes so it's enough to prepare only the nodes of this session. | |
| 150 | - // If that changes, we'll have to make sure this propagate to other sessions too. | |
| 151 | - prepare(stmt.getQueryString(), future.getAddress()); | |
| 152 | - } catch (InterruptedException e) { | |
| 153 | - Thread.currentThread().interrupt(); | |
| 154 | - // This method doesn't propagate interruption, at least not for now. However, if we've | |
| 155 | - // interrupted preparing queries on other node it's not a problem as we'll re-prepare | |
| 156 | - // later if need be. So just ignore. | |
| 148 | + if (cluster.getConfiguration().getQueryOptions().isPrepareOnAllHosts()){ | |
| 149 | + try { | |
| 150 | + // All Sessions are connected to the same nodes so it's enough to prepare only the nodes of this session. | |
| 151 | + // If that changes, we'll have to make sure this propagate to other sessions too. | |
| 152 | + prepare(stmt.getQueryString(), future.getAddress()); | |
| 153 | + } catch (InterruptedException e) { | |
| 154 | + Thread.currentThread().interrupt(); | |
| 155 | + // This method doesn't propagate interruption, at least not for now. However, if we've | |
| 156 | + // interrupted preparing queries on other node it's not a problem as we'll re-prepare | |
| 157 | + // later if need be. So just ignore. | |
| 158 | + } | |
| 157 | 159 | } |
| 158 | 160 | return stmt; |
| 159 | 161 | default: |
modified driver-core/src/test/java/com/datastax/driver/core/SCassandraCluster.java +4 −0
| @@ -94,6 +94,10 @@ | ||
| 94 | 94 | return activityClients.get(node - 1).retrieveQueries(); |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | + public List<PreparedStatementExecution> retrievePreparedStatementExecutions(int node){ | |
| 98 | + return activityClients.get(node - 1).retrievePreparedStatementExecutions(); | |
| 99 | + } | |
| 100 | + | |
| 97 | 101 | public void clearAllPrimes() { |
| 98 | 102 | for (PrimingClient primingClient : primingClients) |
| 99 | 103 | primingClient.clearAllPrimes(); |