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