-
Notifications
You must be signed in to change notification settings - Fork 289
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restart crashed plugins #1236
Merged
Merged
Restart crashed plugins #1236
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package plugin | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
"github.com/kubeshop/botkube/pkg/api/executor" | ||
"github.com/kubeshop/botkube/pkg/api/source" | ||
"github.com/kubeshop/botkube/pkg/config" | ||
) | ||
|
||
// HealthMonitor restarts a failed plugin process and inform scheduler to start dispatching loop again with a new client that was generated. | ||
type HealthMonitor struct { | ||
log logrus.FieldLogger | ||
logConfig config.Logger | ||
sourceSupervisorChan chan pluginMetadata | ||
executorSupervisorChan chan pluginMetadata | ||
schedulerChan chan string | ||
executorsStore *store[executor.Executor] | ||
sourcesStore *store[source.Source] | ||
policy config.PluginRestartPolicy | ||
pluginRestartStats map[string]int | ||
healthCheckInterval time.Duration | ||
} | ||
|
||
// NewHealthMonitor returns a new HealthMonitor instance. | ||
func NewHealthMonitor(logger logrus.FieldLogger, logCfg config.Logger, policy config.PluginRestartPolicy, schedulerChan chan string, sourceSupervisorChan, executorSupervisorChan chan pluginMetadata, executorsStore *store[executor.Executor], sourcesStore *store[source.Source], healthCheckInterval time.Duration) *HealthMonitor { | ||
return &HealthMonitor{ | ||
log: logger, | ||
logConfig: logCfg, | ||
policy: policy, | ||
schedulerChan: schedulerChan, | ||
sourceSupervisorChan: sourceSupervisorChan, | ||
executorSupervisorChan: executorSupervisorChan, | ||
executorsStore: executorsStore, | ||
sourcesStore: sourcesStore, | ||
pluginRestartStats: make(map[string]int), | ||
healthCheckInterval: healthCheckInterval, | ||
} | ||
} | ||
|
||
// Start starts monitor processes for sources and executors. | ||
func (m *HealthMonitor) Start(ctx context.Context) { | ||
go m.monitorSourcePluginHealth(ctx) | ||
go m.monitorExecutorPluginHealth(ctx) | ||
} | ||
|
||
func (m *HealthMonitor) monitorSourcePluginHealth(ctx context.Context) { | ||
m.log.Info("Starting source plugin supervisor...") | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case plugin := <-m.sourceSupervisorChan: | ||
m.log.Infof("Restarting source plugin %q, attempt %d/%d...", plugin.pluginKey, m.pluginRestartStats[plugin.pluginKey]+1, m.policy.Threshold) | ||
if source, ok := m.sourcesStore.EnabledPlugins.Get(plugin.pluginKey); ok && source.Cleanup != nil { | ||
m.log.Debugf("Releasing resources of source plugin %q...", plugin.pluginKey) | ||
source.Cleanup() | ||
} | ||
|
||
m.sourcesStore.EnabledPlugins.Delete(plugin.pluginKey) | ||
|
||
if ok := m.shouldRestartPlugin(plugin.pluginKey); !ok { | ||
m.log.Warnf("Plugin %q has been restarted too many times. Deactivating...", plugin.pluginKey) | ||
continue | ||
} | ||
|
||
p, err := createGRPCClient[source.Source](ctx, m.log, m.logConfig, plugin, TypeSource, m.sourceSupervisorChan, m.healthCheckInterval) | ||
if err != nil { | ||
m.log.WithError(err).Errorf("Failed to restart plugin %q.", plugin.pluginKey) | ||
continue | ||
} | ||
|
||
m.sourcesStore.EnabledPlugins.Insert(plugin.pluginKey, p) | ||
m.schedulerChan <- plugin.pluginKey | ||
} | ||
} | ||
} | ||
|
||
func (m *HealthMonitor) monitorExecutorPluginHealth(ctx context.Context) { | ||
m.log.Info("Starting executor plugin supervisor...") | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case plugin := <-m.executorSupervisorChan: | ||
m.log.Infof("Restarting executor plugin %q, attempt %d/%d...", plugin.pluginKey, m.pluginRestartStats[plugin.pluginKey]+1, m.policy.Threshold) | ||
|
||
if executor, ok := m.executorsStore.EnabledPlugins.Get(plugin.pluginKey); ok && executor.Cleanup != nil { | ||
m.log.Infof("Releasing executors of executor plugin %q...", plugin.pluginKey) | ||
executor.Cleanup() | ||
} | ||
|
||
m.executorsStore.EnabledPlugins.Delete(plugin.pluginKey) | ||
if ok := m.shouldRestartPlugin(plugin.pluginKey); !ok { | ||
m.log.Warnf("Plugin %q has been restarted too many times. Deactivating...", plugin.pluginKey) | ||
continue | ||
} | ||
|
||
p, err := createGRPCClient[executor.Executor](ctx, m.log, m.logConfig, plugin, TypeExecutor, m.executorSupervisorChan, m.healthCheckInterval) | ||
if err != nil { | ||
m.log.WithError(err).Errorf("Failed to restart plugin %q.", plugin.pluginKey) | ||
continue | ||
} | ||
|
||
m.executorsStore.EnabledPlugins.Insert(plugin.pluginKey, p) | ||
} | ||
} | ||
} | ||
|
||
func (m *HealthMonitor) shouldRestartPlugin(plugin string) bool { | ||
restarts := m.pluginRestartStats[plugin] | ||
m.pluginRestartStats[plugin]++ | ||
|
||
switch m.policy.Type.ToLower() { | ||
case config.KeepAgentRunningWhenThresholdReached.ToLower(): | ||
return restarts < m.policy.Threshold | ||
case config.RestartAgentWhenThresholdReached.ToLower(): | ||
if restarts >= m.policy.Threshold { | ||
m.log.Fatalf("Plugin %q has been restarted %d times and selected restartPolicy is %q. Exiting...", plugin, restarts, m.policy.Type) | ||
} | ||
return true | ||
default: | ||
m.log.Errorf("Unknown restart policy %q.", m.policy.Type) | ||
return false | ||
} | ||
} |
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, to better understand: It will do the heatlhcheck each 10s and if it restarts 5 times, the plugin will be disabled with this setup, am I correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exactly, however I think that we can bump the disable threshold to ~10 because for now it's for the whole life of the plugin, until the botkube pod won't be restarted. I would like to change it to but probably in the follow-up PRs.