Skip to main content

query/
region_query.rs

1// Copyright 2023 Greptime Team
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 std::sync::Arc;
16
17use api::v1::region::{RemoteDynFilterUnregister, RemoteDynFilterUpdate};
18use async_trait::async_trait;
19use common_meta::node_manager::NodeManagerRef;
20use common_meta::peer::Peer;
21use common_query::request::QueryRequest;
22use common_recordbatch::SendableRecordBatchStream;
23use partition::manager::PartitionRuleManagerRef;
24use session::ReadPreference;
25
26use crate::error::Result;
27
28/// The peer selected to serve a region query.
29///
30/// The target is frozen when the query is dispatched. Follow-up controls must
31/// reuse it instead of resolving the region route again.
32#[derive(Clone, Debug, PartialEq, Eq, Hash)]
33pub struct RegionQueryTarget(Peer);
34
35impl RegionQueryTarget {
36    pub fn new(peer: Peer) -> Self {
37        Self(peer)
38    }
39
40    pub fn peer(&self) -> &Peer {
41        &self.0
42    }
43}
44
45/// A factory to create a [`RegionQueryHandler`].
46pub trait RegionQueryHandlerFactory: Send + Sync {
47    /// Build a [`RegionQueryHandler`] with the given partition manager and node manager.
48    fn build(
49        &self,
50        partition_manager: PartitionRuleManagerRef,
51        node_manager: NodeManagerRef,
52    ) -> RegionQueryHandlerRef;
53}
54
55pub type RegionQueryHandlerFactoryRef = Arc<dyn RegionQueryHandlerFactory>;
56
57#[async_trait]
58pub trait RegionQueryHandler: Send + Sync {
59    async fn select_target(
60        &self,
61        read_preference: ReadPreference,
62        region_id: store_api::storage::RegionId,
63    ) -> Result<RegionQueryTarget>;
64
65    async fn do_get(
66        &self,
67        target: &RegionQueryTarget,
68        request: QueryRequest,
69    ) -> Result<SendableRecordBatchStream>;
70
71    async fn handle_remote_dyn_filter_update(
72        &self,
73        target: &RegionQueryTarget,
74        query_id: String,
75        update: RemoteDynFilterUpdate,
76    ) -> Result<()>;
77
78    async fn handle_remote_dyn_filter_unregister(
79        &self,
80        target: &RegionQueryTarget,
81        query_id: String,
82        unregister: RemoteDynFilterUnregister,
83    ) -> Result<()>;
84}
85
86pub type RegionQueryHandlerRef = Arc<dyn RegionQueryHandler>;
87
88#[cfg(test)]
89mod tests {
90    use common_meta::peer::Peer;
91
92    use super::RegionQueryTarget;
93
94    #[test]
95    fn region_query_target_exposes_immutable_peer() {
96        let peer = Peer {
97            id: 42,
98            addr: "127.0.0.1:3001".to_string(),
99        };
100        let target = RegionQueryTarget::new(peer.clone());
101
102        assert_eq!(target.peer(), &peer);
103    }
104}