-
Notifications
You must be signed in to change notification settings - Fork 1
/
requests.go
459 lines (412 loc) · 11.7 KB
/
requests.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package paxos
import (
"crypto/md5"
"encoding/binary"
"errors"
"fmt"
"io"
"math/rand"
"os"
"sync"
"sync/atomic"
"time"
"github.com/LiuzhouChan/go-paxos/internal/settings"
"github.com/LiuzhouChan/go-paxos/logger"
"github.com/LiuzhouChan/go-paxos/paxospb"
)
const (
badKeyCheck bool = false
sysGcMillisecond uint64 = 15000
)
var (
defaultGCTick uint64 = 2
plog = logger.GetLogger("go-paxos")
)
var (
// ErrInvalidSession indicates that the specified client session is invalid.
ErrInvalidSession = errors.New("invalid session")
// ErrTimeoutTooSmall indicates that the specified timeout value is too small.
ErrTimeoutTooSmall = errors.New("specified timeout value is too small")
// ErrPayloadTooBig indicates that the payload is too big.
ErrPayloadTooBig = errors.New("payload is too big")
// ErrSystemBusy indicates that the system is too busy to handle the request.
ErrSystemBusy = errors.New("system is too busy try again later")
// ErrGroupClosed indicates that the requested cluster is being shut down.
ErrGroupClosed = errors.New("paxos group already closed")
// ErrBadKey indicates that the key is bad, retry the request is recommended.
ErrBadKey = errors.New("bad key try again later")
// ErrPendingConfigChangeExist indicates that there is already a pending
// membership change exist in the system.
ErrPendingConfigChangeExist = errors.New("pending config change request exist")
// ErrTimeout indicates that the operation timed out.
ErrTimeout = errors.New("timeout")
// ErrSystemStopped indicates that the system is being shut down.
ErrSystemStopped = errors.New("system stopped")
// ErrCanceled indicates that the request has been canceled.
ErrCanceled = errors.New("request canceled")
// ErrRejected indicates that the request has been rejected.
ErrRejected = errors.New("request rejected")
)
// IsTempError returns a boolean value indicating whether the specified error
// is a temporary error that worth to be retried later with the exact same
// input, potentially on a more suitable NodeHost instance.
func IsTempError(err error) bool {
return err == ErrSystemBusy ||
err == ErrBadKey ||
err == ErrPendingConfigChangeExist ||
err == ErrGroupClosed ||
err == ErrSystemStopped
}
// RequestResultCode is the result code returned to the client to indicate the
// outcome of the request.
type RequestResultCode int
// RequestResult is the result struct returned for the request.
type RequestResult struct {
// code is the result state of the request.
code RequestResultCode
// Result is the returned result from the Update method of the IStateMachine
// instance. Result is only available when making a proposal and the Code
// value is RequestCompleted.
result uint64
}
// Timeout returns a boolean value indicating whether the Request timed out.
func (rr *RequestResult) Timeout() bool {
return rr.code == requestTimeout
}
// Completed returns a boolean value indicating the request request completed
// successfully. For proposals, it means the proposal has been committed by the
// Raft cluster and applied on the local node. For ReadIndex operation, it means
// the cluster is now ready for a local read.
func (rr *RequestResult) Completed() bool {
return rr.code == requestCompleted
}
// Terminated returns a boolean value indicating the request terminated due to
// the requested Raft cluster is being shut down.
func (rr *RequestResult) Terminated() bool {
return rr.code == requestTerminated
}
// Rejected returns a boolean value indicating the request is rejected. For a
// proposal, it means that the used client session instance is not registered
// or it has been evicted on the server side. When requesting a client session
// to be registered, Rejected means the another client session with the same
// client ID has already been registered. When requesting a client session to
// be unregistered, Rejected means the specified client session is not found
// on the server side. For a membership change request, it means the request
// is out of order and thus ignored. Note that the out-of-order check when
// making membership changes is only imposed when IMasterClient is used in
// NodeHost.
func (rr *RequestResult) Rejected() bool {
return rr.code == requestRejected
}
// GetResult returns the result value of the request. When making a proposal,
// the returned result is the value returned by the Update method of the
// IStateMachine instance.
func (rr *RequestResult) GetResult() uint64 {
return rr.result
}
const (
requestTimeout RequestResultCode = iota
requestCompleted
requestTerminated
requestRejected
)
var requestResultCodeName = [...]string{
"RequestTimeout",
"RequestCompleted",
"RequestTerminated",
"RequestRejected",
}
func (c RequestResultCode) String() string {
return requestResultCodeName[uint64(c)]
}
func getTerminatedResult() RequestResult {
return RequestResult{
code: requestTerminated,
}
}
func getTimeoutResult() RequestResult {
return RequestResult{
code: requestTimeout,
}
}
const (
maxProposalPayloadSize = settings.MaxProposalPayloadSize
)
type logicalClock struct {
tick uint64
lastGcTime uint64
gcTick uint64
tickInMillisecond uint64
}
func (p *logicalClock) increaseTick() {
atomic.AddUint64(&p.tick, 1)
}
func (p *logicalClock) getTick() uint64 {
return atomic.LoadUint64(&p.tick)
}
func (p *logicalClock) getTimeoutTick(timeout time.Duration) uint64 {
timeoutMs := uint64(timeout.Nanoseconds() / 1000000)
return timeoutMs / p.tickInMillisecond
}
// ICompleteHandler is a handler interface that will be invoked when the request
// in completed. This interface is used by the language bindings, applications
// are not expected to directly use this interface.
type ICompleteHandler interface {
Notify(RequestResult)
Release()
}
// RequestState is the object used to provide request result to users.
type RequestState struct {
data []byte
key uint64
deadline uint64
completeHandler ICompleteHandler
// CompleteC is a channel for delivering request result to users.
CompletedC chan RequestResult
pool *sync.Pool
}
func (r *RequestState) notify(result RequestResult) {
if r.completeHandler == nil {
select {
case r.CompletedC <- result:
default:
plog.Panicf("RequestState.CompletedC is full")
}
} else {
r.completeHandler.Notify(result)
r.completeHandler.Release()
r.Release()
}
}
// Release puts the RequestState object back to the sync.Pool pool.
func (r *RequestState) Release() {
if r.pool != nil {
r.data = nil
r.key = 0
r.deadline = 0
r.completeHandler = nil
r.pool.Put(r)
}
}
type proposalShard struct {
mu sync.Mutex
proposals *entryQueue
pending map[uint64]*RequestState
pool *sync.Pool
stopped bool
expireNotified uint64
logicalClock
}
func newPendingProposalShard(pool *sync.Pool,
proposals *entryQueue, tickInMilliSecond uint64) *proposalShard {
gcTick := defaultGCTick
if gcTick == 0 {
panic("invalid gcTick")
}
lcu := logicalClock{
tickInMillisecond: tickInMilliSecond,
gcTick: gcTick,
}
p := &proposalShard{
proposals: proposals,
pending: make(map[uint64]*RequestState),
logicalClock: lcu,
pool: pool,
}
return p
}
func (p *proposalShard) propose(value []byte,
key uint64, handler ICompleteHandler,
timeout time.Duration) (*RequestState, error) {
timeoutTick := p.getTimeoutTick(timeout)
if timeoutTick == 0 {
return nil, ErrTimeoutTooSmall
}
if uint64(len(value)) > maxProposalPayloadSize {
return nil, ErrPayloadTooBig
}
entry := paxospb.Entry{
Key: key,
AcceptorState: paxospb.AcceptorState{
AccetpedValue: prepareProposalPayload(value),
},
}
req := p.pool.Get().(*RequestState)
req.completeHandler = handler
req.key = entry.Key
req.deadline = p.getTick() + timeoutTick
if len(req.CompletedC) > 0 {
req.CompletedC = make(chan RequestResult, 1)
}
p.mu.Lock()
p.pending[entry.Key] = req
p.mu.Unlock()
added, stopped := p.proposals.add(entry)
if stopped {
plog.Warningf("dropping proposals, group stopped")
p.mu.Lock()
delete(p.pending, entry.Key)
p.mu.Unlock()
return nil, ErrGroupClosed
}
if !added {
p.mu.Lock()
delete(p.pending, entry.Key)
p.mu.Unlock()
plog.Warningf("dropping proposals, overloaded")
return nil, ErrSystemBusy
}
return req, nil
}
func (p *proposalShard) close() {
p.mu.Lock()
defer p.mu.Unlock()
p.stopped = true
if p.proposals != nil {
p.proposals.close()
}
for _, c := range p.pending {
c.notify(getTerminatedResult())
}
}
func (p *proposalShard) getProposal(key, now uint64) *RequestState {
p.mu.Lock()
if p.stopped {
p.mu.Unlock()
return nil
}
ps, ok := p.pending[key]
if ok && ps.deadline >= now {
delete(p.pending, key)
p.mu.Unlock()
return ps
}
p.mu.Unlock()
return nil
}
func (p *proposalShard) applied(key, result uint64, rejected bool) {
now := p.getTick()
var code RequestResultCode
if rejected {
code = requestRejected
} else {
code = requestCompleted
}
ps := p.getProposal(key, now)
if ps != nil {
ps.notify(RequestResult{code: code, result: result})
}
tick := p.getTick()
if tick != p.expireNotified {
p.gcAt(now)
p.expireNotified = tick
}
}
func (p *proposalShard) gc() {
now := p.getTick()
p.gcAt(now)
}
func (p *proposalShard) gcAt(now uint64) {
p.mu.Lock()
defer p.mu.Unlock()
if p.stopped {
return
}
// plog.Infof("now-p.LastGcTime %v, getTick() %v", now-p.lastGcTime, p.getTick())
if now-p.lastGcTime < p.gcTick {
return
}
p.lastGcTime = now
deletedKeys := make(map[uint64]bool)
for key, pRec := range p.pending {
if pRec.deadline < now {
pRec.notify(getTimeoutResult())
deletedKeys[key] = true
}
}
if len(deletedKeys) == 0 {
return
}
for key := range deletedKeys {
delete(p.pending, key)
}
}
type keyGenerator struct {
randMu sync.Mutex
rand *rand.Rand
}
func (k *keyGenerator) nextKey() uint64 {
k.randMu.Lock()
v := k.rand.Uint64()
k.randMu.Unlock()
return v
}
func getRandomGenerator(groupID, nodeID uint64,
addr string, partition uint64) *keyGenerator {
pid := os.Getpid()
nano := time.Now().UnixNano()
seedStr := fmt.Sprintf("%d-%d-%d-%d-%s-%d",
pid, nano, groupID, nodeID, addr, partition)
m := md5.New()
if _, err := io.WriteString(m, seedStr); err != nil {
panic(err)
}
md5sum := m.Sum(nil)
seed := binary.LittleEndian.Uint64(md5sum)
return &keyGenerator{rand: rand.New(rand.NewSource(int64(seed)))}
}
type pendingProposal struct {
shards []*proposalShard
keyg []*keyGenerator
ps uint64
idx uint64
}
func newPendingProposal(pool *sync.Pool, proposals *entryQueue,
groupID, nodeID uint64, paxosAddress string, tickInMilliSecond uint64) *pendingProposal {
ps := uint64(16)
p := &pendingProposal{
shards: make([]*proposalShard, ps),
keyg: make([]*keyGenerator, ps),
ps: ps,
idx: 0,
}
for i := uint64(0); i < ps; i++ {
p.shards[i] = newPendingProposalShard(pool, proposals, tickInMilliSecond)
p.keyg[i] = getRandomGenerator(groupID, nodeID, paxosAddress, i)
}
return p
}
func (p *pendingProposal) propose(value []byte, handler ICompleteHandler,
timeout time.Duration) (*RequestState, error) {
key := p.nextKey()
pp := p.shards[key%p.ps]
return pp.propose(value, key, handler, timeout)
}
func (p *pendingProposal) nextKey() uint64 {
p.idx++
return p.keyg[p.idx%p.ps].nextKey()
}
func (p *pendingProposal) close() {
for _, pp := range p.shards {
pp.close()
}
}
func (p *pendingProposal) applied(key uint64, result uint64, rejected bool) {
pp := p.shards[key%p.ps]
pp.applied(key, result, rejected)
}
func (p *pendingProposal) increaseTick() {
for i := uint64(0); i < p.ps; i++ {
p.shards[i].increaseTick()
}
}
func (p *pendingProposal) gc() {
for i := uint64(0); i < p.ps; i++ {
p.shards[i].gc()
}
}
func prepareProposalPayload(cmd []byte) []byte {
dst := make([]byte, len(cmd))
copy(dst, cmd)
return dst
}