From b4595a6b52417c716f8e70563bb5a7ef05067707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Sun, 25 Apr 2021 05:42:22 +0200 Subject: [PATCH] fix: do not keep references to invalidated clients (#1093) SpannerImpl would keep a reference to all database clients that had been created and then invalidated because the database was deleted or did not exists in the first place. If the getDatabaseClient(String) method would be called multiple times for the same invalid database, the memory usage of the application would continue to increase. Fixes #1089 --- .../java/com/google/cloud/spanner/SpannerImpl.java | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerImpl.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerImpl.java index eeb4ad51f5..f16382e835 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerImpl.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerImpl.java @@ -92,9 +92,6 @@ private static String nextDatabaseClientId(DatabaseId databaseId) { private final CloseableExecutorProvider asyncExecutorProvider; - @GuardedBy("this") - private final List invalidatedDbClients = new ArrayList<>(); - @GuardedBy("this") private final Map sessionClients = new HashMap<>(); @@ -204,9 +201,8 @@ public DatabaseClient getDatabaseClient(DatabaseId db) { checkClosed(); String clientId = null; if (dbClients.containsKey(db) && !dbClients.get(db).pool.isValid()) { - // Move the invalidated client to a separate list, so we can close it together with the - // other database clients when the Spanner instance is closed. - invalidatedDbClients.add(dbClients.get(db)); + // Close the invalidated client and remove it. + dbClients.get(db).closeAsync(new ClosedException()); clientId = dbClients.get(db).clientId; dbClients.remove(db); } @@ -253,8 +249,7 @@ void close(long timeout, TimeUnit unit) { checkClosed(); closedException = new ClosedException(); closureFutures = new ArrayList<>(); - invalidatedDbClients.addAll(dbClients.values()); - for (DatabaseClientImpl dbClient : invalidatedDbClients) { + for (DatabaseClientImpl dbClient : dbClients.values()) { closureFutures.add(dbClient.closeAsync(closedException)); } dbClients.clear();