Loading...
Searching...
No Matches
resdb_state_accessor.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
21
22#include <glog/logging.h>
23
24#include <future>
25#include <thread>
26
27namespace resdb {
28
31
32std::unique_ptr<NetChannel> ResDBStateAccessor::GetNetChannel(
33 const std::string& ip, int port) {
34 return std::make_unique<NetChannel>(ip, port);
35}
36
37// Obtain ReplicaState of each replica.
38absl::StatusOr<std::vector<ReplicaState>>
40 const auto& client_info = config_.GetReplicaInfos()[0];
41
42 Request request;
43 std::unique_ptr<NetChannel> client =
44 GetNetChannel(client_info.ip(), client_info.port());
45 client->SetRecvTimeout(1000000); // 1s for recv timeout.
46
47 int ret = client->SendRequest(request, Request::TYPE_REPLICA_STATE);
48 if (ret) {
49 return absl::InternalError("send data fail.");
50 }
51
52 std::unique_ptr<ReplicaState> state = std::make_unique<ReplicaState>();
53 ret = client->RecvRawMessage(state.get());
54 if (ret < 0) {
55 return absl::InternalError("recv data fail.");
56 }
57 if (state == nullptr) {
58 return absl::InternalError("recv data fail.");
59 }
60
61 std::vector<ReplicaState> resp;
62 for (const auto& region : state->replica_config().region()) {
63 for (const auto& info : region.replica_info()) {
64 ReplicaState new_state;
65 *new_state.mutable_replica_info() = info;
66 resp.push_back(new_state);
67 }
68 }
69 return resp;
70}
71
72} // namespace resdb
const std::vector< ReplicaInfo > & GetReplicaInfos() const
absl::StatusOr< std::vector< ReplicaState > > GetReplicaStates()
virtual std::unique_ptr< NetChannel > GetNetChannel(const std::string &ip, int port)
ResDBStateAccessor(const ResDBConfig &config)
std::unique_ptr< ResDBConfig > config_