googleapis / google-cloud-php

Google Cloud Client Library for PHP
https://cloud.google.com/php/docs/reference
Apache License 2.0
1.09k stars 436 forks source link

[Spanner] misleading error when incorrect name of db/project/instance is used #5364

Closed oprudkyi closed 1 year ago

oprudkyi commented 2 years ago

Steps to reproduce

When I am trying to access non-existent db there is an error

A session did not become available in the allotted number of attempts. {"exception":"[object] (RuntimeException(code: 0): A session did not become available in the allotted number of attempts. at /var/www/vendor/google/cloud-spanner/src/Session/CacheSessionPool.php:741)
[stacktrace]
#0 /var/www/vendor/google/cloud-core/src/Lock/LockTrait.php(67): Google\\Cloud\\Spanner\\Session\\CacheSessionPool->Google\\Cloud\\Spanner\\Session\\{closure}()
#1 /var/www/vendor/google/cloud-spanner/src/Session/CacheSessionPool.php(745): Google\\Cloud\\Core\\Lock\\SemaphoreLock->synchronize(Object(Closure))
#2 /var/www/vendor/google/cloud-spanner/src/Session/CacheSessionPool.php(296): Google\\Cloud\\Spanner\\Session\\CacheSessionPool->waitForNextAvailableSession()
#3 /var/www/vendor/google/cloud-spanner/src/Database.php(2022): Google\\Cloud\\Spanner\\Session\\CacheSessionPool->acquire('rw')
#4 /var/www/vendor/google/cloud-spanner/src/Database.php(849): Google\\Cloud\\Spanner\\Database->selectSession('rw', Array)
#5 /var/www/vendor/colopl/laravel-spanner/src/Colopl/Spanner/Concerns/ManagesTransactions.php(88): Google\\Cloud\\Spanner\\Database->runTransaction(Object(Closure), Array)
#6 /var/www/vendor/colopl/laravel-spanner/src/Colopl/Spanner/Connection.php(510): Colopl\\Spanner\\Connection->Colopl\\Spanner\\Concerns\\{closure}()
#7 /var/www/vendor/colopl/laravel-spanner/src/Colopl/Spanner/Concerns/ManagesTransactions.php(93): Colopl\\Spanner\\Connection->withSessionNotFoundHandling(Object(Closure))

probably existence (accessibility) of project/instance/db should be checked somehow before or at least error message should include project/instance/db to simplify debugging

moved from https://github.com/colopl/laravel-spanner/issues/40

saranshdhingra commented 2 years ago

Hi @oprudkyi I tried the following code snippet:

$instanceId = "";
$databaseId = "";
$spanner = new SpannerClient();
$instance = $spanner->instance($instanceId);
$database = $instance->database($databaseId);

if (!$database->exists()) {
    throw new \LogicException("Database $databaseId does not exist");
}

The line $database = $instance->database($databaseId); doesn't throw an exception on using an incorrect database id.

Can you share the code snippet that you used?

oprudkyi commented 2 years ago

Hi @saranshdhingra

I am using https://github.com/colopl/laravel-spanner basically you have to use CacheSessionPool and Database->runTransaction to get this error

something like

use Google\Cloud\Spanner\SpannerClient;
use Google\Cloud\Spanner\Session\CacheSessionPool;
use Google\Auth\Cache\SysVCacheItemPool;
use Google\Cloud\Spanner\Transaction;

$cache = new SysVCacheItemPool();
$sessionPool = new CacheSessionPool($cache, ['maxSessions' => 100]);

$spanner = new SpannerClient([ 'projectId' => 'project' ]);
$connection = $spanner->connect("instance", "database", ['sessionPool' => $sessionPool]);

$connection->runTransaction(function (Transaction $t) {
    $t->select("SELECT 1");
    $t->commit();
});
taka-oyama commented 1 year ago

Seems like this was fixed?

vishwarajanand commented 1 year ago

Yes, we should close this!