|
| 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 com.datastax.oss.driver.api.core.CqlSession; |
| 21 | +import com.datastax.oss.driver.api.core.config.DriverConfigLoader; |
| 22 | +import com.datastax.oss.driver.api.core.context.DriverContext; |
| 23 | +import com.datastax.oss.driver.api.core.cql.PreparedStatement; |
| 24 | +import com.datastax.oss.driver.api.core.session.ProgrammaticArguments; |
| 25 | +import com.datastax.oss.driver.api.core.session.SessionBuilder; |
| 26 | +import com.datastax.oss.driver.api.testinfra.session.SessionUtils; |
| 27 | +import com.datastax.oss.driver.internal.core.context.DefaultDriverContext; |
| 28 | +import com.datastax.oss.driver.internal.core.cql.CqlPrepareAsyncProcessor; |
| 29 | +import com.datastax.oss.driver.internal.core.cql.CqlPrepareSyncProcessor; |
| 30 | +import com.datastax.oss.driver.internal.core.session.BuiltInRequestProcessors; |
| 31 | +import com.datastax.oss.driver.internal.core.session.RequestProcessor; |
| 32 | +import com.datastax.oss.driver.internal.core.session.RequestProcessorRegistry; |
| 33 | +import com.datastax.oss.driver.shaded.guava.common.cache.RemovalListener; |
| 34 | +import com.datastax.oss.driver.shaded.guava.common.util.concurrent.Uninterruptibles; |
| 35 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 36 | +import java.nio.ByteBuffer; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Optional; |
| 39 | +import java.util.concurrent.CompletableFuture; |
| 40 | +import org.junit.AfterClass; |
| 41 | +import org.junit.BeforeClass; |
| 42 | +import org.slf4j.Logger; |
| 43 | +import org.slf4j.LoggerFactory; |
| 44 | + |
| 45 | +/** |
| 46 | + * Base class for prepared statement integration tests that provides a customized |
| 47 | + * CqlPrepareAsyncProcessor with strong values cache to prevent cache entries from unexpectedly |
| 48 | + * disappearing during tests. |
| 49 | + */ |
| 50 | +public abstract class PreparedStatementITBase { |
| 51 | + |
| 52 | + private static final Logger LOG = LoggerFactory.getLogger(PreparedStatementITBase.class); |
| 53 | + |
| 54 | + /** Event class for prepared statement removal notifications. */ |
| 55 | + protected static class PreparedStatementRemovalEvent { |
| 56 | + |
| 57 | + public final ByteBuffer queryId; |
| 58 | + |
| 59 | + public PreparedStatementRemovalEvent(ByteBuffer queryId) { |
| 60 | + this.queryId = queryId; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean equals(Object o) { |
| 65 | + if (this == o) return true; |
| 66 | + if (o == null || !(o instanceof PreparedStatementRemovalEvent)) return false; |
| 67 | + PreparedStatementRemovalEvent that = (PreparedStatementRemovalEvent) o; |
| 68 | + return java.util.Objects.equals(queryId, that.queryId); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public int hashCode() { |
| 73 | + return java.util.Objects.hash(queryId); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public String toString() { |
| 78 | + return "PreparedStatementRemovalEvent{" + "queryId=" + queryId + '}'; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Customized CqlPrepareAsyncProcessor that uses strong values cache instead of weak values to |
| 84 | + * prevent cache entries from unexpectedly disappearing during tests. |
| 85 | + */ |
| 86 | + protected static class TestCqlPrepareAsyncProcessor extends CqlPrepareAsyncProcessor { |
| 87 | + |
| 88 | + private static final Logger LOG = |
| 89 | + LoggerFactory.getLogger(PreparedStatementITBase.TestCqlPrepareAsyncProcessor.class); |
| 90 | + |
| 91 | + /** |
| 92 | + * Creates a removal listener that fires PreparedStatementRemovalEvent when cache entries are |
| 93 | + * removed. |
| 94 | + */ |
| 95 | + private static RemovalListener<Object, Object> buildCacheRemoveCallback( |
| 96 | + @NonNull Optional<DefaultDriverContext> context) { |
| 97 | + return (evt) -> { |
| 98 | + try { |
| 99 | + LOG.error( |
| 100 | + "Cache removal callback triggered, cause: {}, key: {}", evt.getCause(), evt.getKey()); |
| 101 | + CompletableFuture<PreparedStatement> future = |
| 102 | + (CompletableFuture<PreparedStatement>) evt.getValue(); |
| 103 | + |
| 104 | + // Add more detailed logging about the future state |
| 105 | + LOG.error( |
| 106 | + "Future state - done: {}, cancelled: {}, completedExceptionally: {}", |
| 107 | + future.isDone(), |
| 108 | + future.isCancelled(), |
| 109 | + future.isCompletedExceptionally()); |
| 110 | + |
| 111 | + if (future.isDone() && !future.isCompletedExceptionally() && !future.isCancelled()) { |
| 112 | + ByteBuffer queryId = Uninterruptibles.getUninterruptibly(future).getId(); |
| 113 | + LOG.error("Firing PreparedStatementRemovalEvent for queryId: {}", queryId); |
| 114 | + context.ifPresent( |
| 115 | + ctx -> { |
| 116 | + LOG.error("About to fire PreparedStatementRemovalEvent on event bus"); |
| 117 | + ctx.getEventBus().fire(new PreparedStatementRemovalEvent(queryId)); |
| 118 | + LOG.error("PreparedStatementRemovalEvent fired successfully"); |
| 119 | + }); |
| 120 | + } else { |
| 121 | + LOG.error("Skipping removal event - future not in valid state for extraction"); |
| 122 | + } |
| 123 | + } catch (Exception e) { |
| 124 | + LOG.error("Unable to register removal handler", e); |
| 125 | + } |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + public TestCqlPrepareAsyncProcessor(@NonNull Optional<DefaultDriverContext> context) { |
| 130 | + // Default CqlPrepareAsyncProcessor uses weak values here as well. We avoid doing so |
| 131 | + // to prevent cache entries from unexpectedly disappearing mid-test. |
| 132 | + super(context, builder -> builder.removalListener(buildCacheRemoveCallback(context))); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** Customized DefaultDriverContext that uses TestCqlPrepareAsyncProcessor. */ |
| 137 | + protected static class TestDefaultDriverContext extends DefaultDriverContext { |
| 138 | + public TestDefaultDriverContext( |
| 139 | + DriverConfigLoader configLoader, ProgrammaticArguments programmaticArguments) { |
| 140 | + super(configLoader, programmaticArguments); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + protected RequestProcessorRegistry buildRequestProcessorRegistry() { |
| 145 | + // Re-create the processor cache to insert the TestCqlPrepareAsyncProcessor with it's strong |
| 146 | + // prepared statement cache, see JAVA-3062 |
| 147 | + List<RequestProcessor<?, ?>> processors = |
| 148 | + BuiltInRequestProcessors.createDefaultProcessors(this); |
| 149 | + processors.removeIf((processor) -> processor instanceof CqlPrepareAsyncProcessor); |
| 150 | + processors.removeIf((processor) -> processor instanceof CqlPrepareSyncProcessor); |
| 151 | + CqlPrepareAsyncProcessor asyncProcessor = new TestCqlPrepareAsyncProcessor(Optional.of(this)); |
| 152 | + processors.add(2, asyncProcessor); |
| 153 | + processors.add(3, new CqlPrepareSyncProcessor(asyncProcessor)); |
| 154 | + return new RequestProcessorRegistry( |
| 155 | + getSessionName(), processors.toArray(new RequestProcessor[0])); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + /** Customized SessionBuilder that uses TestDefaultDriverContext. */ |
| 160 | + protected static class TestSessionBuilder extends SessionBuilder { |
| 161 | + |
| 162 | + @Override |
| 163 | + protected Object wrap(@NonNull CqlSession defaultSession) { |
| 164 | + return defaultSession; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + protected DriverContext buildContext( |
| 169 | + DriverConfigLoader configLoader, ProgrammaticArguments programmaticArguments) { |
| 170 | + return new TestDefaultDriverContext(configLoader, programmaticArguments); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + @BeforeClass |
| 175 | + public static void setup() { |
| 176 | + System.setProperty( |
| 177 | + SessionUtils.SESSION_BUILDER_CLASS_PROPERTY, PreparedStatementITBase.class.getName()); |
| 178 | + } |
| 179 | + |
| 180 | + @AfterClass |
| 181 | + public static void teardown() { |
| 182 | + System.clearProperty(SessionUtils.SESSION_BUILDER_CLASS_PROPERTY); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Factory method for creating the custom session builder. This method is called by SessionUtils |
| 187 | + * via reflection. |
| 188 | + */ |
| 189 | + public static SessionBuilder builder() { |
| 190 | + return new TestSessionBuilder(); |
| 191 | + } |
| 192 | +} |
0 commit comments