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