Keep plugins persistently running in the background (#12064)

# Description
This PR uses the new plugin protocol to intelligently keep plugin
processes running in the background for further plugin calls.

Running plugins can be seen by running the new `plugin list` command,
and stopped by running the new `plugin stop` command.

This is an enhancement for the performance of plugins, as starting new
plugin processes has overhead, especially for plugins in languages that
take a significant amount of time on startup. It also enables plugins
that have persistent state between commands, making the migration of
features like dataframes and `stor` to plugins possible.

Plugins are automatically stopped by the new plugin garbage collector,
configurable with `$env.config.plugin_gc`:

```nushell
  $env.config.plugin_gc = {
      # Configuration for plugin garbage collection
      default: {
          enabled: true # true to enable stopping of inactive plugins
          stop_after: 10sec # how long to wait after a plugin is inactive to stop it
      }
      plugins: {
          # alternate configuration for specific plugins, by name, for example:
          #
          # gstat: {
          #     enabled: false
          # }
      }
  }
```

If garbage collection is enabled, plugins will be stopped after
`stop_after` passes after they were last active. Plugins are counted as
inactive if they have no running plugin calls. Reading the stream from
the response of a plugin call is still considered to be activity, but if
a plugin holds on to a stream but the call ends without an active
streaming response, it is not counted as active even if it is reading
it. Plugins can explicitly disable the GC as appropriate with
`engine.set_gc_disabled(true)`.

The `version` command now lists plugin names rather than plugin
commands. The list of plugin commands is accessible via `plugin list`.

Recommend doing this together with #12029, because it will likely force
plugin developers to do the right thing with mutability and lead to less
unexpected behavior when running plugins nested / in parallel.

# User-Facing Changes
- new command: `plugin list`
- new command: `plugin stop`
- changed command: `version` (now lists plugin names, rather than
commands)
- new config: `$env.config.plugin_gc`
- Plugins will keep running and be reused, at least for the configured
GC period
- Plugins that used mutable state in weird ways like `inc` did might
misbehave until fixed
- Plugins can disable GC if they need to
- Had to change plugin signature to accept `&EngineInterface` so that
the GC disable feature works. #12029 does this anyway, and I'm expecting
(resolvable) conflicts with that

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

Because there is some specific OS behavior required for plugins to not
respond to Ctrl-C directly, I've developed against and tested on both
Linux and Windows to ensure that works properly.

# After Submitting
I think this probably needs to be in the book somewhere
This commit is contained in:
Devyn Cairns
2024-03-09 15:10:22 -08:00
committed by GitHub
parent 430fb1fcb6
commit bc19be25b1
44 changed files with 2131 additions and 304 deletions

View File

@ -13,8 +13,8 @@ use nu_protocol::{
},
engine::{StateWorkingSet, DEFAULT_OVERLAY_NAME},
eval_const::eval_constant,
span, Alias, BlockId, DeclId, Exportable, Module, ModuleId, ParseError, PositionalArg,
ResolvedImportPattern, Span, Spanned, SyntaxShape, Type, Value, VarId,
span, Alias, BlockId, DeclId, Exportable, IntoSpanned, Module, ModuleId, ParseError,
PositionalArg, ResolvedImportPattern, Span, Spanned, SyntaxShape, Type, Value, VarId,
};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
@ -3543,8 +3543,10 @@ pub fn parse_where(working_set: &mut StateWorkingSet, spans: &[Span]) -> Pipelin
#[cfg(feature = "plugin")]
pub fn parse_register(working_set: &mut StateWorkingSet, spans: &[Span]) -> Pipeline {
use nu_plugin::{get_signature, PluginDeclaration};
use nu_protocol::{engine::Stack, PluginSignature};
use std::sync::Arc;
use nu_plugin::{get_signature, PersistentPlugin, PluginDeclaration};
use nu_protocol::{engine::Stack, PluginIdentity, PluginSignature, RegisteredPlugin};
let cwd = working_set.get_cwd();
@ -3671,35 +3673,61 @@ pub fn parse_register(working_set: &mut StateWorkingSet, spans: &[Span]) -> Pipe
// We need the current environment variables for `python` based plugins
// Or we'll likely have a problem when a plugin is implemented in a virtual Python environment.
let stack = Stack::new();
let current_envs =
nu_engine::env::env_to_strings(working_set.permanent_state, &stack).unwrap_or_default();
let get_envs = || {
let stack = Stack::new();
nu_engine::env::env_to_strings(working_set.permanent_state, &stack)
};
let error = arguments.and_then(|(path, path_span)| {
let path = path.path_buf();
// restrict plugin file name starts with `nu_plugin_`
let valid_plugin_name = path
.file_name()
.map(|s| s.to_string_lossy().starts_with("nu_plugin_"));
let Some(true) = valid_plugin_name else {
return Err(ParseError::LabeledError(
"Register plugin failed".into(),
"plugin name must start with nu_plugin_".into(),
path_span,
));
};
// Create the plugin identity. This validates that the plugin name starts with `nu_plugin_`
let identity =
PluginIdentity::new(path, shell).map_err(|err| err.into_spanned(path_span))?;
// Find garbage collection config
let gc_config = working_set
.get_config()
.plugin_gc
.get(identity.name())
.clone();
// Add it to the working set
let plugin = working_set.find_or_create_plugin(&identity, || {
Arc::new(PersistentPlugin::new(identity.clone(), gc_config))
});
// Downcast the plugin to `PersistentPlugin` - we generally expect this to succeed. The
// trait object only exists so that nu-protocol can contain plugins without knowing anything
// about their implementation, but we only use `PersistentPlugin` in practice.
let plugin: Arc<PersistentPlugin> = plugin.as_any().downcast().map_err(|_| {
ParseError::InternalError(
"encountered unexpected RegisteredPlugin type".into(),
spans[0],
)
})?;
let signatures = signature.map_or_else(
|| {
let signatures =
get_signature(&path, shell.as_deref(), &current_envs).map_err(|err| {
ParseError::LabeledError(
"Error getting signatures".into(),
err.to_string(),
spans[0],
)
});
// It's important that the plugin is restarted if we're going to get signatures
//
// The user would expect that `register` would always run the binary to get new
// signatures, in case it was replaced with an updated binary
plugin.stop().map_err(|err| {
ParseError::LabeledError(
"Failed to restart plugin to get new signatures".into(),
err.to_string(),
spans[0],
)
})?;
let signatures = get_signature(plugin.clone(), get_envs).map_err(|err| {
ParseError::LabeledError(
"Error getting signatures".into(),
err.to_string(),
spans[0],
)
});
if signatures.is_ok() {
// mark plugins file as dirty only when the user is registering plugins
@ -3715,7 +3743,7 @@ pub fn parse_register(working_set: &mut StateWorkingSet, spans: &[Span]) -> Pipe
for signature in signatures {
// create plugin command declaration (need struct impl Command)
// store declaration in working set
let plugin_decl = PluginDeclaration::new(path.clone(), signature, shell.clone());
let plugin_decl = PluginDeclaration::new(&plugin, signature);
working_set.add_decl(Box::new(plugin_decl));
}