-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathsource.rs
More file actions
412 lines (381 loc) · 15.6 KB
/
Copy pathsource.rs
File metadata and controls
412 lines (381 loc) · 15.6 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
use std::collections::HashMap;
use std::collections::hash_map::DefaultHasher;
use std::future::ready;
use std::hash::{Hash, Hasher};
use std::time::SystemTime;
use anyhow::Result;
use arrow::array::RecordBatch;
use arrow::datatypes::SchemaRef;
use arroyo_state::global_table_config;
use arroyo_state::tables::global_keyed_map::GlobalKeyedView;
use async_compression::tokio::bufread::{GzipDecoder, ZstdDecoder};
use async_trait::async_trait;
use bincode::{Decode, Encode};
use datafusion::common::ScalarValue;
use futures::StreamExt;
use parquet::arrow::ParquetRecordBatchStreamBuilder;
use parquet::arrow::async_reader::ParquetObjectReader;
use arroyo_operator::context::{SourceCollector, SourceContext};
use regex::Regex;
use tokio::io::{AsyncBufReadExt, AsyncRead, BufReader};
use tokio::select;
use tokio_stream::Stream;
use tokio_stream::wrappers::LinesStream;
use tracing::info;
use crate::filesystem::config;
use crate::filesystem::config::SourceFileCompressionFormat;
use arroyo_operator::SourceFinishType;
use arroyo_operator::operator::SourceOperator;
use arroyo_rpc::errors::DataflowError;
use arroyo_rpc::formats::{BadData, Format, Framing};
use arroyo_rpc::grpc::rpc::TableConfig;
use arroyo_rpc::{ControlMessage, connector_err, grpc::rpc::StopMode};
use arroyo_storage::StorageProvider;
use arroyo_types::to_nanos;
#[allow(unused)]
pub struct FileSystemSourceFunc {
pub source: config::FileSystemSource,
pub format: Format,
pub framing: Option<Framing>,
pub bad_data: Option<BadData>,
pub file_states: HashMap<String, FileReadState>,
}
#[derive(Encode, Decode, Debug, Clone, PartialEq, PartialOrd)]
pub enum FileReadState {
Finished,
RecordsRead(usize),
}
#[async_trait]
impl SourceOperator for FileSystemSourceFunc {
fn tables(&self) -> HashMap<String, TableConfig> {
global_table_config("a", "fs")
}
fn name(&self) -> String {
"FileSystem".to_string()
}
async fn run(
&mut self,
ctx: &mut SourceContext,
collector: &mut SourceCollector,
) -> Result<SourceFinishType, DataflowError> {
let storage_provider = StorageProvider::for_url_with_options(
&self.source.path,
self.source.storage_options.clone(),
)
.await
.map_err(|err| connector_err!(User, NoRetry, source: err.into(), "failed to construct storage provider"))?;
let regex_pattern = self
.source
.regex_pattern
.as_ref()
.map(|pattern| Regex::new(pattern))
.transpose()
.map_err(|err| {
connector_err!(User, NoRetry, source: err.into(),
"invalid regex pattern {}",
self.source.regex_pattern.as_ref().unwrap())
})?;
collector.initialize_deserializer(
self.format.clone(),
self.framing.clone(),
self.bad_data.clone(),
&[],
);
let parallelism = ctx.task_info.parallelism;
let task_index = ctx.task_info.task_index;
// TODO: sort by creation time
let mut file_paths = storage_provider
.list(regex_pattern.is_some())
.await
.map_err(|err| connector_err!(External, WithBackoff, source: err.into(), "could not list files"))?
.filter(|path| {
let Ok(path) = path else {
return ready(true);
};
// hash the path and modulo by the number of tasks
let mut hasher = DefaultHasher::new();
path.hash(&mut hasher);
if (hasher.finish() as usize) % parallelism as usize != task_index as usize {
return ready(false);
}
if let Some(matcher) = ®ex_pattern {
ready(matcher.is_match(path.as_ref()))
} else {
ready(true)
}
});
let state: &mut GlobalKeyedView<String, (String, FileReadState)> = ctx
.table_manager
.get_global_keyed_state("a")
.await
.expect("should have table");
self.file_states = state.get_all().clone().into_values().collect();
while let Some(path) = file_paths.next().await {
let obj_key = path
.map_err(|err| connector_err!(External, WithBackoff, source: err.into(), "could not get next path"))?
.to_string();
if let Some(FileReadState::Finished) = self.file_states.get(&obj_key) {
// already finished
continue;
}
if let Some(finish_type) = self
.read_file(ctx, collector, &storage_provider, &obj_key)
.await?
{
return Ok(finish_type);
}
}
info!("FileSystem source finished");
Ok(SourceFinishType::Final)
}
}
impl FileSystemSourceFunc {
async fn get_newline_separated_stream<'a>(
&mut self,
storage_provider: &'a StorageProvider,
path: String,
) -> Result<
Box<dyn Stream<Item = Result<String, DataflowError>> + Unpin + Send + 'a>,
DataflowError,
> {
match &self.format {
Format::Json(_) => {
let stream_reader = storage_provider.get_as_stream(path).await.unwrap();
let compression_reader: Box<dyn AsyncRead + Unpin + Send> = match self
.source
.compression_format
{
SourceFileCompressionFormat::Zstd => {
Box::new(ZstdDecoder::new(BufReader::new(stream_reader)))
}
SourceFileCompressionFormat::Gzip => {
Box::new(GzipDecoder::new(BufReader::new(stream_reader)))
}
SourceFileCompressionFormat::None => Box::new(BufReader::new(stream_reader)),
};
// use line iterators
let lines = LinesStream::new(BufReader::new(compression_reader).lines());
Ok(Box::new(lines.map(|string_result| {
string_result.map_err(|err| connector_err!(External, WithBackoff, source: err.into(), "could not get next path"))
})))
}
other => Err(connector_err!(
User,
NoRetry,
"newline separated stream not supported for {other:?}"
)),
}
}
async fn get_record_batch_stream(
&mut self,
storage_provider: &StorageProvider,
path: &str,
out_schema: SchemaRef,
) -> Result<
Box<dyn Stream<Item = Result<RecordBatch, DataflowError>> + Unpin + Send>,
DataflowError,
> {
match &self.format {
Format::Parquet(_) => {
let object_meta = storage_provider
.get_backing_store()
.head(&(path.into()))
.await
.map_err(|err| connector_err!(External, WithBackoff, source: err.into(), "could not get object metadata"))?;
let object_reader = ParquetObjectReader::new(
storage_provider.get_backing_store(),
object_meta.location,
)
.with_file_size(object_meta.size);
let reader_builder = ParquetRecordBatchStreamBuilder::new(object_reader)
.await
.map_err(|err| connector_err!(External, WithBackoff, source: err.into(), "could not construct parquet reader for file {path}"))?
.with_batch_size(8192);
let stream = reader_builder.build().map_err(|err| {
connector_err!(External, WithBackoff, source: err.into(), "could not construct parquet stream for file {path}")
})?;
let result = Box::new(stream.map(move |res| match res {
Ok(record_batch) => {
// add timestamp
let mut columns = record_batch.columns().to_vec();
let current_time = to_nanos(SystemTime::now());
let current_time_scalar =
ScalarValue::TimestampNanosecond(Some(current_time as i64), None);
let time_column = current_time_scalar
.to_array_of_size(record_batch.num_rows())
.unwrap();
columns.push(time_column);
RecordBatch::try_new(
out_schema.clone(),
columns
).map_err(|e| connector_err!(User, NoRetry, source: e.into(), "The parquet file has a schema that does not match the table schema"))
},
Err(err) => Err(connector_err!(
User, NoRetry, source: err.into(),
"could not read record batch from stream",
)),
}))
as Box<dyn Stream<Item = Result<RecordBatch, DataflowError>> + Send + Unpin>;
Ok(result)
}
_ => unreachable!("code path only for Parquet"),
}
}
async fn read_file(
&mut self,
ctx: &mut SourceContext,
collector: &mut SourceCollector,
storage_provider: &StorageProvider,
obj_key: &String,
) -> Result<Option<SourceFinishType>, DataflowError> {
let read_state = self
.file_states
.entry(obj_key.to_string())
.or_insert(FileReadState::RecordsRead(0));
let records_read = match read_state {
FileReadState::RecordsRead(records_read) => *records_read,
FileReadState::Finished => {
return Err(connector_err!(
User,
NoRetry,
"{obj_key} has already been read",
));
}
};
match self.format {
Format::Json(_) => {
let line_reader = self
.get_newline_separated_stream(storage_provider, obj_key.to_string())
.await?
.skip(records_read);
self.read_line_file(ctx, collector, line_reader, obj_key, records_read)
.await
}
Format::Avro(_) => todo!(),
Format::Parquet(_) => {
let record_batch_stream = self
.get_record_batch_stream(
storage_provider,
obj_key,
ctx.out_schema.schema.clone(),
)
.await?
.skip(records_read);
self.read_parquet_file(ctx, collector, record_batch_stream, obj_key, records_read)
.await
}
Format::RawString(_) => todo!(),
Format::RawBytes(_) => todo!(),
Format::Protobuf(_) => todo!("Protobuf not supported"),
Format::MsgPack(_) => todo!("MsgPack not supported"),
}
}
async fn read_parquet_file(
&mut self,
ctx: &mut SourceContext,
collector: &mut SourceCollector,
mut record_batch_stream: impl Stream<Item = Result<RecordBatch, DataflowError>> + Unpin + Send,
obj_key: &String,
mut records_read: usize,
) -> Result<Option<SourceFinishType>, DataflowError> {
loop {
select! {
item = record_batch_stream.next() => {
match item.transpose()? {
Some(batch) => {
collector.collect(batch).await?;
records_read += 1;
}
None => {
info!("finished reading file {}", obj_key);
self.file_states.insert(obj_key.to_string(), FileReadState::Finished);
return Ok(None);
}
}
},
msg_res = ctx.control_rx.recv() => {
if let Some(control_message) = msg_res {
self.file_states.insert(obj_key.to_string(), FileReadState::RecordsRead(records_read));
if let Some(finish_type) = self.process_control_message(ctx, collector, control_message).await {
return Ok(Some(finish_type))
}
}
}
}
}
}
async fn read_line_file(
&mut self,
ctx: &mut SourceContext,
collector: &mut SourceCollector,
mut line_reader: impl Stream<Item = Result<String, DataflowError>> + Unpin + Send,
obj_key: &String,
mut records_read: usize,
) -> Result<Option<SourceFinishType>, DataflowError> {
loop {
select! {
line = line_reader.next() => {
match line.transpose()? {
Some(line) => {
collector.deserialize_slice(line.as_bytes(), SystemTime::now(), None).await?;
records_read += 1;
if collector.should_flush() {
collector.flush_buffer().await?;
}
}
None => {
info!("finished reading file {}", obj_key);
collector.flush_buffer().await?;
self.file_states.insert(obj_key.to_string(), FileReadState::Finished);
return Ok(None);
}
}
},
msg_res = ctx.control_rx.recv() => {
if let Some(control_message) = msg_res {
self.file_states.insert(obj_key.to_string(), FileReadState::RecordsRead(records_read));
if let Some(finish_type) = self.process_control_message(ctx, collector, control_message).await {
return Ok(Some(finish_type))
}
}
}
}
}
}
async fn process_control_message(
&mut self,
ctx: &mut SourceContext,
collector: &mut SourceCollector,
control_message: ControlMessage,
) -> Option<SourceFinishType> {
match control_message {
ControlMessage::Checkpoint(c) => {
for (file, read_state) in &self.file_states {
ctx.table_manager
.get_global_keyed_state("a")
.await
.unwrap()
.insert(file.clone(), (file.clone(), read_state.clone()))
.await;
}
// checkpoint our state
if self.start_checkpoint(c, ctx, collector).await {
Some(SourceFinishType::Immediate)
} else {
None
}
}
ControlMessage::Stop { mode } => {
info!("Stopping FileSystem source {:?}", mode);
match mode {
StopMode::Graceful => Some(SourceFinishType::Graceful),
StopMode::Immediate => Some(SourceFinishType::Immediate),
}
}
ControlMessage::Commit { .. } => {
unreachable!("sources shouldn't receive commit messages");
}
_ => None,
}
}
}