From 48235b42703bc4d4b4db6e913305a091060e5763 Mon Sep 17 00:00:00 2001 From: Taka Oyama Date: Thu, 13 Apr 2023 12:51:36 +0900 Subject: [PATCH 1/6] feature: separate connection per session so it is not shared --- src/SpannerServiceProvider.php | 14 ++++++++------ tests/Console/SessionsCommandTest.php | 4 ++-- tests/Console/WarmupCommandTest.php | 5 +---- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/SpannerServiceProvider.php b/src/SpannerServiceProvider.php index 3137ec08..3e781366 100644 --- a/src/SpannerServiceProvider.php +++ b/src/SpannerServiceProvider.php @@ -67,8 +67,8 @@ protected function createSpannerConnection(array $config): Connection $config['database'], $config['prefix'], $config, - $this->createAuthCache(), - $this->createSessionPool($config['session_pool'] ?? []) + $this->createAuthCache($config['name']), + $this->createSessionPool($config['name'], $config['session_pool'] ?? []) ); } @@ -87,22 +87,24 @@ protected function parseConfig(array $config, string $name): array } /** + * @param string $name * @param array $sessionPoolConfig * @return SessionPoolInterface */ - protected function createSessionPool(array $sessionPoolConfig): SessionPoolInterface + protected function createSessionPool(string $name, array $sessionPoolConfig): SessionPoolInterface { - $cachePath = storage_path(implode(DIRECTORY_SEPARATOR, ['framework', 'spanner'])); + $cachePath = storage_path(implode(DIRECTORY_SEPARATOR, ['framework', 'spanner', $name])); $adapter = new FilesystemAdapter('session', 0, $cachePath); return new CacheSessionPool($adapter, $sessionPoolConfig); } /** + * @param string $name * @return CacheItemPoolInterface */ - protected function createAuthCache(): CacheItemPoolInterface + protected function createAuthCache(string $name, ): CacheItemPoolInterface { - $cachePath = storage_path(implode(DIRECTORY_SEPARATOR, ['framework', 'spanner'])); + $cachePath = storage_path(implode(DIRECTORY_SEPARATOR, ['framework', 'spanner', $name])); return new FilesystemAdapter('auth', 0, $cachePath); } diff --git a/tests/Console/SessionsCommandTest.php b/tests/Console/SessionsCommandTest.php index e3dea730..7b576366 100644 --- a/tests/Console/SessionsCommandTest.php +++ b/tests/Console/SessionsCommandTest.php @@ -57,8 +57,8 @@ public function test_no_args(): void ->run(); $this->artisan('spanner:sessions') - ->expectsOutputToContain('main contains 1 session(s).') - ->expectsOutputToContain('alternative contains 1 session(s).') + ->expectsOutputToContain('main contains 2 session(s).') + ->expectsOutputToContain('alternative contains 2 session(s).') ->assertSuccessful() ->run(); } diff --git a/tests/Console/WarmupCommandTest.php b/tests/Console/WarmupCommandTest.php index 4bf3873c..5c9ef21d 100644 --- a/tests/Console/WarmupCommandTest.php +++ b/tests/Console/WarmupCommandTest.php @@ -32,12 +32,9 @@ public function test_no_args(): void $this->setUpDatabaseOnce($conn); } - // Emulator cannot distinguish different connections - $altWarmupCount = env('SPANNER_EMULATOR_HOST') ? 0 : 1; - $this->artisan('spanner:warmup') ->expectsOutputToContain("Warmed up 1 sessions for main") - ->expectsOutputToContain("Warmed up {$altWarmupCount} sessions for alternative") + ->expectsOutputToContain("Warmed up 1 sessions for alternative") ->assertSuccessful() ->run(); From a592dae822270a55342f5f2e8766b5b76a272bd3 Mon Sep 17 00:00:00 2001 From: Taka Oyama Date: Thu, 13 Apr 2023 12:58:48 +0900 Subject: [PATCH 2/6] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a92d6677..752cacd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Fixed Changed - Use google-cloud-php's CacheSessionPool since the [concerned bug](https://github.com/googleapis/google-cloud-php/issues/5567) has been fixed in [v1.53](https://github.com/googleapis/google-cloud-php-spanner/releases/tag/v1.58.2). (#90) +- Separate session pool and authentication per connection so transaction works properly. # v5.0.0 From 2496474817bdedc2f979a4742774d8159e1d3792 Mon Sep 17 00:00:00 2001 From: Taka Oyama Date: Thu, 13 Apr 2023 13:00:44 +0900 Subject: [PATCH 3/6] fix --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 752cacd5..0e3aad09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ Fixed Changed - Use google-cloud-php's CacheSessionPool since the [concerned bug](https://github.com/googleapis/google-cloud-php/issues/5567) has been fixed in [v1.53](https://github.com/googleapis/google-cloud-php-spanner/releases/tag/v1.58.2). (#90) -- Separate session pool and authentication per connection so transaction works properly. +- Separate session pool and authentication per connection so transaction works properly. (#89) # v5.0.0 From 475bba524df1d8be83a7b66f93b29a63e8284ef1 Mon Sep 17 00:00:00 2001 From: Taka Oyama Date: Thu, 13 Apr 2023 14:19:24 +0900 Subject: [PATCH 4/6] fix --- tests/ConnectionTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index bc2a2be1..4d559df5 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -249,7 +249,7 @@ public function test_AuthCache_with_FileSystemAdapter(): void { $conn = $this->getDefaultConnection(); $conn->select('SELECT 1'); - self::assertDirectoryExists(storage_path('framework/spanner/auth')); + self::assertDirectoryExists(storage_path("framework/spanner/{$conn->getName()}/auth")); } public function testSessionPool(): void @@ -272,7 +272,7 @@ public function test_session_pool_with_FileSystemAdapter(): void { $conn = $this->getDefaultConnection(); $conn->select('SELECT 1'); - self::assertDirectoryExists(storage_path('framework/spanner/session')); + self::assertDirectoryExists(storage_path("framework/spanner/{$conn->getName()}/session")); } public function test_clearSessionPool(): void From e9256e6f8dbdff4719e4af2875069d9706d34190 Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Fri, 14 Apr 2023 11:35:23 +0900 Subject: [PATCH 5/6] Update src/SpannerServiceProvider.php Co-authored-by: Tomohito YABU --- src/SpannerServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SpannerServiceProvider.php b/src/SpannerServiceProvider.php index 3e781366..c165b285 100644 --- a/src/SpannerServiceProvider.php +++ b/src/SpannerServiceProvider.php @@ -102,7 +102,7 @@ protected function createSessionPool(string $name, array $sessionPoolConfig): Se * @param string $name * @return CacheItemPoolInterface */ - protected function createAuthCache(string $name, ): CacheItemPoolInterface + protected function createAuthCache(string $name): CacheItemPoolInterface { $cachePath = storage_path(implode(DIRECTORY_SEPARATOR, ['framework', 'spanner', $name])); return new FilesystemAdapter('auth', 0, $cachePath); From 2653b15c54721655655ec9a05e74415cd541844b Mon Sep 17 00:00:00 2001 From: Taka Oyama Date: Fri, 14 Apr 2023 11:49:56 +0900 Subject: [PATCH 6/6] no need to skip this --- tests/Console/SessionsCommandTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/Console/SessionsCommandTest.php b/tests/Console/SessionsCommandTest.php index 7b576366..c6bae3a7 100644 --- a/tests/Console/SessionsCommandTest.php +++ b/tests/Console/SessionsCommandTest.php @@ -42,10 +42,6 @@ protected function createSessions(Connection $connection, int $amount): array public function test_no_args(): void { - if (getenv('SPANNER_EMULATOR_HOST')) { - $this->markTestSkipped('Cannot list sessions on emulator'); - } - foreach (['main', 'alternative'] as $name) { $conn = $this->getConnection($name); assert($conn instanceof Connection);