diff --git a/src/Spanner/Session/CacheSessionPool.php b/src/Spanner/Session/CacheSessionPool.php index e49a117872de..a533938e8d23 100644 --- a/src/Spanner/Session/CacheSessionPool.php +++ b/src/Spanner/Session/CacheSessionPool.php @@ -311,22 +311,12 @@ public function keepAlive(Session $session) * would leave 6 sessions in the queue. The count of items to be deleted will * be rounded up in the case of a fraction. * - * A session may be removed from the cache, but still tracked as active by - * the Spanner backend if a delete operation failed. To ensure you do not - * exceed the maximum number of sessions available per node, please be sure - * to check the return value of this method to be certain all sessions have - * been deleted. - * * Please note this method will attempt to synchronously delete sessions and * will block until complete. * * @param int $percent The percentage to downsize the pool by. Must be * between 1 and 100. - * @return array An associative array containing a key `deleted` which holds - * an integer value representing the number of queued sessions - * deleted on the backend and a key `failed` which holds a list of - * queued {@see Google\Cloud\Spanner\Session\Session} objects which - * failed to delete. + * @return int The number of sessions removed from the pool. * @throws \InvaldArgumentException */ public function downsize($percent) @@ -335,7 +325,6 @@ public function downsize($percent) throw new \InvalidArgumentException('The provided percent must be between 1 and 100.'); } - $failed = []; $toDelete = $this->config['lock']->synchronize(function () use ($percent) { $item = $this->cacheItemPool->getItem($this->cacheKey); $data = (array) $item->get() ?: $this->initialize(); @@ -361,15 +350,10 @@ public function downsize($percent) if ($ex instanceof NotFoundException) { continue; } - - $failed[] = $session; } } - return [ - 'deleted' => count($toDelete) - count($failed), - 'failed' => $failed - ]; + return count($toDelete); } /** @@ -430,22 +414,11 @@ public function warmup() /** * Clear the cache and attempt to delete all sessions in the pool. * - * A session may be removed from the cache, but still tracked as active by - * the Spanner backend if a delete operation failed. To ensure you do not - * exceed the maximum number of sessions available per node, please be sure - * to check the return value of this method to be certain all sessions have - * been deleted. - * * Please note this method will attempt to synchronously delete sessions and * will block until complete. - * - * @return array An array containing a list of - * {@see Google\Cloud\Spanner\Session\Session} objects which failed - * to delete. */ public function clear() { - $failed = []; $sessions = $this->config['lock']->synchronize(function () { $sessions = []; $item = $this->cacheItemPool->getItem($this->cacheKey); @@ -473,12 +446,8 @@ public function clear() if ($ex instanceof NotFoundException) { continue; } - - $failed[] = $session; } } - - return $failed; } /** diff --git a/tests/unit/Spanner/Session/CacheSessionPoolTest.php b/tests/unit/Spanner/Session/CacheSessionPoolTest.php index 452281eec4cb..199779ede381 100644 --- a/tests/unit/Spanner/Session/CacheSessionPoolTest.php +++ b/tests/unit/Spanner/Session/CacheSessionPoolTest.php @@ -18,6 +18,7 @@ namespace Google\Cloud\Tests\Unit\Spanner\Session; use Google\Auth\Cache\MemoryCacheItemPool; +use Google\Cloud\Core\Lock\MockValues; use Google\Cloud\Spanner\Database; use Google\Cloud\Spanner\Session\CacheSessionPool; use Google\Cloud\Spanner\Session\Session; @@ -46,6 +47,7 @@ public function setUp() $this->checkAndSkipGrpcTests(); putenv('GOOGLE_CLOUD_SYSV_ID=U'); $this->time = time(); + MockValues::initialize(); } /** @@ -254,9 +256,11 @@ public function testDownsizeDeletes($percent, $expectedDeleteCount) 'toCreate' => [] ])); $pool->setDatabase($this->getDatabase(false, true)); - $response = $pool->downsize($percent); - $this->assertEquals($expectedDeleteCount, $response['deleted']); + $this->assertEquals( + $expectedDeleteCount, + $pool->downsize($percent) + ); } public function downsizeDataProvider() @@ -268,31 +272,6 @@ public function downsizeDataProvider() ]; } - public function testDownsizeFails() - { - $time = time() + 3600; - $pool = new CacheSessionPoolStub($this->getCacheItemPool([ - 'queue' => [ - [ - 'name' => 'session0', - 'expiration' => $time - ], - [ - 'name' => 'session1', - 'expiration' => $time - ] - ], - 'inUse' => [], - 'toCreate' => [] - ])); - $pool->setDatabase($this->getDatabase()); - $response = $pool->downsize(100); - - $this->assertEquals(0, $response['deleted']); - $this->assertEquals(1, count($response['failed'])); - $this->assertContainsOnlyInstancesOf(Session::class, $response['failed']); - } - /** * @dataProvider invalidPercentDownsizeDataProvider */