/
wait_apply.go
146 lines (115 loc) · 2.79 KB
/
wait_apply.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
package bdb
import (
"fmt"
"sync"
"sync/atomic"
"github.com/niukuo/ragit/logging"
)
type waitRequest struct {
resCh chan struct{}
waitIndex uint64
reqID uint64
}
type waitApplyRequests struct {
capacity int
mutex sync.Mutex
requests map[uint64]*waitRequest
waitReqID uint64
appliedIndex uint64
confIndexLeft uint64
confIndexRight uint64
logger logging.Logger
}
func newWaitApplyRequests(cap int, logger logging.Logger) *waitApplyRequests {
return &waitApplyRequests{
capacity: cap,
requests: make(map[uint64]*waitRequest),
logger: logger,
}
}
func (w *waitApplyRequests) addRequest(waitIndex uint64) (*waitRequest, error) {
w.mutex.Lock()
defer w.mutex.Unlock()
if waitIndex <= w.appliedIndex {
return nil, nil
}
if len(w.requests) >= w.capacity {
return nil, fmt.Errorf("pending wait request is more than %d", w.capacity)
}
waitForApplyRequests.Inc()
reqID := atomic.AddUint64(&w.waitReqID, 1)
resCh := make(chan struct{})
req := &waitRequest{resCh: resCh, waitIndex: waitIndex, reqID: reqID}
w.requests[reqID] = req
return req, nil
}
func (w *waitApplyRequests) delRequest(reqID uint64) {
w.mutex.Lock()
defer w.mutex.Unlock()
waitForApplyRequests.Dec()
delete(w.requests, reqID)
}
func (w *waitApplyRequests) checkWait_nolock() {
for id, req := range w.requests {
if req.waitIndex <= w.appliedIndex {
close(req.resCh)
delete(w.requests, id)
}
}
}
func (w *waitApplyRequests) applyConfIndex(index uint64) {
w.mutex.Lock()
defer w.mutex.Unlock()
defer func() {
w.logger.Infof("applyConfIndex: %d, appliedIndex: %d, confIndexLeft: %d, confIndexRight: %d",
index, w.appliedIndex, w.confIndexLeft, w.confIndexRight)
}()
if index == 0 {
w.confIndexLeft = 0
w.confIndexRight = 0
return
}
if index <= w.confIndexRight {
return
}
if index == w.confIndexRight+1 {
w.confIndexRight = index
} else {
w.confIndexLeft = index
w.confIndexRight = index
}
if w.appliedIndexChase_nolock() {
w.checkWait_nolock()
}
}
func (w *waitApplyRequests) applyDataIndex(index uint64) {
w.mutex.Lock()
defer w.mutex.Unlock()
defer func() {
w.logger.Infof("applyDataIndex: %d, appliedIndex: %d, confIndexLeft: %d, confIndexRight: %d",
index, w.appliedIndex, w.confIndexLeft, w.confIndexRight)
}()
if index == 0 {
w.appliedIndex = 0
return
}
if index <= w.appliedIndex {
return
}
w.appliedIndex = index
w.appliedIndexChase_nolock()
w.checkWait_nolock()
}
func (w *waitApplyRequests) appliedIndexChase_nolock() bool { //return if appliedIndex updated
if w.appliedIndex >= w.confIndexRight {
w.confIndexLeft = w.appliedIndex
w.confIndexRight = w.appliedIndex
return false
}
if w.appliedIndex < w.confIndexLeft-1 {
return false
}
w.appliedIndex = w.confIndexRight
w.confIndexLeft = w.confIndexRight
return true
}