Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kuma-cp): don't always recompute mesh contexts #4267

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions pkg/xds/cache/mesh/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Cache struct {
cache *once.Cache
meshContextBuilder xds_context.MeshContextBuilder

latestMeshContext *xds_context.MeshContext
latestMeshContext map[string]*xds_context.MeshContext
}

func NewCache(
Expand All @@ -33,27 +33,28 @@ func NewCache(
return &Cache{
cache: c,
meshContextBuilder: meshContextBuilder,
latestMeshContext: map[string]*xds_context.MeshContext{},
}, nil
}

func (c *Cache) GetMeshContext(ctx context.Context, syncLog logr.Logger, mesh string) (xds_context.MeshContext, error) {
elt, err := c.cache.GetOrRetrieve(ctx, mesh, once.RetrieverFunc(func(ctx context.Context, key string) (interface{}, error) {
if c.latestMeshContext == nil {
if c.latestMeshContext[mesh] == nil {
meshCtx, err := c.meshContextBuilder.Build(ctx, mesh)
if err != nil {
return xds_context.MeshContext{}, err
}
c.latestMeshContext = &meshCtx
c.latestMeshContext[mesh] = &meshCtx
} else {
meshCtx, err := c.meshContextBuilder.BuildIfChanged(ctx, mesh, c.latestMeshContext.Hash)
meshCtx, err := c.meshContextBuilder.BuildIfChanged(ctx, mesh, c.latestMeshContext[mesh].Hash)
if err != nil {
return xds_context.MeshContext{}, err
}
if meshCtx != nil {
c.latestMeshContext = meshCtx
c.latestMeshContext[mesh] = meshCtx
}
}
return *c.latestMeshContext, nil
return *c.latestMeshContext[mesh], nil
}))
if err != nil {
return xds_context.MeshContext{}, err
Expand Down
9 changes: 9 additions & 0 deletions pkg/xds/cache/mesh/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ var _ = Describe("MeshSnapshot Cache", func() {
Expect(err).ToNot(HaveOccurred())
hash2 := meshCtx2.Hash

<-time.After(expiration)

// Computing one meshcontext shouldn't cause us to recompute other
// meshcontexts
nextMeshCtx0, err := meshCache.GetMeshContext(context.Background(), logr.Discard(), "mesh-0")
Expect(err).ToNot(HaveOccurred())
// If the meshcontxt hasn't been recomputed, its fields will be identical
Expect(nextMeshCtx0.DataSourceLoader).To(BeIdenticalTo(meshCtx0.DataSourceLoader))

dp := core_mesh.NewDataplaneResource()
err = s.Get(context.Background(), dp, core_store.GetByKey("dp-1", "mesh-0"))
Expect(err).ToNot(HaveOccurred())
Expand Down