26
26
import com .google .cloud .spanner .Options .QueryOption ;
27
27
import com .google .cloud .spanner .Options .ReadOption ;
28
28
import com .google .cloud .spanner .SessionClient .SessionConsumer ;
29
+ import com .google .cloud .spanner .SpannerException .ResourceNotFoundException ;
29
30
import com .google .common .annotations .VisibleForTesting ;
30
31
import com .google .common .base .Function ;
31
32
import com .google .common .base .MoreObjects ;
@@ -781,13 +782,14 @@ public void close() {
781
782
if (lastException != null && isSessionNotFound (lastException )) {
782
783
invalidateSession (this );
783
784
} else {
784
- if (lastException != null && isDatabaseNotFound (lastException )) {
785
+ if (lastException != null && isDatabaseOrInstanceNotFound (lastException )) {
785
786
// Mark this session pool as no longer valid and then release the session into the pool as
786
787
// there is nothing we can do with it anyways.
787
788
synchronized (lock ) {
788
- SessionPool .this .databaseNotFound =
789
+ SessionPool .this .resourceNotFoundException =
789
790
MoreObjects .firstNonNull (
790
- SessionPool .this .databaseNotFound , (DatabaseNotFoundException ) lastException );
791
+ SessionPool .this .resourceNotFoundException ,
792
+ (ResourceNotFoundException ) lastException );
791
793
}
792
794
}
793
795
lastException = null ;
@@ -1075,7 +1077,7 @@ private static enum Position {
1075
1077
private SettableFuture <Void > closureFuture ;
1076
1078
1077
1079
@ GuardedBy ("lock" )
1078
- private DatabaseNotFoundException databaseNotFound ;
1080
+ private ResourceNotFoundException resourceNotFoundException ;
1079
1081
1080
1082
@ GuardedBy ("lock" )
1081
1083
private final LinkedList <PooledSession > readSessions = new LinkedList <>();
@@ -1213,8 +1215,8 @@ private boolean isSessionNotFound(SpannerException e) {
1213
1215
return e .getErrorCode () == ErrorCode .NOT_FOUND && e .getMessage ().contains ("Session not found" );
1214
1216
}
1215
1217
1216
- private boolean isDatabaseNotFound (SpannerException e ) {
1217
- return e instanceof DatabaseNotFoundException ;
1218
+ private boolean isDatabaseOrInstanceNotFound (SpannerException e ) {
1219
+ return e instanceof DatabaseNotFoundException || e instanceof InstanceNotFoundException ;
1218
1220
}
1219
1221
1220
1222
private boolean isPermissionDenied (SpannerException e ) {
@@ -1249,7 +1251,7 @@ private PooledSession findSessionToKeepAlive(
1249
1251
/** @return true if this {@link SessionPool} is still valid. */
1250
1252
boolean isValid () {
1251
1253
synchronized (lock ) {
1252
- return closureFuture == null && databaseNotFound == null ;
1254
+ return closureFuture == null && resourceNotFoundException == null ;
1253
1255
}
1254
1256
}
1255
1257
@@ -1279,14 +1281,14 @@ PooledSession getReadSession() throws SpannerException {
1279
1281
span .addAnnotation ("Pool has been closed" );
1280
1282
throw new IllegalStateException ("Pool has been closed" );
1281
1283
}
1282
- if (databaseNotFound != null ) {
1284
+ if (resourceNotFoundException != null ) {
1283
1285
span .addAnnotation ("Database has been deleted" );
1284
1286
throw SpannerExceptionFactory .newSpannerException (
1285
1287
ErrorCode .NOT_FOUND ,
1286
1288
String .format (
1287
1289
"The session pool has been invalidated because a previous RPC returned 'Database not found': %s" ,
1288
- databaseNotFound .getMessage ()),
1289
- databaseNotFound );
1290
+ resourceNotFoundException .getMessage ()),
1291
+ resourceNotFoundException );
1290
1292
}
1291
1293
sess = readSessions .poll ();
1292
1294
if (sess == null ) {
@@ -1344,14 +1346,14 @@ PooledSession getReadWriteSession() {
1344
1346
span .addAnnotation ("Pool has been closed" );
1345
1347
throw new IllegalStateException ("Pool has been closed" );
1346
1348
}
1347
- if (databaseNotFound != null ) {
1349
+ if (resourceNotFoundException != null ) {
1348
1350
span .addAnnotation ("Database has been deleted" );
1349
1351
throw SpannerExceptionFactory .newSpannerException (
1350
1352
ErrorCode .NOT_FOUND ,
1351
1353
String .format (
1352
1354
"The session pool has been invalidated because a previous RPC returned 'Database not found': %s" ,
1353
- databaseNotFound .getMessage ()),
1354
- databaseNotFound );
1355
+ resourceNotFoundException .getMessage ()),
1356
+ resourceNotFoundException );
1355
1357
}
1356
1358
sess = writePreparedSessions .poll ();
1357
1359
if (sess == null ) {
@@ -1495,17 +1497,18 @@ private void handleCreateSessionsFailure(SpannerException e, int count) {
1495
1497
break ;
1496
1498
}
1497
1499
}
1498
- this .databaseNotFound =
1500
+ this .resourceNotFoundException =
1499
1501
MoreObjects .firstNonNull (
1500
- this .databaseNotFound , isDatabaseNotFound (e ) ? (DatabaseNotFoundException ) e : null );
1502
+ this .resourceNotFoundException ,
1503
+ isDatabaseOrInstanceNotFound (e ) ? (ResourceNotFoundException ) e : null );
1501
1504
}
1502
1505
}
1503
1506
1504
1507
private void handlePrepareSessionFailure (SpannerException e , PooledSession session ) {
1505
1508
synchronized (lock ) {
1506
1509
if (isSessionNotFound (e )) {
1507
1510
invalidateSession (session );
1508
- } else if (isDatabaseNotFound (e ) || isPermissionDenied (e )) {
1511
+ } else if (isDatabaseOrInstanceNotFound (e ) || isPermissionDenied (e )) {
1509
1512
// Database has been deleted or the user has no permission to write to this database. We
1510
1513
// should stop trying to prepare any transactions. Also propagate the error to all waiters,
1511
1514
// as any further waiting is pointless.
@@ -1520,10 +1523,10 @@ private void handlePrepareSessionFailure(SpannerException e, PooledSession sessi
1520
1523
if (isClosed ()) {
1521
1524
decrementPendingClosures (1 );
1522
1525
}
1523
- this .databaseNotFound =
1526
+ this .resourceNotFoundException =
1524
1527
MoreObjects .firstNonNull (
1525
- this .databaseNotFound ,
1526
- isDatabaseNotFound (e ) ? (DatabaseNotFoundException ) e : null );
1528
+ this .resourceNotFoundException ,
1529
+ isDatabaseOrInstanceNotFound (e ) ? (ResourceNotFoundException ) e : null );
1527
1530
} else if (readWriteWaiters .size () > 0 ) {
1528
1531
releaseSession (session , Position .FIRST );
1529
1532
readWriteWaiters .poll ().put (e );
0 commit comments