-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
111 lines (98 loc) · 2.27 KB
/
node.go
File metadata and controls
111 lines (98 loc) · 2.27 KB
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
package main
import (
"fmt"
"log"
"net"
"os"
"path/filepath"
"time"
"github.com/hashicorp/raft"
pb "github.com/triplewy/simpledb/grpc"
"google.golang.org/grpc"
)
const (
retainSnapshotCount = 2
raftTimeout = 10 * time.Second
)
// Node represents database node
type Node struct {
Config *Config
Server *grpc.Server
store *store
raft *raft.Raft
}
// NewNode creates a node with a gRPC server and database
func NewNode(config *Config) (*Node, error) {
node := new(Node)
node.Config = config
err := node.newStore()
if err != nil {
return nil, err
}
err = node.setupRPC()
if err != nil {
return nil, err
}
err = node.setupRaft()
if err != nil {
return nil, err
}
return node, nil
}
func (node *Node) setupRPC() error {
addr := fmt.Sprintf(":%d", node.Config.rpcPort)
listener, err := net.Listen("tcp", addr)
if err != nil {
return err
}
node.Server = grpc.NewServer()
pb.RegisterSimpleDbServer(node.Server, node)
go func() {
if err := node.Server.Serve(listener); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}()
return nil
}
func (node *Node) setupRaft() error {
// Get node outbound ip
id, err := getOutboundIP()
if err != nil {
return err
}
// Setup Raft configuration.
config := raft.DefaultConfig()
config.LocalID = raft.ServerID(id.String())
// Setup Raft communication.
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", node.Config.raftPort))
if err != nil {
return err
}
transport, err := raft.NewTCPTransport(fmt.Sprintf(":%d", node.Config.raftPort), addr, 3, 10*time.Second, os.Stderr)
if err != nil {
return err
}
// Create the snapshot store. This allows the Raft to truncate the log.
snapshots, err := raft.NewFileSnapshotStore(filepath.Join(node.Config.dataDir, "snapshots"), retainSnapshotCount, os.Stderr)
if err != nil {
return fmt.Errorf("file snapshot store: %s", err)
}
// Create raft node
ra, err := raft.NewRaft(config, node.store, node.store, node.store, snapshots, transport)
if err != nil {
return fmt.Errorf("new raft: %s", err)
}
node.raft = ra
// if enableSingle {
configuration := raft.Configuration{
Servers: []raft.Server{
{
ID: config.LocalID,
Address: transport.LocalAddr(),
},
},
}
node.raft.BootstrapCluster(configuration)
// }
return nil
}