-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathconfig.rs
More file actions
1200 lines (984 loc) · 35.3 KB
/
Copy pathconfig.rs
File metadata and controls
1200 lines (984 loc) · 35.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use anyhow::{anyhow, bail};
use arc_swap::ArcSwapOption;
use figment::Figment;
use figment::providers::{Env, Format, Json, Toml, Yaml};
use k8s_openapi::api::core::v1::{
EnvVar, LocalObjectReference, ResourceRequirements, Toleration, Volume, VolumeMount,
};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::OwnerReference;
use log::warn;
use regex::Regex;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::fmt::{Debug, Formatter};
use std::fs;
use std::net::IpAddr;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::process::exit;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
use url::Url;
const DEFAULT_CONFIG: &str = include_str!("../default.toml");
const SENSITIVE_MASK: &str = "*********";
static CONFIG: ArcSwapOption<Config> = ArcSwapOption::const_empty();
pub fn initialize_config(path: Option<&Path>, dir: Option<&Path>) {
let mut paths = vec![];
if let Some(path) = path {
if !path.exists() {
eprintln!(
"Cannot load configuration from {}; file does not exist",
path.to_string_lossy()
);
exit(1);
}
paths.push(path.to_path_buf());
}
// find config files in directory
if let Some(dir) = dir {
if let Ok(rd) = fs::read_dir(dir) {
paths.extend(rd.filter_map(|f| Some(f.ok()?.path())).filter(|p| {
p.extension()
.map(|ext| {
&*ext.to_string_lossy() == "yaml" || &*ext.to_string_lossy() == "toml"
})
.unwrap_or(false)
}));
} else {
warn!("Invalid configuration directory '{}", dir.to_string_lossy());
}
}
let current = CONFIG.load();
let mut config: Config = match load_config(&paths).extract() {
Ok(config) => config,
Err(errors) => {
eprintln!("Configuration is invalid!");
for err in errors {
eprintln!(" • {err}");
}
exit(1);
}
};
config.config_path = path.map(|p| p.to_path_buf());
config.config_dir = dir.map(|p| p.to_path_buf());
if current.is_none()
&& CONFIG
.compare_and_swap(current, Some(Arc::new(config)))
.is_none()
{
return;
}
panic!("Unable to initialize configuration; it's already initialized!");
}
pub fn update<F: Fn(&mut Config)>(f: F) {
CONFIG.rcu(|c| {
let mut new = (**c
.as_ref()
.expect("tried to update config; but not yet loaded!"))
.clone();
f(&mut new);
Some(Arc::new(new))
});
}
pub fn config() -> Arc<Config> {
let cur = CONFIG.load();
if cur.is_none() {
warn!("Config accessed before initialization! This should only happen in tests.");
CONFIG.compare_and_swap(cur, Some(load_config(&[]).extract().unwrap()));
} else {
drop(cur);
}
CONFIG.load_full().unwrap()
}
fn add_legacy_str(config: Figment, old: &str, new: &str) -> Figment {
if let Ok(v) = std::env::var(old) {
warn!(
"Using deprecated config option '{old}' -- will be removed in 0.12; \
see the config docs to migrate https://doc.arroyo.dev/config"
);
config.merge((new, v))
} else {
config
}
}
fn add_legacy_int(config: Figment, old: &str, new: &str) -> Figment {
if let Ok(v) = std::env::var(old) {
warn!(
"Using deprecated config option '{old}' -- will be removed in 0.12; \
see the config docs to migrate https://doc.arroyo.dev/config"
);
match v.parse::<u16>() {
Ok(v) => {
return config.merge((new, v));
}
Err(_) => {
warn!("Invalid config for {old} -- expected a number");
}
}
}
config
}
fn load_config(paths: &[PathBuf]) -> Figment {
// Priority (from highest--overriding--to lowest--overridden) is:
// 1. ARROYO__* environment variables
// 2. The config file specified in <path>
// 3. Any *.toml or *.yaml files specified in <dir>
// 4. arroyo.toml in the current directory
// 5. $(user conf dir)/arroyo/config.{toml,yaml}
// 6. ../default.toml
let mut figment = Figment::from(Toml::string(DEFAULT_CONFIG));
// support a few legacy configs with warnings -- to be removed in 0.12.
figment = add_legacy_str(figment, "CHECKPOINT_URL", "checkpoint-url");
figment = add_legacy_str(figment, "ARTIFACT_URL", "compiler.artifact-url");
figment = add_legacy_str(figment, "SCHEDULER", "controller.scheduler");
figment = add_legacy_str(figment, "DATABASE_HOST", "database.postgres.host");
figment = add_legacy_int(figment, "DATABASE_PORT", "database.postgres.port");
figment = add_legacy_str(figment, "DATABASE_USER", "database.postgres.user");
figment = add_legacy_str(figment, "DATABASE_PASSWORD", "database.postgres.password");
figment = add_legacy_str(figment, "DATABASE_NAME", "database.postgres.database-name");
figment = add_legacy_str(figment, "CONTROLLER_ADDR", "controller-endpoint");
figment = add_legacy_str(figment, "COMPILER_ADDR", "compiler-endpoint");
if let Some(config_dir) = dirs::config_dir() {
figment = figment
.admerge(Yaml::file(config_dir.join("arroyo/config.yaml")))
.admerge(Toml::file(config_dir.join("arroyo/config.toml")));
}
figment = figment
.admerge(Yaml::file("arroyo.yaml"))
.admerge(Toml::file("arroyo.toml"));
for path in paths {
match path.extension().and_then(OsStr::to_str) {
Some("yaml") => {
figment = figment.admerge(Yaml::file(path));
}
Some("json") => {
figment = figment.admerge(Json::file(path));
}
_ => {
figment = figment.admerge(Toml::file(path));
}
}
}
figment.admerge(
Env::prefixed("ARROYO__").map(|p| p.as_str().replace("__", ".").replace("_", "-").into()),
)
}
/// Arroyo configuration
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct Config {
/// gRPC client configuration
#[serde(default)]
pub grpc: GrpcConfig,
/// API service configuration
pub api: ApiConfig,
/// Controller service configuration
pub controller: ControllerConfig,
/// Compiler service configuration
pub compiler: CompilerConfig,
/// Node service configuration
pub node: NodeConfig,
/// Worker configuration
pub worker: WorkerConfig,
/// Admin service configuration
pub admin: AdminConfig,
/// Global TLS configuration
#[serde(default)]
pub tls: TlsConfig,
/// Default pipeline configuration
pub pipeline: PipelineConfig,
/// Database configuration
pub database: DatabaseConfig,
/// Process scheduler configuration
pub process_scheduler: ProcessSchedulerConfig,
/// Manual scheduler configuration
pub manual_scheduler: ManualSchedulerConfig,
// Kubernetes scheduler configuration
pub kubernetes_scheduler: KubernetesSchedulerConfig,
// Logging config
pub logging: LogConfig,
/// URL of an object store or filesystem for storing checkpoints
pub checkpoint_url: String,
/// Default interval for checkpointing
pub default_checkpoint_interval: HumanReadableDuration,
/// The endpoint of the controller, used by other services to connect to it. This must be set
/// if running the controller on a separate machine from the other services or on a separate
/// process with a non-standard port.
pub controller_endpoint: Option<Url>,
// The endpoint of the API service, used by the Web UI
pub api_endpoint: Option<Url>,
/// The endpoint of the compiler, used by the API server to connect to it. This must be set
/// if running the compiler on a separate machine from the other services or on a separate
/// process with a non-standard port.
compiler_endpoint: Option<Url>,
/// Hostname for this node; if set, this will be used for connections made to this node,
/// otherwise we will attempt to determine the local ip address. Setting this will generally
/// be useful for TLS.
pub hostname: Option<String>,
/// Path to the config file
pub config_path: Option<PathBuf>,
/// Directory to look for config files in
pub config_dir: Option<PathBuf>,
/// Controls where the "job controller" lives, either on the controller or a worker-leader
pub job_controller: JobControllerMode,
/// Run options
#[serde(default)]
pub run: RunConfig,
/// Telemetry config
#[serde(default)]
pub disable_telemetry: bool,
}
#[derive(Debug, Default, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct GrpcConfig {
/// Maximum time to establish a gRPC connection
pub connect_timeout: Option<HumanReadableDuration>,
}
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub enum JobControllerMode {
#[default]
Controller,
Worker,
}
impl Config {
pub fn hostname(&self) -> &str {
self.hostname.as_deref().unwrap_or("localhost")
}
pub fn controller_endpoint(&self) -> String {
self.controller_endpoint
.as_ref()
.map(|t| t.to_string())
.unwrap_or_else(|| format!("http://{}:{}", self.hostname(), self.controller.rpc_port))
}
pub fn compiler_endpoint(&self) -> String {
self.compiler_endpoint
.as_ref()
.map(|t| t.to_string())
.unwrap_or_else(|| format!("http://{}:{}", self.hostname(), self.compiler.rpc_port))
}
/// Get effective TLS configuration for a service, falling back to global config
pub fn get_tls_config<'a>(
&'a self,
service_tls: &'a Option<TlsConfig>,
) -> Option<&'a TlsConfig> {
if !self.is_tls_enabled(service_tls) {
return None;
};
service_tls.as_ref().or(Some(&self.tls))
}
/// Check if TLS is enabled for a service
pub fn is_tls_enabled(&self, service_tls: &Option<TlsConfig>) -> bool {
service_tls
.as_ref()
.map(|tls| tls.enabled)
.unwrap_or(self.tls.enabled)
}
}
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
#[serde(rename_all = "kebab-case", deny_unknown_fields, tag = "type")]
pub enum ApiAuthMode {
#[default]
None,
Mtls {
#[serde(rename = "ca-cert-file")]
ca_cert_file: Option<PathBuf>,
},
StaticApiKey {
#[serde(rename = "api-key")]
api_key: Sensitive<String>,
},
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ApiConfig {
/// The host the API service should bind to
pub bind_address: IpAddr,
/// The HTTP port for the API service
pub http_port: u16,
/// The HTTP port for the API service in run mode; defaults to a random port
pub run_http_port: Option<u16>,
/// TLS configuration for API service
#[serde(default)]
pub tls: Option<TlsConfig>,
#[serde(default)]
pub auth_mode: ApiAuthMode,
#[serde(default)]
pub cors: CorsConfig,
}
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct CorsConfig {
#[serde(default)]
pub origin_policy: CorsOriginPolicy,
#[serde(default)]
pub allowed_origins: Vec<String>,
#[serde(default)]
pub allow_credentials: bool,
}
#[derive(Debug, Deserialize, Serialize, Copy, Clone, Default, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum CorsOriginPolicy {
#[default]
Any,
AllowList,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ControllerConfig {
/// The host the controller should bind to
pub bind_address: IpAddr,
/// The RPC port for the controller
pub rpc_port: u16,
/// The scheduler to use
pub scheduler: Scheduler,
/// TLS configuration for controller gRPC service
#[serde(default)]
pub tls: Option<TlsConfig>,
/// Poll interval for leader status
pub leader_poll_interval: HumanReadableDuration,
/// Metric system configurations
pub metrics: MetricsConfig,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct CompactionConfig {
/// Whether to enable compaction for checkpoints
pub enabled: bool,
/// The number of outstanding checkpoints that will trigger compaction
pub checkpoints_to_compact: u32,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct CompilerConfig {
/// Bind address for the compiler
pub bind_address: IpAddr,
/// RPC port for the compiler
pub rpc_port: u16,
/// Whether the compiler should attempt to install clang if it's not already installed
pub install_clang: bool,
/// Whether the compiler should attempt to install rustc if it's not already installed
pub install_rustc: bool,
/// Where to store compilation artifacts
pub artifact_url: String,
/// Directory to build artifacts in
pub build_dir: String,
/// Whether to use a local version of the UDF library or the published crate (only
/// enable in development environments)
#[serde(default)]
pub use_local_udf_crate: bool,
/// TLS configuration for compiler gRPC service
#[serde(default)]
pub tls: Option<TlsConfig>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct WorkerConfig {
/// Bind address for the worker RPC socket
pub bind_address: IpAddr,
/// RPC port for the worker to listen on; set to 0 to use a random available port
pub rpc_port: u16,
/// Data port for the worker to listen on; set to 0 to use a random available port
pub data_port: u16,
/// Number of task slots for this worker
pub task_slots: u32,
/// ID for this worker
#[serde(default)]
pub id: Option<u64>,
/// ID for the machine this worker is running on
#[serde(default)]
pub machine_id: Option<String>,
/// Name to identify this worker (e.g., e.g., its hostname or a pod name)
pub name: Option<String>,
/// Size of the queues between nodes in the dataflow graph
pub queue_size: u32,
/// TLS configuration for worker TCP shuffling
#[serde(default)]
pub tls: Option<TlsConfig>,
/// Maximum number of checkpoints to keep in history for serving the checkpoint details APIs
pub checkpoint_details_to_keep: u32,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct NodeConfig {
/// ID for this node
pub id: Option<String>,
/// Bind address for the node service
pub bind_address: IpAddr,
/// RPC port for the node service
pub rpc_port: u16,
/// Number of task slots for this node
pub task_slots: u32,
/// TLS configuration for Node gRPC
#[serde(default)]
pub tls: Option<TlsConfig>,
}
impl NodeConfig {
pub fn id(&self) -> u64 {
todo!()
}
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct AdminConfig {
/// Bind address for the admin service
pub bind_address: IpAddr,
/// HTTP port the admin service will listen on
pub http_port: u16,
/// TLS configuration for admin service
#[serde(default)]
pub tls: Option<TlsConfig>,
#[serde(default)]
pub auth_mode: ApiAuthMode,
#[serde(default)]
pub allow_unauthenticated_metrics: bool,
}
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
#[serde(rename_all = "kebab-case")]
pub enum DefaultSink {
#[default]
Preview,
Stdout,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct PipelineConfig {
/// Batch size
pub source_batch_size: usize,
/// Batch linger time (how long to wait before flushing)
pub source_batch_linger: HumanReadableDuration,
/// How often to flush aggregates
pub update_aggregate_flush_interval: HumanReadableDuration,
/// How many restarts to allow before moving to failed (-1 for infinite)
pub allowed_restarts: i32,
/// After this amount of time, we consider the job to be healthy and reset the restarts counter
pub healthy_duration: HumanReadableDuration,
/// Number of seconds to wait for a worker heartbeat before considering it dead
pub worker_heartbeat_timeout: HumanReadableDuration,
/// Amount of time to wait for workers to start up before considering them failed
pub worker_startup_time: HumanReadableDuration,
/// Amount of time to wait for tasks to startup before considering it failed
pub task_startup_time: HumanReadableDuration,
/// Initial backoff delay for retryable state errors
pub state_initial_backoff: HumanReadableDuration,
/// Maximum backoff delay for retryable state errors
pub state_max_backoff: HumanReadableDuration,
/// Default sink, for when none is specified
#[serde(default)]
pub default_sink: DefaultSink,
/// Whether to persist deserialization errors to job_log_messages
pub store_deserialization_errors: bool,
pub chaining: ChainingConfig,
pub compaction: CompactionConfig,
pub checkpoint: CheckpointConfig,
}
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct CheckpointConfig {
/// Checkpoint timeout
#[serde(default)]
pub timeout: Option<HumanReadableDuration>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ChainingConfig {
/// Whether to enable operator chaining
pub enabled: bool,
}
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub enum DatabaseType {
Postgres,
Sqlite,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct DatabaseConfig {
pub r#type: DatabaseType,
pub postgres: PostgresConfig,
#[serde(default)]
pub sqlite: SqliteConfig,
}
/// A validated Postgres schema name. Only allows `[a-zA-Z0-9_]`.
#[derive(Clone, Eq, PartialEq)]
pub struct SchemaName(String);
impl SchemaName {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Default for SchemaName {
fn default() -> Self {
Self("public".to_string())
}
}
impl PartialEq<&str> for SchemaName {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
impl std::fmt::Display for SchemaName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(&self.0, f)
}
}
impl std::fmt::Debug for SchemaName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.0, f)
}
}
impl Serialize for SchemaName {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.0)
}
}
impl<'de> Deserialize<'de> for SchemaName {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
if s.is_empty() {
return Err(de::Error::custom(
"database.postgres.schema must not be empty",
));
}
if !s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
return Err(de::Error::custom(format!(
"database.postgres.schema '{}' contains invalid characters; \
only alphanumerics and underscores are allowed",
s
)));
}
Ok(Self(s))
}
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct PostgresConfig {
pub database_name: String,
pub host: String,
pub port: u16,
pub user: String,
pub password: Sensitive<String>,
#[serde(default)]
pub schema: SchemaName,
/// TLS mode for Postgres connections, defaults to `NoTls` (plaintext).
#[serde(default)]
pub tls: PostgresTlsMode,
}
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
#[serde(rename_all = "kebab-case", tag = "mode")]
pub enum PostgresTlsMode {
/// Plaintext — no TLS.
#[default]
NoTls,
/// TLS encrypted, but skip server certificate verification.
SkipVerification,
/// TLS with server certificate verification against the system trust
/// store only (no additional CA).
SystemRoots,
/// TLS with server certificate verification against the system trust
/// store plus an additional CA cert at `path`.
CaCert { path: String },
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct SqliteConfig {
pub path: PathBuf,
}
impl Default for SqliteConfig {
fn default() -> Self {
Self {
path: dirs::config_dir()
.map(|p| p.join("arroyo/config.sqlite"))
.unwrap_or_else(|| PathBuf::from_str("config.sqlite").unwrap()),
}
}
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct MetricsConfig {
/// Whether to enable pipeline metric collection and storage
pub enabled: bool,
}
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub enum Scheduler {
Embedded,
Process,
Manual,
Node,
Kubernetes,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ProcessSchedulerConfig {
pub slots_per_process: u32,
/// If enabled, the workers spun up by the process scheduler will be killed when the controller
/// shuts down. Note if disabled, this may orphan workers -- this is primarily used for testing
/// state machine recovery.
pub shutdown_with_controller: bool,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ManualSchedulerConfig {
/// The number of task slots assigned to each worker process. The scheduler will print a command
/// line for `ceil(slots / slots_per_process)` workers.
pub slots_per_process: u32,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub enum ResourceMode {
/// In per-slot mode, tasks are packed onto workers up to the `task-slots` config, and for each
/// slot the amount of resources specified in `resources` is provided
PerSlot,
/// In per-pod mode, every pod has exactly `task-slots` slots, and exactly the resources in
/// `resources`, even if it is scheduled for fewer slots. This mirrors the behavior before 0.11.
PerPod,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct KubernetesSchedulerConfig {
pub namespace: String,
pub controller: Option<OwnerReference>,
pub resource_mode: ResourceMode,
pub worker: KubernetesWorkerConfig,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct KubernetesWorkerConfig {
name_prefix: String,
#[serde(default)]
name: Option<String>,
pub image: String,
pub image_pull_policy: String,
#[serde(default)]
pub image_pull_secrets: Vec<LocalObjectReference>,
pub service_account_name: String,
#[serde(default)]
pub labels: BTreeMap<String, String>,
#[serde(default)]
pub annotations: BTreeMap<String, String>,
#[serde(default)]
pub env: Vec<EnvVar>,
pub resources: ResourceRequirements,
pub task_slots: u32,
#[serde(default)]
pub volumes: Vec<Volume>,
#[serde(default)]
pub volume_mounts: Vec<VolumeMount>,
pub command: String,
pub node_selector: BTreeMap<String, String>,
pub tolerations: Vec<Toleration>,
}
impl KubernetesWorkerConfig {
pub fn name(&self) -> String {
self.name
.as_ref()
.cloned()
.unwrap_or_else(|| format!("{}-worker", self.name_prefix))
}
}
#[derive(Clone)]
pub struct HumanReadableDuration {
duration: Duration,
original: String,
}
impl From<Duration> for HumanReadableDuration {
fn from(value: Duration) -> Self {
Self {
duration: value,
original: format!("{}ns", value.as_nanos()),
}
}
}
impl Deref for HumanReadableDuration {
type Target = Duration;
fn deref(&self) -> &Self::Target {
&self.duration
}
}
impl Debug for HumanReadableDuration {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.original.fmt(f)
}
}
impl Serialize for HumanReadableDuration {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.original)
}
}
impl<'de> Deserialize<'de> for HumanReadableDuration {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let str = String::deserialize(deserializer)?;
let r = Regex::new(r"^(\d+)\s*([a-zA-Zµ]+)$").unwrap();
let captures = r
.captures(&str)
.ok_or_else(|| de::Error::custom(format!("invalid duration specification '{str}'")))?;
let mut capture = captures.iter();
capture.next();
let n: u64 = capture.next().unwrap().unwrap().as_str().parse().unwrap();
let unit = capture.next().unwrap().unwrap().as_str();
let duration = match unit {
"ns" | "nanos" => Duration::from_nanos(n),
"µs" | "micros" => Duration::from_micros(n),
"ms" | "millis" => Duration::from_millis(n),
"s" | "secs" | "seconds" => Duration::from_secs(n),
"m" | "mins" | "minutes" => Duration::from_secs(n * 60),
"h" | "hrs" | "hours" => Duration::from_secs(n * 60 * 60),
x => return Err(de::Error::custom(format!("unknown time unit '{x}'"))),
};
Ok(HumanReadableDuration {
duration,
original: str,
})
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct LogConfig {
/// Set the log format
#[serde(default)]
pub format: LogFormat,
/// Nonblocking logging may reduce tail latency at the cost of higher memory usage
#[serde(default)]
pub nonblocking: bool,
/// Set the number of lines to buffer before dropping logs or exerting backpressure on senders
/// Only valid when nonblocking is set to true
pub buffered_lines_limit: usize,
/// Set switch whether record file line number in log
#[serde(default)]
pub enable_file_line: bool,
/// Set switch whether record file name in log
#[serde(default)]
pub enable_file_name: bool,
/// Static logging fields
#[serde(default)]
pub static_fields: BTreeMap<String, String>,
}
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub enum LogFormat {
#[default]
Plaintext,
Json,
Logfmt,
}
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct RunConfig {
/// Supplies the default query for `arroyo run`; otherwise the query is read from the command
/// line or from stdin
pub query: Option<String>,
/// Sets the state directory, where state will be read from and written to
pub state_dir: Option<String>,
}
#[derive(Clone)]
pub struct Sensitive<T: Serialize + DeserializeOwned + Debug + Clone>(T);
impl<T: Serialize + DeserializeOwned + Debug + Clone> std::fmt::Debug for Sensitive<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{SENSITIVE_MASK}")
}
}
impl<'de, T: Serialize + DeserializeOwned + Debug + Clone> Deserialize<'de> for Sensitive<T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Ok(Sensitive(T::deserialize(deserializer)?))
}
}
impl<T: Serialize + DeserializeOwned + Debug + Clone> Serialize for Sensitive<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(SENSITIVE_MASK)
}
}
impl<T: Serialize + DeserializeOwned + Debug + Clone> Deref for Sensitive<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0