use std::sync::Arc;
use std::time::Duration;
use nodedb_cluster::calvin::types::TxClass;
use nodedb_cluster::{SubmitCalvinInboxRequest, SubmitCalvinInboxResponse, TypedClusterError};
use crate::control::planner::calvin::submit::submit_local_assign;
use crate::control::state::SharedState;
pub struct RegistryCalvinSubmitInbox {
state: Arc<SharedState>,
}
impl RegistryCalvinSubmitInbox {
pub fn new(state: Arc<SharedState>) -> Self {
Self { state }
}
}
#[async_trait::async_trait]
impl nodedb_cluster::CalvinSubmitInbox for RegistryCalvinSubmitInbox {
async fn on_submit_calvin_inbox(
&self,
req: SubmitCalvinInboxRequest,
) -> SubmitCalvinInboxResponse {
let mut tx_class: TxClass = match zerompk::from_msgpack(&req.tx_class_bytes) {
Ok(tc) => tc,
Err(e) => {
return SubmitCalvinInboxResponse {
inbox_seq: 0,
epoch: 0,
position: 0,
participants: 0,
error: Some(TypedClusterError::Internal {
code: 0,
message: format!("calvin-inbox: failed to decode TxClass: {e}"),
}),
};
}
};
tx_class.restore_derived();
let timeout = Duration::from_millis(req.deadline_remaining_ms.max(1));
match submit_local_assign(&self.state, tx_class, timeout).await {
Ok(a) => SubmitCalvinInboxResponse {
inbox_seq: a.inbox_seq,
epoch: a.epoch,
position: a.position,
participants: a.participants as u64,
error: None,
},
Err(e) => SubmitCalvinInboxResponse {
inbox_seq: 0,
epoch: 0,
position: 0,
participants: 0,
error: Some(TypedClusterError::Internal {
code: 0,
message: format!("calvin-inbox local submit-and-assign failed: {e}"),
}),
},
}
}
}