Skip to main content

tansu_storage/service/txn/
add_partitions.rs

1// Copyright ⓒ 2024-2025 Peter Morgan <peter.james.morgan@gmail.com>
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 rama::{Context, Service};
16use tansu_sans_io::{AddPartitionsToTxnRequest, AddPartitionsToTxnResponse, ApiKey, ErrorCode};
17use tracing::instrument;
18
19use crate::{Error, Result, Storage, TxnAddPartitionsRequest, TxnAddPartitionsResponse};
20
21/// A [`Service`] using [`Storage`] as [`Context`] taking [`AddPartitionsToTxnRequest`] returning [`AddPartitionsToTxnResponse`].
22#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
23pub struct AddPartitionService;
24
25impl ApiKey for AddPartitionService {
26    const KEY: i16 = AddPartitionsToTxnRequest::KEY;
27}
28
29impl<G> Service<G, AddPartitionsToTxnRequest> for AddPartitionService
30where
31    G: Storage,
32{
33    type Response = AddPartitionsToTxnResponse;
34    type Error = Error;
35
36    #[instrument(skip(ctx, req))]
37    async fn serve(
38        &self,
39        ctx: Context<G>,
40        req: AddPartitionsToTxnRequest,
41    ) -> Result<Self::Response, Self::Error> {
42        let req = TxnAddPartitionsRequest::try_from(req)?;
43
44        match ctx.state().txn_add_partitions(req).await? {
45            TxnAddPartitionsResponse::VersionZeroToThree(results_by_topic_v_3_and_below) => {
46                Ok(AddPartitionsToTxnResponse::default()
47                    .throttle_time_ms(0)
48                    .error_code(Some(ErrorCode::None.into()))
49                    .results_by_transaction(Some([].into()))
50                    .results_by_topic_v_3_and_below(Some(results_by_topic_v_3_and_below)))
51            }
52
53            TxnAddPartitionsResponse::VersionFourPlus(results_by_transaction) => {
54                Ok(AddPartitionsToTxnResponse::default()
55                    .throttle_time_ms(0)
56                    .error_code(Some(ErrorCode::None.into()))
57                    .results_by_transaction(Some(results_by_transaction))
58                    .results_by_topic_v_3_and_below(Some([].into())))
59            }
60        }
61    }
62}