Line data Source code
1 : use aws_config::environment::EnvironmentVariableCredentialsProvider;
2 : use aws_config::imds::credentials::ImdsCredentialsProvider;
3 : use aws_config::meta::credentials::CredentialsProviderChain;
4 : use aws_config::meta::region::RegionProviderChain;
5 : use aws_config::profile::ProfileFileCredentialsProvider;
6 : use aws_config::provider_config::ProviderConfig;
7 : use aws_config::web_identity_token::WebIdentityTokenCredentialsProvider;
8 : use aws_config::Region;
9 : use futures::future::Either;
10 : use proxy::auth;
11 : use proxy::auth::backend::AuthRateLimiter;
12 : use proxy::auth::backend::MaybeOwned;
13 : use proxy::cancellation::CancelMap;
14 : use proxy::cancellation::CancellationHandler;
15 : use proxy::config::remote_storage_from_toml;
16 : use proxy::config::AuthenticationConfig;
17 : use proxy::config::CacheOptions;
18 : use proxy::config::HttpConfig;
19 : use proxy::config::ProjectInfoCacheOptions;
20 : use proxy::console;
21 : use proxy::context::parquet::ParquetUploadArgs;
22 : use proxy::http;
23 : use proxy::http::health_server::AppMetrics;
24 : use proxy::metrics::Metrics;
25 : use proxy::rate_limiter::EndpointRateLimiter;
26 : use proxy::rate_limiter::LeakyBucketConfig;
27 : use proxy::rate_limiter::RateBucketInfo;
28 : use proxy::rate_limiter::WakeComputeRateLimiter;
29 : use proxy::redis::cancellation_publisher::RedisPublisherClient;
30 : use proxy::redis::connection_with_credentials_provider::ConnectionWithCredentialsProvider;
31 : use proxy::redis::elasticache;
32 : use proxy::redis::notifications;
33 : use proxy::scram::threadpool::ThreadPool;
34 : use proxy::serverless::cancel_set::CancelSet;
35 : use proxy::serverless::GlobalConnPoolOptions;
36 : use proxy::usage_metrics;
37 :
38 : use anyhow::bail;
39 : use proxy::config::{self, ProxyConfig};
40 : use proxy::serverless;
41 : use remote_storage::RemoteStorageConfig;
42 : use std::net::SocketAddr;
43 : use std::pin::pin;
44 : use std::sync::Arc;
45 : use tokio::net::TcpListener;
46 : use tokio::sync::Mutex;
47 : use tokio::task::JoinSet;
48 : use tokio_util::sync::CancellationToken;
49 : use tracing::info;
50 : use tracing::warn;
51 : use tracing::Instrument;
52 : use utils::{project_build_tag, project_git_version, sentry_init::init_sentry};
53 :
54 : project_git_version!(GIT_VERSION);
55 : project_build_tag!(BUILD_TAG);
56 :
57 : use clap::{Parser, ValueEnum};
58 :
59 : #[global_allocator]
60 : static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
61 :
62 10 : #[derive(Clone, Debug, ValueEnum)]
63 : enum AuthBackendType {
64 : Console,
65 : #[cfg(feature = "testing")]
66 : Postgres,
67 : // clap only shows the name, not the alias, in usage text.
68 : // TODO: swap name/alias and deprecate "link"
69 : #[value(name("link"), alias("web"))]
70 : Web,
71 : }
72 :
73 : /// Neon proxy/router
74 2 : #[derive(Parser)]
75 : #[command(version = GIT_VERSION, about)]
76 : struct ProxyCliArgs {
77 : /// Name of the region this proxy is deployed in
78 1 : #[clap(long, default_value_t = String::new())]
79 0 : region: String,
80 : /// listen for incoming client connections on ip:port
81 : #[clap(short, long, default_value = "127.0.0.1:4432")]
82 0 : proxy: String,
83 1 : #[clap(value_enum, long, default_value_t = AuthBackendType::Web)]
84 0 : auth_backend: AuthBackendType,
85 : /// listen for management callback connection on ip:port
86 : #[clap(short, long, default_value = "127.0.0.1:7000")]
87 0 : mgmt: String,
88 : /// listen for incoming http connections (metrics, etc) on ip:port
89 : #[clap(long, default_value = "127.0.0.1:7001")]
90 0 : http: String,
91 : /// listen for incoming wss connections on ip:port
92 : #[clap(long)]
93 : wss: Option<String>,
94 : /// redirect unauthenticated users to the given uri in case of web auth
95 : #[clap(short, long, default_value = "http://localhost:3000/psql_session/")]
96 0 : uri: String,
97 : /// cloud API endpoint for authenticating users
98 : #[clap(
99 : short,
100 : long,
101 : default_value = "http://localhost:3000/authenticate_proxy_request/"
102 : )]
103 0 : auth_endpoint: String,
104 : /// path to TLS key for client postgres connections
105 : ///
106 : /// tls-key and tls-cert are for backwards compatibility, we can put all certs in one dir
107 : #[clap(short = 'k', long, alias = "ssl-key")]
108 : tls_key: Option<String>,
109 : /// path to TLS cert for client postgres connections
110 : ///
111 : /// tls-key and tls-cert are for backwards compatibility, we can put all certs in one dir
112 : #[clap(short = 'c', long, alias = "ssl-cert")]
113 : tls_cert: Option<String>,
114 : /// path to directory with TLS certificates for client postgres connections
115 : #[clap(long)]
116 : certs_dir: Option<String>,
117 : /// timeout for the TLS handshake
118 : #[clap(long, default_value = "15s", value_parser = humantime::parse_duration)]
119 0 : handshake_timeout: tokio::time::Duration,
120 : /// http endpoint to receive periodic metric updates
121 : #[clap(long)]
122 : metric_collection_endpoint: Option<String>,
123 : /// how often metrics should be sent to a collection endpoint
124 : #[clap(long)]
125 : metric_collection_interval: Option<String>,
126 : /// cache for `wake_compute` api method (use `size=0` to disable)
127 : #[clap(long, default_value = config::CacheOptions::CACHE_DEFAULT_OPTIONS)]
128 0 : wake_compute_cache: String,
129 : /// lock for `wake_compute` api method. example: "shards=32,permits=4,epoch=10m,timeout=1s". (use `permits=0` to disable).
130 : #[clap(long, default_value = config::ConcurrencyLockOptions::DEFAULT_OPTIONS_WAKE_COMPUTE_LOCK)]
131 0 : wake_compute_lock: String,
132 : /// lock for `connect_compute` api method. example: "shards=32,permits=4,epoch=10m,timeout=1s". (use `permits=0` to disable).
133 : #[clap(long, default_value = config::ConcurrencyLockOptions::DEFAULT_OPTIONS_CONNECT_COMPUTE_LOCK)]
134 0 : connect_compute_lock: String,
135 : /// Allow self-signed certificates for compute nodes (for testing)
136 1 : #[clap(long, default_value_t = false, value_parser = clap::builder::BoolishValueParser::new(), action = clap::ArgAction::Set)]
137 0 : allow_self_signed_compute: bool,
138 : #[clap(flatten)]
139 : sql_over_http: SqlOverHttpArgs,
140 : /// timeout for scram authentication protocol
141 : #[clap(long, default_value = "15s", value_parser = humantime::parse_duration)]
142 0 : scram_protocol_timeout: tokio::time::Duration,
143 : /// size of the threadpool for password hashing
144 1 : #[clap(long, default_value_t = 4)]
145 0 : scram_thread_pool_size: u8,
146 : /// Require that all incoming requests have a Proxy Protocol V2 packet **and** have an IP address associated.
147 1 : #[clap(long, default_value_t = false, value_parser = clap::builder::BoolishValueParser::new(), action = clap::ArgAction::Set)]
148 0 : require_client_ip: bool,
149 : /// Disable dynamic rate limiter and store the metrics to ensure its production behaviour.
150 1 : #[clap(long, default_value_t = true, value_parser = clap::builder::BoolishValueParser::new(), action = clap::ArgAction::Set)]
151 0 : disable_dynamic_rate_limiter: bool,
152 : /// Endpoint rate limiter max number of requests per second.
153 : ///
154 : /// Provided in the form `<Requests Per Second>@<Bucket Duration Size>`.
155 : /// Can be given multiple times for different bucket sizes.
156 5 : #[clap(long, default_values_t = RateBucketInfo::DEFAULT_ENDPOINT_SET)]
157 1 : endpoint_rps_limit: Vec<RateBucketInfo>,
158 : /// Wake compute rate limiter max number of requests per second.
159 5 : #[clap(long, default_values_t = RateBucketInfo::DEFAULT_SET)]
160 1 : wake_compute_limit: Vec<RateBucketInfo>,
161 : /// Whether the auth rate limiter actually takes effect (for testing)
162 1 : #[clap(long, default_value_t = false, value_parser = clap::builder::BoolishValueParser::new(), action = clap::ArgAction::Set)]
163 0 : auth_rate_limit_enabled: bool,
164 : /// Authentication rate limiter max number of hashes per second.
165 5 : #[clap(long, default_values_t = RateBucketInfo::DEFAULT_AUTH_SET)]
166 1 : auth_rate_limit: Vec<RateBucketInfo>,
167 : /// The IP subnet to use when considering whether two IP addresses are considered the same.
168 1 : #[clap(long, default_value_t = 64)]
169 0 : auth_rate_limit_ip_subnet: u8,
170 : /// Redis rate limiter max number of requests per second.
171 5 : #[clap(long, default_values_t = RateBucketInfo::DEFAULT_SET)]
172 1 : redis_rps_limit: Vec<RateBucketInfo>,
173 : /// cache for `allowed_ips` (use `size=0` to disable)
174 : #[clap(long, default_value = config::CacheOptions::CACHE_DEFAULT_OPTIONS)]
175 0 : allowed_ips_cache: String,
176 : /// cache for `role_secret` (use `size=0` to disable)
177 : #[clap(long, default_value = config::CacheOptions::CACHE_DEFAULT_OPTIONS)]
178 0 : role_secret_cache: String,
179 : /// redis url for notifications (if empty, redis_host:port will be used for both notifications and streaming connections)
180 : #[clap(long)]
181 : redis_notifications: Option<String>,
182 : /// what from the available authentications type to use for the regional redis we have. Supported are "irsa" and "plain".
183 : #[clap(long, default_value = "irsa")]
184 0 : redis_auth_type: String,
185 : /// redis host for streaming connections (might be different from the notifications host)
186 : #[clap(long)]
187 : redis_host: Option<String>,
188 : /// redis port for streaming connections (might be different from the notifications host)
189 : #[clap(long)]
190 : redis_port: Option<u16>,
191 : /// redis cluster name, used in aws elasticache
192 : #[clap(long)]
193 : redis_cluster_name: Option<String>,
194 : /// redis user_id, used in aws elasticache
195 : #[clap(long)]
196 : redis_user_id: Option<String>,
197 : /// aws region to retrieve credentials
198 1 : #[clap(long, default_value_t = String::new())]
199 0 : aws_region: String,
200 : /// cache for `project_info` (use `size=0` to disable)
201 : #[clap(long, default_value = config::ProjectInfoCacheOptions::CACHE_DEFAULT_OPTIONS)]
202 0 : project_info_cache: String,
203 : /// cache for all valid endpoints
204 : #[clap(long, default_value = config::EndpointCacheConfig::CACHE_DEFAULT_OPTIONS)]
205 0 : endpoint_cache_config: String,
206 : #[clap(flatten)]
207 : parquet_upload: ParquetUploadArgs,
208 :
209 : /// interval for backup metric collection
210 : #[clap(long, default_value = "10m", value_parser = humantime::parse_duration)]
211 0 : metric_backup_collection_interval: std::time::Duration,
212 : /// remote storage configuration for backup metric collection
213 : /// Encoded as toml (same format as pageservers), eg
214 : /// `{bucket_name='the-bucket',bucket_region='us-east-1',prefix_in_bucket='proxy',endpoint='http://minio:9000'}`
215 : #[clap(long, value_parser = remote_storage_from_toml)]
216 : metric_backup_collection_remote_storage: Option<RemoteStorageConfig>,
217 : /// chunk size for backup metric collection
218 : /// Size of each event is no more than 400 bytes, so 2**22 is about 200MB before the compression.
219 : #[clap(long, default_value = "4194304")]
220 0 : metric_backup_collection_chunk_size: usize,
221 : /// Whether to retry the connection to the compute node
222 : #[clap(long, default_value = config::RetryConfig::CONNECT_TO_COMPUTE_DEFAULT_VALUES)]
223 0 : connect_to_compute_retry: String,
224 : /// Whether to retry the wake_compute request
225 : #[clap(long, default_value = config::RetryConfig::WAKE_COMPUTE_DEFAULT_VALUES)]
226 0 : wake_compute_retry: String,
227 :
228 : /// Configure if this is a private access proxy for the POC: In that case the proxy will ignore the IP allowlist
229 1 : #[clap(long, default_value_t = false, value_parser = clap::builder::BoolishValueParser::new(), action = clap::ArgAction::Set)]
230 0 : is_private_access_proxy: bool,
231 : }
232 :
233 2 : #[derive(clap::Args, Clone, Copy, Debug)]
234 : struct SqlOverHttpArgs {
235 : /// timeout for http connection requests
236 : #[clap(long, default_value = "15s", value_parser = humantime::parse_duration)]
237 0 : sql_over_http_timeout: tokio::time::Duration,
238 :
239 : /// Whether the SQL over http pool is opt-in
240 1 : #[clap(long, default_value_t = true, value_parser = clap::builder::BoolishValueParser::new(), action = clap::ArgAction::Set)]
241 0 : sql_over_http_pool_opt_in: bool,
242 :
243 : /// How many connections to pool for each endpoint. Excess connections are discarded
244 1 : #[clap(long, default_value_t = 20)]
245 0 : sql_over_http_pool_max_conns_per_endpoint: usize,
246 :
247 : /// How many connections to pool for each endpoint. Excess connections are discarded
248 1 : #[clap(long, default_value_t = 20000)]
249 0 : sql_over_http_pool_max_total_conns: usize,
250 :
251 : /// How long pooled connections should remain idle for before closing
252 : #[clap(long, default_value = "5m", value_parser = humantime::parse_duration)]
253 0 : sql_over_http_idle_timeout: tokio::time::Duration,
254 :
255 : /// Duration each shard will wait on average before a GC sweep.
256 : /// A longer time will causes sweeps to take longer but will interfere less frequently.
257 : #[clap(long, default_value = "10m", value_parser = humantime::parse_duration)]
258 0 : sql_over_http_pool_gc_epoch: tokio::time::Duration,
259 :
260 : /// How many shards should the global pool have. Must be a power of two.
261 : /// More shards will introduce less contention for pool operations, but can
262 : /// increase memory used by the pool
263 1 : #[clap(long, default_value_t = 128)]
264 0 : sql_over_http_pool_shards: usize,
265 :
266 1 : #[clap(long, default_value_t = 10000)]
267 0 : sql_over_http_client_conn_threshold: u64,
268 :
269 1 : #[clap(long, default_value_t = 64)]
270 0 : sql_over_http_cancel_set_shards: usize,
271 : }
272 :
273 : #[tokio::main]
274 0 : async fn main() -> anyhow::Result<()> {
275 0 : let _logging_guard = proxy::logging::init().await?;
276 0 : let _panic_hook_guard = utils::logging::replace_panic_hook_with_tracing_panic_hook();
277 0 : let _sentry_guard = init_sentry(Some(GIT_VERSION.into()), &[]);
278 0 :
279 0 : info!("Version: {GIT_VERSION}");
280 0 : info!("Build_tag: {BUILD_TAG}");
281 0 : let neon_metrics = ::metrics::NeonMetrics::new(::metrics::BuildInfo {
282 0 : revision: GIT_VERSION,
283 0 : build_tag: BUILD_TAG,
284 0 : });
285 0 :
286 0 : let jemalloc = match proxy::jemalloc::MetricRecorder::new() {
287 0 : Ok(t) => Some(t),
288 0 : Err(e) => {
289 0 : tracing::error!(error = ?e, "could not start jemalloc metrics loop");
290 0 : None
291 0 : }
292 0 : };
293 0 :
294 0 : let args = ProxyCliArgs::parse();
295 0 : let config = build_config(&args)?;
296 0 :
297 0 : info!("Authentication backend: {}", config.auth_backend);
298 0 : info!("Using region: {}", args.aws_region);
299 0 :
300 0 : let region_provider =
301 0 : RegionProviderChain::default_provider().or_else(Region::new(args.aws_region.clone()));
302 0 : let provider_conf =
303 0 : ProviderConfig::without_region().with_region(region_provider.region().await);
304 0 : let aws_credentials_provider = {
305 0 : // uses "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY"
306 0 : CredentialsProviderChain::first_try("env", EnvironmentVariableCredentialsProvider::new())
307 0 : // uses "AWS_PROFILE" / `aws sso login --profile <profile>`
308 0 : .or_else(
309 0 : "profile-sso",
310 0 : ProfileFileCredentialsProvider::builder()
311 0 : .configure(&provider_conf)
312 0 : .build(),
313 0 : )
314 0 : // uses "AWS_WEB_IDENTITY_TOKEN_FILE", "AWS_ROLE_ARN", "AWS_ROLE_SESSION_NAME"
315 0 : // needed to access remote extensions bucket
316 0 : .or_else(
317 0 : "token",
318 0 : WebIdentityTokenCredentialsProvider::builder()
319 0 : .configure(&provider_conf)
320 0 : .build(),
321 0 : )
322 0 : // uses imds v2
323 0 : .or_else("imds", ImdsCredentialsProvider::builder().build())
324 0 : };
325 0 : let elasticache_credentials_provider = Arc::new(elasticache::CredentialsProvider::new(
326 0 : elasticache::AWSIRSAConfig::new(
327 0 : args.aws_region.clone(),
328 0 : args.redis_cluster_name,
329 0 : args.redis_user_id,
330 0 : ),
331 0 : aws_credentials_provider,
332 0 : ));
333 0 : let regional_redis_client = match (args.redis_auth_type.as_str(), &args.redis_notifications) {
334 0 : ("plain", redis_url) => match redis_url {
335 0 : None => {
336 0 : bail!("plain auth requires redis_notifications to be set");
337 0 : }
338 0 : Some(url) => Some(
339 0 : ConnectionWithCredentialsProvider::new_with_static_credentials(url.to_string()),
340 0 : ),
341 0 : },
342 0 : ("irsa", _) => match (&args.redis_host, args.redis_port) {
343 0 : (Some(host), Some(port)) => Some(
344 0 : ConnectionWithCredentialsProvider::new_with_credentials_provider(
345 0 : host.to_string(),
346 0 : port,
347 0 : elasticache_credentials_provider.clone(),
348 0 : ),
349 0 : ),
350 0 : (None, None) => {
351 0 : warn!("irsa auth requires redis-host and redis-port to be set, continuing without regional_redis_client");
352 0 : None
353 0 : }
354 0 : _ => {
355 0 : bail!("redis-host and redis-port must be specified together");
356 0 : }
357 0 : },
358 0 : _ => {
359 0 : bail!("unknown auth type given");
360 0 : }
361 0 : };
362 0 :
363 0 : let redis_notifications_client = if let Some(url) = args.redis_notifications {
364 0 : Some(ConnectionWithCredentialsProvider::new_with_static_credentials(url.to_string()))
365 0 : } else {
366 0 : regional_redis_client.clone()
367 0 : };
368 0 :
369 0 : // Check that we can bind to address before further initialization
370 0 : let http_address: SocketAddr = args.http.parse()?;
371 0 : info!("Starting http on {http_address}");
372 0 : let http_listener = TcpListener::bind(http_address).await?.into_std()?;
373 0 :
374 0 : let mgmt_address: SocketAddr = args.mgmt.parse()?;
375 0 : info!("Starting mgmt on {mgmt_address}");
376 0 : let mgmt_listener = TcpListener::bind(mgmt_address).await?;
377 0 :
378 0 : let proxy_address: SocketAddr = args.proxy.parse()?;
379 0 : info!("Starting proxy on {proxy_address}");
380 0 : let proxy_listener = TcpListener::bind(proxy_address).await?;
381 0 : let cancellation_token = CancellationToken::new();
382 0 :
383 0 : let cancel_map = CancelMap::default();
384 0 :
385 0 : let redis_rps_limit = Vec::leak(args.redis_rps_limit.clone());
386 0 : RateBucketInfo::validate(redis_rps_limit)?;
387 0 :
388 0 : let redis_publisher = match ®ional_redis_client {
389 0 : Some(redis_publisher) => Some(Arc::new(Mutex::new(RedisPublisherClient::new(
390 0 : redis_publisher.clone(),
391 0 : args.region.clone(),
392 0 : redis_rps_limit,
393 0 : )?))),
394 0 : None => None,
395 0 : };
396 0 : let cancellation_handler = Arc::new(CancellationHandler::<
397 0 : Option<Arc<tokio::sync::Mutex<RedisPublisherClient>>>,
398 0 : >::new(
399 0 : cancel_map.clone(),
400 0 : redis_publisher,
401 0 : proxy::metrics::CancellationSource::FromClient,
402 0 : ));
403 0 :
404 0 : // bit of a hack - find the min rps and max rps supported and turn it into
405 0 : // leaky bucket config instead
406 0 : let max = args
407 0 : .endpoint_rps_limit
408 0 : .iter()
409 0 : .map(|x| x.rps())
410 0 : .max_by(f64::total_cmp)
411 0 : .unwrap_or(EndpointRateLimiter::DEFAULT.max);
412 0 : let rps = args
413 0 : .endpoint_rps_limit
414 0 : .iter()
415 0 : .map(|x| x.rps())
416 0 : .min_by(f64::total_cmp)
417 0 : .unwrap_or(EndpointRateLimiter::DEFAULT.rps);
418 0 : let endpoint_rate_limiter = Arc::new(EndpointRateLimiter::new_with_shards(
419 0 : LeakyBucketConfig { rps, max },
420 0 : 64,
421 0 : ));
422 0 :
423 0 : // client facing tasks. these will exit on error or on cancellation
424 0 : // cancellation returns Ok(())
425 0 : let mut client_tasks = JoinSet::new();
426 0 : client_tasks.spawn(proxy::proxy::task_main(
427 0 : config,
428 0 : proxy_listener,
429 0 : cancellation_token.clone(),
430 0 : cancellation_handler.clone(),
431 0 : endpoint_rate_limiter.clone(),
432 0 : ));
433 0 :
434 0 : // TODO: rename the argument to something like serverless.
435 0 : // It now covers more than just websockets, it also covers SQL over HTTP.
436 0 : if let Some(serverless_address) = args.wss {
437 0 : let serverless_address: SocketAddr = serverless_address.parse()?;
438 0 : info!("Starting wss on {serverless_address}");
439 0 : let serverless_listener = TcpListener::bind(serverless_address).await?;
440 0 :
441 0 : client_tasks.spawn(serverless::task_main(
442 0 : config,
443 0 : serverless_listener,
444 0 : cancellation_token.clone(),
445 0 : cancellation_handler.clone(),
446 0 : endpoint_rate_limiter.clone(),
447 0 : ));
448 0 : }
449 0 :
450 0 : client_tasks.spawn(proxy::context::parquet::worker(
451 0 : cancellation_token.clone(),
452 0 : args.parquet_upload,
453 0 : ));
454 0 :
455 0 : // maintenance tasks. these never return unless there's an error
456 0 : let mut maintenance_tasks = JoinSet::new();
457 0 : maintenance_tasks.spawn(proxy::handle_signals(
458 0 : cancellation_token.clone(),
459 0 : || async { Ok(()) },
460 0 : ));
461 0 : maintenance_tasks.spawn(http::health_server::task_main(
462 0 : http_listener,
463 0 : AppMetrics {
464 0 : jemalloc,
465 0 : neon_metrics,
466 0 : proxy: proxy::metrics::Metrics::get(),
467 0 : },
468 0 : ));
469 0 : maintenance_tasks.spawn(console::mgmt::task_main(mgmt_listener));
470 0 :
471 0 : if let Some(metrics_config) = &config.metric_collection {
472 0 : // TODO: Add gc regardles of the metric collection being enabled.
473 0 : maintenance_tasks.spawn(usage_metrics::task_main(metrics_config));
474 0 : client_tasks.spawn(usage_metrics::task_backup(
475 0 : &metrics_config.backup_metric_collection_config,
476 0 : cancellation_token.clone(),
477 0 : ));
478 0 : }
479 0 :
480 0 : if let auth::Backend::Console(api, _) = &config.auth_backend {
481 0 : if let proxy::console::provider::ConsoleBackend::Console(api) = &**api {
482 0 : match (redis_notifications_client, regional_redis_client.clone()) {
483 0 : (None, None) => {}
484 0 : (client1, client2) => {
485 0 : let cache = api.caches.project_info.clone();
486 0 : if let Some(client) = client1 {
487 0 : maintenance_tasks.spawn(notifications::task_main(
488 0 : client,
489 0 : cache.clone(),
490 0 : cancel_map.clone(),
491 0 : args.region.clone(),
492 0 : ));
493 0 : }
494 0 : if let Some(client) = client2 {
495 0 : maintenance_tasks.spawn(notifications::task_main(
496 0 : client,
497 0 : cache.clone(),
498 0 : cancel_map.clone(),
499 0 : args.region.clone(),
500 0 : ));
501 0 : }
502 0 : maintenance_tasks.spawn(async move { cache.clone().gc_worker().await });
503 0 : }
504 0 : }
505 0 : if let Some(regional_redis_client) = regional_redis_client {
506 0 : let cache = api.caches.endpoints_cache.clone();
507 0 : let con = regional_redis_client;
508 0 : let span = tracing::info_span!("endpoints_cache");
509 0 : maintenance_tasks.spawn(
510 0 : async move { cache.do_read(con, cancellation_token.clone()).await }
511 0 : .instrument(span),
512 0 : );
513 0 : }
514 0 : }
515 0 : }
516 0 :
517 0 : let maintenance = loop {
518 0 : // get one complete task
519 0 : match futures::future::select(
520 0 : pin!(maintenance_tasks.join_next()),
521 0 : pin!(client_tasks.join_next()),
522 0 : )
523 0 : .await
524 0 : {
525 0 : // exit immediately on maintenance task completion
526 0 : Either::Left((Some(res), _)) => break proxy::flatten_err(res)?,
527 0 : // exit with error immediately if all maintenance tasks have ceased (should be caught by branch above)
528 0 : Either::Left((None, _)) => bail!("no maintenance tasks running. invalid state"),
529 0 : // exit immediately on client task error
530 0 : Either::Right((Some(res), _)) => proxy::flatten_err(res)?,
531 0 : // exit if all our client tasks have shutdown gracefully
532 0 : Either::Right((None, _)) => return Ok(()),
533 0 : }
534 0 : };
535 0 :
536 0 : // maintenance tasks return Infallible success values, this is an impossible value
537 0 : // so this match statically ensures that there are no possibilities for that value
538 0 : match maintenance {}
539 0 : }
540 :
541 : /// ProxyConfig is created at proxy startup, and lives forever.
542 0 : fn build_config(args: &ProxyCliArgs) -> anyhow::Result<&'static ProxyConfig> {
543 0 : let thread_pool = ThreadPool::new(args.scram_thread_pool_size);
544 0 : Metrics::install(thread_pool.metrics.clone());
545 :
546 0 : let tls_config = match (&args.tls_key, &args.tls_cert) {
547 0 : (Some(key_path), Some(cert_path)) => Some(config::configure_tls(
548 0 : key_path,
549 0 : cert_path,
550 0 : args.certs_dir.as_ref(),
551 0 : )?),
552 0 : (None, None) => None,
553 0 : _ => bail!("either both or neither tls-key and tls-cert must be specified"),
554 : };
555 :
556 0 : if args.allow_self_signed_compute {
557 0 : warn!("allowing self-signed compute certificates");
558 0 : }
559 0 : let backup_metric_collection_config = config::MetricBackupCollectionConfig {
560 0 : interval: args.metric_backup_collection_interval,
561 0 : remote_storage_config: args.metric_backup_collection_remote_storage.clone(),
562 0 : chunk_size: args.metric_backup_collection_chunk_size,
563 0 : };
564 :
565 0 : let metric_collection = match (
566 0 : &args.metric_collection_endpoint,
567 0 : &args.metric_collection_interval,
568 : ) {
569 0 : (Some(endpoint), Some(interval)) => Some(config::MetricCollectionConfig {
570 0 : endpoint: endpoint.parse()?,
571 0 : interval: humantime::parse_duration(interval)?,
572 0 : backup_metric_collection_config,
573 : }),
574 0 : (None, None) => None,
575 0 : _ => bail!(
576 0 : "either both or neither metric-collection-endpoint \
577 0 : and metric-collection-interval must be specified"
578 0 : ),
579 : };
580 0 : if !args.disable_dynamic_rate_limiter {
581 0 : bail!("dynamic rate limiter should be disabled");
582 0 : }
583 :
584 0 : let auth_backend = match &args.auth_backend {
585 : AuthBackendType::Console => {
586 0 : let wake_compute_cache_config: CacheOptions = args.wake_compute_cache.parse()?;
587 0 : let project_info_cache_config: ProjectInfoCacheOptions =
588 0 : args.project_info_cache.parse()?;
589 0 : let endpoint_cache_config: config::EndpointCacheConfig =
590 0 : args.endpoint_cache_config.parse()?;
591 :
592 0 : info!("Using NodeInfoCache (wake_compute) with options={wake_compute_cache_config:?}");
593 0 : info!(
594 0 : "Using AllowedIpsCache (wake_compute) with options={project_info_cache_config:?}"
595 : );
596 0 : info!("Using EndpointCacheConfig with options={endpoint_cache_config:?}");
597 0 : let caches = Box::leak(Box::new(console::caches::ApiCaches::new(
598 0 : wake_compute_cache_config,
599 0 : project_info_cache_config,
600 0 : endpoint_cache_config,
601 0 : )));
602 :
603 : let config::ConcurrencyLockOptions {
604 0 : shards,
605 0 : limiter,
606 0 : epoch,
607 0 : timeout,
608 0 : } = args.wake_compute_lock.parse()?;
609 0 : info!(?limiter, shards, ?epoch, "Using NodeLocks (wake_compute)");
610 0 : let locks = Box::leak(Box::new(console::locks::ApiLocks::new(
611 0 : "wake_compute_lock",
612 0 : limiter,
613 0 : shards,
614 0 : timeout,
615 0 : epoch,
616 0 : &Metrics::get().wake_compute_lock,
617 0 : )?));
618 0 : tokio::spawn(locks.garbage_collect_worker());
619 :
620 0 : let url = args.auth_endpoint.parse()?;
621 0 : let endpoint = http::Endpoint::new(url, http::new_client());
622 0 :
623 0 : let mut wake_compute_rps_limit = args.wake_compute_limit.clone();
624 0 : RateBucketInfo::validate(&mut wake_compute_rps_limit)?;
625 0 : let wake_compute_endpoint_rate_limiter =
626 0 : Arc::new(WakeComputeRateLimiter::new(wake_compute_rps_limit));
627 0 : let api = console::provider::neon::Api::new(
628 0 : endpoint,
629 0 : caches,
630 0 : locks,
631 0 : wake_compute_endpoint_rate_limiter,
632 0 : );
633 0 : let api = console::provider::ConsoleBackend::Console(api);
634 0 : auth::Backend::Console(MaybeOwned::Owned(api), ())
635 : }
636 : #[cfg(feature = "testing")]
637 : AuthBackendType::Postgres => {
638 0 : let url = args.auth_endpoint.parse()?;
639 0 : let api = console::provider::mock::Api::new(url);
640 0 : let api = console::provider::ConsoleBackend::Postgres(api);
641 0 : auth::Backend::Console(MaybeOwned::Owned(api), ())
642 : }
643 : AuthBackendType::Web => {
644 0 : let url = args.uri.parse()?;
645 0 : auth::Backend::Web(MaybeOwned::Owned(url), ())
646 : }
647 : };
648 :
649 : let config::ConcurrencyLockOptions {
650 0 : shards,
651 0 : limiter,
652 0 : epoch,
653 0 : timeout,
654 0 : } = args.connect_compute_lock.parse()?;
655 0 : info!(
656 : ?limiter,
657 : shards,
658 : ?epoch,
659 0 : "Using NodeLocks (connect_compute)"
660 : );
661 0 : let connect_compute_locks = console::locks::ApiLocks::new(
662 0 : "connect_compute_lock",
663 0 : limiter,
664 0 : shards,
665 0 : timeout,
666 0 : epoch,
667 0 : &Metrics::get().proxy.connect_compute_lock,
668 0 : )?;
669 :
670 0 : let http_config = HttpConfig {
671 0 : accept_websockets: true,
672 0 : pool_options: GlobalConnPoolOptions {
673 0 : max_conns_per_endpoint: args.sql_over_http.sql_over_http_pool_max_conns_per_endpoint,
674 0 : gc_epoch: args.sql_over_http.sql_over_http_pool_gc_epoch,
675 0 : pool_shards: args.sql_over_http.sql_over_http_pool_shards,
676 0 : idle_timeout: args.sql_over_http.sql_over_http_idle_timeout,
677 0 : opt_in: args.sql_over_http.sql_over_http_pool_opt_in,
678 0 : max_total_conns: args.sql_over_http.sql_over_http_pool_max_total_conns,
679 0 : },
680 0 : cancel_set: CancelSet::new(args.sql_over_http.sql_over_http_cancel_set_shards),
681 0 : client_conn_threshold: args.sql_over_http.sql_over_http_client_conn_threshold,
682 0 : };
683 0 : let authentication_config = AuthenticationConfig {
684 0 : thread_pool,
685 0 : scram_protocol_timeout: args.scram_protocol_timeout,
686 0 : rate_limiter_enabled: args.auth_rate_limit_enabled,
687 0 : rate_limiter: AuthRateLimiter::new(args.auth_rate_limit.clone()),
688 0 : rate_limit_ip_subnet: args.auth_rate_limit_ip_subnet,
689 0 : ip_allowlist_check_enabled: !args.is_private_access_proxy,
690 0 : };
691 :
692 0 : let config = Box::leak(Box::new(ProxyConfig {
693 0 : tls_config,
694 0 : auth_backend,
695 0 : metric_collection,
696 0 : allow_self_signed_compute: args.allow_self_signed_compute,
697 0 : http_config,
698 0 : authentication_config,
699 0 : require_client_ip: args.require_client_ip,
700 0 : handshake_timeout: args.handshake_timeout,
701 0 : region: args.region.clone(),
702 0 : wake_compute_retry_config: config::RetryConfig::parse(&args.wake_compute_retry)?,
703 0 : connect_compute_locks,
704 0 : connect_to_compute_retry_config: config::RetryConfig::parse(
705 0 : &args.connect_to_compute_retry,
706 0 : )?,
707 : }));
708 :
709 0 : tokio::spawn(config.connect_compute_locks.garbage_collect_worker());
710 0 :
711 0 : Ok(config)
712 0 : }
713 :
714 : #[cfg(test)]
715 : mod tests {
716 : use std::time::Duration;
717 :
718 : use clap::Parser;
719 : use proxy::rate_limiter::RateBucketInfo;
720 :
721 : #[test]
722 1 : fn parse_endpoint_rps_limit() {
723 1 : let config = super::ProxyCliArgs::parse_from([
724 1 : "proxy",
725 1 : "--endpoint-rps-limit",
726 1 : "100@1s",
727 1 : "--endpoint-rps-limit",
728 1 : "20@30s",
729 1 : ]);
730 1 :
731 1 : assert_eq!(
732 1 : config.endpoint_rps_limit,
733 1 : vec![
734 1 : RateBucketInfo::new(100, Duration::from_secs(1)),
735 1 : RateBucketInfo::new(20, Duration::from_secs(30)),
736 1 : ]
737 1 : );
738 1 : }
739 : }
|