meta_srv/
mocks.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::Arc;
use std::time::Duration;

use api::v1::meta::cluster_server::ClusterServer;
use api::v1::meta::heartbeat_server::HeartbeatServer;
use api::v1::meta::procedure_service_server::ProcedureServiceServer;
use api::v1::meta::store_server::StoreServer;
use client::client_manager::NodeClients;
use common_grpc::channel_manager::{ChannelConfig, ChannelManager};
use common_meta::key::TableMetadataManager;
use common_meta::kv_backend::etcd::EtcdStore;
use common_meta::kv_backend::memory::MemoryKvBackend;
use common_meta::kv_backend::{KvBackendRef, ResettableKvBackendRef};
use hyper_util::rt::TokioIo;
use tonic::codec::CompressionEncoding;
use tower::service_fn;

use crate::add_compressed_service;
use crate::metasrv::builder::MetasrvBuilder;
use crate::metasrv::{Metasrv, MetasrvOptions, SelectorRef};

#[derive(Clone)]
pub struct MockInfo {
    pub server_addr: String,
    pub channel_manager: ChannelManager,
    pub metasrv: Arc<Metasrv>,
    pub kv_backend: KvBackendRef,
    pub in_memory: Option<ResettableKvBackendRef>,
}

pub async fn mock_with_memstore() -> MockInfo {
    let kv_backend = Arc::new(MemoryKvBackend::new());
    let in_memory = Arc::new(MemoryKvBackend::new());
    mock(
        MetasrvOptions {
            server_addr: "127.0.0.1:3002".to_string(),
            ..Default::default()
        },
        kv_backend,
        None,
        None,
        Some(in_memory),
    )
    .await
}

pub async fn mock_with_etcdstore(addr: &str) -> MockInfo {
    let kv_backend = EtcdStore::with_endpoints([addr], 128).await.unwrap();
    mock(
        MetasrvOptions {
            server_addr: "127.0.0.1:3002".to_string(),
            ..Default::default()
        },
        kv_backend,
        None,
        None,
        None,
    )
    .await
}

pub async fn mock(
    opts: MetasrvOptions,
    kv_backend: KvBackendRef,
    selector: Option<SelectorRef>,
    datanode_clients: Option<Arc<NodeClients>>,
    in_memory: Option<ResettableKvBackendRef>,
) -> MockInfo {
    let server_addr = opts.server_addr.clone();
    let table_metadata_manager = Arc::new(TableMetadataManager::new(kv_backend.clone()));

    table_metadata_manager.init().await.unwrap();

    let builder = MetasrvBuilder::new()
        .options(opts)
        .kv_backend(kv_backend.clone());

    let builder = match selector {
        Some(s) => builder.selector(s),
        None => builder,
    };

    let builder = match datanode_clients {
        Some(clients) => builder.node_manager(clients),
        None => builder,
    };

    let builder = match &in_memory {
        Some(in_memory) => builder.in_memory(in_memory.clone()),
        None => builder,
    };

    let metasrv = builder.build().await.unwrap();
    metasrv.try_start().await.unwrap();

    let (client, server) = tokio::io::duplex(1024);
    let metasrv = Arc::new(metasrv);
    let service = metasrv.clone();

    let _handle = tokio::spawn(async move {
        let mut router = tonic::transport::Server::builder();
        let router = add_compressed_service!(router, HeartbeatServer::from_arc(service.clone()));
        let router = add_compressed_service!(router, StoreServer::from_arc(service.clone()));
        let router =
            add_compressed_service!(router, ProcedureServiceServer::from_arc(service.clone()));
        let router = add_compressed_service!(router, ClusterServer::from_arc(service.clone()));
        router
            .serve_with_incoming(futures::stream::iter(vec![Ok::<_, std::io::Error>(server)]))
            .await
    });

    let config = ChannelConfig::new()
        .timeout(Duration::from_secs(10))
        .connect_timeout(Duration::from_secs(10))
        .tcp_nodelay(true);
    let channel_manager = ChannelManager::with_config(config);

    // Move client to an option so we can _move_ the inner value
    // on the first attempt to connect. All other attempts will fail.
    let mut client = Some(client);
    let res = channel_manager.reset_with_connector(
        &server_addr,
        service_fn(move |_| {
            let client = client.take();

            async move {
                if let Some(client) = client {
                    Ok(TokioIo::new(client))
                } else {
                    Err(std::io::Error::new(
                        std::io::ErrorKind::Other,
                        "Client already taken",
                    ))
                }
            }
        }),
    );
    let _ = res.unwrap();

    MockInfo {
        server_addr,
        channel_manager,
        metasrv,
        kv_backend,
        in_memory,
    }
}