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

Expose Reader streamcut to read from given Stream Cut #22

Merged
merged 5 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#[macro_use]
extern crate cfg_if;

use crate::stream_reader_group::StreamReaderGroupConfig;
use crate::stream_reader_group::{StreamCuts, StreamReaderGroupConfig};

mod byte_stream;
mod stream_manager;
Expand Down Expand Up @@ -58,6 +58,7 @@ fn pravega_client(py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<StreamReaderGroup>()?;
m.add_class::<StreamScalingPolicy>()?;
m.add_class::<StreamRetentionPolicy>()?;
m.add_class::<StreamCuts>()?;
m.add_class::<ByteStream>()?;
let txn_exception = py.get_type::<TxnFailedException>();
txn_exception.setattr("__doc__", TXNFAILED_EXCEPTION_DOCSTRING)?;
Expand Down
42 changes: 29 additions & 13 deletions src/stream_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// http://www.apache.org/licenses/LICENSE-2.0
//

use std::collections::HashMap;
use pravega_client::event::reader_group::{StreamCutV1, StreamCutVersioned};
use crate::stream_reader_group::StreamCuts;
cfg_if! {
if #[cfg(feature = "python_binding")] {
use crate::stream_writer_transactional::StreamTxnWriter;
Expand Down Expand Up @@ -558,32 +561,45 @@
/// event.reader_group=manager.create_reader_group("rg1", "scope", "stream", true)
/// ```
///
#[pyo3(text_signature = "($self, reader_group_name, scope_name, stream_name, read_from_tail)")]
#[pyo3(text_signature = "($self, reader_group_name, scope_name, stream_name, read_from_tail, stream_cut)")]
#[args(read_from_tail = "false")]
pub fn create_reader_group(
&self,
reader_group_name: &str,
scope_name: &str,
stream_name: &str,
read_from_tail: bool,
stream_cut: Option<StreamCuts>,
) -> PyResult<StreamReaderGroup> {
let scope = Scope::from(scope_name.to_string());
let stream = Stream::from(stream_name.to_string());
let scoped_stream = ScopedStream {
scope: scope.clone(),
stream: Stream::from(stream_name.to_string()),
stream: stream.clone(),
};
let handle = self.cf.runtime_handle();
let rg_config = if read_from_tail {
// Create a reader group to read from the current TAIL/end of the Stream.
ReaderGroupConfigBuilder::default()
.read_from_tail_of_stream(scoped_stream)
.build()
} else {
// Create a reader group to read from current HEAD/start of the Stream.
ReaderGroupConfigBuilder::default()
.read_from_head_of_stream(scoped_stream)
.build()
};
let rg_config = if let Some(ref stream_cut) = stream_cut {
let mut positions = HashMap::new();

Check warning on line 582 in src/stream_manager.rs

View check run for this annotation

Codecov / codecov/patch

src/stream_manager.rs#L582

Added line #L582 was not covered by tests
// Iterate over the keys of the offset_map
for (segment_val, position) in stream_cut.stream_cuts.segment_offset_map.iter() {
let scoped_segment = ScopedSegment::new(scope.clone(), stream.clone(), Segment::from(*segment_val));
positions.insert(scoped_segment, *position);
}
let stream_cut_v1 = StreamCutV1::new(scoped_stream.clone(), positions);
// Create a reader group to read from given StreamCut .
ReaderGroupConfigBuilder::default().read_from_stream(scoped_stream.clone(), StreamCutVersioned::V1(stream_cut_v1)).build()

Check warning on line 590 in src/stream_manager.rs

View check run for this annotation

Codecov / codecov/patch

src/stream_manager.rs#L584-L590

Added lines #L584 - L590 were not covered by tests
}else if read_from_tail {
// Create a reader group to read from the current TAIL/end of the Stream.
ReaderGroupConfigBuilder::default()
.read_from_tail_of_stream(scoped_stream)
.build()

Check warning on line 595 in src/stream_manager.rs

View check run for this annotation

Codecov / codecov/patch

src/stream_manager.rs#L593-L595

Added lines #L593 - L595 were not covered by tests
} else {
// Create a reader group to read from current HEAD/start of the Stream.
ReaderGroupConfigBuilder::default()
.read_from_head_of_stream(scoped_stream)
.build()
};

let rg = handle.block_on(self.cf.create_reader_group_with_config(
reader_group_name.to_string(),
rg_config,
Expand Down
3 changes: 3 additions & 0 deletions src/stream_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@
fn data(&self) -> &[u8] {
self.value.as_slice()
}

///Return the offset
fn offset(&self) -> i64 { self.offset_in_segment }

Check warning on line 153 in src/stream_reader.rs

View check run for this annotation

Codecov / codecov/patch

src/stream_reader.rs#L153

Added line #L153 was not covered by tests
/// Returns the string representation.
fn to_str(&self) -> String {
format!("offset {:?} data :{:?}", self.offset_in_segment, self.value)
Expand Down
47 changes: 45 additions & 2 deletions src/stream_reader_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

cfg_if! {
if #[cfg(feature = "python_binding")] {
use pravega_client_shared::ScopedStream;
use pravega_client::event::reader_group::ReaderGroup;
use std::collections::HashMap;
use pyo3::prelude::*;
use pyo3::PyResult;
use pyo3::PyObjectProtocol;
Expand All @@ -22,7 +22,8 @@
use crate::stream_reader::StreamReader;
use pravega_client::event::reader_group::{ReaderGroupConfig, ReaderGroupConfigBuilder};
use pravega_client::event::reader_group_state::ReaderGroupStateError;
use pravega_client_shared::{Scope, Stream};
use pravega_client_shared::{Scope, Stream, StreamCut};
use pravega_client_shared::ScopedStream;
use pyo3::types::PyTuple;
use pyo3::exceptions;
}
Expand Down Expand Up @@ -99,6 +100,33 @@
}
}

#[cfg(feature = "python_binding")]
#[pyclass]
#[derive(Clone)]

Check warning on line 105 in src/stream_reader_group.rs

View check run for this annotation

Codecov / codecov/patch

src/stream_reader_group.rs#L105

Added line #L105 was not covered by tests
pub(crate) struct StreamCuts {
pub(crate) stream_cuts: StreamCut,
}
#[cfg(feature = "python_binding")]
#[pymethods]

Check warning on line 110 in src/stream_reader_group.rs

View check run for this annotation

Codecov / codecov/patch

src/stream_reader_group.rs#L110

Added line #L110 was not covered by tests
impl StreamCuts {

fn get_segment_offset_map(&self) -> HashMap<i64, i64> {
self.stream_cuts.segment_offset_map.clone()
}

Check warning on line 115 in src/stream_reader_group.rs

View check run for this annotation

Codecov / codecov/patch

src/stream_reader_group.rs#L113-L115

Added lines #L113 - L115 were not covered by tests

fn to_str(&self) -> String {
format!("StreamCuts: {:?}", self.stream_cuts)
}

Check warning on line 119 in src/stream_reader_group.rs

View check run for this annotation

Codecov / codecov/patch

src/stream_reader_group.rs#L117-L119

Added lines #L117 - L119 were not covered by tests
}

#[cfg(feature = "python_binding")]
#[pyproto]
impl PyObjectProtocol for StreamCuts {
fn __repr__(&self) -> PyResult<String> {
Ok(format!("StreamCuts({:?})", self.to_str()))
}

Check warning on line 127 in src/stream_reader_group.rs

View check run for this annotation

Codecov / codecov/patch

src/stream_reader_group.rs#L125-L127

Added lines #L125 - L127 were not covered by tests
}

///
/// This represents a Stream reader for a given Stream.
/// Note: A python object of StreamReader cannot be created directly without using the StreamManager.
Expand Down Expand Up @@ -179,6 +207,21 @@
}
}

/// Return the latest StreamCut from ReaderGroup.
/// Use this StreamCut in the ReaderGroupConfig to initiate reading from this streamcut.
pub fn get_streamcut(&self) -> PyResult<StreamCuts> {

let streamcut = self
.runtime_handle
.block_on(self.reader_group.get_streamcut());
info!(
"Got streamcut {:?} ", streamcut
);
Ok(StreamCuts {
stream_cuts: streamcut
})
}

Check warning on line 223 in src/stream_reader_group.rs

View check run for this annotation

Codecov / codecov/patch

src/stream_reader_group.rs#L212-L223

Added lines #L212 - L223 were not covered by tests

/// Returns the string representation.
fn to_str(&self) -> String {
format!(
Expand Down
1 change: 1 addition & 0 deletions tests/pravega_reader_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#
# http://www.apache.org/licenses/LICENSE-2.0
#
import time

import pravega_client
import random
Expand Down
Loading