Skip to content

4.1: CASSANDRA-19703: Ensure prepared_statement INSERT timestamp precedes eviction DELETE #3917

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

Closed

Conversation

tolbertam
Copy link
Contributor

@tolbertam tolbertam commented Feb 21, 2025

Updates SystemKeyspace.writePreparedStatement to accept a timestamp
associated with the Prepared creation time. Using this timestamp
will ensure that an INSERT into system.prepared_statements will
always precede the timestamp for the same Prepared in
SystemKeyspace.removePreparedStatement.

This is needed because Caffeine 2.9.2 may evict an entry as soon
as it is inserted if the maximum weight of the cache is exceeded
causing the DELETE to be executed before the INSERT.

Additionally, any clusters currently experiencing a leaky
system.prepared_statements table from this bug may struggle to
bounce into a version with this fix as
SystemKeyspace.loadPreparedPreparedStatements currently does
not paginate the query to system.prepared_statements, causing heap
OOMs. To fix this this patch adds pagination at 5000 rows and
aborts loading once the cache size is loaded. This should allow
nodes to come up and delete older prepared statements that may no
longer be used as the cache fills up (which should happen immediately).

This patch does not address the issue of Caffeine immediately evicting
a prepared statement, however it will prevent the
system.prepared_statements table from growing unbounded. For most users
this should be adequate, as the cache should only be filled when there
are erroneously many unique prepared statements. In such a case we can
expect that clients will constantly prepare statements regardless
of whether or not the cache is evicting statements.

patch by Andy Tolbert; reviewed by Berenguer Blasi and Caleb Rackliffe for CASSANDRA-19703

@tolbertam tolbertam changed the base branch from trunk to cassandra-4.1 February 21, 2025 00:03
@tolbertam tolbertam marked this pull request as ready for review February 21, 2025 05:00
@tolbertam tolbertam force-pushed the CASSANDRA-19703-4.1 branch from b4e9ee8 to 927960e Compare April 14, 2025 13:50
@bereng
Copy link
Contributor

bereng commented Apr 15, 2025

IIUC here we're fixing 3 things:

  • Ordering of timestamps preventing the leak
  • Paged reading when preloading to further soften any leaks
  • Stop preloading when cache is already full to prevent trashing the cache upon leaks

@bereng
Copy link
Contributor

bereng commented May 6, 2025

Aren't we missing a test we stop loading pstmnts once cache is full? Maybe you could use byteman to capture the call to the log warn method. That with a modified yaml to have a smaller pstmnts cache somehow should do the trick. I think it's one important feature we're not testing unless I am missing sthg.

@bereng
Copy link
Contributor

bereng commented May 9, 2025

LGTM modulus the 2 minor issues I mentioned last. I would still heavily compact junits but that's just me. We need another C* committer to +1 this now right?

@tolbertam
Copy link
Contributor Author

Pushed some changes which hopefully addresses the remaining feedback, hopefully everything looks ok! I merged in recent changes from 4.1 and ran a full CI run. I'll find a second reviewer tomorrow.

@tolbertam tolbertam force-pushed the CASSANDRA-19703-4.1 branch from 5f2194e to 9c1bb60 Compare May 13, 2025 17:41
@tolbertam tolbertam requested a review from maedhroz May 13, 2025 17:42
@tolbertam
Copy link
Contributor Author

went ahead and rebased and squashed the commits as I had updated from trunk previously which messed with the history a bit. Made no changes and ensured a clean diff before pushing.

CHANGES.txt Outdated
@@ -5,6 +5,7 @@
* Optionally skip exception logging on invalid legacy protocol magic exception (CASSANDRA-19483)
* Fix SimpleClient ability to release acquired capacity (CASSANDRA-20202)
* Fix WaitQueue.Signal.awaitUninterruptibly may block forever if invoking thread is interrupted (CASSANDRA-20084)
* Ensure prepared_statement INSERT timestamp precedes eviction DELETE (CASSANDRA-19703)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think new entries go at the top

// should detect a possible leak and defer paging indefinitely by returning early in preloadPreparedStatements.
int statementsLoadedWhenFull = -1;
long accumulatedSize = 0;
long cacheSize = new DataStorageSpec.LongBytesBound(DatabaseDescriptor.getPreparedStatementsCacheSizeMiB(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use QueryProcessor.PREPARED_STATEMENT_CACHE_SIZE_BYTES instead now that we have a constant for this. will fix.

@bereng
Copy link
Contributor

bereng commented May 14, 2025

LGTM, we need PRs and CI for the other branches now.

@tolbertam
Copy link
Contributor Author

thanks 👍 will go ahead and squash into a single commit, and kick off CI against 4.1/5.0/trunk branches.

@tolbertam tolbertam force-pushed the CASSANDRA-19703-4.1 branch from 29bbb19 to 5e4f087 Compare May 14, 2025 14:34
@tolbertam
Copy link
Contributor Author

tolbertam commented May 14, 2025

squashed with no changes.

@tolbertam tolbertam changed the title CASSANDRA-19703: Ensure prepared_statement INSERT timestamp precedes eviction DELETE 4.1: CASSANDRA-19703: Ensure prepared_statement INSERT timestamp precedes eviction DELETE May 14, 2025
@tolbertam tolbertam force-pushed the CASSANDRA-19703-4.1 branch from 5e4f087 to cf0e58e Compare May 15, 2025 02:20
@bereng
Copy link
Contributor

bereng commented May 15, 2025

I think we need 4.0 as well

@tolbertam
Copy link
Contributor Author

tolbertam commented May 18, 2025

I think we need 4.0 as well

Technically the bug is not likely to be encountered on 4.0 as Caffeine is running an older version that behaves differently, but it wouldn't hurt to do so, will create a branch.

@tolbertam
Copy link
Contributor Author

Added two small changes (c85d156 and 3c80c2e) which I have squashed on the other branches. I have added test results to the jira. Can squash into a single commit assuming everything looks good.

Updates SystemKeyspace.writePreparedStatement to accept a timestamp
associated with the Prepared creation time. Using this timestamp
will ensure that an INSERT into system.prepared_statements will
always precede the timestamp for the same Prepared in
SystemKeyspace.removePreparedStatement.

This is needed because Caffeine 2.9.2 may evict an entry as soon
as it is inserted if the maximum weight of the cache is exceeded
causing the DELETE to be executed before the INSERT.

Additionally, any clusters currently experiencing a leaky
system.prepared_statements table from this bug may struggle to
bounce into a version with this fix as
SystemKeyspace.loadPreparedPreparedStatements currently does
not paginate the query to system.prepared_statements, causing heap
OOMs.  To fix this this patch adds pagination at 5000 rows and
aborts loading once the cache size is loaded. This should allow
nodes to come up and delete older prepared statements that may no
longer be used as the cache fills up (which should happen immediately).

This patch does not address the issue of Caffeine immediately evicting
a prepared statement, however it will prevent the
system.prepared_statements table from growing unbounded.  For most users
this should be adequate, as the cache should only be filled when there
are erroneously many unique prepared statements. In such a case we can
expect that clients will constantly prepare statements regardless
of whether or not the cache is evicting statements.

patch by Andy Tolbert; reviewed by Berenguer Blasi and Caleb Rackliffe for CASSANDRA-19703
@tolbertam tolbertam force-pushed the CASSANDRA-19703-4.1 branch from 3c80c2e to 499847b Compare May 20, 2025 13:11
@tolbertam
Copy link
Contributor Author

squashed with no changes.

@tolbertam tolbertam closed this May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants