Skip to main content

tansu_storage/service/txn/
add_offsets.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::{AddOffsetsToTxnRequest, AddOffsetsToTxnResponse, ApiKey};
17use tracing::instrument;
18
19use crate::{Error, Result, Storage};
20
21/// A [`Service`] using [`Storage`] as [`Context`] taking [`AddOffsetsToTxnRequest`] returning [`AddOffsetsToTxnResponse`].
22#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
23pub struct AddOffsetsService;
24
25impl ApiKey for AddOffsetsService {
26    const KEY: i16 = AddOffsetsToTxnRequest::KEY;
27}
28
29impl<G> Service<G, AddOffsetsToTxnRequest> for AddOffsetsService
30where
31    G: Storage,
32{
33    type Response = AddOffsetsToTxnResponse;
34    type Error = Error;
35
36    #[instrument(skip(ctx, req))]
37    async fn serve(
38        &self,
39        ctx: Context<G>,
40        req: AddOffsetsToTxnRequest,
41    ) -> Result<Self::Response, Self::Error> {
42        ctx.state()
43            .txn_add_offsets(
44                req.transactional_id.as_str(),
45                req.producer_id,
46                req.producer_epoch,
47                req.group_id.as_str(),
48            )
49            .await
50            .map(|error_code| {
51                AddOffsetsToTxnResponse::default()
52                    .throttle_time_ms(0)
53                    .error_code(error_code.into())
54            })
55    }
56}