mirror of https://github.com/grpc/grpc.git
commit
043be3d303
122 changed files with 2028 additions and 906 deletions
@ -0,0 +1,68 @@ |
|||||||
|
//
|
||||||
|
// Copyright 2016 gRPC authors.
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_CALL_DATA_H |
||||||
|
#define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_CALL_DATA_H |
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include "src/core/ext/filters/client_channel/service_config.h" |
||||||
|
#include "src/core/ext/filters/client_channel/service_config_parser.h" |
||||||
|
#include "src/core/lib/channel/context.h" |
||||||
|
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
||||||
|
|
||||||
|
namespace grpc_core { |
||||||
|
|
||||||
|
/// When a service config is applied to a call in the client_channel_filter,
|
||||||
|
/// we create an instance of this object on the arena. A pointer to this
|
||||||
|
/// object is also stored in the call_context, so that future filters can
|
||||||
|
/// easily access method and global parameters for the call.
|
||||||
|
class ServiceConfigCallData { |
||||||
|
public: |
||||||
|
ServiceConfigCallData( |
||||||
|
RefCountedPtr<ServiceConfig> service_config, |
||||||
|
const ServiceConfigParser::ParsedConfigVector* method_configs, |
||||||
|
grpc_call_context_element* call_context) |
||||||
|
: service_config_(std::move(service_config)), |
||||||
|
method_configs_(method_configs) { |
||||||
|
call_context[GRPC_CONTEXT_SERVICE_CONFIG_CALL_DATA].value = this; |
||||||
|
call_context[GRPC_CONTEXT_SERVICE_CONFIG_CALL_DATA].destroy = Destroy; |
||||||
|
} |
||||||
|
|
||||||
|
ServiceConfig* service_config() { return service_config_.get(); } |
||||||
|
|
||||||
|
ServiceConfigParser::ParsedConfig* GetMethodParsedConfig(size_t index) const { |
||||||
|
return method_configs_ != nullptr ? (*method_configs_)[index].get() |
||||||
|
: nullptr; |
||||||
|
} |
||||||
|
|
||||||
|
ServiceConfigParser::ParsedConfig* GetGlobalParsedConfig(size_t index) const { |
||||||
|
return service_config_->GetGlobalParsedConfig(index); |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
static void Destroy(void* ptr) { |
||||||
|
ServiceConfigCallData* self = static_cast<ServiceConfigCallData*>(ptr); |
||||||
|
self->~ServiceConfigCallData(); |
||||||
|
} |
||||||
|
|
||||||
|
RefCountedPtr<ServiceConfig> service_config_; |
||||||
|
const ServiceConfigParser::ParsedConfigVector* method_configs_ = nullptr; |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace grpc_core
|
||||||
|
|
||||||
|
#endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_CALL_DATA_H */ |
@ -0,0 +1,87 @@ |
|||||||
|
//
|
||||||
|
// Copyright 2015 gRPC authors.
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include "src/core/ext/filters/client_channel/service_config_parser.h" |
||||||
|
|
||||||
|
#include <grpc/support/log.h> |
||||||
|
|
||||||
|
namespace grpc_core { |
||||||
|
|
||||||
|
namespace { |
||||||
|
typedef absl::InlinedVector<std::unique_ptr<ServiceConfigParser::Parser>, |
||||||
|
ServiceConfigParser::kNumPreallocatedParsers> |
||||||
|
ServiceConfigParserList; |
||||||
|
ServiceConfigParserList* g_registered_parsers; |
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void ServiceConfigParser::Init() { |
||||||
|
GPR_ASSERT(g_registered_parsers == nullptr); |
||||||
|
g_registered_parsers = new ServiceConfigParserList(); |
||||||
|
} |
||||||
|
|
||||||
|
void ServiceConfigParser::Shutdown() { |
||||||
|
delete g_registered_parsers; |
||||||
|
g_registered_parsers = nullptr; |
||||||
|
} |
||||||
|
|
||||||
|
size_t ServiceConfigParser::RegisterParser(std::unique_ptr<Parser> parser) { |
||||||
|
g_registered_parsers->push_back(std::move(parser)); |
||||||
|
return g_registered_parsers->size() - 1; |
||||||
|
} |
||||||
|
|
||||||
|
ServiceConfigParser::ParsedConfigVector |
||||||
|
ServiceConfigParser::ParseGlobalParameters(const Json& json, |
||||||
|
grpc_error** error) { |
||||||
|
ParsedConfigVector parsed_global_configs; |
||||||
|
std::vector<grpc_error*> error_list; |
||||||
|
for (size_t i = 0; i < g_registered_parsers->size(); i++) { |
||||||
|
grpc_error* parser_error = GRPC_ERROR_NONE; |
||||||
|
auto parsed_config = |
||||||
|
(*g_registered_parsers)[i]->ParseGlobalParams(json, &parser_error); |
||||||
|
if (parser_error != GRPC_ERROR_NONE) { |
||||||
|
error_list.push_back(parser_error); |
||||||
|
} |
||||||
|
parsed_global_configs.push_back(std::move(parsed_config)); |
||||||
|
} |
||||||
|
if (!error_list.empty()) { |
||||||
|
*error = GRPC_ERROR_CREATE_FROM_VECTOR("Global Params", &error_list); |
||||||
|
} |
||||||
|
return parsed_global_configs; |
||||||
|
} |
||||||
|
|
||||||
|
ServiceConfigParser::ParsedConfigVector |
||||||
|
ServiceConfigParser::ParsePerMethodParameters(const Json& json, |
||||||
|
grpc_error** error) { |
||||||
|
ParsedConfigVector parsed_method_configs; |
||||||
|
std::vector<grpc_error*> error_list; |
||||||
|
for (size_t i = 0; i < g_registered_parsers->size(); i++) { |
||||||
|
grpc_error* parser_error = GRPC_ERROR_NONE; |
||||||
|
auto parsed_config = |
||||||
|
(*g_registered_parsers)[i]->ParsePerMethodParams(json, &parser_error); |
||||||
|
if (parser_error != GRPC_ERROR_NONE) { |
||||||
|
error_list.push_back(parser_error); |
||||||
|
} |
||||||
|
parsed_method_configs.push_back(std::move(parsed_config)); |
||||||
|
} |
||||||
|
if (!error_list.empty()) { |
||||||
|
*error = GRPC_ERROR_CREATE_FROM_VECTOR("methodConfig", &error_list); |
||||||
|
} |
||||||
|
return parsed_method_configs; |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace grpc_core
|
@ -0,0 +1,89 @@ |
|||||||
|
//
|
||||||
|
// Copyright 2016 gRPC authors.
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_PARSER_H |
||||||
|
#define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_PARSER_H |
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include <memory> |
||||||
|
|
||||||
|
#include "absl/container/inlined_vector.h" |
||||||
|
|
||||||
|
#include "src/core/lib/iomgr/error.h" |
||||||
|
#include "src/core/lib/json/json.h" |
||||||
|
|
||||||
|
namespace grpc_core { |
||||||
|
|
||||||
|
// Service config parser registry.
|
||||||
|
// See service_config.h for more information.
|
||||||
|
class ServiceConfigParser { |
||||||
|
public: |
||||||
|
/// This is the base class that all service config parsers MUST use to store
|
||||||
|
/// parsed service config data.
|
||||||
|
class ParsedConfig { |
||||||
|
public: |
||||||
|
virtual ~ParsedConfig() = default; |
||||||
|
}; |
||||||
|
|
||||||
|
/// This is the base class that all service config parsers should derive from.
|
||||||
|
class Parser { |
||||||
|
public: |
||||||
|
virtual ~Parser() = default; |
||||||
|
|
||||||
|
virtual std::unique_ptr<ParsedConfig> ParseGlobalParams( |
||||||
|
const Json& /* json */, grpc_error** error) { |
||||||
|
// Avoid unused parameter warning on debug-only parameter
|
||||||
|
(void)error; |
||||||
|
GPR_DEBUG_ASSERT(error != nullptr); |
||||||
|
return nullptr; |
||||||
|
} |
||||||
|
|
||||||
|
virtual std::unique_ptr<ParsedConfig> ParsePerMethodParams( |
||||||
|
const Json& /* json */, grpc_error** error) { |
||||||
|
// Avoid unused parameter warning on debug-only parameter
|
||||||
|
(void)error; |
||||||
|
GPR_DEBUG_ASSERT(error != nullptr); |
||||||
|
return nullptr; |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
static constexpr int kNumPreallocatedParsers = 4; |
||||||
|
typedef absl::InlinedVector<std::unique_ptr<ParsedConfig>, |
||||||
|
kNumPreallocatedParsers> |
||||||
|
ParsedConfigVector; |
||||||
|
|
||||||
|
static void Init(); |
||||||
|
static void Shutdown(); |
||||||
|
|
||||||
|
/// Globally register a service config parser. On successful registration, it
|
||||||
|
/// returns the index at which the parser was registered. On failure, -1 is
|
||||||
|
/// returned. Each new service config update will go through all the
|
||||||
|
/// registered parser. Each parser is responsible for reading the service
|
||||||
|
/// config json and returning a parsed config. This parsed config can later be
|
||||||
|
/// retrieved using the same index that was returned at registration time.
|
||||||
|
static size_t RegisterParser(std::unique_ptr<Parser> parser); |
||||||
|
|
||||||
|
static ParsedConfigVector ParseGlobalParameters(const Json& json, |
||||||
|
grpc_error** error); |
||||||
|
|
||||||
|
static ParsedConfigVector ParsePerMethodParameters(const Json& json, |
||||||
|
grpc_error** error); |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace grpc_core
|
||||||
|
|
||||||
|
#endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_PARSER_H */ |
@ -0,0 +1,168 @@ |
|||||||
|
<?php |
||||||
|
/* |
||||||
|
* |
||||||
|
* Copyright 2020 gRPC authors. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace Grpc; |
||||||
|
|
||||||
|
/** |
||||||
|
* This is an experimental and incomplete implementation of gRPC server |
||||||
|
* for PHP. APIs are _definitely_ going to be changed. |
||||||
|
* |
||||||
|
* DO NOT USE in production. |
||||||
|
*/ |
||||||
|
|
||||||
|
/** |
||||||
|
* Class RpcServer |
||||||
|
* @package Grpc |
||||||
|
*/ |
||||||
|
class RpcServer extends Server |
||||||
|
{ |
||||||
|
protected $call; |
||||||
|
// [ <String method_full_path> => [ |
||||||
|
// 'service' => <Object service>, |
||||||
|
// 'method' => <String method_name>, |
||||||
|
// 'request' => <Object request>, |
||||||
|
// ] ] |
||||||
|
protected $paths_map; |
||||||
|
|
||||||
|
private function waitForNextEvent() { |
||||||
|
return $this->requestCall(); |
||||||
|
} |
||||||
|
|
||||||
|
private function loadRequest($request) { |
||||||
|
if (!$this->call) { |
||||||
|
throw new Exception("serverCall is not ready"); |
||||||
|
} |
||||||
|
$event = $this->call->startBatch([ |
||||||
|
OP_RECV_MESSAGE => true, |
||||||
|
]); |
||||||
|
if (!$event->message) { |
||||||
|
throw new Exception("Did not receive a proper message"); |
||||||
|
} |
||||||
|
$request->mergeFromString($event->message); |
||||||
|
return $request; |
||||||
|
} |
||||||
|
|
||||||
|
protected function sendOkResponse($response) { |
||||||
|
if (!$this->call) { |
||||||
|
throw new Exception("serverCall is not ready"); |
||||||
|
} |
||||||
|
$this->call->startBatch([ |
||||||
|
OP_SEND_INITIAL_METADATA => [], |
||||||
|
OP_SEND_MESSAGE => ['message' => |
||||||
|
$response->serializeToString()], |
||||||
|
OP_SEND_STATUS_FROM_SERVER => [ |
||||||
|
'metadata' => [], |
||||||
|
'code' => STATUS_OK, |
||||||
|
'details' => 'OK', |
||||||
|
], |
||||||
|
OP_RECV_CLOSE_ON_SERVER => true, |
||||||
|
]); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Add a service to this server |
||||||
|
* |
||||||
|
* @param Object $service The service to be added |
||||||
|
*/ |
||||||
|
public function handle($service) { |
||||||
|
$rf = new \ReflectionClass($service); |
||||||
|
|
||||||
|
// If input does not have a parent class, which should be the |
||||||
|
// generated stub, don't proceeed. This might change in the |
||||||
|
// future. |
||||||
|
if (!$rf->getParentClass()) return; |
||||||
|
|
||||||
|
// The input class name needs to match the service name |
||||||
|
$service_name = $rf->getName(); |
||||||
|
$namespace = $rf->getParentClass()->getNamespaceName(); |
||||||
|
$prefix = ""; |
||||||
|
if ($namespace) { |
||||||
|
$parts = explode("\\", $namespace); |
||||||
|
foreach ($parts as $part) { |
||||||
|
$prefix .= lcfirst($part) . "."; |
||||||
|
} |
||||||
|
} |
||||||
|
$base_path = "/" . $prefix . $service_name; |
||||||
|
|
||||||
|
// Right now, assume all the methods in the class are RPC method |
||||||
|
// implementations. Might change in the future. |
||||||
|
$methods = $rf->getMethods(); |
||||||
|
foreach ($methods as $method) { |
||||||
|
$method_name = $method->getName(); |
||||||
|
$full_path = $base_path . "/" . ucfirst($method_name); |
||||||
|
|
||||||
|
$method_params = $method->getParameters(); |
||||||
|
// RPC should have exactly 1 request param |
||||||
|
if (count($method_params) != 1) continue; |
||||||
|
$request_param = $method_params[0]; |
||||||
|
// Method implementation must have type hint for request param |
||||||
|
if (!$request_param->getType()) continue; |
||||||
|
$request_type = $request_param->getType()->getName(); |
||||||
|
|
||||||
|
// $full_path needs to match the incoming event->method |
||||||
|
// from requestCall() for us to know how to handle the request |
||||||
|
$this->paths_map[$full_path] = [ |
||||||
|
'service' => $service, |
||||||
|
'method' => $method_name, |
||||||
|
'request' => new $request_type(), |
||||||
|
]; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function run() { |
||||||
|
$this->start(); |
||||||
|
while (true) { |
||||||
|
// This blocks until the server receives a request |
||||||
|
$event = $this->waitForNextEvent(); |
||||||
|
if (!$event) { |
||||||
|
throw new Exception( |
||||||
|
"Unexpected error: server->waitForNextEvent delivers" |
||||||
|
. " an empty event"); |
||||||
|
} |
||||||
|
if (!$event->call) { |
||||||
|
throw new Exception( |
||||||
|
"Unexpected error: server->waitForNextEvent delivers" |
||||||
|
. " an event without a call"); |
||||||
|
} |
||||||
|
$this->call = $event->call; |
||||||
|
$full_path = $event->method; |
||||||
|
|
||||||
|
// TODO: Can send a proper UNIMPLEMENTED response in the future |
||||||
|
if (!array_key_exists($full_path, $this->paths_map)) continue; |
||||||
|
|
||||||
|
$service = $this->paths_map[$full_path]['service']; |
||||||
|
$method = $this->paths_map[$full_path]['method']; |
||||||
|
$request = $this->paths_map[$full_path]['request']; |
||||||
|
|
||||||
|
$request = $this->loadRequest($request); |
||||||
|
if (!$request) { |
||||||
|
throw new Exception("Unexpected error: fail to parse request"); |
||||||
|
} |
||||||
|
if (!method_exists($service, $method)) { |
||||||
|
// TODO: Can send a proper UNIMPLEMENTED response in the future |
||||||
|
throw new Exception("Method not implemented"); |
||||||
|
} |
||||||
|
|
||||||
|
// Dispatch to actual server logic |
||||||
|
$response = $service->$method($request); |
||||||
|
$this->sendOkResponse($response); |
||||||
|
$this->call = null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,6 @@ |
|||||||
|
<?php |
||||||
|
// DO NOT EDIT |
||||||
|
namespace Grpc\Testing; |
||||||
|
class LoadBalancerStatsServiceStub { |
||||||
|
} |
||||||
|
|
@ -0,0 +1,151 @@ |
|||||||
|
<?php |
||||||
|
/* |
||||||
|
* |
||||||
|
* Copyright 2020 gRPC authors. |
||||||
|
* |
||||||
|
* 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. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
/** |
||||||
|
* This is the PHP xDS Interop test client. This script is meant to be run by |
||||||
|
* the main xDS Interep test runner "run_xds_tests.py", not to be run |
||||||
|
* by itself standalone. |
||||||
|
*/ |
||||||
|
$autoload_path = realpath(dirname(__FILE__).'/../../vendor/autoload.php'); |
||||||
|
require_once $autoload_path; |
||||||
|
|
||||||
|
// The main xds interop test runner will ping this service to ask for |
||||||
|
// the stats of the distribution of the backends, for the next X rpcs. |
||||||
|
class LoadBalancerStatsService |
||||||
|
extends \Grpc\Testing\LoadBalancerStatsServiceStub |
||||||
|
{ |
||||||
|
function getClientStats(\Grpc\Testing\LoadBalancerStatsRequest $request) { |
||||||
|
$num_rpcs = $request->getNumRpcs(); |
||||||
|
$timeout_sec = $request->getTimeoutSec(); |
||||||
|
$rpcs_by_peer = []; |
||||||
|
$num_failures = $num_rpcs; |
||||||
|
|
||||||
|
// Heavy limitation now: the server is blocking, until all |
||||||
|
// the necessary num_rpcs are finished, or timeout is reached |
||||||
|
global $client_thread; |
||||||
|
$start_id = count($client_thread->results) + 1; |
||||||
|
$end_id = $start_id + $num_rpcs; |
||||||
|
$now = hrtime(true); |
||||||
|
$timeout = $now[0] + ($now[1] / 1e9) + $timeout_sec; |
||||||
|
while (true) { |
||||||
|
$curr_hr = hrtime(true); |
||||||
|
$curr_time = $curr_hr[0] + ($curr_hr[1] / 1e9); |
||||||
|
if ($curr_time > $timeout) { |
||||||
|
break; |
||||||
|
} |
||||||
|
// Thread variable seems to be read-only |
||||||
|
$curr_id = count($client_thread->results); |
||||||
|
if ($curr_id >= $end_id) { |
||||||
|
break; |
||||||
|
} |
||||||
|
usleep(50000); |
||||||
|
} |
||||||
|
|
||||||
|
// Tally up results |
||||||
|
$end_id = min($end_id, count($client_thread->results)); |
||||||
|
for ($i = $start_id; $i < $end_id; $i++) { |
||||||
|
$hostname = $client_thread->results[$i]; |
||||||
|
if ($hostname) { |
||||||
|
$num_failures -= 1; |
||||||
|
if (!array_key_exists($hostname, $rpcs_by_peer)) { |
||||||
|
$rpcs_by_peer[$hostname] = 0; |
||||||
|
} |
||||||
|
$rpcs_by_peer[$hostname] += 1; |
||||||
|
} |
||||||
|
} |
||||||
|
$response = new Grpc\Testing\LoadBalancerStatsResponse(); |
||||||
|
$response->setRpcsByPeer($rpcs_by_peer); |
||||||
|
$response->setNumFailures($num_failures); |
||||||
|
return $response; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// This client thread blindly sends a unary RPC to the server once |
||||||
|
// every 1 / qps seconds. |
||||||
|
class ClientThread extends Thread { |
||||||
|
private $server_address_; |
||||||
|
private $target_seconds_between_rpcs_; |
||||||
|
private $fail_on_failed_rpcs_; |
||||||
|
private $autoload_path_; |
||||||
|
public $results; |
||||||
|
|
||||||
|
public function __construct($server_address, $qps, $fail_on_failed_rpcs, |
||||||
|
$autoload_path) { |
||||||
|
$this->server_address_ = $server_address; |
||||||
|
$this->target_seconds_between_rpcs_ = 1.0 / $qps; |
||||||
|
$this->fail_on_failed_rpcs_ = $fail_on_failed_rpcs; |
||||||
|
$this->autoload_path_ = $autoload_path; |
||||||
|
$this->results = []; |
||||||
|
} |
||||||
|
|
||||||
|
public function run() { |
||||||
|
// Autoloaded classes do not get inherited in threads. |
||||||
|
// Hence we need to do this. |
||||||
|
require_once($this->autoload_path_); |
||||||
|
|
||||||
|
$stub = new Grpc\Testing\TestServiceClient($this->server_address_, [ |
||||||
|
'credentials' => Grpc\ChannelCredentials::createInsecure() |
||||||
|
]); |
||||||
|
$request = new Grpc\Testing\SimpleRequest(); |
||||||
|
$target_next_start_us = hrtime(true) / 1000; |
||||||
|
while (true) { |
||||||
|
$now_us = hrtime(true) / 1000; |
||||||
|
$sleep_us = $target_next_start_us - $now_us; |
||||||
|
if ($sleep_us < 0) { |
||||||
|
echo "php xds: warning, rpc takes too long to finish. " |
||||||
|
. "If you consistently see this, the qps is too high.\n"; |
||||||
|
} else { |
||||||
|
usleep($sleep_us); |
||||||
|
} |
||||||
|
$target_next_start_us |
||||||
|
+= ($this->target_seconds_between_rpcs_ * 1000000); |
||||||
|
list($response, $status) |
||||||
|
= $stub->UnaryCall($request)->wait(); |
||||||
|
if ($status->code == Grpc\STATUS_OK) { |
||||||
|
$this->results[] = $response->getHostname(); |
||||||
|
} else { |
||||||
|
if ($this->fail_on_failed_rpcs_) { |
||||||
|
throw new Exception('UnaryCall failed with status ' |
||||||
|
. $status->code); |
||||||
|
} |
||||||
|
$this->results[] = ""; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// This is needed for loading autoload_path in the child thread |
||||||
|
public function start(int $options = PTHREADS_INHERIT_ALL) { |
||||||
|
return parent::start(PTHREADS_INHERIT_NONE); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
// Note: num_channels are currently ignored for now |
||||||
|
$args = getopt('', ['fail_on_failed_rpcs:', 'num_channels:', |
||||||
|
'server:', 'stats_port:', 'qps:']); |
||||||
|
|
||||||
|
$client_thread = new ClientThread($args['server'], $args['qps'], |
||||||
|
$args['fail_on_failed_rpcs'], |
||||||
|
$autoload_path); |
||||||
|
$client_thread->start(); |
||||||
|
|
||||||
|
$server = new Grpc\RpcServer(); |
||||||
|
$server->addHttp2Port('0.0.0.0:'.$args['stats_port']); |
||||||
|
$server->handle(new LoadBalancerStatsService()); |
||||||
|
$server->run(); |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue