From f6bad74183fccbd2aa08b8713764bfa336b4d7c3 Mon Sep 17 00:00:00 2001 From: Kensei Nakada Date: Wed, 27 Sep 2023 00:12:24 +0900 Subject: [PATCH] fix: make reservedResourceAmounts fully goroutine-safe --- pkg/controllers/reserved_resource_amounts.go | 3 +++ pkg/controllers/reserved_resource_amounts_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/pkg/controllers/reserved_resource_amounts.go b/pkg/controllers/reserved_resource_amounts.go index 844239f..41677a9 100644 --- a/pkg/controllers/reserved_resource_amounts.go +++ b/pkg/controllers/reserved_resource_amounts.go @@ -115,7 +115,10 @@ func (c *reservedResourceAmounts) reservedResourceAmount(nn types.NamespacedName defer func() { _ = c.keyMutex.UnlockKey(nn.String()) }() + + c.RLock() podResourceAmountMap, ok := c.cache[nn] + c.RUnlock() if !ok { return schedulev1alpha1.ResourceAmount{}, sets.New[string]() } diff --git a/pkg/controllers/reserved_resource_amounts_test.go b/pkg/controllers/reserved_resource_amounts_test.go index b26854c..703a691 100644 --- a/pkg/controllers/reserved_resource_amounts_test.go +++ b/pkg/controllers/reserved_resource_amounts_test.go @@ -64,8 +64,20 @@ var _ = Describe("ReservedResourceAmounts", func() { remove.Done() }(i) } + + reservedResourceAmount := &sync.WaitGroup{} + reservedResourceAmount.Add(n) + for i := 0; i < n; i++ { + go func(j int) { + r.reservedResourceAmount( + types.NamespacedName{Name: "test"}, + ) + reservedResourceAmount.Done() + }(i) + } add.Wait() remove.Wait() + reservedResourceAmount.Wait() }) It("should be threadsafe on specific throttle's namespacedname", func() { r := newReservedResourceAmounts(1024)