Skip to content

Commit 01e9bbf

Browse files
committed
Add a mutation cache to the ComputeDomainManager in the daemon pod
Signed-off-by: Kevin Klues <kklues@nvidia.com>
1 parent 764615f commit 01e9bbf

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

cmd/compute-domain-daemon/computedomain.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434

3535
const (
3636
informerResyncPeriod = 10 * time.Minute
37+
mutationCacheTTL = time.Hour
3738
)
3839

3940
// GetComputeDomainFunc is a function type for getting a ComputeDomain by UID.
@@ -54,7 +55,8 @@ type ComputeDomainManager struct {
5455
previousNodes []*nvapi.ComputeDomainNode
5556
updatedNodesChan chan []*nvapi.ComputeDomainNode
5657

57-
podManager *PodManager
58+
podManager *PodManager
59+
mutationCache cache.MutationCache
5860
}
5961

6062
// NewComputeDomainManager creates a new ComputeDomainManager instance.
@@ -101,6 +103,15 @@ func (m *ComputeDomainManager) Start(ctx context.Context) (rerr error) {
101103
return fmt.Errorf("error adding indexer for ComputeDomain UID: %w", err)
102104
}
103105

106+
// Create mutation cache to track our own updates
107+
m.mutationCache = cache.NewIntegerResourceVersionMutationCache(
108+
klog.Background(),
109+
m.informer.GetStore(),
110+
m.informer.GetIndexer(),
111+
mutationCacheTTL,
112+
true,
113+
)
114+
104115
_, err = m.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
105116
AddFunc: func(obj any) {
106117
m.config.workQueue.Enqueue(obj, m.onAddOrUpdate)
@@ -157,23 +168,19 @@ func (m *ComputeDomainManager) Stop() error {
157168
return nil
158169
}
159170

160-
// Get gets the ComputeDomain by UID from the informer cache.
171+
// Get gets the ComputeDomain by UID from the mutation cache.
161172
func (m *ComputeDomainManager) Get(uid string) (*nvapi.ComputeDomain, error) {
162-
objs, err := m.informer.GetIndexer().ByIndex("uid", uid)
173+
cds, err := getByComputeDomainUID[*nvapi.ComputeDomain](m.mutationCache, uid)
163174
if err != nil {
164175
return nil, fmt.Errorf("error retrieving ComputeDomain by UID: %w", err)
165176
}
166-
if len(objs) == 0 {
177+
if len(cds) == 0 {
167178
return nil, nil
168179
}
169-
if len(objs) != 1 {
180+
if len(cds) != 1 {
170181
return nil, fmt.Errorf("multiple ComputeDomains with the same UID")
171182
}
172-
cd, ok := objs[0].(*nvapi.ComputeDomain)
173-
if !ok {
174-
return nil, fmt.Errorf("error casting to ComputeDomain")
175-
}
176-
return cd, nil
183+
return cds[0], nil
177184
}
178185

179186
// onAddOrUpdate handles the addition or update of a ComputeDomain.
@@ -266,6 +273,9 @@ func (m *ComputeDomainManager) UpdateComputeDomainNodeInfo(ctx context.Context,
266273
return fmt.Errorf("error updating nodes in ComputeDomain status: %w", err)
267274
}
268275

276+
// Add the updated ComputeDomain to the mutation cache
277+
m.mutationCache.Mutation(newCD)
278+
269279
return nil
270280
}
271281

cmd/compute-domain-daemon/indexers.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121

2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/client-go/tools/cache"
2324
)
2425

2526
func uidIndexer[T metav1.ObjectMetaAccessor](obj any) ([]string, error) {
@@ -29,3 +30,19 @@ func uidIndexer[T metav1.ObjectMetaAccessor](obj any) ([]string, error) {
2930
}
3031
return []string{string(d.GetObjectMeta().GetUID())}, nil
3132
}
33+
34+
// getByComputeDomainUID retrieves objects by UID using the mutation cache.
35+
func getByComputeDomainUID[T any](mutationCache cache.MutationCache, uid string) ([]T, error) {
36+
objs, err := mutationCache.ByIndex("uid", uid)
37+
if err != nil {
38+
return nil, fmt.Errorf("error retrieving objects by UID: %w", err)
39+
}
40+
41+
var result []T
42+
for _, obj := range objs {
43+
if t, ok := obj.(T); ok {
44+
result = append(result, t)
45+
}
46+
}
47+
return result, nil
48+
}

0 commit comments

Comments
 (0)