Skip to content
Merged
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
35 changes: 2 additions & 33 deletions src/Spanner/Session/CacheSessionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -473,12 +446,8 @@ public function clear()
if ($ex instanceof NotFoundException) {
continue;
}

$failed[] = $session;
}
}

return $failed;
}

/**
Expand Down
33 changes: 6 additions & 27 deletions tests/unit/Spanner/Session/CacheSessionPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,6 +47,7 @@ public function setUp()
$this->checkAndSkipGrpcTests();
putenv('GOOGLE_CLOUD_SYSV_ID=U');
$this->time = time();
MockValues::initialize();
}

/**
Expand Down Expand Up @@ -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()
Expand All @@ -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
*/
Expand Down