NebulaGraph CPP Client  release-3.4
MetaClient.h
1 /* Copyright (c) 2020 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 #pragma once
7 
8 #include <functional>
9 #include <memory>
10 #include <string>
11 #include <thread>
12 #include <unordered_map>
13 #include <vector>
14 
15 #include "common/datatypes/HostAddr.h"
16 #include "common/thrift/ThriftTypes.h"
17 #include "nebula/mclient/MConfig.h"
18 
19 struct pair_hash {
20  template <class T1, class T2>
21  std::size_t operator()(const std::pair<T1, T2> &pair) const {
22  return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second);
23  }
24 };
25 
26 namespace folly {
27 
28 class IOThreadPoolExecutor;
29 template <class T>
30 class Promise;
31 
32 } // namespace folly
33 
34 namespace nebula {
35 
36 namespace thrift {
37 
38 template <class ClientType>
40 
41 } // namespace thrift
42 
43 namespace meta {
44 namespace cpp2 {
45 
46 enum class ListHostType;
47 class HostItem;
48 class MetaServiceAsyncClient;
49 class ListSpacesReq;
50 class ListSpacesResp;
51 class IdName;
52 class EdgeItem;
53 class ListEdgesReq;
54 class ListEdgesResp;
55 
56 } // namespace cpp2
57 } // namespace meta
58 
59 using SpaceIdName = std::pair<GraphSpaceID, std::string>;
60 using SpaceNameIdMap = std::unordered_map<std::string, GraphSpaceID>;
61 using SpaceEdgeNameTypeMap =
62  std::unordered_map<std::pair<GraphSpaceID, std::string>, EdgeType, pair_hash>;
63 
64 class MetaClient {
65  public:
66  explicit MetaClient(const std::vector<std::string> &metaAddrs,
67  const MConfig &mConfig = MConfig{});
68 
69  ~MetaClient();
70 
71  std::pair<bool, GraphSpaceID> getSpaceIdByNameFromCache(const std::string &name);
72 
73  std::pair<bool, EdgeType> getEdgeTypeByNameFromCache(GraphSpaceID spaceId,
74  const std::string &name);
75 
76  std::pair<bool, std::vector<PartitionID>> getPartsFromCache(GraphSpaceID spaceId);
77 
78  std::pair<bool, HostAddr> getPartLeaderFromCache(GraphSpaceID spaceId, PartitionID partId);
79 
80  private:
81  bool loadData();
82 
83  std::pair<bool, std::vector<SpaceIdName>> listSpaces();
84 
85  std::pair<bool, std::vector<meta::cpp2::HostItem>> listHosts(meta::cpp2::ListHostType tp);
86 
87  std::pair<bool, std::vector<meta::cpp2::EdgeItem>> listEdgeSchemas(GraphSpaceID spaceId);
88 
89  void loadLeader(const std::vector<nebula::meta::cpp2::HostItem> &hostItems,
90  const SpaceNameIdMap &spaceIndexByName);
91 
92  std::vector<SpaceIdName> toSpaceIdName(const std::vector<meta::cpp2::IdName> &tIdNames);
93 
94  template <class Request,
95  class RemoteFunc,
96  class RespGenerator,
97  class RpcResponse = typename std::result_of<RemoteFunc(
98  std::shared_ptr<meta::cpp2::MetaServiceAsyncClient>, Request)>::type::value_type,
99  class Response = typename std::result_of<RespGenerator(RpcResponse)>::type>
100  void getResponse(Request req,
101  RemoteFunc remoteFunc,
102  RespGenerator respGen,
103  folly::Promise<std::pair<bool, Response>> pro);
104 
105  private:
106  std::vector<HostAddr> metaAddrs_;
107  MConfig mConfig_;
108  SpaceNameIdMap spaceIndexByName_;
109  SpaceEdgeNameTypeMap spaceEdgeIndexByName_;
110  std::unordered_map<std::pair<GraphSpaceID, PartitionID>, HostAddr, pair_hash> spacePartLeaderMap_;
111  std::unordered_map<GraphSpaceID, std::vector<PartitionID>> spacePartsMap_;
112  std::shared_ptr<folly::IOThreadPoolExecutor> ioExecutor_;
113  std::shared_ptr<thrift::ThriftClientManager<meta::cpp2::MetaServiceAsyncClient>> clientsMan_;
114 };
115 
116 } // namespace nebula