-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathmod.rs
143 lines (123 loc) · 4.47 KB
/
mod.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
/*
* Parseable Server (C) 2022 - 2024 Parseable, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
pub mod format;
use arrow_array::RecordBatch;
use arrow_schema::{Field, Fields, Schema};
use itertools::Itertools;
use std::sync::Arc;
use self::error::EventError;
use crate::{metadata, staging::STAGING, storage::StreamType};
use chrono::NaiveDateTime;
use std::collections::HashMap;
pub const DEFAULT_TIMESTAMP_KEY: &str = "p_timestamp";
#[derive(Clone)]
pub struct Event {
pub stream_name: String,
pub rb: RecordBatch,
pub origin_format: &'static str,
pub origin_size: u64,
pub is_first_event: bool,
pub parsed_timestamp: NaiveDateTime,
pub time_partition: Option<String>,
pub custom_partition_values: HashMap<String, String>,
pub stream_type: StreamType,
}
// Events holds the schema related to a each event for a single log stream
impl Event {
pub async fn process(self) -> Result<(), EventError> {
let mut key = get_schema_key(&self.rb.schema().fields);
if self.time_partition.is_some() {
let parsed_timestamp_to_min = self.parsed_timestamp.format("%Y%m%dT%H%M").to_string();
key.push_str(&parsed_timestamp_to_min);
}
if !self.custom_partition_values.is_empty() {
for (k, v) in self.custom_partition_values.iter().sorted_by_key(|v| v.0) {
key.push_str(&format!("&{k}={v}"));
}
}
if self.is_first_event {
commit_schema(&self.stream_name, self.rb.schema())?;
}
STAGING.get_or_create_stream(&self.stream_name).push(
&key,
&self.rb,
self.parsed_timestamp,
&self.custom_partition_values,
self.stream_type,
)?;
metadata::STREAM_INFO.update_stats(
&self.stream_name,
self.origin_format,
self.origin_size,
self.rb.num_rows(),
self.parsed_timestamp,
)?;
crate::livetail::LIVETAIL.process(&self.stream_name, &self.rb);
Ok(())
}
pub fn process_unchecked(&self) -> Result<(), EventError> {
let key = get_schema_key(&self.rb.schema().fields);
STAGING.get_or_create_stream(&self.stream_name).push(
&key,
&self.rb,
self.parsed_timestamp,
&self.custom_partition_values,
self.stream_type,
)?;
Ok(())
}
}
pub fn get_schema_key(fields: &[Arc<Field>]) -> String {
// Fields must be sorted
let mut hasher = xxhash_rust::xxh3::Xxh3::new();
for field in fields.iter().sorted_by_key(|v| v.name()) {
hasher.update(field.name().as_bytes());
}
let hash = hasher.digest();
format!("{hash:x}")
}
pub fn commit_schema(stream_name: &str, schema: Arc<Schema>) -> Result<(), EventError> {
let mut stream_metadata = metadata::STREAM_INFO.write().expect("lock poisoned");
let map = &mut stream_metadata
.get_mut(stream_name)
.expect("map has entry for this stream name")
.schema;
let current_schema = Schema::new(map.values().cloned().collect::<Fields>());
let schema = Schema::try_merge(vec![current_schema, schema.as_ref().clone()])?;
map.clear();
map.extend(schema.fields.iter().map(|f| (f.name().clone(), f.clone())));
Ok(())
}
pub mod error {
use arrow_schema::ArrowError;
use crate::metadata::error::stream_info::MetadataError;
use crate::staging::StagingError;
use crate::storage::ObjectStorageError;
#[derive(Debug, thiserror::Error)]
pub enum EventError {
#[error("Stream Writer Failed: {0}")]
StreamWriter(#[from] StagingError),
#[error("Metadata Error: {0}")]
Metadata(#[from] MetadataError),
#[error("Stream Writer Failed: {0}")]
Arrow(#[from] ArrowError),
#[error("ObjectStorage Error: {0}")]
ObjectStorage(#[from] ObjectStorageError),
}
}