Skip to content

graph/db: async graph cache population #10065

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
wants to merge 7 commits into
base: elle-reset-callbacks
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,9 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(

chanGraphOpts := []graphdb.ChanGraphOption{
graphdb.WithUseGraphCache(!cfg.DB.NoGraphCache),
graphdb.WithAsyncGraphCachePopulation(
!cfg.DB.SyncGraphCacheLoad,
),
}

// We want to pre-allocate the channel graph cache according to what we
Expand Down
8 changes: 8 additions & 0 deletions docs/release-notes/release-notes-0.20.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,16 @@ reader of a payment request.
to improve readability and maintainability of the code.

## Breaking Changes

## Performance Improvements

* Let the [channel graph cache be populated
asynchronously](https://github.com/lightningnetwork/lnd/pull/10065) on
startup. While the cache is being populated, the graph is still available for
queries, but all read queries will be served from the database until the cache
is fully populated. This new behaviour can be opted out of via the new
`--db.sync-graph-cache-load` option.

## Deprecations

### ⚠️ **Warning:** The following RPCs will be removed in release version **0.21**:
Expand Down
42 changes: 33 additions & 9 deletions graph/db/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ type ChannelGraph struct {
started atomic.Bool
stopped atomic.Bool

graphCache *GraphCache
opts *chanGraphOptions

// cacheLoaded is true if the initial graphCache population has
// finished. We use this to ensure that when performing any reads,
// we only read from the graphCache if it has been fully populated.
cacheLoaded atomic.Bool
graphCache *GraphCache

V1Store
*topologyManager
Expand All @@ -50,6 +56,7 @@ func NewChannelGraph(v1Store V1Store,
}

g := &ChannelGraph{
opts: opts,
V1Store: v1Store,
topologyManager: newTopologyManager(),
quit: make(chan struct{}),
Expand All @@ -74,8 +81,19 @@ func (c *ChannelGraph) Start() error {
log.Debugf("ChannelGraph starting")
defer log.Debug("ChannelGraph started")

if c.graphCache != nil {
if err := c.populateCache(context.TODO()); err != nil {
ctx := context.TODO()
if c.opts.asyncGraphCachePopulation {
c.wg.Add(1)
go func() {
defer c.wg.Done()

if err := c.populateCache(ctx); err != nil {
log.Errorf("Could not populate the graph "+
"cache: %v", err)
}
}()
} else {
if err := c.populateCache(ctx); err != nil {
return fmt.Errorf("could not populate the graph "+
"cache: %w", err)
}
Expand Down Expand Up @@ -157,9 +175,13 @@ func (c *ChannelGraph) handleTopologySubscriptions() {
}

// populateCache loads the entire channel graph into the in-memory graph cache.
//
// NOTE: This should only be called if the graphCache has been constructed.
func (c *ChannelGraph) populateCache(ctx context.Context) error {
if c.graphCache == nil {
log.Info("In-memory channel graph cache disabled")

return nil
}

startTime := time.Now()
log.Info("Populating in-memory channel graph, this might take a " +
"while...")
Expand Down Expand Up @@ -188,6 +210,8 @@ func (c *ChannelGraph) populateCache(ctx context.Context) error {
return err
}

c.cacheLoaded.Store(true)

log.Infof("Finished populating in-memory channel graph (took %v, %s)",
time.Since(startTime), c.graphCache.Stats())

Expand All @@ -207,7 +231,7 @@ func (c *ChannelGraph) populateCache(ctx context.Context) error {
func (c *ChannelGraph) ForEachNodeDirectedChannel(node route.Vertex,
cb func(channel *DirectedChannel) error, reset func()) error {

if c.graphCache != nil {
if c.graphCache != nil && c.cacheLoaded.Load() {
return c.graphCache.ForEachChannel(node, cb)
}

Expand All @@ -223,7 +247,7 @@ func (c *ChannelGraph) ForEachNodeDirectedChannel(node route.Vertex,
func (c *ChannelGraph) FetchNodeFeatures(node route.Vertex) (
*lnwire.FeatureVector, error) {

if c.graphCache != nil {
if c.graphCache != nil && c.cacheLoaded.Load() {
return c.graphCache.GetFeatures(node), nil
}

Expand All @@ -237,7 +261,7 @@ func (c *ChannelGraph) FetchNodeFeatures(node route.Vertex) (
func (c *ChannelGraph) GraphSession(cb func(graph NodeTraverser) error,
reset func()) error {

if c.graphCache != nil {
if c.graphCache != nil && c.cacheLoaded.Load() {
return cb(c)
}

Expand All @@ -252,7 +276,7 @@ func (c *ChannelGraph) ForEachNodeCached(ctx context.Context,
cb func(node route.Vertex, chans map[uint64]*DirectedChannel) error,
reset func()) error {

if c.graphCache != nil {
if c.graphCache != nil && c.cacheLoaded.Load() {
return c.graphCache.ForEachNode(cb)
}

Expand Down
Loading
Loading