-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
graph: remove redundant iteration through a node's persisted channels #9496
Conversation
The AddNode method on the GraphCache calls `AddNodeFeatures` underneath and then iterates through all the node's persisted channels and adds them to the cache too via `AddChannel`. This is, however, not required since at the time the cache is populated in `NewChannelGraph`, the cache is populated will all persisted nodes and all persisted channels. Then, once any new channels come in, via `AddChannelEdge`, they are added to the cache via AddChannel. If any new nodes come in via `AddLightningNode`, then currently the cache's AddNode method is called which the both adds the node and again iterates through all persisted channels and re-adds them to the cache. This is definitely redundent since the initial cache population and updates via AddChannelEdge should keep the cache fresh in terms of channels. So we remove this for 2 reasons: 1) to remove the redundent DB calls and 2) this requires a kvdb.RTx to be passed in to the GraphCache calls which will make it hard to extract the cache out of the CRUD layer and be used more generally. The AddNode method made sense when the cache was first added in the code-base [here](lightningnetwork@369c09b#diff-ae36bdb6670644d20c4e43f3a0ed47f71886c2bcdf3cc2937de24315da5dc072R213) since then during graph cache population, nodes and channels would be added to the cache in a single DB transaction. This was, however, changed [later on](lightningnetwork@352008a) to be done in 2 separate DB calls for efficiency reasons.
Important Review skippedAuto reviews are limited to specific labels. 🏷️ Labels to auto review (1)
Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
(will fix linter issue after review round) |
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.
Straightforward PR! Just need to fix the linter otherwise LGTM🦾
With the previous commit, the AddNode method was removed and since that was the only method making use of the ForEachChannel on the GraphCacheNode interface, we can remove that method. Since the only two methods left just expose the node's pub key and features, it really is not required anymore and so the entire thing can be removed along with the implementation of it.
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.
LGTM 🍫
@@ -772,8 +772,8 @@ func (c *ChannelGraph) forEachNode( | |||
// graph, executing the passed callback with each node encountered. If the | |||
// callback returns an error, then the transaction is aborted and the iteration | |||
// stops early. | |||
func (c *ChannelGraph) ForEachNodeCacheable(cb func(kvdb.RTx, | |||
GraphCacheNode) error) error { | |||
func (c *ChannelGraph) ForEachNodeCacheable(cb func(route.Vertex, |
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.
nit: I think the Cacheable
suffix doesn't make much sense anymore since there's no more graphCacheNode
. I'd suggest we simplify naming a bit too. wdyt?
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 think it may still be appropriate given that this only passes to the call back the info that we need for the cache. So this is different from ForEachNode
which passes back a full models.LightningNode
.
Thoughts?
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.
🚢
Today, each time the
GraphCache
sAddNode
method is called, the DB is consulted and the node'sknown channels are iterated over and re-added to the cache. While this design made sense at the time
that the cache was added, it no longer is required with the updates to the architecture that have taken
place since.
This change is part of #9494 as this removal of
AddNode
means that we no longer need to pass a
kvdb.RTx
to thegraphCache
methods and so it will beeasier to move the cache out of the CRUD layer in an upcoming PR.
More context:
The AddNode method on the GraphCache calls
AddNodeFeatures
underneathand then iterates through all the node's persisted channels and adds
them to the cache too via
AddChannel
.This is, however, not required since at the time the cache is populated
in
NewChannelGraph
, the cache is populated will all persisted nodesand all persisted channels. Then, once any new channels come in, via
AddChannelEdge
, they are added to the cache via AddChannel. If any newnodes come in via
AddLightningNode
, then currently the cache's AddNodemethod is called which the both adds the node and again iterates through
all persisted channels and re-adds them to the cache. This is definitely
redundent since the initial cache population and updates via
AddChannelEdge should keep the cache fresh in terms of channels.
So we remove this for 2 reasons: 1) to remove the redundent DB calls and
2) this requires a kvdb.RTx to be passed in to the GraphCache calls
which will make it hard to extract the cache out of the CRUD layer
and be used more generally.
The AddNode method made sense when the cache was first added in the
code-base here since then during graph cache population, nodes and channels
would be added to the cache in a single DB transaction. This was, however,
changed later on to be done in 2 separate DB calls for efficiency reasons.