/
database.go
225 lines (191 loc) · 6.21 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Package database implements a database backed storage backend.
package database
import (
"context"
"errors"
"fmt"
"io"
"path/filepath"
"github.com/oasisprotocol/oasis-core/go/common/crypto/hash"
"github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
"github.com/oasisprotocol/oasis-core/go/storage/api"
"github.com/oasisprotocol/oasis-core/go/storage/mkvs/checkpoint"
nodedb "github.com/oasisprotocol/oasis-core/go/storage/mkvs/db/api"
badgerNodedb "github.com/oasisprotocol/oasis-core/go/storage/mkvs/db/badger"
)
const (
// BackendNameBadgerDB is the name of the BadgeDB backed database backend.
BackendNameBadgerDB = "badger"
// DBFileBadgerDB is the default BadgerDB backing store filename.
DBFileBadgerDB = "mkvs_storage.badger.db"
checkpointDir = "checkpoints"
)
// DefaultFileName returns the default database filename for the specified
// backend.
func DefaultFileName(backend string) string {
switch backend {
case BackendNameBadgerDB:
return DBFileBadgerDB
default:
panic("storage/database: can't get default filename for unknown backend")
}
}
type databaseBackend struct {
nodedb nodedb.NodeDB
checkpointer checkpoint.CreateRestorer
rootCache *api.RootCache
signer signature.Signer
initCh chan struct{}
readOnly bool
}
// New constructs a new database backed storage Backend instance.
func New(cfg *api.Config) (api.Backend, error) {
ndbCfg := cfg.ToNodeDB()
var (
ndb nodedb.NodeDB
err error
)
switch cfg.Backend {
case BackendNameBadgerDB:
ndb, err = badgerNodedb.New(ndbCfg)
default:
err = errors.New("storage/database: unsupported backend")
}
if err != nil {
return nil, fmt.Errorf("storage/database: failed to create node database: %w", err)
}
rootCache, err := api.NewRootCache(ndb, nil, cfg.ApplyLockLRUSlots, cfg.InsecureSkipChecks)
if err != nil {
ndb.Close()
return nil, fmt.Errorf("storage/database: failed to create root cache: %w", err)
}
// Satisfy the interface.
initCh := make(chan struct{})
close(initCh)
// Create the checkpointer.
creator, err := checkpoint.NewFileCreator(filepath.Join(cfg.DB, checkpointDir), ndb)
if err != nil {
ndb.Close()
return nil, fmt.Errorf("storage/database: failed to create checkpoint creator: %w", err)
}
restorer, err := checkpoint.NewRestorer(ndb)
if err != nil {
ndb.Close()
return nil, fmt.Errorf("storage/database: failed to create checkpoint restorer: %w", err)
}
return &databaseBackend{
nodedb: ndb,
checkpointer: checkpoint.NewCreateRestorer(creator, restorer),
rootCache: rootCache,
signer: cfg.Signer,
initCh: initCh,
readOnly: cfg.ReadOnly,
}, nil
}
func (ba *databaseBackend) Apply(ctx context.Context, request *api.ApplyRequest) ([]*api.Receipt, error) {
if ba.readOnly {
return nil, fmt.Errorf("storage/database: failed to Apply: %w", api.ErrReadOnly)
}
oldRoot := api.Root{
Namespace: request.Namespace,
Version: request.SrcRound,
Type: request.RootType,
Hash: request.SrcRoot,
}
expectedNewRoot := api.Root{
Namespace: request.Namespace,
Version: request.DstRound,
Type: request.RootType,
Hash: request.DstRoot,
}
newRoot, err := ba.rootCache.Apply(
ctx,
oldRoot,
expectedNewRoot,
request.WriteLog,
)
if err != nil {
return nil, fmt.Errorf("storage/database: failed to Apply: %w", err)
}
receipt, err := api.SignReceipt(ba.signer, request.Namespace, request.DstRound, []api.RootType{request.RootType}, []hash.Hash{*newRoot})
return []*api.Receipt{receipt}, err
}
func (ba *databaseBackend) ApplyBatch(ctx context.Context, request *api.ApplyBatchRequest) ([]*api.Receipt, error) {
if ba.readOnly {
return nil, fmt.Errorf("storage/database: failed to ApplyBatch: %w", api.ErrReadOnly)
}
newRoots := make([]hash.Hash, 0, len(request.Ops))
newTypes := make([]api.RootType, 0, len(request.Ops))
for _, op := range request.Ops {
oldRoot := api.Root{
Namespace: request.Namespace,
Version: op.SrcRound,
Type: op.RootType,
Hash: op.SrcRoot,
}
expectedNewRoot := api.Root{
Namespace: request.Namespace,
Version: request.DstRound,
Type: op.RootType,
Hash: op.DstRoot,
}
newRoot, err := ba.rootCache.Apply(
ctx,
oldRoot,
expectedNewRoot,
op.WriteLog,
)
if err != nil {
return nil, fmt.Errorf("storage/database: failed to Apply, op: %w", err)
}
newRoots = append(newRoots, *newRoot)
newTypes = append(newTypes, op.RootType)
}
receipt, err := api.SignReceipt(ba.signer, request.Namespace, request.DstRound, newTypes, newRoots)
return []*api.Receipt{receipt}, err
}
func (ba *databaseBackend) Cleanup() {
ba.nodedb.Close()
}
func (ba *databaseBackend) Initialized() <-chan struct{} {
return ba.initCh
}
func (ba *databaseBackend) SyncGet(ctx context.Context, request *api.GetRequest) (*api.ProofResponse, error) {
tree, err := ba.rootCache.GetTree(ctx, request.Tree.Root)
if err != nil {
return nil, err
}
defer tree.Close()
return tree.SyncGet(ctx, request)
}
func (ba *databaseBackend) SyncGetPrefixes(ctx context.Context, request *api.GetPrefixesRequest) (*api.ProofResponse, error) {
tree, err := ba.rootCache.GetTree(ctx, request.Tree.Root)
if err != nil {
return nil, err
}
defer tree.Close()
return tree.SyncGetPrefixes(ctx, request)
}
func (ba *databaseBackend) SyncIterate(ctx context.Context, request *api.IterateRequest) (*api.ProofResponse, error) {
tree, err := ba.rootCache.GetTree(ctx, request.Tree.Root)
if err != nil {
return nil, err
}
defer tree.Close()
return tree.SyncIterate(ctx, request)
}
func (ba *databaseBackend) GetDiff(ctx context.Context, request *api.GetDiffRequest) (api.WriteLogIterator, error) {
return ba.nodedb.GetWriteLog(ctx, request.StartRoot, request.EndRoot)
}
func (ba *databaseBackend) GetCheckpoints(ctx context.Context, request *checkpoint.GetCheckpointsRequest) ([]*checkpoint.Metadata, error) {
return ba.checkpointer.GetCheckpoints(ctx, request)
}
func (ba *databaseBackend) GetCheckpointChunk(ctx context.Context, chunk *checkpoint.ChunkMetadata, w io.Writer) error {
return ba.checkpointer.GetCheckpointChunk(ctx, chunk, w)
}
func (ba *databaseBackend) Checkpointer() checkpoint.CreateRestorer {
return ba.checkpointer
}
func (ba *databaseBackend) NodeDB() nodedb.NodeDB {
return ba.nodedb
}