Skip to content

Commit c086241

Browse files
committed
multi: add Start and Stop methods for ChannelGraph
We do this in preparation for moving channel cache population logic out of the constructor and into the Start method. We also will later on (when topology subscription is moved to the ChannelGraph), have a goroutine that will need to be kicked off and stopped.
1 parent e7eef1e commit c086241

File tree

8 files changed

+75
-0
lines changed

8 files changed

+75
-0
lines changed

autopilot/prefattach_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ func newDiskChanGraph(t *testing.T) (testGraph, error) {
4949
graphDB, err := graphdb.NewChannelGraph(&graphdb.Config{KVDB: backend})
5050
require.NoError(t, err)
5151

52+
require.NoError(t, graphDB.Start())
53+
t.Cleanup(func() {
54+
require.NoError(t, graphDB.Stop())
55+
})
56+
5257
return &testDBGraph{
5358
db: graphDB,
5459
databaseChannelGraph: databaseChannelGraph{

graph/db/graph.go

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

33
import (
44
"errors"
5+
"sync"
6+
"sync/atomic"
57
"time"
68

79
"github.com/btcsuite/btcd/chaincfg/chainhash"
@@ -31,9 +33,15 @@ type Config struct {
3133
// KVStore. Upcoming commits will move the graph cache out of the KVStore and
3234
// into this layer so that the KVStore is only responsible for CRUD operations.
3335
type ChannelGraph struct {
36+
started atomic.Bool
37+
stopped atomic.Bool
38+
3439
graphCache *GraphCache
3540

3641
*KVStore
42+
43+
quit chan struct{}
44+
wg sync.WaitGroup
3745
}
3846

3947
// NewChannelGraph creates a new ChannelGraph instance with the given backend.
@@ -53,6 +61,7 @@ func NewChannelGraph(cfg *Config, options ...ChanGraphOption) (*ChannelGraph,
5361
if !opts.useGraphCache {
5462
return &ChannelGraph{
5563
KVStore: store,
64+
quit: make(chan struct{}),
5665
}, nil
5766
}
5867

@@ -91,9 +100,38 @@ func NewChannelGraph(cfg *Config, options ...ChanGraphOption) (*ChannelGraph,
91100
return &ChannelGraph{
92101
KVStore: store,
93102
graphCache: graphCache,
103+
quit: make(chan struct{}),
94104
}, nil
95105
}
96106

107+
// Start kicks off any goroutines required for the ChannelGraph to function.
108+
// If the graph cache is enabled, then it will be populated with the contents of
109+
// the database.
110+
func (c *ChannelGraph) Start() error {
111+
if !c.started.CompareAndSwap(false, true) {
112+
return nil
113+
}
114+
log.Debugf("ChannelGraph starting")
115+
defer log.Debug("ChannelGraph started")
116+
117+
return nil
118+
}
119+
120+
// Stop signals any active goroutines for a graceful closure.
121+
func (c *ChannelGraph) Stop() error {
122+
if !c.stopped.CompareAndSwap(false, true) {
123+
return nil
124+
}
125+
126+
log.Debugf("ChannelGraph shutting down...")
127+
defer log.Debug("Builder shutdown complete")
128+
129+
close(c.quit)
130+
c.wg.Wait()
131+
132+
return nil
133+
}
134+
97135
// ForEachNodeDirectedChannel iterates through all channels of a given node,
98136
// executing the passed callback on the directed edge representing the channel
99137
// and its incoming policy. If the callback returns an error, then the iteration

graph/db/graph_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4077,6 +4077,10 @@ func TestGraphLoading(t *testing.T) {
40774077

40784078
graph, err := NewChannelGraph(&Config{KVDB: backend})
40794079
require.NoError(t, err)
4080+
require.NoError(t, graph.Start())
4081+
t.Cleanup(func() {
4082+
require.NoError(t, graph.Stop())
4083+
})
40804084

40814085
// Populate the graph with test data.
40824086
const numNodes = 100
@@ -4087,6 +4091,10 @@ func TestGraphLoading(t *testing.T) {
40874091
// populated.
40884092
graphReloaded, err := NewChannelGraph(&Config{KVDB: backend})
40894093
require.NoError(t, err)
4094+
require.NoError(t, graphReloaded.Start())
4095+
t.Cleanup(func() {
4096+
require.NoError(t, graphReloaded.Stop())
4097+
})
40904098

40914099
// Assert that the cache content is identical.
40924100
require.Equal(

graph/db/kv_store.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/lightningnetwork/lnd/kvdb"
2727
"github.com/lightningnetwork/lnd/lnwire"
2828
"github.com/lightningnetwork/lnd/routing/route"
29+
"github.com/stretchr/testify/require"
2930
)
3031

3132
var (
@@ -4714,10 +4715,12 @@ func MakeTestGraph(t testing.TB, modifiers ...KVStoreOptionModifier) (
47144715

47154716
return nil, err
47164717
}
4718+
require.NoError(t, graph.Start())
47174719

47184720
t.Cleanup(func() {
47194721
_ = backend.Close()
47204722
backendCleanup()
4723+
require.NoError(t, graph.Stop())
47214724
})
47224725

47234726
return graph, nil

graph/notifications_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,10 @@ func makeTestGraph(t *testing.T, useCache bool) (*graphdb.ChannelGraph,
11001100
if err != nil {
11011101
return nil, nil, err
11021102
}
1103+
require.NoError(t, graph.Start())
1104+
t.Cleanup(func() {
1105+
require.NoError(t, graph.Stop())
1106+
})
11031107

11041108
return graph, backend, nil
11051109
}

peer/test_utils.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,10 @@ func createTestPeer(t *testing.T) *peerTestCtx {
619619
KVDB: graphBackend,
620620
})
621621
require.NoError(t, err)
622+
require.NoError(t, dbAliceGraph.Start())
623+
t.Cleanup(func() {
624+
require.NoError(t, dbAliceGraph.Stop())
625+
})
622626

623627
dbAliceChannel := channeldb.OpenForTesting(t, dbPath)
624628

routing/pathfind_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ func makeTestGraph(t *testing.T, useCache bool) (*graphdb.ChannelGraph,
173173
if err != nil {
174174
return nil, nil, err
175175
}
176+
require.NoError(t, graph.Start())
177+
t.Cleanup(func() {
178+
require.NoError(t, graph.Stop())
179+
})
176180

177181
return graph, backend, nil
178182
}

server.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,12 @@ func (s *server) Start() error {
22872287
return
22882288
}
22892289

2290+
cleanup = cleanup.add(s.graphDB.Stop)
2291+
if err := s.graphDB.Start(); err != nil {
2292+
startErr = err
2293+
return
2294+
}
2295+
22902296
cleanup = cleanup.add(s.graphBuilder.Stop)
22912297
if err := s.graphBuilder.Start(); err != nil {
22922298
startErr = err
@@ -2588,6 +2594,9 @@ func (s *server) Stop() error {
25882594
if err := s.graphBuilder.Stop(); err != nil {
25892595
srvrLog.Warnf("failed to stop graphBuilder %v", err)
25902596
}
2597+
if err := s.graphDB.Stop(); err != nil {
2598+
srvrLog.Warnf("failed to stop graphDB %v", err)
2599+
}
25912600
if err := s.chainArb.Stop(); err != nil {
25922601
srvrLog.Warnf("failed to stop chainArb: %v", err)
25932602
}

0 commit comments

Comments
 (0)