Skip to main content

plugins/
frontend.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::sync::Arc;
16
17use auth::{DefaultPermissionChecker, PermissionCheckerRef, UserProviderRef};
18use common_base::Plugins;
19use common_base::secrets::ExposeSecret;
20use common_meta::cache::CacheRegistryBuilder;
21use frontend::error::{IllegalAuthConfigSnafu, Result};
22use frontend::frontend::FrontendOptions;
23use frontend::heartbeat::FrontendHeartbeatExtensions;
24use frontend::instance::Instance;
25use frontend::instance::builder::FrontendBuilder;
26use snafu::ResultExt;
27
28use crate::options::PluginOptions;
29
30/// Sets up frontend plugins before the [`FrontendBuilder`] is constructed.
31///
32/// This is where "infrastructure configurators" are registered — plugins that the builder
33/// consumes during construction (e.g., `CatalogManagerConfiguratorRef`, cache invalidators).
34///
35/// In distributed mode this is called twice:
36/// 1. First without meta config (before `create_meta_client`), for plugins needed by the meta client.
37/// 2. Second with meta config pulled from metasrv, for dynamic configurators.
38///
39/// In standalone mode it is called once with `None`.
40#[allow(unused_mut)]
41pub async fn setup_frontend_plugins_pre_build(
42    plugins: &mut Plugins,
43    _plugin_options: &[PluginOptions],
44    fe_opts: &FrontendOptions,
45    _meta_config: Option<&[PluginOptions]>,
46) -> Result<()> {
47    if let Some(user_provider) = fe_opts.user_provider.as_ref() {
48        let provider = auth::user_provider_from_option(user_provider.expose_secret())
49            .context(IllegalAuthConfigSnafu)?;
50        let permission_checker = DefaultPermissionChecker::arc();
51
52        plugins.insert::<PermissionCheckerRef>(permission_checker);
53        plugins.insert::<UserProviderRef>(provider);
54    }
55    Ok(())
56}
57
58/// Sets up frontend plugins after the [`FrontendBuilder`] is constructed
59/// but before [`FrontendBuilder::try_build()`] and [`FrontendBuilder::with_plugin()`].
60///
61/// This is where "feature plugins" are registered — plugins that consume builder context
62/// (e.g., `KvBackendRef`, `CatalogManagerRef`) to construct themselves.
63pub async fn setup_frontend_plugins_post_build(
64    _plugins: &mut Plugins,
65    _plugin_options: &[PluginOptions],
66    _builder: &FrontendBuilder,
67) -> Result<()> {
68    Ok(())
69}
70
71/// Sets up heartbeat extensions after the frontend [`Instance`] is available.
72///
73/// Implementations may use the instance to construct extensions and register them in the
74/// [`FrontendHeartbeatExtensions`] stored in `plugins`. Registrations must be idempotent because
75/// plugin setup can be invoked more than once.
76pub async fn setup_frontend_heartbeat_extensions(
77    plugins: &mut Plugins,
78    _plugin_options: &[PluginOptions],
79    _instance: &Arc<Instance>,
80) -> Result<()> {
81    plugins.get_or_insert(FrontendHeartbeatExtensions::default);
82    Ok(())
83}
84
85pub async fn start_frontend_plugins(_instance: &Instance) -> Result<()> {
86    Ok(())
87}
88
89/// Allows frontend plugins to add cache invalidators to the layered registry.
90pub fn configure_cache_registry(_plugins: &Plugins) -> Option<CacheRegistryBuilder> {
91    None
92}
93
94pub mod context {
95    use std::sync::Arc;
96
97    use flow::FrontendClient;
98    use meta_client::MetaClientRef;
99
100    /// The context for [`catalog::kvbackend::CatalogManagerConfiguratorRef`] in standalone or
101    /// distributed.
102    pub enum CatalogManagerConfigureContext {
103        Distributed(DistributedCatalogManagerConfigureContext),
104        Standalone(StandaloneCatalogManagerConfigureContext),
105    }
106
107    pub struct DistributedCatalogManagerConfigureContext {
108        pub meta_client: MetaClientRef,
109    }
110
111    pub struct StandaloneCatalogManagerConfigureContext {
112        pub fe_client: Arc<FrontendClient>,
113    }
114}