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

[WIP] Implement refactoring changes #1286

Draft
wants to merge 41 commits into
base: refactoring-wip
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
6ec4c3d
First implementation of CacheService and MonitorService complete
aaron-congo Feb 5, 2025
184b68a
wip
aaron-congo Feb 5, 2025
2ddebce
Merge branch 'refactoring-wip' into refactoring
aaron-congo Feb 5, 2025
053dd75
Merge branch 'refactoring-wip' into refactoring
aaron-congo Feb 7, 2025
4437581
Merge branch 'refactoring' into refactoring-wip
aaron-congo Feb 7, 2025
a12410f
wip
aaron-congo Feb 7, 2025
3e17520
PR comments
aaron-congo Feb 7, 2025
e35d740
Merge branch 'refactoring-wip' into refactoring
aaron-congo Feb 7, 2025
07b36fc
Implemented ExpirationCache and StorageServiceImpl
aaron-congo Feb 11, 2025
8bfbd4f
Replaced topologyCache with StorageService
aaron-congo Feb 12, 2025
f7ef88f
Error when passing Topology.class as the itemClass
aaron-congo Feb 12, 2025
53a1429
Added itemClass parameter to get() method
aaron-congo Feb 13, 2025
5d3f698
Failover integration test passing
aaron-congo Feb 13, 2025
8b3e30e
Remove MonitorGroup and MonitorServiceImpl
aaron-congo Feb 13, 2025
a9041ae
Instantiate StorageService in Driver class
aaron-congo Feb 14, 2025
afd7a56
cleanup
aaron-congo Feb 14, 2025
98d6dff
ExpiringCache fixes
aaron-congo Feb 14, 2025
bc83286
Merge branch 'refactoring-wip' into storage-service
aaron-congo Feb 14, 2025
e0af969
Fix build errors
aaron-congo Feb 15, 2025
472c793
cleanup
aaron-congo Feb 18, 2025
a516492
cleanup
aaron-congo Feb 18, 2025
2107add
PR comments
aaron-congo Feb 18, 2025
55e3eec
Cleanup, gradle check passing
aaron-congo Feb 19, 2025
b12c200
Lazily auto-register default caches
aaron-congo Feb 19, 2025
34aaadb
Move cleanup thread to StorageServiceImpl
aaron-congo Feb 21, 2025
5e03973
Minor fixes, cleanup
aaron-congo Feb 21, 2025
4cd7117
Add log to cleanup thread
aaron-congo Feb 21, 2025
da8e939
Adjust default cleanup time
aaron-congo Feb 21, 2025
a1794ec
Replace custom endpoint storage
aaron-congo Feb 24, 2025
dcad5c3
Implement ServiceContainer
aaron-congo Feb 26, 2025
bd92538
Cleanup
aaron-congo Feb 26, 2025
0c2b548
MonitorServiceImpl wip
aaron-congo Feb 27, 2025
e7f0ae2
Make CacheItem protected
aaron-congo Feb 28, 2025
1ece19d
Remove cleanupInterval from ExpirationCache
aaron-congo Feb 28, 2025
f4390ca
MonitorServiceImpl with MonitorItem
aaron-congo Feb 28, 2025
e06375a
MonitorServiceImpl wip
aaron-congo Mar 3, 2025
e8a8820
Implemented inactive timeout, AbstractMonitor
aaron-congo Mar 4, 2025
bef3657
Implement default registration
aaron-congo Mar 4, 2025
b05c6f4
replace commented out default suppliers with TODO
aaron-congo Mar 5, 2025
f647219
Auto-register default monitor types if missing
aaron-congo Mar 5, 2025
01014b4
PR comments, fix missing monitor error processing
aaron-congo Mar 5, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
import software.amazon.jdbc.dialect.Dialect;
import software.amazon.jdbc.hostavailability.SimpleHostAvailabilityStrategy;
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;
import software.amazon.jdbc.util.storage.StorageService;
import software.amazon.jdbc.util.storage.StorageServiceImpl;
import software.amazon.jdbc.util.telemetry.GaugeCallable;
import software.amazon.jdbc.util.telemetry.TelemetryContext;
import software.amazon.jdbc.util.telemetry.TelemetryCounter;
Expand All @@ -85,6 +87,7 @@ public class PluginBenchmarks {
"jdbc:aws-wrapper:postgresql://instance-0.XYZ.us-east-2.rds.amazonaws.com";
private static final String TEST_HOST = "instance-0";
private static final int TEST_PORT = 5432;
private static final StorageService storageService = new StorageServiceImpl();
private final HostSpec writerHostSpec = new HostSpecBuilder(new SimpleHostAvailabilityStrategy())
.host(TEST_HOST).port(TEST_PORT).build();

Expand Down Expand Up @@ -164,7 +167,8 @@ public ConnectionWrapper initAndReleaseWithExecutionTimePlugin() throws SQLExcep
mockTelemetryFactory,
mockPluginService,
mockHostListProviderService,
mockPluginManagerService)) {
mockPluginManagerService,
storageService)) {
wrapper.releaseResources();
return wrapper;
}
Expand All @@ -179,7 +183,8 @@ public ConnectionWrapper initAndReleaseWithAuroraHostListPlugin() throws SQLExce
mockTelemetryFactory,
mockPluginService,
mockHostListProviderService,
mockPluginManagerService)) {
mockPluginManagerService,
storageService)) {
wrapper.releaseResources();
return wrapper;
}
Expand All @@ -194,7 +199,8 @@ public ConnectionWrapper initAndReleaseWithExecutionTimeAndAuroraHostListPlugins
mockTelemetryFactory,
mockPluginService,
mockHostListProviderService,
mockPluginManagerService)) {
mockPluginManagerService,
storageService)) {
wrapper.releaseResources();
return wrapper;
}
Expand All @@ -209,7 +215,8 @@ public ConnectionWrapper initAndReleaseWithReadWriteSplittingPlugin() throws SQL
mockTelemetryFactory,
mockPluginService,
mockHostListProviderService,
mockPluginManagerService)) {
mockPluginManagerService,
storageService)) {
wrapper.releaseResources();
return wrapper;
}
Expand All @@ -225,7 +232,8 @@ public ConnectionWrapper initAndReleaseWithAuroraHostListAndReadWriteSplittingPl
mockTelemetryFactory,
mockPluginService,
mockHostListProviderService,
mockPluginManagerService)) {
mockPluginManagerService,
storageService)) {
wrapper.releaseResources();
return wrapper;
}
Expand All @@ -243,7 +251,8 @@ public ConnectionWrapper initAndReleaseWithReadWriteSplittingPlugin_internalConn
mockTelemetryFactory,
mockPluginService,
mockHostListProviderService,
mockPluginManagerService)) {
mockPluginManagerService,
storageService)) {
wrapper.releaseResources();
ConnectionProviderManager.releaseResources();
Driver.resetCustomConnectionProvider();
Expand All @@ -264,7 +273,8 @@ public ConnectionWrapper initAndReleaseWithAuroraHostListAndReadWriteSplittingPl
mockTelemetryFactory,
mockPluginService,
mockHostListProviderService,
mockPluginManagerService)) {
mockPluginManagerService,
storageService)) {
wrapper.releaseResources();
ConnectionProviderManager.releaseResources();
Driver.resetCustomConnectionProvider();
Expand All @@ -281,7 +291,8 @@ public Statement executeStatementBaseline() throws SQLException {
mockTelemetryFactory,
mockPluginService,
mockHostListProviderService,
mockPluginManagerService);
mockPluginManagerService,
storageService);
Statement statement = wrapper.createStatement()) {
return statement;
}
Expand All @@ -297,7 +308,8 @@ public ResultSet executeStatementWithExecutionTimePlugin() throws SQLException {
mockTelemetryFactory,
mockPluginService,
mockHostListProviderService,
mockPluginManagerService);
mockPluginManagerService,
storageService);
Statement statement = wrapper.createStatement();
ResultSet resultSet = statement.executeQuery("some sql")) {
return resultSet;
Expand All @@ -314,7 +326,8 @@ public ResultSet executeStatementWithTelemetryDisabled() throws SQLException {
mockTelemetryFactory,
mockPluginService,
mockHostListProviderService,
mockPluginManagerService);
mockPluginManagerService,
storageService);
Statement statement = wrapper.createStatement();
ResultSet resultSet = statement.executeQuery("some sql")) {
return resultSet;
Expand All @@ -331,7 +344,8 @@ public ResultSet executeStatementWithTelemetry() throws SQLException {
mockTelemetryFactory,
mockPluginService,
mockHostListProviderService,
mockPluginManagerService);
mockPluginManagerService,
storageService);
Statement statement = wrapper.createStatement();
ResultSet resultSet = statement.executeQuery("some sql")) {
return resultSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import software.amazon.jdbc.HostListProviderService;
import software.amazon.jdbc.PluginManagerService;
import software.amazon.jdbc.PluginService;
import software.amazon.jdbc.util.storage.StorageService;
import software.amazon.jdbc.util.telemetry.TelemetryFactory;
import software.amazon.jdbc.wrapper.ConnectionWrapper;

Expand All @@ -35,9 +36,10 @@ public TestConnectionWrapper(@NonNull Properties props,
@NonNull final TelemetryFactory telemetryFactory,
@NonNull PluginService pluginService,
@NonNull HostListProviderService hostListProviderService,
@NonNull PluginManagerService pluginManagerService)
@NonNull PluginManagerService pluginManagerService,
@NonNull StorageService storageService)
throws SQLException {
super(props, url, connectionPluginManager, telemetryFactory, pluginService, hostListProviderService,
pluginManagerService);
pluginManagerService, storageService);
}
}
6 changes: 6 additions & 0 deletions wrapper/src/main/java/software/amazon/jdbc/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import software.amazon.jdbc.util.PropertyUtils;
import software.amazon.jdbc.util.RdsUtils;
import software.amazon.jdbc.util.StringUtils;
import software.amazon.jdbc.util.storage.StorageService;
import software.amazon.jdbc.util.storage.StorageServiceImpl;
import software.amazon.jdbc.util.telemetry.DefaultTelemetryFactory;
import software.amazon.jdbc.util.telemetry.TelemetryContext;
import software.amazon.jdbc.util.telemetry.TelemetryFactory;
Expand All @@ -77,6 +79,8 @@ public class Driver implements java.sql.Driver {
private static final Logger LOGGER = Logger.getLogger("software.amazon.jdbc.Driver");
private static @Nullable Driver registeredDriver;

private static final StorageService storageService = new StorageServiceImpl();

private static final AtomicReference<ResetSessionStateOnCloseCallable> resetSessionStateOnCloseCallable =
new AtomicReference<>(null);
private static final AtomicReference<TransferSessionStateOnSwitchCallable> transferSessionStateOnSwitchCallable =
Expand Down Expand Up @@ -223,6 +227,7 @@ public Connection connect(final String url, final Properties info) throws SQLExc
effectiveConnectionProvider,
targetDriverDialect,
configurationProfile,
storageService,
telemetryFactory);

} catch (Exception ex) {
Expand Down Expand Up @@ -388,6 +393,7 @@ public static void resetConnectionInitFunc() {
}

public static void clearCaches() {
storageService.clearAll();
RdsUtils.clearCache();
RdsHostListProvider.clearAll();
PluginServiceImpl.clearCache();
Expand Down
6 changes: 6 additions & 0 deletions wrapper/src/main/java/software/amazon/jdbc/PluginService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import software.amazon.jdbc.hostavailability.HostAvailability;
import software.amazon.jdbc.states.SessionStateService;
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;
import software.amazon.jdbc.util.storage.StorageService;
import software.amazon.jdbc.util.telemetry.TelemetryFactory;

/**
Expand Down Expand Up @@ -80,11 +81,16 @@ EnumSet<NodeChangeOptions> setCurrentConnection(

HostSpec getInitialConnectionHostSpec();

// TODO: evaluate if there is a better way to pass the storage service to the monitors that need it.
StorageService getStorageService();

/**
* Set the collection of hosts that should be allowed and/or blocked for connections.
*
* @param allowedAndBlockedHosts An object defining the allowed and blocked sets of hosts.
* @deprecated use StorageService#set(ItemCategory.ALLOWED_AND_BLOCKED_HOSTS, key, allowedAndBlockedHosts) instead.
*/
@Deprecated
void setAllowedAndBlockedHosts(AllowedAndBlockedHosts allowedAndBlockedHosts);

/**
Expand Down
37 changes: 26 additions & 11 deletions wrapper/src/main/java/software/amazon/jdbc/PluginServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand All @@ -53,7 +52,10 @@
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;
import software.amazon.jdbc.util.CacheMap;
import software.amazon.jdbc.util.Messages;
import software.amazon.jdbc.util.ServiceContainer;
import software.amazon.jdbc.util.Utils;
import software.amazon.jdbc.util.storage.ItemCategory;
import software.amazon.jdbc.util.storage.StorageService;
import software.amazon.jdbc.util.telemetry.TelemetryFactory;

public class PluginServiceImpl implements PluginService, CanReleaseResources,
Expand All @@ -63,13 +65,14 @@ public class PluginServiceImpl implements PluginService, CanReleaseResources,
protected static final long DEFAULT_HOST_AVAILABILITY_CACHE_EXPIRE_NANO = TimeUnit.MINUTES.toNanos(5);

protected static final CacheMap<String, HostAvailability> hostAvailabilityExpiringCache = new CacheMap<>();
protected final ServiceContainer serviceContainer;
protected final ConnectionPluginManager pluginManager;
protected final StorageService storageService;
private final Properties props;
private final String originalUrl;
private final String driverProtocol;
protected volatile HostListProvider hostListProvider;
protected List<HostSpec> allHosts = new ArrayList<>();
protected AtomicReference<AllowedAndBlockedHosts> allowedAndBlockedHosts = new AtomicReference<>();
protected Connection currentConnection;
protected HostSpec currentHostSpec;
protected HostSpec initialConnectionHostSpec;
Expand All @@ -87,14 +90,15 @@ public class PluginServiceImpl implements PluginService, CanReleaseResources,
protected final ReentrantLock connectionSwitchLock = new ReentrantLock();

public PluginServiceImpl(
@NonNull final ConnectionPluginManager pluginManager,
@NonNull final ServiceContainer serviceContainer,
@NonNull final Properties props,
@NonNull final String originalUrl,
@NonNull final String targetDriverProtocol,
@NonNull final TargetDriverDialect targetDriverDialect)
throws SQLException {

this(pluginManager,
this(
serviceContainer,
new ExceptionManager(),
props,
originalUrl,
Expand All @@ -106,13 +110,14 @@ public PluginServiceImpl(
}

public PluginServiceImpl(
@NonNull final ConnectionPluginManager pluginManager,
@NonNull final ServiceContainer serviceContainer,
@NonNull final Properties props,
@NonNull final String originalUrl,
@NonNull final String targetDriverProtocol,
@NonNull final TargetDriverDialect targetDriverDialect,
@Nullable final ConfigurationProfile configurationProfile) throws SQLException {
this(pluginManager,
this(
serviceContainer,
new ExceptionManager(),
props,
originalUrl,
Expand All @@ -124,7 +129,7 @@ public PluginServiceImpl(
}

public PluginServiceImpl(
@NonNull final ConnectionPluginManager pluginManager,
@NonNull final ServiceContainer serviceContainer,
@NonNull final ExceptionManager exceptionManager,
@NonNull final Properties props,
@NonNull final String originalUrl,
Expand All @@ -133,14 +138,16 @@ public PluginServiceImpl(
@NonNull final TargetDriverDialect targetDriverDialect,
@Nullable final ConfigurationProfile configurationProfile,
@Nullable final SessionStateService sessionStateService) throws SQLException {
this.pluginManager = pluginManager;
this.serviceContainer = serviceContainer;
this.pluginManager = serviceContainer.getConnectionPluginManager();
this.props = props;
this.originalUrl = originalUrl;
this.driverProtocol = targetDriverProtocol;
this.configurationProfile = configurationProfile;
this.exceptionManager = exceptionManager;
this.dialectProvider = dialectProvider != null ? dialectProvider : new DialectManager(this);
this.targetDriverDialect = targetDriverDialect;
this.storageService = serviceContainer.getStorageService();
this.connectionProviderManager = new ConnectionProviderManager(
this.pluginManager.getDefaultConnProvider(),
this.pluginManager.getEffectiveConnProvider());
Expand Down Expand Up @@ -206,8 +213,15 @@ public HostSpec getInitialConnectionHostSpec() {
}

@Override
public StorageService getStorageService() {
return this.storageService;
}

@Override
@Deprecated
public void setAllowedAndBlockedHosts(AllowedAndBlockedHosts allowedAndBlockedHosts) {
this.allowedAndBlockedHosts.set(allowedAndBlockedHosts);
this.storageService.set(
ItemCategory.ALLOWED_AND_BLOCKED_HOSTS, this.initialConnectionHostSpec.getHost(), allowedAndBlockedHosts);
}

@Override
Expand Down Expand Up @@ -401,7 +415,8 @@ public List<HostSpec> getAllHosts() {

@Override
public List<HostSpec> getHosts() {
AllowedAndBlockedHosts hostPermissions = this.allowedAndBlockedHosts.get();
AllowedAndBlockedHosts hostPermissions = this.storageService.get(
ItemCategory.ALLOWED_AND_BLOCKED_HOSTS, this.initialConnectionHostSpec.getHost(), AllowedAndBlockedHosts.class);
if (hostPermissions == null) {
return this.allHosts;
}
Expand Down Expand Up @@ -705,7 +720,7 @@ public void updateDialect(final @NonNull Connection connection) throws SQLExcept
}

final HostListProviderSupplier supplier = this.dialect.getHostListProvider();
this.setHostListProvider(supplier.getProvider(props, this.originalUrl, this, this));
this.setHostListProvider(supplier.getProvider(this.props, this.originalUrl, this.serviceContainer));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.sql.Statement;
import java.util.Collections;
import java.util.List;
import software.amazon.jdbc.PluginService;
import software.amazon.jdbc.hostlistprovider.AuroraHostListProvider;
import software.amazon.jdbc.hostlistprovider.monitoring.MonitoringRdsHostListProvider;
import software.amazon.jdbc.plugin.failover2.FailoverConnectionPlugin;
Expand Down Expand Up @@ -81,25 +82,24 @@ public boolean isDialect(final Connection connection) {

@Override
public HostListProviderSupplier getHostListProvider() {
return (properties, initialUrl, hostListProviderService, pluginService) -> {

return (properties, initialUrl, serviceContainer) -> {
final PluginService pluginService = serviceContainer.getPluginService();
final FailoverConnectionPlugin failover2Plugin = pluginService.getPlugin(FailoverConnectionPlugin.class);

if (failover2Plugin != null) {
return new MonitoringRdsHostListProvider(
properties,
initialUrl,
hostListProviderService,
serviceContainer,
TOPOLOGY_QUERY,
NODE_ID_QUERY,
IS_READER_QUERY,
IS_WRITER_QUERY,
pluginService);
IS_WRITER_QUERY);
}
return new AuroraHostListProvider(
properties,
initialUrl,
hostListProviderService,
serviceContainer,
TOPOLOGY_QUERY,
NODE_ID_QUERY,
IS_READER_QUERY);
Expand Down
Loading
Loading