|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package com.datastax.oss.driver.core.cql; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.junit.Assert.fail; |
| 22 | + |
| 23 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 24 | +import com.datastax.oss.driver.api.core.cql.PrepareRequest; |
| 25 | +import com.datastax.oss.driver.api.core.cql.PreparedStatement; |
| 26 | +import com.datastax.oss.driver.api.testinfra.ccm.CustomCcmRule; |
| 27 | +import com.datastax.oss.driver.api.testinfra.session.SessionRule; |
| 28 | +import com.datastax.oss.driver.api.testinfra.session.SessionUtils; |
| 29 | +import com.datastax.oss.driver.categories.IsolatedTests; |
| 30 | +import com.datastax.oss.driver.internal.core.context.DefaultDriverContext; |
| 31 | +import com.datastax.oss.driver.internal.core.cql.CqlPrepareAsyncProcessor; |
| 32 | +import com.datastax.oss.driver.shaded.guava.common.base.Predicates; |
| 33 | +import com.datastax.oss.driver.shaded.guava.common.cache.Cache; |
| 34 | +import com.datastax.oss.driver.shaded.guava.common.collect.Iterables; |
| 35 | +import java.util.concurrent.CompletableFuture; |
| 36 | +import org.junit.After; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.Rule; |
| 39 | +import org.junit.Test; |
| 40 | +import org.junit.experimental.categories.Category; |
| 41 | +import org.junit.rules.RuleChain; |
| 42 | +import org.junit.rules.TestRule; |
| 43 | + |
| 44 | +@Category(IsolatedTests.class) |
| 45 | +public class PreparedStatementCancellationIT { |
| 46 | + |
| 47 | + private CustomCcmRule ccmRule = CustomCcmRule.builder().build(); |
| 48 | + |
| 49 | + private SessionRule<CqlSession> sessionRule = SessionRule.builder(ccmRule).build(); |
| 50 | + |
| 51 | + @Rule public TestRule chain = RuleChain.outerRule(ccmRule).around(sessionRule); |
| 52 | + |
| 53 | + @Before |
| 54 | + public void setup() { |
| 55 | + |
| 56 | + CqlSession session = SessionUtils.newSession(ccmRule, sessionRule.keyspace()); |
| 57 | + session.execute("DROP TABLE IF EXISTS test_table_1"); |
| 58 | + session.execute("CREATE TABLE test_table_1 (k int primary key, v int)"); |
| 59 | + session.execute("INSERT INTO test_table_1 (k,v) VALUES (1, 100)"); |
| 60 | + session.execute("INSERT INTO test_table_1 (k,v) VALUES (2, 200)"); |
| 61 | + session.execute("INSERT INTO test_table_1 (k,v) VALUES (3, 300)"); |
| 62 | + session.close(); |
| 63 | + } |
| 64 | + |
| 65 | + @After |
| 66 | + public void teardown() { |
| 67 | + |
| 68 | + CqlSession session = SessionUtils.newSession(ccmRule, sessionRule.keyspace()); |
| 69 | + session.execute("DROP TABLE test_table_1"); |
| 70 | + session.close(); |
| 71 | + } |
| 72 | + |
| 73 | + private CompletableFuture<PreparedStatement> toCompletableFuture(CqlSession session, String cql) { |
| 74 | + |
| 75 | + return session.prepareAsync(cql).toCompletableFuture(); |
| 76 | + } |
| 77 | + |
| 78 | + private CqlPrepareAsyncProcessor findProcessor(CqlSession session) { |
| 79 | + |
| 80 | + DefaultDriverContext context = (DefaultDriverContext) session.getContext(); |
| 81 | + return (CqlPrepareAsyncProcessor) |
| 82 | + Iterables.find( |
| 83 | + context.getRequestProcessorRegistry().getProcessors(), |
| 84 | + Predicates.instanceOf(CqlPrepareAsyncProcessor.class)); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void should_cache_valid_cql() throws Exception { |
| 89 | + |
| 90 | + CqlSession session = SessionUtils.newSession(ccmRule, sessionRule.keyspace()); |
| 91 | + CqlPrepareAsyncProcessor processor = findProcessor(session); |
| 92 | + Cache<PrepareRequest, CompletableFuture<PreparedStatement>> cache = processor.getCache(); |
| 93 | + assertThat(cache.size()).isEqualTo(0); |
| 94 | + |
| 95 | + // Make multiple CompletableFuture requests for the specified CQL, then wait until |
| 96 | + // the cached request finishes and confirm that all futures got the same values |
| 97 | + String cql = "select v from test_table_1 where k = ?"; |
| 98 | + CompletableFuture<PreparedStatement> cf1 = toCompletableFuture(session, cql); |
| 99 | + CompletableFuture<PreparedStatement> cf2 = toCompletableFuture(session, cql); |
| 100 | + assertThat(cache.size()).isEqualTo(1); |
| 101 | + |
| 102 | + CompletableFuture<PreparedStatement> future = Iterables.get(cache.asMap().values(), 0); |
| 103 | + PreparedStatement stmt = future.get(); |
| 104 | + |
| 105 | + assertThat(cf1.isDone()).isTrue(); |
| 106 | + assertThat(cf2.isDone()).isTrue(); |
| 107 | + |
| 108 | + assertThat(cf1.join()).isEqualTo(stmt); |
| 109 | + assertThat(cf2.join()).isEqualTo(stmt); |
| 110 | + } |
| 111 | + |
| 112 | + // A holdover from work done on JAVA-3055. This probably isn't _desired_ behaviour but this test |
| 113 | + // documents the fact that the current driver impl will behave in this way. We should probably |
| 114 | + // consider changing this in a future release, although it's worthwhile fully considering the |
| 115 | + // implications of such a change. |
| 116 | + @Test |
| 117 | + public void will_cache_invalid_cql() throws Exception { |
| 118 | + |
| 119 | + CqlSession session = SessionUtils.newSession(ccmRule, sessionRule.keyspace()); |
| 120 | + CqlPrepareAsyncProcessor processor = findProcessor(session); |
| 121 | + Cache<PrepareRequest, CompletableFuture<PreparedStatement>> cache = processor.getCache(); |
| 122 | + assertThat(cache.size()).isEqualTo(0); |
| 123 | + |
| 124 | + // Verify that we get the CompletableFuture even if the CQL is invalid but that nothing is |
| 125 | + // cached |
| 126 | + String cql = "select v fromfrom test_table_1 where k = ?"; |
| 127 | + CompletableFuture<PreparedStatement> cf = toCompletableFuture(session, cql); |
| 128 | + |
| 129 | + // join() here should throw exceptions due to the invalid syntax... for purposes of this test we |
| 130 | + // can ignore this |
| 131 | + try { |
| 132 | + cf.join(); |
| 133 | + fail(); |
| 134 | + } catch (Exception e) { |
| 135 | + } |
| 136 | + |
| 137 | + assertThat(cache.size()).isEqualTo(1); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void should_not_affect_cache_if_returned_futures_are_cancelled() throws Exception { |
| 142 | + |
| 143 | + CqlSession session = SessionUtils.newSession(ccmRule, sessionRule.keyspace()); |
| 144 | + CqlPrepareAsyncProcessor processor = findProcessor(session); |
| 145 | + Cache<PrepareRequest, CompletableFuture<PreparedStatement>> cache = processor.getCache(); |
| 146 | + assertThat(cache.size()).isEqualTo(0); |
| 147 | + |
| 148 | + String cql = "select v from test_table_1 where k = ?"; |
| 149 | + CompletableFuture<PreparedStatement> cf = toCompletableFuture(session, cql); |
| 150 | + |
| 151 | + assertThat(cf.isCancelled()).isFalse(); |
| 152 | + assertThat(cf.cancel(false)).isTrue(); |
| 153 | + assertThat(cf.isCancelled()).isTrue(); |
| 154 | + assertThat(cf.isCompletedExceptionally()).isTrue(); |
| 155 | + |
| 156 | + // Confirm that cancelling the CompletableFuture returned to the user does _not_ cancel the |
| 157 | + // future used within the cache. CacheEntry very deliberately doesn't maintain a reference |
| 158 | + // to it's contained CompletableFuture so we have to get at this by secondary effects. |
| 159 | + assertThat(cache.size()).isEqualTo(1); |
| 160 | + CompletableFuture<PreparedStatement> future = Iterables.get(cache.asMap().values(), 0); |
| 161 | + PreparedStatement rv = future.get(); |
| 162 | + assertThat(rv).isNotNull(); |
| 163 | + assertThat(rv.getQuery()).isEqualTo(cql); |
| 164 | + assertThat(cache.size()).isEqualTo(1); |
| 165 | + } |
| 166 | +} |
0 commit comments