-
Notifications
You must be signed in to change notification settings - Fork 3
Rippleitinnz-bloom-filtering-patch1 [DO NOT MERGE] #405
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
rippleitinnz
wants to merge
16
commits into
EvernodeXRPL:main
Choose a base branch
from
rippleitinnz:rippleitinnz-bloom-patch1
base: main
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
16 commits
Select commit
Hold shift + click to select a range
e86b758
Merge pull request #1 from rippleitinnz/rippleitinnz-patch-1
rippleitinnz 0858131
Merge pull request #3 from rippleitinnz/rippleitinnz-patch-3
rippleitinnz 144c7dd
Merge pull request #2 from rippleitinnz/rippleitinnz-patch-2
rippleitinnz d5bfad9
Merge branch 'EvernodeXRPL:main' into main
rippleitinnz 70b44c4
Update rollover_hashset.cpp
rippleitinnz 6c3d428
Update rollover_hashset.hpp
rippleitinnz 7c6c3fe
Update rollover_hashset.hpp
rippleitinnz 7dc4285
Create bloom_filter.hpp
rippleitinnz e56add9
Update peer_comm_session.cpp
rippleitinnz 7277bcb
Update CMakeLists.txt
rippleitinnz 8bd65e3
Update self_node.cpp
rippleitinnz 94d38e2
Update consensus.cpp
rippleitinnz 3fb6913
Update usr.hpp
rippleitinnz 7d9a2ea
Delete src/util/rollover_hashset.cpp
rippleitinnz 6ee6833
Delete src/util/rollover_hashset.hpp
rippleitinnz e48d2df
Update bloom_filter.hpp
rippleitinnz 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
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
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,173 @@ | ||
| #pragma once | ||
|
|
||
| #include <atomic> | ||
| #include <array> | ||
| #include <cstdint> | ||
| #include <functional> | ||
| #include <bit> | ||
| #include <chrono> | ||
|
|
||
| namespace util { | ||
|
|
||
| /** | ||
| * Lock-free Bloom filter implementation. | ||
| * Each filter uses 16MB (134,217,728 bits) with 4 hash functions. | ||
| * Handles ~13 million items at 1% false positive rate. | ||
| */ | ||
| class bloom_filter_impl { | ||
| static constexpr size_t BITS = 134217728; // 16MB = 128M bits | ||
| static constexpr size_t K = 4; // Number of hash functions | ||
| static constexpr size_t BITS_PER_WORD = 64; | ||
| static constexpr size_t NUM_WORDS = BITS / BITS_PER_WORD; | ||
|
|
||
| std::array<std::atomic<uint64_t>, NUM_WORDS> bits; | ||
|
|
||
| // MurmurHash3 mix function | ||
| static uint64_t murmur_mix(uint64_t h) { | ||
| h ^= h >> 33; | ||
| h *= 0xff51afd7ed558ccd; | ||
| h ^= h >> 33; | ||
| h *= 0xc4ceb9fe1a85ec53; | ||
| h ^= h >> 33; | ||
| return h; | ||
| } | ||
|
|
||
| std::array<size_t, K> get_positions(const std::string& data) const { | ||
| std::array<size_t, K> positions; | ||
| uint64_t h1 = std::hash<std::string>{}(data); | ||
| uint64_t h2 = murmur_mix(h1); | ||
|
|
||
| for (size_t i = 0; i < K; ++i) { | ||
| uint64_t hash = h1 + i * h2; | ||
| positions[i] = hash % BITS; | ||
| } | ||
|
|
||
| return positions; | ||
| } | ||
|
|
||
| public: | ||
| bloom_filter_impl() { | ||
| clear(); | ||
| } | ||
|
|
||
| void clear() { | ||
| for (auto& word : bits) { | ||
| word.store(0, std::memory_order_relaxed); | ||
| } | ||
| } | ||
|
|
||
| // Returns true if successfully inserted (was new), false if might already exist | ||
| bool try_insert(const std::string& data) { | ||
| auto positions = get_positions(data); | ||
| bool was_new = false; | ||
|
|
||
| for (size_t pos : positions) { | ||
| size_t word_idx = pos / BITS_PER_WORD; | ||
| size_t bit_idx = pos % BITS_PER_WORD; | ||
| uint64_t mask = uint64_t(1) << bit_idx; | ||
|
|
||
| uint64_t prev = bits[word_idx].fetch_or(mask, std::memory_order_relaxed); | ||
|
|
||
| if ((prev & mask) == 0) { | ||
| was_new = true; | ||
| } | ||
| } | ||
|
|
||
| return was_new; | ||
| } | ||
|
|
||
| // Check if item might exist (read-only) | ||
| bool might_contain(const std::string& data) const { | ||
| auto positions = get_positions(data); | ||
|
|
||
| for (size_t pos : positions) { | ||
| size_t word_idx = pos / BITS_PER_WORD; | ||
| size_t bit_idx = pos % BITS_PER_WORD; | ||
| uint64_t mask = uint64_t(1) << bit_idx; | ||
|
|
||
| if ((bits[word_idx].load(std::memory_order_relaxed) & mask) == 0) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Rolling bloom filter using double buffering. | ||
| * Total size: 32MB (2 x 16MB filters) | ||
| * Automatically rotates filters every 5 minutes. | ||
| */ | ||
| class bloom_filter { | ||
| static constexpr int64_t ROTATION_INTERVAL_MS = 300000; // 5 minutes in milliseconds | ||
|
|
||
| bloom_filter_impl filter1; | ||
| bloom_filter_impl filter2; | ||
| std::atomic<int> active_filter{1}; // 1 or 2 | ||
| std::atomic<int64_t> last_rotation_time; | ||
|
|
||
| int64_t current_time_ms() const { | ||
| return std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| std::chrono::steady_clock::now().time_since_epoch() | ||
| ).count(); | ||
| } | ||
|
|
||
| void check_rotation() { | ||
| int64_t now = current_time_ms(); | ||
| int64_t last_rotation = last_rotation_time.load(std::memory_order_relaxed); | ||
|
|
||
| if (now - last_rotation >= ROTATION_INTERVAL_MS) { | ||
| // Try to update the rotation time atomically | ||
| if (last_rotation_time.compare_exchange_strong(last_rotation, now, | ||
| std::memory_order_relaxed)) { | ||
| // We won the race to rotate | ||
| int current = active_filter.load(std::memory_order_relaxed); | ||
| int next = (current == 1) ? 2 : 1; | ||
|
|
||
| // Clear the filter that will become active next rotation | ||
| if (next == 1) { | ||
| filter1.clear(); | ||
| } else { | ||
| filter2.clear(); | ||
| } | ||
|
|
||
| // Switch active filter | ||
| active_filter.store(next, std::memory_order_relaxed); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public: | ||
| bloom_filter() : last_rotation_time(current_time_ms()) { | ||
| // Both filters start clear | ||
| } | ||
|
|
||
| // Returns true if successfully inserted (was new), false if might already exist | ||
| bool try_emplace(const std::string& data) { | ||
| check_rotation(); | ||
|
|
||
| // Check both filters first | ||
| bool in_filter1 = filter1.might_contain(data); | ||
| bool in_filter2 = filter2.might_contain(data); | ||
|
|
||
| if (in_filter1 || in_filter2) { | ||
| return false; // Already exists | ||
| } | ||
|
|
||
| // Insert into both filters | ||
| filter1.try_insert(data); | ||
| filter2.try_insert(data); | ||
|
|
||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| // Typedef to match rollover_hashset name | ||
| using rollover_hashset = bloom_filter; | ||
|
|
||
| // Global instances for different message types | ||
| inline bloom_filter recent_peermsg_hashes; | ||
| inline bloom_filter recent_selfmsg_hashes; | ||
|
|
||
| } // namespace util |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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.
where's the actual use of the bloom filter to deduplicate? you probably need to run two at the same time so that periodically you can clear one or the other to prevent them filling up and becoming useless
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.
The bloom filter implementation maintains the exact same interface (try_emplace) so no other code changes are needed. The global recent_peermsg_hashes is now defined in bloom_filter.hpp and works exactly the same way.
Will look at running two bloom filters at the same time and will amend