8000 CQ message store: Don't scan for valid messages when deleting by mkuratczyk · Pull Request #13951 · rabbitmq/rabbitmq-server · GitHub
[go: up one dir, main page]

Skip to content

CQ message store: Don't scan for valid messages when deleting #13951

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
wants to merge 1 commit into from

Conversation

mkuratczyk
Copy link
Contributor
@mkuratczyk mkuratczyk commented May 26, 2025

Looking for valid messages before deleting a message store file was an old check for extra safety. However, it is a bottleneck under intense workload: it can take a few seconds to delete a file (a vast majority of that spent scanning for messages), while new files can be created faster than that. That leads to the disk usage growing, even if the number of messages in the queues is not.

We are not aware of any situations where this check actually found valid messages in a file that was requested to be deleted. This is a well tested code path a this point, so let's just remove the extra check.

References #13940 #13233 #13430.

Looking for valid messages before deleting a message store file
was an old check for extra safety. However, it is a bottleneck
under intense workload: it can take a few seconds to delete a file
(a vast majority of that spent scanning for messages), while new
files can be created faster than that. That leads to the disk usage
growing, even if the number of messages in the queues is not.

We are not aware of any situations where this check actually found
valid messages in a file that was requested to be deleted.
This is a well tested code path a this point, so let's just remove
the extra check.
@mkuratczyk mkuratczyk requested a review from lhoguin May 26, 2025 07:54
@mkuratczyk mkuratczyk changed the title Don't scan for valid messages when deleting CQ message store: Don't scan for valid messages when deleting May 26, 2025
@michaelklishin michaelklishin added this to the 4.2.0 milestone May 26, 2025
@@ -2134,7 +2134,6 @@ delete_file(File, State = #gc_state { file_summary_ets = FileSummaryEts,
_ ->
[#file_summary{ valid_total_size = 0,
file_size = FileSize }] = ets:lookup(FileSummaryEts, File),
[] = scan_and_vacuum_message_file(File, State),
Copy link
Contributor

Choose a reason for hiding this comment

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

(this is a discussion that is brought over from #13940 (reply in thread), but I think this PR is a better place)

I wonder which code part calls index_delete for the index entries that point to the deleted file, if this line is removed? (and how there won't be a leak of index entries) (I mean instead of

index_delete_object(IndexEts, Entry),
)

(Loic said its OK, so probably Im missing something)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My explanation is not as good as Loïc's but here is my logic: scan_and_vacuum_message_file executes all actions on the messages found by scan_file_for_valid_messages. The list of those found messages is later returned and matched against []. Therefore, index_delete_object is never really executed here (that we know of) - if it were, we'd see a badmatch for [] = scan_and_vacuum_message_file(File, State) and we agreed that we've never seen it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let me double check, I think there used to be some deletes delayed to this function. Not sure that's true anymore. If it is we can do a quicker ets delete than going over the entire file.

Copy link
Contributor

Choose a reason for hiding this comment

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

You were right, I have provided details in the new review. Thanks!

Copy link
Contributor
@lhoguin lhoguin left a comment

Choose a reason for hiding this comment

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

We must handle the case where entries in the index exist but have a ref_count of 0. Basically perform the equivalent of https://github.com/rabbitmq/rabbitmq-server/pull/13951/files#diff-152a5c9ff6f63d7db77f29ba31364bc77e8ee24164c48499e7831d2b9e4f8a5cR2149-R2150

We don't need to lookup entries, a single ets:match_delete/2 call that matches on file = File and ref_count = 0 should be enough to remove everything in one go. There shouldn't be entries with a positive ref_count after that.

@gomoripeti
Copy link
Contributor

We must handle the case where entries in the index exist but have a ref_count of 0. Basically perform the equivalent of https://github.com/rabbitmq/rabbitmq-server/pull/13951/files#diff-152a5c9ff6f63d7db77f29ba31364bc77e8ee24164c48499e7831d2b9e4f8a5cR2149-R2150

We don't need to lookup entries, a single ets:match_delete/2 call that matches on file = File and ref_count = 0 should be enough to remove everything in one go. There shouldn't be entries with a positive ref_count after that.

I agree there is no need to read anything from the file itself.

But a simple ets:match_delete/2 on the index ETS table is also not good enough. There used to be an ets:select to gather entries but because the key is not the file index it could be slow if there are many entries in the index. (This ETS solution

load_and_vacuum_message_file(File, State) ->
was replaced by the current one in 2955c4e)

But there could be a "secondary index", another ets table that maps from rdq file -> msgId, that could be used for efficient lookup. (This efficient lookup could be used during compaction as well, so the file scanning is only left for dirty recovery)

@lhoguin
Copy link
Contributor
lhoguin commented May 26, 2025

It's not exactly the same. The lookup/delete is done all in ets, and it's only to be used on deletion, not on compaction. I don't know if this will be good enough, benchmarking is necessary, but at least for deletion it should be better than scanning the file.

The problem with a secondary ets table is that you have to write to that table and that slows down other operations. The only real solution is a new file format where the messages in the file are always easy to find (linked list or tree based or similar, basically a format where we have either a message+link to next message location, or just a link to next message location, allowing jumping around the file to get all the entries without having to scan). The problem in the current format being that when we compact, we end up with holes that contain garbage, whereas we would want a link to the next message there. Perhaps that's doable without too many changes to the current format but there's always compatibility issues to think about. That's a heavy change and I'm busy with other things.

For now I'd settle for "better".

@lhoguin
Copy link
Contributor
lhoguin commented May 26, 2025

For what it's worth we don't have to run the match_delete every time a file is deleted, we only need to run it from time to time to clean up the memory index. If it's not good enough when we run it every time, we may run it every N files deleted.

@mkuratczyk
Copy link
Contributor Author

UPDATE: I've implemented a delayed index cleanup but it turned out to be significantly slower (ets:select_delete with a fancy match spec vs the current ets:lookup and delete if needed). Loïc is trying a different approach now.

@mkuratczyk
Copy link
Contributor Author

Yup, this new approach looks much more promising: #13959

My test case so far:

  1. run perf-test -ad false -f persistent -u cq -y 0 -c 1000 -C 10000000 -s 5000; r purge_queue cq
  2. measure the time from when the first file is deleted until the last file is deleted (there are 3107 files created by this test)

On 4.1/main, it takes ~30 seconds for me
This PR + ets:select_delete to clean up the index every 10 files deleted: over 2 minutes
#13959: 9 seconds

I will do a bit more testing

@mkuratczyk
Copy link
Contributor Author

Closing in favour of
#13959
#13967

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

Successfully merging this pull request may close these issues.

4 participants
0