-
Notifications
You must be signed in to change notification settings - Fork 671
/
manager.go
319 lines (281 loc) · 9.84 KB
/
manager.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package manager
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/leveldb"
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/database/meterdb"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/database/rocksdb"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/version"
)
var (
errNonSortedAndUniqueDBs = errors.New("managed databases were not sorted and unique")
errNoDBs = errors.New("no dbs given")
)
var _ Manager = &manager{}
type Manager interface {
// Current returns the database with the current database version.
Current() *VersionedDatabase
// Previous returns the database prior to the current database and true if a
// previous database exists.
Previous() (*VersionedDatabase, bool)
// GetDatabases returns all the managed databases in order from current to
// the oldest version.
GetDatabases() []*VersionedDatabase
// Close all of the databases controlled by the manager.
Close() error
// NewPrefixDBManager returns a new database manager with each of its
// databases prefixed with [prefix].
NewPrefixDBManager(prefix []byte) Manager
// NewNestedPrefixDBManager returns a new database manager where each of its
// databases has the nested prefix [prefix] applied to it.
NewNestedPrefixDBManager(prefix []byte) Manager
// NewMeterDBManager returns a new database manager with each of its
// databases wrapped with a meterdb instance to support metrics on database
// performance.
NewMeterDBManager(namespace string, registerer prometheus.Registerer) (Manager, error)
// NewCompleteMeterDBManager wraps each database instance with a meterdb
// instance. The namespace is concatenated with the version of the database.
// Note: calling this more than once with the same [namespace] will cause a
// conflict error for the [registerer].
NewCompleteMeterDBManager(namespace string, registerer prometheus.Registerer) (Manager, error)
}
type manager struct {
// databases with the current version at index 0 and prior versions in
// descending order
// invariant: len(databases) > 0
databases []*VersionedDatabase
}
// NewRocksDB creates a database manager of rocksDBs at [filePath] by creating a
// database instance from each directory with a version <= [currentVersion]. If
// [includePreviousVersions], opens previous database versions and includes them
// in the returned Manager.
func NewRocksDB(
dbDirPath string,
log logging.Logger,
currentVersion version.Version,
) (Manager, error) {
return new(
rocksdb.New,
dbDirPath,
log,
currentVersion,
)
}
// NewLevelDB creates a database manager of levelDBs at [filePath] by creating a
// database instance from each directory with a version <= [currentVersion]. If
// [includePreviousVersions], opens previous database versions and includes them
// in the returned Manager.
func NewLevelDB(
dbDirPath string,
log logging.Logger,
currentVersion version.Version,
) (Manager, error) {
return new(
leveldb.New,
dbDirPath,
log,
currentVersion,
)
}
// new creates a database manager at [filePath] by creating a database instance
// from each directory with a version <= [currentVersion]. If
// [includePreviousVersions], opens previous database versions and includes them
// in the returned Manager.
func new(
newDB func(string, logging.Logger) (database.Database, error),
dbDirPath string,
log logging.Logger,
currentVersion version.Version,
) (Manager, error) {
parser := version.NewDefaultParser()
currentDBPath := filepath.Join(dbDirPath, currentVersion.String())
currentDB, err := newDB(currentDBPath, log)
if err != nil {
return nil, fmt.Errorf("couldn't create db at %s: %w", currentDBPath, err)
}
manager := &manager{
databases: []*VersionedDatabase{
{
Database: currentDB,
Version: currentVersion,
},
},
}
// Open old database versions and add them to [manager]
err = filepath.Walk(dbDirPath, func(path string, info os.FileInfo, err error) error {
// the walkFn is called with a non-nil error argument if an os.Lstat
// or Readdirnames call returns an error. Both cases are considered
// fatal in the traversal.
// Reference: https://golang.org/pkg/path/filepath/#WalkFunc
if err != nil {
return err
}
// Skip the root directory
if path == dbDirPath {
return nil
}
// If the database directory contains any files, ignore them.
if !info.IsDir() {
return nil
}
_, dbName := filepath.Split(path)
version, err := parser.Parse(dbName)
if err != nil {
// If the database directory contains any directories that don't
// match the expected version format, ignore them.
return filepath.SkipDir
}
// If [version] is greater than or equal to the specified version
// skip over creating the new database to avoid creating the same db
// twice or creating a database with a version ahead of the desired one.
if cmp := version.Compare(currentVersion); cmp >= 0 {
return filepath.SkipDir
}
db, err := newDB(path, log)
if err != nil {
return fmt.Errorf("couldn't create db at %s: %w", path, err)
}
manager.databases = append(manager.databases, &VersionedDatabase{
Database: db,
Version: version,
})
return filepath.SkipDir
})
SortDescending(manager.databases)
// If an error occurred walking [dbDirPath] close the
// database manager and return the original error here.
if err != nil {
_ = manager.Close()
return nil, err
}
return manager, nil
}
// NewMemDB returns a database manager with a single memdb instance with
// [currentVersion].
func NewMemDB(currentVersion version.Version) Manager {
return &manager{
databases: []*VersionedDatabase{
{
Database: memdb.New(),
Version: currentVersion,
},
},
}
}
// NewManagerFromDBs
func NewManagerFromDBs(dbs []*VersionedDatabase) (Manager, error) {
if len(dbs) == 0 {
return nil, errNoDBs
}
SortDescending(dbs)
sortedAndUnique := utils.IsSortedAndUnique(innerSortDescendingVersionedDBs(dbs))
if !sortedAndUnique {
return nil, errNonSortedAndUniqueDBs
}
return &manager{
databases: dbs,
}, nil
}
func (m *manager) Current() *VersionedDatabase { return m.databases[0] }
func (m *manager) Previous() (*VersionedDatabase, bool) {
if len(m.databases) < 2 {
return nil, false
}
return m.databases[1], true
}
func (m *manager) GetDatabases() []*VersionedDatabase { return m.databases }
func (m *manager) Close() error {
errs := wrappers.Errs{}
for _, db := range m.databases {
errs.Add(db.Close())
}
return errs.Err
}
// NewPrefixDBManager creates a new manager with each database instance prefixed
// by [prefix]
func (m *manager) NewPrefixDBManager(prefix []byte) Manager {
m, _ = m.wrapManager(func(vdb *VersionedDatabase) (*VersionedDatabase, error) {
return &VersionedDatabase{
Database: prefixdb.New(prefix, vdb.Database),
Version: vdb.Version,
}, nil
})
return m
}
// NewNestedPrefixDBManager creates a new manager with each database instance
// wrapped with a nested prfix of [prefix]
func (m *manager) NewNestedPrefixDBManager(prefix []byte) Manager {
m, _ = m.wrapManager(func(vdb *VersionedDatabase) (*VersionedDatabase, error) {
return &VersionedDatabase{
Database: prefixdb.NewNested(prefix, vdb.Database),
Version: vdb.Version,
}, nil
})
return m
}
// NewMeterDBManager wraps the current database instance with a meterdb instance.
// Note: calling this more than once with the same [namespace] will cause a conflict error for the [registerer]
func (m *manager) NewMeterDBManager(namespace string, registerer prometheus.Registerer) (Manager, error) {
currentDB := m.Current()
currentMeterDB, err := meterdb.New(namespace, registerer, currentDB.Database)
if err != nil {
return nil, err
}
newManager := &manager{
databases: make([]*VersionedDatabase, len(m.databases)),
}
copy(newManager.databases[1:], m.databases[1:])
// Overwrite the current database with the meter DB
newManager.databases[0] = &VersionedDatabase{
Database: currentMeterDB,
Version: currentDB.Version,
}
return newManager, nil
}
// NewCompleteMeterDBManager wraps each database instance with a meterdb instance. The namespace
// is concatenated with the version of the database. Note: calling this more than once
// with the same [namespace] will cause a conflict error for the [registerer]
func (m *manager) NewCompleteMeterDBManager(namespace string, registerer prometheus.Registerer) (Manager, error) {
return m.wrapManager(func(vdb *VersionedDatabase) (*VersionedDatabase, error) {
mdb, err := meterdb.New(fmt.Sprintf("%s_%s", namespace, strings.ReplaceAll(vdb.Version.String(), ".", "_")), registerer, vdb.Database)
if err != nil {
return nil, err
}
return &VersionedDatabase{
Database: mdb,
Version: vdb.Version,
}, nil
})
}
// wrapManager returns a new database manager with each managed database wrapped
// by the [wrap] function. If an error is returned by wrap, the error is
// returned immediately. If [wrap] never returns an error, then wrapManager is
// guaranteed to never return an error. The function wrap must return a database
// that can be closed without closing the underlying database.
func (m *manager) wrapManager(wrap func(db *VersionedDatabase) (*VersionedDatabase, error)) (*manager, error) {
newManager := &manager{
databases: make([]*VersionedDatabase, 0, len(m.databases)),
}
for _, db := range m.databases {
wrappedDB, err := wrap(db)
if err != nil {
// ignore additional errors in favor of returning the original error
_ = newManager.Close()
return nil, err
}
newManager.databases = append(newManager.databases, wrappedDB)
}
return newManager, nil
}