From 642904204acc116720a8ec1a347fc549f84cf6b2 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 6 Jul 2023 16:48:06 -0400 Subject: [PATCH] enable global schema cache --- internal/plugin/grpc_provider.go | 26 ++++++++++++-------------- internal/plugin6/grpc_provider.go | 26 ++++++++++++-------------- internal/terraform/context_plugins.go | 5 ++++- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/internal/plugin/grpc_provider.go b/internal/plugin/grpc_provider.go index c1b9a1af0752..8a71ef793253 100644 --- a/internal/plugin/grpc_provider.go +++ b/internal/plugin/grpc_provider.go @@ -68,9 +68,9 @@ type GRPCProvider struct { ctx context.Context // schema stores the schema for this provider. This is used to properly - // serialize the state for requests. - mu sync.Mutex - schemas providers.GetProviderSchemaResponse + // serialize the requests for schemas. + mu sync.Mutex + schema providers.GetProviderSchemaResponse } func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResponse) { @@ -85,8 +85,8 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp } } - if p.schemas.Provider.Block != nil { - return p.schemas + if p.schema.Provider.Block != nil { + return p.schema } resp.ResourceTypes = make(map[string]providers.Schema) @@ -135,18 +135,16 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp if protoResp.ServerCapabilities != nil { resp.ServerCapabilities.PlanDestroy = protoResp.ServerCapabilities.PlanDestroy + resp.ServerCapabilities.GetProviderSchemaOptional = protoResp.ServerCapabilities.GetProviderSchemaOptional } - // FIXME: Waiting for a provider capability to prevent caching - // providers which always need GetProviderSchema called. // set the global cache if we can - //if !p.Addr.IsZero() { - // providers.SchemaCache.Set(p.Addr, resp) - //} else { - // // otherwise store it in the local cache - // p.schemas = resp - //} - p.schemas = resp + if !p.Addr.IsZero() && resp.ServerCapabilities.GetProviderSchemaOptional { + providers.SchemaCache.Set(p.Addr, resp) + } else { + // otherwise store it in the local cache + p.schema = resp + } return resp } diff --git a/internal/plugin6/grpc_provider.go b/internal/plugin6/grpc_provider.go index c4888d709cc3..9b1022a53368 100644 --- a/internal/plugin6/grpc_provider.go +++ b/internal/plugin6/grpc_provider.go @@ -68,9 +68,9 @@ type GRPCProvider struct { ctx context.Context // schema stores the schema for this provider. This is used to properly - // serialize the state for requests. - mu sync.Mutex - schemas providers.GetProviderSchemaResponse + // serialize the requests for schemas. + mu sync.Mutex + schema providers.GetProviderSchemaResponse } func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResponse) { @@ -85,8 +85,8 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp } } - if p.schemas.Provider.Block != nil { - return p.schemas + if p.schema.Provider.Block != nil { + return p.schema } resp.ResourceTypes = make(map[string]providers.Schema) @@ -135,18 +135,16 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp if protoResp.ServerCapabilities != nil { resp.ServerCapabilities.PlanDestroy = protoResp.ServerCapabilities.PlanDestroy + resp.ServerCapabilities.GetProviderSchemaOptional = protoResp.ServerCapabilities.GetProviderSchemaOptional } - // FIXME: Waiting for a provider capability to prevent caching - // providers which always need GetProviderSchema called. // set the global cache if we can - //if !p.Addr.IsZero() { - // providers.SchemaCache.Set(p.Addr, resp) - //} else { - // // otherwise store it in the local cache - // p.schemas = resp - //} - p.schemas = resp + if !p.Addr.IsZero() && resp.ServerCapabilities.GetProviderSchemaOptional { + providers.SchemaCache.Set(p.Addr, resp) + } else { + // otherwise store it in the local cache + p.schema = resp + } return resp } diff --git a/internal/terraform/context_plugins.go b/internal/terraform/context_plugins.go index 1297afef81c6..f49ffed41324 100644 --- a/internal/terraform/context_plugins.go +++ b/internal/terraform/context_plugins.go @@ -68,7 +68,10 @@ func (cp *contextPlugins) NewProvisionerInstance(typ string) (provisioners.Inter func (cp *contextPlugins) ProviderSchema(addr addrs.Provider) (providers.ProviderSchema, error) { log.Printf("[TRACE] terraform.contextPlugins: Initializing provider %q to read its schema", addr) - // check the global schema cache first + // Check the global schema cache first. + // This cache is only written by the provider client, and transparently + // used by GetProviderSchema, but we check it here because at this point we + // may be able to avoid spinning up the provider instance at all. schemas, ok := providers.SchemaCache.Get(addr) if ok { return schemas, nil