-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathraft_log_ack.go
More file actions
154 lines (129 loc) · 4.69 KB
/
raft_log_ack.go
File metadata and controls
154 lines (129 loc) · 4.69 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
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
package raft
import (
"context"
"github.com/ccassar/raft/internal/raft_pb"
"go.uber.org/atomic"
"go.uber.org/zap"
"sync"
)
// raftLogAcknowledger is responsible for generating acknowledgements for log commands to the application.
// Note that none of the raft package goroutines ever block waiting on ack generation.
// We track the logCommandContainer which describes how to ack back to the application (whether local or remote),
// and the index which, once committed, triggers the acknowledgment.
type logCommandPendingAck struct {
index int64
container *logCommandContainer
}
type raftLogAcknowledger struct {
target *atomic.Int64
// A one-deep channel which indicates that commitIndex may have moved ahead of lastApplied, and that the publisher
// may have new log commands it needs to push to the application.
updatesAvailable chan struct{}
// pendingAcks tracks pending acknowledgements. This slice is added to by the raftEngine when it receives
// new log commands through trackPEndingAck call, and is drained from the front by this ack generator. No
// need for read/write mutex since we only have one reader and one writer contending for the resource.
pendingAcksMu sync.Mutex
pendingAcks []*logCommandPendingAck
}
// trackPendingAck is called to track pending ack for a given sequence. This call must be made before the
// index is committed.
func (acker *raftLogAcknowledger) trackPendingAck(cmd *logCommandContainer, index int64) {
acker.pendingAcksMu.Lock()
defer acker.pendingAcksMu.Unlock()
acker.pendingAcks = append(acker.pendingAcks, &logCommandPendingAck{index: index, container: cmd})
}
// notify is called to wake up acknowledger to determine whether it needs to acknowledge newly committed updates
// to application (local or remote).
func (acker *raftLogAcknowledger) notify() {
select {
case acker.updatesAvailable <- struct{}{}:
default:
}
}
func (acker *raftLogAcknowledger) run(ctx context.Context, wg *sync.WaitGroup, lg *zap.SugaredLogger) {
defer wg.Done()
lg.Debug("raftLogAcknowledger, start running")
outerLoop:
for {
select {
case <-acker.updatesAvailable:
target := acker.target.Load()
//
// Find any acknowledgments pending beneath the target and release them.
var pendingFrom, pendingTo int64
count := 0
pending := 0
for {
var doAck *logCommandPendingAck
acker.pendingAcksMu.Lock()
if len(acker.pendingAcks) > 0 {
doAck = acker.pendingAcks[0]
if doAck.index <= target {
acker.pendingAcks = acker.pendingAcks[1:]
} else {
doAck = nil
pending = len(acker.pendingAcks)
if pending > 0 {
pendingFrom = acker.pendingAcks[0].index
pendingTo = acker.pendingAcks[pending-1].index
}
}
}
acker.pendingAcksMu.Unlock()
if doAck == nil {
// wait for next notification
break
}
count++
// Acknowledge and return the container.
doAck.container.reply = &raft_pb.LogCommandReply{
Ack: true,
}
// Channel of one for reply is always available, so no need to mess about.
doAck.container.returnChan <- doAck.container
}
if count > 0 {
lg.Debugw(
"raftLogAcknowledger, sent acknowledgements to applications",
"count", count, "target", target,
"pendingCount", pending, "pendingFrom", pendingFrom, "pendingTo", pendingTo)
}
case <-ctx.Done():
// We are shutting down or demoted from leader. Nak any remaining pending entries, and leave.
lg.Debug("raftLogAcknowledger, received shutdown (or resigning from leader)")
acker.pendingAcksMu.Lock()
var pendingFrom, pendingTo int64
pending := len(acker.pendingAcks)
if pending > 0 {
pendingFrom = acker.pendingAcks[0].index
pendingTo = acker.pendingAcks[pending-1].index
}
for _, doAck := range acker.pendingAcks {
doAck.container.err = ctx.Err()
// Channel of one for reply is always available, so no need to mess about.
doAck.container.returnChan <- doAck.container
}
acker.pendingAcksMu.Unlock()
if pending > 0 {
lg.Debugw(
"raftLogAcknowledger, sent negative acknowledgements to applications for pending acks",
"pendingCount", pending, "pendingFrom", pendingFrom, "pendingTo", pendingTo)
}
break outerLoop
}
}
lg.Debug("raftLogAcknowledger, stop running")
}
// createLogAcknowledgerAndRun is typically called by the leader to set up its acknowledger.
func createLogAcknowledgerAndRun(
ctx context.Context, wg *sync.WaitGroup, target *atomic.Int64, lg *zap.SugaredLogger) *raftLogAcknowledger {
defer wg.Done()
acker := &raftLogAcknowledger{
target: target,
updatesAvailable: make(chan struct{}, 1),
pendingAcks: []*logCommandPendingAck{},
}
wg.Add(1)
go acker.run(ctx, wg, lg)
return acker
}