-
Notifications
You must be signed in to change notification settings - Fork 2.2k
graphdb: add caching for isPublicNode query #10363
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
base: master
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @Abdulkbk, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a caching mechanism for the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
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.
Code Review
This pull request introduces caching for the IsPublicNode query to address a significant performance bottleneck in gossip message processing. The changes are well-structured, including the addition of a new cache, configuration options, and cache invalidation logic at various points where a node's public status might change. I've identified a potential race condition in one of the cache invalidation paths and an opportunity to optimize the cache-aside pattern to prevent thundering herds. Overall, this is a valuable improvement.
| // Store the result in cache. | ||
| s.cacheMu.Lock() | ||
| _, err = s.publicNodeCache.Put(pubKey, &cachedPublicNode{ | ||
| isPublic: isPublic, | ||
| }) | ||
| if err != nil { | ||
| log.Warnf("unable to store node %x in cache: %v", pubKey, err) | ||
| } | ||
|
|
||
| s.cacheMu.Unlock() | ||
|
|
||
| return isPublic, nil |
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.
There's a potential for a thundering herd problem here. If multiple goroutines call IsPublicNode for the same key that is not in the cache, they will all miss the cache, query the database, and then attempt to write to the cache. To optimize this and prevent redundant database queries, you can re-check the cache after acquiring the write lock. This ensures that only the first goroutine populates the cache, and subsequent ones will use the cached value.
// Store the result in cache. We use a double-checked locking pattern
// here to avoid a thundering herd problem where multiple goroutines
// query the DB for the same key on a cache miss.
s.cacheMu.Lock()
defer s.cacheMu.Unlock()
// Re-check the cache to avoid a race where another goroutine populated
// it between our read-lock release and write-lock acquisition.
if cached, err := s.publicNodeCache.Get(pubKey); err == nil && cached != nil {
return cached.isPublic, nil
}
_, err = s.publicNodeCache.Put(pubKey, &cachedPublicNode{
isPublic: isPublic,
})
if err != nil {
log.Warnf("unable to store node %x in cache: %v", pubKey, err)
}
return isPublic, nil0a81125 to
85b45d5
Compare
|
@Abdulkbk, remember to re-request review from reviewers when ready |
c6c7bbe to
aec3619
Compare
ziggie1984
left a comment
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.
Good idea to cache the call, let's also add some benchmark here.
graph/db/sql_store.go
Outdated
| s.cacheMu.RLock() | ||
| cached, err := s.publicNodeCache.Get(pubKey) | ||
|
|
||
| switch { |
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 find this logic hard to follow can we instead do:
// Cache hit - return immediately.
if err == nil && cached != nil {
return cached.isPublic, nil
}
// Log unexpected errors (anything other than "not found").
if err != nil && !errors.Is(err, cache.ErrElementNotFound) {
log.Warnf("unexpected error checking node cache: %v", err)
}
aec3619 to
cc98da3
Compare
This commit adds the struct we'll use to cache the node. It also adds the require `Size` method for the lru package.
cc98da3 to
8c43ead
Compare
|
I cherry-picked f9078e5 from #10356 adding the benchmark for go test -tags=test_db_sqlite -bench=BenchmarkIsPublicNode -v
The difference is very significant with cache (~ 10000x faster). |
|
I notice the |
f9078e5 to
cf003b0
Compare
|
can you add a release note entry for 20.1 |
graph/db/sql_store.go
Outdated
| type cachedPublicNode struct { | ||
| isPublic bool | ||
| } | ||
| type cachedPublicNode struct{} |
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 don't understand why you remove the isPublic again ?
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.
Ohh. I was supposed to remove it from the commit that added the field so the commit compiles and to make the linter happy. I will fix this.
| default: | ||
| s.rejectCache.remove(edge.ChannelID) | ||
| s.chanCache.remove(edge.ChannelID) | ||
| s.removePublicNodeCache( |
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 don't think we can remove the edge here just because we fail to add the edge, we are dealing with nodes here not channels. I don't acutally think we need to delte them here.
|
|
||
| s.rejectCache.remove(chanID) | ||
| s.chanCache.remove(chanID) | ||
| s.removePublicNodeCache(pubKey1, pubKey2) |
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.
same here, we cannot do this here, they can still have other channels
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'm not sure. At the MarkEdgeZombie callsite, we also remove the channel from graphcache. It makes sense to remove the cache here too, since even if the node has other channels, we can't be sure if they're private or public. It's safer to get that info from the DB after this call.
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.
If the node was ever public the train is passed and we should keep it in the cache. A node either remains private the entire time or remains private the entire time. It does not really sense to switch from public to private.
| s.chanCache.remove(chanID) | ||
| } | ||
|
|
||
| var pubkeys [][33]byte |
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.
no need to delete here, its an LRU cache, if a node was public we don't bother because the pubkey already was annoucned.
| for _, channel := range closedChans { | ||
| s.rejectCache.remove(channel.ChannelID) | ||
| s.chanCache.remove(channel.ChannelID) | ||
| s.removePublicNodeCache( |
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.
not able to delete
| for _, channel := range removedChans { | ||
| s.rejectCache.remove(channel.ChannelID) | ||
| s.chanCache.remove(channel.ChannelID) | ||
| s.removePublicNodeCache( |
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.
same here, cannot b e deleted
| // | ||
| // NOTE: This can safely be called without holding a lock since the lru is | ||
| // thread safe. | ||
| func (s *SQLStore) removePublicNodeCache(pubkeys ...[33]byte) { |
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 don't think we need this if we remove all the callsites
| return fmt.Errorf("unable to delete node: %w", err) | ||
| } | ||
|
|
||
| s.removePublicNodeCache(pubKey) |
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.
even here, I am acutally not sure if we should remove it from the cache, its an LRU cache so it cycles unused values out, so we still might get some infos to this node if the gossip is delayed ?
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.
Shouldn't we be as cautious as possible? Let's assume initially the node is public. After a DeleteNode call, if you call IsPublicNode without a cache, you get false, but if we have a cache that wasn't invalidated, then you get true. Wouldn't that be a discrepancy?
In this commit, we add publicNodeCache into the sqlstore. We also add the necessary config for initializing the cache. Additionally, we introduce a new config `public-node-cache-size` which let us set values for the cache size. Signed-off-by: Abdullahi Yunus <[email protected]>
In this commit, we first check for the node in our cache before querying the database when determining if a node is public or not.
In this commit, we remove nodes from the node cache in various db method call site which execution could affect the public status of the nodes.
In this commit we add a benchmark to test the performance of IsPublicNode query.
ec6a60b to
590351c
Compare
fixes #10337
continues #10356
Change Description
From the issue description:
In this PR, we add caching to the
IsPublicNodemethod in theSQLStore. SinceAdding cache will significantly reduce database overhead and accelerate gossip message processing.
Steps to Test
Steps for reviewers to follow to test the change.