-
Notifications
You must be signed in to change notification settings - Fork 54
Bugfix: replication shouldn't break if mfiles are closed #1792
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cb81cce
Re-register files to replicator when mfiles are closed
spuun bbd4194
Dont close mfiles while full sync is going on
spuun ec127bf
Add specs
spuun 9b7df77
Dont close file during full sync
spuun 743e41e
Add comments to spec code
spuun 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,65 @@ require "file_utils" | |
| require "time" | ||
| require "../src/lavinmq/message_store" | ||
|
|
||
| # Replicator mock to "record" files being registered | ||
| class SpyReplicator | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this needs a comment - what is this and why is it here? |
||
| include LavinMQ::Clustering::Replicator | ||
|
|
||
| getter registered_files = Hash(String, Symbol).new | ||
| getter deleted_files = Set(String).new | ||
|
|
||
| def register_file(path : String) | ||
| @registered_files[path] = :path | ||
| end | ||
|
|
||
| def register_file(file : File) | ||
| @registered_files[file.path] = :file | ||
| end | ||
|
|
||
| def register_file(mfile : MFile) | ||
| @registered_files[mfile.path] = :mfile | ||
| end | ||
|
|
||
| def replace_file(path : String) | ||
| end | ||
|
|
||
| def append(path : String, obj) | ||
| end | ||
|
|
||
| def delete_file(path : String, wg : WaitGroup) | ||
| @deleted_files << path | ||
| end | ||
|
|
||
| def followers : Array(LavinMQ::Clustering::Follower) | ||
| Array(LavinMQ::Clustering::Follower).new | ||
| end | ||
|
|
||
| def syncing_followers : Array(LavinMQ::Clustering::Follower) | ||
| Array(LavinMQ::Clustering::Follower).new | ||
| end | ||
|
|
||
| def all_followers : Array(LavinMQ::Clustering::Follower) | ||
| Array(LavinMQ::Clustering::Follower).new | ||
| end | ||
|
|
||
| def close | ||
| end | ||
|
|
||
| def listen(server : TCPServer) | ||
| end | ||
|
|
||
| def clear | ||
| end | ||
|
|
||
| def password : String | ||
| "" | ||
| end | ||
|
|
||
| def wait_for_sync(& : -> Nil) : Nil | ||
| yield | ||
| end | ||
| end | ||
|
|
||
| def mktmpdir(&) | ||
| path = File.tempname | ||
| Dir.mkdir_p(path) | ||
|
|
@@ -13,9 +72,9 @@ def mktmpdir(&) | |
| end | ||
| end | ||
|
|
||
| def with_store(*, durable = true, &) | ||
| def with_store(*, replicator = nil, durable = true, &) | ||
| mktmpdir do |dir| | ||
| store = LavinMQ::MessageStore.new(dir, nil, durable: durable) | ||
| store = LavinMQ::MessageStore.new(dir, replicator, durable: durable) | ||
| begin | ||
| yield store, dir | ||
| ensure | ||
|
|
@@ -284,4 +343,92 @@ describe LavinMQ::MessageStore do | |
| end | ||
| end | ||
| end | ||
|
|
||
| describe "replication" do | ||
| it "registers the initial segment file" do | ||
| mktmpdir do |dir| | ||
| replicator = SpyReplicator.new | ||
| store = LavinMQ::MessageStore.new(dir, replicator) | ||
| store.close | ||
| replicator.registered_files.keys.map { |p| File.basename(p) }.should contain("msgs.0000000001") | ||
| end | ||
| end | ||
|
|
||
| it "registers existing segment files on startup" do | ||
| mktmpdir do |dir| | ||
| File.write(File.join(dir, "msgs.0000000001"), "\x04\x00\x00\x00") | ||
| replicator = SpyReplicator.new | ||
| store = LavinMQ::MessageStore.new(dir, replicator) | ||
| store.close | ||
| replicator.registered_files.keys.map { |p| File.basename(p) }.should contain("msgs.0000000001") | ||
| end | ||
| end | ||
|
|
||
| it "registers ack files on startup" do | ||
| mktmpdir do |dir| | ||
| File.write(File.join(dir, "msgs.0000000001"), "\x04\x00\x00\x00") | ||
| File.write(File.join(dir, "acks.0000000001"), "") | ||
| replicator = SpyReplicator.new | ||
| store = LavinMQ::MessageStore.new(dir, replicator) | ||
| store.close | ||
| replicator.registered_files.keys.map { |p| File.basename(p) }.should contain("acks.0000000001") | ||
| end | ||
| end | ||
|
|
||
| it "does not register orphaned ack files" do | ||
| mktmpdir do |dir| | ||
| File.write(File.join(dir, "msgs.0000000001"), "\x04\x00\x00\x00") | ||
| File.write(File.join(dir, "acks.0000000002"), "") | ||
| replicator = SpyReplicator.new | ||
| store = LavinMQ::MessageStore.new(dir, replicator) | ||
| store.close | ||
| replicator.registered_files.keys.map { |p| File.basename(p) }.should_not contain("acks.0000000002") | ||
| end | ||
| end | ||
|
|
||
| it "deletes orphaned ack files from the replicator on startup" do | ||
| mktmpdir do |dir| | ||
| File.write(File.join(dir, "msgs.0000000001"), "\x04\x00\x00\x00") | ||
| orphan_path = File.join(dir, "acks.0000000002") | ||
| File.write(orphan_path, "") | ||
| replicator = SpyReplicator.new | ||
| store = LavinMQ::MessageStore.new(dir, replicator) | ||
| store.close | ||
| replicator.deleted_files.should contain(orphan_path) | ||
| end | ||
| end | ||
|
|
||
| it "deletes fully-acked segments from the replicator on startup" do | ||
| mktmpdir do |dir| | ||
| msg_size = LavinMQ::Config.instance.segment_size.to_u64 - (LavinMQ::BytesMessage::MIN_BYTESIZE + 5) | ||
| msg = LavinMQ::Message.new(RoughTime.unix_ms, "e", "k", AMQ::Protocol::Properties.new, msg_size, IO::Memory.new("a" * msg_size)) | ||
|
|
||
| store = LavinMQ::MessageStore.new(dir, nil, durable: true) | ||
| 2.times { store.push(msg) } | ||
| store.close | ||
| wait_for { store.closed } | ||
|
|
||
| File.open(File.join(dir, "acks.0000000001"), "w") do |f| | ||
| f.write_bytes(4u32) | ||
| end | ||
|
|
||
| replicator = SpyReplicator.new | ||
| store = LavinMQ::MessageStore.new(dir, replicator, durable: true) | ||
| store.close | ||
|
|
||
| replicator.deleted_files.map { |p| File.basename(p) }.should contain("msgs.0000000001") | ||
| end | ||
| end | ||
|
|
||
| it "re-registers files without an MFile reference when closed" do | ||
| mktmpdir do |dir| | ||
| replicator = SpyReplicator.new | ||
| store = LavinMQ::MessageStore.new(dir, replicator) | ||
| store.close | ||
| sleep 1.millisecond | ||
| file_registrations = replicator.registered_files.select { |_, t| t == :path } | ||
| file_registrations.keys.map { |p| File.basename(p) }.should contain("msgs.0000000001") | ||
| end | ||
| end | ||
| end | ||
| end | ||
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
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
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.
I'd like a comment here as well, explaining why this runs 2 times and maybe what happens in the loop