Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't close while holding lock #13

Merged
merged 1 commit into from
Mar 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions java/jdbc/src/main/java/io/vitess/jdbc/VitessVTGateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -91,7 +93,8 @@ public VTGateConnections(final VitessConnection connection) {
new TimerTask() {
@Override
public void run() {
refreshUpdatedSSLConnections(hostInfo, connection);
refreshUpdatedSSLConnections(hostInfo,
connection);
}
},
TimeUnit.SECONDS.toMillis(connection.getRefreshSeconds()),
Expand Down Expand Up @@ -141,21 +144,25 @@ private static void updateVtGateConnHashMap(String identifier, VitessJDBCUrl.Hos

private static void refreshUpdatedSSLConnections(VitessJDBCUrl.HostInfo hostInfo,
VitessConnection connection) {
Set<VTGateConnection> closedConnections = new HashSet<>();
synchronized (VitessVTGateManager.class) {
int updatedCount = 0;
for (Map.Entry<String, VTGateConnection> entry : vtGateConnHashMap.entrySet()) {
if (entry.getValue() instanceof RefreshableVTGateConnection) {
RefreshableVTGateConnection existing = (RefreshableVTGateConnection) entry.getValue();
if (existing.checkKeystoreUpdates()) {
updatedCount++;
VTGateConnection old = vtGateConnHashMap
.replace(entry.getKey(), getVtGateConn(hostInfo, connection));
closeRefreshedConnection(old);
closedConnections.add(old);
}
}
}
if (updatedCount > 0) {
logger.info("refreshed " + updatedCount + " vtgate connections due to keystore update");
}

if (closedConnections.size() > 0) {
logger.info(
"refreshed " + closedConnections.size() + " vtgate connections due to keystore update");
for (VTGateConnection closedConnection : closedConnections) {
closeRefreshedConnection(closedConnection);
}
}
}
Expand Down Expand Up @@ -216,13 +223,11 @@ private static VTGateConnection getVtGateConn(VitessJDBCUrl.HostInfo hostInfo,
.trustStorePath(trustStorePath).trustStorePassword(trustStorePassword)
.trustAlias(trustAlias);

return new RefreshableVTGateConnection(
new GrpcClientFactory(channelProvider, errorHandler)
.createTls(context, hostInfo.toString(), tlsOptions), keyStorePath, trustStorePath);
return new RefreshableVTGateConnection(new GrpcClientFactory(channelProvider, errorHandler)
.createTls(context, hostInfo.toString(), tlsOptions), keyStorePath, trustStorePath);
} else {
return new VTGateConnection(
new GrpcClientFactory(channelProvider, errorHandler)
.create(context, hostInfo.toString()));
return new VTGateConnection(new GrpcClientFactory(channelProvider, errorHandler)
.create(context, hostInfo.toString()));
}
}

Expand All @@ -235,8 +240,7 @@ private static RetryingInterceptorConfig getRetryingInterceptorConfig(VitessConn
conn.getGrpcRetryMaxBackoffMillis(), conn.getGrpcRetryBackoffMultiplier());
}

private static ErrorHandler getErrorHandlerFromProperties(
VitessConnection connection) {
private static ErrorHandler getErrorHandlerFromProperties(VitessConnection connection) {
// Skip reflection in default case
if (Strings.isNullOrEmpty(connection.getErrorHandlerClass())) {
return new DefaultErrorHandler();
Expand Down