From 628042e65e8932ec01af213848b871f39ecb42b5 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Mon, 13 Mar 2023 06:04:20 -0700 Subject: [PATCH] tests: Protect globalLocalDrives against races (#16800) --- cmd/background-newdisks-heal-ops.go | 3 +++ cmd/erasure-server-pool.go | 2 ++ cmd/globals.go | 6 ++++-- cmd/peer-s3-server.go | 16 ++++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/cmd/background-newdisks-heal-ops.go b/cmd/background-newdisks-heal-ops.go index 0e51ffb6f..c588c0259 100644 --- a/cmd/background-newdisks-heal-ops.go +++ b/cmd/background-newdisks-heal-ops.go @@ -271,6 +271,9 @@ func initAutoHeal(ctx context.Context, objAPI ObjectLayer) { } func getLocalDisksToHeal() (disksToHeal Endpoints) { + globalLocalDrivesMu.RLock() + globalLocalDrives := globalLocalDrives + globalLocalDrivesMu.RUnlock() for _, disk := range globalLocalDrives { _, err := disk.GetDiskID() if errors.Is(err, errUnformattedDisk) { diff --git a/cmd/erasure-server-pool.go b/cmd/erasure-server-pool.go index 3bd9d97e0..b2f4c385e 100644 --- a/cmd/erasure-server-pool.go +++ b/cmd/erasure-server-pool.go @@ -156,7 +156,9 @@ func newErasureServerPools(ctx context.Context, endpointServerPools EndpointServ break } + globalLocalDrivesMu.Lock() globalLocalDrives = localDrives + defer globalLocalDrivesMu.Unlock() return z, nil } diff --git a/cmd/globals.go b/cmd/globals.go index 942782e3d..9124c4b5d 100644 --- a/cmd/globals.go +++ b/cmd/globals.go @@ -353,8 +353,10 @@ var ( globalServiceFreezeCnt int32 globalServiceFreezeMu sync.Mutex // Updates. - // List of local drives to this node, this is only set during server startup. - globalLocalDrives []StorageAPI + // List of local drives to this node, this is only set during server startup, + // and should never be mutated. Hold globalLocalDrivesMu to access. + globalLocalDrives []StorageAPI + globalLocalDrivesMu sync.RWMutex // Is MINIO_CI_CD set? globalIsCICD bool diff --git a/cmd/peer-s3-server.go b/cmd/peer-s3-server.go index 5137437b1..bc1ebc3d3 100644 --- a/cmd/peer-s3-server.go +++ b/cmd/peer-s3-server.go @@ -80,6 +80,10 @@ func (s *peerS3Server) HealthHandler(w http.ResponseWriter, r *http.Request) { } func listBucketsLocal(ctx context.Context, opts BucketOptions) (buckets []BucketInfo, err error) { + globalLocalDrivesMu.RLock() + globalLocalDrives := globalLocalDrives + globalLocalDrivesMu.RUnlock() + quorum := (len(globalLocalDrives) / 2) buckets = make([]BucketInfo, 0, 32) @@ -128,6 +132,10 @@ func listBucketsLocal(ctx context.Context, opts BucketOptions) (buckets []Bucket } func getBucketInfoLocal(ctx context.Context, bucket string, opts BucketOptions) (BucketInfo, error) { + globalLocalDrivesMu.RLock() + globalLocalDrives := globalLocalDrives + globalLocalDrivesMu.RUnlock() + g := errgroup.WithNErrs(len(globalLocalDrives)).WithConcurrency(32) bucketsInfo := make([]BucketInfo, len(globalLocalDrives)) @@ -173,6 +181,10 @@ func getBucketInfoLocal(ctx context.Context, bucket string, opts BucketOptions) } func deleteBucketLocal(ctx context.Context, bucket string, opts DeleteBucketOptions) error { + globalLocalDrivesMu.RLock() + globalLocalDrives := globalLocalDrives + globalLocalDrivesMu.RUnlock() + g := errgroup.WithNErrs(len(globalLocalDrives)).WithConcurrency(32) // Make a volume entry on all underlying storage disks. @@ -208,6 +220,10 @@ func deleteBucketLocal(ctx context.Context, bucket string, opts DeleteBucketOpti } func makeBucketLocal(ctx context.Context, bucket string, opts MakeBucketOptions) error { + globalLocalDrivesMu.RLock() + globalLocalDrives := globalLocalDrives + globalLocalDrivesMu.RUnlock() + g := errgroup.WithNErrs(len(globalLocalDrives)).WithConcurrency(32) // Make a volume entry on all underlying storage disks.