Skip to content

Commit

Permalink
add RequestHandler to SpannerClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ajupazhamayil committed Apr 1, 2024
1 parent 0f9b3b7 commit 843be5e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 32 deletions.
8 changes: 0 additions & 8 deletions Core/src/LongRunning/LROTraitV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ trait LROTraitV2
*/
private Serializer $serializer;

/**
* @var string
*/
private $clientClass;

/**
* @var array
*/
Expand Down Expand Up @@ -73,14 +68,12 @@ trait LROTraitV2
private function setLroProperties(
RequestHandler $requestHandler,
Serializer $serializer,
string $clientClass,
array $lroCallables,
array $lroResponseMappers,
$resource = null
) {
$this->requestHandler = $requestHandler;
$this->serializer = $serializer;
$this->clientClass = $clientClass;
$this->lroCallables = $lroCallables;
$this->lroResponseMappers = $lroResponseMappers;
$this->lroResource = $resource;
Expand All @@ -101,7 +94,6 @@ public function resumeOperation($operationName, array $info = [])
$this->serializer,
$this->lroCallables,
$this->lroResponseMappers,
$this->clientClass,
$operationName,
$info
);
Expand Down
9 changes: 0 additions & 9 deletions Core/src/LongRunning/LongRunningOperationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ class LongRunningOperationManager
*/
private Serializer $serializer;

/**
* @var string
*/
private string $clientClass;

/**
* @var string
*/
Expand Down Expand Up @@ -87,8 +82,6 @@ class LongRunningOperationManager
* callable Type should correspond to an expected value of
* operation.metadata.typeUrl.
* @param array $lroResponseMappers A list of mappers for deserializing operation results.
* @param string $clientClass The client class that will be passed on to the
* sendRequest method of the $requestHandler.
* @param string $name The Operation name.
* @param array $info [optional] The operation info.
*/
Expand All @@ -98,14 +91,12 @@ public function __construct(
array $callablesMap,
array $lroResponseMappers,
$name,
string $clientClass,
array $info = []
) {
$this->serializer = $serializer;
$this->requestHandler = $requestHandler;
$this->callablesMap = $callablesMap;
$this->lroResponseMappers = $lroResponseMappers;
$this->clientClass = $clientClass;
$this->name = $name;
$this->info = $info;
}
Expand Down
4 changes: 2 additions & 2 deletions Spanner/src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ public function __construct(
$this->setLroProperties(
$requestHandler,
$serializer,
DatabaseAdminClient::class,
$lroCallables,
self::$lroResponseMappers,
$this->name
Expand Down Expand Up @@ -702,7 +701,8 @@ public function iam()
$this->requestHandler,
$this->serializer,
SpannerClient::class,
$this->name);
$this->name
);
}

return $this->iam;
Expand Down
83 changes: 70 additions & 13 deletions Spanner/src/SpannerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@

namespace Google\Cloud\Spanner;

