-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb_dist_kv.go
171 lines (144 loc) · 4 KB
/
b_dist_kv.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
package main
import (
"encoding/json"
"fmt"
"github.com/hashicorp/raft"
"log"
"net"
"os"
"time"
)
const (
retainSnapshotCount = 2
raftTimeout = 10 * time.Second
)
type command struct {
Op string `json:"op,omitempty"`
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
type KVStore interface {
Get(key string) (string, error)
Set(key, value string) error
Delete(key string) error
Join(nodeID string, addr string) error
}
type RaftKVStore struct {
RaftDir string
RaftBind string
raft *raft.Raft
store StorageEngine
}
var _ KVStore = new(RaftKVStore)
func NewRaftKVStore() *RaftKVStore {
return &RaftKVStore{
store: NewMemStorageEngine(),
}
}
func (s *RaftKVStore) Open(isFirstNode bool, localID string) error {
// Setup Raft configuration.
config := raft.DefaultConfig()
config.LocalID = raft.ServerID(localID)
// Setup Raft communication.
addr, err := net.ResolveTCPAddr("tcp", s.RaftBind)
if err != nil {
return err
}
transport, err := raft.NewTCPTransport(s.RaftBind, addr, 3, 10*time.Second, os.Stderr)
if err != nil {
return err
}
// Create the snapshot store. This allows the Raft to truncate the log.
// Create the log store and stable store.
snapshotStore, err := raft.NewFileSnapshotStore(s.RaftDir, retainSnapshotCount, os.Stderr)
if err != nil {
return fmt.Errorf("file snapshot store: %s", err)
}
var logStore raft.LogStore = raft.NewInmemStore()
var stableStore raft.StableStore = raft.NewInmemStore()
// Instantiate the Raft systems.
ra, err := raft.NewRaft(config, (*fsm)(s), logStore, stableStore, snapshotStore, transport)
if err != nil {
return fmt.Errorf("new raft: %s", err)
}
s.raft = ra
if isFirstNode {
configuration := raft.Configuration{
Servers: []raft.Server{
{
ID: config.LocalID,
Address: transport.LocalAddr(),
},
},
}
ra.BootstrapCluster(configuration)
}
return nil
}
// Get returns the value for the given key from local store.
func (s *RaftKVStore) Get(key string) (string, error) {
return s.store.Get(key)
}
// Set & Delete sets the value for the given key, via distributed consensus.
// Mostly focused on Apply method of fsm
func (s *RaftKVStore) Set(key, value string) error {
if s.raft.State() != raft.Leader {
return fmt.Errorf("not leader")
}
c := &command{
Op: "set",
Key: key,
Value: value,
}
b, err := json.Marshal(c)
if err != nil {
return err
}
f := s.raft.Apply(b, raftTimeout)
return f.Error()
}
func (s *RaftKVStore) Delete(key string) error {
if s.raft.State() != raft.Leader {
return fmt.Errorf("not leader")
}
c := &command{
Op: "delete",
Key: key,
}
b, err := json.Marshal(c)
if err != nil {
return err
}
f := s.raft.Apply(b, raftTimeout)
return f.Error()
}
func (s *RaftKVStore) Join(nodeID string, addr string) error {
log.Printf("received join request for remote node %s at %s", nodeID, addr)
configFuture := s.raft.GetConfiguration()
if err := configFuture.Error(); err != nil {
log.Printf("failed to get raft configuration: %v", err)
return err
}
for _, srv := range configFuture.Configuration().Servers {
// If a node already exists with either the joining node's ID or address,
// that node may need to be removed from the config first.
if srv.ID == raft.ServerID(nodeID) || srv.Address == raft.ServerAddress(addr) {
// However if *both* the ID and the address are the same, then nothing -- not even
// a join operation -- is needed.
if srv.ID == raft.ServerID(nodeID) && srv.Address == raft.ServerAddress(addr) {
log.Printf("node %s at %s already member of cluster, ignoring join request", nodeID, addr)
return nil
}
future := s.raft.RemoveServer(srv.ID, 0, 0)
if err := future.Error(); err != nil {
return fmt.Errorf("error removing existing node %s at %s: %s", nodeID, addr, err)
}
}
}
f := s.raft.AddVoter(raft.ServerID(nodeID), raft.ServerAddress(addr), 0, 0)
if f.Error() != nil {
return f.Error()
}
log.Printf("node %s at %s joined successfully", nodeID, addr)
return nil
}