use std::sync::Arc;
use std::time::Duration;
use nodedb_cluster::calvin::types::TxClass;
use nodedb_cluster::{SubmitCalvinTxnRequest, SubmitCalvinTxnResponse, TypedClusterError};
use crate::control::planner::calvin::submit_and_await_calvin_with_timeout;
use crate::control::state::SharedState;
pub struct RegistryCalvinSubmit {
state: Arc<SharedState>,
}
impl RegistryCalvinSubmit {
pub fn new(state: Arc<SharedState>) -> Self {
Self { state }
}
}
#[async_trait::async_trait]
impl nodedb_cluster::CalvinSubmit for RegistryCalvinSubmit {
async fn on_submit_calvin_txn(&self, req: SubmitCalvinTxnRequest) -> SubmitCalvinTxnResponse {
let mut tx_class: TxClass = match zerompk::from_msgpack(&req.tx_class_bytes) {
Ok(tc) => tc,
Err(e) => {
return SubmitCalvinTxnResponse {
error: Some(TypedClusterError::Internal {
code: 0,
message: format!("calvin-submit: failed to decode TxClass: {e}"),
}),
payload_bytes: None,
};
}
};
tx_class.restore_derived();
let timeout = Duration::from_millis(req.deadline_remaining_ms.max(1));
match submit_and_await_calvin_with_timeout(&self.state, tx_class, timeout).await {
Ok(applied) => SubmitCalvinTxnResponse {
error: None,
payload_bytes: applied.map(|r| r.payload.to_vec()),
},
Err(e) => SubmitCalvinTxnResponse {
error: Some(TypedClusterError::Internal {
code: 0,
message: format!("calvin-submit local submit-and-await failed: {e}"),
}),
payload_bytes: None,
},
}
}
}