-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
239 lines (204 loc) · 5.43 KB
/
service.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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"sync/atomic"
"time"
"github.com/alphauslabs/juno/internal"
"github.com/alphauslabs/juno/internal/flags"
"github.com/alphauslabs/juno/internal/fleet"
pb "github.com/alphauslabs/juno/proto/v1"
"github.com/golang/glog"
gaxv2 "github.com/googleapis/gax-go/v2"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type service struct {
fd *fleet.FleetData
pb.UnimplementedJunoServer
}
func (s *service) Lock(stream pb.Juno_LockServer) error {
if atomic.LoadInt32(&s.fd.Online) == 0 {
m := fmt.Sprintf("Node %v not yet ready. Please try again later.", *flags.Id)
return status.Errorf(codes.Unavailable, m)
}
defer func(begin time.Time) { glog.Infof("method Lock took %v", time.Since(begin)) }(time.Now())
ctx := stream.Context()
var attempts int
bo := gaxv2.Backoff{} // 30s
for {
if !fleet.NoLeader(s.fd) {
break
} else {
attempts++
if attempts >= 10 {
return status.Errorf(codes.Unavailable, fleet.ErrNoLeader.Error())
}
time.Sleep(bo.Pause())
continue
}
}
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
req, err := stream.Recv()
if err == io.EOF {
return nil
}
b, _ := json.Marshal(req)
glog.Infof("stream req: %v", string(b))
if err != nil {
glog.Errorf("Recv failed: %v", err)
return err
}
switch {
case req.Name != "" && !req.Release && !req.Extend: // acquire lock
var available bool
lockState := s.fd.StateMachine.GetLockState(req.Name) // from local copy
if lockState.Name != "" && lockState.State == 0 {
available = true
}
if available {
var duration int64 = 5
if req.LeaseTime > duration {
duration = req.LeaseTime
}
b, _ := json.Marshal(internal.NewEvent(
fleet.ReachConsensusInput{
CmdType: fleet.CmdTypeGetLock,
Key: req.Name, // lock name
Value: fmt.Sprintf("%v", duration), // lease duration
},
fleet.EventSource,
fleet.CtrlLeaderFwdConsensus,
))
outb, err := fleet.SendToLeader(ctx, s.fd.App, b)
if err != nil {
return status.Errorf(codes.Internal, err.Error())
}
var out *fleet.ReachConsensusOutput
json.Unmarshal(outb, &out)
limit := 5 // 5s max retry limit
attempts = 0 // reset
for {
if ctx.Err() != nil {
return ctx.Err()
}
s.fd.SetValueMtx.Lock()
copy, ok := s.fd.SetValueHistory[int(out.Round)]
s.fd.SetValueMtx.Unlock()
if ok && copy.Applied {
break
}
time.Sleep(time.Millisecond * 1)
attempts++
if attempts > 0 && (attempts%1000 == 0) {
glog.Infof("checkpoint: Cannot get value for round %v.", out.Round)
limit--
if limit <= 0 {
return status.Errorf(codes.Internal, "Cannot get value for round %v.", out.Round)
}
}
}
lockState = s.fd.StateMachine.GetLockState(req.Name) // from local copy
if lockState.State == 1 {
resp := pb.LockResponse{
Name: req.Name,
Token: "token",
}
if err := stream.Send(&resp); err != nil {
glog.Errorf("Send failed: %v", err)
return err
}
}
} else {
return status.Errorf(codes.Unavailable, "Lock unavailable. Add wait option (todo).")
}
}
}
}
func (s *service) AddToSet(ctx context.Context, req *pb.AddToSetRequest) (*pb.AddToSetResponse, error) {
if atomic.LoadInt32(&s.fd.Online) == 0 {
m := fmt.Sprintf("Node %v not yet ready. Please try again later.", *flags.Id)
return nil, status.Errorf(codes.Unavailable, m)
}
var leader int
var attempts int
defer func(begin time.Time, l, a *int) {
glog.Infof("method AddToSet (leader?=%v, attempts=%v) took %v", *l, *a, time.Since(begin))
}(time.Now(), &leader, &attempts)
reqb, _ := json.Marshal(req)
glog.Infof("request=%v", string(reqb))
bo := gaxv2.Backoff{} // 30s
for {
if !fleet.NoLeader(s.fd) {
break
} else {
attempts++
if attempts >= 10 {
return nil, status.Errorf(codes.Unavailable, fleet.ErrNoLeader.Error())
}
time.Sleep(bo.Pause())
continue
}
}
var out *fleet.ReachConsensusOutput
var err error
switch {
case fleet.IsLeader(s.fd):
leader = 1
out, err = fleet.ReachConsensus(ctx, &fleet.ReachConsensusInput{
FleetData: s.fd,
CmdType: fleet.CmdTypeAddToSet,
Key: req.Key,
Value: req.Value,
})
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
default:
b, _ := json.Marshal(internal.NewEvent(
fleet.ReachConsensusInput{
CmdType: fleet.CmdTypeAddToSet,
Key: req.Key,
Value: req.Value,
},
fleet.EventSource,
fleet.CtrlLeaderFwdConsensus,
))
outb, err := fleet.SendToLeader(ctx, s.fd.App, b)
if err != nil {
return nil, status.Errorf(codes.Internal, err.Error())
}
json.Unmarshal(outb, &out)
}
limit := 5 // 5s max retry limit
attempts = 0 // reset
for {
if ctx.Err() != nil {
return nil, ctx.Err()
}
s.fd.SetValueMtx.Lock()
copy, ok := s.fd.SetValueHistory[int(out.Round)]
s.fd.SetValueMtx.Unlock()
if ok && copy.Applied {
break
}
time.Sleep(time.Millisecond * 1)
attempts++
if attempts > 0 && (attempts%1000 == 0) {
glog.Infof("checkpoint: Cannot get value for round %v.", out.Round)
limit--
if limit <= 0 {
return nil, status.Errorf(codes.Internal, "Cannot get value for round %v.", out.Round)
}
}
}
count := len(s.fd.StateMachine.Members(req.Key))
return &pb.AddToSetResponse{Key: out.Key, Count: int64(count)}, nil
}