Skip to content

Commit

Permalink
Cache the OpenAPI schema. (#833)
Browse files Browse the repository at this point in the history
For large programs, re-fetching this schema repeatedly adds up to a
significant portion of execution time. This caching reduces the
execution time of the Istio example by about 60-70%.

Contributes to #827.
  • Loading branch information
pgavlin authored and lblackstone committed Oct 8, 2019
1 parent abeafd5 commit 894a4fc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Improvements

- Cache the OpenAPI schema to improve performance. (https://github.com/pulumi/pulumi-kubernetes/pull/833).
- Aggregate error messages from Pods on Job Read. (https://github.com/pulumi/pulumi-kubernetes/pull/831).
- Improve interactive status for Jobs. (https://github.com/pulumi/pulumi-kubernetes/pull/832).

Expand Down
18 changes: 17 additions & 1 deletion pkg/clients/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ limitations under the License.
// mechanism, and causes unactionable error messages to appear in the CLI.
// This error logging was disabled.
//
// Additionally, we improved caching of the OpenAPI schema in the OpenAPISchema method.
// If this change merges upstream, we will reconcile those changes in a future release.
//
// [1] https://github.com/kubernetes/client-go/blob/2dda7ceeec102b5a60e2abf37758642118501910/discovery/cached/memcache.go

package clients
Expand Down Expand Up @@ -56,6 +59,7 @@ type memCacheClient struct {
delegate discovery.DiscoveryInterface

lock sync.RWMutex
schema *openapi_v2.Document
groupToServerResources map[string]*cacheEntry
groupList *metav1.APIGroupList
cacheValid bool
Expand Down Expand Up @@ -159,7 +163,18 @@ func (d *memCacheClient) ServerVersion() (*version.Info, error) {
}

func (d *memCacheClient) OpenAPISchema() (*openapi_v2.Document, error) {
return d.delegate.OpenAPISchema()
d.lock.Lock()
defer d.lock.Unlock()

if d.schema == nil {
sch, err := d.delegate.OpenAPISchema()
if err != nil {
return nil, err
}
d.schema = sch
}

return d.schema, nil
}

func (d *memCacheClient) Fresh() bool {
Expand All @@ -179,6 +194,7 @@ func (d *memCacheClient) Invalidate() {
d.cacheValid = false
d.groupToServerResources = nil
d.groupList = nil
d.schema = nil
}

// refreshLocked refreshes the state of cache. The caller must hold d.lock for
Expand Down

0 comments on commit 894a4fc

Please sign in to comment.