Elffuss BraidHub

kikocis/java-driver public

JAVA-797 : Add option to prepare statements only on one node
Changeset296c787d932c0ba04eacf731d34aa5c593be4b6bc4f4a4cab2d1f559678eec35
AuthorKevin Gallardo <kevin.gallardo@datastax.com> · 2015-07-08 11:47 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-08-09 20:19 +02:00
Parentsd4015c5defee

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -31,6 +31,7 @@
3131 - [improvement] Allow PlainTextAuthProvider to change its credentials at runtime (JAVA-719)
3232 - [new feature] Make it possible to register for SchemaChange Events (JAVA-151)
3333 - [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)
3435
3536 Merged from 2.0.10_fixes branch:
3637
modified driver-core/src/main/java/com/datastax/driver/core/QueryOptions.java +42 −0
@@ -47,6 +47,7 @@
4747 private volatile int fetchSize = DEFAULT_FETCH_SIZE;
4848 private volatile boolean defaultIdempotence = DEFAULT_IDEMPOTENCE;
4949 private volatile Cluster.Manager manager;
50+ private volatile boolean prepareOnAllHosts = true;
5051
5152 /**
5253 * Creates a new {@link QueryOptions} instance using the {@link #DEFAULT_CONSISTENCY_LEVEL},
@@ -169,4 +170,45 @@
169170 public boolean getDefaultIdempotence() {
170171 return defaultIdempotence;
171172 }
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+ }
172214 }
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +11 −9
@@ -145,15 +145,17 @@
145145 Responses.Result.Prepared pmsg = (Responses.Result.Prepared)rm;
146146 PreparedStatement stmt = DefaultPreparedStatement.fromMessage(pmsg, cluster.getMetadata(), query, poolsState.keyspace);
147147 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+ }
157159 }
158160 return stmt;
159161 default:
modified driver-core/src/test/java/com/datastax/driver/core/SCassandraCluster.java +4 −0
@@ -94,6 +94,10 @@
9494 return activityClients.get(node - 1).retrieveQueries();
9595 }
9696
97+ public List<PreparedStatementExecution> retrievePreparedStatementExecutions(int node){
98+ return activityClients.get(node - 1).retrievePreparedStatementExecutions();
99+ }
100+
97101 public void clearAllPrimes() {
98102 for (PrimingClient primingClient : primingClients)
99103 primingClient.clearAllPrimes();