/Users/deen/code/yugabyte-db/src/yb/master/util/yql_vtable_helpers.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | |
14 | | #include "yb/master/util/yql_vtable_helpers.h" |
15 | | |
16 | | #include <future> |
17 | | |
18 | | #include <boost/container/small_vector.hpp> |
19 | | |
20 | | #include "yb/common/ql_value.h" |
21 | | |
22 | | #include "yb/master/master_heartbeat.pb.h" |
23 | | |
24 | | #include "yb/util/net/dns_resolver.h" |
25 | | #include "yb/util/net/net_util.h" |
26 | | #include "yb/util/result.h" |
27 | | #include "yb/util/status_format.h" |
28 | | #include "yb/util/yb_partition.h" |
29 | | |
30 | | namespace yb { |
31 | | namespace master { |
32 | | namespace util { |
33 | | |
34 | | // Ideally, we want clients to use YB's own load-balancing policy for Cassandra to route the |
35 | | // requests to the respective nodes hosting the partition keys. But for clients using vanilla |
36 | | // drivers and thus Cassandra's own token-aware policy, we still want the requests to hit our nodes |
37 | | // evenly. To do that, we split Cassandra's token ring (signed 64-bit number space) evenly and |
38 | | // return the token for each node in the node list. |
39 | 165k | QLValuePB GetTokensValue(size_t index, size_t node_count) { |
40 | 165k | CHECK_GT(node_count, 0); |
41 | 165k | QLValuePB value_pb; |
42 | 165k | QLValuePB *token = value_pb.mutable_set_value()->add_elems(); |
43 | 165k | token->set_string_value(YBPartition::CqlTokenSplit(node_count, index)); |
44 | 165k | return value_pb; |
45 | 165k | } |
46 | | |
47 | | bool RemoteEndpointMatchesList(const google::protobuf::RepeatedPtrField<HostPortPB>& host_ports, |
48 | 1 | const InetAddress& remote_endpoint) { |
49 | 1 | for (const HostPortPB& rpc_address : host_ports) { |
50 | | // host portion of rpc_address might be a hostname and hence we need to resolve it. |
51 | 1 | boost::container::small_vector<IpAddress, 5> resolved_addresses; |
52 | 1 | if (!HostToAddresses(rpc_address.host(), &resolved_addresses).ok()) { |
53 | 0 | LOG (WARNING) << "Could not resolve host: " << rpc_address.host(); |
54 | 0 | continue; |
55 | 0 | } |
56 | 1 | if (std::find( |
57 | 1 | resolved_addresses.begin(), resolved_addresses.end(), remote_endpoint.address()) != |
58 | 1 | resolved_addresses.end()) { |
59 | 1 | return true; |
60 | 1 | } |
61 | 1 | } |
62 | | |
63 | 0 | return false; |
64 | 1 | } |
65 | | |
66 | | bool RemoteEndpointMatchesTServer(const TSInformationPB& ts_info, |
67 | 1 | const InetAddress& remote_endpoint) { |
68 | 1 | const auto& common = ts_info.registration().common(); |
69 | 1 | if (RemoteEndpointMatchesList(common.private_rpc_addresses(), remote_endpoint)) { |
70 | 1 | return true; |
71 | 1 | } |
72 | 0 | if (RemoteEndpointMatchesList(common.broadcast_addresses(), remote_endpoint)) { |
73 | 0 | return true; |
74 | 0 | } |
75 | 0 | return false; |
76 | 0 | } |
77 | | |
78 | 56.0k | QLValuePB GetReplicationValue(size_t replication_factor) { |
79 | 56.0k | QLValuePB value_pb; |
80 | 56.0k | QLMapValuePB *map_value = value_pb.mutable_map_value(); |
81 | | |
82 | | // replication strategy |
83 | 56.0k | QLValuePB *elem = map_value->add_keys(); |
84 | 56.0k | elem->set_string_value("class"); |
85 | 56.0k | elem = map_value->add_values(); |
86 | 56.0k | elem->set_string_value("org.apache.cassandra.locator.SimpleStrategy"); |
87 | | |
88 | | // replication factor |
89 | 56.0k | elem = map_value->add_keys(); |
90 | 56.0k | elem->set_string_value("replication_factor"); |
91 | 56.0k | elem = map_value->add_values(); |
92 | 56.0k | elem->set_string_value(std::to_string(replication_factor)); |
93 | | |
94 | 56.0k | return value_pb; |
95 | 56.0k | } |
96 | | |
97 | | PublicPrivateIPFutures GetPublicPrivateIPFutures( |
98 | 165k | const TSInformationPB& ts_info, DnsResolver* resolver) { |
99 | 165k | const auto& common = ts_info.registration().common(); |
100 | 165k | PublicPrivateIPFutures result; |
101 | | |
102 | 165k | const auto& private_host = common.private_rpc_addresses()[0].host(); |
103 | 165k | if (private_host.empty()) { |
104 | 0 | std::promise<Result<IpAddress>> promise; |
105 | 0 | result.private_ip_future = promise.get_future(); |
106 | 0 | promise.set_value(STATUS_FORMAT( |
107 | 0 | IllegalState, "Tablet server $0 doesn't have any rpc addresses registered", |
108 | 0 | ts_info.tserver_instance().permanent_uuid())); |
109 | 0 | return result; |
110 | 0 | } |
111 | | |
112 | 165k | result.private_ip_future = resolver->ResolveFuture(private_host); |
113 | | |
114 | 165k | if (!common.broadcast_addresses().empty()) { |
115 | 0 | result.public_ip_future = resolver->ResolveFuture(common.broadcast_addresses()[0].host()); |
116 | 165k | } else { |
117 | 165k | result.public_ip_future = result.private_ip_future; |
118 | 165k | } |
119 | | |
120 | 165k | return result; |
121 | 165k | } |
122 | | |
123 | | const QLValuePB& GetValueHelper<QLValuePB>::Apply( |
124 | 814k | const QLValuePB& value_pb, const DataType data_type) { |
125 | 814k | return value_pb; |
126 | 814k | } |
127 | | |
128 | 12.9M | QLValuePB GetValueHelper<std::string>::Apply(const std::string& strval, const DataType data_type) { |
129 | 12.9M | QLValuePB value_pb; |
130 | 12.9M | switch (data_type) { |
131 | 12.8M | case STRING: |
132 | 12.8M | value_pb.set_string_value(strval); |
133 | 12.8M | break; |
134 | 100k | case BINARY: |
135 | 100k | value_pb.set_binary_value(strval); |
136 | 100k | break; |
137 | 0 | default: |
138 | 0 | LOG(ERROR) << "unexpected string type " << data_type; |
139 | 0 | break; |
140 | 12.9M | } |
141 | 12.9M | return value_pb; |
142 | 12.9M | } |
143 | | |
144 | | QLValuePB GetValueHelper<std::string>::Apply( |
145 | 995k | const char* strval, size_t len, const DataType data_type) { |
146 | 995k | QLValuePB value_pb; |
147 | 995k | switch (data_type) { |
148 | 995k | case STRING: |
149 | 995k | value_pb.set_string_value(strval, len); |
150 | 995k | break; |
151 | 0 | case BINARY: |
152 | 0 | value_pb.set_binary_value(strval, len); |
153 | 0 | break; |
154 | 0 | default: |
155 | 0 | LOG(ERROR) << "unexpected string type " << data_type; |
156 | 0 | break; |
157 | 995k | } |
158 | 995k | return value_pb; |
159 | 995k | } |
160 | | |
161 | 2.34M | QLValuePB GetValueHelper<int32_t>::Apply(const int32_t intval, const DataType data_type) { |
162 | 2.34M | QLValuePB value_pb; |
163 | 2.34M | switch (data_type) { |
164 | 86 | case INT64: |
165 | 86 | value_pb.set_int64_value(intval); |
166 | 86 | break; |
167 | 2.34M | case INT32: |
168 | 2.34M | value_pb.set_int32_value(intval); |
169 | 2.34M | break; |
170 | 0 | case INT16: |
171 | 0 | value_pb.set_int16_value(intval); |
172 | 0 | break; |
173 | 0 | case INT8: |
174 | 0 | value_pb.set_int8_value(intval); |
175 | 0 | break; |
176 | 0 | default: |
177 | 0 | LOG(ERROR) << "unexpected int type " << data_type; |
178 | 0 | break; |
179 | 2.34M | } |
180 | 2.34M | return value_pb; |
181 | 2.34M | } |
182 | | |
183 | | QLValuePB GetValueHelper<InetAddress>::Apply( |
184 | 495k | const InetAddress& inet_val, const DataType data_type) { |
185 | 495k | QLValuePB result; |
186 | 495k | QLValue::set_inetaddress_value(inet_val, &result); |
187 | 495k | return result; |
188 | 495k | } |
189 | | |
190 | 649k | QLValuePB GetValueHelper<Uuid>::Apply(const Uuid& uuid_val, const DataType data_type) { |
191 | 649k | QLValuePB result; |
192 | 649k | QLValue::set_uuid_value(uuid_val, &result); |
193 | 649k | return result; |
194 | 649k | } |
195 | | |
196 | 90.6k | QLValuePB GetValueHelper<bool>::Apply(const bool bool_val, const DataType data_type) { |
197 | 90.6k | QLValuePB value_pb; |
198 | 90.6k | value_pb.set_bool_value(bool_val); |
199 | 90.6k | return value_pb; |
200 | 90.6k | } |
201 | | |
202 | | } // namespace util |
203 | | } // namespace master |
204 | | } // namespace yb |