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_meta::cache::CacheRegistryBuilder;
20use frontend::error::{IllegalAuthConfigSnafu, Result};
21use frontend::frontend::FrontendOptions;
22use frontend::heartbeat::FrontendHeartbeatExtensions;
23use frontend::instance::Instance;
24use frontend::instance::builder::FrontendBuilder;
25use snafu::ResultExt;
26
27use crate::options::PluginOptions;
28
29/// Sets up frontend plugins before the [`FrontendBuilder`] is constructed.
30///
31/// This is where "infrastructure configurators" are registered — plugins that the builder
32/// consumes during construction (e.g., `CatalogManagerConfiguratorRef`, cache invalidators).
33///
34/// In distributed mode this is called twice:
35/// 1. First without meta config (before `create_meta_client`), for plugins needed by the meta client.
36/// 2. Second with meta config pulled from metasrv, for dynamic configurators.
37///
38/// In standalone mode it is called once with `None`.
39#[allow(unused_mut)]
40pub async fn setup_frontend_plugins_pre_build(
41    plugins: &mut Plugins,
42    _plugin_options: &[PluginOptions],
43    fe_opts: &FrontendOptions,
44    _meta_config: Option<&[PluginOptions]>,
45) -> Result<()> {
46    if let Some(user_provider) = fe_opts.user_provider.as_ref() {
47        let provider =
48            auth::user_provider_from_option(user_provider).context(IllegalAuthConfigSnafu)?;
49        let permission_checker = DefaultPermissionChecker::arc();
50
51        plugins.insert::<PermissionCheckerRef>(permission_checker);
52        plugins.insert::<UserProviderRef>(provider);
53    }
54    Ok(())
55}
56
57/// Sets up frontend plugins after the [`FrontendBuilder`] is constructed
58/// but before [`FrontendBuilder::try_build()`] and [`FrontendBuilder::with_plugin()`].
59///
60/// This is where "feature plugins" are registered — plugins that consume builder context
61/// (e.g., `KvBackendRef`, `CatalogManagerRef`) to construct themselves.
62pub async fn setup_frontend_plugins_post_build(
63    _plugins: &mut Plugins,
64    _plugin_options: &[PluginOptions],
65    _builder: &FrontendBuilder,
66) -> Result<()> {
67    Ok(())
68}
69
70/// Sets up heartbeat extensions after the frontend [`Instance`] is available.
71///
72/// Implementations may use the instance to construct extensions and register them in the
73/// [`FrontendHeartbeatExtensions`] stored in `plugins`. Registrations must be idempotent because
74/// plugin setup can be invoked more than once.
75pub async fn setup_frontend_heartbeat_extensions(
76    plugins: &mut Plugins,
77    _plugin_options: &[PluginOptions],
78    _instance: &Arc<Instance>,
79) -> Result<()> {
80    plugins.get_or_insert(FrontendHeartbeatExtensions::default);
81    Ok(())
82}
83
84pub async fn start_frontend_plugins(_instance: &Instance) -> Result<()> {
85    Ok(())
86}
87
88/// Allows frontend plugins to add cache invalidators to the layered registry.
89pub fn configure_cache_registry(_plugins: &Plugins) -> Option<CacheRegistryBuilder> {
90    None
91}
92
93pub mod context {
94    use std::sync::Arc;
95
96    use flow::FrontendClient;
97    use meta_client::MetaClientRef;
98
99    /// The context for [`catalog::kvbackend::CatalogManagerConfiguratorRef`] in standalone or
100    /// distributed.
101    pub enum CatalogManagerConfigureContext {
102        Distributed(DistributedCatalogManagerConfigureContext),
103        Standalone(StandaloneCatalogManagerConfigureContext),
104    }
105
106    pub struct DistributedCatalogManagerConfigureContext {
107        pub meta_client: MetaClientRef,
108    }
109
110    pub struct StandaloneCatalogManagerConfigureContext {
111        pub fe_client: Arc<FrontendClient>,
112    }
113}