Elffuss BraidHub

kikocis/java-driver public

Merge pull request #404 from datastax/java658

JAVA-658 : Provide an option to cancel re-prepare all statements if onUp.
Changeset811679f7e41a88deffa37a3f5c403d648e7a3ff25e989ff098fd3ee522c49b25
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-08-09 22:47 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-08-09 22:47 +02:00
Parentsbb06e23cbd95, 48deaab8061a

Browse the files at this changeset

5 files changed

modified changelog/README.md +1 −0
@@ -32,6 +32,7 @@
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)
3434 - [improvement] Provide an option to prepare statements only on one node (JAVA-797)
35+- [improvement] Provide an option to not re-prepare all statements in onUp (JAVA-658)
3536
3637 Merged from 2.0.10_fixes branch:
3738
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +2 −1
@@ -1550,7 +1550,8 @@
15501550 }
15511551
15521552 try {
1553- reusedConnection = prepareAllQueries(host, reusedConnection);
1553+ if (getCluster().getConfiguration().getQueryOptions().isReprepareOnUp())
1554+ reusedConnection = prepareAllQueries(host, reusedConnection);
15541555 } catch (InterruptedException e) {
15551556 Thread.currentThread().interrupt();
15561557 // Don't propagate because we don't want to prevent other listener to run
modified driver-core/src/main/java/com/datastax/driver/core/QueryOptions.java +41 −3
@@ -46,6 +46,7 @@
4646 private volatile ConsistencyLevel serialConsistency = DEFAULT_SERIAL_CONSISTENCY_LEVEL;
4747 private volatile int fetchSize = DEFAULT_FETCH_SIZE;
4848 private volatile boolean defaultIdempotence = DEFAULT_IDEMPOTENCE;
49+ private volatile boolean reprepareOnUp = true;
4950 private volatile Cluster.Manager manager;
5051 private volatile boolean prepareOnAllHosts = true;
5152
@@ -188,9 +189,9 @@
188189 * chance of having been hit by at least one client for each statement.
189190 * <p>
190191 * 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).
192+ * prepared a given statement, it needs to be re-prepared on the fly the first time
193+ * it gets executed; this causes a performance penalty (one extra roundtrip to resend
194+ * the query to prepare, and another to retry the execution).
194195 *
195196 * @param prepareOnAllHosts the new value to set to indicate whether to prepare
196197 * statements once or on all nodes.
@@ -211,4 +212,41 @@
211212 public boolean isPrepareOnAllHosts() {
212213 return this.prepareOnAllHosts;
213214 }
215+
216+ /**
217+ * Set whether the driver should re-prepare all cached prepared statements on a host
218+ * when it marks it back up.
219+ * <p>
220+ * This option is enabled by default.
221+ * <p>
222+ * The reason why you might want to disable it is to optimize reconnection time when
223+ * you believe hosts often get marked down because of temporary network issues, rather
224+ * than the host really crashing. In that case, the host still has prepared statements
225+ * in its cache when the driver reconnects, so re-preparing is redundant.
226+ * <p>
227+ * On the other hand, if that assumption turns out to be wrong and the host had
228+ * really restarted, its prepared statement cache is empty, and statements need to be
229+ * re-prepared on the fly the first time they get executed; this causes a performance
230+ * penalty (one extra roundtrip to resend the query to prepare, and another to retry
231+ * the execution).
232+ *
233+ * @param reprepareOnUp whether the driver should re-prepare when marking a node up.
234+ * @return this {@code QueryOptions} instance.
235+ */
236+ public QueryOptions setReprepareOnUp(boolean reprepareOnUp){
237+ this.reprepareOnUp = reprepareOnUp;
238+ return this;
239+ }
240+
241+ /**
242+ * Whether the driver should re-prepare all cached prepared statements on a host
243+ * when its marks that host back up.
244+ *
245+ * @return the value.
246+ *
247+ * @see #setReprepareOnUp(boolean)
248+ */
249+ public boolean isReprepareOnUp() {
250+ return this.reprepareOnUp;
251+ }
214252 }
modified driver-core/src/test/java/com/datastax/driver/core/QueryOptionsTest.java +83 −2
@@ -16,6 +16,8 @@
1616 package com.datastax.driver.core;
1717
1818 import java.util.List;
19+import java.util.concurrent.TimeUnit;
20+
1921 import com.google.common.collect.Lists;
2022 import org.scassandra.http.client.PreparedStatementPreparation;
2123 import org.testng.annotations.*;
@@ -25,7 +27,7 @@
2527 public class QueryOptionsTest {
2628 SCassandraCluster scassandra;
2729
28- QueryOptions queryOptions = new QueryOptions();
30+ QueryOptions queryOptions;
2931
3032 SortingLoadBalancingPolicy loadBalancingPolicy;
3133
@@ -41,6 +43,7 @@
4143
4244 @BeforeMethod(groups = "short")
4345 public void beforeMethod() {
46+ queryOptions = new QueryOptions();
4447 loadBalancingPolicy = new SortingLoadBalancingPolicy();
4548 cluster = Cluster.builder()
4649 .addContactPoint(CCMBridge.ipOfNode(2))
@@ -55,7 +58,7 @@
5558 host3 = TestUtils.findHost(cluster, 3);
5659
5760 // Make sure there are no prepares
58- for(int host : Lists.newArrayList(1, 2, 3)) {
61+ for (int host : Lists.newArrayList(1, 2, 3)) {
5962 assertThat(scassandra.retrievePreparedStatementPreparations(host)).hasSize(0);
6063 }
6164 }
@@ -134,8 +137,86 @@
134137 validatePrepared(true);
135138 }
136139
140+ private void valideReprepareOnUp(boolean expectReprepare) {
141+ String query = "select sansa_stark from the_known_world";
142+ session.prepare(query);
143+
144+ List<PreparedStatementPreparation> preparationOne = scassandra.retrievePreparedStatementPreparations(1);
145+
146+ assertThat(preparationOne).hasSize(1);
147+ assertThat(preparationOne.get(0).getPreparedStatementText()).isEqualTo(query);
148+
149+ scassandra.clearAllRecordedActivity();
150+ scassandra.stop(1);
151+ assertThat(cluster).host(1).goesDownWithin(10, TimeUnit.SECONDS);
152+
153+ scassandra.start(1);
154+ assertThat(cluster).host(1).comesUpWithin(60, TimeUnit.SECONDS);
155+
156+ preparationOne = scassandra.retrievePreparedStatementPreparations(1);
157+ if (expectReprepare) {
158+ assertThat(preparationOne).hasSize(1);
159+ assertThat(preparationOne.get(0).getPreparedStatementText()).isEqualTo(query);
160+ } else {
161+ assertThat(preparationOne).isEmpty();
162+ }
163+ }
164+
165+ /**
166+ * <p>
167+ * Validates that statements are reprepared when a node comes back up when
168+ * {@link QueryOptions#setReprepareOnUp(boolean)} is set to true.
169+ * </p>
170+ *
171+ * @test_category prepared_statements:prepared
172+ * @expected_result reprepare query on the restarted node.
173+ * @jira_ticket JAVA-658
174+ * @since 2.0.11, 2.1.8, 2.2.1
175+ */
176+ @Test(groups = "short")
177+ public void should_reprepare_on_up_when_enabled() {
178+ queryOptions.setReprepareOnUp(true);
179+ valideReprepareOnUp(true);
180+ }
181+
182+ /**
183+ * <p>
184+ * Validates that statements are reprepared when a node comes back up with
185+ * the default configuration.
186+ * </p>
187+ *
188+ * @test_category prepared_statements:prepared
189+ * @expected_result reprepare query on the restarted node.
190+ * @jira_ticket JAVA-658
191+ * @since 2.0.11, 2.1.8, 2.2.1
192+ */
193+ @Test(groups = "short")
194+ public void should_reprepare_on_up_by_default() {
195+ valideReprepareOnUp(true);
196+ }
197+
198+ /**
199+ * <p>
200+ * Validates that statements are not reprepared when a node comes back up when
201+ * {@link QueryOptions#setReprepareOnUp(boolean)} is set to false.
202+ * </p>
203+ *
204+ * @test_category prepared_statements:prepared
205+ * @expected_result do not reprepare query on the restarted node.
206+ * @jira_ticket JAVA-658
207+ * @since 2.0.11, 2.1.8, 2.2.1
208+ */
209+ @Test(groups = "short")
210+ public void should_not_reprepare_on_up_when_disabled() {
211+ queryOptions.setReprepareOnUp(false);
212+ valideReprepareOnUp(false);
213+ }
214+
137215 @AfterMethod(groups = "short")
138216 public void afterMethod() {
217+ if (cluster != null)
218+ cluster.close();
219+
139220 scassandra.clearAllPrimes();
140221 scassandra.clearAllRecordedActivity();
141222 }
modified driver-core/src/test/java/com/datastax/driver/core/SCassandraCluster.java +10 −0
@@ -85,6 +85,16 @@
8585 scassandra.stop();
8686 }
8787
88+ public void start(int node) {
89+ int i = node - 1;
90+ scassandras.get(i).start();
91+ primePeers(primingClients.get(i), scassandras.get(i));
92+ }
93+
94+ public void stop(int node) {
95+ scassandras.get(node - 1).stop();
96+ }
97+
8898 public SCassandraCluster prime(int node, PrimingRequest request) {
8999 primingClients.get(node - 1).prime(request);
90100 return this;