Skip to content

Commit 62430e1

Browse files
authored
Merge pull request #908 from shajmakh/nrt-gen-log-fix
nrt: log: return updated generation value for cache
2 parents f071739 + ffe2ce2 commit 62430e1

File tree

2 files changed

+74
-8
lines changed

2 files changed

+74
-8
lines changed

pkg/noderesourcetopology/cache/overreserve.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,16 @@ func NewOverReserve(ctx context.Context, lh logr.Logger, cfg *apiconfig.NodeReso
101101
func (ov *OverReserve) GetCachedNRTCopy(ctx context.Context, nodeName string, pod *corev1.Pod) (*topologyv1alpha2.NodeResourceTopology, CachedNRTInfo) {
102102
ov.lock.Lock()
103103
defer ov.lock.Unlock()
104+
info := CachedNRTInfo{Generation: ov.generation}
104105
if ov.nodesWithForeignPods.IsSet(nodeName) {
105-
return nil, CachedNRTInfo{}
106+
return nil, info
106107
}
107108

108-
info := CachedNRTInfo{Fresh: true}
109+
info.Fresh = true
109110
nrt := ov.nrts.GetNRTCopyByNodeName(nodeName)
110111
if nrt == nil {
111112
return nil, info
112113
}
113-
114-
info.Generation = ov.generation
115114
nodeAssumedResources, ok := ov.assumedResources[nodeName]
116115
if !ok {
117116
return nrt, info
@@ -341,7 +340,7 @@ func (ov *OverReserve) Resync() {
341340
}
342341

343342
// FlushNodes drops all the cached information about a given node, resetting its state clean.
344-
func (ov *OverReserve) FlushNodes(lh logr.Logger, nrts ...*topologyv1alpha2.NodeResourceTopology) {
343+
func (ov *OverReserve) FlushNodes(lh logr.Logger, nrts ...*topologyv1alpha2.NodeResourceTopology) uint64 {
345344
ov.lock.Lock()
346345
defer ov.lock.Unlock()
347346

@@ -355,12 +354,14 @@ func (ov *OverReserve) FlushNodes(lh logr.Logger, nrts ...*topologyv1alpha2.Node
355354
}
356355

357356
if len(nrts) == 0 {
358-
return
357+
return ov.generation
359358
}
360359

361360
// increase only if we mutated the internal state
362361
ov.generation += 1
363362
lh.V(2).Info("generation", "new", ov.generation)
363+
return ov.generation
364+
364365
}
365366

366367
// to be used only in tests

pkg/noderesourcetopology/cache/overreserve_test.go

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"k8s.io/apimachinery/pkg/api/equality"
3131
"k8s.io/apimachinery/pkg/api/resource"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33+
"k8s.io/apimachinery/pkg/runtime"
3334
podlisterv1 "k8s.io/client-go/listers/core/v1"
3435
"k8s.io/klog/v2"
3536

@@ -434,17 +435,35 @@ func TestFlush(t *testing.T) {
434435

435436
lh := klog.Background()
436437

437-
nrtCache.FlushNodes(lh, expectedNodeTopology.DeepCopy())
438+
expectedGen := nrtCache.generation + 1
439+
gen1 := nrtCache.FlushNodes(lh, expectedNodeTopology.DeepCopy())
440+
if gen1 != expectedGen {
441+
t.Fatalf("generation is expected to increase once after flushing a dirty node\ngot %d expected %d", gen1, expectedGen)
442+
}
438443

439444
dirtyNodes := nrtCache.GetDesyncedNodes(lh)
440445
if dirtyNodes.Len() != 0 {
441446
t.Errorf("dirty nodes after flush: %v", dirtyNodes)
442447
}
443448

444-
nrtObj, _ := nrtCache.GetCachedNRTCopy(context.Background(), "node1", testPod)
449+
nrtObj, nrtInfo := nrtCache.GetCachedNRTCopy(context.Background(), "node1", testPod)
445450
if !reflect.DeepEqual(nrtObj, expectedNodeTopology) {
446451
t.Fatalf("unexpected object from cache\ngot: %s\nexpected: %s\n", dumpNRT(nrtObj), dumpNRT(nodeTopologies[0]))
447452
}
453+
454+
expectedNrtInfo := CachedNRTInfo{
455+
Fresh: true,
456+
Generation: expectedGen,
457+
}
458+
if !reflect.DeepEqual(nrtInfo, expectedNrtInfo) {
459+
t.Fatalf("unexpected NRT info from cache\ngot: %+v\nexpected: %+v\n", nrtInfo, expectedNrtInfo)
460+
}
461+
462+
// flush again without dirty nodes
463+
gen2 := nrtCache.FlushNodes(lh)
464+
if gen2 != expectedGen {
465+
t.Fatalf("generation shouldn't change with no dirty nodes\ngot %d expected %d", gen2, expectedGen)
466+
}
448467
}
449468

450469
func TestResyncNoPodFingerprint(t *testing.T) {
@@ -971,3 +990,49 @@ func TestMakeNodeToPodDataMap(t *testing.T) {
971990
})
972991
}
973992
}
993+
994+
func TestOverresevedGetCachedNRTCopyWithForeignPods(t *testing.T) {
995+
testNodeName := "worker-node-G"
996+
nrt := makeTestNRT(testNodeName)
997+
pod := &corev1.Pod{} // API placeholder
998+
fakePodLister := &fakePodLister{}
999+
1000+
objs := []runtime.Object{nrt}
1001+
fakeClient, err := tu.NewFakeClient(objs...)
1002+
if err != nil {
1003+
t.Fatal(err)
1004+
}
1005+
1006+
ctx := context.Background()
1007+
lh := klog.Background()
1008+
nrtCache, err := NewOverReserve(ctx, lh, nil, fakeClient, fakePodLister, podprovider.IsPodRelevantAlways)
1009+
if err != nil {
1010+
t.Fatalf("unexpected error creating cache: %v", err)
1011+
}
1012+
1013+
expectedNrtInfo := CachedNRTInfo{
1014+
Generation: 0,
1015+
Fresh: true,
1016+
}
1017+
_, gotInfo := nrtCache.GetCachedNRTCopy(ctx, testNodeName, pod)
1018+
if !reflect.DeepEqual(gotInfo, expectedNrtInfo) {
1019+
t.Errorf("mismatched nrt info from cache, got %+v expected %+v", gotInfo, expectedNrtInfo)
1020+
}
1021+
1022+
// pointless, but will force a generation increase
1023+
gen := nrtCache.FlushNodes(lh, nrt)
1024+
if gen == 0 {
1025+
t.Fatalf("FlushNodes didn't increase the generation")
1026+
}
1027+
1028+
// expected to mark cached data as stale (!fresh)
1029+
nrtCache.NodeHasForeignPods(testNodeName, pod)
1030+
1031+
_, gotInfo = nrtCache.GetCachedNRTCopy(ctx, testNodeName, pod)
1032+
if gotInfo.Generation != gen {
1033+
t.Errorf("mismatched generation, got %v expected %v", gotInfo.Generation, gen)
1034+
}
1035+
if gotInfo.Fresh {
1036+
t.Errorf("cached data reported fresh when node has foreign pods")
1037+
}
1038+
}

0 commit comments

Comments
 (0)