-
Notifications
You must be signed in to change notification settings - Fork 36
/
migrate_handler.go
602 lines (545 loc) · 23 KB
/
migrate_handler.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
package gater
import (
"encoding/hex"
"encoding/json"
"net/http"
"strings"
"time"
sdktypes "github.com/cosmos/cosmos-sdk/types"
permissiontypes "github.com/bnb-chain/greenfield/x/permission/types"
"github.com/bnb-chain/greenfield-storage-provider/base/types/gfsperrors"
"github.com/bnb-chain/greenfield-storage-provider/base/types/gfsptask"
"github.com/bnb-chain/greenfield-storage-provider/core/piecestore"
coretask "github.com/bnb-chain/greenfield-storage-provider/core/task"
modelgateway "github.com/bnb-chain/greenfield-storage-provider/model/gateway"
"github.com/bnb-chain/greenfield-storage-provider/pkg/log"
"github.com/bnb-chain/greenfield/types/common"
sptypes "github.com/bnb-chain/greenfield/x/sp/types"
storagetypes "github.com/bnb-chain/greenfield/x/storage/types"
virtualgrouptypes "github.com/bnb-chain/greenfield/x/virtualgroup/types"
)
// dest sp receive migrate gvg notify from src sp.
func (g *GateModular) notifyMigrateSwapOutHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
reqCtx *RequestContext
swapOutMsg []byte
)
defer func() {
reqCtx.Cancel()
if err != nil {
reqCtx.SetError(err)
reqCtx.SetHTTPCode(int(gfsperrors.MakeGfSpError(err).GetHttpStatusCode()))
modelgateway.MakeErrorResponse(w, gfsperrors.MakeGfSpError(err))
} else {
reqCtx.SetHTTPCode(http.StatusOK)
}
log.CtxDebugw(reqCtx.Context(), reqCtx.String())
}()
reqCtx, _ = NewRequestContext(r, g)
migrateSwapOutHeader := r.Header.Get(GnfdMigrateSwapOutMsgHeader)
if swapOutMsg, err = hex.DecodeString(migrateSwapOutHeader); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to parse migrate swap out header", "error", err)
err = ErrDecodeMsg
return
}
swapOut := virtualgrouptypes.MsgSwapOut{}
if err = json.Unmarshal(swapOutMsg, &swapOut); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to unmarshal migrate swap out msg", "error", err)
err = ErrDecodeMsg
return
}
if err = g.baseApp.GfSpClient().NotifyMigrateSwapOut(reqCtx.Context(), &swapOut); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to notify migrate swap out", "swap_out", swapOut, "error", err)
err = ErrNotifySwapOutWithDetail("failed to notify migrate swap out, swap_out: " + swapOut.String() + ",error: " + err.Error())
return
}
}
func (g *GateModular) checkMigratePieceAuth(reqCtx *RequestContext, migrateGVGHeader string) (bool, error) {
var (
err error
migrateGVGMsg []byte
)
migrateGVGMsg, err = hex.DecodeString(migrateGVGHeader)
if err != nil {
log.Errorw("failed to parse migrate gvg header", "migrate_gvg_header", migrateGVGHeader, "error", err)
return false, ErrDecodeMsg
}
migrateGVG := gfsptask.GfSpMigrateGVGTask{}
err = json.Unmarshal(migrateGVGMsg, &migrateGVG)
if err != nil {
log.Errorw("failed to unmarshal migrate gvg msg", "error", err)
return false, ErrDecodeMsg
}
if migrateGVG.GetExpireTime() < time.Now().Unix() {
log.Errorw("failed to check migrate gvg expire time", "gvg_task", migrateGVG)
return false, ErrNoPermission
}
destSPAddr, err := reqCtx.verifyTaskSignature(migrateGVG.GetSignBytes(), migrateGVG.GetSignature())
if err != nil {
log.Errorw("failed to verify task signature", "gvg_task", migrateGVG, "error", err)
return false, err
}
sp, err := g.spCachePool.QuerySPByAddress(destSPAddr.String())
if err != nil {
log.Errorw("failed to query sp", "gvg_task", migrateGVG, "dest_sp_addr", destSPAddr.String(), "error", err)
return false, err
}
effect, err := g.baseApp.GfSpClient().VerifyMigrateGVGPermission(reqCtx.Context(), migrateGVG.GetBucketID(), migrateGVG.GetSrcGvg().GetId(), sp.GetId())
if effect == nil || err != nil {
log.Errorw("failed to verify migrate gvg permission", "gvg_bucket_id", migrateGVG.GetBucketID(),
"src_gvg_id", migrateGVG.GetSrcGvg().GetId(), "dest_sp", sp.GetId(), "effect", effect, "error", err)
return false, err
}
if *effect == permissiontypes.EFFECT_ALLOW {
return true, nil
}
return false, nil
}
// checkMigrateBucketQuotaAuth parse migrateBucketMsgHeader to GfSpBucketMigrationInfo and check if request has permission
// only used by preMigrateBucketHandler and postMigrateBucketHandler
func (g *GateModular) checkMigrateBucketQuotaAuth(reqCtx *RequestContext, migrateBucketMsgHeader string) (bool, *gfsptask.GfSpBucketMigrationInfo, error) {
allow, bucketMigrationInfo, sp, err := g.verifySignatureAndSP(reqCtx, migrateBucketMsgHeader)
if !allow {
return allow, bucketMigrationInfo, err
}
effect, err := g.baseApp.GfSpClient().VerifyMigrateGVGPermission(reqCtx.Context(), bucketMigrationInfo.GetBucketId(), 0 /*not used*/, sp.GetId())
if effect == nil {
if err != nil && strings.Contains(err.Error(), "the bucket is not in migration status") {
// metadata service support cancel & completion migrate bucket
return true, bucketMigrationInfo, nil
}
log.Errorw("failed to verify migrate bucket permission", "bucket_migration_info", bucketMigrationInfo,
"dest_sp", sp.GetId(), "effect", effect, "error", err)
return false, bucketMigrationInfo, err
}
return *effect == permissiontypes.EFFECT_ALLOW, bucketMigrationInfo, nil
}
// verifySignatureAndSP parse migrateBucketMsgHeader to GfSpBucketMigrationInfo and check if request has permission
func (g *GateModular) verifySignatureAndSP(reqCtx *RequestContext, migrateBucketMsgHeader string) (bool, *gfsptask.GfSpBucketMigrationInfo, *sptypes.StorageProvider, error) {
var (
err error
migrateBucketMsg []byte
sp *sptypes.StorageProvider
)
migrateBucketMsg, err = hex.DecodeString(migrateBucketMsgHeader)
if err != nil {
log.Errorw("failed to parse migrate bucket msg header", "migrate_bucket_header", migrateBucketMsg, "error", err)
return false, &gfsptask.GfSpBucketMigrationInfo{}, sp, ErrDecodeMsg
}
bucketMigrationInfo := gfsptask.GfSpBucketMigrationInfo{}
if err = json.Unmarshal(migrateBucketMsg, &bucketMigrationInfo); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to unmarshal post migrate bucket msg", "error", err)
err = ErrDecodeMsg
return false, &bucketMigrationInfo, sp, err
}
if bucketMigrationInfo.GetExpireTime() < time.Now().Unix() {
log.Errorw("failed to check migrate bucket expire time", "bucket_migration_info", bucketMigrationInfo)
return false, &bucketMigrationInfo, sp, ErrNoPermission
}
destSPAddr, err := reqCtx.verifyTaskSignature(bucketMigrationInfo.GetSignBytes(), bucketMigrationInfo.GetSignature())
if err != nil {
log.Errorw("failed to verify task signature", "bucket_migration_info", bucketMigrationInfo, "error", err)
return false, &bucketMigrationInfo, sp, err
}
sp, err = g.spCachePool.QuerySPByAddress(destSPAddr.String())
if err != nil {
log.Errorw("failed to query sp", "bucket_migration_info", bucketMigrationInfo, "dest_sp_addr", destSPAddr.String(), "error", err)
return false, &bucketMigrationInfo, sp, err
}
return true, &bucketMigrationInfo, sp, nil
}
// migratePieceHandler handles migrate piece request between SPs which is used in SP exiting case.
func (g *GateModular) migratePieceHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
allowMigrate bool
reqCtx *RequestContext
migratePieceMsg []byte
pieceKey string
pieceSize int64
pieceData []byte
)
defer func() {
reqCtx.Cancel()
if err != nil {
reqCtx.SetError(err)
reqCtx.SetHTTPCode(int(gfsperrors.MakeGfSpError(err).GetHttpStatusCode()))
modelgateway.MakeErrorResponse(w, gfsperrors.MakeGfSpError(err))
} else {
reqCtx.SetHTTPCode(http.StatusOK)
}
log.CtxDebugw(reqCtx.Context(), reqCtx.String())
}()
reqCtx, _ = NewRequestContext(r, g)
migratePieceHeader := r.Header.Get(GnfdMigratePieceMsgHeader)
migratePieceMsg, err = hex.DecodeString(migratePieceHeader)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to parse migrate piece header", "error", err)
err = ErrDecodeMsg
return
}
migratePiece := gfsptask.GfSpMigratePieceTask{}
err = json.Unmarshal(migratePieceMsg, &migratePiece)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to unmarshal migrate piece msg", "error", err)
err = ErrDecodeMsg
return
}
if allowMigrate, err = g.checkMigratePieceAuth(reqCtx, r.Header.Get(GnfdMigrateGVGMsgHeader)); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to check migrate piece auth", "migrate_piece", migratePiece, "error", err)
return
}
if !allowMigrate {
log.CtxErrorw(reqCtx.Context(), "no permission to migrate piece", "migrate_piece", migratePiece)
err = ErrNoPermission
return
}
objectInfo := migratePiece.GetObjectInfo()
if objectInfo == nil {
log.CtxError(reqCtx.Context(), "failed to get migrate piece object info due to has no object info")
err = ErrInvalidHeader
return
}
chainObjectInfo, bucketInfo, params, err := g.getObjectChainMeta(reqCtx.Context(), objectInfo.GetObjectName(), objectInfo.GetBucketName())
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to get object on chain meta", "error", err)
err = ErrInvalidHeader
return
}
redundancyNumber := int32(migratePiece.GetStorageParams().GetRedundantDataChunkNum()+migratePiece.GetStorageParams().GetRedundantParityChunkNum()) - 1
objectID := migratePiece.GetObjectInfo().Id.Uint64()
objectVersion := migratePiece.GetObjectInfo().GetVersion()
segmentIdx := migratePiece.GetSegmentIdx()
redundancyIdx := migratePiece.GetRedundancyIdx()
if redundancyIdx < piecestore.PrimarySPRedundancyIndex || redundancyIdx > redundancyNumber {
err = ErrInvalidRedundancyIndex
return
}
if redundancyIdx == piecestore.PrimarySPRedundancyIndex {
pieceKey = g.baseApp.PieceOp().SegmentPieceKey(objectID, segmentIdx, objectVersion)
pieceSize = g.baseApp.PieceOp().SegmentPieceSize(migratePiece.ObjectInfo.GetPayloadSize(),
segmentIdx, migratePiece.GetStorageParams().GetMaxSegmentSize())
} else {
pieceKey = g.baseApp.PieceOp().ECPieceKey(objectID, segmentIdx, uint32(redundancyIdx), objectVersion)
pieceSize = g.baseApp.PieceOp().ECPieceSize(migratePiece.ObjectInfo.GetPayloadSize(), segmentIdx,
migratePiece.GetStorageParams().GetMaxSegmentSize(), migratePiece.GetStorageParams().GetRedundantDataChunkNum())
}
pieceTask := &gfsptask.GfSpDownloadPieceTask{}
pieceTask.InitDownloadPieceTask(chainObjectInfo, bucketInfo, params, coretask.DefaultSmallerPriority, false, bucketInfo.Owner,
uint64(pieceSize), pieceKey, 0, uint64(pieceSize),
g.baseApp.TaskTimeout(pieceTask, objectInfo.GetPayloadSize()), g.baseApp.TaskMaxRetry(pieceTask))
pieceData, err = g.baseApp.GfSpClient().GetPiece(reqCtx.Context(), pieceTask)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to download segment piece", "piece_key", pieceKey, "error", err)
return
}
_, err = w.Write(pieceData)
if err != nil {
err = ErrReplyData
log.CtxErrorw(reqCtx.Context(), "failed to reply the migrate data", "error", err)
return
}
log.CtxInfow(reqCtx.Context(), "succeed to migrate one piece", "object_id", objectID, "segment_piece_index",
segmentIdx, "redundancy_index", redundancyIdx)
}
// getSecondaryBlsMigrationBucketApprovalHandler handles the bucket migration approval request for secondarySP using bls
func (g *GateModular) getSecondaryBlsMigrationBucketApprovalHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
reqCtx *RequestContext
migrationBucketApprovalMsg []byte
)
defer func() {
reqCtx.Cancel()
if err != nil {
reqCtx.SetError(err)
reqCtx.SetHTTPCode(int(gfsperrors.MakeGfSpError(err).GetHttpStatusCode()))
modelgateway.MakeErrorResponse(w, gfsperrors.MakeGfSpError(err))
} else {
reqCtx.SetHTTPCode(http.StatusOK)
}
log.CtxDebugw(reqCtx.Context(), reqCtx.String())
}()
reqCtx, _ = NewRequestContext(r, g)
migrationBucketApprovalHeader := r.Header.Get(GnfdSecondarySPMigrationBucketMsgHeader)
migrationBucketApprovalMsg, err = hex.DecodeString(migrationBucketApprovalHeader)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to parse secondary migration bucket approval header", "error", err)
err = ErrDecodeMsg
return
}
signDoc := &storagetypes.SecondarySpMigrationBucketSignDoc{}
if err = storagetypes.ModuleCdc.UnmarshalJSON(migrationBucketApprovalMsg, signDoc); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to unmarshal migration bucket approval msg", "error", err)
err = ErrDecodeMsg
return
}
signature, err := g.baseApp.GfSpClient().SignSecondarySPMigrationBucket(reqCtx.Context(), signDoc)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to sign secondary sp migration bucket", "error", err)
err = ErrMigrateApprovalWithDetail("failed to sign secondary sp migration bucket, error: " + err.Error())
return
}
w.Header().Set(GnfdSecondarySPMigrationBucketApprovalHeader, hex.EncodeToString(signature))
log.CtxInfow(reqCtx.Context(), "succeed to sign secondary sp migration bucket approval", "bucket_id",
signDoc.BucketId.String())
}
func (g *GateModular) getSwapOutApproval(w http.ResponseWriter, r *http.Request) {
var (
err error
reqCtx *RequestContext
swapOutApprovalMsg []byte
)
defer func() {
reqCtx.Cancel()
if err != nil {
reqCtx.SetError(err)
reqCtx.SetHTTPCode(int(gfsperrors.MakeGfSpError(err).GetHttpStatusCode()))
modelgateway.MakeErrorResponse(w, gfsperrors.MakeGfSpError(err))
} else {
reqCtx.SetHTTPCode(http.StatusOK)
}
log.CtxDebugw(reqCtx.Context(), reqCtx.String())
}()
reqCtx, _ = NewRequestContext(r, g)
swapOutApprovalHeader := r.Header.Get(GnfdUnsignedApprovalMsgHeader)
swapOutApprovalMsg, err = hex.DecodeString(swapOutApprovalHeader)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to parse swap out approval header", "error", err)
err = ErrDecodeMsg
return
}
swapOutApproval := &virtualgrouptypes.MsgSwapOut{}
if err = virtualgrouptypes.ModuleCdc.UnmarshalJSON(swapOutApprovalMsg, swapOutApproval); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to unmarshal swap out approval msg", "error", err)
err = ErrDecodeMsg
return
}
swapOutApproval.SuccessorSpApproval = &common.Approval{ExpiredHeight: 100}
if err = swapOutApproval.ValidateBasic(); err != nil {
log.Errorw("failed to basic check approval msg", "swap_out_approval", swapOutApproval, "error", err)
err = ErrValidateMsg
return
}
log.Infow("get swap out approval", "msg", swapOutApproval)
signature, err := g.baseApp.GfSpClient().SignSwapOut(reqCtx.Context(), swapOutApproval)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to sign swap out", "error", err)
err = ErrMigrateApprovalWithDetail("failed to sign swap out, error: " + err.Error())
return
}
swapOutApproval.SuccessorSpApproval.Sig = signature
bz := storagetypes.ModuleCdc.MustMarshalJSON(swapOutApproval)
w.Header().Set(GnfdSignedApprovalMsgHeader, hex.EncodeToString(sdktypes.MustSortJSON(bz)))
log.CtxInfow(reqCtx.Context(), "succeed to sign swap out approval", "swap_out", swapOutApproval.String())
}
// getLatestBucketQuotaHandler handles the query quota request for bucket migrate
func (g *GateModular) getLatestBucketQuotaHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
reqCtx *RequestContext
bucketID uint64
bucketMigrationInfo *gfsptask.GfSpBucketMigrationInfo
allowMigrate bool
bz []byte
quota *gfsptask.GfSpBucketQuotaInfo
)
defer func() {
reqCtx.Cancel()
if err != nil {
reqCtx.SetError(err)
reqCtx.SetHTTPCode(int(gfsperrors.MakeGfSpError(err).GetHttpStatusCode()))
modelgateway.MakeErrorResponse(w, gfsperrors.MakeGfSpError(err))
} else {
reqCtx.SetHTTPCode(http.StatusOK)
}
log.CtxDebugw(reqCtx.Context(), reqCtx.String())
}()
reqCtx, _ = NewRequestContext(r, g)
if allowMigrate, bucketMigrationInfo, _, err = g.verifySignatureAndSP(reqCtx, r.Header.Get(GnfdMigrateBucketMsgHeader)); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to check migrate bucket auth", "bucket_migration_info", bucketMigrationInfo, "error", err)
return
}
if !allowMigrate {
log.CtxErrorw(reqCtx.Context(), "no permission to get latest bucket quota", "bucket_migration_info", bucketMigrationInfo)
err = ErrNoPermission
return
}
bucketID = bucketMigrationInfo.GetBucketId()
if quota, err = g.baseApp.GfSpClient().GetLatestBucketReadQuota(reqCtx.Context(), bucketID); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to get bucket read quota", "bucket_id",
bucketID, "error", err)
return
}
if bz, err = quota.Marshal(); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to marshal", "bucket_id",
bucketID, "error", err)
return
}
w.Header().Set(GnfdQuotaInfoHeader, hex.EncodeToString(bz))
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
log.CtxInfow(reqCtx.Context(), "succeed to get latest bucket quota", "bucket_id",
bucketID, "quota", quota)
}
// preMigrateBucketHandler handles the prepare request for bucket migrate
func (g *GateModular) preMigrateBucketHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
reqCtx *RequestContext
bucketID uint64
bucketSize uint64
bucketMigrationInfo *gfsptask.GfSpBucketMigrationInfo
allowMigrate bool
quota *gfsptask.GfSpBucketQuotaInfo
bz []byte
)
defer func() {
reqCtx.Cancel()
if err != nil {
reqCtx.SetError(err)
reqCtx.SetHTTPCode(int(gfsperrors.MakeGfSpError(err).GetHttpStatusCode()))
modelgateway.MakeErrorResponse(w, gfsperrors.MakeGfSpError(err))
} else {
reqCtx.SetHTTPCode(http.StatusOK)
}
log.CtxDebugw(reqCtx.Context(), reqCtx.String())
}()
reqCtx, _ = NewRequestContext(r, g)
if allowMigrate, bucketMigrationInfo, err = g.checkMigrateBucketQuotaAuth(reqCtx, r.Header.Get(GnfdMigrateBucketMsgHeader)); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to check migrate bucket auth", "bucket_migration_info", bucketMigrationInfo, "error", err)
return
}
if !allowMigrate {
log.CtxErrorw(reqCtx.Context(), "no permission to pre migrate bucket", "bucket_migration_info", bucketMigrationInfo)
err = ErrNoPermission
return
}
bucketID = bucketMigrationInfo.GetBucketId()
if quota, err = g.baseApp.GfSpClient().NotifyPreMigrateBucketAndDeductQuota(reqCtx.Context(), bucketID); err != nil || quota == nil {
log.CtxErrorw(reqCtx.Context(), "failed to pre migrate bucket, the bucket may already notified", "bucket_id",
bucketID, "error", err)
// if the bucket has already pre notified ignore the error
if strings.Contains(err.Error(), "the bucket has already notified") {
err = nil
}
return
}
if bz, err = quota.Marshal(); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to marshal", "bucket_id",
bucketID, "error", err)
return
}
w.Header().Set(GnfdQuotaInfoHeader, hex.EncodeToString(bz))
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
log.CtxInfow(reqCtx.Context(), "succeed to pre bucket migrate and deduct quota", "bucket_id",
bucketID, "quota", quota, "bucket_quota_size", bucketSize)
}
// postMigrateBucketHandler notifying the source sp about the completion of migration bucket
func (g *GateModular) postMigrateBucketHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
reqCtx *RequestContext
bucketID uint64
bucketMigrationInfo *gfsptask.GfSpBucketMigrationInfo
allowMigrate bool
latestQuota *gfsptask.GfSpBucketQuotaInfo
)
defer func() {
reqCtx.Cancel()
if err != nil {
reqCtx.SetError(err)
reqCtx.SetHTTPCode(int(gfsperrors.MakeGfSpError(err).GetHttpStatusCode()))
modelgateway.MakeErrorResponse(w, gfsperrors.MakeGfSpError(err))
} else {
reqCtx.SetHTTPCode(http.StatusOK)
}
log.CtxDebugw(reqCtx.Context(), reqCtx.String())
}()
reqCtx, _ = NewRequestContext(r, g)
if allowMigrate, bucketMigrationInfo, err = g.checkMigrateBucketQuotaAuth(reqCtx, r.Header.Get(GnfdMigrateBucketMsgHeader)); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to check migrate bucket auth", "bucket_migration_info", bucketMigrationInfo, "error", err)
return
}
if !allowMigrate {
log.CtxErrorw(reqCtx.Context(), "no permission to post migrate bucket", "bucket_migration_info", bucketMigrationInfo)
err = ErrNoPermission
return
}
bucketID = bucketMigrationInfo.GetBucketId()
if latestQuota, err = g.baseApp.GfSpClient().NotifyPostMigrateBucketAndRecoupQuota(reqCtx.Context(), bucketMigrationInfo); err != nil {
log.CtxErrorw(reqCtx.Context(), "post migrate bucket error, the bucket may already notified", "bucket_id",
bucketID, "error", err)
return
}
bz, err := latestQuota.Marshal()
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to marshal", "bucket_id",
bucketID, "error", err)
return
}
w.Header().Set(GnfdQuotaInfoHeader, hex.EncodeToString(bz))
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
log.CtxInfow(reqCtx.Context(), "succeed to post bucket migrate", "bucket_id",
bucketID, "latest_quota", latestQuota, "bucket_migration_info", bucketMigrationInfo)
}
// sufficientQuotaForBucketMigrationHandler check if the source SP node has sufficient quota for bucket migration at approval phase
func (g *GateModular) sufficientQuotaForBucketMigrationHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
reqCtx *RequestContext
bucketID uint64
bucketSize uint64
bucketMigrationInfo *gfsptask.GfSpBucketMigrationInfo
allowMigrate bool
)
defer func() {
reqCtx.Cancel()
if err != nil {
reqCtx.SetError(err)
reqCtx.SetHTTPCode(int(gfsperrors.MakeGfSpError(err).GetHttpStatusCode()))
modelgateway.MakeErrorResponse(w, gfsperrors.MakeGfSpError(err))
} else {
reqCtx.SetHTTPCode(http.StatusOK)
}
log.CtxDebugw(reqCtx.Context(), reqCtx.String())
}()
reqCtx, _ = NewRequestContext(r, g)
if allowMigrate, bucketMigrationInfo, _, err = g.verifySignatureAndSP(reqCtx, r.Header.Get(GnfdMigrateBucketMsgHeader)); err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to check migrate bucket auth", "bucket_migration_info", bucketMigrationInfo, "error", err)
return
}
if !allowMigrate {
log.CtxErrorw(reqCtx.Context(), "no permission to check source sp has enough quota", "bucket_migration_info", bucketMigrationInfo)
err = ErrNoPermission
return
}
bucketID = bucketMigrationInfo.GetBucketId()
quota, err := g.baseApp.GfSpClient().GetLatestBucketReadQuota(reqCtx.Context(), bucketID)
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to get bucket read quota", "bucket_id",
bucketID, "error", err)
return
}
// get bucket quota and check
bucketSize, err = g.getBucketTotalSize(reqCtx.Context(), bucketID)
if err != nil {
return
}
// empty bucket will approval
if quota.FreeQuotaSize+quota.ChargedQuotaSize-quota.ReadConsumedSize+quota.MonthlyFreeQuotaSize > bucketSize || bucketSize == 0 {
quota.AllowMigrate = true
} else {
quota.AllowMigrate = false
}
bz, err := quota.Marshal()
if err != nil {
log.CtxErrorw(reqCtx.Context(), "failed to marshal", "bucket_id",
bucketID, "error", err)
return
}
w.Header().Set(GnfdQuotaInfoHeader, hex.EncodeToString(bz))
w.Header().Set(ContentTypeHeader, ContentTypeXMLHeaderValue)
log.CtxInfow(reqCtx.Context(), "succeed to check bucket migrate quota", "bucket_id", bucketID,
"quota", quota, "bucket_size", bucketSize)
}