forked from open-telemetry/otel-arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
329 lines (291 loc) · 9.79 KB
/
main.rs
File metadata and controls
329 lines (291 loc) · 9.79 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//! Create and run a multi-core pipeline
use clap::Parser;
use otap_df_config::pipeline::PipelineConfig;
use otap_df_config::pipeline_group::{CoreAllocation, CoreRange, Quota};
use otap_df_config::{PipelineGroupId, PipelineId};
use otap_df_controller::Controller;
use otap_df_otap::OTAP_PIPELINE_FACTORY;
use std::path::PathBuf;
use sysinfo::System;
#[cfg(all(
not(windows),
feature = "jemalloc",
feature = "mimalloc",
not(any(test, doc)),
not(clippy)
))]
compile_error!(
"Features `jemalloc` and `mimalloc` are mutually exclusive. \
To build with mimalloc, use: cargo build --release --no-default-features --features mimalloc"
);
#[cfg(feature = "mimalloc")]
use mimalloc::MiMalloc;
#[cfg(all(not(windows), feature = "jemalloc", not(feature = "mimalloc")))]
use tikv_jemallocator::Jemalloc;
#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
#[cfg(all(not(windows), feature = "jemalloc", not(feature = "mimalloc")))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;
#[derive(Parser)]
#[command(
author,
version,
about,
long_about = None,
after_help = system_info()
)]
struct Args {
/// Path to the pipeline configuration file (.json, .yaml, or .yml)
#[arg(short, long)]
pipeline: PathBuf,
/// Number of cores to use (0 for default)
#[arg(long, default_value = "0", conflicts_with = "core_id_range")]
num_cores: usize,
/// Inclusive range of CPU core IDs to pin threads to (e.g. "0-3", "0..3,5", "0..=3,6-7").
#[arg(long, value_name = "START..END", value_parser = parse_core_id_allocation, conflicts_with = "num_cores")]
core_id_range: Option<CoreAllocation>,
/// Address to bind the HTTP admin server to (e.g., "127.0.0.1:8080", "0.0.0.0:8080")
#[arg(long, default_value = "127.0.0.1:8080")]
http_admin_bind: String,
}
fn parse_core_id_allocation(s: &str) -> Result<CoreAllocation, String> {
// Accept format (EBNF):
// S -> digit | CoreRange | S,",",S
// CoreRange -> digit,"..",digit | digit,"..=",digit | digit,"-",digit
// digit -> [0-9]+
Ok(CoreAllocation::CoreSet {
set: s
.split(',')
.map(|part| {
part.trim()
.parse::<usize>()
// A single ID is a range with the same start and end
.map(|n| CoreRange { start: n, end: n })
.or_else(|_| parse_core_id_range(part))
})
.collect::<Result<Vec<CoreRange>, String>>()?,
})
}
fn parse_core_id_range(s: &str) -> Result<CoreRange, String> {
// Accept formats: "a..=b", "a..b", "a-b"
let normalized = s.replace("..=", "-").replace("..", "-");
let mut parts = normalized.split('-');
let start = parts
.next()
.ok_or_else(|| "missing start of core id range".to_string())?
.trim()
.parse::<usize>()
.map_err(|_| "invalid start (expected unsigned integer)".to_string())?;
let end = parts
.next()
.ok_or_else(|| "missing end of core id range".to_string())?
.trim()
.parse::<usize>()
.map_err(|_| "invalid end (expected unsigned integer)".to_string())?;
if parts.next().is_some() {
return Err("unexpected extra data after end of range".to_string());
}
Ok(CoreRange { start, end })
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize rustls crypto provider (required for rustls 0.23+)
// We use ring as the default provider
#[cfg(feature = "experimental-tls")]
rustls::crypto::ring::default_provider()
.install_default()
.map_err(|e| format!("Failed to install rustls crypto provider: {e:?}"))?;
let args = Args::parse();
// For now, we predefine pipeline group and pipeline IDs.
// That will be replaced with a more dynamic approach in the future.
let pipeline_group_id: PipelineGroupId = "default_pipeline_group".into();
let pipeline_id: PipelineId = "default_pipeline".into();
println!("{}", system_info());
// Load pipeline configuration from file
let pipeline_cfg = PipelineConfig::from_file(
pipeline_group_id.clone(),
pipeline_id.clone(),
&args.pipeline,
)?;
// Create controller and start pipeline with multi-core support
let controller = Controller::new(&OTAP_PIPELINE_FACTORY);
// Map CLI arguments to the core allocation enum
let core_allocation = if let Some(range) = args.core_id_range {
range
} else if args.num_cores == 0 {
CoreAllocation::AllCores
} else {
CoreAllocation::CoreCount {
count: args.num_cores,
}
};
let quota = Quota { core_allocation };
// Print the requested core configuration
match "a.core_allocation {
CoreAllocation::AllCores => println!("Requested core allocation: all available cores"),
CoreAllocation::CoreCount { count } => println!("Requested core allocation: {count} cores"),
CoreAllocation::CoreSet { .. } => {
println!("Requested core allocation: {}", quota.core_allocation);
}
}
let admin_settings = otap_df_config::engine::HttpAdminSettings {
bind_address: args.http_admin_bind,
};
let result = controller.run_forever(
pipeline_group_id,
pipeline_id,
pipeline_cfg,
quota,
admin_settings,
);
match result {
Ok(_) => {
println!("Pipeline run successfully");
std::process::exit(0);
}
Err(e) => {
eprintln!("Pipeline failed to run: {e}");
std::process::exit(1);
}
}
}
fn system_info() -> String {
// Your custom logic here - this could read files, check system state, etc.
let available_cores = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1);
let build_mode = if cfg!(debug_assertions) {
"debug"
} else {
"release"
};
let memory_allocator = if cfg!(feature = "mimalloc") {
"mimalloc"
} else if cfg!(all(feature = "jemalloc", not(windows))) {
"jemalloc"
} else {
"system"
};
let mut sys = System::new_all();
sys.refresh_memory();
let total_memory_gb = sys.total_memory() as f64 / 1_073_741_824.0;
let available_memory_gb = sys.available_memory() as f64 / 1_073_741_824.0;
let debug_warning = if cfg!(debug_assertions) {
"\n\n⚠️ WARNING: This binary was compiled in debug mode.
Debug builds are NOT recommended for production, benchmarks, or performance testing.
Use 'cargo build --release' for optimal performance."
} else {
""
};
// Get available OTAP plugins
let receivers: Vec<&str> = OTAP_PIPELINE_FACTORY
.get_receiver_factory_map()
.keys()
.copied()
.collect();
let processors: Vec<&str> = OTAP_PIPELINE_FACTORY
.get_processor_factory_map()
.keys()
.copied()
.collect();
let exporters: Vec<&str> = OTAP_PIPELINE_FACTORY
.get_exporter_factory_map()
.keys()
.copied()
.collect();
let mut receivers_sorted = receivers;
let mut processors_sorted = processors;
let mut exporters_sorted = exporters;
receivers_sorted.sort();
processors_sorted.sort();
exporters_sorted.sort();
format!(
"System Information:
Available CPU cores: {}
Available memory: {:.2} GB / {:.2} GB
Build mode: {}
Memory allocator: {}
Available Plugin URNs:
Receivers: {}
Processors: {}
Exporters: {}
Configuration files can be found in the configs/ directory.{}",
available_cores,
available_memory_gb,
total_memory_gb,
build_mode,
memory_allocator,
receivers_sorted.join(", "),
processors_sorted.join(", "),
exporters_sorted.join(", "),
debug_warning
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_core_range_ok() {
assert_eq!(
parse_core_id_range("0..=4"),
Ok(CoreRange { start: 0, end: 4 })
);
assert_eq!(
parse_core_id_range("0..4"),
Ok(CoreRange { start: 0, end: 4 })
);
assert_eq!(
parse_core_id_range("0-4"),
Ok(CoreRange { start: 0, end: 4 })
);
}
#[test]
fn parse_core_allocation_ok() {
assert_eq!(
parse_core_id_allocation("0..=4,5,6-7"),
Ok(CoreAllocation::CoreSet {
set: vec![
CoreRange { start: 0, end: 4 },
CoreRange { start: 5, end: 5 },
CoreRange { start: 6, end: 7 }
]
})
);
assert_eq!(
parse_core_id_allocation("0..4"),
Ok(CoreAllocation::CoreSet {
set: vec![CoreRange { start: 0, end: 4 }]
})
);
}
#[test]
fn parse_core_range_missing_start() {
assert_eq!(
parse_core_id_range(""),
Err("invalid start (expected unsigned integer)".to_string())
);
assert_eq!(
parse_core_id_range("a..4"),
Err("invalid start (expected unsigned integer)".to_string())
);
assert_eq!(
parse_core_id_range("-1..4"),
Err("invalid start (expected unsigned integer)".to_string())
);
assert_eq!(
parse_core_id_range("1.."),
Err("invalid end (expected unsigned integer)".to_string())
);
assert_eq!(
parse_core_id_range("1..a"),
Err("invalid end (expected unsigned integer)".to_string())
);
assert_eq!(
parse_core_id_range("1..2a"),
Err("invalid end (expected unsigned integer)".to_string())
);
}
}