You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@yunikorn.apache.org by ma...@apache.org on 2026/09/28 12:13:01 UTC

(yunikorn-k8shim) branch master updated: [YUNIKORN-3422] Publish the scheduler cache node lists atomically (#1099)

This is an automated email from the ASF dual-hosted git repository.

manirajv06 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/yunikorn-k8shim.git


The following commit(s) were added to refs/heads/master by this push:
     new 02e92e7a [YUNIKORN-3422] Publish the scheduler cache node lists atomically (#1099)
02e92e7a is described below

commit 02e92e7ae624f576b6dcb824e29717f062b0784e
Author: rjgoyln <pt...@gmail.com>
AuthorDate: Mon Sep 28 17:42:27 2026 +0530

    [YUNIKORN-3422] Publish the scheduler cache node lists atomically (#1099)
    
    The three node-list getters are reached from the predicate shared lister
    under the cache read lock, and the core runs preemption predicate checks
    on up to ten goroutines at once. Several of them see an invalidated list
    and populate the same field together, so the slice header can be
    published torn.
    
    Building the lists eagerly at invalidation time would move the work under
    the write lock, where it would run on every node status update and on
    every pod event carrying affinity. Keeping the population lazy and
    publishing it atomically leaves the hot path lock-free.
    
    Closes: #1099
    
    Signed-off-by: Manikandan R <ma...@gmail.com>
---
 pkg/cache/external/scheduler_cache.go      |  87 ++++++++++---------
 pkg/cache/external/scheduler_cache_test.go | 135 +++++++++++++++++++++++------
 2 files changed, 158 insertions(+), 64 deletions(-)

diff --git a/pkg/cache/external/scheduler_cache.go b/pkg/cache/external/scheduler_cache.go
index 9b8ab4a4..02cd1812 100644
--- a/pkg/cache/external/scheduler_cache.go
+++ b/pkg/cache/external/scheduler_cache.go
@@ -20,6 +20,7 @@ package external
 
 import (
 	"fmt"
+	"sync/atomic"
 
 	"go.uber.org/zap"
 	"go.uber.org/zap/zapcore"
@@ -53,10 +54,12 @@ type SchedulerCache struct {
 	clients        *client.Clients // client APIs
 	klogger        klog.Logger
 
-	// cached data, re-calculated on demand from nodesMap
-	nodesInfo                        []fwk.NodeInfo
-	nodesInfoPodsWithAffinity        []fwk.NodeInfo
-	nodesInfoPodsWithReqAntiAffinity []fwk.NodeInfo
+	// cached data, re-calculated on demand from nodesMap. The predicate shared lister populates
+	// these while holding the read lock, so they are published atomically: concurrent predicate
+	// checks would otherwise write the same field at the same time.
+	nodesInfo                        atomic.Pointer[[]fwk.NodeInfo]
+	nodesInfoPodsWithAffinity        atomic.Pointer[[]fwk.NodeInfo]
+	nodesInfoPodsWithReqAntiAffinity atomic.Pointer[[]fwk.NodeInfo]
 }
 
 func NewSchedulerCache(clients *client.Clients) *SchedulerCache {
@@ -84,49 +87,55 @@ func (cache *SchedulerCache) GetNodesInfoMap() map[string]*framework.NodeInfo {
 // GetNodesInfo returns a (possibly cached) list of nodes. This is explicitly for the use of the predicate
 // shared lister and requires that the scheduler cache lock be held while accessing.
 func (cache *SchedulerCache) GetNodesInfo() []fwk.NodeInfo {
-	if cache.nodesInfo == nil {
-		nodeList := make([]fwk.NodeInfo, 0, len(cache.nodesMap))
-		for _, node := range cache.nodesMap {
-			nodeList = append(nodeList, node)
-		}
-		cache.nodesInfo = nodeList
+	if cached := cache.nodesInfo.Load(); cached != nil {
+		return *cached
+	}
+
+	nodeList := make([]fwk.NodeInfo, 0, len(cache.nodesMap))
+	for _, node := range cache.nodesMap {
+		nodeList = append(nodeList, node)
 	}
+	cache.nodesInfo.Store(&nodeList)
 
-	return cache.nodesInfo
+	return nodeList
 }
 
 // GetNodesInfoPodsWithAffinity returns a (possibly cached) list of nodes which contain pods with affinity.
 // This is explicitly for the use of the predicate shared lister and requires that the scheduler cache lock
 // be held while accessing.
 func (cache *SchedulerCache) GetNodesInfoPodsWithAffinity() []fwk.NodeInfo {
-	if cache.nodesInfoPodsWithAffinity == nil {
-		nodeList := make([]fwk.NodeInfo, 0, len(cache.nodesMap))
-		for _, node := range cache.nodesMap {
-			if len(node.PodsWithAffinity) > 0 {
-				nodeList = append(nodeList, node)
-			}
+	if cached := cache.nodesInfoPodsWithAffinity.Load(); cached != nil {
+		return *cached
+	}
+
+	nodeList := make([]fwk.NodeInfo, 0, len(cache.nodesMap))
+	for _, node := range cache.nodesMap {
+		if len(node.PodsWithAffinity) > 0 {
+			nodeList = append(nodeList, node)
 		}
-		cache.nodesInfoPodsWithAffinity = nodeList
 	}
+	cache.nodesInfoPodsWithAffinity.Store(&nodeList)
 
-	return cache.nodesInfoPodsWithAffinity
+	return nodeList
 }
 
 // GetNodesInfoPodsWithReqAntiAffinity returns a (possibly cached) list of nodes which contain pods with required anti-affinity.
 // This is explicitly for the use of the predicate shared lister and requires that the scheduler cache lock
 // be held while accessing.
 func (cache *SchedulerCache) GetNodesInfoPodsWithReqAntiAffinity() []fwk.NodeInfo {
-	if cache.nodesInfoPodsWithReqAntiAffinity == nil {
-		nodeList := make([]fwk.NodeInfo, 0, len(cache.nodesMap))
-		for _, node := range cache.nodesMap {
-			if len(node.PodsWithRequiredAntiAffinity) > 0 {
-				nodeList = append(nodeList, node)
-			}
+	if cached := cache.nodesInfoPodsWithReqAntiAffinity.Load(); cached != nil {
+		return *cached
+	}
+
+	nodeList := make([]fwk.NodeInfo, 0, len(cache.nodesMap))
+	for _, node := range cache.nodesMap {
+		if len(node.PodsWithRequiredAntiAffinity) > 0 {
+			nodeList = append(nodeList, node)
 		}
-		cache.nodesInfoPodsWithReqAntiAffinity = nodeList
 	}
+	cache.nodesInfoPodsWithReqAntiAffinity.Store(&nodeList)
 
-	return cache.nodesInfoPodsWithReqAntiAffinity
+	return nodeList
 }
 
 func (cache *SchedulerCache) LockForReads() {
@@ -172,7 +181,7 @@ func (cache *SchedulerCache) updateNode(node *v1.Node) (*v1.Node, []*v1.Pod) {
 		log.Log(log.ShimCacheExternal).Debug("Adding node to cache", zap.String("nodeName", node.Name))
 		nodeInfo = framework.NewNodeInfo()
 		cache.nodesMap[node.Name] = nodeInfo
-		cache.nodesInfo = nil
+		cache.nodesInfo.Store(nil)
 		nodeInfo.SetNode(node)
 
 		// look for orphaned pods to adopt
@@ -189,8 +198,8 @@ func (cache *SchedulerCache) updateNode(node *v1.Node) (*v1.Node, []*v1.Pod) {
 		nodeInfo.SetNode(node)
 	}
 
-	cache.nodesInfoPodsWithAffinity = nil
-	cache.nodesInfoPodsWithReqAntiAffinity = nil
+	cache.nodesInfoPodsWithAffinity.Store(nil)
+	cache.nodesInfoPodsWithReqAntiAffinity.Store(nil)
 	cache.updatePVCRefCounts(nodeInfo, false)
 
 	return prevNode, adopted
@@ -240,9 +249,9 @@ func (cache *SchedulerCache) removeNode(node *v1.Node) (*v1.Node, []*v1.Pod) {
 
 	log.Log(log.ShimCacheExternal).Debug("Removing node from cache", zap.String("nodeName", node.Name))
 	delete(cache.nodesMap, node.Name)
-	cache.nodesInfo = nil
-	cache.nodesInfoPodsWithAffinity = nil
-	cache.nodesInfoPodsWithReqAntiAffinity = nil
+	cache.nodesInfo.Store(nil)
+	cache.nodesInfoPodsWithAffinity.Store(nil)
+	cache.nodesInfoPodsWithReqAntiAffinity.Store(nil)
 	cache.updatePVCRefCounts(nodeInfo, true)
 
 	return result, orphans
@@ -339,10 +348,10 @@ func (cache *SchedulerCache) updatePod(pod *v1.Pod) bool {
 				}
 				cache.updatePVCRefCounts(nodeInfo, false)
 				if podWithAffinity(pod) {
-					cache.nodesInfoPodsWithAffinity = nil
+					cache.nodesInfoPodsWithAffinity.Store(nil)
 				}
 				if podWithRequiredAntiAffinity(pod) {
-					cache.nodesInfoPodsWithReqAntiAffinity = nil
+					cache.nodesInfoPodsWithReqAntiAffinity.Store(nil)
 				}
 			}
 			if pod.Spec.NodeName == "" && cache.isAssumedPod(key) {
@@ -377,10 +386,10 @@ func (cache *SchedulerCache) updatePod(pod *v1.Pod) bool {
 			nodeInfo.AddPod(pod)
 			cache.assignedPods[key] = pod.Spec.NodeName
 			if podWithAffinity(pod) {
-				cache.nodesInfoPodsWithAffinity = nil
+				cache.nodesInfoPodsWithAffinity.Store(nil)
 			}
 			if podWithRequiredAntiAffinity(pod) {
-				cache.nodesInfoPodsWithReqAntiAffinity = nil
+				cache.nodesInfoPodsWithReqAntiAffinity.Store(nil)
 			}
 			cache.updatePVCRefCounts(nodeInfo, false)
 		}
@@ -432,8 +441,8 @@ func (cache *SchedulerCache) removePod(pod *v1.Pod) {
 	delete(cache.assumedPods, key)
 	delete(cache.orphanedPods, key)
 	delete(cache.podsCycleState, key)
-	cache.nodesInfoPodsWithAffinity = nil
-	cache.nodesInfoPodsWithReqAntiAffinity = nil
+	cache.nodesInfoPodsWithAffinity.Store(nil)
+	cache.nodesInfoPodsWithReqAntiAffinity.Store(nil)
 }
 
 func (cache *SchedulerCache) GetPod(uid string) *v1.Pod {
diff --git a/pkg/cache/external/scheduler_cache_test.go b/pkg/cache/external/scheduler_cache_test.go
index 280024bf..48f3a207 100644
--- a/pkg/cache/external/scheduler_cache_test.go
+++ b/pkg/cache/external/scheduler_cache_test.go
@@ -20,6 +20,7 @@ package external
 
 import (
 	"fmt"
+	"sync"
 	"testing"
 
 	"gotest.tools/v3/assert"
@@ -252,12 +253,12 @@ func TestUpdateNode(t *testing.T) {
 	assert.Equal(t, nodeInCache.Node().Spec.Unschedulable, false)
 
 	cache.RemoveNode(newNode)
-	assert.Equal(t, 0, len(cache.nodesInfo), "nodesInfo list size")
+	assert.Assert(t, cache.nodesInfo.Load() == nil, "nodesInfo list was not invalidated")
 }
 
 func TestGetNodesInfo(t *testing.T) {
 	cache := NewSchedulerCache(client.NewMockedAPIProvider(false).GetAPIs())
-	assert.Assert(t, cache.nodesInfo == nil)
+	assert.Assert(t, cache.nodesInfo.Load() == nil)
 	node := &v1.Node{
 		ObjectMeta: apis.ObjectMeta{
 			Name:      host1,
@@ -269,7 +270,7 @@ func TestGetNodesInfo(t *testing.T) {
 		},
 	}
 	cache.UpdateNode(node)
-	assert.Assert(t, cache.nodesInfo == nil)
+	assert.Assert(t, cache.nodesInfo.Load() == nil)
 	nodesInfo := cache.GetNodesInfo()
 	expectHost(t, host1, nodesInfo)
 
@@ -296,13 +297,13 @@ func TestGetNodesInfo(t *testing.T) {
 		},
 	}
 	cache.UpdateNode(newNode)
-	assert.Assert(t, cache.nodesInfo == nil, "nodesInfo list was not invalidated")
+	assert.Assert(t, cache.nodesInfo.Load() == nil, "nodesInfo list was not invalidated")
 	nodesInfo = cache.GetNodesInfo()
 	expectHost1AndHost2(t, nodesInfo)
 
 	// remove
 	cache.RemoveNode(node)
-	assert.Assert(t, cache.nodesInfo == nil, "nodesInfo list was not invalidated")
+	assert.Assert(t, cache.nodesInfo.Load() == nil, "nodesInfo list was not invalidated")
 	nodesInfo = cache.GetNodesInfo()
 	expectHost(t, host2, nodesInfo)
 }
@@ -310,7 +311,7 @@ func TestGetNodesInfo(t *testing.T) {
 //nolint:funlen
 func TestGetNodesInfoPodsWithAffinity(t *testing.T) {
 	cache := NewSchedulerCache(client.NewMockedAPIProvider(false).GetAPIs())
-	assert.Assert(t, cache.nodesInfoPodsWithAffinity == nil)
+	assert.Assert(t, cache.nodesInfoPodsWithAffinity.Load() == nil)
 	node := &v1.Node{
 		ObjectMeta: apis.ObjectMeta{
 			Name:      host1,
@@ -322,7 +323,7 @@ func TestGetNodesInfoPodsWithAffinity(t *testing.T) {
 		},
 	}
 	cache.UpdateNode(node)
-	assert.Assert(t, cache.nodesInfoPodsWithAffinity == nil)
+	assert.Assert(t, cache.nodesInfoPodsWithAffinity.Load() == nil)
 	cache.AssumePod(&v1.Pod{
 		TypeMeta: apis.TypeMeta{
 			Kind:       "Pod",
@@ -354,7 +355,7 @@ func TestGetNodesInfoPodsWithAffinity(t *testing.T) {
 		},
 	}
 	cache.UpdateNode(newNode)
-	assert.Assert(t, cache.nodesInfoPodsWithAffinity == nil, "nodesInfo list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithAffinity.Load() == nil, "nodesInfo list was not invalidated")
 	cache.AssumePod(&v1.Pod{
 		TypeMeta: apis.TypeMeta{
 			Kind:       "Pod",
@@ -371,13 +372,13 @@ func TestGetNodesInfoPodsWithAffinity(t *testing.T) {
 			NodeName: host2,
 		},
 	}, true)
-	assert.Assert(t, cache.nodesInfoPodsWithAffinity == nil, "nodesInfo list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithAffinity.Load() == nil, "nodesInfo list was not invalidated")
 	nodesInfo = cache.GetNodesInfoPodsWithAffinity()
 	expectHost1AndHost2(t, nodesInfo)
 
 	// remove node
 	cache.RemoveNode(newNode)
-	assert.Assert(t, cache.nodesInfoPodsWithAffinity == nil, "nodesInfo list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithAffinity.Load() == nil, "nodesInfo list was not invalidated")
 	nodesInfo = cache.GetNodesInfoPodsWithAffinity()
 	expectHost(t, host1, nodesInfo)
 
@@ -393,7 +394,7 @@ func TestGetNodesInfoPodsWithAffinity(t *testing.T) {
 		},
 	}
 	cache.UpdateNode(updatedNode)
-	assert.Assert(t, cache.nodesInfoPodsWithAffinity == nil, "node list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithAffinity.Load() == nil, "node list was not invalidated")
 	nodesInfo = cache.GetNodesInfoPodsWithAffinity()
 	expectHost(t, host1, nodesInfo)
 
@@ -415,13 +416,13 @@ func TestGetNodesInfoPodsWithAffinity(t *testing.T) {
 		},
 	}
 	cache.AssumePod(pod2, true)
-	assert.Assert(t, cache.nodesInfoPodsWithAffinity == nil, "node list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithAffinity.Load() == nil, "node list was not invalidated")
 	nodesInfo = cache.GetNodesInfoPodsWithAffinity()
 	expectHost(t, host1, nodesInfo)
 
 	// remove pod
 	cache.RemovePod(pod2)
-	assert.Assert(t, cache.nodesInfoPodsWithAffinity == nil, "node list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithAffinity.Load() == nil, "node list was not invalidated")
 	nodesInfo = cache.GetNodesInfoPodsWithAffinity()
 	expectHost(t, host1, nodesInfo)
 
@@ -440,7 +441,7 @@ func TestGetNodesInfoPodsWithAffinity(t *testing.T) {
 	expectHost(t, host1, nodesInfo)
 	cache.assumePod(pod3, true)
 	cache.updatePod(pod3)
-	assert.Assert(t, cache.nodesInfoPodsWithAffinity != nil, "node list was invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithAffinity.Load() != nil, "node list was invalidated")
 
 	// add & update pod w/ affinity
 	pod4 := &v1.Pod{
@@ -463,13 +464,13 @@ func TestGetNodesInfoPodsWithAffinity(t *testing.T) {
 	expectHost(t, host1, nodesInfo)
 	cache.assumePod(pod4, true)
 	cache.updatePod(pod4)
-	assert.Assert(t, cache.nodesInfoPodsWithAffinity == nil, "node list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithAffinity.Load() == nil, "node list was not invalidated")
 }
 
 //nolint:funlen
 func TestGetNodesInfoPodsWithReqAntiAffinity(t *testing.T) {
 	cache := NewSchedulerCache(client.NewMockedAPIProvider(false).GetAPIs())
-	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity == nil)
+	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity.Load() == nil)
 	node := &v1.Node{
 		ObjectMeta: apis.ObjectMeta{
 			Name:      host1,
@@ -481,7 +482,7 @@ func TestGetNodesInfoPodsWithReqAntiAffinity(t *testing.T) {
 		},
 	}
 	cache.UpdateNode(node)
-	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity == nil)
+	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity.Load() == nil)
 	cache.AssumePod(&v1.Pod{
 		TypeMeta: apis.TypeMeta{
 			Kind:       "Pod",
@@ -515,7 +516,7 @@ func TestGetNodesInfoPodsWithReqAntiAffinity(t *testing.T) {
 		},
 	}
 	cache.UpdateNode(newNode)
-	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity == nil, "nodesInfo list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity.Load() == nil, "nodesInfo list was not invalidated")
 	cache.AssumePod(&v1.Pod{
 		TypeMeta: apis.TypeMeta{
 			Kind:       "Pod",
@@ -534,13 +535,13 @@ func TestGetNodesInfoPodsWithReqAntiAffinity(t *testing.T) {
 			NodeName: host2,
 		},
 	}, true)
-	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity == nil, "nodesInfo list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity.Load() == nil, "nodesInfo list was not invalidated")
 	nodesInfo = cache.GetNodesInfoPodsWithReqAntiAffinity()
 	expectHost1AndHost2(t, nodesInfo)
 
 	// remove node
 	cache.RemoveNode(newNode)
-	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity == nil, "nodesInfo list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity.Load() == nil, "nodesInfo list was not invalidated")
 	nodesInfo = cache.GetNodesInfoPodsWithReqAntiAffinity()
 	expectHost(t, host1, nodesInfo)
 
@@ -556,7 +557,7 @@ func TestGetNodesInfoPodsWithReqAntiAffinity(t *testing.T) {
 		},
 	}
 	cache.UpdateNode(updatedNode)
-	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity == nil, "node list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity.Load() == nil, "node list was not invalidated")
 	nodesInfo = cache.GetNodesInfoPodsWithReqAntiAffinity()
 	expectHost(t, host1, nodesInfo)
 
@@ -580,13 +581,13 @@ func TestGetNodesInfoPodsWithReqAntiAffinity(t *testing.T) {
 		},
 	}
 	cache.AssumePod(pod2, true)
-	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity == nil, "node list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity.Load() == nil, "node list was not invalidated")
 	nodesInfo = cache.GetNodesInfoPodsWithReqAntiAffinity()
 	expectHost(t, host1, nodesInfo)
 
 	// remove pod
 	cache.RemovePod(pod2)
-	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity == nil, "node list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity.Load() == nil, "node list was not invalidated")
 	nodesInfo = cache.GetNodesInfoPodsWithReqAntiAffinity()
 	expectHost(t, host1, nodesInfo)
 
@@ -605,7 +606,7 @@ func TestGetNodesInfoPodsWithReqAntiAffinity(t *testing.T) {
 	expectHost(t, host1, nodesInfo)
 	cache.assumePod(pod3, true)
 	cache.updatePod(pod3)
-	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity != nil, "node list was invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity.Load() != nil, "node list was invalidated")
 
 	// add & update pod w/ anti-affinity
 	pod4 := &v1.Pod{
@@ -630,7 +631,91 @@ func TestGetNodesInfoPodsWithReqAntiAffinity(t *testing.T) {
 	expectHost(t, host1, nodesInfo)
 	cache.assumePod(pod4, true)
 	cache.updatePod(pod4)
-	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity == nil, "node list was not invalidated")
+	assert.Assert(t, cache.nodesInfoPodsWithReqAntiAffinity.Load() == nil, "node list was not invalidated")
+}
+
+func TestGetNodesInfoConcurrentPopulation(t *testing.T) {
+	cache := NewSchedulerCache(client.NewMockedAPIProvider(false).GetAPIs())
+	cache.UpdateNode(&v1.Node{
+		ObjectMeta: apis.ObjectMeta{
+			Name:      host1,
+			Namespace: "default",
+			UID:       nodeUID1,
+		},
+	})
+	cache.AssumePod(&v1.Pod{
+		TypeMeta: apis.TypeMeta{
+			Kind:       "Pod",
+			APIVersion: "v1",
+		},
+		ObjectMeta: apis.ObjectMeta{
+			Name: podName1,
+			UID:  podUID1,
+		},
+		Spec: v1.PodSpec{
+			Affinity: &v1.Affinity{
+				PodAntiAffinity: &v1.PodAntiAffinity{
+					RequiredDuringSchedulingIgnoredDuringExecution: []v1.PodAffinityTerm{{}},
+				},
+			},
+			NodeName: host1,
+		},
+	}, true)
+	node2 := &v1.Node{
+		ObjectMeta: apis.ObjectMeta{
+			Name:      host2,
+			Namespace: "default",
+			UID:       nodeUID2,
+		},
+	}
+
+	// adding and removing a node invalidates all three lists, so every round leaves the readers
+	// with nothing cached to share
+	const rounds = 5
+	for i := 0; i < rounds; i++ {
+		cache.UpdateNode(node2)
+		assertConcurrentReads(t, cache, 2)
+		cache.RemoveNode(node2)
+		assertConcurrentReads(t, cache, 1)
+	}
+}
+
+type nodeListSizes struct {
+	all                 int
+	withAffinity        int
+	withReqAntiAffinity int
+}
+
+// assertConcurrentReads populates the cached node lists the way the predicate shared lister does:
+// from several goroutines at once, holding nothing but the read lock.
+func assertConcurrentReads(t *testing.T, cache *SchedulerCache, expectedNodes int) {
+	t.Helper()
+	const readers = 8
+	seen := make([]nodeListSizes, readers)
+	start := make(chan struct{})
+	var wg sync.WaitGroup
+	for i := 0; i < readers; i++ {
+		wg.Add(1)
+		go func() {
+			defer wg.Done()
+			<-start
+			cache.LockForReads()
+			defer cache.UnlockForReads()
+			seen[i] = nodeListSizes{
+				all:                 len(cache.GetNodesInfo()),
+				withAffinity:        len(cache.GetNodesInfoPodsWithAffinity()),
+				withReqAntiAffinity: len(cache.GetNodesInfoPodsWithReqAntiAffinity()),
+			}
+		}()
+	}
+	close(start)
+	wg.Wait()
+
+	// host1 holds the only pod, and a required anti-affinity term counts as affinity as well
+	want := nodeListSizes{all: expectedNodes, withAffinity: 1, withReqAntiAffinity: 1}
+	for _, got := range seen {
+		assert.Equal(t, want, got, "reader saw a different set of node lists")
+	}
 }
 
 func TestUpdateNonExistNode(t *testing.T) {


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@yunikorn.apache.org
For additional commands, e-mail: issues-help@yunikorn.apache.org