forked from apache/cassandra-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 42
Anchor prepare cache entry via PreparedStatement back-reference #893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nikagra
wants to merge
2
commits into
scylladb:scylla-4.x
Choose a base branch
from
nikagra:fix/prepare-cache-anchor-liveness
base: scylla-4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
core/src/main/java/com/datastax/oss/driver/internal/core/cql/PrepareCacheAnchor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.datastax.oss.driver.internal.core.cql; | ||
|
|
||
| import com.datastax.oss.driver.api.core.cql.PreparedStatement; | ||
| import edu.umd.cs.findbugs.annotations.Nullable; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| /** | ||
| * Internal hook allowing a {@link PreparedStatement} to keep its prepare cache entry reachable. | ||
| * | ||
| * <p>{@link CqlPrepareAsyncProcessor} caches prepare futures with weak values, so an entry can be | ||
| * collected while the application still holds the resulting statement, causing a needless | ||
| * re-PREPARE. Storing the cached future on the statement ties the entry's lifetime to the | ||
| * statement's: the cache holds a weak reference to the future, the future references the statement, | ||
| * and the statement references the future back. The cycle stays reachable while the application | ||
| * holds the statement, and becomes collectible as a whole once it does not. | ||
| * | ||
| * <p>Implementations only need to retain the reference; the anchor is never read back. | ||
| */ | ||
| public interface PrepareCacheAnchor { | ||
|
|
||
| /** | ||
| * Retains the prepare cache entry for this statement, preventing its weak-value eviction for as | ||
| * long as this statement is reachable. | ||
| */ | ||
| void setPrepareCacheAnchor(@Nullable CompletableFuture<PreparedStatement> anchor); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
.../src/test/java/com/datastax/oss/driver/internal/core/cql/PreparedStatementTestHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.datastax.oss.driver.internal.core.cql; | ||
|
|
||
| import com.datastax.oss.driver.api.core.ConsistencyLevel; | ||
| import com.datastax.oss.driver.api.core.DefaultProtocolVersion; | ||
| import com.datastax.oss.driver.api.core.RequestRoutingType; | ||
| import com.datastax.oss.driver.api.core.cql.ColumnDefinitions; | ||
| import com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry; | ||
| import com.datastax.oss.protocol.internal.util.Bytes; | ||
| import java.util.Collections; | ||
|
|
||
| /** Builds minimally-valid {@link DefaultPreparedStatement} instances for tests. */ | ||
| public class PreparedStatementTestHelper { | ||
|
|
||
| /** Returns a statement with no consistency levels and no routing type configured. */ | ||
| public static DefaultPreparedStatement newPreparedStatement() { | ||
| return newPreparedStatement(null, null, null); | ||
| } | ||
|
|
||
| public static DefaultPreparedStatement newPreparedStatement( | ||
| ConsistencyLevel consistencyLevel, | ||
| ConsistencyLevel serialConsistencyLevel, | ||
| RequestRoutingType requestRoutingType) { | ||
| ColumnDefinitions variableDefinitions = | ||
| DefaultColumnDefinitions.valueOf(Collections.emptyList()); | ||
| return new DefaultPreparedStatement( | ||
| Bytes.fromHexString("0x"), | ||
| "SELECT * FROM test.foo WHERE pk = ?", | ||
| variableDefinitions, | ||
| Collections.emptyList(), | ||
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| Collections.emptyMap(), | ||
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| Collections.emptyMap(), | ||
| null, | ||
| null, | ||
| null, | ||
| Integer.MIN_VALUE, | ||
| consistencyLevel, | ||
| serialConsistencyLevel, | ||
| false, | ||
| CodecRegistry.DEFAULT, | ||
| DefaultProtocolVersion.DEFAULT, | ||
| requestRoutingType); | ||
| } | ||
|
|
||
| private PreparedStatementTestHelper() {} | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: scylladb/java-driver
Length of output: 21278
🏁 Script executed:
Repository: scylladb/java-driver
Length of output: 46005
🏁 Script executed:
Repository: scylladb/java-driver
Length of output: 351
Do not require garbage collection within a fixed time.
System.gc()only requests garbage collection. The weak-reference assertion can fail becausecollectGarbage()returns after 500 ms without checking whether collection occurred. Use an eventual condition that retries collection and cache cleanup until the expected state is reached.🤖 Prompt for AI Agents