Skip to content

Commit

Permalink
Fix connection leak in AuroraClusterDiscovery
Browse files Browse the repository at this point in the history
  • Loading branch information
wyrzyk committed Jul 25, 2023
1 parent 0c7874f commit 988eb0e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ Dropping a requirement of a major version of a dependency is a new contract.


## [Unreleased]
[Unreleased]: https://github.com/atlassian-labs/db-replica/compare/release-2.9.4...master
[Unreleased]: https://github.com/atlassian-labs/db-replica/compare/release-2.10.0...master

### Fix
- Connection leak in `AuroraClusterDiscovery`

## [2.10.0] - 2023-07-24

### Added
- Support for custom connection provider for replica discovery in `AuroraMultiReplicaConsistency`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.sql.SQLException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import static java.lang.String.format;
Expand Down Expand Up @@ -44,29 +45,44 @@ private AuroraClusterDiscovery(
}

public Collection<Database> getReplicas(Supplier<Connection> connectionSupplier) {
return discoveredReplicasCache.get(() -> fetchReplicas(discovererConnectionSupplier != null ? discovererConnectionSupplier : connectionSupplier)).orElseGet(
() -> {
lazyLogger.debug(() -> "AuroraClusterDiscovery#getReplicas no replicas cached.");
return Collections.emptyList();
});
return discoveredReplicasCache
.get(() -> discovererConnectionSupplier != null ? fetchReplicas() : fetchReplicas(connectionSupplier)).orElseGet(
() -> {
lazyLogger.debug(() -> "AuroraClusterDiscovery#getReplicas no replicas cached.");
return Collections.emptyList();
});
}

private Collection<Database> fetchReplicas(Supplier<Connection> connectionSupplier) {
try {
final Connection connection = connectionSupplier.get();
final AuroraReplicasDiscoverer discoverer = createDiscoverer(connection);
return discoverer.fetchReplicasUrls(connection).stream().map(auroraUrl -> {
JdbcUrl url = auroraUrl.toJdbcUrl();
return new AuroraReplicaNode(
auroraUrl.getEndpoint().getServerId(),
replicaConnectionPerUrlProvider.getReplicaConnectionProvider(url)
);
}).collect(toList());
return getDatabases(connection);
} catch (SQLException exception) {
throw new ReadReplicaDiscoveryOperationException(exception);
}
}

private Collection<Database> fetchReplicas() {
try {
try (final Connection connection = discovererConnectionSupplier.get()) {
return getDatabases(connection);
}
} catch (SQLException exception) {
throw new ReadReplicaDiscoveryOperationException(exception);
}
}

private List<Database> getDatabases(Connection connection) throws SQLException {
final AuroraReplicasDiscoverer discoverer = createDiscoverer(connection);
return discoverer.fetchReplicasUrls(connection).stream().map(auroraUrl -> {
JdbcUrl url = auroraUrl.toJdbcUrl();
return new AuroraReplicaNode(
auroraUrl.getEndpoint().getServerId(),
replicaConnectionPerUrlProvider.getReplicaConnectionProvider(url)
);
}).collect(toList());
}

private AuroraReplicasDiscoverer createDiscoverer(Connection connection) {
if (this.clusterUri != null) {
return createDiscovererFromClusterUri();
Expand Down

0 comments on commit 988eb0e

Please sign in to comment.