-
Notifications
You must be signed in to change notification settings - Fork 12
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
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
27c0701
Migrate storage gRPC error codes to tonic::Status
sitaowang1998 a1ee868
Merge branch 'main' into storage-grpc-migration
sitaowang1998 b98b085
Rename map_inbound_status to inbound_status_to_error to match main's …
sitaowang1998 b241c34
Rename map_inbound_status and map_liveness_status to match main's sta…
sitaowang1998 429fdbb
Merge branch 'storage-grpc-migration' of github.com:sitaowang1998/spi…
sitaowang1998 25fc42d
Merge origin/main into storage-grpc-migration
sitaowang1998 86fe70a
Add project skeleton
sitaowang1998 11381cd
Merge branch 'storage-grpc-migration'
sitaowang1998 697c49a
Add error
sitaowang1998 24fb358
Add grpc client connection
sitaowang1998 e0cf5da
Add job orchestration client
sitaowang1998 391ed65
Add resource group grpc client
sitaowang1998 44c1b81
Add tests
sitaowang1998 40c55a9
Merge origin/main into client
sitaowang1998 add524c
Merge remote-tracking branch 'origin/main' into client
sitaowang1998 ec89ff5
Address comment
sitaowang1998 3a3f712
docs(spider-client): Document Unauthenticated error for cancel_job
sitaowang1998 ac3f9da
Address comment
sitaowang1998 4e1d1d8
Merge branch 'client' of github.com:sitaowang1998/spider into client
sitaowang1998 ccdb787
Bug fix
sitaowang1998 a342b49
Remove mock test
sitaowang1998 dcbefaa
Refactor structure and visibility
sitaowang1998 16b7a0e
Merge branch 'main' into client
sitaowang1998 6b7207e
Merge branch 'main' into client
sitaowang1998 54b6941
Merge branch 'main' into client
sitaowang1998 dc83db0
Review and polish.
LinZhihao-723 e3cc8d8
Polish mod-level docstring for grpc clients.
LinZhihao-723 78d7726
Fix docstring.
LinZhihao-723 252fbcd
Fix linter error.
LinZhihao-723 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| //! [`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, | ||
| grpc::{job::JobOrchestrationClient, resource_group::ResourceGroupManagementClient}, | ||
| }; | ||
|
|
||
| /// User-facing client for the Spider storage gRPC services. | ||
| #[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. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// A new [`SpiderClient`] connected to `endpoint` on success. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// | ||
| /// * [`ClientError::Transport`] if tonic cannot create or connect to the endpoint. | ||
| 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. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The [`JobId`] the storage server assigned to the registered job on success. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// | ||
| /// * [`ClientError::Serialization`] if the task graph or inputs cannot be serialized or | ||
| /// compressed. | ||
| /// * [`ClientError::InvalidArgument`] if the storage server rejects the task graph or inputs. | ||
| /// * [`ClientError::Unauthenticated`] if the resource group is unknown or unauthorized. | ||
| /// * [`ClientError::Transport`] if the gRPC transport fails or the connection is lost. | ||
| /// * [`ClientError::Server`] for any other server-reported error. | ||
| 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. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The job's [`JobState`] after the start request is accepted on success. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// | ||
| /// * [`ClientError::JobNotFound`] if no job with `job_id` exists. | ||
| /// * [`ClientError::InvalidJobState`] if the job is not in a state that allows starting. | ||
| /// * [`ClientError::UnspecifiedJobState`] if the server reports an unspecified job state. | ||
| /// * [`ClientError::Transport`] if the gRPC transport fails, the connection is lost, or the | ||
| /// server reports an unrecognized job state. | ||
| /// * [`ClientError::Server`] for any other server-reported error. | ||
| pub async fn start_job(&self, job_id: JobId) -> Result<JobState, ClientError> { | ||
| self.job_orchestration.start_job(job_id).await | ||
| } | ||
|
|
||
| /// Cancels a job. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The job's [`JobState`] after the cancellation request is accepted on success. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// | ||
| /// * [`ClientError::JobNotFound`] if no job with `job_id` exists. | ||
| /// * [`ClientError::InvalidJobState`] if the job is not in a state that allows cancellation. | ||
| /// * [`ClientError::UnspecifiedJobState`] if the server reports an unspecified job state. | ||
| /// * [`ClientError::Transport`] if the gRPC transport fails, the connection is lost, or the | ||
| /// server reports an unrecognized job state. | ||
| /// * [`ClientError::Server`] for any other server-reported error. | ||
| 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. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The job's current [`JobState`] on success. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// | ||
| /// * [`ClientError::JobNotFound`] if no job with `job_id` exists. | ||
| /// * [`ClientError::UnspecifiedJobState`] if the server reports an unspecified job state. | ||
| /// * [`ClientError::Transport`] if the gRPC transport fails, the connection is lost, or the | ||
| /// server reports an unrecognized job state. | ||
| /// * [`ClientError::Server`] for any other server-reported error. | ||
| 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. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The job's outputs, deserialized from the storage wire format into opaque msgpack payloads, | ||
| /// on success. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// | ||
| /// * [`ClientError::JobNotFound`] if no job with `job_id` exists. | ||
| /// * [`ClientError::InvalidJobState`] if the job has not yet succeeded. | ||
| /// * [`ClientError::Deserialization`] if the returned outputs cannot be decompressed or | ||
| /// unframed. | ||
| /// * [`ClientError::Transport`] if the gRPC transport fails or the connection is lost. | ||
| /// * [`ClientError::Server`] for any other server-reported error. | ||
| 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. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The job's error message on success. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// | ||
| /// * [`ClientError::JobNotFound`] if no job with `job_id` exists. | ||
| /// * [`ClientError::InvalidJobState`] if the job has not yet failed. | ||
| /// * [`ClientError::Transport`] if the gRPC transport fails or the connection is lost. | ||
| /// * [`ClientError::Server`] for any other server-reported 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. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// The [`ResourceGroupId`] the storage server assigned to the registered resource group on | ||
| /// success. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// | ||
| /// * [`ClientError::InvalidArgument`] if the storage server rejects the request as invalid. | ||
| /// * [`ClientError::Unauthenticated`] if the resource group is unknown or the password is | ||
| /// invalid. | ||
| /// * [`ClientError::Transport`] if the gRPC transport fails or the connection is lost. | ||
| /// * [`ClientError::Server`] for any other server-reported error. | ||
| 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. | ||
| /// | ||
| /// # Returns | ||
| /// | ||
| /// `Ok(())` on success. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns an error if: | ||
| /// | ||
| /// * [`ClientError::InvalidArgument`] if the storage server rejects the request as invalid. | ||
| /// * [`ClientError::Unauthenticated`] if the resource group is unknown or the password is | ||
| /// invalid. | ||
| /// * [`ClientError::Transport`] if the gRPC transport fails or the connection is lost. | ||
| /// * [`ClientError::Server`] for any other server-reported error. | ||
| 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 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| //! Client error type for the Spider storage gRPC services. | ||
|
|
||
| /// 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")] | ||
| JobNotFound, | ||
|
|
||
| /// 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 unspecified")] | ||
| UnspecifiedJobState, | ||
| } | ||
|
|
||
| /// Converts a displayable transport-layer error into [`ClientError::Transport`]. | ||
| /// | ||
| /// # 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()) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.