-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributed_log.go
269 lines (255 loc) · 6.93 KB
/
distributed_log.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
package log
import (
"bytes"
"fmt"
api "github.com/anshulsood11/loghouse/api/v1"
"github.com/hashicorp/raft"
raftboltdb "github.com/hashicorp/raft-boltdb/v2"
"google.golang.org/protobuf/proto"
"os"
"path/filepath"
"time"
)
type DistributedLog struct {
config Config
log *Log
raft *raft.Raft
}
func NewDistributedLog(dataDir string, config Config) (*DistributedLog, error) {
l := &DistributedLog{config: config}
if err := l.setupLog(dataDir); err != nil {
return nil, err
}
if err := l.setupRaft(dataDir); err != nil {
return nil, err
}
return l, nil
}
/*
setupLog(dataDir string) creates the log for this server, where this server will store
the user’s records.
*/
func (l *DistributedLog) setupLog(dataDir string) error {
logDir := filepath.Join(dataDir, "log")
if err := os.MkdirAll(logDir, 0755); err != nil {
return err
}
var err error
l.log, err = NewLog(logDir, l.config)
return err
}
func (l *DistributedLog) setupRaft(dataDir string) error {
// Finite-state machine that applies the commands given
fsm := &fsm{log: l.log}
logDir := filepath.Join(dataDir, "raft", "log")
if err := os.MkdirAll(logDir, 0755); err != nil {
return err
}
logConfig := l.config
logConfig.Segment.InitialOffset = 1
// Log Store where Raft stores the given commands. Using our own log implementation.
// Initial Offset is set to 1 as it is required by Raft.
logStore, err := newLogStore(logDir, logConfig)
if err != nil {
return err
}
// Stable Store is a K-V store where Raft stores the metadata (like the server’s current
// term or the candidate the server voted for)
stableStore, err := raftboltdb.NewBoltStore(
filepath.Join(dataDir, "raft", "stable"),
)
if err != nil {
return err
}
retain := 1
// Snapshot Store is where Raft stores compact snapshots of its data
// variable retain specifies the number of previous snapshots to retain
snapshotStore, err := raft.NewFileSnapshotStore(
filepath.Join(dataDir, "raft"),
retain,
os.Stderr,
)
if err != nil {
return err
}
maxPool := 5
timeout := 10 * time.Second
// Raft uses transport to communicate with other nodes
transport := raft.NewNetworkTransport(
l.config.Raft.StreamLayer,
maxPool,
timeout,
os.Stderr,
)
config := raft.DefaultConfig()
config.LocalID = l.config.Raft.LocalID
if l.config.Raft.HeartbeatTimeout != 0 {
config.HeartbeatTimeout = l.config.Raft.HeartbeatTimeout
}
if l.config.Raft.ElectionTimeout != 0 {
config.ElectionTimeout = l.config.Raft.ElectionTimeout
}
if l.config.Raft.LeaderLeaseTimeout != 0 {
config.LeaderLeaseTimeout = l.config.Raft.LeaderLeaseTimeout
}
if l.config.Raft.CommitTimeout != 0 {
config.CommitTimeout = l.config.Raft.CommitTimeout
}
l.raft, err = raft.NewRaft(
config,
fsm,
logStore,
stableStore,
snapshotStore,
transport,
)
if err != nil {
return err
}
hasState, err := raft.HasExistingState(
logStore,
stableStore,
snapshotStore,
)
if err != nil {
return err
}
// Only when the server is the only voter, then we bootstrap. The subsequently added
// servers don't bootstrap.
if l.config.Raft.Bootstrap && !hasState {
config := raft.Configuration{
Servers: []raft.Server{{
ID: config.LocalID,
Address: transport.LocalAddr(),
}},
}
err = l.raft.BootstrapCluster(config).Error()
}
return err
}
func (l *DistributedLog) Append(record *api.Record) (uint64, error) {
res, err := l.apply(
AppendRequestType,
&api.ProduceRequest{Record: record},
)
if err != nil {
return 0, err
}
return res.(*api.ProduceResponse).Offset, nil
}
// This must be run on the leader else it will fail
func (l *DistributedLog) apply(reqType RequestType, req proto.Message) (
interface{},
error,
) {
var buf bytes.Buffer
_, err := buf.Write([]byte{byte(reqType)})
if err != nil {
return nil, err
}
b, err := proto.Marshal(req)
if err != nil {
return nil, err
}
_, err = buf.Write(b)
if err != nil {
return nil, err
}
timeout := 10 * time.Second
future := l.raft.Apply(buf.Bytes(), timeout)
if future.Error() != nil {
return nil, future.Error()
}
res := future.Response()
if err, ok := res.(error); ok {
return nil, err
}
return res, nil
}
func (l *DistributedLog) Read(offset uint64) (*api.Record, error) {
return l.log.Read(offset)
}
// Join adds the server to the Raft cluster. Every server is added as a voter.
// Servers can be added as a non-voters as well which are useful to replicate
// the state to multiple servers to serve read only eventually consistent state.
// Adding more voter servers makes replications and elections slower because
// the leader has more servers to communicate with to reach a majority. This must
// be run on the leader, or it will fail
func (l *DistributedLog) Join(id, addr string) error {
if leader := l.raft.VerifyLeader(); leader.Error() != nil {
return nil
}
configFuture := l.raft.GetConfiguration()
if err := configFuture.Error(); err != nil {
return err
}
joinedServerID := raft.ServerID(id)
joinedServerAddr := raft.ServerAddress(addr)
for _, server := range configFuture.Configuration().Servers {
if server.ID == joinedServerID && server.Address == joinedServerAddr {
// server has already joined
return nil
}
if server.ID == joinedServerID || server.Address == joinedServerAddr {
// remove any existing server with serverID and address combination not matching
removeFuture := l.raft.RemoveServer(joinedServerID, 0, 0)
if err := removeFuture.Error(); err != nil {
return err
}
}
}
addFuture := l.raft.AddVoter(joinedServerID, joinedServerAddr, 0, 0)
if err := addFuture.Error(); err != nil {
return err
}
return nil
}
// Leave removes the server from the cluster. Removing the leader will
// trigger a new election.
func (l *DistributedLog) Leave(id string) error {
if leader := l.raft.VerifyLeader(); leader.Error() != nil {
return nil
}
removeFuture := l.raft.RemoveServer(raft.ServerID(id), 0, 0)
return removeFuture.Error()
}
// WaitForLeader blocks until the cluster has elected a leader or times out.
// It’s useful when writing tests
func (l *DistributedLog) WaitForLeader(timeout time.Duration) error {
timeoutCh := time.After(timeout)
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-timeoutCh:
return fmt.Errorf("timed out")
case <-ticker.C:
if _, currLeaderID := l.raft.LeaderWithID(); currLeaderID != "" {
return nil
}
}
}
}
func (l *DistributedLog) Close() error {
f := l.raft.Shutdown()
if err := f.Error(); err != nil {
return err
}
return l.log.Close()
}
func (l *DistributedLog) GetServers() ([]*api.Server, error) {
future := l.raft.GetConfiguration()
if err := future.Error(); err != nil {
return nil, err
}
var servers []*api.Server
currLeaderAddress, _ := l.raft.LeaderWithID()
for _, server := range future.Configuration().Servers {
servers = append(servers, &api.Server{
Id: string(server.ID),
RpcAddr: string(server.Address),
IsLeader: currLeaderAddress == server.Address,
})
}
return servers, nil
}