Skip to content

Commit

Permalink
chore: rename InstanceData to ConnectionInfo (#1642)
Browse files Browse the repository at this point in the history
  • Loading branch information
hessjcg committed Oct 25, 2023
1 parent d7c34b3 commit 64038f8
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import javax.net.ssl.SSLContext;

/** Represents the results of a certificate and metadata refresh operation. */
class InstanceData {
class ConnectionInfo {

private final Metadata metadata;
private final SSLContext sslContext;
private final SslData sslData;
private final Instant expiration;

InstanceData(Metadata metadata, SslData sslData, Instant expiration) {
ConnectionInfo(Metadata metadata, SslData sslData, Instant expiration) {
this.metadata = metadata;
this.sslData = sslData;
this.sslContext = sslData.getSslContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/** Internal Use Only: Gets the instance data for the CloudSqlInstance from the API. */
interface ConnectionInfoRepository {
/** Internal Use Only: Gets the instance data for the CloudSqlInstance from the API. */
ListenableFuture<InstanceData> getInstanceData(
ListenableFuture<ConnectionInfo> getConnectionInfo(
CloudSqlInstanceName instanceName,
AccessTokenSupplier accessTokenSupplier,
AuthType authType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class DefaultConnectionInfoCache {
connectionName,
executor,
() ->
connectionInfoRepository.getInstanceData(
connectionInfoRepository.getConnectionInfo(
this.instanceName, this.accessTokenSupplier, this.authType, executor, keyPair),
new AsyncRateLimiter(minRefreshDelayMs));
}
Expand All @@ -88,21 +88,22 @@ class DefaultConnectionInfoCache {
* successful attempt. If no attempts succeed within the timeout, throws a RuntimeException with
* the exception from the last failed refresh attempt as the cause.
*/
private InstanceData getInstanceData(long timeoutMs) {
return refresher.getData(timeoutMs);
private ConnectionInfo getConnectionInfo(long timeoutMs) {
return refresher.getConnectionInfo(timeoutMs);
}

/** Returns SslData to establish mTLS connections. */
SslData getSslData(long timeoutMs) {
return getInstanceData(timeoutMs).getSslData();
return getConnectionInfo(timeoutMs).getSslData();
}

/**
* Returns an unconnected {@link SSLSocket} using the SSLContext associated with the instance. May
* block until required instance data is available.
*/
SSLSocket createSslSocket(long timeoutMs) throws IOException {
return (SSLSocket) getInstanceData(timeoutMs).getSslContext().getSocketFactory().createSocket();
return (SSLSocket)
getConnectionInfo(timeoutMs).getSslContext().getSocketFactory().createSocket();
}

/**
Expand All @@ -116,7 +117,7 @@ SSLSocket createSslSocket(long timeoutMs) throws IOException {
* preferences.
*/
String getPreferredIp(List<IpType> preferredTypes, long timeoutMs) {
Map<IpType, String> ipAddrs = getInstanceData(timeoutMs).getIpAddrs();
Map<IpType, String> ipAddrs = getConnectionInfo(timeoutMs).getIpAddrs();
for (IpType ipType : preferredTypes) {
String preferredIp = ipAddrs.get(ipType);
if (preferredIp != null) {
Expand All @@ -134,11 +135,11 @@ void forceRefresh() {
this.refresher.forceRefresh();
}

ListenableFuture<InstanceData> getNext() {
ListenableFuture<ConnectionInfo> getNext() {
return refresher.getNext();
}

ListenableFuture<InstanceData> getCurrent() {
ListenableFuture<ConnectionInfo> getCurrent() {
return refresher.getCurrent();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private String generatePublicKeyCert(KeyPair keyPair) {

/** Internal Use Only: Gets the instance data for the CloudSqlInstance from the API. */
@Override
public ListenableFuture<InstanceData> getInstanceData(
public ListenableFuture<ConnectionInfo> getConnectionInfo(
CloudSqlInstanceName instanceName,
AccessTokenSupplier accessTokenSupplier,
AuthType authType,
Expand Down Expand Up @@ -127,7 +127,7 @@ public ListenableFuture<InstanceData> getInstanceData(
executor);

// Once both the SSLContext and Metadata are complete, return the results
ListenableFuture<InstanceData> done =
ListenableFuture<ConnectionInfo> done =
Futures.whenAllComplete(metadataFuture, ephemeralCertificateFuture, sslContextFuture)
.call(
() -> {
Expand All @@ -151,7 +151,7 @@ public ListenableFuture<InstanceData> getInstanceData(

logger.fine(String.format("[%s] INSTANCE DATA DONE", instanceName));

return new InstanceData(
return new ConnectionInfo(
Futures.getDone(metadataFuture),
Futures.getDone(sslContextFuture),
expiration);
Expand Down
61 changes: 30 additions & 31 deletions core/src/main/java/com/google/cloud/sql/core/Refresher.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ class Refresher {
private final AsyncRateLimiter rateLimiter;

private final RefreshCalculator refreshCalculator = new RefreshCalculator();
private final Supplier<ListenableFuture<InstanceData>> refreshOperation;
private final Supplier<ListenableFuture<ConnectionInfo>> refreshOperation;
private final String name;

@GuardedBy("instanceDataGuard")
private ListenableFuture<InstanceData> currentInstanceData;
private ListenableFuture<ConnectionInfo> current;

@GuardedBy("instanceDataGuard")
private ListenableFuture<InstanceData> nextInstanceData;
private ListenableFuture<ConnectionInfo> next;

@GuardedBy("instanceDataGuard")
private boolean refreshRunning;
Expand All @@ -66,15 +66,15 @@ class Refresher {
Refresher(
String name,
ListeningScheduledExecutorService executor,
Supplier<ListenableFuture<InstanceData>> refreshOperation,
Supplier<ListenableFuture<ConnectionInfo>> refreshOperation,
AsyncRateLimiter rateLimiter) {
this.name = name;
this.executor = executor;
this.refreshOperation = refreshOperation;
this.rateLimiter = rateLimiter;
synchronized (instanceDataGuard) {
forceRefresh();
this.currentInstanceData = this.nextInstanceData;
this.current = this.next;
}
}

Expand All @@ -89,14 +89,14 @@ class Refresher {
* successful attempt. If no attempts succeed within the timeout, throws a RuntimeException with
* the exception from the last failed refresh attempt as the cause.
*/
InstanceData getData(long timeoutMs) {
ListenableFuture<InstanceData> instanceDataFuture;
ConnectionInfo getConnectionInfo(long timeoutMs) {
ListenableFuture<ConnectionInfo> f;
synchronized (instanceDataGuard) {
instanceDataFuture = currentInstanceData;
f = current;
}

try {
return instanceDataFuture.get(timeoutMs, TimeUnit.MILLISECONDS);
return f.get(timeoutMs, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
synchronized (instanceDataGuard) {
if (currentRefreshFailure != null) {
Expand Down Expand Up @@ -134,16 +134,16 @@ void forceRefresh() {
return;
}

if (nextInstanceData != null) {
nextInstanceData.cancel(false);
if (next != null) {
next.cancel(false);
}

logger.fine(
String.format(
"[%s] Force Refresh: the next refresh operation was cancelled."
+ " Scheduling new refresh operation immediately.",
name));
nextInstanceData = this.startRefreshAttempt();
next = this.startRefreshAttempt();
}
}

Expand All @@ -156,7 +156,7 @@ void forceRefresh() {
* @see com.google.cloud.sql.core.Refresher#handleRefreshResult(
* com.google.common.util.concurrent.ListenableFuture)
*/
private ListenableFuture<InstanceData> startRefreshAttempt() {
private ListenableFuture<ConnectionInfo> startRefreshAttempt() {
// As soon as we begin submitting refresh attempts to the executor, mark a refresh
// as "in-progress" so that subsequent forceRefresh() calls balk until this one completes.
synchronized (instanceDataGuard) {
Expand All @@ -172,27 +172,26 @@ private ListenableFuture<InstanceData> startRefreshAttempt() {
executor);

// Once rate limiter is done, attempt to getInstanceData.
ListenableFuture<InstanceData> dataFuture =
ListenableFuture<ConnectionInfo> f =
Futures.whenAllComplete(delay).callAsync(refreshOperation::get, executor);

// Finally, reschedule refresh after getInstanceData is complete.
return Futures.whenAllComplete(dataFuture)
.callAsync(() -> handleRefreshResult(dataFuture), executor);
return Futures.whenAllComplete(f).callAsync(() -> handleRefreshResult(f), executor);
}

private ListenableFuture<InstanceData> handleRefreshResult(
ListenableFuture<InstanceData> dataFuture) {
private ListenableFuture<ConnectionInfo> handleRefreshResult(
ListenableFuture<ConnectionInfo> connectionInfoFuture) {
try {
// This does not block, because it only gets called when dataFuture has completed.
// This does not block, because it only gets called when connectionInfoFuture has completed.
// This will throw an exception if the refresh attempt has failed.
InstanceData data = dataFuture.get();
ConnectionInfo info = connectionInfoFuture.get();

logger.fine(
String.format(
"[%s] Refresh Operation: Completed refresh with new certificate expiration at %s.",
name, data.getExpiration().toString()));
name, info.getExpiration().toString()));
long secondsToRefresh =
refreshCalculator.calculateSecondsUntilNextRefresh(Instant.now(), data.getExpiration());
refreshCalculator.calculateSecondsUntilNextRefresh(Instant.now(), info.getExpiration());

logger.fine(
String.format(
Expand All @@ -207,16 +206,16 @@ private ListenableFuture<InstanceData> handleRefreshResult(
// Refresh completed successfully, reset forceRefreshRunning.
refreshRunning = false;
currentRefreshFailure = null;
currentInstanceData = Futures.immediateFuture(data);
current = Futures.immediateFuture(info);

// Now update nextInstanceData to perform a refresh after the
// scheduled delay
nextInstanceData =
next =
Futures.scheduleAsync(
this::startRefreshAttempt, secondsToRefresh, TimeUnit.SECONDS, executor);

// Resolves to an T immediately
return currentInstanceData;
return current;
}

} catch (ExecutionException | InterruptedException e) {
Expand All @@ -227,23 +226,23 @@ private ListenableFuture<InstanceData> handleRefreshResult(
e);
synchronized (instanceDataGuard) {
currentRefreshFailure = e;
nextInstanceData = this.startRefreshAttempt();
next = this.startRefreshAttempt();

// Resolves after the next successful refresh attempt.
return nextInstanceData;
return next;
}
}
}

ListenableFuture<InstanceData> getNext() {
ListenableFuture<ConnectionInfo> getNext() {
synchronized (instanceDataGuard) {
return this.nextInstanceData;
return this.next;
}
}

ListenableFuture<InstanceData> getCurrent() {
ListenableFuture<ConnectionInfo> getCurrent() {
synchronized (instanceDataGuard) {
return this.currentInstanceData;
return this.current;
}
}
}
Loading

0 comments on commit 64038f8

Please sign in to comment.