2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-05-19 16:53:54 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-20 13:18:15 -04:00
|
|
|
"errors"
|
2020-05-19 16:53:54 -04:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2022-04-24 05:36:31 -04:00
|
|
|
"time"
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2023-06-19 20:53:08 -04:00
|
|
|
"github.com/minio/madmin-go/v3"
|
2023-02-13 11:09:52 -05:00
|
|
|
"github.com/minio/minio-go/v7/pkg/set"
|
2020-07-14 12:38:05 -04:00
|
|
|
"github.com/minio/minio-go/v7/pkg/tags"
|
2021-06-01 17:59:40 -04:00
|
|
|
bucketsse "github.com/minio/minio/internal/bucket/encryption"
|
|
|
|
"github.com/minio/minio/internal/bucket/lifecycle"
|
|
|
|
objectlock "github.com/minio/minio/internal/bucket/object/lock"
|
|
|
|
"github.com/minio/minio/internal/bucket/replication"
|
|
|
|
"github.com/minio/minio/internal/bucket/versioning"
|
|
|
|
"github.com/minio/minio/internal/event"
|
|
|
|
"github.com/minio/minio/internal/kms"
|
|
|
|
"github.com/minio/minio/internal/logger"
|
2023-09-04 15:57:37 -04:00
|
|
|
"github.com/minio/pkg/v2/policy"
|
|
|
|
"github.com/minio/pkg/v2/sync/errgroup"
|
2020-05-19 16:53:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// BucketMetadataSys captures all bucket metadata for a given cluster.
|
|
|
|
type BucketMetadataSys struct {
|
2023-02-09 13:29:20 -05:00
|
|
|
objAPI ObjectLayer
|
|
|
|
|
2020-05-19 16:53:54 -04:00
|
|
|
sync.RWMutex
|
|
|
|
metadataMap map[string]BucketMetadata
|
|
|
|
}
|
|
|
|
|
2022-09-09 06:06:34 -04:00
|
|
|
// Count returns number of bucket metadata map entries.
|
|
|
|
func (sys *BucketMetadataSys) Count() int {
|
|
|
|
sys.RLock()
|
|
|
|
defer sys.RUnlock()
|
|
|
|
|
|
|
|
return len(sys.metadataMap)
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:53:54 -04:00
|
|
|
// Remove bucket metadata from memory.
|
2023-02-13 11:09:52 -05:00
|
|
|
func (sys *BucketMetadataSys) Remove(buckets ...string) {
|
2020-05-19 16:53:54 -04:00
|
|
|
sys.Lock()
|
2023-02-13 11:09:52 -05:00
|
|
|
for _, bucket := range buckets {
|
|
|
|
delete(sys.metadataMap, bucket)
|
|
|
|
globalBucketMonitor.DeleteBucket(bucket)
|
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
sys.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-02-13 11:09:52 -05:00
|
|
|
// RemoveStaleBuckets removes all stale buckets in memory that are not on disk.
|
|
|
|
func (sys *BucketMetadataSys) RemoveStaleBuckets(diskBuckets set.StringSet) {
|
|
|
|
sys.Lock()
|
|
|
|
defer sys.Unlock()
|
|
|
|
|
|
|
|
for bucket := range sys.metadataMap {
|
|
|
|
if diskBuckets.Contains(bucket) {
|
|
|
|
continue
|
|
|
|
} // doesn't exist on disk remove from memory.
|
|
|
|
delete(sys.metadataMap, bucket)
|
|
|
|
globalBucketMonitor.DeleteBucket(bucket)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:53:54 -04:00
|
|
|
// Set - sets a new metadata in-memory.
|
|
|
|
// Only a shallow copy is saved and fields with references
|
|
|
|
// cannot be modified without causing a race condition,
|
|
|
|
// so they should be replaced atomically and not appended to, etc.
|
|
|
|
// Data is not persisted to disk.
|
|
|
|
func (sys *BucketMetadataSys) Set(bucket string, meta BucketMetadata) {
|
2023-01-03 11:16:39 -05:00
|
|
|
if !isMinioMetaBucketName(bucket) {
|
2020-05-19 16:53:54 -04:00
|
|
|
sys.Lock()
|
|
|
|
sys.metadataMap[bucket] = meta
|
|
|
|
sys.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 20:55:09 -04:00
|
|
|
func (sys *BucketMetadataSys) updateAndParse(ctx context.Context, bucket string, configFile string, configData []byte, parse bool) (updatedAt time.Time, err error) {
|
2020-10-09 12:59:52 -04:00
|
|
|
objAPI := newObjectLayerFn()
|
2020-05-19 16:53:54 -04:00
|
|
|
if objAPI == nil {
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, errServerNotInitialized
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
2023-01-03 11:16:39 -05:00
|
|
|
if isMinioMetaBucketName(bucket) {
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, errInvalidArgument
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
2022-10-19 20:55:09 -04:00
|
|
|
meta, err := loadBucketMetadataParse(ctx, objAPI, bucket, parse)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
2021-09-20 20:41:01 -04:00
|
|
|
if !globalIsErasure && !globalIsDistErasure && errors.Is(err, errVolumeNotFound) {
|
|
|
|
// Only single drive mode needs this fallback.
|
|
|
|
meta = newBucketMetadata(bucket)
|
|
|
|
} else {
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, err
|
2021-09-20 20:41:01 -04:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-06-28 21:09:20 -04:00
|
|
|
updatedAt = UTCNow()
|
2020-05-19 16:53:54 -04:00
|
|
|
switch configFile {
|
|
|
|
case bucketPolicyConfig:
|
|
|
|
meta.PolicyConfigJSON = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.PolicyConfigUpdatedAt = updatedAt
|
2020-05-19 16:53:54 -04:00
|
|
|
case bucketNotificationConfig:
|
2020-05-21 14:03:59 -04:00
|
|
|
meta.NotificationConfigXML = configData
|
2020-05-19 16:53:54 -04:00
|
|
|
case bucketLifecycleConfig:
|
|
|
|
meta.LifecycleConfigXML = configData
|
2023-05-25 01:52:39 -04:00
|
|
|
meta.LifecycleConfigUpdatedAt = updatedAt
|
2020-05-19 16:53:54 -04:00
|
|
|
case bucketSSEConfig:
|
|
|
|
meta.EncryptionConfigXML = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.EncryptionConfigUpdatedAt = updatedAt
|
2020-06-16 01:09:39 -04:00
|
|
|
case bucketTaggingConfig:
|
2020-05-19 16:53:54 -04:00
|
|
|
meta.TaggingConfigXML = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.TaggingConfigUpdatedAt = updatedAt
|
2020-08-20 16:18:06 -04:00
|
|
|
case bucketQuotaConfigFile:
|
|
|
|
meta.QuotaConfigJSON = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.QuotaConfigUpdatedAt = updatedAt
|
2020-05-19 16:53:54 -04:00
|
|
|
case objectLockConfig:
|
2020-05-21 14:03:59 -04:00
|
|
|
meta.ObjectLockConfigXML = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.ObjectLockConfigUpdatedAt = updatedAt
|
2020-06-12 23:04:01 -04:00
|
|
|
case bucketVersioningConfig:
|
|
|
|
meta.VersioningConfigXML = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.VersioningConfigUpdatedAt = updatedAt
|
2020-07-21 20:49:56 -04:00
|
|
|
case bucketReplicationConfig:
|
|
|
|
meta.ReplicationConfigXML = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.ReplicationConfigUpdatedAt = updatedAt
|
2020-07-28 14:50:47 -04:00
|
|
|
case bucketTargetsFile:
|
2022-07-18 21:54:27 -04:00
|
|
|
meta.BucketTargetsConfigJSON, meta.BucketTargetsConfigMetaJSON, err = encryptBucketMetadata(ctx, meta.Name, configData, kms.Context{
|
2021-04-03 12:03:42 -04:00
|
|
|
bucket: meta.Name,
|
|
|
|
bucketTargetsFile: bucketTargetsFile,
|
|
|
|
})
|
2020-12-21 19:21:33 -05:00
|
|
|
if err != nil {
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, fmt.Errorf("Error encrypting bucket target metadata %w", err)
|
2020-12-21 19:21:33 -05:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
default:
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, fmt.Errorf("Unknown bucket %s metadata update requested %s", bucket, configFile)
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
2023-08-25 10:59:16 -04:00
|
|
|
err = sys.save(ctx, meta)
|
|
|
|
return updatedAt, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sys *BucketMetadataSys) save(ctx context.Context, meta BucketMetadata) error {
|
|
|
|
objAPI := newObjectLayerFn()
|
|
|
|
if objAPI == nil {
|
|
|
|
return errServerNotInitialized
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
2023-08-25 10:59:16 -04:00
|
|
|
if isMinioMetaBucketName(meta.Name) {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := meta.Save(ctx, objAPI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2023-08-25 10:59:16 -04:00
|
|
|
sys.Set(meta.Name, meta)
|
|
|
|
globalNotificationSys.LoadBucketMetadata(bgContext(ctx), meta.Name) // Do not use caller context here
|
|
|
|
return nil
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
2022-10-19 20:55:09 -04:00
|
|
|
// Delete delete the bucket metadata for the specified bucket.
|
|
|
|
// must be used by all callers instead of using Update() with nil configData.
|
|
|
|
func (sys *BucketMetadataSys) Delete(ctx context.Context, bucket string, configFile string) (updatedAt time.Time, err error) {
|
|
|
|
return sys.updateAndParse(ctx, bucket, configFile, nil, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update update bucket metadata for the specified bucket.
|
|
|
|
// The configData data should not be modified after being sent here.
|
|
|
|
func (sys *BucketMetadataSys) Update(ctx context.Context, bucket string, configFile string, configData []byte) (updatedAt time.Time, err error) {
|
|
|
|
return sys.updateAndParse(ctx, bucket, configFile, configData, true)
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:53:54 -04:00
|
|
|
// Get metadata for a bucket.
|
|
|
|
// If no metadata exists errConfigNotFound is returned and a new metadata is returned.
|
|
|
|
// Only a shallow copy is returned, so referenced data should not be modified,
|
|
|
|
// but can be replaced atomically.
|
2020-08-20 13:38:53 -04:00
|
|
|
//
|
|
|
|
// This function should only be used with
|
|
|
|
// - GetBucketInfo
|
|
|
|
// - ListBuckets
|
|
|
|
// For all other bucket specific metadata, use the relevant
|
|
|
|
// calls implemented specifically for each of those features.
|
2020-05-19 16:53:54 -04:00
|
|
|
func (sys *BucketMetadataSys) Get(bucket string) (BucketMetadata, error) {
|
2023-01-03 11:16:39 -05:00
|
|
|
if isMinioMetaBucketName(bucket) {
|
2020-05-19 16:53:54 -04:00
|
|
|
return newBucketMetadata(bucket), errConfigNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.RLock()
|
|
|
|
defer sys.RUnlock()
|
|
|
|
|
|
|
|
meta, ok := sys.metadataMap[bucket]
|
|
|
|
if !ok {
|
|
|
|
return newBucketMetadata(bucket), errConfigNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return meta, nil
|
|
|
|
}
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
// GetVersioningConfig returns configured versioning config
|
|
|
|
// The returned object may not be modified.
|
2022-05-31 05:57:57 -04:00
|
|
|
func (sys *BucketMetadataSys) GetVersioningConfig(bucket string) (*versioning.Versioning, time.Time, error) {
|
2023-02-22 00:18:49 -05:00
|
|
|
meta, _, err := sys.GetConfig(GlobalContext, bucket)
|
2020-06-12 23:04:01 -04:00
|
|
|
if err != nil {
|
2022-05-31 05:57:57 -04:00
|
|
|
if errors.Is(err, errConfigNotFound) {
|
|
|
|
return &versioning.Versioning{XMLNS: "http://s3.amazonaws.com/doc/2006-03-01/"}, meta.Created, nil
|
|
|
|
}
|
|
|
|
return &versioning.Versioning{XMLNS: "http://s3.amazonaws.com/doc/2006-03-01/"}, time.Time{}, err
|
2020-06-12 23:04:01 -04:00
|
|
|
}
|
2022-05-31 05:57:57 -04:00
|
|
|
return meta.versioningConfig, meta.VersioningConfigUpdatedAt, nil
|
2020-06-12 23:04:01 -04:00
|
|
|
}
|
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
// GetTaggingConfig returns configured tagging config
|
|
|
|
// The returned object may not be modified.
|
2022-04-24 05:36:31 -04:00
|
|
|
func (sys *BucketMetadataSys) GetTaggingConfig(bucket string) (*tags.Tags, time.Time, error) {
|
2023-02-22 00:18:49 -05:00
|
|
|
meta, _, err := sys.GetConfig(GlobalContext, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketTaggingNotFound{Bucket: bucket}
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, err
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2020-05-20 13:18:15 -04:00
|
|
|
if meta.taggingConfig == nil {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketTaggingNotFound{Bucket: bucket}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return meta.taggingConfig, meta.TaggingConfigUpdatedAt, nil
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
// GetObjectLockConfig returns configured object lock config
|
|
|
|
// The returned object may not be modified.
|
2022-04-24 05:36:31 -04:00
|
|
|
func (sys *BucketMetadataSys) GetObjectLockConfig(bucket string) (*objectlock.Config, time.Time, error) {
|
2023-02-22 00:18:49 -05:00
|
|
|
meta, _, err := sys.GetConfig(GlobalContext, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketObjectLockConfigNotFound{Bucket: bucket}
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, err
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
|
|
|
if meta.objectLockConfig == nil {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketObjectLockConfigNotFound{Bucket: bucket}
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return meta.objectLockConfig, meta.ObjectLockConfigUpdatedAt, nil
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
// GetLifecycleConfig returns configured lifecycle config
|
|
|
|
// The returned object may not be modified.
|
2023-05-25 01:52:39 -04:00
|
|
|
func (sys *BucketMetadataSys) GetLifecycleConfig(bucket string) (*lifecycle.Lifecycle, time.Time, error) {
|
2023-02-22 00:18:49 -05:00
|
|
|
meta, _, err := sys.GetConfig(GlobalContext, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) {
|
2023-05-25 01:52:39 -04:00
|
|
|
return nil, time.Time{}, BucketLifecycleNotFound{Bucket: bucket}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2023-05-25 01:52:39 -04:00
|
|
|
return nil, time.Time{}, err
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
|
|
|
if meta.lifecycleConfig == nil {
|
2023-05-25 01:52:39 -04:00
|
|
|
return nil, time.Time{}, BucketLifecycleNotFound{Bucket: bucket}
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2023-05-25 01:52:39 -04:00
|
|
|
return meta.lifecycleConfig, meta.LifecycleConfigUpdatedAt, nil
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetNotificationConfig returns configured notification config
|
|
|
|
// The returned object may not be modified.
|
|
|
|
func (sys *BucketMetadataSys) GetNotificationConfig(bucket string) (*event.Config, error) {
|
2023-02-22 00:18:49 -05:00
|
|
|
meta, _, err := sys.GetConfig(GlobalContext, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return meta.notificationConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSSEConfig returns configured SSE config
|
|
|
|
// The returned object may not be modified.
|
2022-04-24 05:36:31 -04:00
|
|
|
func (sys *BucketMetadataSys) GetSSEConfig(bucket string) (*bucketsse.BucketSSEConfig, time.Time, error) {
|
2023-02-22 00:18:49 -05:00
|
|
|
meta, _, err := sys.GetConfig(GlobalContext, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketSSEConfigNotFound{Bucket: bucket}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, err
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
|
|
|
if meta.sseConfig == nil {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketSSEConfigNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
return meta.sseConfig, meta.EncryptionConfigUpdatedAt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreatedAt returns the time of creation of bucket
|
|
|
|
func (sys *BucketMetadataSys) CreatedAt(bucket string) (time.Time, error) {
|
2023-02-22 00:18:49 -05:00
|
|
|
meta, _, err := sys.GetConfig(GlobalContext, bucket)
|
2022-04-24 05:36:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return meta.Created.UTC(), nil
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPolicyConfig returns configured bucket policy
|
|
|
|
// The returned object may not be modified.
|
2023-09-04 15:57:37 -04:00
|
|
|
func (sys *BucketMetadataSys) GetPolicyConfig(bucket string) (*policy.BucketPolicy, time.Time, error) {
|
2023-02-22 00:18:49 -05:00
|
|
|
meta, _, err := sys.GetConfig(GlobalContext, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) {
|
2022-05-31 05:57:57 -04:00
|
|
|
return nil, time.Time{}, BucketPolicyNotFound{Bucket: bucket}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-05-31 05:57:57 -04:00
|
|
|
return nil, time.Time{}, err
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
|
|
|
if meta.policyConfig == nil {
|
2022-05-31 05:57:57 -04:00
|
|
|
return nil, time.Time{}, BucketPolicyNotFound{Bucket: bucket}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-05-31 05:57:57 -04:00
|
|
|
return meta.policyConfig, meta.PolicyConfigUpdatedAt, nil
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
// GetQuotaConfig returns configured bucket quota
|
|
|
|
// The returned object may not be modified.
|
2022-04-24 05:36:31 -04:00
|
|
|
func (sys *BucketMetadataSys) GetQuotaConfig(ctx context.Context, bucket string) (*madmin.BucketQuota, time.Time, error) {
|
2023-02-22 00:18:49 -05:00
|
|
|
meta, _, err := sys.GetConfig(ctx, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
2022-06-18 09:55:39 -04:00
|
|
|
if errors.Is(err, errConfigNotFound) {
|
|
|
|
return nil, time.Time{}, BucketQuotaConfigNotFound{Bucket: bucket}
|
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, err
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return meta.quotaConfig, meta.QuotaConfigUpdatedAt, nil
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2020-07-21 20:49:56 -04:00
|
|
|
// GetReplicationConfig returns configured bucket replication config
|
|
|
|
// The returned object may not be modified.
|
2022-04-24 05:36:31 -04:00
|
|
|
func (sys *BucketMetadataSys) GetReplicationConfig(ctx context.Context, bucket string) (*replication.Config, time.Time, error) {
|
2023-02-22 00:18:49 -05:00
|
|
|
meta, reloaded, err := sys.GetConfig(ctx, bucket)
|
2020-07-21 20:49:56 -04:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketReplicationConfigNotFound{Bucket: bucket}
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, err
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if meta.replicationConfig == nil {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketReplicationConfigNotFound{Bucket: bucket}
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2023-02-22 00:18:49 -05:00
|
|
|
if reloaded {
|
|
|
|
globalBucketTargetSys.set(BucketInfo{
|
|
|
|
Name: bucket,
|
|
|
|
}, meta)
|
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return meta.replicationConfig, meta.ReplicationConfigUpdatedAt, nil
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
2020-07-30 22:55:22 -04:00
|
|
|
// GetBucketTargetsConfig returns configured bucket targets for this bucket
|
2020-07-21 20:49:56 -04:00
|
|
|
// The returned object may not be modified.
|
2020-07-30 22:55:22 -04:00
|
|
|
func (sys *BucketMetadataSys) GetBucketTargetsConfig(bucket string) (*madmin.BucketTargets, error) {
|
2023-02-22 00:18:49 -05:00
|
|
|
meta, reloaded, err := sys.GetConfig(GlobalContext, bucket)
|
2020-07-21 20:49:56 -04:00
|
|
|
if err != nil {
|
2022-05-31 05:57:57 -04:00
|
|
|
if errors.Is(err, errConfigNotFound) {
|
|
|
|
return nil, BucketRemoteTargetNotFound{Bucket: bucket}
|
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-28 14:50:47 -04:00
|
|
|
if meta.bucketTargetConfig == nil {
|
2020-07-30 22:55:22 -04:00
|
|
|
return nil, BucketRemoteTargetNotFound{Bucket: bucket}
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2023-02-22 00:18:49 -05:00
|
|
|
if reloaded {
|
|
|
|
globalBucketTargetSys.set(BucketInfo{
|
|
|
|
Name: bucket,
|
|
|
|
}, meta)
|
|
|
|
}
|
2020-07-28 14:50:47 -04:00
|
|
|
return meta.bucketTargetConfig, nil
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
2023-02-08 00:44:42 -05:00
|
|
|
// GetConfigFromDisk read bucket metadata config from disk.
|
|
|
|
func (sys *BucketMetadataSys) GetConfigFromDisk(ctx context.Context, bucket string) (BucketMetadata, error) {
|
|
|
|
objAPI := newObjectLayerFn()
|
|
|
|
if objAPI == nil {
|
|
|
|
return newBucketMetadata(bucket), errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
if isMinioMetaBucketName(bucket) {
|
|
|
|
return newBucketMetadata(bucket), errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
return loadBucketMetadata(ctx, objAPI, bucket)
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:49:56 -04:00
|
|
|
// GetConfig returns a specific configuration from the bucket metadata.
|
2020-05-20 13:18:15 -04:00
|
|
|
// The returned object may not be modified.
|
2023-02-22 00:18:49 -05:00
|
|
|
// reloaded will be true if metadata refreshed from disk
|
|
|
|
func (sys *BucketMetadataSys) GetConfig(ctx context.Context, bucket string) (meta BucketMetadata, reloaded bool, err error) {
|
2020-10-09 12:59:52 -04:00
|
|
|
objAPI := newObjectLayerFn()
|
2020-05-20 13:18:15 -04:00
|
|
|
if objAPI == nil {
|
2023-02-22 00:18:49 -05:00
|
|
|
return newBucketMetadata(bucket), reloaded, errServerNotInitialized
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
|
|
|
|
2023-01-03 11:16:39 -05:00
|
|
|
if isMinioMetaBucketName(bucket) {
|
2023-02-22 00:18:49 -05:00
|
|
|
return newBucketMetadata(bucket), reloaded, errInvalidArgument
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:11:13 -04:00
|
|
|
sys.RLock()
|
2020-05-20 13:18:15 -04:00
|
|
|
meta, ok := sys.metadataMap[bucket]
|
2020-05-20 17:11:13 -04:00
|
|
|
sys.RUnlock()
|
2020-05-20 13:18:15 -04:00
|
|
|
if ok {
|
2023-02-22 00:18:49 -05:00
|
|
|
return meta, reloaded, nil
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2023-02-22 00:18:49 -05:00
|
|
|
meta, err = loadBucketMetadata(ctx, objAPI, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
2023-02-22 00:18:49 -05:00
|
|
|
return meta, reloaded, err
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2020-05-20 17:11:13 -04:00
|
|
|
sys.Lock()
|
2020-05-20 13:18:15 -04:00
|
|
|
sys.metadataMap[bucket] = meta
|
2020-05-20 17:11:13 -04:00
|
|
|
sys.Unlock()
|
2021-06-08 23:11:27 -04:00
|
|
|
|
2023-02-22 00:18:49 -05:00
|
|
|
return meta, true, nil
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init - initializes bucket metadata system for all buckets.
|
|
|
|
func (sys *BucketMetadataSys) Init(ctx context.Context, buckets []BucketInfo, objAPI ObjectLayer) error {
|
|
|
|
if objAPI == nil {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2023-02-09 13:29:20 -05:00
|
|
|
sys.objAPI = objAPI
|
|
|
|
|
2023-05-19 11:44:48 -04:00
|
|
|
// Load bucket metadata sys.
|
|
|
|
sys.init(ctx, buckets)
|
2023-02-09 13:29:20 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-02 01:32:53 -04:00
|
|
|
// concurrently load bucket metadata to speed up loading bucket metadata.
|
2023-11-15 15:43:25 -05:00
|
|
|
func (sys *BucketMetadataSys) concurrentLoad(ctx context.Context, buckets []BucketInfo, failedBuckets map[string]struct{}) {
|
2020-06-02 01:32:53 -04:00
|
|
|
g := errgroup.WithNErrs(len(buckets))
|
2023-02-13 11:09:52 -05:00
|
|
|
bucketMetas := make([]BucketMetadata, len(buckets))
|
2020-06-02 01:32:53 -04:00
|
|
|
for index := range buckets {
|
|
|
|
index := index
|
|
|
|
g.Go(func() error {
|
2023-02-09 13:29:20 -05:00
|
|
|
_, _ = sys.objAPI.HealBucket(ctx, buckets[index].Name, madmin.HealOpts{
|
2020-12-14 15:07:07 -05:00
|
|
|
// Ensure heal opts for bucket metadata be deep healed all the time.
|
|
|
|
ScanMode: madmin.HealDeepScan,
|
2022-02-07 13:39:57 -05:00
|
|
|
Recreate: true,
|
2020-12-14 15:07:07 -05:00
|
|
|
})
|
2023-02-13 11:09:52 -05:00
|
|
|
meta, err := loadBucketMetadata(ctx, sys.objAPI, buckets[index].Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bucketMetas[index] = meta
|
|
|
|
return nil
|
2020-06-02 01:32:53 -04:00
|
|
|
}, index)
|
|
|
|
}
|
2023-02-13 11:09:52 -05:00
|
|
|
|
|
|
|
errs := g.Wait()
|
|
|
|
for _, err := range errs {
|
2020-05-19 16:53:54 -04:00
|
|
|
if err != nil {
|
2020-10-22 16:36:24 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
}
|
2023-02-13 11:09:52 -05:00
|
|
|
|
|
|
|
// Hold lock here to update in-memory map at once,
|
|
|
|
// instead of serializing the Go routines.
|
|
|
|
sys.Lock()
|
|
|
|
for i, meta := range bucketMetas {
|
|
|
|
if errs[i] != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sys.metadataMap[buckets[i].Name] = meta
|
|
|
|
}
|
|
|
|
sys.Unlock()
|
|
|
|
|
|
|
|
for i, meta := range bucketMetas {
|
|
|
|
if errs[i] != nil {
|
2023-11-15 15:43:25 -05:00
|
|
|
if failedBuckets == nil {
|
|
|
|
failedBuckets = make(map[string]struct{})
|
|
|
|
}
|
|
|
|
failedBuckets[buckets[i].Name] = struct{}{}
|
2023-02-13 11:09:52 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
globalEventNotifier.set(buckets[i], meta) // set notification targets
|
|
|
|
globalBucketTargetSys.set(buckets[i], meta) // set remote replication targets
|
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
2023-11-15 15:43:25 -05:00
|
|
|
func (sys *BucketMetadataSys) refreshBucketsMetadataLoop(ctx context.Context, failedBuckets map[string]struct{}) {
|
2023-02-09 13:29:20 -05:00
|
|
|
const bucketMetadataRefresh = 15 * time.Minute
|
|
|
|
|
2023-07-14 07:00:29 -04:00
|
|
|
sleeper := newDynamicSleeper(2, 150*time.Millisecond, false)
|
|
|
|
|
2023-02-09 13:29:20 -05:00
|
|
|
t := time.NewTimer(bucketMetadataRefresh)
|
|
|
|
defer t.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
buckets, err := sys.objAPI.ListBuckets(ctx, BucketOptions{})
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
2023-11-15 15:43:25 -05:00
|
|
|
break
|
2023-02-09 13:29:20 -05:00
|
|
|
}
|
2023-02-13 11:09:52 -05:00
|
|
|
|
|
|
|
// Handle if we have some buckets in-memory those are stale.
|
|
|
|
// first delete them and then replace the newer state()
|
|
|
|
// from disk.
|
|
|
|
diskBuckets := set.CreateStringSet()
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
diskBuckets.Add(bucket.Name)
|
|
|
|
}
|
|
|
|
sys.RemoveStaleBuckets(diskBuckets)
|
|
|
|
|
2023-11-15 15:43:25 -05:00
|
|
|
for i := range buckets {
|
2023-07-14 07:00:29 -04:00
|
|
|
wait := sleeper.Timer(ctx)
|
|
|
|
|
2023-11-15 15:43:25 -05:00
|
|
|
meta, err := loadBucketMetadata(ctx, sys.objAPI, buckets[i].Name)
|
2023-02-09 13:29:20 -05:00
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
2023-07-14 07:00:29 -04:00
|
|
|
wait() // wait to proceed to next entry.
|
2023-02-09 13:29:20 -05:00
|
|
|
continue
|
|
|
|
}
|
2023-07-14 07:00:29 -04:00
|
|
|
|
2023-11-15 15:43:25 -05:00
|
|
|
sys.Lock()
|
|
|
|
sys.metadataMap[buckets[i].Name] = meta
|
|
|
|
sys.Unlock()
|
|
|
|
|
|
|
|
// Initialize the failed buckets
|
|
|
|
if _, ok := failedBuckets[buckets[i].Name]; ok {
|
|
|
|
globalEventNotifier.set(buckets[i], meta)
|
|
|
|
globalBucketTargetSys.set(buckets[i], meta)
|
|
|
|
delete(failedBuckets, buckets[i].Name)
|
|
|
|
}
|
|
|
|
|
2023-07-14 07:00:29 -04:00
|
|
|
wait() // wait to proceed to next entry.
|
2023-02-09 13:29:20 -05:00
|
|
|
}
|
|
|
|
}
|
2023-11-15 15:43:25 -05:00
|
|
|
t.Reset(bucketMetadataRefresh)
|
2023-02-09 13:29:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 01:32:53 -04:00
|
|
|
// Loads bucket metadata for all buckets into BucketMetadataSys.
|
2023-02-09 13:29:20 -05:00
|
|
|
func (sys *BucketMetadataSys) init(ctx context.Context, buckets []BucketInfo) {
|
2020-06-02 01:32:53 -04:00
|
|
|
count := 100 // load 100 bucket metadata at a time.
|
2023-11-15 15:43:25 -05:00
|
|
|
failedBuckets := make(map[string]struct{})
|
2020-06-02 01:32:53 -04:00
|
|
|
for {
|
|
|
|
if len(buckets) < count {
|
2023-11-15 15:43:25 -05:00
|
|
|
sys.concurrentLoad(ctx, buckets, failedBuckets)
|
2023-02-09 13:29:20 -05:00
|
|
|
break
|
2020-06-02 01:32:53 -04:00
|
|
|
}
|
2023-11-15 15:43:25 -05:00
|
|
|
sys.concurrentLoad(ctx, buckets[:count], failedBuckets)
|
2020-06-02 01:32:53 -04:00
|
|
|
buckets = buckets[count:]
|
|
|
|
}
|
2023-02-09 13:29:20 -05:00
|
|
|
|
|
|
|
if globalIsDistErasure {
|
2023-11-15 15:43:25 -05:00
|
|
|
go sys.refreshBucketsMetadataLoop(ctx, failedBuckets)
|
2023-02-09 13:29:20 -05:00
|
|
|
}
|
2020-06-02 01:32:53 -04:00
|
|
|
}
|
|
|
|
|
2021-01-05 13:45:26 -05:00
|
|
|
// Reset the state of the BucketMetadataSys.
|
|
|
|
func (sys *BucketMetadataSys) Reset() {
|
|
|
|
sys.Lock()
|
|
|
|
for k := range sys.metadataMap {
|
|
|
|
delete(sys.metadataMap, k)
|
|
|
|
}
|
|
|
|
sys.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:53:54 -04:00
|
|
|
// NewBucketMetadataSys - creates new policy system.
|
|
|
|
func NewBucketMetadataSys() *BucketMetadataSys {
|
|
|
|
return &BucketMetadataSys{
|
|
|
|
metadataMap: make(map[string]BucketMetadata),
|
|
|
|
}
|
|
|
|
}
|