forked from canonical/lxd
/
db.go
520 lines (463 loc) · 14.3 KB
/
db.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
package db
import (
"database/sql"
"fmt"
"sync"
"time"
"github.com/CanonicalLtd/go-grpc-sql"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/lxc/lxd/lxd/db/cluster"
"github.com/lxc/lxd/lxd/db/node"
"github.com/lxc/lxd/lxd/db/query"
"github.com/lxc/lxd/shared/logger"
)
var (
// DbErrAlreadyDefined hapens when the given entry already exists,
// for example a container.
DbErrAlreadyDefined = fmt.Errorf("The container/snapshot already exists")
/* NoSuchObjectError is in the case of joins (and probably other) queries,
* we don't get back sql.ErrNoRows when no rows are returned, even though we do
* on selects without joins. Instead, you can use this error to
* propagate up and generate proper 404s to the client when something
* isn't found so we don't abuse sql.ErrNoRows any more than we
* already do.
*/
NoSuchObjectError = fmt.Errorf("No such object")
Upgrading = fmt.Errorf("The cluster database is upgrading")
)
// Node mediates access to LXD's data stored in the node-local SQLite database.
type Node struct {
db *sql.DB // Handle to the node-local SQLite database file.
dir string // Reference to the directory where the database file lives.
}
// OpenNode creates a new Node object.
//
// The fresh hook parameter is used by the daemon to mark all known patch names
// as applied when a brand new database is created.
//
// The legacyPatches parameter is used as a mean to apply the legacy V10, V11,
// V15, V29 and V30 non-db updates during the database upgrade sequence, to
// avoid any change in semantics wrt the old logic (see PR #3322).
//
// Return the newly created Node object, and a Dump of the pre-clustering data
// if we've migrating to a cluster-aware version.
func OpenNode(dir string, fresh func(*Node) error, legacyPatches map[int]*LegacyPatch) (*Node, *Dump, error) {
// When updating the node database schema we'll detect if we're
// transitioning to the dqlite-based database and dump all the data
// before purging the schema. This data will be then imported by the
// daemon into the dqlite database.
var dump *Dump
db, err := node.Open(dir)
if err != nil {
return nil, nil, err
}
legacyHook := legacyPatchHook(db, legacyPatches)
hook := func(version int, tx *sql.Tx) error {
if version == node.UpdateFromPreClustering {
logger.Debug("Loading pre-clustering sqlite data")
var err error
dump, err = LoadPreClusteringData(tx)
if err != nil {
return err
}
}
return legacyHook(version, tx)
}
initial, err := node.EnsureSchema(db, dir, hook)
if err != nil {
return nil, nil, err
}
node := &Node{
db: db,
dir: dir,
}
if initial == 0 {
if fresh != nil {
err := fresh(node)
if err != nil {
return nil, nil, err
}
}
}
return node, dump, nil
}
// ForLegacyPatches is a aid for the hack in initializeDbObject, which sets
// the db-related Deamon attributes upfront, to be backward compatible with the
// legacy patches that need to interact with the database.
func ForLegacyPatches(db *sql.DB) *Node {
return &Node{db: db}
}
// DB returns the low level database handle to the node-local SQLite
// database.
//
// FIXME: this is used for compatibility with some legacy code, and should be
// dropped once there are no call sites left.
func (n *Node) DB() *sql.DB {
return n.db
}
// Dir returns the directory of the underlying SQLite database file.
func (n *Node) Dir() string {
return n.dir
}
// Transaction creates a new NodeTx object and transactionally executes the
// node-level database interactions invoked by the given function. If the
// function returns no error, all database changes are committed to the
// node-level database, otherwise they are rolled back.
func (n *Node) Transaction(f func(*NodeTx) error) error {
nodeTx := &NodeTx{}
return query.Transaction(n.db, func(tx *sql.Tx) error {
nodeTx.tx = tx
return f(nodeTx)
})
}
// Close the database facade.
func (n *Node) Close() error {
return n.db.Close()
}
// Begin a new transaction against the local database. Legacy method.
func (n *Node) Begin() (*sql.Tx, error) {
return begin(n.db)
}
// Cluster mediates access to LXD's data stored in the cluster dqlite database.
type Cluster struct {
db *sql.DB // Handle to the cluster dqlite database, gated behind gRPC SQL.
nodeID int64 // Node ID of this LXD instance.
mu sync.RWMutex
}
// OpenCluster creates a new Cluster object for interacting with the dqlite
// database.
//
// - name: Basename of the database file holding the data. Typically "db.bin".
// - dialer: Function used to connect to the dqlite backend via gRPC SQL.
// - address: Network address of this node (or empty string).
// - api: Number of API extensions that this node supports.
//
// The address and api parameters will be used to determine if the cluster
// database matches our version, and possibly trigger a schema update. If the
// schema update can't be performed right now, because some nodes are still
// behind, an Upgrading error is returned.
func OpenCluster(name string, dialer grpcsql.Dialer, address string) (*Cluster, error) {
db, err := cluster.Open(name, dialer)
if err != nil {
return nil, errors.Wrap(err, "failed to open database")
}
// Test that the cluster database is operational. We wait up to 10
// minutes, in case there's no quorum of nodes online yet.
timeout := time.After(10 * time.Minute)
for {
err = db.Ping()
if err == nil {
break
}
cause := errors.Cause(err)
if cause != context.DeadlineExceeded {
return nil, err
}
time.Sleep(10 * time.Second)
select {
case <-timeout:
return nil, fmt.Errorf("failed to connect to cluster database")
default:
}
}
nodesVersionsMatch, err := cluster.EnsureSchema(db, address)
if err != nil {
return nil, errors.Wrap(err, "failed to ensure schema")
}
cluster := &Cluster{
db: db,
}
db.SetMaxOpenConns(1)
// Figure out the ID of this node.
err = cluster.Transaction(func(tx *ClusterTx) error {
nodes, err := tx.Nodes()
if err != nil {
return errors.Wrap(err, "failed to fetch nodes")
}
if len(nodes) == 1 && nodes[0].Address == "0.0.0.0" {
// We're not clustered
cluster.NodeID(1)
return nil
}
for _, node := range nodes {
if node.Address == address {
cluster.nodeID = node.ID
return nil
}
}
return fmt.Errorf("no node registered with address %s", address)
})
if err != nil {
return nil, err
}
if !nodesVersionsMatch {
err = ErrSomeNodesAreBehind
}
return cluster, err
}
// ErrSomeNodesAreBehind is returned by OpenCluster if some of the nodes in the
// cluster have a schema or API version that is less recent than this node.
var ErrSomeNodesAreBehind = fmt.Errorf("some nodes are behind this node's version")
// ForLocalInspection is a aid for the hack in initializeDbObject, which
// sets the db-related Deamon attributes upfront, to be backward compatible
// with the legacy patches that need to interact with the database.
func ForLocalInspection(db *sql.DB) *Cluster {
return &Cluster{db: db}
}
// Transaction creates a new ClusterTx object and transactionally executes the
// cluster database interactions invoked by the given function. If the function
// returns no error, all database changes are committed to the cluster database
// database, otherwise they are rolled back.
//
// If EnterExclusive has been called before, calling Transaction will block
// until ExitExclusive has been called as well to release the lock.
func (c *Cluster) Transaction(f func(*ClusterTx) error) error {
c.mu.RLock()
defer c.mu.RUnlock()
return c.transaction(f)
}
// EnterExclusive acquires a lock on the cluster db, so any successive call to
// Transaction will block until ExitExclusive has been called.
func (c *Cluster) EnterExclusive() error {
logger.Debug("Acquiring exclusive lock on cluster db")
ch := make(chan struct{})
go func() {
c.mu.Lock()
ch <- struct{}{}
}()
timeout := 20 * time.Second
select {
case <-ch:
return nil
case <-time.After(timeout):
return fmt.Errorf("timeout (%s)", timeout)
}
}
// ExitExclusive runs the given transaction and then releases the lock acquired
// with EnterExclusive.
func (c *Cluster) ExitExclusive(f func(*ClusterTx) error) error {
logger.Debug("Releasing exclusive lock on cluster db")
defer c.mu.Unlock()
return c.transaction(f)
}
func (c *Cluster) transaction(f func(*ClusterTx) error) error {
clusterTx := &ClusterTx{
nodeID: c.nodeID,
}
return query.Retry(func() error {
return query.Transaction(c.db, func(tx *sql.Tx) error {
clusterTx.tx = tx
return f(clusterTx)
})
})
}
// NodeID sets the the node NodeID associated with this cluster instance. It's used for
// backward-compatibility of all db-related APIs that were written before
// clustering and don't accept a node NodeID, so in those cases we automatically
// use this value as implicit node NodeID.
func (c *Cluster) NodeID(id int64) {
c.nodeID = id
}
// Close the database facade.
func (c *Cluster) Close() error {
return c.db.Close()
}
// DB returns the low level database handle to the cluster database.
//
// FIXME: this is used for compatibility with some legacy code, and should be
// dropped once there are no call sites left.
func (c *Cluster) DB() *sql.DB {
return c.db
}
// Begin a new transaction against the cluster database.
//
// FIXME: legacy method.
func (c *Cluster) Begin() (*sql.Tx, error) {
return begin(c.db)
}
// UpdateSchemasDotGo updates the schema.go files in the local/ and cluster/
// sub-packages.
func UpdateSchemasDotGo() error {
err := node.SchemaDotGo()
if err != nil {
return fmt.Errorf("failed to update node schema.go: %v", err)
}
err = cluster.SchemaDotGo()
if err != nil {
return fmt.Errorf("failed to update cluster schema.go: %v", err)
}
return nil
}
func isNoMatchError(err error) bool {
if err == nil {
return false
}
if err.Error() == "sql: no rows in result set" {
return true
}
return false
}
func begin(db *sql.DB) (*sql.Tx, error) {
for i := 0; i < 1000; i++ {
tx, err := db.Begin()
if err == nil {
return tx, nil
}
if !query.IsRetriableError(err) {
logger.Debugf("DbBegin: error %q", err)
return nil, err
}
time.Sleep(30 * time.Millisecond)
}
logger.Debugf("DbBegin: DB still locked")
logger.Debugf(logger.GetStack())
return nil, fmt.Errorf("DB is locked")
}
func TxCommit(tx *sql.Tx) error {
for i := 0; i < 1000; i++ {
err := tx.Commit()
if err == nil || err == sql.ErrTxDone { // Ignore duplicate commits/rollbacks
return nil
}
if !query.IsRetriableError(err) {
logger.Debugf("Txcommit: error %q", err)
return err
}
time.Sleep(30 * time.Millisecond)
}
logger.Debugf("Txcommit: db still locked")
logger.Debugf(logger.GetStack())
return fmt.Errorf("DB is locked")
}
func dbQueryRowScan(db *sql.DB, q string, args []interface{}, outargs []interface{}) error {
for i := 0; i < 1000; i++ {
err := db.QueryRow(q, args...).Scan(outargs...)
if err == nil {
return nil
}
if isNoMatchError(err) {
return err
}
if !query.IsRetriableError(err) {
return err
}
time.Sleep(30 * time.Millisecond)
}
logger.Debugf("DbQueryRowScan: query %q args %q, DB still locked", q, args)
logger.Debugf(logger.GetStack())
return fmt.Errorf("DB is locked")
}
func dbQuery(db *sql.DB, q string, args ...interface{}) (*sql.Rows, error) {
for i := 0; i < 1000; i++ {
result, err := db.Query(q, args...)
if err == nil {
return result, nil
}
if !query.IsRetriableError(err) {
logger.Debugf("DbQuery: query %q error %q", q, err)
return nil, err
}
time.Sleep(30 * time.Millisecond)
}
logger.Debugf("DbQuery: query %q args %q, DB still locked", q, args)
logger.Debugf(logger.GetStack())
return nil, fmt.Errorf("DB is locked")
}
func doDbQueryScan(qi queryer, q string, args []interface{}, outargs []interface{}) ([][]interface{}, error) {
rows, err := qi.Query(q, args...)
if err != nil {
return [][]interface{}{}, err
}
defer rows.Close()
result := [][]interface{}{}
for rows.Next() {
ptrargs := make([]interface{}, len(outargs))
for i := range outargs {
switch t := outargs[i].(type) {
case string:
str := ""
ptrargs[i] = &str
case int:
integer := 0
ptrargs[i] = &integer
case int64:
integer := int64(0)
ptrargs[i] = &integer
default:
return [][]interface{}{}, fmt.Errorf("Bad interface type: %s", t)
}
}
err = rows.Scan(ptrargs...)
if err != nil {
return [][]interface{}{}, err
}
newargs := make([]interface{}, len(outargs))
for i := range ptrargs {
switch t := outargs[i].(type) {
case string:
newargs[i] = *ptrargs[i].(*string)
case int:
newargs[i] = *ptrargs[i].(*int)
case int64:
newargs[i] = *ptrargs[i].(*int64)
default:
return [][]interface{}{}, fmt.Errorf("Bad interface type: %s", t)
}
}
result = append(result, newargs)
}
err = rows.Err()
if err != nil {
return [][]interface{}{}, err
}
return result, nil
}
type queryer interface {
Query(query string, args ...interface{}) (*sql.Rows, error)
}
/*
* . qi anything implementing the querier interface (i.e. either sql.DB or sql.Tx)
* . q is the database query
* . inargs is an array of interfaces containing the query arguments
* . outfmt is an array of interfaces containing the right types of output
* arguments, i.e.
* var arg1 string
* var arg2 int
* outfmt := {}interface{}{arg1, arg2}
*
* The result will be an array (one per output row) of arrays (one per output argument)
* of interfaces, containing pointers to the actual output arguments.
*/
func queryScan(qi queryer, q string, inargs []interface{}, outfmt []interface{}) ([][]interface{}, error) {
for i := 0; i < 1000; i++ {
result, err := doDbQueryScan(qi, q, inargs, outfmt)
if err == nil {
return result, nil
}
if !query.IsRetriableError(err) {
logger.Debugf("DbQuery: query %q error %q", q, err)
return nil, err
}
time.Sleep(30 * time.Millisecond)
}
logger.Debugf("DbQueryscan: query %q inargs %q, DB still locked", q, inargs)
logger.Debugf(logger.GetStack())
return nil, fmt.Errorf("DB is locked")
}
func exec(db *sql.DB, q string, args ...interface{}) (sql.Result, error) {
for i := 0; i < 1000; i++ {
result, err := db.Exec(q, args...)
if err == nil {
return result, nil
}
if !query.IsRetriableError(err) {
logger.Debugf("DbExec: query %q error %q", q, err)
return nil, err
}
time.Sleep(30 * time.Millisecond)
}
logger.Debugf("DbExec: query %q args %q, DB still locked", q, args)
logger.Debugf(logger.GetStack())
return nil, fmt.Errorf("DB is locked")
}