/
raft.go
126 lines (110 loc) · 3.5 KB
/
raft.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
package cluster
import (
"fmt"
"os"
"path/filepath"
dqlite "github.com/CanonicalLtd/go-dqlite"
"github.com/lxc/lxd/lxd/db"
"github.com/lxc/lxd/lxd/node"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/log15"
"github.com/lxc/lxd/shared/logger"
"github.com/pkg/errors"
)
// Create a raft instance and all its dependencies, to be used as backend for
// the dqlite driver running on this LXD node.
//
// If this node should not serve as dqlite node, nil is returned.
//
// The raft instance will use an in-memory transport if clustering is not
// enabled on this node.
//
// The certInfo parameter should contain the cluster TLS keypair and optional
// CA certificate.
//
// The latency parameter is a coarse grain measure of how fast/reliable network
// links are. This is used to tweak the various timeouts parameters of the raft
// algorithm. See the raft.Config structure for more details. A value of 1.0
// means use the default values from hashicorp's raft package. Values closer to
// 0 reduce the values of the various timeouts (useful when running unit tests
// in-memory).
func newRaft(database *db.Node, cert *shared.CertInfo, latency float64) (*raftInstance, error) {
if latency <= 0 {
return nil, fmt.Errorf("latency should be positive")
}
// Figure out if we actually need to act as dqlite node.
var info *db.RaftNode
err := database.Transaction(func(tx *db.NodeTx) error {
var err error
info, err = node.DetermineRaftNode(tx)
return err
})
if err != nil {
return nil, err
}
// If we're not part of the dqlite cluster, there's nothing to do.
if info == nil {
return nil, nil
}
logger.Debug("Start database node", log15.Ctx{"id": info.ID, "address": info.Address})
// Initialize a raft instance along with all needed dependencies.
instance, err := raftInstanceInit(database, info, cert, latency)
if err != nil {
return nil, err
}
return instance, nil
}
// A LXD-specific wrapper around raft.Raft, which also holds a reference to its
// network transport and dqlite FSM.
type raftInstance struct {
info dqlite.ServerInfo
}
// Create a new raftFactory, instantiating all needed raft dependencies.
func raftInstanceInit(
db *db.Node, node *db.RaftNode, cert *shared.CertInfo, latency float64) (*raftInstance, error) {
addr := node.Address
if addr == "" {
// This is a standalone node not exposed to the network.
addr = "1"
}
// Rename legacy data directory if needed.
dir := filepath.Join(db.Dir(), "global")
legacyDir := filepath.Join(db.Dir(), "..", "raft")
if shared.PathExists(legacyDir) {
if shared.PathExists(dir) {
return nil, fmt.Errorf("both legacy and new global database directories exist")
}
logger.Info("Renaming global database directory from raft/ to database/global/")
err := os.Rename(legacyDir, dir)
if err != nil {
return nil, errors.Wrap(err, "failed to rename legacy global database directory")
}
}
// Data directory
if !shared.PathExists(dir) {
err := os.Mkdir(dir, 0750)
if err != nil {
return nil, err
}
}
instance := &raftInstance{}
instance.info.ID = uint64(node.ID)
instance.info.Address = addr
return instance, nil
}
// An address provider that looks up server addresses in the raft_nodes table.
type raftAddressProvider struct {
db *db.Node
}
func (p *raftAddressProvider) ServerAddr(databaseID int) (string, error) {
var address string
err := p.db.Transaction(func(tx *db.NodeTx) error {
var err error
address, err = tx.RaftNodeAddress(int64(databaseID))
return err
})
if err != nil {
return "", err
}
return address, nil
}