-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathwal.rs
More file actions
163 lines (141 loc) · 5.37 KB
/
wal.rs
File metadata and controls
163 lines (141 loc) · 5.37 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
//! Write Ahead Log for both Aligned and Unaligned Reduce Operation. The WAL is to persist the data
//! we read from the ISB and store it until the processing is complete.
use crate::config::components::reduce::StorageConfig;
use crate::message::{IntOffset, Message, Offset};
use crate::reduce::error::Error;
use crate::reduce::pbq::WAL;
use crate::reduce::wal::segment::WalType;
use crate::reduce::wal::segment::append::AppendOnlyWal;
use crate::reduce::wal::segment::compactor::{Compactor, WindowKind};
use crate::shared::grpc::{prost_timestamp_from_utc, utc_from_timestamp};
use bytes::Bytes;
use std::sync::Arc;
/// A WAL Segment.
pub(crate) mod segment;
/// All the errors WAL could face.
pub(crate) mod error;
#[derive(Debug, Clone)]
pub(crate) struct WalMessage {
pub(crate) message: Message,
}
// Convert Message to Bytes used to persist in WAL
impl TryFrom<WalMessage> for Bytes {
type Error = Error;
fn try_from(wal_message: WalMessage) -> Result<Self, Self::Error> {
let message = wal_message.message;
let Offset::Int(int_offset) = message.offset else {
return Err(Error::Other("Invalid offset".to_string()));
};
let proto_message = numaflow_pb::objects::isb::ReadMessage {
message: Some(numaflow_pb::objects::isb::Message {
header: Some(numaflow_pb::objects::isb::Header {
message_info: Some(numaflow_pb::objects::isb::MessageInfo {
event_time: Some(prost_timestamp_from_utc(message.event_time)),
is_late: false,
}),
kind: message.typ.into(),
id: Some(message.id.into()),
keys: message.keys.to_vec(),
headers: Arc::unwrap_or_clone(message.headers),
metadata: message.metadata.map(|m| Arc::unwrap_or_clone(m).into()),
}),
body: Some(numaflow_pb::objects::isb::Body {
payload: message.value.clone(),
}),
}),
read_offset: int_offset.offset,
watermark: message.watermark.map(prost_timestamp_from_utc),
metadata: None,
};
Ok(Bytes::from(prost::Message::encode_to_vec(&proto_message)))
}
}
// Convert Bytes to Message, used while reading from WAL
impl TryFrom<Bytes> for WalMessage {
type Error = Error;
fn try_from(value: Bytes) -> Result<Self, Self::Error> {
let proto_read_message: numaflow_pb::objects::isb::ReadMessage =
prost::Message::decode(value.as_ref()).map_err(|e| Error::Other(e.to_string()))?;
let proto_message = proto_read_message
.message
.ok_or_else(|| Error::Other("Missing inner message".to_string()))?;
let header = proto_message
.header
.ok_or_else(|| Error::Other("Missing header".to_string()))?;
let body = proto_message
.body
.ok_or_else(|| Error::Other("Missing body".to_string()))?;
let msg = Message {
typ: header.kind.into(),
keys: Arc::from(header.keys),
tags: None,
value: Bytes::from(body.payload),
offset: Offset::Int(IntOffset::new(proto_read_message.read_offset, 0)),
event_time: header
.message_info
.expect("info can't be empty")
.event_time
.map(utc_from_timestamp)
.expect("event time should be present"),
watermark: proto_read_message.watermark.map(utc_from_timestamp),
id: header.id.map(Into::into).unwrap_or_default(),
..Default::default()
};
Ok(WalMessage { message: msg })
}
}
impl From<WalMessage> for Message {
fn from(value: WalMessage) -> Self {
value.message
}
}
impl From<Message> for WalMessage {
fn from(value: Message) -> Self {
WalMessage { message: value }
}
}
/// Create WAL components for reduce operations
///
/// This function creates both the main WAL and GC WAL if storage is configured.
/// The only difference between aligned and unaligned reducers is the WindowKind.
pub(crate) async fn create_wal_components(
storage_config: Option<&StorageConfig>,
window_kind: WindowKind,
) -> crate::Result<(Option<WAL>, Option<AppendOnlyWal>)> {
if let Some(storage_config) = storage_config {
let wal_path = storage_config.path.clone();
let append_only_wal = AppendOnlyWal::new(
WalType::Data,
wal_path.clone(),
storage_config.max_file_size_mb,
storage_config.flush_interval_ms,
storage_config.max_segment_age_secs,
)
.await?;
let compactor = Compactor::new(
wal_path.clone(),
window_kind,
storage_config.max_file_size_mb,
storage_config.flush_interval_ms,
storage_config.max_segment_age_secs,
)
.await?;
let gc_wal = AppendOnlyWal::new(
WalType::Gc,
wal_path,
storage_config.max_file_size_mb,
storage_config.flush_interval_ms,
storage_config.max_segment_age_secs,
)
.await?;
Ok((
Some(WAL {
append_only_wal,
compactor,
}),
Some(gc_wal),
))
} else {
Ok((None, None))
}
}