- Notifications
You must be signed in to change notification settings - Fork926
fix: avoid terraform state concurrent access, remove global mutex#5273
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
20 changes: 15 additions & 5 deletionscli/server.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
75 changes: 34 additions & 41 deletionsprovisioner/terraform/executor.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -23,27 +23,15 @@ import ( | ||
"github.com/coder/coder/provisionersdk/proto" | ||
) | ||
type executor struct { | ||
mut *sync.Mutex | ||
coadler marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
binaryPath string | ||
// cachePath and workdir must not be used by multiple processes at once. | ||
cachePath string | ||
workdir string | ||
} | ||
func (e*executor) basicEnv() []string { | ||
// Required for "terraform init" to find "git" to | ||
// clone Terraform modules. | ||
env := safeEnviron() | ||
@@ -55,7 +43,8 @@ func (e executor) basicEnv() []string { | ||
return env | ||
} | ||
// execWriteOutput must only be called while the lock is held. | ||
func (e *executor) execWriteOutput(ctx, killCtx context.Context, args, env []string, stdOutWriter, stdErrWriter io.WriteCloser) (err error) { | ||
defer func() { | ||
closeErr := stdOutWriter.Close() | ||
if err == nil && closeErr != nil { | ||
@@ -98,7 +87,8 @@ func (e executor) execWriteOutput(ctx, killCtx context.Context, args, env []stri | ||
return cmd.Wait() | ||
} | ||
// execParseJSON must only be called while the lock is held. | ||
func (e *executor) execParseJSON(ctx, killCtx context.Context, args, env []string, v interface{}) error { | ||
if ctx.Err() != nil { | ||
return ctx.Err() | ||
} | ||
@@ -133,7 +123,7 @@ func (e executor) execParseJSON(ctx, killCtx context.Context, args, env []string | ||
return nil | ||
} | ||
func (e*executor) checkMinVersion(ctx context.Context) error { | ||
v, err := e.version(ctx) | ||
if err != nil { | ||
return err | ||
@@ -147,7 +137,8 @@ func (e executor) checkMinVersion(ctx context.Context) error { | ||
return nil | ||
} | ||
// version doesn't need the lock because it doesn't read or write to any state. | ||
func (e *executor) version(ctx context.Context) (*version.Version, error) { | ||
return versionFromBinaryPath(ctx, e.binaryPath) | ||
} | ||
@@ -177,7 +168,10 @@ func versionFromBinaryPath(ctx context.Context, binaryPath string) (*version.Ver | ||
return version.NewVersion(vj.Version) | ||
} | ||
func (e *executor) init(ctx, killCtx context.Context, logr logSink) error { | ||
e.mut.Lock() | ||
defer e.mut.Unlock() | ||
outWriter, doneOut := logWriter(logr, proto.LogLevel_DEBUG) | ||
errWriter, doneErr := logWriter(logr, proto.LogLevel_ERROR) | ||
defer func() { | ||
@@ -193,23 +187,14 @@ func (e executor) init(ctx, killCtx context.Context, logr logSink) error { | ||
"-input=false", | ||
} | ||
return e.execWriteOutput(ctx, killCtx, args, e.basicEnv(), outWriter, errWriter) | ||
} | ||
// revive:disable-next-line:flag-parameter | ||
func (e *executor) plan(ctx, killCtx context.Context, env, vars []string, logr logSink, destroy bool) (*proto.Provision_Response, error) { | ||
e.mut.Lock() | ||
defer e.mut.Unlock() | ||
planfilePath := filepath.Join(e.workdir, "terraform.tfplan") | ||
args := []string{ | ||
"plan", | ||
@@ -257,7 +242,8 @@ func (e executor) plan(ctx, killCtx context.Context, env, vars []string, logr lo | ||
}, nil | ||
} | ||
// planResources must only be called while the lock is held. | ||
func (e *executor) planResources(ctx, killCtx context.Context, planfilePath string) ([]*proto.Resource, error) { | ||
plan, err := e.showPlan(ctx, killCtx, planfilePath) | ||
if err != nil { | ||
return nil, xerrors.Errorf("show terraform plan file: %w", err) | ||
@@ -270,14 +256,16 @@ func (e executor) planResources(ctx, killCtx context.Context, planfilePath strin | ||
return ConvertResources(plan.PlannedValues.RootModule, rawGraph) | ||
} | ||
// showPlan must only be called while the lock is held. | ||
func (e *executor) showPlan(ctx, killCtx context.Context, planfilePath string) (*tfjson.Plan, error) { | ||
args := []string{"show", "-json", "-no-color", planfilePath} | ||
p := new(tfjson.Plan) | ||
err := e.execParseJSON(ctx, killCtx, args, e.basicEnv(), p) | ||
return p, err | ||
} | ||
// graph must only be called while the lock is held. | ||
func (e *executor) graph(ctx, killCtx context.Context) (string, error) { | ||
if ctx.Err() != nil { | ||
return "", ctx.Err() | ||
} | ||
@@ -302,9 +290,12 @@ func (e executor) graph(ctx, killCtx context.Context) (string, error) { | ||
} | ||
// revive:disable-next-line:flag-parameter | ||
func (e*executor) apply( | ||
ctx, killCtx context.Context, plan []byte, env []string, logr logSink, | ||
) (*proto.Provision_Response, error) { | ||
e.mut.Lock() | ||
defer e.mut.Unlock() | ||
planFile, err := ioutil.TempFile("", "coder-terrafrom-plan") | ||
if err != nil { | ||
return nil, xerrors.Errorf("create plan file: %w", err) | ||
@@ -356,7 +347,8 @@ func (e executor) apply( | ||
}, nil | ||
} | ||
// stateResources must only be called while the lock is held. | ||
func (e *executor) stateResources(ctx, killCtx context.Context) ([]*proto.Resource, error) { | ||
state, err := e.state(ctx, killCtx) | ||
if err != nil { | ||
return nil, err | ||
@@ -375,7 +367,8 @@ func (e executor) stateResources(ctx, killCtx context.Context) ([]*proto.Resourc | ||
return resources, nil | ||
} | ||
// state must only be called while the lock is held. | ||
func (e *executor) state(ctx, killCtx context.Context) (*tfjson.State, error) { | ||
args := []string{"show", "-json", "-no-color"} | ||
state := &tfjson.State{} | ||
err := e.execParseJSON(ctx, killCtx, args, e.basicEnv(), state) | ||
6 changes: 3 additions & 3 deletionsprovisioner/terraform/provision_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
13 changes: 9 additions & 4 deletionsprovisioner/terraform/serve.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletionprovisionerd/provisionerd.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.