-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathmod.rs
146 lines (125 loc) · 4.38 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
144
145
146
/*
* 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::update_stats,
parseable::{StagingError, PARSEABLE},
storage::StreamType,
LOCK_EXPECT,
};
use chrono::NaiveDateTime;
use std::collections::HashMap;
pub const DEFAULT_TIMESTAMP_KEY: &str = "p_timestamp";
pub const USER_AGENT_KEY: &str = "p_user_agent";
pub const SOURCE_IP_KEY: &str = "p_src_ip";
pub const FORMAT_KEY: &str = "p_format";
#[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 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())?;
}
PARSEABLE.get_or_create_stream(&self.stream_name).push(
&key,
&self.rb,
self.parsed_timestamp,
&self.custom_partition_values,
)?;
update_stats(
&self.stream_name,
self.origin_format,
self.origin_size,
self.rb.num_rows(),
self.parsed_timestamp.date(),
);
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);
PARSEABLE.get_or_create_stream(&self.stream_name).push(
&key,
&self.rb,
self.parsed_timestamp,
&self.custom_partition_values,
)?;
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<(), StagingError> {
let mut stream_metadata = PARSEABLE.streams.write().expect("lock poisoned");
let map = &mut stream_metadata
.get_mut(stream_name)
.expect("map has entry for this stream name")
.metadata
.write()
.expect(LOCK_EXPECT)
.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 crate::{parseable::StagingError, storage::ObjectStorageError};
#[derive(Debug, thiserror::Error)]
pub enum EventError {
#[error("Staging Failed: {0}")]
Staging(#[from] StagingError),
#[error("ObjectStorage Error: {0}")]
ObjectStorage(#[from] ObjectStorageError),
}
}