risingwave_frontend/
lib.rs

1// Copyright 2025 RisingWave Labs
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
15#![allow(clippy::derive_partial_eq_without_eq)]
16#![feature(map_try_insert)]
17#![feature(negative_impls)]
18#![feature(coroutines)]
19#![feature(proc_macro_hygiene, stmt_expr_attributes)]
20#![feature(trait_alias)]
21#![feature(if_let_guard)]
22#![feature(assert_matches)]
23#![feature(box_patterns)]
24#![feature(macro_metavar_expr)]
25#![feature(min_specialization)]
26#![feature(extend_one)]
27#![feature(type_alias_impl_trait)]
28#![feature(impl_trait_in_assoc_type)]
29#![feature(error_generic_member_access)]
30#![feature(iterator_try_collect)]
31#![feature(used_with_arg)]
32#![feature(try_trait_v2)]
33#![feature(never_type)]
34#![recursion_limit = "256"]
35
36#[cfg(test)]
37risingwave_expr_impl::enable!();
38#[cfg(test)]
39risingwave_batch_executors::enable!();
40
41#[macro_use]
42mod catalog;
43
44use std::collections::HashSet;
45use std::time::Duration;
46
47pub use catalog::TableCatalog;
48mod binder;
49pub use binder::{Binder, bind_data_type};
50pub mod expr;
51pub mod handler;
52pub use handler::PgResponseStream;
53mod observer;
54pub mod optimizer;
55pub use optimizer::{Explain, OptimizerContext, OptimizerContextRef, PlanRef};
56mod planner;
57use pgwire::net::TcpKeepalive;
58pub use planner::Planner;
59mod scheduler;
60pub mod session;
61mod stream_fragmenter;
62use risingwave_common::config::{MetricLevel, OverrideConfig};
63use risingwave_common::util::meta_addr::MetaAddressStrategy;
64use risingwave_common::util::resource_util::memory::system_memory_available_bytes;
65use risingwave_common::util::tokio_util::sync::CancellationToken;
66pub use stream_fragmenter::build_graph;
67mod utils;
68pub use utils::{WithOptions, WithOptionsSecResolved, explain_stream_graph};
69pub(crate) mod error;
70mod meta_client;
71pub mod metrics_reader;
72pub use metrics_reader::MetricsReaderImpl;
73pub mod test_utils;
74mod user;
75pub mod webhook;
76
77pub mod health_service;
78mod monitor;
79
80pub mod rpc;
81mod telemetry;
82
83use std::ffi::OsString;
84use std::iter;
85use std::sync::Arc;
86
87use clap::Parser;
88use pgwire::pg_server::pg_serve;
89use session::SessionManagerImpl;
90
91/// Command-line arguments for frontend-node.
92#[derive(Parser, Clone, Debug, OverrideConfig)]
93#[command(
94    version,
95    about = "The stateless proxy that parses SQL queries and performs planning and optimizations of query jobs"
96)]
97pub struct FrontendOpts {
98    // TODO: rename to listen_addr and separate out the port.
99    /// The address that this service listens to.
100    /// Usually the localhost + desired port.
101    #[clap(long, env = "RW_LISTEN_ADDR", default_value = "0.0.0.0:4566")]
102    pub listen_addr: String,
103
104    /// The amount of time with no network activity after which the server will send a
105    /// TCP keepalive message to the client.
106    #[clap(long, env = "RW_TCP_KEEPALIVE_IDLE_SECS", default_value = "300")]
107    pub tcp_keepalive_idle_secs: usize,
108
109    /// The address for contacting this instance of the service.
110    /// This would be synonymous with the service's "public address"
111    /// or "identifying address".
112    /// Optional, we will use `listen_addr` if not specified.
113    #[clap(long, env = "RW_ADVERTISE_ADDR")]
114    pub advertise_addr: Option<String>,
115
116    /// The address via which we will attempt to connect to a leader meta node.
117    #[clap(long, env = "RW_META_ADDR", default_value = "http://127.0.0.1:5690")]
118    pub meta_addr: MetaAddressStrategy,
119
120    /// We will start a http server at this address via `MetricsManager`.
121    /// Then the prometheus instance will poll the metrics from this address.
122    #[clap(
123        long,
124        env = "RW_PROMETHEUS_LISTENER_ADDR",
125        default_value = "127.0.0.1:2222"
126    )]
127    pub prometheus_listener_addr: String,
128
129    #[clap(
130        long,
131        alias = "health-check-listener-addr",
132        env = "RW_HEALTH_CHECK_LISTENER_ADDR",
133        default_value = "0.0.0.0:6786"
134    )]
135    pub frontend_rpc_listener_addr: String,
136
137    /// The path of `risingwave.toml` configuration file.
138    ///
139    /// If empty, default configuration values will be used.
140    ///
141    /// Note that internal system parameters should be defined in the configuration file at
142    /// [`risingwave_common::config`] instead of command line arguments.
143    #[clap(long, env = "RW_CONFIG_PATH", default_value = "")]
144    pub config_path: String,
145
146    /// Used for control the metrics level, similar to log level.
147    ///
148    /// level = 0: disable metrics
149    /// level > 0: enable metrics
150    #[clap(long, hide = true, env = "RW_METRICS_LEVEL")]
151    #[override_opts(path = server.metrics_level)]
152    pub metrics_level: Option<MetricLevel>,
153
154    /// Enable heap profile dump when memory usage is high.
155    #[clap(long, hide = true, env = "RW_HEAP_PROFILING_DIR")]
156    #[override_opts(path = server.heap_profiling.dir)]
157    pub heap_profiling_dir: Option<String>,
158
159    #[clap(long, hide = true, env = "ENABLE_BARRIER_READ")]
160    #[override_opts(path = batch.enable_barrier_read)]
161    pub enable_barrier_read: Option<bool>,
162
163    /// The path of the temp secret file directory.
164    #[clap(
165        long,
166        hide = true,
167        env = "RW_TEMP_SECRET_FILE_DIR",
168        default_value = "./secrets"
169    )]
170    pub temp_secret_file_dir: String,
171
172    /// Total available memory for the frontend node in bytes. Used for batch computing.
173    #[clap(long, env = "RW_FRONTEND_TOTAL_MEMORY_BYTES", default_value_t = default_frontend_total_memory_bytes())]
174    pub frontend_total_memory_bytes: usize,
175
176    /// The address that the webhook service listens to.
177    /// Usually the localhost + desired port.
178    #[clap(long, env = "RW_WEBHOOK_LISTEN_ADDR", default_value = "0.0.0.0:4560")]
179    pub webhook_listen_addr: String,
180
181    /// Address of the serverless backfill controller.
182    /// Needed if frontend receives a query like
183    /// CREATE MATERIALIZED VIEW ... WITH ( `cloud.serverless_backfill_enabled=true` )
184    /// Feature disabled by default.
185    #[clap(long, env = "RW_SBC_ADDR", default_value = "")]
186    pub serverless_backfill_controller_addr: String,
187
188    /// Prometheus endpoint URL for querying metrics.
189    /// Optional, used for querying Prometheus metrics from the frontend.
190    #[clap(long, env = "RW_PROMETHEUS_ENDPOINT")]
191    pub prometheus_endpoint: Option<String>,
192
193    /// The additional selector used when querying Prometheus.
194    ///
195    /// The format is same as `PromQL`. Example: `instance="foo",namespace="bar"`
196    #[clap(long, env = "RW_PROMETHEUS_SELECTOR")]
197    pub prometheus_selector: Option<String>,
198}
199
200impl risingwave_common::opts::Opts for FrontendOpts {
201    fn name() -> &'static str {
202        "frontend"
203    }
204
205    fn meta_addr(&self) -> MetaAddressStrategy {
206        self.meta_addr.clone()
207    }
208}
209
210impl Default for FrontendOpts {
211    fn default() -> Self {
212        FrontendOpts::parse_from(iter::empty::<OsString>())
213    }
214}
215
216use std::future::Future;
217use std::pin::Pin;
218
219use pgwire::memory_manager::MessageMemoryManager;
220use pgwire::pg_protocol::{ConnectionContext, TlsConfig};
221
222use crate::session::SESSION_MANAGER;
223
224/// Start frontend
225pub fn start(
226    opts: FrontendOpts,
227    shutdown: CancellationToken,
228) -> Pin<Box<dyn Future<Output = ()> + Send>> {
229    // WARNING: don't change the function signature. Making it `async fn` will cause
230    // slow compile in release mode.
231    Box::pin(async move {
232        let listen_addr = opts.listen_addr.clone();
233        let webhook_listen_addr = opts.webhook_listen_addr.parse().unwrap();
234        let tcp_keepalive =
235            TcpKeepalive::new().with_time(Duration::from_secs(opts.tcp_keepalive_idle_secs as _));
236
237        let session_mgr = Arc::new(SessionManagerImpl::new(opts).await.unwrap());
238        SESSION_MANAGER.get_or_init(|| session_mgr.clone());
239        let redact_sql_option_keywords = Arc::new(
240            session_mgr
241                .env()
242                .batch_config()
243                .redact_sql_option_keywords
244                .iter()
245                .map(|s| s.to_lowercase())
246                .collect::<HashSet<_>>(),
247        );
248        let frontend_config = &session_mgr.env().frontend_config();
249        let message_memory_manager = Arc::new(MessageMemoryManager::new(
250            frontend_config.max_total_query_size_bytes,
251            frontend_config.min_single_query_size_bytes,
252            frontend_config.max_single_query_size_bytes,
253        ));
254
255        let webhook_service = crate::webhook::WebhookService::new(webhook_listen_addr);
256        let _task = tokio::spawn(webhook_service.serve());
257        pg_serve(
258            &listen_addr,
259            tcp_keepalive,
260            session_mgr.clone(),
261            ConnectionContext {
262                tls_config: TlsConfig::new_default(),
263                redact_sql_option_keywords: Some(redact_sql_option_keywords),
264                message_memory_manager,
265            },
266            shutdown,
267        )
268        .await
269        .unwrap()
270    })
271}
272
273pub fn default_frontend_total_memory_bytes() -> usize {
274    system_memory_available_bytes()
275}