Fix bug for repeatedly create bucket in minio (#6477)

* Fix bug for repeatedly create bucket in minio

Signed-off-by: xiaocai2333 <cai.zhang@zilliz.com>

* Simplify code

Signed-off-by: xiaocai2333 <cai.zhang@zilliz.com>

* Add log

Signed-off-by: xiaocai2333 <cai.zhang@zilliz.com>
This commit is contained in:
cai.zhang 2021-07-14 09:55:46 +08:00 committed by GitHub
parent 724f10b9a0
commit f3a5b46288
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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