/
coordinator.go
325 lines (262 loc) · 7.01 KB
/
coordinator.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
package turing
import (
"context"
"net"
"strconv"
"time"
"github.com/lni/dragonboat/v3"
"github.com/lni/dragonboat/v3/client"
"github.com/lni/dragonboat/v3/config"
"github.com/lni/dragonboat/v3/statemachine"
"github.com/256dpi/turing/wire"
)
const clusterID uint64 = 1
type coordinator struct {
config Config
node *dragonboat.NodeHost
staleReads *bundler
linearReads *bundler
writes *bundler
session *client.Session
operations []wire.Operation
}
func createCoordinator(cfg Config, registry *registry, manager *manager) (*coordinator, error) {
// prepare members
members := make(map[uint64]string)
for _, member := range cfg.Members {
members[member.ID] = member.Address()
}
// calculate rrt in ms
var rttMS = uint64(cfg.RoundTripTime / time.Millisecond)
// TODO: Allow node host tuning.
// prepare node config
nodeConfig := config.Config{
NodeID: cfg.ID,
ClusterID: clusterID,
CheckQuorum: true,
ElectionRTT: rttMS * 1000, // 1000ms @ 1ms RTT
HeartbeatRTT: rttMS * 100, // 100ms @ 1ms RTT
SnapshotEntries: 10000,
CompactionOverhead: 20000,
}
// prepare node host config
hostConfig := config.NodeHostConfig{
DeploymentID: clusterID,
WALDir: cfg.RaftDir(),
NodeHostDir: cfg.RaftDir(),
RTTMillisecond: rttMS,
RaftAddress: cfg.Local().Address(),
Expert: config.ExpertConfig{
FS: cfg.RaftFS(),
},
}
// create node host
node, err := dragonboat.NewNodeHost(hostConfig)
if err != nil {
return nil, err
}
// prepare replicator factory
factory := func(uint64, uint64) statemachine.IOnDiskStateMachine {
return newReplicator(cfg, registry, manager)
}
// start cluster
err = node.StartOnDiskCluster(members, false, factory, nodeConfig)
if err != nil {
return nil, err
}
// create coordinator
coordinator := &coordinator{
config: cfg,
node: node,
session: node.GetNoOPSession(clusterID),
operations: make([]wire.Operation, cfg.ProposalBatchSize),
}
// create stale read bundler
coordinator.staleReads = newBundler(bundlerOptions{
queueSize: (cfg.ConcurrentReaders + 1) * cfg.LookupBatchSize,
batchSize: cfg.LookupBatchSize,
concurrency: cfg.ConcurrentReaders,
handler: coordinator.performStaleLookup,
})
// create liner read bundler
coordinator.linearReads = newBundler(bundlerOptions{
queueSize: (cfg.ConcurrentReaders + 1) * cfg.LookupBatchSize,
batchSize: cfg.LookupBatchSize,
concurrency: cfg.ConcurrentReaders,
handler: coordinator.performLinearLookup,
})
// create write bundler
coordinator.writes = newBundler(bundlerOptions{
queueSize: (cfg.ConcurrentProposers + 1) * cfg.ProposalBatchSize,
batchSize: cfg.ProposalBatchSize,
concurrency: cfg.ConcurrentProposers,
handler: coordinator.performUpdates,
})
return coordinator, nil
}
var coordinatorUpdate = systemMetrics.WithLabelValues("coordinator.update")
func (c *coordinator) update(ins Instruction, fn func(error)) error {
// observe
timer := observe(coordinatorUpdate)
defer timer.finish()
// queue update
err := c.writes.process(ins, fn)
if err != nil {
return err
}
return nil
}
var coordinatorPerformUpdates = systemMetrics.WithLabelValues("coordinator.performUpdates")
func (c *coordinator) performUpdates(list []Instruction) error {
// observe
timer := observe(coordinatorPerformUpdates)
defer timer.finish()
// TODO: Clarify the handling of proposal failures, retries and idempotency.
// prepare command
cmd := wire.Command{
Operations: c.operations[:0],
}
// add operations
for _, ins := range list {
// encode instructions
encodedInstruction, ref, err := ins.Encode()
if err != nil {
return err
}
// ensure release
if ref != nil {
defer ref.Release()
}
// add operation
cmd.Operations = append(cmd.Operations, wire.Operation{
Name: ins.Describe().Name,
Code: encodedInstruction,
})
}
// encode command
encodedCommand, ref, err := cmd.Encode(true)
if err != nil {
return err
}
// release
defer ref.Release()
// prepare context
ctx, cancel := context.WithTimeout(context.Background(), c.config.ProposalTimeout)
defer cancel()
// propose
result, err := c.node.SyncPropose(ctx, c.session, encodedCommand)
if err != nil {
return err
}
// walk command and decode results
err = wire.WalkCommand(result.Data, func(i int, op wire.Operation) (bool, error) {
// decode result if available
if len(op.Code) > 0 {
return true, list[i].Decode(op.Code)
}
return true, nil
})
if err != nil {
return err
}
return nil
}
var coordinatorLookup = systemMetrics.WithLabelValues("coordinator.lookup")
func (c *coordinator) lookup(ins Instruction, fn func(error), options Options) error {
// observe
timer := observe(coordinatorLookup)
defer timer.finish()
// queue read
if options.StaleRead {
return c.staleReads.process(ins, fn)
}
return c.linearReads.process(ins, fn)
}
var coordinatorPerformStaleLookup = systemMetrics.WithLabelValues("coordinator.performStaleLookup")
func (c *coordinator) performStaleLookup(list []Instruction) error {
// observe
timer := observe(coordinatorPerformStaleLookup)
defer timer.finish()
// perform stale read
_, err := c.node.StaleRead(clusterID, list)
if err != nil {
return err
}
return nil
}
var coordinatorPerformLinearLookup = systemMetrics.WithLabelValues("coordinator.performLinearLookup")
func (c *coordinator) performLinearLookup(list []Instruction) error {
// observe
timer := observe(coordinatorPerformLinearLookup)
defer timer.finish()
// prepare context
ctx, cancel := context.WithTimeout(context.Background(), c.config.LinearReadTimeout)
defer cancel()
// perform linear read
_, err := c.node.SyncRead(ctx, clusterID, list)
if err != nil {
return err
}
return nil
}
var coordinatorStatus = systemMetrics.WithLabelValues("coordinator.status")
func (c *coordinator) status() Status {
// observe
timer := observe(coordinatorStatus)
defer timer.finish()
// get info
info := c.node.GetNodeHostInfo(dragonboat.NodeHostInfoOption{
SkipLogInfo: true,
})
// prepare id and role
var id uint64
role := RoleFollower
// prepare members
var members []Member
// check info
if len(info.ClusterInfoList) > 0 {
// set id
id = info.ClusterInfoList[0].NodeID
// set observer
if info.ClusterInfoList[0].IsObserver {
role = RoleObserver
}
// set leader
if info.ClusterInfoList[0].IsLeader {
role = RoleLeader
}
// parse members
for id, addr := range info.ClusterInfoList[0].Nodes {
host, port, _ := net.SplitHostPort(addr)
portNum, _ := strconv.Atoi(port)
members = append(members, Member{
ID: id,
Host: host,
Port: portNum,
})
}
}
// prepare leader
var leader *Member
// get leader
lid, ok, _ := c.node.GetLeaderID(clusterID)
if ok {
for _, member := range members {
if member.ID == lid {
leader = &member
}
}
}
// create status
status := Status{
ID: id,
Role: role,
Leader: leader,
Members: members,
}
return status
}
func (c *coordinator) close() {
// stop node
c.node.Stop()
}