Skip to content

Commit 1ac52d5

Browse files
authored
fix: close AlloyDB gRPC client on connector close (#633)
When the connector is closed, it must also close the underlying AlloyDB gRPC-based client to avoid any errors. This ensures a clean shutdown of the Connector generally.
1 parent b5e927d commit 1ac52d5

File tree

5 files changed

+39
-15
lines changed

5 files changed

+39
-15
lines changed

alloydb-jdbc-connector/src/main/java/com/google/cloud/alloydb/ConnectionInfoRepository.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818

1919
import com.google.cloud.alloydb.v1alpha.InstanceName;
2020
import com.google.common.util.concurrent.ListenableFuture;
21+
import java.io.Closeable;
2122
import java.security.KeyPair;
2223

23-
interface ConnectionInfoRepository {
24+
interface ConnectionInfoRepository extends Closeable {
2425
ListenableFuture<ConnectionInfo> getConnectionInfo(InstanceName instanceName, KeyPair publicKey);
2526
}

alloydb-jdbc-connector/src/main/java/com/google/cloud/alloydb/Connector.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ public ConnectorConfig getConfig() {
6161
return config;
6262
}
6363

64-
public void close() {
64+
public void close() throws IOException {
6565
logger.debug("Close all connections and remove them from cache.");
6666
this.instances.forEach((key, c) -> c.close());
6767
this.instances.clear();
68+
this.connectionInfoRepo.close();
6869
}
6970

7071
Socket connect(ConnectionConfig config) throws IOException {

alloydb-jdbc-connector/src/main/java/com/google/cloud/alloydb/InternalConnectorRegistry.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,32 @@ public void close(String name) {
140140
if (connector == null) {
141141
throw new IllegalArgumentException("Named connection " + name + " does not exist.");
142142
}
143-
connector.close();
143+
try {
144+
connector.close();
145+
} catch (IOException e) {
146+
throw new RuntimeException(e);
147+
}
144148
}
145149

146150
/** Shutdown all connectors. */
147151
private void shutdownConnectors() {
148-
this.unnamedConnectors.forEach((key, c) -> c.close());
152+
this.unnamedConnectors.forEach(
153+
(key, c) -> {
154+
try {
155+
c.close();
156+
} catch (IOException e) {
157+
throw new RuntimeException(e);
158+
}
159+
});
149160
this.unnamedConnectors.clear();
150-
this.namedConnectors.forEach((key, c) -> c.close());
161+
this.namedConnectors.forEach(
162+
(key, c) -> {
163+
try {
164+
c.close();
165+
} catch (IOException e) {
166+
throw new RuntimeException(e);
167+
}
168+
});
151169
this.namedConnectors.clear();
152170
}
153171

alloydb-jdbc-connector/src/test/java/com/google/cloud/alloydb/InMemoryConnectionInfoRepo.java

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.cloud.alloydb.v1alpha.InstanceName;
2020
import com.google.common.util.concurrent.Futures;
2121
import com.google.common.util.concurrent.ListenableFuture;
22+
import java.io.IOException;
2223
import java.security.KeyPair;
2324
import java.util.ArrayList;
2425
import java.util.Arrays;
@@ -56,4 +57,9 @@ public final void addResponses(Callable<ConnectionInfo>... callables) {
5657
public final int getIndex() {
5758
return this.index.get();
5859
}
60+
61+
@Override
62+
public void close() throws IOException {
63+
// no-op
64+
}
5965
}

alloydb-jdbc-connector/src/test/java/com/google/cloud/alloydb/InternalConnectorRegistryTest.java

+8-10
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static org.junit.Assert.assertThrows;
2121

2222
import com.google.cloud.alloydb.v1alpha.InstanceName;
23-
import java.io.IOException;
2423
import java.util.Collections;
2524
import java.util.Properties;
2625
import org.junit.Before;
@@ -38,7 +37,7 @@ public void setUp() {
3837
}
3938

4039
@Test
41-
public void create_failOnInvalidInstanceName() throws IOException {
40+
public void create_failOnInvalidInstanceName() {
4241
IllegalArgumentException ex =
4342
assertThrows(
4443
IllegalArgumentException.class,
@@ -53,7 +52,7 @@ public void create_failOnInvalidInstanceName() throws IOException {
5352
}
5453

5554
@Test
56-
public void create_failOnEmptyTargetPrincipal() throws IOException, InterruptedException {
55+
public void create_failOnEmptyTargetPrincipal() {
5756
IllegalArgumentException ex =
5857
assertThrows(
5958
IllegalArgumentException.class,
@@ -72,7 +71,7 @@ public void create_failOnEmptyTargetPrincipal() throws IOException, InterruptedE
7271
}
7372

7473
@Test
75-
public void registerConnection_failWithDuplicateName() throws InterruptedException {
74+
public void registerConnection_failWithDuplicateName() {
7675
// Register a Connection
7776
String namedConnector = "my-connection-name";
7877
ConnectorConfig configWithDetails = new ConnectorConfig.Builder().build();
@@ -89,8 +88,7 @@ public void registerConnection_failWithDuplicateName() throws InterruptedExcepti
8988
}
9089

9190
@Test
92-
public void registerConnection_failWithDuplicateNameAndDifferentConfig()
93-
throws InterruptedException {
91+
public void registerConnection_failWithDuplicateNameAndDifferentConfig() {
9492
String namedConnector = "test-connection";
9593
ConnectorConfig config =
9694
new ConnectorConfig.Builder().withTargetPrincipal("[email protected]").build();
@@ -110,7 +108,7 @@ public void registerConnection_failWithDuplicateNameAndDifferentConfig()
110108
}
111109

112110
@Test
113-
public void closeNamedConnection_failWhenNotFound() throws InterruptedException {
111+
public void closeNamedConnection_failWhenNotFound() {
114112
String namedConnector = "a-connection";
115113
// Assert that you can't close a connection that doesn't exist
116114
IllegalArgumentException ex =
@@ -123,7 +121,7 @@ public void closeNamedConnection_failWhenNotFound() throws InterruptedException
123121
}
124122

125123
@Test
126-
public void connect_failOnClosedNamedConnection() throws InterruptedException {
124+
public void connect_failOnClosedNamedConnection() {
127125
// Register a Connection
128126
String namedConnector = "my-connection";
129127
ConnectorConfig configWithDetails = new ConnectorConfig.Builder().build();
@@ -149,7 +147,7 @@ public void connect_failOnClosedNamedConnection() throws InterruptedException {
149147
}
150148

151149
@Test
152-
public void connect_failOnUnknownNamedConnection() throws InterruptedException {
150+
public void connect_failOnUnknownNamedConnection() {
153151
// Attempt and fail to connect using the Named Connection not registered
154152
String namedConnector = "first-connection";
155153
Properties connProps = new Properties();
@@ -166,7 +164,7 @@ public void connect_failOnUnknownNamedConnection() throws InterruptedException {
166164
}
167165

168166
@Test
169-
public void connect_failOnNamedConnectionAfterResetInstance() throws InterruptedException {
167+
public void connect_failOnNamedConnectionAfterResetInstance() {
170168
// Register a Connection
171169
String namedConnector = "this-connection";
172170
ConnectorConfig config = new ConnectorConfig.Builder().build();

0 commit comments

Comments
 (0)