diff --git a/etc/config-template.toml b/etc/config-template.toml index 2cadc2045ad..cb9a6fb4260 100644 --- a/etc/config-template.toml +++ b/etc/config-template.toml @@ -445,8 +445,7 @@ [coprocessor-v2] ## Path to the directory where compiled coprocessor plugins are located. ## Plugins in this directory will be automatically loaded by TiKV. -## If a coprocessor plugin file is deleted, it will be automatically unloaded. -## If the config value is not set, hot-reloading will be disabled. +## If the config value is not set, the coprocessor plugin will be disabled. # coprocessor-plugin-directory = "./coprocessors" [rocksdb] diff --git a/src/coprocessor_v2/endpoint.rs b/src/coprocessor_v2/endpoint.rs index d126e24f386..d1acc16c03f 100644 --- a/src/coprocessor_v2/endpoint.rs +++ b/src/coprocessor_v2/endpoint.rs @@ -19,28 +19,32 @@ enum CoprocessorError { /// A pool to build and run Coprocessor request handlers. #[derive(Clone)] pub struct Endpoint { - plugin_registry: Arc, + plugin_registry: Option>, } impl tikv_util::AssertSend for Endpoint {} impl Endpoint { pub fn new(copr_cfg: &Config) -> Self { - let mut plugin_registry = PluginRegistry::new(); - - // Enable hot-reloading of plugins if the user configured a directory. - if let Some(plugin_directory) = &copr_cfg.coprocessor_plugin_directory { - let r = plugin_registry.start_hot_reloading(plugin_directory); - if let Err(err) = r { - warn!("unable to start hot-reloading for coprocessor plugins."; - "coprocessor_directory" => plugin_directory.display(), - "error" => ?err); - } - } - - Self { - plugin_registry: Arc::new(plugin_registry), - } + let plugin_registry = + copr_cfg + .coprocessor_plugin_directory + .as_ref() + .map(|plugin_directory| { + let mut plugin_registry = PluginRegistry::new(); + + if let Err(err) = plugin_registry.start_hot_reloading(plugin_directory) { + warn!( + "unable to start hot-reloading for coprocessor plugins."; + "coprocessor_directory" => plugin_directory.display(), + "error" => ?err + ); + } + + Arc::new(plugin_registry) + }); + + Self { plugin_registry } } /// Handles a request to the coprocessor framework. @@ -72,15 +76,17 @@ impl Endpoint { storage: &Storage, mut req: kvrpcpb::RawCoprocessorRequest, ) -> Result { - let plugin = self + let plugin_registry = self .plugin_registry - .get_plugin(&req.copr_name) - .ok_or_else(|| { - CoprocessorError::Other(format!( - "No registered coprocessor with name '{}'", - req.copr_name - )) - })?; + .as_ref() + .ok_or_else(|| CoprocessorError::Other("Coprocessor plugin is disabled!".to_owned()))?; + + let plugin = plugin_registry.get_plugin(&req.copr_name).ok_or_else(|| { + CoprocessorError::Other(format!( + "No registered coprocessor with name '{}'", + req.copr_name + )) + })?; // Check whether the found plugin satisfies the version constraint. let version_req = VersionReq::parse(&req.copr_version_req)