-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathconfig.rs
More file actions
289 lines (256 loc) · 11 KB
/
Copy pathconfig.rs
File metadata and controls
289 lines (256 loc) · 11 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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use clap::{Parser, ValueEnum};
use serde::{Deserialize, Serialize};
use std::fmt;
const DEFAULT_BOOTSTRAP_SERVER: &str = "127.0.0.1:9123";
const DEFAULT_REQUEST_MAX_SIZE: i32 = 10 * 1024 * 1024;
const DEFAULT_WRITER_BATCH_SIZE: i32 = 2 * 1024 * 1024;
const DEFAULT_RETRIES: i32 = i32::MAX;
const DEFAULT_PREFETCH_NUM: usize = 4;
const DEFAULT_DOWNLOAD_THREADS: usize = 3;
const DEFAULT_SCANNER_REMOTE_LOG_READ_CONCURRENCY: usize = 4;
const DEFAULT_MAX_POLL_RECORDS: usize = 500;
const DEFAULT_WRITER_BATCH_TIMEOUT_MS: i64 = 100;
const DEFAULT_ACKS: &str = "all";
const DEFAULT_CONNECT_TIMEOUT_MS: u64 = 120_000;
const DEFAULT_REQUEST_TIMEOUT_MS: u64 = 30_000;
const DEFAULT_SECURITY_PROTOCOL: &str = "PLAINTEXT";
const DEFAULT_SASL_MECHANISM: &str = "PLAIN";
/// Bucket assigner strategy for tables without bucket keys.
/// Matches Java `client.writer.bucket.no-key-assigner`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum NoKeyAssigner {
/// Sticks to one bucket until the batch is full, then switches.
Sticky,
/// Assigns each record to the next bucket in a rotating sequence.
RoundRobin,
}
impl fmt::Display for NoKeyAssigner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NoKeyAssigner::Sticky => write!(f, "sticky"),
NoKeyAssigner::RoundRobin => write!(f, "round_robin"),
}
}
}
#[derive(Parser, Clone, Deserialize, Serialize)]
#[command(author, version, about, long_about = None)]
pub struct Config {
#[arg(long, default_value_t = String::from(DEFAULT_BOOTSTRAP_SERVER))]
pub bootstrap_servers: String,
#[arg(long, default_value_t = DEFAULT_REQUEST_MAX_SIZE)]
pub writer_request_max_size: i32,
#[arg(long, default_value_t = String::from(DEFAULT_ACKS))]
pub writer_acks: String,
#[arg(long, default_value_t = DEFAULT_RETRIES)]
pub writer_retries: i32,
#[arg(long, default_value_t = DEFAULT_WRITER_BATCH_SIZE)]
pub writer_batch_size: i32,
#[arg(long, value_enum, default_value_t = NoKeyAssigner::Sticky)]
pub writer_bucket_no_key_assigner: NoKeyAssigner,
/// Maximum number of remote log segments to prefetch
/// Default: 4 (matching Java CLIENT_SCANNER_REMOTE_LOG_PREFETCH_NUM)
#[arg(long, default_value_t = DEFAULT_PREFETCH_NUM)]
pub scanner_remote_log_prefetch_num: usize,
/// Maximum concurrent remote log downloads
/// Default: 3 (matching Java REMOTE_FILE_DOWNLOAD_THREAD_NUM)
#[arg(long, default_value_t = DEFAULT_DOWNLOAD_THREADS)]
pub remote_file_download_thread_num: usize,
/// Intra-file remote log read concurrency for each remote segment download.
/// Download path always uses streaming reader.
#[arg(long, default_value_t = DEFAULT_SCANNER_REMOTE_LOG_READ_CONCURRENCY)]
pub scanner_remote_log_read_concurrency: usize,
/// Maximum number of records returned in a single call to poll() for LogScanner.
/// Default: 500 (matching Java CLIENT_SCANNER_LOG_MAX_POLL_RECORDS)
#[arg(long, default_value_t = DEFAULT_MAX_POLL_RECORDS)]
pub scanner_log_max_poll_records: usize,
/// The maximum time to wait for a batch to be completed in milliseconds.
/// Default: 100 (matching Java CLIENT_WRITER_BATCH_TIMEOUT)
#[arg(long, default_value_t = DEFAULT_WRITER_BATCH_TIMEOUT_MS)]
pub writer_batch_timeout_ms: i64,
/// Connect timeout in milliseconds for TCP transport connect.
/// Default: 120000 (120 seconds).
#[arg(long, default_value_t = DEFAULT_CONNECT_TIMEOUT_MS)]
pub connect_timeout_ms: u64,
/// Request timeout in milliseconds for individual RPC calls.
/// Default: 30000 (30 seconds).
#[arg(long, default_value_t = DEFAULT_REQUEST_TIMEOUT_MS)]
pub request_timeout_ms: u64,
#[arg(long, default_value_t = String::from(DEFAULT_SECURITY_PROTOCOL))]
pub security_protocol: String,
#[arg(long, default_value_t = String::from(DEFAULT_SASL_MECHANISM))]
pub security_sasl_mechanism: String,
#[arg(long, default_value_t = String::new())]
pub security_sasl_username: String,
#[arg(long, default_value_t = String::new())]
#[serde(skip_serializing)]
pub security_sasl_password: String,
}
impl std::fmt::Debug for Config {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Config")
.field("bootstrap_servers", &self.bootstrap_servers)
.field("writer_request_max_size", &self.writer_request_max_size)
.field("writer_acks", &self.writer_acks)
.field("writer_retries", &self.writer_retries)
.field("writer_batch_size", &self.writer_batch_size)
.field(
"writer_bucket_no_key_assigner",
&self.writer_bucket_no_key_assigner,
)
.field(
"scanner_remote_log_prefetch_num",
&self.scanner_remote_log_prefetch_num,
)
.field(
"remote_file_download_thread_num",
&self.remote_file_download_thread_num,
)
.field(
"scanner_log_max_poll_records",
&self.scanner_log_max_poll_records,
)
.field("writer_batch_timeout_ms", &self.writer_batch_timeout_ms)
.field("connect_timeout_ms", &self.connect_timeout_ms)
.field("request_timeout_ms", &self.request_timeout_ms)
.field("security_protocol", &self.security_protocol)
.field("security_sasl_mechanism", &self.security_sasl_mechanism)
.field("security_sasl_username", &self.security_sasl_username)
.field("security_sasl_password", &"[REDACTED]")
.finish()
}
}
impl Default for Config {
fn default() -> Self {
Self {
bootstrap_servers: String::from(DEFAULT_BOOTSTRAP_SERVER),
writer_request_max_size: DEFAULT_REQUEST_MAX_SIZE,
writer_acks: String::from(DEFAULT_ACKS),
writer_retries: i32::MAX,
writer_batch_size: DEFAULT_WRITER_BATCH_SIZE,
writer_bucket_no_key_assigner: NoKeyAssigner::Sticky,
scanner_remote_log_prefetch_num: DEFAULT_PREFETCH_NUM,
remote_file_download_thread_num: DEFAULT_DOWNLOAD_THREADS,
scanner_remote_log_read_concurrency: DEFAULT_SCANNER_REMOTE_LOG_READ_CONCURRENCY,
scanner_log_max_poll_records: DEFAULT_MAX_POLL_RECORDS,
writer_batch_timeout_ms: DEFAULT_WRITER_BATCH_TIMEOUT_MS,
connect_timeout_ms: DEFAULT_CONNECT_TIMEOUT_MS,
request_timeout_ms: DEFAULT_REQUEST_TIMEOUT_MS,
security_protocol: String::from(DEFAULT_SECURITY_PROTOCOL),
security_sasl_mechanism: String::from(DEFAULT_SASL_MECHANISM),
security_sasl_username: String::new(),
security_sasl_password: String::new(),
}
}
}
impl Config {
/// Returns true when the security protocol indicates SASL authentication
/// should be performed. Matches Java's `SaslAuthenticationPlugin` which
/// registers as `"sasl"` (case-insensitive).
pub fn is_sasl_enabled(&self) -> bool {
self.security_protocol.eq_ignore_ascii_case("sasl")
}
/// Validates security configuration. Returns `Ok(())` when the config is
/// consistent, or an error message when SASL is enabled but the config is
/// incomplete or uses an unsupported mechanism.
pub fn validate_security(&self) -> Result<(), String> {
if !self.is_sasl_enabled() {
return Ok(());
}
if !self.security_sasl_mechanism.eq_ignore_ascii_case("PLAIN") {
return Err(format!(
"Unsupported SASL mechanism: '{}'. Only 'PLAIN' is supported.",
self.security_sasl_mechanism
));
}
if self.security_sasl_username.is_empty() {
return Err(
"security_sasl_username must be set when security_protocol is 'sasl'".to_string(),
);
}
if self.security_sasl_password.is_empty() {
return Err(
"security_sasl_password must be set when security_protocol is 'sasl'".to_string(),
);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_is_not_sasl() {
let config = Config::default();
assert!(!config.is_sasl_enabled());
assert!(config.validate_security().is_ok());
}
#[test]
fn test_sasl_enabled_valid() {
let config = Config {
security_protocol: "sasl".to_string(),
security_sasl_mechanism: "PLAIN".to_string(),
security_sasl_username: "admin".to_string(),
security_sasl_password: "secret".to_string(),
..Config::default()
};
assert!(config.is_sasl_enabled());
assert!(config.validate_security().is_ok());
}
#[test]
fn test_sasl_enabled_case_insensitive() {
let config = Config {
security_protocol: "SASL".to_string(),
security_sasl_username: "admin".to_string(),
security_sasl_password: "secret".to_string(),
..Config::default()
};
assert!(config.is_sasl_enabled());
assert!(config.validate_security().is_ok());
}
#[test]
fn test_sasl_missing_username() {
let config = Config {
security_protocol: "sasl".to_string(),
security_sasl_password: "secret".to_string(),
..Config::default()
};
assert!(config.validate_security().is_err());
}
#[test]
fn test_sasl_missing_password() {
let config = Config {
security_protocol: "sasl".to_string(),
security_sasl_username: "admin".to_string(),
..Config::default()
};
assert!(config.validate_security().is_err());
}
#[test]
fn test_sasl_unsupported_mechanism() {
let config = Config {
security_protocol: "sasl".to_string(),
security_sasl_mechanism: "SCRAM-SHA-256".to_string(),
security_sasl_username: "admin".to_string(),
security_sasl_password: "secret".to_string(),
..Config::default()
};
assert!(config.validate_security().is_err());
}
}