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

Introduce APIs for list_recordings and update_metadata #8223

Merged
merged 8 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5779,6 +5779,7 @@ dependencies = [
"prost",
"re_arrow2",
"re_dataframe",
"re_log",
"re_log_types",
"thiserror",
"tonic",
Expand Down Expand Up @@ -6971,6 +6972,7 @@ dependencies = [
"re_web_viewer_server",
"re_ws_comms",
"tokio",
"tokio-stream",
"tonic",
"url",
"uuid",
Expand Down
1 change: 1 addition & 0 deletions crates/store/re_protos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ version.workspace = true
[dependencies]
re_log_types.workspace = true
re_dataframe.workspace = true
re_log.workspace = true

# External
arrow2 = { workspace = true, features = ["io_ipc"] }
Expand Down
41 changes: 41 additions & 0 deletions examples/python/remote/metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Script to show how to interact with a remote storage node via python APIs."""

from __future__ import annotations

import argparse

import polars as pl
import pyarrow as pa
import rerun as rr

if __name__ == "__main__":
parser = argparse.ArgumentParser()

subparsers = parser.add_subparsers(dest="subcommand")

print_cmd = subparsers.add_parser("print", help="Print everything")
update_cmd = subparsers.add_parser("update", help="Update metadata for a recording")

update_cmd.add_argument("id", help="ID of the recording to update")
update_cmd.add_argument("key", help="Key of the metadata to update")
update_cmd.add_argument("value", help="Value of the metadata to update")

args = parser.parse_args()

# Register the new rrd
conn = rr.remote.connect("http://0.0.0.0:51234")

catalog = pl.from_arrow(conn.list_recordings())

if args.subcommand == "print":
print(catalog)

if args.subcommand == "update":
id = catalog.filter(catalog["id"].str.starts_with(args.id)).select(pl.first("id")).item()

if id is None:
print("ID not found")
exit(1)
print(f"Updating metadata for {id}")

conn.update_metadata(id, {args.key: pa.array([args.value])})
231 changes: 213 additions & 18 deletions pixi.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ python = "=3.11"
[feature.examples-common.pypi-dependencies]
# External deps
jupyter = ">=1.0"
polars = ">=0.12.0"

segment-anything = { git = "https://github.com/facebookresearch/segment-anything.git" }
mesh-to-sdf = { git = "https://github.com/marian42/mesh_to_sdf.git" }
Expand Down
4 changes: 3 additions & 1 deletion rerun_py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ name = "rerun_bindings" # name of the .so library that the Python module will im


[features]
default = ["extension-module"]
default = ["extension-module", "remote"]
jleibs marked this conversation as resolved.
Show resolved Hide resolved

## Extra features that aren't ready to be included in release builds yet.
extra = ["pypi", "remote"]
Expand All @@ -40,6 +40,7 @@ remote = [
"dep:re_protos",
"dep:re_ws_comms",
"dep:tokio",
"dep:tokio-stream",
"dep:tonic",
"dep:url",
]
Expand Down Expand Up @@ -87,6 +88,7 @@ uuid.workspace = true
object_store = { workspace = true, optional = true, features = ["aws"] }
re_protos = { workspace = true, optional = true }
tokio = { workspace = true, optional = true }
tokio-stream = { workspace = true, optional = true }
# Not used yet, but we will need it when we start streaming data
#tokio-stream = { workspace = true, optional = true }
tonic = { workspace = true, default-features = false, features = [
Expand Down
4 changes: 2 additions & 2 deletions rerun_py/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,8 +565,8 @@ impl PySchema {
/// to retrieve the data.
#[pyclass(name = "Recording")]
pub struct PyRecording {
store: ChunkStoreHandle,
cache: re_dataframe::QueryCacheHandle,
pub(crate) store: ChunkStoreHandle,
pub(crate) cache: re_dataframe::QueryCacheHandle,
}

/// A view of a recording restricted to a given index, containing a specific set of entities and components.
Expand Down
170 changes: 159 additions & 11 deletions rerun_py/src/remote.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
#![allow(unsafe_op_in_unsafe_fn)]
use arrow::{array::ArrayData, pyarrow::PyArrowType};
use arrow::{
array::{ArrayData, RecordBatch, RecordBatchIterator, RecordBatchReader},
datatypes::Schema,
pyarrow::PyArrowType,
};
// False positive due to #[pyfunction] macro
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyDict, Bound, PyResult};
use re_chunk::TransportChunk;
use re_protos::v0::{
storage_node_client::StorageNodeClient, EncoderVersion, ListRecordingsRequest,
RecordingMetadata, RecordingType, RegisterRecordingRequest,
use re_chunk::{Chunk, TransportChunk};
use re_chunk_store::ChunkStore;
use re_dataframe::ChunkStoreHandle;
use re_log_types::{StoreInfo, StoreSource};
use re_protos::{
codec::decode,
v0::{
storage_node_client::StorageNodeClient, EncoderVersion, FetchRecordingRequest,
ListRecordingsRequest, RecordingId, RecordingMetadata, RecordingType,
RegisterRecordingRequest, UpdateRecordingMetadataRequest,
},
};
use re_sdk::{ApplicationId, StoreId, StoreKind, Time};

use crate::dataframe::PyRecording;

/// Register the `rerun.remote` module.
pub(crate) fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
Expand Down Expand Up @@ -52,8 +66,9 @@ pub struct PyConnection {
#[pymethods]
impl PyConnection {
/// List all recordings registered with the node.
fn list_recordings(&mut self) -> PyResult<Vec<PyRecordingMetadata>> {
self.runtime.block_on(async {
fn list_recordings(&mut self) -> PyResult<PyArrowType<Box<dyn RecordBatchReader + Send>>> {
let reader = self.runtime.block_on(async {
// TODO(jleibs): Support column projection
let request = ListRecordingsRequest {
column_projection: None,
};
Expand All @@ -64,13 +79,33 @@ impl PyConnection {
.await
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?;

Ok(resp
let transport_chunks = resp
.into_inner()
.recordings
.into_iter()
.map(|recording| PyRecordingMetadata { info: recording })
.collect())
})
.map(|recording| recording.data())
.collect::<Result<Vec<_>, _>>()
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?;

let record_batches: Vec<Result<RecordBatch, arrow::error::ArrowError>> =
transport_chunks
.into_iter()
.map(|tc| tc.try_to_arrow_record_batch())
.collect();

// TODO(jleibs): surfacing this schema is awkward. This should be more explicit in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we expose schema as top level field for get/list metadata, even though it's in the transport chunk?

I guess for the Query (i.e. data API) we should (as initially discussed) strive to go the opposite way and only include the schema in the first message of the stream?

// the gRPC APIs somehow.
let schema = record_batches
.first()
.and_then(|batch| batch.as_ref().ok().map(|batch| batch.schema()))
.unwrap_or(std::sync::Arc::new(Schema::empty()));

let reader = RecordBatchIterator::new(record_batches, schema);

Ok::<_, PyErr>(reader)
})?;

Ok(PyArrowType(Box::new(reader)))
}

/// Register a recording along with some metadata
Expand Down Expand Up @@ -146,6 +181,119 @@ impl PyConnection {
Ok(recording_id)
})
}

/// Updates the metadata for a recording.
#[pyo3(signature = (
id,
metadata
))]
fn update_metadata(&mut self, id: &str, metadata: &Bound<'_, PyDict>) -> PyResult<()> {
self.runtime.block_on(async {
let (schema, data): (
Vec<arrow2::datatypes::Field>,
Vec<Box<dyn arrow2::array::Array>>,
) = metadata
.iter()
.map(|(key, value)| {
let key = key.to_string();
let value = value.extract::<MetadataLike>()?;
let value_array = value.to_arrow2()?;
let field =
arrow2::datatypes::Field::new(key, value_array.data_type().clone(), true);
Ok((field, value_array))
})
.collect::<PyResult<Vec<_>>>()?
.into_iter()
.unzip();

let schema = arrow2::datatypes::Schema::from(schema);

let data = arrow2::chunk::Chunk::new(data);

let metadata_tc = TransportChunk {
schema: schema.clone(),
data,
};

let metadata = RecordingMetadata::try_from(EncoderVersion::V0, &metadata_tc)
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?;

let request = UpdateRecordingMetadataRequest {
recording_id: Some(RecordingId { id: id.to_owned() }),
metadata: Some(metadata),
};

self.client
.update_recording_metadata(request)
.await
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?;

Ok(())
})
}

/// Opens a recording for query and analysis.
#[pyo3(signature = (
id,
))]
fn open_recording(&mut self, id: &str) -> PyResult<PyRecording> {
use tokio_stream::StreamExt as _;
let store = self.runtime.block_on(async {
let mut resp = self
.client
.fetch_recording(FetchRecordingRequest {
recording_id: Some(RecordingId { id: id.to_owned() }),
})
.await
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?
.into_inner();

// TODO(jleibs): Does this come from RDP?
let store_id = StoreId::from_string(StoreKind::Recording, id.to_owned());

let store_info = StoreInfo {
application_id: ApplicationId::from("rerun_data_platform"),
store_id: store_id.clone(),
cloned_from: None,
is_official_example: false,
started: Time::now(),
store_source: StoreSource::Unknown,
store_version: None,
};

let mut store = ChunkStore::new(store_id, Default::default());
store.set_info(store_info);

while let Some(result) = resp.next().await {
let response = result.map_err(|err| PyRuntimeError::new_err(err.to_string()))?;
let tc = decode(EncoderVersion::V0, &response.payload)
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?;

let Some(tc) = tc else {
return Err(PyRuntimeError::new_err("Stream error"));
};

let chunk = Chunk::from_transport(&tc)
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?;

store
.insert_chunk(&std::sync::Arc::new(chunk))
.map_err(|err| PyRuntimeError::new_err(err.to_string()))?;
}

Ok(store)
})?;

let handle = ChunkStoreHandle::new(store);

let cache =
re_dataframe::QueryCacheHandle::new(re_dataframe::QueryCache::new(handle.clone()));

Ok(PyRecording {
store: handle,
cache,
})
}
}

/// A type alias for metadata.
Expand Down
Loading