Debian Codegraph @

test/core/util/port_server_client.cc

raw
1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include <stdint.h>
22 #include <stdlib.h>
23
24 #include <cmath>
25 #include <memory>
26 #include <string>
27 #include <utility>
28
29 #include "absl/status/statusor.h"
30
31 #include "src/core/lib/gprpp/orphanable.h"
32 #include "src/core/lib/gprpp/ref_counted_ptr.h"
33 #include "src/core/lib/gprpp/status_helper.h"
34 #include "src/core/lib/gprpp/time.h"
35 #include "src/core/lib/http/parser.h"
36 #include "src/core/lib/iomgr/closure.h"
37 #include "src/core/lib/iomgr/error.h"
38 #include "src/core/lib/iomgr/exec_ctx.h"
39 #include "src/core/lib/iomgr/iomgr_fwd.h"
40 #include "src/core/lib/iomgr/polling_entity.h"
41 #include "src/core/lib/iomgr/pollset.h"
42 #include "src/core/lib/uri/uri_parser.h"
43 #include "test/core/util/test_config.h"
44
45 #ifdef GRPC_TEST_PICK_PORT
46 #include <string.h>
47
48 #include "absl/strings/str_format.h"
49
50 #include <grpc/grpc.h>
51 #include <grpc/grpc_security.h>
52 #include <grpc/support/alloc.h>
53 #include <grpc/support/log.h>
54 #include <grpc/support/sync.h>
55 #include <grpc/support/time.h>
56
57 #include "src/core/lib/http/httpcli.h"
58 #include "src/core/lib/security/credentials/credentials.h"
59 #include "test/core/util/port_server_client.h"
60
61 typedef struct freereq {
62 gpr_mu* mu = nullptr;
63 grpc_polling_entity pops = {};
64 int done = 0;
65 } freereq;
66
67 static void destroy_pops_and_shutdown(void* p, grpc_error_handle /*error*/) {
68 grpc_pollset* pollset =
69 grpc_polling_entity_pollset(static_cast<grpc_polling_entity*>(p));
70 grpc_pollset_destroy(pollset);
71 gpr_free(pollset);
72 }
73
74 static void freed_port_from_server(void* arg, grpc_error_handle /*error*/) {
75 freereq* pr = static_cast<freereq*>(arg);
76 gpr_mu_lock(pr->mu);
77 pr->done = 1;
78 GRPC_LOG_IF_ERROR(
79 "pollset_kick",
80 grpc_pollset_kick(grpc_polling_entity_pollset(&pr->pops), nullptr));
81 gpr_mu_unlock(pr->mu);
82 }
83
84 void grpc_free_port_using_server(int port) {
85 grpc_http_request req;
86 grpc_http_response rsp;
87 freereq pr;
88 grpc_closure* shutdown_closure;
89
90 grpc_init();
91 {
92 grpc_core::ExecCtx exec_ctx;
93
94 pr = {};
95 memset(&req, 0, sizeof(req));
96 rsp = {};
97
98 grpc_pollset* pollset =
99 static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
100 grpc_pollset_init(pollset, &pr.mu);
101 pr.pops = grpc_polling_entity_create_from_pollset(pollset);
102 shutdown_closure = GRPC_CLOSURE_CREATE(destroy_pops_and_shutdown, &pr.pops,
103 grpc_schedule_on_exec_ctx);
104
105 std::string path = absl::StrFormat("/drop/%d", port);
106 auto uri = grpc_core::URI::Create("https", GRPC_PORT_SERVER_ADDRESS, path,
107 {} /* query params */, "" /* fragment */);
108 GPR_ASSERT(uri.ok());
109 auto http_request = grpc_core::HttpRequest::Get(
110 std::move(*uri), nullptr /* channel args */, &pr.pops, &req,
111 grpc_core::Timestamp::Now() + grpc_core::Duration::Seconds(30),
112 GRPC_CLOSURE_CREATE(freed_port_from_server, &pr,
113 grpc_schedule_on_exec_ctx),
114 &rsp,
115 grpc_core::RefCountedPtr<grpc_channel_credentials>(
116 grpc_insecure_credentials_create()));
117 http_request->Start();
118 grpc_core::ExecCtx::Get()->Flush();
119 gpr_mu_lock(pr.mu);
120 while (!pr.done) {
121 grpc_pollset_worker* worker = nullptr;
122 if (!GRPC_LOG_IF_ERROR(
123 "pollset_work",
124 grpc_pollset_work(grpc_polling_entity_pollset(&pr.pops), &worker,
125 grpc_core::Timestamp::Now() +
126 grpc_core::Duration::Seconds(1)))) {
127 pr.done = 1;
128 }
129 }
130 gpr_mu_unlock(pr.mu);
131
132 grpc_pollset_shutdown(grpc_polling_entity_pollset(&pr.pops),
133 shutdown_closure);
134
135 grpc_http_response_destroy(&rsp);
136 }
137 grpc_shutdown();
138 }
139
140 typedef struct portreq {
141 gpr_mu* mu = nullptr;
142 grpc_polling_entity pops = {};
143 int port = 0;
144 int retries = 0;
145 char* server = nullptr;
146 grpc_http_response response = {};
147 grpc_core::OrphanablePtr<grpc_core::HttpRequest> http_request;
148 } portreq;
149
150 static void got_port_from_server(void* arg, grpc_error_handle error) {
151 size_t i;
152 int port = 0;
153 portreq* pr = static_cast<portreq*>(arg);
154 pr->http_request.reset();
155 int failed = 0;
156 grpc_http_response* response = &pr->response;
157
158 if (!error.ok()) {
159 failed = 1;
160 gpr_log(GPR_DEBUG, "failed port pick from server: retrying [%s]",
161 grpc_core::StatusToString(error).c_str());
162 } else if (response->status != 200) {
163 failed = 1;
164 gpr_log(GPR_DEBUG, "failed port pick from server: status=%d",
165 response->status);
166 }
167
168 if (failed) {
169 grpc_http_request req;
170 memset(&req, 0, sizeof(req));
171 if (pr->retries >= 5) {
172 gpr_mu_lock(pr->mu);
173 pr->port = 0;
174 GRPC_LOG_IF_ERROR(
175 "pollset_kick",
176 grpc_pollset_kick(grpc_polling_entity_pollset(&pr->pops), nullptr));
177 gpr_mu_unlock(pr->mu);
178 return;
179 }
180 GPR_ASSERT(pr->retries < 10);
181 gpr_sleep_until(gpr_time_add(
182 gpr_now(GPR_CLOCK_REALTIME),
183 gpr_time_from_millis(
184 static_cast<int64_t>(
185 1000.0 * (1 + pow(1.3, pr->retries) * rand() / RAND_MAX)),
186 GPR_TIMESPAN)));
187 pr->retries++;
188 grpc_http_response_destroy(&pr->response);
189 pr->response = {};
190 auto uri = grpc_core::URI::Create("http", pr->server, "/get",
191 {} /* query params */, "" /* fragment */);
192 GPR_ASSERT(uri.ok());
193 pr->http_request = grpc_core::HttpRequest::Get(
194 std::move(*uri), nullptr /* channel args */, &pr->pops, &req,
195 grpc_core::Timestamp::Now() + grpc_core::Duration::Seconds(30),
196 GRPC_CLOSURE_CREATE(got_port_from_server, pr,
197 grpc_schedule_on_exec_ctx),
198 &pr->response,
199 grpc_core::RefCountedPtr<grpc_channel_credentials>(
200 grpc_insecure_credentials_create()));
201 pr->http_request->Start();
202 return;
203 }
204 GPR_ASSERT(response);
205 GPR_ASSERT(response->status == 200);
206 for (i = 0; i < response->body_length; i++) {
207 GPR_ASSERT(response->body[i] >= '0' && response->body[i] <= '9');
208 port = port * 10 + response->body[i] - '0';
209 }
210 GPR_ASSERT(port > 1024);
211 gpr_mu_lock(pr->mu);
212 pr->port = port;
213 GRPC_LOG_IF_ERROR(
214 "pollset_kick",
215 grpc_pollset_kick(grpc_polling_entity_pollset(&pr->pops), nullptr));
216 gpr_mu_unlock(pr->mu);
217 }
218
219 int grpc_pick_port_using_server(void) {
220 grpc_http_request req;
221 portreq pr;
222 grpc_closure* shutdown_closure;
223
224 grpc_init();
225 {
226 grpc_core::ExecCtx exec_ctx;
227 pr = {};
228 memset(&req, 0, sizeof(req));
229 grpc_pollset* pollset =
230 static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size()));
231 grpc_pollset_init(pollset, &pr.mu);
232 pr.pops = grpc_polling_entity_create_from_pollset(pollset);
233 shutdown_closure = GRPC_CLOSURE_CREATE(destroy_pops_and_shutdown, &pr.pops,
234 grpc_schedule_on_exec_ctx);
235 pr.port = -1;
236 pr.server = const_cast<char*>(GRPC_PORT_SERVER_ADDRESS);
237 auto uri = grpc_core::URI::Create("http", GRPC_PORT_SERVER_ADDRESS, "/get",
238 {} /* query params */, "" /* fragment */);
239 GPR_ASSERT(uri.ok());
240 auto http_request = grpc_core::HttpRequest::Get(
241 std::move(*uri), nullptr /* channel args */, &pr.pops, &req,
242 grpc_core::Timestamp::Now() + grpc_core::Duration::Seconds(30),
243 GRPC_CLOSURE_CREATE(got_port_from_server, &pr,
244 grpc_schedule_on_exec_ctx),
245 &pr.response,
246 grpc_core::RefCountedPtr<grpc_channel_credentials>(
247 grpc_insecure_credentials_create()));
248 http_request->Start();
249 grpc_core::ExecCtx::Get()->Flush();
250 gpr_mu_lock(pr.mu);
251 while (pr.port == -1) {
252 grpc_pollset_worker* worker = nullptr;
253 if (!GRPC_LOG_IF_ERROR(
254 "pollset_work",
255 grpc_pollset_work(grpc_polling_entity_pollset(&pr.pops), &worker,
256 grpc_core::Timestamp::Now() +
257 grpc_core::Duration::Seconds(1)))) {
258 pr.port = 0;
259 }
260 }
261 gpr_mu_unlock(pr.mu);
262
263 grpc_http_response_destroy(&pr.response);
264 grpc_pollset_shutdown(grpc_polling_entity_pollset(&pr.pops),
265 shutdown_closure);
266
267 grpc_core::ExecCtx::Get()->Flush();
268 }
269 grpc_shutdown();
270
271 return pr.port;
272 }
273
274 #endif // GRPC_TEST_PICK_PORT