[Spanner] requestTimeout option not working as expected
We expected requestTimeout to terminate the request when it exceeds the set time but when we tried to get thousands of rows at once, the request did not timeout and waited for the results to return.
Environment details
- OS: Alpine Linux 3.10.1
- PHP version: 7.2.20
- Package name and version: googleapis/google-cloud-php-spanner 1.17.0
Steps to reproduce
- create a table and insert millions of rows.
- set
$client = new SpannerClient(['requestTimeout' => 1]);. - send SQL statement which returns thousands of rows.
- php times out after 30 seconds. (seems like it was waiting for a response on
Google\ApiCore\ServerStream->readAll())
Code example
$client = new SpannerClient(['requestTimeout' => 1]);
$db = $client->connect($instanceId, $databaseId);
$db->execute('SELECT * from LargeTable')->rows()->current();
@taka-oyama,
I've tried reproducing this with gRPC v1.21 and v1.22 - so far I've been receiving a deadline exceeded exception almost immediately.
Would you please be able to share the version of gRPC being utilized?
gRPC version we are using is v1.20. We will try to reproduce this with the latest version.
@dwsupplee Hi, I investigated more deeply. As a result, it turned out that \Google\ApiCore\Serializer::encodeMessage was spending a lot of time, not gRPC communication.
For example, if running LIMIT 1000 query, \Google\ApiCore\Serializer::encodeElement is called more than 1000 times, even if only the first row is fetched by calling Generator::current().
Code example
require __DIR__.'/vendor/autoload.php';
use Google\Cloud\Spanner\SpannerClient;
use OpenCensus\Trace\Tracer;
opencensus_trace_method(\Google\ApiCore\ServerStream::class, 'readAll');
opencensus_trace_method(\Grpc\Call::class, 'startBatch');
opencensus_trace_method(\Generator::class, 'current');
opencensus_trace_method(\Google\ApiCore\Serializer::class, 'encodeMessage');
opencensus_trace_method(\Google\ApiCore\Serializer::class, 'encodeElement');
$exporter = new \OpenCensus\Trace\Exporter\OneLineEchoExporter();
Tracer::start($exporter);
$instanceId = 'xxx';
$databaseId = 'xxx';
$client = new SpannerClient(['requestTimeout' => 1]);
$db = $client->connect($instanceId, $databaseId);
$row = $db->execute('SELECT * from LargeTable LIMIT 1000')->rows()->current();
Output
[ 5409.30 ms] /test.php
[http.status_code] 200
[http.host] localhost:8080
[http.port] 80
[http.method] GET
[http.path] /test.php
[http.user_agent] curl/7.54.0
[ 258.49 ms] Grpc\Call::startBatch
[ 744.52 ms] Grpc\Call::startBatch
[ 12.41 ms] Google\ApiCore\Serializer::encodeMessage
[ 2.96 ms] Google\ApiCore\Serializer::encodeElement
[ 0.81 ms] Google\ApiCore\Serializer::encodeElement
[ 0.04 ms] Google\ApiCore\Serializer::encodeElement
[ 0.04 ms] Google\ApiCore\Serializer::encodeElement
[ 0.71 ms] Google\ApiCore\Serializer::encodeElement
[ 0.04 ms] Google\ApiCore\Serializer::encodeElement
[ 0.04 ms] Google\ApiCore\Serializer::encodeElement
[ 3461.13 ms] Generator::current
[ 0.39 ms] Grpc\Call::startBatch
[ 0.01 ms] Google\ApiCore\ServerStream::readAll
[ 24.62 ms] Google\ApiCore\ServerStream::readAll
[ 22.09 ms] Grpc\Call::startBatch
[ 3398.86 ms] Google\ApiCore\Serializer::encodeMessage
[ 5.23 ms] Google\ApiCore\Serializer::encodeElement
[ 3.82 ms] Google\ApiCore\Serializer::encodeElement
[ 1.70 ms] Google\ApiCore\Serializer::encodeElement
[ 0.04 ms] Google\ApiCore\Serializer::encodeElement
[ 0.87 ms] Google\ApiCore\Serializer::encodeElement
[ 0.05 ms] Google\ApiCore\Serializer::encodeElement
[ 1.49 ms] Google\ApiCore\Serializer::encodeElement
[ 0.04 ms] Google\ApiCore\Serializer::encodeElement
... ( more than 1000 times)
[ 0.01 ms] Generator::current
[ 6.32 ms] Google\ApiCore\ServerStream::readAll
[ 0.03 ms] Grpc\Call::startBatch
[ 0.02 ms] Grpc\Call::startBatch
Thank you very much for the added details. That helps greatly as we look in to this.
@taka-oyama & @castaneai,
I'm still having trouble replicating this issue as described - would it be possible to share a sample set of data in a similar shape to your use case?
In regards to the large number of encodeElement calls - the first response the library receives from the streaming call is a PartialResultSet. This response can contain a large number of rows (in some cases even the entire expected result set), depending on how large each row is. We have a centralized point in our request/response cycle which encodes responses from the API into a common format and then passes this response off to the client library code. It is at this point the client library code takes that response (yielded to us from the generator provided by the gRPC call) and processes each individual row, then yields them up to the user. This means there's a good chance all expected rows will actually be encoded up front as opposed to one by one as you yield them in user land code. This is something we have plans to improve upon, most likely as part of a 2.0 release for Spanner (as there is a good chance we would need to include some breaking changes to improve the workflow). In the meantime, is it possible to limit the scope of your query to information you plan to utilize as part of the PHP request?
@dwsupplee Thank you for your investigating.
We found this problem while investigating other different problems. In a real use case, it is rare to execute such a large table SELECT.
Anyway, it is desirable to be able to set a timeout that includes an encodeElement in our use case.
I watched how the requestTimeout parameter is passed.
The requestTimeout value is specified when creating the SpannerClient, and is stored in the $requestTimeout field of the RequestWrapperTrait trait of the GrpcRequestWrapper() object.
- The
SpannerClientconstructor creates a connection using the gRPC class in which aGrpcRequestWrapperis created. The parameter containing therequestTimeoutis then successfully passed along this chain.
In the process of calling Database::execute('SELECT * from LargeTable'), GrpcRequestWrapper->send, the requestTimeout is converted to milliseconds and under the name noRetriesRpcTimeoutMillis participates in the request stack, through the gRPC module generated in GapicClientTrait->createCallStack.

