Skip to content

Commit b979ebb

Browse files
authored
fix: clean up ServerlessRuntime record and cache on plugin uninstall (#749)
* fix: delete ServerlessRuntime record on plugin uninstall when refs reach zero * refactor: expose ClearServerlessRuntimeCache as public method on PluginManager * fix: clear serverless runtime cache on full plugin uninstall (ENG-474) * fix: clean up ServerlessRuntime record and cache on plugin upgrade when refs reach zero
1 parent 918ce6f commit b979ebb

5 files changed

Lines changed: 34 additions & 3 deletions

File tree

internal/core/plugin_manager/installer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (p *PluginManager) SwitchServerlessEndpoint(
5050
if err != nil {
5151
return err
5252
}
53-
return p.clearServerlessRuntimeCache(pluginUniqueIdentifier)
53+
return p.ClearServerlessRuntimeCache(pluginUniqueIdentifier)
5454
}
5555

5656
// serverless runtime uses a strategy that firstly compile the plugin into a docker image
@@ -109,7 +109,7 @@ func (p *PluginManager) Reinstall(
109109

110110
// cleanup system cache for serverless runtime model
111111
// cleanup must be done after updating the model, otherwise race condition may occur
112-
if err := p.clearServerlessRuntimeCache(pluginUniqueIdentifier); err != nil {
112+
if err := p.ClearServerlessRuntimeCache(pluginUniqueIdentifier); err != nil {
113113
log.Error("failed to cleanup system cache for serverless runtime model", "error", err)
114114
responseStream.Write(installation_entities.PluginInstallResponse{
115115
Event: installation_entities.PluginInstallEventError,

internal/core/plugin_manager/serverless.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (p *PluginManager) getServerlessPluginRuntimeModel(
8787
return runtime, nil
8888
}
8989

90-
func (p *PluginManager) clearServerlessRuntimeCache(
90+
func (p *PluginManager) ClearServerlessRuntimeCache(
9191
identity plugin_entities.PluginUniqueIdentifier,
9292
) error {
9393
_, err := cache.Del(p.getServerlessRuntimeCacheKey(identity))

internal/service/install_plugin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,14 @@ func UninstallPlugin(
364364

365365
if deleteResponse != nil && deleteResponse.IsPluginDeleted {
366366
helper.DeletePluginDeclarationCache(pluginUniqueIdentifier, plugin_entities.PluginRuntimeType(installation.RuntimeType))
367+
368+
if plugin_entities.PluginRuntimeType(installation.RuntimeType) == plugin_entities.PLUGIN_RUNTIME_TYPE_SERVERLESS {
369+
if manager := plugin_manager.Manager(); manager != nil {
370+
if err := manager.ClearServerlessRuntimeCache(pluginUniqueIdentifier); err != nil {
371+
log.Error("failed to clear serverless runtime cache on uninstall", "error", err)
372+
}
373+
}
374+
}
367375
}
368376

369377
if deleteResponse != nil && deleteResponse.IsPluginDeleted && deleteResponse.Plugin != nil && deleteResponse.Plugin.InstallType == plugin_entities.PLUGIN_RUNTIME_TYPE_LOCAL {

internal/tasks/install_plugin_utils.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,12 @@ func RemovePluginIfNeeded(
174174
return errors.Join(err, errors.New("failed to shutdown plugin gracefully"))
175175
}
176176
}
177+
178+
if shouldCleanup && response.DeletedPlugin != nil && response.DeletedPlugin.InstallType == plugin_entities.PLUGIN_RUNTIME_TYPE_SERVERLESS {
179+
if err := manager.ClearServerlessRuntimeCache(originalPluginUniqueIdentifier); err != nil {
180+
log.Error("failed to clear serverless runtime cache on upgrade", "error", err)
181+
}
182+
}
183+
177184
return nil
178185
}

internal/types/models/curd/atomic.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ func UninstallPlugin(
372372
}, tx); err != nil {
373373
return err
374374
}
375+
376+
if pluginToBeReturns.InstallType == plugin_entities.PLUGIN_RUNTIME_TYPE_SERVERLESS {
377+
if err := db.DeleteByCondition(&models.ServerlessRuntime{
378+
PluginUniqueIdentifier: pluginUniqueIdentifier.String(),
379+
}, tx); err != nil {
380+
return err
381+
}
382+
}
375383
}
376384

377385
return nil
@@ -498,6 +506,14 @@ func UpgradePlugin(
498506
}, tx); err != nil {
499507
return err
500508
}
509+
510+
if originalPlugin.InstallType == plugin_entities.PLUGIN_RUNTIME_TYPE_SERVERLESS {
511+
if err := db.DeleteByCondition(&models.ServerlessRuntime{
512+
PluginUniqueIdentifier: originalPluginUniqueIdentifier.String(),
513+
}, tx); err != nil {
514+
return err
515+
}
516+
}
501517
} else if err != nil {
502518
return err
503519
}

0 commit comments

Comments
 (0)