-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlease_manager.go
262 lines (234 loc) · 6.15 KB
/
lease_manager.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
package main
import (
"context"
"math"
"strings"
"sync"
"time"
pb "github.com/KDF5000/flease/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type Transport interface {
Read(addr string, k uint64) (ReadResult, bool)
Write(addr string, k uint64, v LeaseValue) bool
}
type LeaseManager struct {
node uint64
peers []string
trans map[string]pb.LeaseClient
mu sync.Mutex
// own lease
lease Lease
// lease expire time, sec
expiredDuration uint32
// time diff, ns
e uint32
exit chan struct{}
}
func NewLeaseManager(expired, e uint32, node uint64, peers string) (*LeaseManager, error) {
return &LeaseManager{
node: node,
peers: strings.Split(peers, ","),
trans: make(map[string]pb.LeaseClient),
expiredDuration: expired,
e: e,
exit: make(chan struct{}),
}, nil
}
func (m *LeaseManager) Start() {
timer := time.NewTimer(0)
for {
select {
case <-timer.C:
val, ok := m.GetLease()
if ok && val.Node == m.node {
logF(m.node, "become or keep leader, val: %+v", val)
}
case <-m.exit:
logF(m.node, "recieve exit sigal")
timer.Stop()
return
}
timer.Reset(time.Duration(m.expiredDuration/2) * time.Second)
}
}
func (m *LeaseManager) Stop() {
close(m.exit)
}
func (m *LeaseManager) Read(k uint64) (*ReadResult, bool) {
return m.lease.Read(k)
}
func (m *LeaseManager) Write(k uint64, v LeaseValue) bool {
return m.lease.Write(k, v)
}
func (m *LeaseManager) nextK() uint64 {
return uint64(time.Now().UnixNano())
}
func (m *LeaseManager) read(k uint64) (*ReadResult, bool) {
commitCh := make(chan *ReadResult, len(m.peers))
abortCh := make(chan struct{}, len(m.peers))
respCh := make(chan struct{}, len(m.peers))
for _, peer := range m.peers {
go func(p string) {
m.mu.Lock()
trans, ok := m.trans[p]
if !ok {
conn, err := grpc.Dial(p, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
abortCh <- struct{}{}
return
}
trans = pb.NewLeaseClient(conn)
m.trans[p] = trans
}
m.mu.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
logF(m.node, "begin to send read request to %s...", p)
resp, err := trans.Read(ctx, &pb.ReadRequest{K: k})
defer cancel()
logF(m.node, "recv read response from %s, err: %v, resp: %+v", p, err, resp)
if err == nil {
if resp.Status == pb.Status_COMMIT {
val := ReadResult{
K: k,
LastWrite: resp.LastWrite,
Value: LeaseValue{
Node: resp.Lease.Node,
LeaseTime: resp.Lease.LeaseTime,
},
}
commitCh <- &val
} else {
abortCh <- struct{}{}
}
} else {
logF(m.node, "failed to send write request to %s", p)
}
respCh <- struct{}{}
}(peer)
}
var responses []*ReadResult
var totalResp int
for {
// timeout := time.After(time.Minute)
select {
case val := <-commitCh:
responses = append(responses, val)
logF(m.node, "receive %d commit read response, expect majority: %d",
len(responses), int(math.Ceil(float64(len(m.peers)+1)/2)))
if len(responses) >= int(math.Ceil(float64(len(m.peers)+1)/2)) {
var maxReadResult *ReadResult
var maxK uint64
for _, resp := range responses {
if resp.LastWrite > maxK {
maxK = resp.LastWrite
maxReadResult = resp
}
}
return maxReadResult, true
}
case <-abortCh:
logF(m.node, "recieve abort read")
return nil, false
case <-respCh:
totalResp++
if totalResp >= len(m.peers) {
logF(m.node, "receive all read rpc response")
return nil, false
}
}
}
}
func (m *LeaseManager) write(k uint64, val *LeaseValue) bool {
commitCh := make(chan struct{}, len(m.peers))
abortCh := make(chan struct{}, len(m.peers))
respCh := make(chan struct{}, len(m.peers))
for _, peer := range m.peers {
go func(p string) {
m.mu.Lock()
trans, ok := m.trans[p]
if !ok {
conn, err := grpc.Dial(p, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
abortCh <- struct{}{}
return
}
trans = pb.NewLeaseClient(conn)
m.trans[p] = trans
}
m.mu.Unlock()
req := pb.WriteRequest{K: k, Lease: &pb.LeaseValue{
Node: val.Node,
LeaseTime: val.LeaseTime,
}}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
logF(m.node, "begin to send write request to %s...", p)
resp, err := trans.Write(ctx, &req)
defer cancel()
logF(m.node, "recv write response from %s,err: %v, resp: %+v", p, err, resp)
if err == nil {
if resp.Status == pb.Status_ABORT {
abortCh <- struct{}{}
} else {
commitCh <- struct{}{}
}
}
respCh <- struct{}{}
}(peer)
}
var commitCnt int
var totalResp int
timeout := time.After(time.Minute)
for {
select {
case <-commitCh:
commitCnt++
logF(m.node, "write response: %d, majority: %d", commitCnt, int(math.Ceil(float64(len(m.peers)+1)/2)))
if commitCnt >= int(math.Ceil(float64(len(m.peers)+1)/2)) {
return true
}
case <-abortCh:
logF(m.node, "receive abort read")
return false
case <-timeout:
logF(m.node, "write timeout")
return false
case <-respCh:
totalResp++
if totalResp >= len(m.peers) {
logF(m.node, "receive all write rpc response")
return false
}
}
}
}
func (m *LeaseManager) GetLease() (*LeaseValue, bool) {
return m.getLease(m.nextK())
}
func (m *LeaseManager) getLease(k uint64) (*LeaseValue, bool) {
res, commit := m.read(k)
if !commit {
return nil, false
}
now := time.Now().UnixNano()
if res != nil && res.Value.LeaseTime < now && res.Value.LeaseTime+int64(m.e) > now {
time.Sleep(time.Duration(m.e) * time.Nanosecond)
return m.getLease(k)
}
var lease LeaseValue
if res == nil || (res.Value.Node == 0 && res.Value.LeaseTime == 0) ||
res.Value.LeaseTime+int64(m.e) < now || res.Value.Node == m.node {
lease.Node = m.node
lease.LeaseTime = now + int64(m.expiredDuration)*int64(time.Second)
logF(m.node, "refresh or create lease, %+v", lease)
} else {
lease = res.Value
logF(m.node, "keep old lease, %+v", lease)
}
logF(m.node, "try to write lease, %+v", lease)
if m.write(k, &lease) {
return &lease, true
}
return nil, false
}