Skip to content

Commit 5925071

Browse files
authored
chore: introduce enum for sink response type (#115)
Signed-off-by: Yashash H L <[email protected]>
1 parent c68963a commit 5925071

File tree

30 files changed

+174
-171
lines changed

30 files changed

+174
-171
lines changed

examples/batchmap-cat/.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

examples/batchmap-flatmap/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl batchmap::BatchMapper for Flatmap {
2121
// Split the string by ","
2222
let parts: Vec<&str> = s.split(',').collect();
2323

24-
// return the resulting parts
24+
// return the resulting parts as a Vec<Message>
2525
for part in parts {
2626
response.append(Message::new(Vec::from(part)).with_keys(datum.keys.clone()));
2727
}

examples/flatmap-stream/.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

examples/flatmap-stream/src/main.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ struct Cat;
1212
#[tonic::async_trait]
1313
impl mapstream::MapStreamer for Cat {
1414
async fn map_stream(&self, input: mapstream::MapStreamRequest, tx: Sender<Message>) {
15-
for i in 0..2 {
16-
let message = Message::new(input.value.clone())
17-
.with_keys(vec![format!("key-{}", i)])
15+
let payload_str = String::from_utf8(input.value).unwrap_or_default();
16+
let splits: Vec<&str> = payload_str.split(',').collect();
17+
18+
for split in splits {
19+
let message = Message::new(split.as_bytes().to_vec())
20+
.with_keys(input.keys.clone())
1821
.with_tags(vec![]);
1922
if tx.send(message).await.is_err() {
2023
break;

examples/map-cat/.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

examples/map-tickgen-serde/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
99
serde = { version = "1.0.103", features = ["derive"] }
1010
serde_json = "1.0.103"
1111
numaflow = { path = "../../numaflow" }
12+
chrono = "0.4.26"

examples/map-tickgen-serde/src/main.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use chrono::{SecondsFormat, TimeZone, Utc};
12
use numaflow::map;
23
use numaflow::map::Message;
34
use serde::Serialize;
4-
use std::time::UNIX_EPOCH;
55

66
#[tokio::main]
77
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
@@ -35,11 +35,13 @@ impl map::Mapper for TickGen {
3535
let Ok(payload) = serde_json::from_slice::<Payload>(&input.value) else {
3636
return vec![];
3737
};
38-
let ts = UNIX_EPOCH + std::time::Duration::from_nanos(payload.created_ts as u64);
38+
let ts = Utc
39+
.timestamp_nanos(payload.created_ts)
40+
.to_rfc3339_opts(SecondsFormat::Nanos, true);
3941
let message = map::Message::new(
4042
serde_json::to_vec(&ResultPayload {
4143
value: payload.data.value,
42-
time: format!("{:?}", ts),
44+
time: ts,
4345
})
4446
.unwrap_or_default(),
4547
)
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
target/
1+
target/

examples/mapt-event-time-filter/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2021"
77
numaflow = { path = "../../numaflow" }
88
tokio = "1.38.0"
99
tonic = "0.12.0"
10+
chrono = "0.4.38"

examples/mapt-event-time-filter/src/main.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ impl sourcetransform::SourceTransformer for EventTimeFilter {
2020
}
2121

2222
mod filter_impl {
23+
use chrono::{TimeZone, Utc};
2324
use numaflow::sourcetransform::{Message, SourceTransformRequest};
24-
use std::time::{Duration, UNIX_EPOCH};
2525

2626
/// Filters messages based on their event time.
2727
/// Returns different types of messages depending on the event time comparison.
2828
pub fn filter_event_time(input: SourceTransformRequest) -> Vec<Message> {
29-
let jan_first_2022 = UNIX_EPOCH + Duration::new(1_640_995_200, 0); // 2022-01-01 00:00:00 UTC
30-
let jan_first_2023 = UNIX_EPOCH + Duration::new(1_672_348_800, 0); // 2023-01-01 00:00:00 UTC
29+
let jan_first_2022 = Utc.with_ymd_and_hms(2022, 1, 1, 0, 0, 0).unwrap();
30+
let jan_first_2023 = Utc.with_ymd_and_hms(2023, 1, 1, 0, 0, 0).unwrap();
3131

3232
if input.eventtime < jan_first_2022 {
3333
vec![Message::message_to_drop(input.eventtime)]
@@ -43,19 +43,18 @@ mod filter_impl {
4343

4444
#[cfg(test)]
4545
mod tests {
46-
use numaflow::sourcetransform::SourceTransformRequest;
47-
use std::time::{Duration, SystemTime, UNIX_EPOCH};
48-
4946
use crate::filter_impl::filter_event_time;
47+
use chrono::{TimeZone, Utc};
48+
use numaflow::sourcetransform::SourceTransformRequest;
5049

5150
/// Tests that events from 2022 are tagged as within the year 2022.
5251
#[test]
5352
fn test_filter_event_time_should_return_within_year_2022() {
54-
let time = UNIX_EPOCH + Duration::new(1_656_732_000, 0); // 2022-07-02 02:00:00 UTC
53+
let time = Utc.with_ymd_and_hms(2022, 7, 2, 2, 0, 0).unwrap();
5554
let source_request = SourceTransformRequest {
5655
keys: vec![],
5756
value: vec![],
58-
watermark: SystemTime::now(),
57+
watermark: Default::default(),
5958
eventtime: time,
6059
headers: Default::default(),
6160
};
@@ -69,11 +68,11 @@ mod tests {
6968
/// Tests that events from 2023 are tagged as after the year 2022.
7069
#[test]
7170
fn test_filter_event_time_should_return_after_year_2022() {
72-
let time = UNIX_EPOCH + Duration::new(1_682_348_800, 0); // 2023-07-02 02:00:00 UTC
71+
let time = Utc.with_ymd_and_hms(2023, 7, 2, 2, 0, 0).unwrap();
7372
let source_request = SourceTransformRequest {
7473
keys: vec![],
7574
value: vec![],
76-
watermark: SystemTime::now(),
75+
watermark: Default::default(),
7776
eventtime: time,
7877
headers: Default::default(),
7978
};
@@ -87,11 +86,11 @@ mod tests {
8786
/// Tests that events before 2022 are dropped.
8887
#[test]
8988
fn test_filter_event_time_should_drop() {
90-
let time = UNIX_EPOCH + Duration::new(1_594_732_000, 0); // 2021-07-02 02:00:00 UTC
89+
let time = Utc.with_ymd_and_hms(2021, 7, 2, 2, 0, 0).unwrap();
9190
let source_request = SourceTransformRequest {
9291
keys: vec![],
9392
value: vec![],
94-
watermark: SystemTime::now(),
93+
watermark: Default::default(),
9594
eventtime: time,
9695
headers: Default::default(),
9796
};

examples/reduce-counter/.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

examples/sideinput/.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

examples/simple-source/.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

examples/simple-source/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2021"
77
tonic = "0.12.0"
88
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
99
numaflow = { path = "../../numaflow" }
10+
chrono = "0.4.38"

examples/simple-source/src/main.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
88
}
99

1010
pub(crate) mod simple_source {
11+
use chrono::Utc;
1112
use numaflow::source::{Message, Offset, SourceReadRequest, Sourcer};
1213
use std::sync::atomic::{AtomicUsize, Ordering};
13-
use std::time::{SystemTime, UNIX_EPOCH};
1414
use std::{collections::HashSet, sync::RwLock};
1515
use tokio::sync::mpsc::Sender;
1616

@@ -38,14 +38,10 @@ pub(crate) mod simple_source {
3838
return;
3939
}
4040

41-
let event_time = SystemTime::now();
41+
let event_time = Utc::now();
4242
let mut message_offsets = Vec::with_capacity(request.count);
4343
for i in 0..request.count {
44-
let offset = format!(
45-
"{}-{}",
46-
event_time.duration_since(UNIX_EPOCH).unwrap().as_nanos(),
47-
i
48-
);
44+
let offset = format!("{}-{}", event_time.timestamp_nanos_opt().unwrap(), i);
4945
let payload = self.counter.fetch_add(1, Ordering::Relaxed).to_string();
5046
transmitter
5147
.send(Message {

examples/sink-log/.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target/

examples/source-transformer-now/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2021"
77
tonic = "0.12.0"
88
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
99
numaflow = { path = "../../numaflow" }
10+
chrono = "0.4.40"

examples/source-transformer-now/src/main.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use numaflow::sourcetransform;
2-
use std::time::SystemTime;
32

43
/// A simple source transformer which assigns event time to the current time in utc.
54
@@ -17,7 +16,7 @@ impl sourcetransform::SourceTransformer for NowCat {
1716
input: sourcetransform::SourceTransformRequest,
1817
) -> Vec<sourcetransform::Message> {
1918
vec![
20-
sourcetransform::Message::new(input.value, SystemTime::now())
19+
sourcetransform::Message::new(input.value, chrono::offset::Utc::now())
2120
.with_keys(input.keys.clone()),
2221
]
2322
}

numaflow/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ tracing = "0.1.40"
3737
uuid = { version = "1.10.0", features = ["v4"] }
3838
thiserror = "2.0.12"
3939
hyper-util = "0.1.7"
40+
chrono = "0.4.38"
4041

4142
[build-dependencies]
4243
tonic-build = "0.12.3"

numaflow/src/batchmap.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use chrono::{DateTime, Utc};
12
use std::collections::HashMap;
23
use std::fs;
34
use std::path::PathBuf;
45
use std::sync::Arc;
5-
use std::time::SystemTime;
66
use tokio::sync::mpsc::channel;
77
use tokio::sync::{mpsc, oneshot};
88
use tokio::task::JoinHandle;
@@ -83,9 +83,9 @@ pub struct Datum {
8383
/// The value in the (key, value) terminology of map/reduce paradigm.
8484
pub value: Vec<u8>,
8585
/// [watermark](https://numaflow.numaproj.io/core-concepts/watermarks/) represented by time is a guarantee that we will not see an element older than this time.
86-
pub watermark: SystemTime,
86+
pub watermark: DateTime<Utc>,
8787
/// Time of the element as seen at source or aligned after a reduce operation.
88-
pub event_time: SystemTime,
88+
pub event_time: DateTime<Utc>,
8989
/// ID is the unique id of the message to be sent to the Batch Map.
9090
pub id: String,
9191
/// Headers for the message.
@@ -103,12 +103,8 @@ impl TryFrom<MapRequest> for Datum {
103103
Ok(Self {
104104
keys: request.keys,
105105
value: request.value,
106-
watermark: shared::prost_timestamp_to_system_time(
107-
request.watermark.unwrap_or_default(),
108-
),
109-
event_time: shared::prost_timestamp_to_system_time(
110-
request.event_time.unwrap_or_default(),
111-
),
106+
watermark: shared::utc_from_timestamp(request.watermark),
107+
event_time: shared::utc_from_timestamp(request.event_time),
112108
id: sr.id,
113109
headers: request.headers,
114110
})

numaflow/src/map.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use chrono::{DateTime, Utc};
12
use std::collections::HashMap;
23
use std::fs;
34
use std::path::PathBuf;
45
use std::sync::Arc;
5-
use std::time::SystemTime;
66
use tokio::sync::{mpsc, oneshot};
77
use tokio::task::JoinHandle;
88
use tokio_stream::wrappers::ReceiverStream;
@@ -163,9 +163,9 @@ pub struct MapRequest {
163163
/// The value in the (key, value) terminology of map/reduce paradigm.
164164
pub value: Vec<u8>,
165165
/// [watermark](https://numaflow.numaproj.io/core-concepts/watermarks/) represented by time is a guarantee that we will not see an element older than this time.
166-
pub watermark: SystemTime,
166+
pub watermark: DateTime<Utc>,
167167
/// Time of the element as seen at source or aligned after a reduce operation.
168-
pub eventtime: SystemTime,
168+
pub eventtime: DateTime<Utc>,
169169
/// Headers for the message.
170170
pub headers: HashMap<String, String>,
171171
}
@@ -175,8 +175,8 @@ impl From<proto::map_request::Request> for MapRequest {
175175
Self {
176176
keys: value.keys,
177177
value: value.value,
178-
watermark: shared::prost_timestamp_to_system_time(value.watermark.unwrap_or_default()),
179-
eventtime: shared::prost_timestamp_to_system_time(value.event_time.unwrap_or_default()),
178+
watermark: shared::utc_from_timestamp(value.watermark),
179+
eventtime: shared::utc_from_timestamp(value.event_time),
180180
headers: value.headers,
181181
}
182182
}

numaflow/src/mapstream.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
use chrono::{DateTime, Utc};
12
use std::collections::HashMap;
23
use std::fs;
34
use std::path::PathBuf;
45
use std::sync::Arc;
5-
use std::time::SystemTime;
66
use tokio::sync::mpsc::Sender;
77
use tokio::sync::{mpsc, oneshot};
88
use tokio::task::JoinHandle;
@@ -167,9 +167,9 @@ pub struct MapStreamRequest {
167167
/// The value in the (key, value) terminology of map/reduce paradigm.
168168
pub value: Vec<u8>,
169169
/// [watermark](https://numaflow.numaproj.io/core-concepts/watermarks/) represented by time is a guarantee that we will not see an element older than this time.
170-
pub watermark: SystemTime,
170+
pub watermark: DateTime<Utc>,
171171
/// Time of the element as seen at source or aligned after a reduce operation.
172-
pub eventtime: SystemTime,
172+
pub eventtime: DateTime<Utc>,
173173
/// Headers for the message.
174174
pub headers: HashMap<String, String>,
175175
}
@@ -179,8 +179,8 @@ impl From<proto::map_request::Request> for MapStreamRequest {
179179
Self {
180180
keys: value.keys,
181181
value: value.value,
182-
watermark: shared::prost_timestamp_to_system_time(value.watermark.unwrap_or_default()),
183-
eventtime: shared::prost_timestamp_to_system_time(value.event_time.unwrap_or_default()),
182+
watermark: shared::utc_from_timestamp(value.watermark),
183+
eventtime: shared::utc_from_timestamp(value.event_time),
184184
headers: value.headers,
185185
}
186186
}

0 commit comments

Comments
 (0)