Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sequence page to topics #400

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]
Copyright 2024 Momento Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
8 changes: 4 additions & 4 deletions sdk/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ doc = false


[dependencies]
momento-protos = { version = "0.113.0" }
momento-protos = { version = "0.119.4" }
log = "0.4"
hyper = { version = "0.14" }
h2 = { version = "0.3" }
Expand Down
9 changes: 7 additions & 2 deletions sdk/src/topics/messages/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ use crate::{
/// use momento::topics::SubscribeRequest;
/// # let (topic_client, cache_name) = momento_test_util::create_doctest_topic_client();
///
/// // Subscribe to a topic and resume from sequence number 10
/// let request = SubscribeRequest::new(cache_name, "topic", Some(10));
/// // Subscribe to a topic and resume from sequence number 10 and page 2
/// let request = SubscribeRequest::new(cache_name, "topic", Some(10), Some(2));
///
/// // Note: your subscription must be declared as `mut`!
/// let mut subscription = topic_client.send_request(request).await?;
Expand All @@ -50,6 +50,7 @@ pub struct SubscribeRequest {
cache_name: String,
topic: String,
resume_at_topic_sequence_number: Option<u64>,
resume_at_sequence_page: Option<u64>,
}

impl SubscribeRequest {
Expand All @@ -58,11 +59,13 @@ impl SubscribeRequest {
cache_name: impl Into<String>,
topic: impl Into<String>,
resume_at_topic_sequence_number: Option<u64>,
resume_at_sequence_page: Option<u64>,
) -> Self {
Self {
cache_name: cache_name.into(),
topic: topic.into(),
resume_at_topic_sequence_number,
resume_at_sequence_page,
}
}
}
Expand All @@ -80,6 +83,7 @@ impl MomentoRequest for SubscribeRequest {
resume_at_topic_sequence_number: self
.resume_at_topic_sequence_number
.unwrap_or_default(),
sequence_page: self.resume_at_topic_sequence_number.unwrap_or_default(),
},
)?;

Expand All @@ -94,6 +98,7 @@ impl MomentoRequest for SubscribeRequest {
self.cache_name,
self.topic,
self.resume_at_topic_sequence_number.unwrap_or_default(),
self.resume_at_sequence_page.unwrap_or_default(),
SubscriptionState::Subscribed(stream),
))
}
Expand Down
20 changes: 19 additions & 1 deletion sdk/src/topics/messages/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub struct Subscription {
cache_name: String,
topic: String,
current_sequence_number: u64,
current_sequence_page: u64,
current_subscription: SubscriptionState,
}

Expand Down Expand Up @@ -113,13 +114,15 @@ impl Subscription {
cache_name: String,
topic: String,
current_sequence_number: u64,
current_sequence_page: u64,
current_subscription: SubscriptionState,
) -> Subscription {
Subscription {
client,
cache_name,
topic,
current_sequence_number,
current_sequence_page,
current_subscription,
}
}
Expand All @@ -134,10 +137,12 @@ impl Subscription {
pubsub::subscription_item::Kind::Item(item) => match item.value {
Some(value) => {
let sequence_number = item.topic_sequence_number;
let sequence_page = item.sequence_page;
match value.kind {
Some(topic_value_kind) => {
MapKind::RealItem(SubscriptionItem::Value(SubscriptionValue {
topic_sequence_number: sequence_number,
topic_sequence_page: sequence_page,
kind: match topic_value_kind {
pubsub::topic_value::Kind::Text(text) => {
ValueKind::Text(text)
Expand All @@ -156,6 +161,7 @@ impl Subscription {
MapKind::RealItem(SubscriptionItem::Discontinuity(Discontinuity {
last_sequence_number: None,
new_sequence_number: sequence_number,
new_sequence_page: sequence_page,
}))
}
}
Expand All @@ -166,6 +172,7 @@ impl Subscription {
MapKind::RealItem(SubscriptionItem::Discontinuity(Discontinuity {
last_sequence_number: Some(discontinuity.last_topic_sequence),
new_sequence_number: discontinuity.new_topic_sequence,
new_sequence_page: discontinuity.new_sequence_page,
}))
}
pubsub::subscription_item::Kind::Heartbeat(_) => MapKind::Heartbeat,
Expand All @@ -179,12 +186,14 @@ impl Subscription {
let cache_name = self.cache_name.clone();
let topic = self.topic.clone();
let resume_at_topic_sequence_number = self.current_sequence_number;
let resume_at_topic_sequence_page = self.current_sequence_page;
async move {
client
.subscribe(SubscriptionRequest {
cache_name,
topic,
resume_at_topic_sequence_number,
sequence_page: resume_at_topic_sequence_page,
})
.await
}
Expand Down Expand Up @@ -212,12 +221,15 @@ impl futures::Stream for Subscription {
SubscriptionItem::Value(v) => {
self.current_sequence_number =
v.topic_sequence_number;
self.current_sequence_page = v.topic_sequence_page;
// We return only SubscriptionValues here
break std::task::Poll::Ready(Some(v.clone()));
}
SubscriptionItem::Discontinuity(d) => {
log::debug!("discontinuity! Updating sequence number and continuing...");
self.current_sequence_number = d.new_sequence_number
self.current_sequence_number =
d.new_sequence_number;
self.current_sequence_page = d.new_sequence_page;
}
}
}
Expand Down Expand Up @@ -304,6 +316,9 @@ pub struct SubscriptionValue {
/// Best-effort sequence number for the topic. This is not transactional, it's just
/// to help you know when things are probably working well or probably not working well.
pub topic_sequence_number: u64,
/// Best-effort page number for the topic. This is not transactional, it's just
/// to help you know when things are probably working well or probably not working well.
rishtigupta marked this conversation as resolved.
Show resolved Hide resolved
pub topic_sequence_page: u64,
/// Authenticated id from Publisher's disposable token
pub publisher_id: String,
}
Expand Down Expand Up @@ -343,6 +358,9 @@ pub struct Discontinuity {
/// This discontinuity's sequence number. The next item on the stream should
/// be a value with the next sequence after this.
pub new_sequence_number: u64,

/// This discontinuity's page number.
pub new_sequence_page: u64,
}

/// How a value should be presented on a subscription stream
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/topics/topic_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl TopicClient {
cache_name: impl Into<String> + Clone,
topic: impl Into<String> + Clone,
) -> Result<Subscription, MomentoError> {
let request = SubscribeRequest::new(cache_name, topic, None);
let request = SubscribeRequest::new(cache_name, topic, None, None);
request.send(self).await
}

Expand Down
Loading