Skip to content

Commit

Permalink
fix: mark duplicate module operation as done
Browse files Browse the repository at this point in the history
This is to prevent deadlock in tests, which may still occasionally
end up in a situation where two or more of the same operations
are scheduled at a time.
  • Loading branch information
radeksimko committed Nov 18, 2021
1 parent 03893f3 commit d69e70d
Showing 1 changed file with 31 additions and 35 deletions.
66 changes: 31 additions & 35 deletions internal/terraform/module/module_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (ml *moduleLoader) nonPrioCapacity() int64 {
}

func (ml *moduleLoader) executeModuleOp(ctx context.Context, modOp ModuleOperation) {
ml.logger.Printf("executing %q for %s", modOp.Type, modOp.ModulePath)
ml.logger.Printf("ML: executing %q for %q", modOp.Type, modOp.ModulePath)
// TODO: Report progress in % for each op based on queue length
defer modOp.markAsDone()

Expand Down Expand Up @@ -196,7 +196,7 @@ func (ml *moduleLoader) executeModuleOp(ctx context.Context, modOp ModuleOperati
modOp.ModulePath, modOp.Type)
return
}
ml.logger.Printf("finished %q for %s", modOp.Type, modOp.ModulePath)
ml.logger.Printf("ML: finished %q for %q", modOp.Type, modOp.ModulePath)

if modOp.Defer != nil {
go modOp.Defer(opErr)
Expand All @@ -209,56 +209,30 @@ func (ml *moduleLoader) EnqueueModuleOp(modOp ModuleOperation) error {
return err
}

ml.logger.Printf("ML: enqueing %q module operation: %s", modOp.Type, modOp.ModulePath)
ml.logger.Printf("ML: enqueing %q module operation: %q", modOp.Type, modOp.ModulePath)

if operationState(mod, modOp.Type) == op.OpStateQueued {
// avoid enqueuing duplicate operation
modOp.markAsDone()
return nil
}

switch modOp.Type {
case op.OpTypeGetTerraformVersion:
if mod.TerraformVersionState == op.OpStateQueued {
// avoid enqueuing duplicate operation
return nil
}
ml.modStore.SetTerraformVersionState(modOp.ModulePath, op.OpStateQueued)
case op.OpTypeObtainSchema:
if mod.ProviderSchemaState == op.OpStateQueued {
// avoid enqueuing duplicate operation
return nil
}
ml.modStore.SetProviderSchemaState(modOp.ModulePath, op.OpStateQueued)
case op.OpTypeParseModuleConfiguration:
if mod.ModuleParsingState == op.OpStateQueued {
// avoid enqueuing duplicate operation
return nil
}
ml.modStore.SetModuleParsingState(modOp.ModulePath, op.OpStateQueued)
case op.OpTypeParseVariables:
if mod.VarsParsingState == op.OpStateQueued {
// avoid enqueuing duplicate operation
return nil
}
ml.modStore.SetVarsParsingState(modOp.ModulePath, op.OpStateQueued)
case op.OpTypeParseModuleManifest:
if mod.ModManifestState == op.OpStateQueued {
// avoid enqueuing duplicate operation
return nil
}
ml.modStore.SetModManifestState(modOp.ModulePath, op.OpStateQueued)
case op.OpTypeLoadModuleMetadata:
if mod.MetaState == op.OpStateQueued {
// avoid enqueuing duplicate operation
return nil
}
ml.modStore.SetMetaState(modOp.ModulePath, op.OpStateQueued)
case op.OpTypeDecodeReferenceTargets:
if mod.RefTargetsState == op.OpStateQueued {
// avoid enqueuing duplicate operation
return nil
}
ml.modStore.SetReferenceTargetsState(modOp.ModulePath, op.OpStateQueued)
case op.OpTypeDecodeReferenceOrigins:
if mod.RefOriginsState == op.OpStateQueued {
// avoid enqueuing duplicate operation
return nil
}
ml.modStore.SetReferenceOriginsState(modOp.ModulePath, op.OpStateQueued)
}

Expand All @@ -268,6 +242,28 @@ func (ml *moduleLoader) EnqueueModuleOp(modOp ModuleOperation) error {
return nil
}

func operationState(mod *state.Module, opType op.OpType) op.OpState {
switch opType {
case op.OpTypeGetTerraformVersion:
return mod.TerraformVersionState
case op.OpTypeObtainSchema:
return mod.ProviderSchemaState
case op.OpTypeParseModuleConfiguration:
return mod.ModuleParsingState
case op.OpTypeParseVariables:
return mod.VarsParsingState
case op.OpTypeParseModuleManifest:
return mod.ModManifestState
case op.OpTypeLoadModuleMetadata:
return mod.MetaState
case op.OpTypeDecodeReferenceTargets:
return mod.RefTargetsState
case op.OpTypeDecodeReferenceOrigins:
return mod.RefOriginsState
}
return op.OpStateUnknown
}

func (ml *moduleLoader) DequeueModule(modPath string) {
ml.queue.DequeueAllModuleOps(modPath)
}

0 comments on commit d69e70d

Please sign in to comment.