kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | bb06e23cbd9533448b9b7a3be71f2ebeda5a9d9684fc7e0d2e5ef18e1e9157db |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-09 20:24 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-08-09 20:24 +02:00 |
| Parents | d4015c5defee, 5f1d9b61739a |
6 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: |
added driver-core/src/test/java/com/datastax/driver/core/QueryOptionsTest.java +148 −0
| @@ -0,0 +1,148 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import java.util.List; | |
| 19 | +import com.google.common.collect.Lists; | |
| 20 | +import org.scassandra.http.client.PreparedStatementPreparation; | |
| 21 | +import org.testng.annotations.*; | |
| 22 | + | |
| 23 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 24 | + | |
| 25 | +public class QueryOptionsTest { | |
| 26 | + SCassandraCluster scassandra; | |
| 27 | + | |
| 28 | + QueryOptions queryOptions = new QueryOptions(); | |
| 29 | + | |
| 30 | + SortingLoadBalancingPolicy loadBalancingPolicy; | |
| 31 | + | |
| 32 | + Cluster cluster = null; | |
| 33 | + Session session = null; | |
| 34 | + Host host1, host2, host3; | |
| 35 | + | |
| 36 | + | |
| 37 | + @BeforeClass(groups = "short") | |
| 38 | + public void beforeClass() { | |
| 39 | + scassandra = new SCassandraCluster(CCMBridge.IP_PREFIX, 3); | |
| 40 | + } | |
| 41 | + | |
| 42 | + @BeforeMethod(groups = "short") | |
| 43 | + public void beforeMethod() { | |
| 44 | + loadBalancingPolicy = new SortingLoadBalancingPolicy(); | |
| 45 | + cluster = Cluster.builder() | |
| 46 | + .addContactPoint(CCMBridge.ipOfNode(2)) | |
| 47 | + .withLoadBalancingPolicy(loadBalancingPolicy) | |
| 48 | + .withQueryOptions(queryOptions) | |
| 49 | + .build(); | |
| 50 | + | |
| 51 | + session = cluster.connect(); | |
| 52 | + | |
| 53 | + host1 = TestUtils.findHost(cluster, 1); | |
| 54 | + host2 = TestUtils.findHost(cluster, 2); | |
| 55 | + host3 = TestUtils.findHost(cluster, 3); | |
| 56 | + | |
| 57 | + // Make sure there are no prepares | |
| 58 | + for(int host : Lists.newArrayList(1, 2, 3)) { | |
| 59 | + assertThat(scassandra.retrievePreparedStatementPreparations(host)).hasSize(0); | |
| 60 | + } | |
| 61 | + } | |
| 62 | + | |
| 63 | + public void validatePrepared(boolean expectAll) { | |
| 64 | + // Prepare the statement | |
| 65 | + String query = "select sansa_stark from the_known_world"; | |
| 66 | + session.prepare(query); | |
| 67 | + | |
| 68 | + // Ensure prepared properly based on expectation. | |
| 69 | + List<PreparedStatementPreparation> preparationOne = scassandra.retrievePreparedStatementPreparations(1); | |
| 70 | + List<PreparedStatementPreparation> preparationTwo = scassandra.retrievePreparedStatementPreparations(2); | |
| 71 | + List<PreparedStatementPreparation> preparationThree = scassandra.retrievePreparedStatementPreparations(3); | |
| 72 | + | |
| 73 | + assertThat(preparationOne).hasSize(1); | |
| 74 | + assertThat(preparationOne.get(0).getPreparedStatementText()).isEqualTo(query); | |
| 75 | + | |
| 76 | + if (expectAll) { | |
| 77 | + assertThat(preparationTwo).hasSize(1); | |
| 78 | + assertThat(preparationTwo.get(0).getPreparedStatementText()).isEqualTo(query); | |
| 79 | + assertThat(preparationThree).hasSize(1); | |
| 80 | + assertThat(preparationThree.get(0).getPreparedStatementText()).isEqualTo(query); | |
| 81 | + } else { | |
| 82 | + assertThat(preparationTwo).isEmpty(); | |
| 83 | + assertThat(preparationThree).isEmpty(); | |
| 84 | + } | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * <p> | |
| 89 | + * Validates that statements are only prepared on one node when | |
| 90 | + * {@link QueryOptions#setPrepareOnAllHosts(boolean)} is set to false. | |
| 91 | + * </p> | |
| 92 | + * | |
| 93 | + * @test_category prepared_statements:prepared | |
| 94 | + * @expected_result prepare query only on the first node. | |
| 95 | + * @jira_ticket JAVA-797 | |
| 96 | + * @since 2.0.11, 2.1.8, 2.2.1 | |
| 97 | + */ | |
| 98 | + @Test(groups = "short") | |
| 99 | + public void should_prepare_once_when_prepare_on_all_hosts_false() { | |
| 100 | + queryOptions.setPrepareOnAllHosts(false); | |
| 101 | + validatePrepared(false); | |
| 102 | + } | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * <p> | |
| 106 | + * Validates that statements are prepared on one node when | |
| 107 | + * {@link QueryOptions#setPrepareOnAllHosts(boolean)} is set to true. | |
| 108 | + * </p> | |
| 109 | + * | |
| 110 | + * @test_category prepared_statements:prepared | |
| 111 | + * @expected_result all nodes prepared the query | |
| 112 | + * @jira_ticket JAVA-797 | |
| 113 | + * @since 2.0.11, 2.1.8, 2.2.1 | |
| 114 | + */ | |
| 115 | + @Test(groups = "short") | |
| 116 | + public void should_prepare_everywhere_when_prepare_on_all_hosts_true() { | |
| 117 | + queryOptions.setPrepareOnAllHosts(true); | |
| 118 | + validatePrepared(true); | |
| 119 | + } | |
| 120 | + | |
| 121 | + /** | |
| 122 | + * <p> | |
| 123 | + * Validates that statements are prepared on one node when | |
| 124 | + * {@link QueryOptions#setPrepareOnAllHosts(boolean)} is not set. | |
| 125 | + * </p> | |
| 126 | + * | |
| 127 | + * @test_category prepared_statements:prepared | |
| 128 | + * @expected_result all nodes prepared the query. | |
| 129 | + * @jira_ticket JAVA-797 | |
| 130 | + * @since 2.0.11 | |
| 131 | + */ | |
| 132 | + @Test(groups = "short") | |
| 133 | + public void should_prepare_everywhere_when_not_configured() { | |
| 134 | + validatePrepared(true); | |
| 135 | + } | |
| 136 | + | |
| 137 | + @AfterMethod(groups = "short") | |
| 138 | + public void afterMethod() { | |
| 139 | + scassandra.clearAllPrimes(); | |
| 140 | + scassandra.clearAllRecordedActivity(); | |
| 141 | + } | |
| 142 | + | |
| 143 | + @AfterClass(groups = "short") | |
| 144 | + public void afterClass() { | |
| 145 | + if (scassandra != null) | |
| 146 | + scassandra.stop(); | |
| 147 | + } | |
| 148 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/SCassandraCluster.java +20 −2
| @@ -94,6 +94,14 @@ | ||
| 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 | + | |
| 101 | + public List<PreparedStatementPreparation> retrievePreparedStatementPreparations(int node) { | |
| 102 | + return activityClients.get(node - 1).retrievePreparedStatementPreparations(); | |
| 103 | + } | |
| 104 | + | |
| 97 | 105 | public void clearAllPrimes() { |
| 98 | 106 | for (PrimingClient primingClient : primingClients) |
| 99 | 107 | primingClient.clearAllPrimes(); |
| @@ -117,14 +125,24 @@ | ||
| 117 | 125 | if (scassandras.get(i).equals(toIgnore)) |
| 118 | 126 | continue; |
| 119 | 127 | InetAddress address = addresses.get(i); |
| 120 | - rows.add(ImmutableMap.<String, Object>builder() | |
| 128 | + Map<String, ?> row = ImmutableMap.<String, Object>builder() | |
| 121 | 129 | .put("peer", address) |
| 122 | 130 | .put("rpc_address", address) |
| 123 | 131 | .put("data_center", "datacenter1") |
| 124 | 132 | .put("rack", "rack1") |
| 125 | 133 | .put("release_version", "2.0.1") |
| 126 | 134 | .put("tokens", ImmutableSet.of(Long.toString(Long.MIN_VALUE + i))) |
| 127 | - .build()); | |
| 135 | + .build(); | |
| 136 | + | |
| 137 | + rows.add(row); | |
| 138 | + | |
| 139 | + String query = "SELECT * FROM system.peers WHERE peer='" + address.toString().substring(1) + "'"; | |
| 140 | + primingClient.prime( | |
| 141 | + PrimingRequest.queryBuilder() | |
| 142 | + .withQuery(query) | |
| 143 | + .withColumnTypes(SELECT_PEERS_COLUMN_TYPES) | |
| 144 | + .withRows(row) | |
| 145 | + .build()); | |
| 128 | 146 | } |
| 129 | 147 | primingClient.prime( |
| 130 | 148 | PrimingRequest.queryBuilder() |
modified pom.xml +1 −5
| @@ -60,11 +60,7 @@ | ||
| 60 | 60 | <testng.version>6.8.8</testng.version> |
| 61 | 61 | <assertj.version>1.7.0</assertj.version> |
| 62 | 62 | <mockito.version>1.10.8</mockito.version> |
| 63 | - <!-- | |
| 64 | - There are more recent versions of Scassandra but they require JDK 7, | |
| 65 | - our build is still using JDK 6. | |
| 66 | - --> | |
| 67 | - <scassandra.version>0.4.1</scassandra.version> | |
| 63 | + <scassandra.version>0.9.1</scassandra.version> | |
| 68 | 64 | <main.basedir>${project.basedir}</main.basedir> |
| 69 | 65 | </properties> |
| 70 | 66 |