From f3a5b46288d80467b5d6ca33538e734512764bd7 Mon Sep 17 00:00:00 2001 From: "cai.zhang" Date: Wed, 14 Jul 2021 09:55:46 +0800 Subject: [PATCH] Fix bug for repeatedly create bucket in minio (#6477) * Fix bug for repeatedly create bucket in minio Signed-off-by: xiaocai2333 * Simplify code Signed-off-by: xiaocai2333 * Add log Signed-off-by: xiaocai2333 --- internal/kv/minio/minio_kv.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/internal/kv/minio/minio_kv.go b/internal/kv/minio/minio_kv.go index 7d1b8e5589..1132711e6a 100644 --- a/internal/kv/minio/minio_kv.go +++ b/internal/kv/minio/minio_kv.go @@ -44,6 +44,7 @@ type Option struct { func NewMinIOKV(ctx context.Context, option *Option) (*MinIOKV, error) { var minIOClient *minio.Client var err error + log.Debug("MinioKV NewMinioKV", zap.Any("option", option)) minIOClient, err = minio.New(option.Address, &minio.Options{ Creds: credentials.NewStaticV4(option.AccessKeyID, option.SecretAccessKeyID, ""), Secure: option.UseSSL, @@ -56,31 +57,30 @@ func NewMinIOKV(ctx context.Context, option *Option) (*MinIOKV, error) { // check valid in first query checkBucketFn := func() error { bucketExists, err = minIOClient.BucketExists(ctx, option.BucketName) - return err + if err != nil { + return err + } + if !bucketExists { + log.Debug("MinioKV NewMinioKV", zap.Any("Check bucket", "bucket not exist")) + if option.CreateBucket { + log.Debug("MinioKV NewMinioKV create bucket.") + return minIOClient.MakeBucket(ctx, option.BucketName, minio.MakeBucketOptions{}) + } + return fmt.Errorf("bucket %s not Existed", option.BucketName) + } + return nil } err = retry.Do(ctx, checkBucketFn, retry.Attempts(300)) if err != nil { return nil, err } - // connection shall be valid here, no need to retry - if option.CreateBucket { - if !bucketExists { - err = minIOClient.MakeBucket(ctx, option.BucketName, minio.MakeBucketOptions{}) - if err != nil { - return nil, err - } - } - } else { - if !bucketExists { - return nil, fmt.Errorf("bucket %s not Existed", option.BucketName) - } - } kv := &MinIOKV{ ctx: ctx, minioClient: minIOClient, bucketName: option.BucketName, } + log.Debug("MinioKV new MinioKV success.") //go kv.performanceTest(false, 16<<20) return kv, nil