kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 42374a8794f274176087774f7227e79c5ae42199059a79013e201ae33453a452 |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-04-22 16:11 -05:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-04-24 17:55 +02:00 |
| Parents | 31367d613edd |
1 file changed
added driver-core/src/test/java/com/datastax/driver/core/ConnectionReleaseTest.java +129 −0
| @@ -0,0 +1,129 @@ | ||
| 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 com.google.common.collect.ImmutableMap; | |
| 19 | +import com.google.common.collect.Lists; | |
| 20 | +import com.google.common.util.concurrent.AsyncFunction; | |
| 21 | +import com.google.common.util.concurrent.Futures; | |
| 22 | +import com.google.common.util.concurrent.ListenableFuture; | |
| 23 | +import org.scassandra.http.client.PrimingRequest; | |
| 24 | +import org.testng.annotations.Test; | |
| 25 | + | |
| 26 | +import java.util.Collection; | |
| 27 | +import java.util.Collections; | |
| 28 | +import java.util.concurrent.TimeUnit; | |
| 29 | +import java.util.concurrent.TimeoutException; | |
| 30 | + | |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 32 | +import static org.testng.Assert.fail; | |
| 33 | + | |
| 34 | +public class ConnectionReleaseTest extends ScassandraTestBase { | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * <p> | |
| 38 | + * Validates that when a future is set that the stream associated with the future's request is released. | |
| 39 | + * This prevents situations where a user may not be specifying a separate executor on a callback/ | |
| 40 | + * transform to a ResultSetFuture, which is not recommended, causing executeAsync to block in borrowConnection | |
| 41 | + * until stream ids become available. | |
| 42 | + * | |
| 43 | + * Executes the following: | |
| 44 | + * | |
| 45 | + * <ol> | |
| 46 | + * <li>Sets # of connections per host to 1.</li> | |
| 47 | + * <li>Sends MAX_STREAM_PER_CONNECTION-1 requests that take 10 seconds to execute.</li> | |
| 48 | + * <li>Calls executeAsync to retrieve records from test1 with k=1.</li> | |
| 49 | + * <li>Transforms executeAsync to take the 'c' column from the result and query test2. | |
| 50 | + * This is done without an executor to ensure the netty worker is used and has to wait for the function | |
| 51 | + * completion.</li> | |
| 52 | + * <li>Asserts that the transformed future completes within pool timeout and the value is as expected.</li> | |
| 53 | + * </ol> | |
| 54 | + * | |
| 55 | + * @jira_ticket JAVA-666 | |
| 56 | + * @expected_result Are able to transform a Future without hanging in executeAsync as connection should be freed | |
| 57 | + * before the transform function is called. | |
| 58 | + * @test_category queries:async | |
| 59 | + * @since 2.0.10, 2.1.6 | |
| 60 | + */ | |
| 61 | + @SuppressWarnings("unchecked") | |
| 62 | + @Test(groups="short") | |
| 63 | + public void should_release_connection_before_completing_future() throws Exception { | |
| 64 | + Cluster cluster = null; | |
| 65 | + Collection<ResultSetFuture> mockFutures = Lists.newArrayList(); | |
| 66 | + try { | |
| 67 | + primingClient.prime( | |
| 68 | + PrimingRequest.queryBuilder() | |
| 69 | + .withQuery("mock query") | |
| 70 | + .withRows(ImmutableMap.of("key", 1)) | |
| 71 | + .withFixedDelay(10000) | |
| 72 | + .build() | |
| 73 | + ); | |
| 74 | + primingClient.prime( | |
| 75 | + PrimingRequest.queryBuilder() | |
| 76 | + .withQuery("select c from test1 where k=1") | |
| 77 | + .withRows(ImmutableMap.of("c", "hello")) | |
| 78 | + .build() | |
| 79 | + ); | |
| 80 | + primingClient.prime( | |
| 81 | + PrimingRequest.queryBuilder() | |
| 82 | + .withQuery("select n from test2 where c='hello'") | |
| 83 | + .withRows(ImmutableMap.of("n", "world")) | |
| 84 | + .build() | |
| 85 | + ); | |
| 86 | + | |
| 87 | + cluster = Cluster.builder().addContactPointsWithPorts(Collections.singletonList(hostAddress)) | |
| 88 | + .withPoolingOptions(new PoolingOptions() | |
| 89 | + .setCoreConnectionsPerHost(HostDistance.LOCAL, 1) | |
| 90 | + .setMaxConnectionsPerHost(HostDistance.LOCAL, 1)) | |
| 91 | + .build(); | |
| 92 | + | |
| 93 | + final Session session = cluster.connect("ks"); | |
| 94 | + // Consume all stream ids except one. | |
| 95 | + for(int i = 0; i < StreamIdGenerator.MAX_STREAM_PER_CONNECTION-1; i++) | |
| 96 | + mockFutures.add(session.executeAsync("mock query")); | |
| 97 | + | |
| 98 | + | |
| 99 | + ListenableFuture<ResultSet> future = Futures.transform(session.executeAsync("select c from test1 where k=1"), | |
| 100 | + new AsyncFunction<ResultSet, ResultSet>() { | |
| 101 | + @Override | |
| 102 | + public ListenableFuture<ResultSet> apply(ResultSet result) { | |
| 103 | + Row row = result.one(); | |
| 104 | + String c = row.getString("c"); | |
| 105 | + // Execute async might hang if no streams are available. This happens if the connection | |
| 106 | + // was not release. | |
| 107 | + return session.executeAsync("select n from test2 where c='" + c + "'"); | |
| 108 | + } | |
| 109 | + }); | |
| 110 | + | |
| 111 | + // Wait for up to pool timeout for response. If future is not complete by then, we can assume | |
| 112 | + // that it is likely that the execute is hung because the connection wasn't released yet. | |
| 113 | + long waitTimeInMs = cluster.getConfiguration().getPoolingOptions().getPoolTimeoutMillis(); | |
| 114 | + try { | |
| 115 | + ResultSet result = future.get(waitTimeInMs, TimeUnit.MILLISECONDS); | |
| 116 | + assertThat(result.one().getString("n")).isEqualTo("world"); | |
| 117 | + } catch (TimeoutException e) { | |
| 118 | + fail("Future timed out after " + waitTimeInMs + "ms. " + | |
| 119 | + "There is a strong possibility connection is not being released."); | |
| 120 | + } | |
| 121 | + } finally { | |
| 122 | + // Cancel all pending requests. | |
| 123 | + for(ResultSetFuture future : mockFutures) | |
| 124 | + future.cancel(true); | |
| 125 | + if(cluster != null) | |
| 126 | + cluster.close(); | |
| 127 | + } | |
| 128 | + } | |
| 129 | +} |