In my opinion, the noRetriesRpcTimeoutMillis parameter is supplied to calls implemented in the gRPC module, but somehow gets ignored.
Configuration: Ubuntu 18.10 x64; php 7.2.19; gRPC version: 1.22.1
Thanks for digging in here @andrewinc.
In my opinion, the noRetriesRpcTimeoutMillis parameter is supplied to calls implemented in the gRPC module, but somehow gets ignored.
From my research, the timeout at the gRPC layer was respected. Were you able to confirm differently?
@dwsupplee I saw that when sending a request an array of $optionalArgs is created and filled, but it is not involved anywhere. Maybe this is precisely the reason for the problem that you need to pass this array as a parameter to $backoff->execute (..)?
On the other hand, when I execute code similar to the one in the first post, I get an Exception with status = "DEADLINE_EXCEEDED"
If I execute with a big restriction, for example 'requestTimeout' => 7
then I get the expected response (see PHP-code on screen):
=========== Hello World
Need to check on any large table if the problem is resolved?
As @castaneai and @dwsupplee noted, the problem cause is not receiving data, but converting it. I guess a row with lots of fields of different types (including arrays!) would be hard to en/decode and thus would lead to the problem.
I managed to reproduce the issue fetching 4000 rows from a 10000 row table using this definition:
CREATE TABLE TableName (
id INT64 NOT NULL,
tbool BOOL,
tfloat FLOAT64,
tstring STRING(MAX),
tdate DATE,
ttimestamp TIMESTAMP,
tarray ARRAY<BOOL>
) PRIMARY KEY (id)
(There is no need to use long string/array values). With a 4 second timeout I get a DeadlineExceededException in about 10 seconds.
Thanks @ava12 for looking into this and for confirming the issue.
I believe it's not possible to respect the timeout given the issue happens when the client has to deal with an unusually large amount of data. In this situation @taka-oyama, I think it makes more sense to do the reads in batches or use the $database->read() API so that the reads can be streamed.
Understood. Thank you for looking into it.