-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_collector.rs
331 lines (290 loc) · 12.1 KB
/
message_collector.rs
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
pub mod pubsub {
tonic::include_proto!("pubsub");
}
use std::sync::Arc;
use bytes::Bytes;
use opendal::{layers::LoggingLayer, services, Operator};
use ractor::{
concurrency::JoinHandle,
factory::{
DiscardHandler, FactoryMessage, Job, JobOptions, WorkerBuilder, WorkerMessage,
WorkerStartContext,
},
Actor, ActorProcessingErr, ActorRef, RpcReplyPort,
};
use tracing::{debug, info};
use crate::{
pubsub::PublishRequest,
s3,
streaming_layer::{Partition, StreamingLayer},
};
// Reference https://github.com/slawlor/ractor/blob/000fbb63e7c5cb9fa522535565d1d74c48df7f8e/ractor/src/factory/tests/mod.rs#L156
const FOUNDATION_DB_TRASACTION_LIMIT: usize = 5_000_000; // 5 MB, original 10MB
const FOUNDATION_DB_KEY_TRASACTION_LIMIT: usize = 50_000; // 5 kb defual original is 10 KB
pub type TopicName = String;
pub type MessageData = Bytes;
#[derive(Debug, Clone)]
pub struct Message {
pub topic_name: TopicName,
pub partition: Partition,
pub data: MessageData,
}
impl Message {
pub fn new(topic_name: TopicName, data: MessageData, partition: Partition) -> Self {
Self {
topic_name,
data,
partition,
}
}
}
pub type ErrorCode = usize;
pub enum MessageCollectorWorkerOperation {
Flush,
Collect(Message, RpcReplyPort<ErrorCode>),
CollectBatch(Vec<PublishRequest>, RpcReplyPort<ErrorCode>),
}
struct MessageState {
keys_size: usize,
total_bytes: usize,
reply_ports: Vec<RpcReplyPort<ErrorCode>>,
s3_file: s3::S3File,
}
impl MessageState {
fn new(operator: Arc<Operator>) -> Self {
Self {
total_bytes: 0,
keys_size: 0,
reply_ports: Vec::with_capacity(500),
s3_file: s3::S3File::new(operator),
}
}
fn push(&mut self, message: Message) {
self.keys_size += message.data.len();
self.total_bytes += message.data.len() + message.topic_name.len();
}
/// clears both messages and total_bytes
fn clear(&mut self) {
self.total_bytes = 0;
self.keys_size = 0;
}
fn will_exceed_foundation_db_transaction_limit(&self, message: &Message) -> bool {
self.keys_size >= FOUNDATION_DB_KEY_TRASACTION_LIMIT
|| self.total_bytes + message.topic_name.len() + message.data.len()
>= FOUNDATION_DB_TRASACTION_LIMIT
}
}
pub struct MessageCollectorState {
message_state: MessageState,
worker_state: WorkerStartContext<String, MessageCollectorWorkerOperation>,
}
pub struct MessageCollectorWorker {
worker_id: ractor::factory::WorkerId,
streaming_db: StreamingLayer,
}
#[async_trait::async_trait]
impl Actor for MessageCollectorWorker {
type State = MessageCollectorState;
type Msg = WorkerMessage<String, MessageCollectorWorkerOperation>;
type Arguments = WorkerStartContext<String, MessageCollectorWorkerOperation>;
async fn pre_start(
&self,
myself: ActorRef<Self::Msg>,
startup_context: Self::Arguments,
) -> Result<Self::State, ActorProcessingErr> {
let wid = startup_context.wid.clone();
myself.send_interval(
ractor::concurrency::tokio_primatives::Duration::from_millis(250),
move || {
// TODO: make sure this gets uniformly distributed to all workers
WorkerMessage::Dispatch(Job {
key: format!("flush_{}", wid.clone()),
msg: MessageCollectorWorkerOperation::Flush,
options: JobOptions::default(),
})
},
);
let mut builder = services::S3::default();
builder
.access_key_id(&std::env::var("AWS_ACCESS_KEY_ID").expect("AWS_ACCESS_KEY_ID not set"));
builder.secret_access_key(
&std::env::var("AWS_SECRET_ACCESS_KEY").expect("AWS_SECRET_ACCESS_KEY not set"),
);
builder.bucket(&std::env::var("S3_BUCKET").expect("S3_BUCKET not set"));
builder.endpoint(&std::env::var("S3_ENDPOINT").expect("S3_ENDPOINT not set"));
builder.region("us-east-1");
let op = Arc::new(
Operator::new(builder)?
.layer(LoggingLayer::default())
.finish(),
);
Ok(MessageCollectorState {
message_state: MessageState::new(op.clone()),
worker_state: startup_context,
})
}
async fn handle(
&self,
_myself: ActorRef<Self::Msg>,
message: Self::Msg,
state: &mut Self::State,
) -> Result<(), ActorProcessingErr> {
match message {
WorkerMessage::FactoryPing(time) => {
debug!("worker {} got ping", state.worker_state.wid);
state
.worker_state
.factory
.cast(ractor::factory::FactoryMessage::WorkerPong(
state.worker_state.wid,
time.elapsed(),
))?;
}
WorkerMessage::Dispatch(job) => {
match job.msg {
MessageCollectorWorkerOperation::Collect(message, reply_port) => {
debug!("worker {} got collect", state.worker_state.wid);
if state
.message_state
.will_exceed_foundation_db_transaction_limit(&message)
{
info!("exceeded foundation db transaction limit");
let start = tokio::time::Instant::now();
match state.message_state.s3_file.upload_and_clear().await {
Ok((path, batch_statistic)) => {
self.streaming_db
.commit_batch_statistics(&path, &batch_statistic)
.await
.expect("could not commit batch");
info!("upload to s3 took: {}ms", start.elapsed().as_millis());
}
Err(e) => {
info!("error in uploading to s3: {}", e);
}
}
info!("upload to s3 took: {}ms", start.elapsed().as_millis());
state.message_state.clear();
state
.message_state
.reply_ports
.drain(..)
.for_each(|reply_port| {
if reply_port.send(0).is_err() {
debug!("Listener dropped their port before we could reply");
}
});
}
state.message_state.push(message.clone()); // FIXME: needed for transaction limit, should move transaction limit check to s3 file?
state.message_state.s3_file.insert_message(
&message.topic_name,
message.partition,
message.clone(),
);
state.message_state.reply_ports.push(reply_port);
}
MessageCollectorWorkerOperation::Flush => {
debug!("worker {} got flush message", state.worker_state.wid);
if state.message_state.s3_file.size() > 0 {
let start = tokio::time::Instant::now();
match state.message_state.s3_file.upload_and_clear().await {
Ok((path, batch_statistic)) => {
self.streaming_db
.commit_batch_statistics(&path, &batch_statistic)
.await
.expect("could not commit batch");
info!("upload to s3 took: {}ms", start.elapsed().as_millis());
}
Err(e) => {
info!("error in uploading to s3: {}", e);
}
}
state.message_state.clear(); // FIXME: needed for transaction limit, should move transaction limit check to s3 file?
info!("replying to listeners...");
state
.message_state
.reply_ports
.drain(..)
.for_each(|reply_port| {
if reply_port.send(0).is_err() {
debug!("Listener dropped their port before we could reply");
}
});
}
}
MessageCollectorWorkerOperation::CollectBatch(messages, reply_port) => {
debug!("worker {} got collect batch", state.worker_state.wid);
for message in messages {
let message = Message::new(
message.topic_name.clone(),
message.message.unwrap().value.clone().into(),
message.partition,
);
state.message_state.push(message.clone());
state.message_state.s3_file.insert_message(
&message.topic_name,
message.partition,
message.clone(),
);
}
state.message_state.reply_ports.push(reply_port);
}
}
// tell the factory job is finished
state
.worker_state
.factory
.cast(ractor::factory::FactoryMessage::Finished(
state.worker_state.wid,
job.key,
))?;
}
}
Ok(())
}
}
pub struct MessageCollectorWorkerBuilder;
impl WorkerBuilder<MessageCollectorWorker> for MessageCollectorWorkerBuilder {
fn build(&self, wid: ractor::factory::WorkerId) -> MessageCollectorWorker {
MessageCollectorWorker {
worker_id: wid,
streaming_db: StreamingLayer::new(),
}
}
}
pub struct MessageCollectorFactory;
pub type ActorFactory = ActorRef<FactoryMessage<TopicName, MessageCollectorWorkerOperation>>;
impl MessageCollectorFactory {
pub async fn create(num_workers: usize) -> (ActorFactory, JoinHandle<()>) {
let factory_definition = ractor::factory::Factory::<
TopicName,
MessageCollectorWorkerOperation,
MessageCollectorWorker,
> {
worker_count: num_workers,
// FIXME: start collecting worker stats
collect_worker_stats: false,
routing_mode: ractor::factory::RoutingMode::<String>::RoundRobin,
discard_threshold: None,
discard_handler: None,
..Default::default()
};
let uuid = uuid::Uuid::new_v4().to_string();
let actor_name = format!("message_collector_factory_{}", uuid);
Actor::spawn(
Some(actor_name),
factory_definition,
Box::new(MessageCollectorWorkerBuilder {}),
)
.await
.expect("Failed to spawn factory")
}
}
struct DiscardedMessageHandler;
impl DiscardHandler<TopicName, MessageCollectorWorkerOperation> for DiscardedMessageHandler {
fn discard(&self, job: Job<TopicName, MessageCollectorWorkerOperation>) {
info!("discarded message: {:?}........", job.key);
}
fn clone_box(&self) -> Box<dyn DiscardHandler<TopicName, MessageCollectorWorkerOperation>> {
Box::new(DiscardedMessageHandler)
}
}