Repository navigation
feat(spider-client): Add user-facing client library for job orchestration and resource group management. #363
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
Changes from 21 commits
27c0701
a1ee868
b98b085
b241c34
429fdbb
25fc42d
86fe70a
11381cd
697c49a
24fb358
e0cf5da
391ed65
44c1b81
40c55a9
add524c
ec89ff5
3a3f712
ac3f9da
4e1d1d8
ccdb787
a342b49
dcbefaa
16b7a0e
6b7207e
54b6941
dc83db0
e3cc8d8
78d7726
252fbcd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [package] | ||
| name = "spider-client" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [lib] | ||
| name = "spider_client" | ||
| path = "src/lib.rs" | ||
|
|
||
| [dependencies] | ||
| spider-core = { path = "../spider-core" } | ||
| spider-proto-rust = { path = "../spider-proto-rust" } | ||
| spider-utils = { path = "../spider-utils" } | ||
| thiserror = "2.0.18" | ||
| tokio = { version = "1.52.3", features = ["macros"] } | ||
| tonic = "0.14.6" |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,185 @@ | ||||
| //! [`SpiderClient`] — the top-level handle holding the gRPC connection pools. | ||||
|
|
||||
| use std::num::NonZeroUsize; | ||||
|
|
||||
| use spider_core::{ | ||||
| job::JobState, | ||||
| task::TaskGraph, | ||||
| types::{ | ||||
| id::{JobId, ResourceGroupId}, | ||||
| io::{TaskInput, TaskOutput}, | ||||
| }, | ||||
| }; | ||||
| use tonic::transport::Endpoint; | ||||
|
|
||||
| use crate::{ | ||||
| error::ClientError, | ||||
| job::JobOrchestrationClient, | ||||
| resource_group::ResourceGroupManagementClient, | ||||
| }; | ||||
|
|
||||
| /// User-facing client for the Spider storage gRPC services. | ||||
| /// | ||||
| /// Wraps a [`JobOrchestrationClient`] and a [`ResourceGroupManagementClient`] against the same | ||||
| /// storage endpoint, so callers who need both job-lifecycle and resource-group operations get a | ||||
| /// single handle and one [`SpiderClient::connect`] call. Callers who need only one service may | ||||
| /// construct the inner client directly. | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the actual user-facing type. I don't think user needs to know about the underlying client types, right? |
||||
| #[derive(Debug, Clone)] | ||||
| pub struct SpiderClient { | ||||
| job_orchestration: JobOrchestrationClient, | ||||
| resource_group: ResourceGroupManagementClient, | ||||
| } | ||||
|
|
||||
| impl SpiderClient { | ||||
| /// Connects pools of `pool_size` connections to the storage gRPC endpoint. | ||||
| /// | ||||
| /// Both the job-orchestration and resource-group-management services are reached through the | ||||
| /// same `endpoint`. | ||||
| /// | ||||
| /// # Returns | ||||
| /// | ||||
| /// A new [`SpiderClient`] connected to `endpoint` on success. | ||||
| /// | ||||
| /// # Errors | ||||
| /// | ||||
| /// Returns [`ClientError::Transport`] if tonic cannot establish a connection to `endpoint`. | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Read this again and tell me if this matches the docstring format, lol
|
||||
| pub async fn connect(endpoint: Endpoint, pool_size: NonZeroUsize) -> Result<Self, ClientError> { | ||||
| let (job_orchestration, resource_group) = tokio::try_join!( | ||||
| JobOrchestrationClient::connect(endpoint.clone(), pool_size), | ||||
| ResourceGroupManagementClient::connect(endpoint, pool_size), | ||||
| )?; | ||||
|
|
||||
| Ok(Self { | ||||
| job_orchestration, | ||||
| resource_group, | ||||
| }) | ||||
| } | ||||
|
|
||||
| /// Serializes and zstd-compresses the task graph and inputs, registers the job, and returns | ||||
| /// its assigned id. Delegates to [`JobOrchestrationClient::submit_job`]. | ||||
| /// | ||||
| /// # Returns | ||||
| /// | ||||
| /// The [`JobId`] the storage server assigned to the registered job on success. | ||||
| /// | ||||
| /// # Errors | ||||
| /// | ||||
| /// See [`JobOrchestrationClient::submit_job`]. | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
| pub async fn submit_job( | ||||
| &self, | ||||
| resource_group_id: ResourceGroupId, | ||||
| task_graph: &TaskGraph, | ||||
| inputs: Vec<TaskInput>, | ||||
| ) -> Result<JobId, ClientError> { | ||||
| self.job_orchestration | ||||
| .submit_job(resource_group_id, task_graph, inputs) | ||||
| .await | ||||
| } | ||||
|
|
||||
| /// Starts a registered job. Delegates to [`JobOrchestrationClient::start_job`]. | ||||
| /// | ||||
| /// # Returns | ||||
| /// | ||||
| /// The job's [`JobState`] after the start request is accepted on success. | ||||
| /// | ||||
| /// # Errors | ||||
| /// | ||||
| /// See [`JobOrchestrationClient::start_job`]. | ||||
| pub async fn start_job(&self, job_id: JobId) -> Result<JobState, ClientError> { | ||||
| self.job_orchestration.start_job(job_id).await | ||||
| } | ||||
|
|
||||
| /// Cancels a job. Delegates to [`JobOrchestrationClient::cancel_job`]. | ||||
| /// | ||||
| /// # Returns | ||||
| /// | ||||
| /// The job's [`JobState`] after the cancellation request is accepted on success. | ||||
| /// | ||||
| /// # Errors | ||||
| /// | ||||
| /// See [`JobOrchestrationClient::cancel_job`]. | ||||
| pub async fn cancel_job(&self, job_id: JobId) -> Result<JobState, ClientError> { | ||||
| self.job_orchestration.cancel_job(job_id).await | ||||
| } | ||||
|
|
||||
| /// Gets the current state of a job. Delegates to [`JobOrchestrationClient::get_job_state`]. | ||||
| /// | ||||
| /// # Returns | ||||
| /// | ||||
| /// The job's current [`JobState`] on success. | ||||
| /// | ||||
| /// # Errors | ||||
| /// | ||||
| /// See [`JobOrchestrationClient::get_job_state`]. | ||||
| pub async fn get_job_state(&self, job_id: JobId) -> Result<JobState, ClientError> { | ||||
| self.job_orchestration.get_job_state(job_id).await | ||||
| } | ||||
|
|
||||
| /// Gets a job's task outputs. Delegates to [`JobOrchestrationClient::get_job_outputs`]. | ||||
| /// | ||||
| /// # Returns | ||||
| /// | ||||
| /// The job's outputs, deserialized from the storage wire format into opaque msgpack payloads, | ||||
| /// on success. | ||||
| /// | ||||
| /// # Errors | ||||
| /// | ||||
| /// See [`JobOrchestrationClient::get_job_outputs`]. | ||||
| pub async fn get_job_outputs(&self, job_id: JobId) -> Result<Vec<TaskOutput>, ClientError> { | ||||
| self.job_orchestration.get_job_outputs(job_id).await | ||||
| } | ||||
|
|
||||
| /// Gets a job's error message. Delegates to [`JobOrchestrationClient::get_job_error`]. | ||||
| /// | ||||
| /// # Returns | ||||
| /// | ||||
| /// The job's error message on success. | ||||
| /// | ||||
| /// # Errors | ||||
| /// | ||||
| /// See [`JobOrchestrationClient::get_job_error`]. | ||||
| pub async fn get_job_error(&self, job_id: JobId) -> Result<String, ClientError> { | ||||
| self.job_orchestration.get_job_error(job_id).await | ||||
| } | ||||
|
|
||||
| /// Registers an external resource group and returns its server-assigned id. Delegates to | ||||
| /// [`ResourceGroupManagementClient::add_resource_group`]. | ||||
| /// | ||||
| /// # Returns | ||||
| /// | ||||
| /// The [`ResourceGroupId`] the storage server assigned to the registered resource group on | ||||
| /// success. | ||||
| /// | ||||
| /// # Errors | ||||
| /// | ||||
| /// See [`ResourceGroupManagementClient::add_resource_group`]. | ||||
| pub async fn add_resource_group( | ||||
| &self, | ||||
| external_resource_group_id: String, | ||||
| password: Vec<u8>, | ||||
| ) -> Result<ResourceGroupId, ClientError> { | ||||
| self.resource_group | ||||
| .add_resource_group(external_resource_group_id, password) | ||||
| .await | ||||
| } | ||||
|
|
||||
| /// Verifies a resource group's password. Delegates to | ||||
| /// [`ResourceGroupManagementClient::verify_resource_group`]. | ||||
| /// | ||||
| /// # Returns | ||||
| /// | ||||
| /// `Ok(())` on success. | ||||
| /// | ||||
| /// # Errors | ||||
| /// | ||||
| /// See [`ResourceGroupManagementClient::verify_resource_group`]. | ||||
| pub async fn verify_resource_group( | ||||
| &self, | ||||
| resource_group_id: ResourceGroupId, | ||||
| password: Vec<u8>, | ||||
| ) -> Result<(), ClientError> { | ||||
| self.resource_group | ||||
| .verify_resource_group(resource_group_id, password) | ||||
| .await | ||||
| } | ||||
| } | ||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| //! Client error type for the Spider storage gRPC services. | ||
| //! | ||
| //! [`ClientError`] is the single error type returned by [`crate::client::SpiderClient`] methods. | ||
| //! It folds transport failures, tonic error status, and payload serialization and | ||
| //! deserialization failures into one concrete enum. See [`ClientError`] for the variants and when | ||
| //! each arises. | ||
|
|
||
| use spider_core::types::id::JobId; | ||
| use tonic::{Code, Status}; | ||
|
|
||
| /// Errors returned by [`crate::client::SpiderClient`] operations. | ||
| #[derive(Debug, thiserror::Error)] | ||
| pub enum ClientError { | ||
| /// The gRPC transport failed or the connection was lost or unestablished. | ||
| #[error("transport error: {0}")] | ||
| Transport(String), | ||
|
|
||
| /// The storage server returned an otherwise-uncategorized error. | ||
| #[error("storage server error: {0}")] | ||
| Server(String), | ||
|
|
||
| /// No job with the requested identifier exists. | ||
| #[error("job not found: {0:?}")] | ||
| JobNotFound(JobId), | ||
|
|
||
| /// The job is not in a state that allows the requested operation. | ||
| #[error("invalid job state: {0}")] | ||
| InvalidJobState(String), | ||
|
sitaowang1998 marked this conversation as resolved.
|
||
|
|
||
| /// The storage server rejected the request as invalid. | ||
| #[error("invalid argument: {0}")] | ||
| InvalidArgument(String), | ||
|
|
||
| /// The resource group or password was rejected. | ||
| #[error("unauthenticated: {0}")] | ||
| Unauthenticated(String), | ||
|
|
||
| /// A failure to serialize, compress, or wire-frame a request payload. | ||
| #[error("serialization error: {0}")] | ||
| Serialization(String), | ||
|
|
||
| /// A failure to deserialize, decompress, or wire-frame a response payload. | ||
| #[error("deserialization error: {0}")] | ||
| Deserialization(String), | ||
|
|
||
| /// The server returned an unspecified job state that has no core representation. | ||
| #[error("job state is unspecified")] | ||
| UnspecifiedJobState, | ||
| } | ||
|
|
||
| /// Maps a job-orchestration gRPC [`Status`] to a [`ClientError`]. | ||
| /// | ||
| /// `job_id` is the job the call targeted; it is attached to [`ClientError::JobNotFound`] when the | ||
| /// server reports `NOT_FOUND`. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The [`ClientError`] for `status`'s code: | ||
| /// | ||
| /// * [`ClientError::JobNotFound`] for `NOT_FOUND`. | ||
| /// * [`ClientError::InvalidJobState`] for `FAILED_PRECONDITION`. | ||
| /// * [`ClientError::InvalidArgument`] for `INVALID_ARGUMENT`. | ||
| /// * [`ClientError::Unauthenticated`] for `UNAUTHENTICATED`. | ||
| /// * [`ClientError::Transport`] for `UNAVAILABLE` (a lost or unestablished connection). | ||
| /// * [`ClientError::Server`] for any other code. | ||
| pub(crate) fn job_status_to_error(status: &Status, job_id: JobId) -> ClientError { | ||
| match status.code() { | ||
| Code::NotFound => ClientError::JobNotFound(job_id), | ||
| Code::FailedPrecondition => ClientError::InvalidJobState(status.message().to_owned()), | ||
| Code::InvalidArgument => ClientError::InvalidArgument(status.message().to_owned()), | ||
| Code::Unauthenticated => ClientError::Unauthenticated(status.message().to_owned()), | ||
| Code::Unavailable => ClientError::Transport(status.message().to_owned()), | ||
| _ => ClientError::Server(status.message().to_owned()), | ||
| } | ||
| } | ||
|
|
||
| /// Maps a resource-group-management gRPC [`Status`] to a [`ClientError`]. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The [`ClientError`] for `status`'s code: | ||
| /// | ||
| /// * [`ClientError::InvalidArgument`] for `INVALID_ARGUMENT`. | ||
| /// * [`ClientError::Unauthenticated`] for `UNAUTHENTICATED` (an unknown or unauthorized resource | ||
| /// group, or an invalid password). | ||
| /// * [`ClientError::Transport`] for `UNAVAILABLE` (a lost or unestablished connection). | ||
| /// * [`ClientError::Server`] for any other code (including `NOT_FOUND`, `FAILED_PRECONDITION`, and | ||
| /// `INTERNAL`). | ||
| pub(crate) fn resource_group_status_to_error(status: &Status) -> ClientError { | ||
| match status.code() { | ||
| Code::InvalidArgument => ClientError::InvalidArgument(status.message().to_owned()), | ||
| Code::Unauthenticated => ClientError::Unauthenticated(status.message().to_owned()), | ||
| Code::Unavailable => ClientError::Transport(status.message().to_owned()), | ||
| _ => ClientError::Server(status.message().to_owned()), | ||
| } | ||
| } | ||
|
|
||
| /// Converts a displayable transport-layer error into [`ClientError::Transport`]. | ||
| /// | ||
| /// Used by the `connect` methods of [`crate::client::SpiderClient`], | ||
| /// [`crate::job::JobOrchestrationClient`], | ||
| /// and [`crate::resource_group::ResourceGroupManagementClient`] to fold `spider_utils::grpc::Error` | ||
| /// into [`ClientError`]. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A [`ClientError::Transport`] containing `error`'s display string. | ||
| pub(crate) fn to_transport_error(error: impl std::fmt::Display) -> ClientError { | ||
| ClientError::Transport(error.to_string()) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will probably rewrite the docstring in this mod myself since it's user-facing.
But you should really review the docstrings carefully. I don't want to waste more time on catching these types of errors.