File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1708,10 +1708,9 @@ private void handleCreateSessionsFailure(SpannerException e, int count) {
17081708
break;
17091709
}
17101710
}
1711-
this.resourceNotFoundException =
1712-
MoreObjects.firstNonNull(
1713-
this.resourceNotFoundException,
1714-
isDatabaseOrInstanceNotFound(e) ? (ResourceNotFoundException) e : null);
1711+
if (isDatabaseOrInstanceNotFound(e)) {
1712+
setResourceNotFoundException((ResourceNotFoundException) e);
1713+
}
17151714
}
17161715
}
17171716

@@ -1738,9 +1737,7 @@ private void handlePrepareSessionFailure(
17381737
decrementPendingClosures(1);
17391738
}
17401739
allSessions.remove(session);
1741-
this.resourceNotFoundException =
1742-
MoreObjects.firstNonNull(
1743-
this.resourceNotFoundException, (ResourceNotFoundException) e);
1740+
setResourceNotFoundException((ResourceNotFoundException) e);
17441741
} else {
17451742
releaseSession(session, Position.FIRST);
17461743
}
@@ -1753,6 +1750,10 @@ private void handlePrepareSessionFailure(
17531750
}
17541751
}
17551752

1753+
void setResourceNotFoundException(ResourceNotFoundException e) {
1754+
this.resourceNotFoundException = MoreObjects.firstNonNull(this.resourceNotFoundException, e);
1755+
}
1756+
17561757
private void decrementPendingClosures(int count) {
17571758
pendingClosure -= count;
17581759
if (pendingClosure == 0) {
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,20 @@ public InstanceAdminClient getInstanceAdminClient() {
185185
public DatabaseClient getDatabaseClient(DatabaseId db) {
186186
synchronized (this) {
187187
checkClosed();
188+
String clientId = null;
188189
if (dbClients.containsKey(db) && !dbClients.get(db).pool.isValid()) {
189190
// Move the invalidated client to a separate list, so we can close it together with the
190191
// other database clients when the Spanner instance is closed.
191192
invalidatedDbClients.add(dbClients.get(db));
193+
clientId = dbClients.get(db).clientId;
192194
dbClients.remove(db);
193195
}
194196
if (dbClients.containsKey(db)) {
195197
return dbClients.get(db);
196198
} else {
197-
String clientId = nextDatabaseClientId(db);
199+
if (clientId == null) {
200+
clientId = nextDatabaseClientId(db);
201+
}
198202
List<LabelValue> labelValues =
199203
ImmutableList.of(
200204
LabelValue.create(clientId),
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.cloud.NoCredentials;
2828
import com.google.cloud.ServiceRpc;
2929
import com.google.cloud.grpc.GrpcTransportOptions;
30+
import com.google.cloud.spanner.SpannerException.DoNotConstructDirectly;
3031
import com.google.cloud.spanner.SpannerImpl.ClosedException;
3132
import com.google.cloud.spanner.spi.v1.SpannerRpc;
3233
import com.google.spanner.v1.ExecuteSqlRequest.QueryOptions;
@@ -190,7 +191,7 @@ public void testSpannerClosed() throws InterruptedException {
190191
}
191192

192193
@Test
193-
public void testClientId() {
194+
public void testClientId() throws Exception {
194195
// Create a unique database id to be sure it has not yet been used in the lifetime of this JVM.
195196
String dbName =
196197
String.format("projects/p1/instances/i1/databases/%s", UUID.randomUUID().toString());
@@ -215,6 +216,13 @@ public void testClientId() {
215216
DatabaseClientImpl databaseClient2 = (DatabaseClientImpl) impl.getDatabaseClient(db2);
216217
assertThat(databaseClient2.clientId).isEqualTo("client-1");
217218

219+
// Getting a new database client for an invalidated database should use the same client id.
220+
databaseClient.pool.setResourceNotFoundException(
221+
new DatabaseNotFoundException(DoNotConstructDirectly.ALLOWED, "not found", null, null));
222+
DatabaseClientImpl revalidated = (DatabaseClientImpl) impl.getDatabaseClient(db);
223+
assertThat(revalidated).isNotSameInstanceAs(databaseClient);
224+
assertThat(revalidated.clientId).isEqualTo(databaseClient.clientId);
225+
218226
// Create a new Spanner instance. This will generate new database clients with new ids.
219227
try (Spanner spanner =
220228
SpannerOptions.newBuilder()

0 commit comments

Comments
 (0)