Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable gRPC GCP channel management library for the spanner API #1146

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Spanner/src/Connection/Grpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
use Google\Protobuf\ListValue;
use Google\Protobuf\Struct;
use Google\Protobuf\Value;
use Grpc\Gcp\ApiConfig;
use Grpc\Gcp\Config;
use Grpc\UnaryCall;

/**
Expand Down Expand Up @@ -120,11 +122,25 @@ class Grpc implements ConnectionInterface
*/
private $longRunningGrpcClients;

private function enableConnectionManagement($hostName, $confPath)
{
// TODO(ddyihai): move this function to GrpcTrait, if we are going
// to enable the grpc-gcp library for other apis.
$conf = new ApiConfig();
$conf->mergeFromJsonString(file_get_contents($confPath));
$config = new Config($hostName, $conf);
return $config;
}

/**
* @param array $config [optional]
*/
public function __construct(array $config = [])
{
if (getenv("ENABLE_GCP_OPTIMIZER") === false) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

// Enable the GCP optimizer by default.
putenv('ENABLE_GCP_OPTIMIZER=TRUE');
}
$this->serializer = new Serializer([
'commit_timestamp' => function ($v) {
return $this->formatTimestampFromApi($v);
Expand Down Expand Up @@ -152,6 +168,20 @@ public function __construct(array $config = [])
? $config['authHttpHandler']
: null
);

if (getenv('ENABLE_GCP_OPTIMIZER') == 'TRUE') {
if (extension_loaded('sysvshm')) {
$api = 'spanner';
$confPath = __DIR__. "/../V1/resources/$api.grpc.config";
$grpcGcpConfig = $this->enableConnectionManagement('spanner.googleapis.com', $confPath);
$grpcConfig['transportConfig']['grpc'] = [
'stubOpts' => [
'grpc_call_invoker' => $grpcGcpConfig->callInvoker()
]
];
}
}

$this->spannerClient = isset($config['gapicSpannerClient'])
? $config['gapicSpannerClient']
: new SpannerClient($grpcConfig);
Expand Down
30 changes: 30 additions & 0 deletions Spanner/src/V1/resources/spanner.grpc.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"channelPool":{"maxSize":10,"maxConcurrentStreamsLowWatermark":1},
"method":[
{"name":["/google.spanner.v1.Spanner/CreateSession"],
"affinity":{"command":"BIND", "affinityKey":"name"}},
{"name":["/google.spanner.v1.Spanner/GetSession"],
"affinity":{"command":"BOUND", "affinityKey":"name"}},
{"name":["/google.spanner.v1.Spanner/DeleteSession"],
"affinity":{"command":"UNBIND", "affinityKey":"name"}},
{"name":["/google.spanner.v1.Spanner/ExecuteSql"],
"affinity":{"command":"BOUND", "affinityKey":"session"}},
{"name":["/google.spanner.v1.Spanner/ExecuteStreamingSql"],
"affinity":{"command":"BOUND", "affinityKey":"session"}},
{"name":["/google.spanner.v1.Spanner/Read"],
"affinity":{"command":"BOUND", "affinityKey":"session"}},
{"name":["/google.spanner.v1.Spanner/StreamingRead"],
"affinity":{"command":"BOUND", "affinityKey":"session"}},
{"name":["/google.spanner.v1.Spanner/BeginTransaction"],
"affinity":{"command":"BOUND", "affinityKey":"session"}},
{"name":["/google.spanner.v1.Spanner/Commit"],
"affinity":{"command":"BOUND", "affinityKey":"session"}},
{"name":["/google.spanner.v1.Spanner/Rollback"],
"affinity":{"command":"BOUND", "affinityKey":"session"}},
{"name":["/google.spanner.v1.Spanner/PartitionQuery"],
"affinity":{"command":"BOUND", "affinityKey":"session"}},
{"name":["/google.spanner.v1.Spanner/PartitionRead"],
"affinity":{"command":"BOUND", "affinityKey":"session"}}
]
}

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"monolog/monolog": "~1",
"psr/http-message": "1.0.*",
"ramsey/uuid": "~3",
"google/gax": "^0.36"
"google/gax": "^0.36",
"google/grpc-gcp": "v0.1.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8|^5.0",
Expand Down