use Google\ApiCore\ClientOptionsTrait;
use Google\ApiCore\Serializer;
use Google\Auth\FetchAuthTokenInterface;
use Google\Cloud\Core\ApiHelperTrait;
use Google\Cloud\Core\ArrayTrait;
use Google\Cloud\Core\ClientTrait;
use Google\Cloud\Core\DetectProjectIdTrait;
use Google\Cloud\Core\Exception\GoogleException;
use Google\Cloud\Core\Int64;
use Google\Cloud\Core\Iterator\ItemIterator;
use Google\Cloud\Core\Iterator\PageIterator;
use Google\Cloud\Core\LongRunning\LongRunningOperation;
use Google\Cloud\Core\LongRunning\LROTrait;
use Google\Cloud\Core\LongRunning\LROTraitV2;
use Google\Cloud\Core\RequestHandler;
use Google\Cloud\Core\ValidateTrait;
use Google\Cloud\Spanner\Admin\Database\V1\DatabaseAdminClient;
use Google\Cloud\Spanner\Admin\Instance\V1\InstanceAdminClient;
Expand Down Expand Up @@ -117,15 +121,24 @@
*/
class SpannerClient
{
use ApiHelperTrait;
use ClientOptionsTrait;
use ArrayTrait;
use ClientTrait;
use LROTrait;
use DetectProjectIdTrait;
use LROTraitV2;
use ValidateTrait;
use RequestTrait;

const VERSION = '1.74.0';

const FULL_CONTROL_SCOPE = 'https://www.googleapis.com/auth/spanner.data';
const ADMIN_SCOPE = 'https://www.googleapis.com/auth/spanner.admin';
private const GAPIC_KEYS = [
GapicSpannerClient::class,
InstanceAdminClient::class,
DatabaseAdminClient::class
];

/**
* @var Connection\ConnectionInterface
Expand Down Expand Up @@ -233,9 +246,16 @@ public function __construct(array $config = [])
'projectIdRequired' => true,
'hasEmulator' => (bool) $emulatorHost,
'emulatorHost' => $emulatorHost,
'queryOptions' => []
'queryOptions' => [],
'transportConfig' => [
'grpc' => [
// increase default limit to 4MB to prevent metadata exhausted errors
'stubOpts' => ['grpc.max_metadata_size' => 4 * 1024 * 1024,]
]
]
];

# Check this to see it needs to be removed, for now keep it.
if (!empty($config['useDiscreteBackoffs'])) {
$config = array_merge_recursive($config, [
'retries' => 0,
Expand All @@ -245,55 +265,92 @@ public function __construct(array $config = [])
]);
}

# delete the connection object
$this->connection = new Grpc($this->configureAuthentication($config));
$this->returnInt64AsObject = $config['returnInt64AsObject'];

$this->setLroProperties(new LongRunningConnection($this->connection), [
$lroCallables = [
[
'typeUrl' => 'type.googleapis.com/google.spanner.admin.instance.v1.UpdateInstanceMetadata',
'callable' => function ($instance) {
$name = InstanceAdminClient::parseName($instance['name'])['instance'];
return $this->instance($name, $instance);
}
], [
],
[
'typeUrl' => 'type.googleapis.com/google.spanner.admin.database.v1.CreateDatabaseMetadata',
'callable' => function ($database) {
$databaseNameComponents = DatabaseAdminClient::parseName($database['name']);
$instanceName = $databaseNameComponents['instance'];
$databaseName = $databaseNameComponents['database'];

$instance = $this->instance($instanceName);
return $instance->database($databaseName);
}
], [
],
[
'typeUrl' => 'type.googleapis.com/google.spanner.admin.database.v1.RestoreDatabaseMetadata',
'callable' => function ($database) {
$databaseNameComponents = DatabaseAdminClient::parseName($database['name']);
$instanceName = $databaseNameComponents['instance'];
$databaseName = $databaseNameComponents['database'];

$instance = $this->instance($instanceName);
return $instance->database($databaseName);
}
],[
],
[
'typeUrl' => 'type.googleapis.com/google.spanner.admin.instance.v1.CreateInstanceMetadata',
'callable' => function ($instance) {
$name = InstanceAdminClient::parseName($instance['name'])['instance'];
return $this->instance($name, $instance);
}
], [
],
[
'typeUrl' => 'type.googleapis.com/google.spanner.admin.database.v1.CreateBackupMetadata',
'callable' => function ($backup) {
$backupNameComponents = DatabaseAdminClient::parseName($backup['name']);
$instanceName = $backupNameComponents['instance'];

$instance = $this->instance($instanceName);
return $instance->backup($backup['name'], $backup);
}
]
]);
];

$this->setLroProperties(
$this->requestHandler,
$this->serializer,
$lroCallables,
self::$lroResponseMappers
);

$this->directedReadOptions = $config['directedReadOptions'] ?? [];

// Configure GAPIC client options
$config = $this->buildClientOptions($config);
$config['credentials'] = $this->createCredentialsWrapper(
$config['credentials'],
$config['credentialsConfig'],
$config['universeDomain']
);
$this->projectId = $this->detectProjectId($config);
$this->serializer = new Serializer([], [
'google.protobuf.Value' => function ($v) {
return $this->flattenValue($v);
},
'google.protobuf.ListValue' => function ($v) {
return $this->flattenListValue($v);
},
'google.protobuf.Struct' => function ($v) {
return $this->flattenStruct($v);
},
'google.protobuf.Timestamp' => function ($v) {
return $this->formatTimestampFromApi($v);
}
]);

$this->requestHandler = new RequestHandler(
$this->serializer,
self::GAPIC_KEYS,
$config
);
}

/**
Expand Down

0 comments on commit 843be5e

Please sign in to comment.