kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 811679f7e41a88deffa37a3f5c403d648e7a3ff25e989ff098fd3ee522c49b25 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-09 22:47 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-09 22:47 +02:00 |
| Parents | bb06e23cbd95, 48deaab8061a |
5 files changed
modified changelog/README.md +1 −0
| @@ -32,6 +32,7 @@ | ||
| 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 | 34 | - [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) | |
| 35 | 36 | |
| 36 | 37 | Merged from 2.0.10_fixes branch: |
| 37 | 38 |
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +2 −1
| @@ -1550,7 +1550,8 @@ | ||
| 1550 | 1550 | } |
| 1551 | 1551 | |
| 1552 | 1552 | try { |
| 1553 | - reusedConnection = prepareAllQueries(host, reusedConnection); | |
| 1553 | + if (getCluster().getConfiguration().getQueryOptions().isReprepareOnUp()) | |
| 1554 | + reusedConnection = prepareAllQueries(host, reusedConnection); | |
| 1554 | 1555 | } catch (InterruptedException e) { |
| 1555 | 1556 | Thread.currentThread().interrupt(); |
| 1556 | 1557 | // 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 @@ | ||
| 46 | 46 | private volatile ConsistencyLevel serialConsistency = DEFAULT_SERIAL_CONSISTENCY_LEVEL; |
| 47 | 47 | private volatile int fetchSize = DEFAULT_FETCH_SIZE; |
| 48 | 48 | private volatile boolean defaultIdempotence = DEFAULT_IDEMPOTENCE; |
| 49 | + private volatile boolean reprepareOnUp = true; | |
| 49 | 50 | private volatile Cluster.Manager manager; |
| 50 | 51 | private volatile boolean prepareOnAllHosts = true; |
| 51 | 52 | |
| @@ -188,9 +189,9 @@ | ||
| 188 | 189 | * chance of having been hit by at least one client for each statement. |
| 189 | 190 | * <p> |
| 190 | 191 | * 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). | |
| 194 | 195 | * |
| 195 | 196 | * @param prepareOnAllHosts the new value to set to indicate whether to prepare |
| 196 | 197 | * statements once or on all nodes. |
| @@ -211,4 +212,41 @@ | ||
| 211 | 212 | public boolean isPrepareOnAllHosts() { |
| 212 | 213 | return this.prepareOnAllHosts; |
| 213 | 214 | } |
| 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 | + } | |
| 214 | 252 | } |
modified driver-core/src/test/java/com/datastax/driver/core/QueryOptionsTest.java +83 −2
| @@ -16,6 +16,8 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import java.util.List; |
| 19 | +import java.util.concurrent.TimeUnit; | |
| 20 | + | |
| 19 | 21 | import com.google.common.collect.Lists; |
| 20 | 22 | import org.scassandra.http.client.PreparedStatementPreparation; |
| 21 | 23 | import org.testng.annotations.*; |
| @@ -25,7 +27,7 @@ | ||
| 25 | 27 | public class QueryOptionsTest { |
| 26 | 28 | SCassandraCluster scassandra; |
| 27 | 29 | |
| 28 | - QueryOptions queryOptions = new QueryOptions(); | |
| 30 | + QueryOptions queryOptions; | |
| 29 | 31 | |
| 30 | 32 | SortingLoadBalancingPolicy loadBalancingPolicy; |
| 31 | 33 | |
| @@ -41,6 +43,7 @@ | ||
| 41 | 43 | |
| 42 | 44 | @BeforeMethod(groups = "short") |
| 43 | 45 | public void beforeMethod() { |
| 46 | + queryOptions = new QueryOptions(); | |
| 44 | 47 | loadBalancingPolicy = new SortingLoadBalancingPolicy(); |
| 45 | 48 | cluster = Cluster.builder() |
| 46 | 49 | .addContactPoint(CCMBridge.ipOfNode(2)) |
| @@ -55,7 +58,7 @@ | ||
| 55 | 58 | host3 = TestUtils.findHost(cluster, 3); |
| 56 | 59 | |
| 57 | 60 | // Make sure there are no prepares |
| 58 | - for(int host : Lists.newArrayList(1, 2, 3)) { | |
| 61 | + for (int host : Lists.newArrayList(1, 2, 3)) { | |
| 59 | 62 | assertThat(scassandra.retrievePreparedStatementPreparations(host)).hasSize(0); |
| 60 | 63 | } |
| 61 | 64 | } |
| @@ -134,8 +137,86 @@ | ||
| 134 | 137 | validatePrepared(true); |
| 135 | 138 | } |
| 136 | 139 | |
| 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 | + | |
| 137 | 215 | @AfterMethod(groups = "short") |
| 138 | 216 | public void afterMethod() { |
| 217 | + if (cluster != null) | |
| 218 | + cluster.close(); | |
| 219 | + | |
| 139 | 220 | scassandra.clearAllPrimes(); |
| 140 | 221 | scassandra.clearAllRecordedActivity(); |
| 141 | 222 | } |
modified driver-core/src/test/java/com/datastax/driver/core/SCassandraCluster.java +10 −0
| @@ -85,6 +85,16 @@ | ||
| 85 | 85 | scassandra.stop(); |
| 86 | 86 | } |
| 87 | 87 | |
| 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 | + | |
| 88 | 98 | public SCassandraCluster prime(int node, PrimingRequest request) { |
| 89 | 99 | primingClients.get(node - 1).prime(request); |
| 90 | 100 | return this; |