Skip to main content

rocketmq_protocol/protocol/header/
check_transaction_state_response_header.rs

1// Copyright 2023 The RocketMQ Rust Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use cheetah_string::CheetahString;
16use rocketmq_macros::RequestHeaderCodec;
17use serde::Deserialize;
18use serde::Serialize;
19
20#[derive(Clone, Debug, Default, Deserialize, Serialize, RequestHeaderCodec)]
21#[serde(rename_all = "camelCase")]
22#[header(
23    type_id = "rocketmq_protocol::protocol::header::check_transaction_state_response_header::CheckTransactionStateResponseHeader",
24    java_class = "org.apache.rocketmq.remoting.protocol.header.CheckTransactionStateResponseHeader",
25    validate = "Self::validate"
26)]
27pub struct CheckTransactionStateResponseHeader {
28    #[header(required)]
29    pub producer_group: CheetahString,
30    #[header(required)]
31    pub tran_state_table_offset: i64,
32    #[header(required)]
33    pub commit_log_offset: i64,
34    #[header(required)]
35    pub commit_or_rollback: i32,
36}
37
38impl CheckTransactionStateResponseHeader {
39    fn validate(&self) -> Result<(), crate::ProtocolContractViolation> {
40        use rocketmq_model::common::sys_flag::message_sys_flag::MessageSysFlag;
41
42        match self.commit_or_rollback {
43            MessageSysFlag::TRANSACTION_COMMIT_TYPE | MessageSysFlag::TRANSACTION_ROLLBACK_TYPE => Ok(()),
44            _ => Err(crate::ProtocolContractViolation::Validation {
45                header: "rocketmq_protocol::protocol::header::check_transaction_state_response_header::CheckTransactionStateResponseHeader",
46                rule: "commit_or_rollback",
47            }),
48        }
49    }
50}
51
52#[cfg(test)]
53mod tests {
54    use std::collections::HashMap;
55
56    use super::*;
57    use crate::protocol::command_custom_header::FromMap;
58
59    fn map_with_state(state: i32) -> HashMap<CheetahString, CheetahString> {
60        HashMap::from([
61            ("producerGroup".into(), "group-a".into()),
62            ("tranStateTableOffset".into(), "1".into()),
63            ("commitLogOffset".into(), "2".into()),
64            ("commitOrRollback".into(), state.to_string().into()),
65        ])
66    }
67
68    #[test]
69    fn accepts_commit_and_rollback_only() {
70        use rocketmq_model::common::sys_flag::message_sys_flag::MessageSysFlag;
71
72        for state in [
73            MessageSysFlag::TRANSACTION_COMMIT_TYPE,
74            MessageSysFlag::TRANSACTION_ROLLBACK_TYPE,
75        ] {
76            assert!(<CheckTransactionStateResponseHeader as FromMap>::from(&map_with_state(state)).is_ok());
77        }
78        assert!(<CheckTransactionStateResponseHeader as FromMap>::from(&map_with_state(
79            MessageSysFlag::TRANSACTION_NOT_TYPE
80        ))
81        .is_err());
82    }
83}