-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathinput_provider.rs
More file actions
145 lines (132 loc) · 5.12 KB
/
Copy pathinput_provider.rs
File metadata and controls
145 lines (132 loc) · 5.12 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
// Licensed 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 crate::runtime::input::Input;
use crate::runtime::task::InputConfig;
pub struct InputProvider;
impl InputProvider {
pub fn from_input_configs(
input_configs: &[InputConfig],
group_idx: usize,
) -> Result<Vec<Box<dyn Input>>, Box<dyn std::error::Error + Send>> {
if input_configs.is_empty() {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!(
"Empty input configs list for input group #{}",
group_idx + 1
),
)) as Box<dyn std::error::Error + Send>);
}
const MAX_INPUTS: usize = 64;
if input_configs.len() > MAX_INPUTS {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!(
"Too many inputs in group #{}: {} (maximum is {})",
group_idx + 1,
input_configs.len(),
MAX_INPUTS
),
)) as Box<dyn std::error::Error + Send>);
}
let mut inputs = Vec::new();
for (input_idx, input_config) in input_configs.iter().enumerate() {
let input = Self::from_input_config(input_config, group_idx, input_idx)?;
inputs.push(input);
}
Ok(inputs)
}
fn from_input_config(
input_config: &InputConfig,
group_idx: usize,
input_idx: usize,
) -> Result<Box<dyn Input>, Box<dyn std::error::Error + Send>> {
match input_config {
InputConfig::Kafka {
bootstrap_servers,
topic,
partition,
group_id,
extra,
runtime: _,
} => {
use crate::runtime::input::InputRunner;
use crate::runtime::input::protocol::kafka::{KafkaConfig, KafkaProtocol};
let servers: Vec<String> = bootstrap_servers
.split(',')
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect();
if servers.is_empty() {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!(
"Invalid bootstrap_servers in input config (group #{}): empty or invalid (topic: {}, group_id: {})",
group_idx + 1,
topic,
group_id
),
)) as Box<dyn std::error::Error + Send>);
}
let partition_i32 = partition.map(|p| p as i32);
let properties = extra.clone();
let kafka_config = KafkaConfig::new(
servers,
topic.clone(),
partition_i32,
group_id.clone(),
properties,
);
let runtime = input_config.input_runtime_config();
Ok(Box::new(InputRunner::new(
KafkaProtocol::new(kafka_config),
group_idx,
input_idx,
runtime,
)))
}
InputConfig::Nats {
url,
subject,
queue_group,
extra,
runtime: _,
} => {
use crate::runtime::input::InputRunner;
use crate::runtime::input::protocol::nats::{NatsConfig, NatsProtocol};
if url.is_empty() {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!(
"Invalid nats url in input config (group #{}): empty (subject: {})",
group_idx + 1,
subject
),
)) as Box<dyn std::error::Error + Send>);
}
let nats_config = NatsConfig::new(
url.clone(),
subject.clone(),
queue_group.clone(),
extra.clone(),
);
let runtime = input_config.input_runtime_config();
Ok(Box::new(InputRunner::new(
NatsProtocol::new(nats_config),
group_idx,
input_idx,
runtime,
)))
}
}
}
}