-
Notifications
You must be signed in to change notification settings - Fork 72
/
interceptors_idempotency.go
434 lines (375 loc) · 11.9 KB
/
interceptors_idempotency.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package gocsi
import (
"context"
"sync"
"time"
"github.com/container-storage-interface/spec/lib/go/csi"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
xctx "golang.org/x/net/context"
)
// IdempotencyProvider is the interface that works with a server-side,
// gRPC interceptor to provide serial access and idempotency for CSI's
// volume resources.
type IdempotencyProvider interface {
// GetVolumeID should return the ID of the volume specified
// by the provided volume name. If the volume does not exist then
// an empty string should be returned.
GetVolumeID(ctx context.Context, name string) (string, error)
// GetVolumeInfo should return information about the volume
// specified by the provided volume ID or name. If the volume does not
// exist then a nil value should be returned.
GetVolumeInfo(ctx context.Context, id, name string) (*csi.VolumeInfo, error)
// IsControllerPublished should return publication for a volume's
// publication status on a specified node.
IsControllerPublished(
ctx context.Context,
volumeID, nodeID string) (map[string]string, error)
// IsNodePublished should return a flag indicating whether or
// not the volume exists and is published on the current host.
IsNodePublished(
ctx context.Context,
id string,
pubVolInfo map[string]string,
targetPath string) (bool, error)
}
// IdempotentInterceptorOption configures the idempotent interceptor.
type IdempotentInterceptorOption func(*idempIntercOpts)
type idempIntercOpts struct {
timeout time.Duration
requireVolume bool
}
// WithIdempTimeout is an IdempotentInterceptorOption that sets the
// timeout used by the idempotent interceptor.
func WithIdempTimeout(t time.Duration) IdempotentInterceptorOption {
return func(o *idempIntercOpts) {
o.timeout = t
}
}
// WithIdempRequireVolumeExists is an IdempotentInterceptorOption that
// enforces the requirement that volumes must exist before proceeding
// with an operation.
func WithIdempRequireVolumeExists() IdempotentInterceptorOption {
return func(o *idempIntercOpts) {
o.requireVolume = true
}
}
// NewIdempotentInterceptor returns a new server-side, gRPC interceptor
// that can be used in conjunction with an IdempotencyProvider to
// provide serialized, idempotent access to the following CSI RPCs:
//
// * CreateVolume
// * DeleteVolume
// * ControllerPublishVolume
// * ControllerUnpublishVolume
// * NodePublishVolume
// * NodeUnpublishVolume
func NewIdempotentInterceptor(
p IdempotencyProvider,
opts ...IdempotentInterceptorOption) grpc.UnaryServerInterceptor {
i := &idempotencyInterceptor{
p: p,
volIDLocks: map[string]*volLockInfo{},
volNameLocks: map[string]*volLockInfo{},
}
// Configure the idempotent interceptor's options.
for _, setOpt := range opts {
setOpt(&i.opts)
}
return i.handle
}
type volLockInfo struct {
MutexWithTryLock
methodInErr map[string]struct{}
}
type idempotencyInterceptor struct {
p IdempotencyProvider
volIDLocksL sync.Mutex
volNameLocksL sync.Mutex
volIDLocks map[string]*volLockInfo
volNameLocks map[string]*volLockInfo
opts idempIntercOpts
}
func (i *idempotencyInterceptor) lockWithID(id string) *volLockInfo {
i.volIDLocksL.Lock()
defer i.volIDLocksL.Unlock()
lock := i.volIDLocks[id]
if lock == nil {
lock = &volLockInfo{
MutexWithTryLock: NewMutexWithTryLock(),
methodInErr: map[string]struct{}{},
}
i.volIDLocks[id] = lock
}
return lock
}
func (i *idempotencyInterceptor) lockWithName(name string) *volLockInfo {
i.volNameLocksL.Lock()
defer i.volNameLocksL.Unlock()
lock := i.volNameLocks[name]
if lock == nil {
lock = &volLockInfo{
MutexWithTryLock: NewMutexWithTryLock(),
methodInErr: map[string]struct{}{},
}
i.volNameLocks[name] = lock
}
return lock
}
func (i *idempotencyInterceptor) handle(
ctx xctx.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (interface{}, error) {
switch treq := req.(type) {
case *csi.ControllerPublishVolumeRequest:
return i.controllerPublishVolume(ctx, treq, info, handler)
case *csi.ControllerUnpublishVolumeRequest:
return i.controllerUnpublishVolume(ctx, treq, info, handler)
case *csi.CreateVolumeRequest:
return i.createVolume(ctx, treq, info, handler)
case *csi.DeleteVolumeRequest:
return i.deleteVolume(ctx, treq, info, handler)
case *csi.NodePublishVolumeRequest:
return i.nodePublishVolume(ctx, treq, info, handler)
case *csi.NodeUnpublishVolumeRequest:
return i.nodeUnpublishVolume(ctx, treq, info, handler)
}
return handler(ctx, req)
}
func (i *idempotencyInterceptor) controllerPublishVolume(
ctx context.Context,
req *csi.ControllerPublishVolumeRequest,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (res interface{}, resErr error) {
lock := i.lockWithID(req.VolumeId)
if !lock.TryLock(i.opts.timeout) {
return nil, ErrOpPending
}
defer lock.Unlock()
// If configured to do so, check to see if the volume exists and
// return an error if it does not.
if i.opts.requireVolume {
volInfo, err := i.p.GetVolumeInfo(ctx, req.VolumeId, "")
if err != nil {
return nil, err
}
if volInfo == nil {
return nil, ErrVolumeNotFound(req.VolumeId)
}
}
pubInfo, err := i.p.IsControllerPublished(ctx, req.VolumeId, req.NodeId)
if err != nil {
return nil, err
}
if pubInfo != nil {
log.WithField("volumeID", req.VolumeId).Info(
"idempotent controller publish")
return &csi.ControllerPublishVolumeResponse{
PublishVolumeInfo: pubInfo,
}, nil
}
return handler(ctx, req)
}
func (i *idempotencyInterceptor) controllerUnpublishVolume(
ctx context.Context,
req *csi.ControllerUnpublishVolumeRequest,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (res interface{}, resErr error) {
lock := i.lockWithID(req.VolumeId)
if !lock.TryLock(i.opts.timeout) {
return nil, ErrOpPending
}
defer lock.Unlock()
// If configured to do so, check to see if the volume exists and
// return an error if it does not.
if i.opts.requireVolume {
volInfo, err := i.p.GetVolumeInfo(ctx, req.VolumeId, "")
if err != nil {
return nil, err
}
if volInfo == nil {
return nil, ErrVolumeNotFound(req.VolumeId)
}
}
pubInfo, err := i.p.IsControllerPublished(ctx, req.VolumeId, req.NodeId)
if err != nil {
return nil, err
}
if pubInfo == nil {
log.WithField("volumeID", req.VolumeId).Info(
"idempotent controller unpublish")
return &csi.ControllerUnpublishVolumeResponse{}, nil
}
return handler(ctx, req)
}
func (i *idempotencyInterceptor) createVolume(
ctx context.Context,
req *csi.CreateVolumeRequest,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (res interface{}, resErr error) {
reqID, _ := GetRequestID(ctx)
fields := map[string]interface{}{
"requestID": reqID,
"volumeName": req.Name,
}
log.WithFields(fields).Debug("idemp: begin createVolume")
// First attempt to lock the volume by the provided name. If no lock
// can be obtained then exit with the appropriate error.
nameLock := i.lockWithName(req.Name)
if !nameLock.TryLock(i.opts.timeout) {
return nil, ErrOpPending
}
defer nameLock.Unlock()
// Next, attempt to get the volume info based on the name.
volInfo, err := i.p.GetVolumeInfo(ctx, "", req.Name)
if err != nil {
return nil, err
}
// If the volInfo is nil then it means the volume does not exist.
// Return early, passing control to the next handler in the chain.
if volInfo == nil {
log.WithFields(fields).Debug("creating volume")
return handler(ctx, req)
}
// If the volInfo is not nil it means the volume already exists.
// The volume info contains the volume's ID. Use that to obtain a
// volume ID-based lock for the volume.
idLock := i.lockWithID(volInfo.Id)
if !idLock.TryLock(i.opts.timeout) {
return nil, ErrOpPending
}
defer idLock.Unlock()
// The ID lock has been obtained. Once again call GetVolumeInfo,
// this time with the volume ID, now that the ID lock is held.
// This ensures the volume still exists since it could have been
// removed in the time it took to obtain the ID lock.
volInfo, err = i.p.GetVolumeInfo(ctx, volInfo.Id, "")
if err != nil {
return nil, err
}
// If the volume info is nil it means the volume was removed in
// the time it took to obtain the lock ID. Return early, passing
// control to the next handler in the chain.
if volInfo == nil {
log.WithFields(fields).Debug("creating volume: 2nd attempt")
return handler(ctx, req)
}
// If the volume info still exists then it means the volume
// exists! Go ahead and return the volume info and note this
// as an idempotent create call.
log.WithFields(map[string]interface{}{
"requestID": reqID,
"volumeID": volInfo.Id,
"volumeName": req.Name}).Info("idempotent create")
// TODO It would be nice if this idempotent response could include
// an error denoting such. Please see https://goo.gl/xEG72U
// for more information.
return &csi.CreateVolumeResponse{
VolumeInfo: volInfo,
}, nil // status.Error(codes.AlreadyExists, req.Name)
}
func (i *idempotencyInterceptor) deleteVolume(
ctx context.Context,
req *csi.DeleteVolumeRequest,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (res interface{}, resErr error) {
lock := i.lockWithID(req.VolumeId)
if !lock.TryLock(i.opts.timeout) {
return nil, ErrOpPending
}
defer lock.Unlock()
// If configured to do so, check to see if the volume exists and
// return an error if it does not.
var volExists bool
if i.opts.requireVolume {
volInfo, err := i.p.GetVolumeInfo(ctx, req.VolumeId, "")
if err != nil {
return nil, err
}
if volInfo == nil {
log.WithField("volumeID", req.VolumeId).Info("idempotent delete.a")
return nil, ErrVolumeNotFound(req.VolumeId)
}
volExists = true
}
// Check to see if the volume exists if that has not yet been
// verified above.
if !volExists {
volInfo, err := i.p.GetVolumeInfo(ctx, req.VolumeId, "")
if err != nil {
return nil, err
}
volExists = volInfo != nil
}
// Indicate an idempotent delete operation if the volume does not exist.
if !volExists {
log.WithField("volumeID", req.VolumeId).Info("idempotent delete.b")
return nil, ErrVolumeNotFound(req.VolumeId)
}
return handler(ctx, req)
}
func (i *idempotencyInterceptor) nodePublishVolume(
ctx context.Context,
req *csi.NodePublishVolumeRequest,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (res interface{}, resErr error) {
lock := i.lockWithID(req.VolumeId)
if !lock.TryLock(i.opts.timeout) {
return nil, ErrOpPending
}
defer lock.Unlock()
// If configured to do so, check to see if the volume exists and
// return an error if it does not.
if i.opts.requireVolume {
volInfo, err := i.p.GetVolumeInfo(ctx, req.VolumeId, "")
if err != nil {
return nil, err
}
if volInfo == nil {
return nil, ErrVolumeNotFound(req.VolumeId)
}
}
ok, err := i.p.IsNodePublished(
ctx, req.VolumeId, req.PublishVolumeInfo, req.TargetPath)
if err != nil {
return nil, err
}
if ok {
log.WithField("volumeID", req.VolumeId).Info("idempotent node publish")
return &csi.NodePublishVolumeResponse{}, nil
}
return handler(ctx, req)
}
func (i *idempotencyInterceptor) nodeUnpublishVolume(
ctx context.Context,
req *csi.NodeUnpublishVolumeRequest,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler) (res interface{}, resErr error) {
lock := i.lockWithID(req.VolumeId)
if !lock.TryLock(i.opts.timeout) {
return nil, ErrOpPending
}
defer lock.Unlock()
// If configured to do so, check to see if the volume exists and
// return an error if it does not.
if i.opts.requireVolume {
volInfo, err := i.p.GetVolumeInfo(ctx, req.VolumeId, "")
if err != nil {
return nil, err
}
if volInfo == nil {
return nil, ErrVolumeNotFound(req.VolumeId)
}
}
ok, err := i.p.IsNodePublished(ctx, req.VolumeId, nil, req.TargetPath)
if err != nil {
return nil, err
}
if !ok {
log.WithField("volumeID", req.VolumeId).Info(
"idempotent node unpublish")
return &csi.NodeUnpublishVolumeResponse{}, nil
}
return handler(ctx, req)
}