Skip to content

Commit

Permalink
Use a new module cache for each execution
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume.bouvignies committed Sep 12, 2023
1 parent 7873b51 commit 9f8622c
Showing 1 changed file with 13 additions and 12 deletions.
Expand Up @@ -49,11 +49,10 @@ var (

type StartosisInterpreter struct {
// This is mutex protected as interpreting two different scripts in parallel could potentially cause
// problems with the moduleGlobalsCache & moduleContentProvider. Fixing this is quite complicated, which we decided not to do.
mutex *sync.Mutex
serviceNetwork service_network.ServiceNetwork
recipeExecutor *runtime_value_store.RuntimeValueStore
moduleGlobalsCache map[string]*startosis_packages.ModuleCacheEntry
// problems with moduleContentProvider. Fixing this is quite complicated, which we decided not to do.
mutex *sync.Mutex
serviceNetwork service_network.ServiceNetwork
recipeExecutor *runtime_value_store.RuntimeValueStore
// TODO AUTH there will be a leak here in case people with different repo visibility access a module
moduleContentProvider startosis_packages.PackageContentProvider
enclaveEnvVars string
Expand All @@ -66,7 +65,6 @@ func NewStartosisInterpreter(serviceNetwork service_network.ServiceNetwork, modu
mutex: &sync.Mutex{},
serviceNetwork: serviceNetwork,
recipeExecutor: runtimeValueStore,
moduleGlobalsCache: make(map[string]*startosis_packages.ModuleCacheEntry),
moduleContentProvider: moduleContentProvider,
enclaveEnvVars: enclaveVarEnvs,
}
Expand Down Expand Up @@ -221,7 +219,10 @@ func (interpreter *StartosisInterpreter) Interpret(
if packageId != startosis_constants.PackageIdPlaceholderForStandaloneScript {
moduleLocator = path.Join(moduleLocator, relativePathtoMainFile)
}
globalVariables, interpretationErr := interpreter.interpretInternal(moduleLocator, serializedStarlark, newInstructionsPlan)

// we use a new cache for every interpretation b/c the content of the module might have changed
moduleGlobalCache := map[string]*startosis_packages.ModuleCacheEntry{}
globalVariables, interpretationErr := interpreter.interpretInternal(moduleLocator, serializedStarlark, newInstructionsPlan, moduleGlobalCache)
if interpretationErr != nil {
return startosis_constants.NoOutputObject, nil, interpretationErr.ToAPIType()
}
Expand Down Expand Up @@ -307,13 +308,13 @@ func (interpreter *StartosisInterpreter) Interpret(
return startosis_constants.NoOutputObject, newInstructionsPlan, nil
}

func (interpreter *StartosisInterpreter) interpretInternal(moduleLocator string, serializedStarlark string, instructionPlan *instructions_plan.InstructionsPlan) (starlark.StringDict, *startosis_errors.InterpretationError) {
func (interpreter *StartosisInterpreter) interpretInternal(moduleLocator string, serializedStarlark string, instructionPlan *instructions_plan.InstructionsPlan, moduleGlobalCache map[string]*startosis_packages.ModuleCacheEntry) (starlark.StringDict, *startosis_errors.InterpretationError) {
// We spin up a new thread for every call to interpreterInternal such that the stacktrace provided by the Starlark
// Go interpreter is relative to each individual thread, and we don't keep accumulating stacktrace entries from the
// previous calls inside the same thread
// The thread name is set to the locator of the module so that we can use it to resolve relative paths
thread := newStarlarkThread(moduleLocator)
predeclared, interpretationErr := interpreter.buildBindings(thread, instructionPlan)
predeclared, interpretationErr := interpreter.buildBindings(thread, instructionPlan, moduleGlobalCache)
if interpretationErr != nil {
return nil, interpretationErr
}
Expand All @@ -326,9 +327,9 @@ func (interpreter *StartosisInterpreter) interpretInternal(moduleLocator string,
return globalVariables, nil
}

func (interpreter *StartosisInterpreter) buildBindings(thread *starlark.Thread, instructionPlan *instructions_plan.InstructionsPlan) (*starlark.StringDict, *startosis_errors.InterpretationError) {
func (interpreter *StartosisInterpreter) buildBindings(thread *starlark.Thread, instructionPlan *instructions_plan.InstructionsPlan, moduleGlobalCache map[string]*startosis_packages.ModuleCacheEntry) (*starlark.StringDict, *startosis_errors.InterpretationError) {
recursiveInterpretForModuleLoading := func(moduleId string, serializedStartosis string) (starlark.StringDict, *startosis_errors.InterpretationError) {
result, err := interpreter.interpretInternal(moduleId, serializedStartosis, instructionPlan)
result, err := interpreter.interpretInternal(moduleId, serializedStartosis, instructionPlan, moduleGlobalCache)
if err != nil {
return nil, err
}
Expand All @@ -345,7 +346,7 @@ func (interpreter *StartosisInterpreter) buildBindings(thread *starlark.Thread,
predeclared[builtins.KurtosisModuleName] = kurtosisModule

// Add all Kurtosis helpers
for _, kurtosisHelper := range KurtosisHelpers(recursiveInterpretForModuleLoading, interpreter.moduleContentProvider, interpreter.moduleGlobalsCache) {
for _, kurtosisHelper := range KurtosisHelpers(recursiveInterpretForModuleLoading, interpreter.moduleContentProvider, moduleGlobalCache) {
predeclared[kurtosisHelper.Name()] = kurtosisHelper
}

Expand Down

0 comments on commit 9f8622c

Please sign in to comment.