Skip to content

Commit 93a6ab8

Browse files
authored
Merge pull request #9853 from lightningnetwork/elle-graphSQL8-prep
graph/db: init SQLStore caches and batch schedulers
2 parents ab717c3 + 3b44982 commit 93a6ab8

File tree

8 files changed

+77
-25
lines changed

8 files changed

+77
-25
lines changed

config_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
10261026
"instances")
10271027
}
10281028

1029-
graphDBOptions := []graphdb.KVStoreOptionModifier{
1029+
graphDBOptions := []graphdb.StoreOptionModifier{
10301030
graphdb.WithRejectCacheSize(cfg.Caches.RejectCacheSize),
10311031
graphdb.WithChannelCacheSize(cfg.Caches.ChannelCacheSize),
10321032
graphdb.WithBatchCommitInterval(cfg.DB.BatchCommitInterval),

graph/db/graph_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4232,6 +4232,15 @@ func TestGraphLoading(t *testing.T) {
42324232
// Next, create the graph for the first time.
42334233
graphStore := NewTestDB(t)
42344234

4235+
// Temporarily add a manual skip for this test, until all the methods
4236+
// it uses have been implemented on the SQLStore struct. We have to
4237+
// manually add this skip because it is the only test that doesn't use
4238+
// the MakeTestGraph helper to create the graph store.
4239+
_, ok := graphStore.(*KVStore)
4240+
if !ok {
4241+
t.Skipf("Skipping TestGraphLoading for non-bbolt graph store")
4242+
}
4243+
42354244
graph, err := NewChannelGraph(graphStore)
42364245
require.NoError(t, err)
42374246
require.NoError(t, graph.Start())

graph/db/kv_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ var _ V1Store = (*KVStore)(nil)
203203

204204
// NewKVStore allocates a new KVStore backed by a DB instance. The
205205
// returned instance has its own unique reject cache and channel cache.
206-
func NewKVStore(db kvdb.Backend, options ...KVStoreOptionModifier) (*KVStore,
206+
func NewKVStore(db kvdb.Backend, options ...StoreOptionModifier) (*KVStore,
207207
error) {
208208

209209
opts := DefaultOptions()

graph/db/options.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func WithPreAllocCacheNumNodes(n int) ChanGraphOption {
6161
}
6262
}
6363

64-
// KVStoreOptions holds parameters for tuning and customizing a graph.DB.
65-
type KVStoreOptions struct {
64+
// StoreOptions holds parameters for tuning and customizing a graph DB.
65+
type StoreOptions struct {
6666
// RejectCacheSize is the maximum number of rejectCacheEntries to hold
6767
// in the rejection cache.
6868
RejectCacheSize int
@@ -81,37 +81,37 @@ type KVStoreOptions struct {
8181
NoMigration bool
8282
}
8383

84-
// DefaultOptions returns a KVStoreOptions populated with default values.
85-
func DefaultOptions() *KVStoreOptions {
86-
return &KVStoreOptions{
84+
// DefaultOptions returns a StoreOptions populated with default values.
85+
func DefaultOptions() *StoreOptions {
86+
return &StoreOptions{
8787
RejectCacheSize: DefaultRejectCacheSize,
8888
ChannelCacheSize: DefaultChannelCacheSize,
8989
NoMigration: false,
9090
}
9191
}
9292

93-
// KVStoreOptionModifier is a function signature for modifying the default
94-
// KVStoreOptions.
95-
type KVStoreOptionModifier func(*KVStoreOptions)
93+
// StoreOptionModifier is a function signature for modifying the default
94+
// StoreOptions.
95+
type StoreOptionModifier func(*StoreOptions)
9696

9797
// WithRejectCacheSize sets the RejectCacheSize to n.
98-
func WithRejectCacheSize(n int) KVStoreOptionModifier {
99-
return func(o *KVStoreOptions) {
98+
func WithRejectCacheSize(n int) StoreOptionModifier {
99+
return func(o *StoreOptions) {
100100
o.RejectCacheSize = n
101101
}
102102
}
103103

104104
// WithChannelCacheSize sets the ChannelCacheSize to n.
105-
func WithChannelCacheSize(n int) KVStoreOptionModifier {
106-
return func(o *KVStoreOptions) {
105+
func WithChannelCacheSize(n int) StoreOptionModifier {
106+
return func(o *StoreOptions) {
107107
o.ChannelCacheSize = n
108108
}
109109
}
110110

111111
// WithBatchCommitInterval sets the batch commit interval for the interval batch
112112
// schedulers.
113-
func WithBatchCommitInterval(interval time.Duration) KVStoreOptionModifier {
114-
return func(o *KVStoreOptions) {
113+
func WithBatchCommitInterval(interval time.Duration) StoreOptionModifier {
114+
return func(o *StoreOptions) {
115115
o.BatchCommitInterval = interval
116116
}
117117
}

graph/db/sql_store.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package graphdb
22

33
import (
4+
"fmt"
5+
"sync"
6+
7+
"github.com/lightningnetwork/lnd/batch"
48
"github.com/lightningnetwork/lnd/sqldb"
59
)
610

@@ -26,6 +30,16 @@ type BatchedSQLQueries interface {
2630
type SQLStore struct {
2731
db BatchedSQLQueries
2832

33+
// cacheMu guards all caches (rejectCache and chanCache). If
34+
// this mutex will be acquired at the same time as the DB mutex then
35+
// the cacheMu MUST be acquired first to prevent deadlock.
36+
cacheMu sync.RWMutex
37+
rejectCache *rejectCache
38+
chanCache *channelCache
39+
40+
chanScheduler batch.Scheduler[SQLQueries]
41+
nodeScheduler batch.Scheduler[SQLQueries]
42+
2943
// Temporary fall-back to the KVStore so that we can implement the
3044
// interface incrementally.
3145
*KVStore
@@ -37,9 +51,32 @@ var _ V1Store = (*SQLStore)(nil)
3751

3852
// NewSQLStore creates a new SQLStore instance given an open BatchedSQLQueries
3953
// storage backend.
40-
func NewSQLStore(db BatchedSQLQueries, kvStore *KVStore) *SQLStore {
41-
return &SQLStore{
42-
db: db,
43-
KVStore: kvStore,
54+
func NewSQLStore(db BatchedSQLQueries, kvStore *KVStore,
55+
options ...StoreOptionModifier) (*SQLStore, error) {
56+
57+
opts := DefaultOptions()
58+
for _, o := range options {
59+
o(opts)
60+
}
61+
62+
if opts.NoMigration {
63+
return nil, fmt.Errorf("the NoMigration option is not yet " +
64+
"supported for SQL stores")
65+
}
66+
67+
s := &SQLStore{
68+
db: db,
69+
KVStore: kvStore,
70+
rejectCache: newRejectCache(opts.RejectCacheSize),
71+
chanCache: newChannelCache(opts.ChannelCacheSize),
4472
}
73+
74+
s.chanScheduler = batch.NewTimeScheduler(
75+
db, &s.cacheMu, opts.BatchCommitInterval,
76+
)
77+
s.nodeScheduler = batch.NewTimeScheduler(
78+
db, nil, opts.BatchCommitInterval,
79+
)
80+
81+
return s, nil
4582
}

graph/db/test_kvdb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
// NewTestDB is a helper function that creates an BBolt database for testing.
13-
func NewTestDB(t testing.TB) *KVStore {
13+
func NewTestDB(t testing.TB) V1Store {
1414
backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
1515
require.NoError(t, err)
1616

graph/db/test_postgres.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// NewTestDB is a helper function that creates a SQLStore backed by a postgres
1515
// database for testing. At the moment, it embeds a KVStore but once the
1616
// SQLStore fully implements the V1Store interface, the KVStore will be removed.
17-
func NewTestDB(t testing.TB) *SQLStore {
17+
func NewTestDB(t testing.TB) V1Store {
1818
backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
1919
require.NoError(t, err)
2020

@@ -38,5 +38,8 @@ func NewTestDB(t testing.TB) *SQLStore {
3838
},
3939
)
4040

41-
return NewSQLStore(executor, graphStore)
41+
store, err := NewSQLStore(executor, graphStore)
42+
require.NoError(t, err)
43+
44+
return store
4245
}

graph/db/test_sqlite.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
// NewTestDB is a helper function that creates a SQLStore backed by a sqlite
1515
// database for testing. At the moment, it embeds a KVStore but once the
1616
// SQLStore fully implements the V1Store interface, the KVStore will be removed.
17-
func NewTestDB(t testing.TB) *SQLStore {
17+
func NewTestDB(t testing.TB) V1Store {
1818
backend, backendCleanup, err := kvdb.GetTestBackend(t.TempDir(), "cgr")
1919
require.NoError(t, err)
2020

@@ -31,5 +31,8 @@ func NewTestDB(t testing.TB) *SQLStore {
3131
},
3232
)
3333

34-
return NewSQLStore(executor, graphStore)
34+
store, err := NewSQLStore(executor, graphStore)
35+
require.NoError(t, err)
36+
37+
return store
3538
}

0 commit comments

Comments
 (0)