Skip to content

Commit

Permalink
enable global schema cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jbardin committed Jul 10, 2023
1 parent fb35d7f commit 6429042
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
26 changes: 12 additions & 14 deletions internal/plugin/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
26 changes: 12 additions & 14 deletions internal/plugin6/grpc_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion internal/terraform/context_plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6429042

Please sign in to comment.