Skip to content

Commit b9f8a9c

Browse files
committed
graph/db: populate the graph cache in Start instead of during construction
In this commit, we move the graph cache population logic out of the ChannelGraph constructor and into its Start method instead.
1 parent c086241 commit b9f8a9c

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

graph/db/graph.go

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package graphdb
22

33
import (
44
"errors"
5+
"fmt"
56
"sync"
67
"sync/atomic"
78
"time"
@@ -58,50 +59,18 @@ func NewChannelGraph(cfg *Config, options ...ChanGraphOption) (*ChannelGraph,
5859
return nil, err
5960
}
6061

61-
if !opts.useGraphCache {
62-
return &ChannelGraph{
63-
KVStore: store,
64-
quit: make(chan struct{}),
65-
}, nil
62+
g := &ChannelGraph{
63+
KVStore: store,
64+
quit: make(chan struct{}),
6665
}
6766

6867
// The graph cache can be turned off (e.g. for mobile users) for a
6968
// speed/memory usage tradeoff.
70-
graphCache := NewGraphCache(opts.preAllocCacheNumNodes)
71-
startTime := time.Now()
72-
log.Debugf("Populating in-memory channel graph, this might take a " +
73-
"while...")
74-
75-
err = store.ForEachNodeCacheable(func(node route.Vertex,
76-
features *lnwire.FeatureVector) error {
77-
78-
graphCache.AddNodeFeatures(node, features)
79-
80-
return nil
81-
})
82-
if err != nil {
83-
return nil, err
69+
if opts.useGraphCache {
70+
g.graphCache = NewGraphCache(opts.preAllocCacheNumNodes)
8471
}
8572

86-
err = store.ForEachChannel(func(info *models.ChannelEdgeInfo,
87-
policy1, policy2 *models.ChannelEdgePolicy) error {
88-
89-
graphCache.AddChannel(info, policy1, policy2)
90-
91-
return nil
92-
})
93-
if err != nil {
94-
return nil, err
95-
}
96-
97-
log.Debugf("Finished populating in-memory channel graph (took %v, %s)",
98-
time.Since(startTime), graphCache.Stats())
99-
100-
return &ChannelGraph{
101-
KVStore: store,
102-
graphCache: graphCache,
103-
quit: make(chan struct{}),
104-
}, nil
73+
return g, nil
10574
}
10675

10776
// Start kicks off any goroutines required for the ChannelGraph to function.
@@ -114,6 +83,13 @@ func (c *ChannelGraph) Start() error {
11483
log.Debugf("ChannelGraph starting")
11584
defer log.Debug("ChannelGraph started")
11685

86+
if c.graphCache != nil {
87+
if err := c.populateCache(); err != nil {
88+
return fmt.Errorf("could not populate the graph "+
89+
"cache: %w", err)
90+
}
91+
}
92+
11793
return nil
11894
}
11995

@@ -132,6 +108,42 @@ func (c *ChannelGraph) Stop() error {
132108
return nil
133109
}
134110

111+
// populateCache loads the entire channel graph into the in-memory graph cache.
112+
//
113+
// NOTE: This should only be called if the graphCache has been constructed.
114+
func (c *ChannelGraph) populateCache() error {
115+
startTime := time.Now()
116+
log.Debugf("Populating in-memory channel graph, this might take a " +
117+
"while...")
118+
119+
err := c.KVStore.ForEachNodeCacheable(func(node route.Vertex,
120+
features *lnwire.FeatureVector) error {
121+
122+
c.graphCache.AddNodeFeatures(node, features)
123+
124+
return nil
125+
})
126+
if err != nil {
127+
return err
128+
}
129+
130+
err = c.KVStore.ForEachChannel(func(info *models.ChannelEdgeInfo,
131+
policy1, policy2 *models.ChannelEdgePolicy) error {
132+
133+
c.graphCache.AddChannel(info, policy1, policy2)
134+
135+
return nil
136+
})
137+
if err != nil {
138+
return err
139+
}
140+
141+
log.Debugf("Finished populating in-memory channel graph (took %v, %s)",
142+
time.Since(startTime), c.graphCache.Stats())
143+
144+
return nil
145+
}
146+
135147
// ForEachNodeDirectedChannel iterates through all channels of a given node,
136148
// executing the passed callback on the directed edge representing the channel
137149
// and its incoming policy. If the callback returns an error, then the iteration

0 commit comments

Comments
 (0)