/
control.go
251 lines (224 loc) · 5.55 KB
/
control.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
package exclusion
import (
"context"
"fmt"
"github.com/daichitakahashi/confort/internal/beacon/proto"
"go.uber.org/multierr"
"google.golang.org/protobuf/types/known/emptypb"
)
type Control interface {
LockForNamespace(ctx context.Context) (func(), error)
LockForBuild(ctx context.Context, image string) (func(), error)
LockForContainerSetup(ctx context.Context, name string) (func(), error)
LockForContainerUse(ctx context.Context, params map[string]ContainerUseParam) (unlock func(), err error)
}
type control struct {
l *Locker
}
func NewControl() Control {
return &control{
l: NewLocker(),
}
}
func (c *control) LockForNamespace(_ context.Context) (func(), error) {
unlock := c.l.LockForNamespace()
return unlock, nil
}
func (c *control) LockForBuild(ctx context.Context, image string) (func(), error) {
return c.l.LockForBuild(ctx, image)
}
func (c *control) LockForContainerSetup(ctx context.Context, name string) (func(), error) {
return c.l.LockForContainerSetup(ctx, name)
}
type ContainerUseParam struct {
Exclusive bool
Init func(ctx context.Context) error
}
func (c *control) LockForContainerUse(ctx context.Context, params map[string]ContainerUseParam) (unlock func(), err error) {
entries := map[string]*AcquireContainerLockEntry{}
for name, param := range params {
entries[name] = &AcquireContainerLockEntry{
Exclusive: param.Exclusive,
Init: param.Init != nil,
}
}
unlock, err = c.l.AcquireContainerLock(ctx, entries)
if err != nil {
return nil, err
}
var initErr error
for name, entry := range entries {
lock := entry.ContainerLock()
if lock.InitAcquired() {
if initErr == nil {
initErr = initSafe(ctx, params[name].Init)
}
lock.SetInitResult(initErr == nil)
}
}
if initErr != nil {
unlock()
return nil, initErr
}
return unlock, nil
}
func initSafe(ctx context.Context, init func(ctx context.Context) error) (err error) {
defer func() {
r := recover()
if r != nil {
err = fmt.Errorf("%v", r)
}
}()
return init(ctx)
}
var _ Control = (*control)(nil)
type beaconControl struct {
cli proto.BeaconServiceClient
}
func NewBeaconControl(cli proto.BeaconServiceClient) Control {
return &beaconControl{
cli: cli,
}
}
func (b *beaconControl) LockForNamespace(ctx context.Context) (func(), error) {
stream, err := b.cli.LockForNamespace(ctx)
if err != nil {
return nil, err
}
err = stream.Send(&proto.LockRequest{
Operation: proto.LockOp_LOCK_OP_LOCK,
})
if err != nil {
return nil, err
}
_, err = stream.Recv()
if err != nil {
return nil, err
}
return func() {
err := stream.Send(&proto.LockRequest{
Operation: proto.LockOp_LOCK_OP_UNLOCK,
})
_ = err // TODO: error handling
_ = stream.CloseSend()
}, nil
}
func (b *beaconControl) LockForBuild(ctx context.Context, image string) (func(), error) {
stream, err := b.cli.LockForBuild(ctx)
if err != nil {
return nil, err
}
err = stream.Send(&proto.KeyedLockRequest{
Key: image,
Operation: proto.LockOp_LOCK_OP_LOCK,
})
if err != nil {
return nil, err
}
_, err = stream.Recv()
if err != nil {
return nil, err
}
return func() {
err := stream.Send(&proto.KeyedLockRequest{
Key: image,
Operation: proto.LockOp_LOCK_OP_UNLOCK,
})
_ = err // TODO: error handling
_ = stream.CloseSend()
}, nil
}
func (b *beaconControl) LockForContainerSetup(ctx context.Context, name string) (func(), error) {
stream, err := b.cli.LockForContainerSetup(ctx)
if err != nil {
return nil, err
}
err = stream.Send(&proto.KeyedLockRequest{
Key: name,
Operation: proto.LockOp_LOCK_OP_LOCK,
})
if err != nil {
return nil, err
}
_, err = stream.Recv()
if err != nil {
return nil, err
}
return func() {
err := stream.Send(&proto.KeyedLockRequest{
Key: name,
Operation: proto.LockOp_LOCK_OP_UNLOCK,
})
_ = err // TODO: error handling
_ = stream.CloseSend()
}, nil
}
func (b *beaconControl) LockForContainerUse(ctx context.Context, params map[string]ContainerUseParam) (unlock func(), err error) {
targets := map[string]*proto.AcquireLockParam{}
for name, param := range params {
var op proto.AcquireOp
if param.Exclusive {
if param.Init == nil {
op = proto.AcquireOp_ACQUIRE_OP_LOCK
} else {
op = proto.AcquireOp_ACQUIRE_OP_INIT_LOCK
}
} else {
if param.Init == nil {
op = proto.AcquireOp_ACQUIRE_OP_SHARED_LOCK
} else {
op = proto.AcquireOp_ACQUIRE_OP_INIT_SHARED_LOCK
}
}
targets[name] = &proto.AcquireLockParam{
Operation: op,
}
}
stream, err := b.cli.AcquireContainerLock(ctx)
if err != nil {
return nil, err
}
err = stream.Send(&proto.AcquireLockRequest{
Param: &proto.AcquireLockRequest_Acquire{
Acquire: &proto.AcquireLockAcquireParam{
Targets: targets,
},
},
})
if err != nil {
return nil, err
}
resp, err := stream.Recv()
if err != nil {
return nil, err
}
for name, result := range resp.GetResults() {
if result.GetAcquireInit() {
initErr := initSafe(ctx, params[name].Init)
err = stream.Send(&proto.AcquireLockRequest{
Param: &proto.AcquireLockRequest_Init{
Init: &proto.AcquireLockInitParam{
Key: name,
InitSucceeded: initErr == nil,
},
},
})
if err != nil {
return nil, multierr.Append(initErr, err)
}
if initErr != nil {
return nil, multierr.Append(initErr, stream.CloseSend())
}
}
}
return func() {
err := stream.Send(&proto.AcquireLockRequest{
Param: &proto.AcquireLockRequest_Release{
Release: &emptypb.Empty{},
},
})
_ = err // TODO: error handling
_ = stream.CloseSend()
}, nil
}
var _ Control = (*beaconControl)(nil)