Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
import javax.sql.DataSource;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand All @@ -36,6 +35,7 @@
import software.amazon.jdbc.util.Messages;
import software.amazon.jdbc.util.PropertyUtils;
import software.amazon.jdbc.util.RdsUtils;
import software.amazon.jdbc.util.ResourceLock;
import software.amazon.jdbc.util.SqlState;
import software.amazon.jdbc.util.WrapperUtils;

Expand All @@ -59,7 +59,7 @@ public class DataSourceConnectionProvider implements ConnectionProvider {
private final @NonNull DataSource dataSource;
private final @NonNull String dataSourceClassName;

private final ReentrantLock lock = new ReentrantLock();
private final ResourceLock lock = new ResourceLock();

private final RdsUtils rdsUtils = new RdsUtils();

Expand Down Expand Up @@ -138,12 +138,9 @@ public Connection connect(

// Data Source object could be shared between different threads while failover in progress.
// That's why it's important to configure Data Source object and get connection atomically.
this.lock.lock();
LOGGER.finest(() -> "Use main DataSource object to create a connection.");
try {
try (ResourceLock ignored = this.lock.obtain()) {
conn = this.openConnection(this.dataSource, protocol, targetDriverDialect, hostSpec, copy);
} finally {
this.lock.unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand All @@ -54,6 +53,7 @@
import software.amazon.jdbc.targetdriverdialect.TargetDriverDialect;
import software.amazon.jdbc.util.FullServicesContainer;
import software.amazon.jdbc.util.Messages;
import software.amazon.jdbc.util.ResourceLock;
import software.amazon.jdbc.util.Utils;
import software.amazon.jdbc.util.storage.CacheMap;
import software.amazon.jdbc.util.telemetry.TelemetryFactory;
Expand Down Expand Up @@ -90,7 +90,7 @@ public class PluginServiceImpl implements PluginService, CanReleaseResources,

protected final SessionStateService sessionStateService;

protected final ReentrantLock connectionSwitchLock = new ReentrantLock();
protected final ResourceLock connectionSwitchLock = new ResourceLock();

public PluginServiceImpl(
@NonNull final FullServicesContainer servicesContainer,
Expand Down Expand Up @@ -281,8 +281,7 @@ public EnumSet<NodeChangeOptions> setCurrentConnection(
@Nullable final ConnectionPlugin skipNotificationForThisPlugin)
throws SQLException {

connectionSwitchLock.lock();
try {
try (ResourceLock ignored = connectionSwitchLock.obtain()) {

if (this.currentConnection == null) {
// setting up an initial connection
Expand Down Expand Up @@ -350,8 +349,6 @@ public EnumSet<NodeChangeOptions> setCurrentConnection(
}
return changes;
}
} finally {
connectionSwitchLock.unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import software.amazon.jdbc.hostavailability.HostAvailability;
import software.amazon.jdbc.util.Messages;
import software.amazon.jdbc.util.ResourceLock;
import software.amazon.jdbc.util.StringUtils;
import software.amazon.jdbc.util.storage.CacheMap;

Expand All @@ -48,7 +48,7 @@ public class RoundRobinHostSelector implements HostSelector {
Pattern.compile("((?<host>[^:/?#]*):(?<weight>[0-9]*))");
protected static final CacheMap<String, RoundRobinClusterInfo> roundRobinCache = new CacheMap<>();

protected static final ReentrantLock lock = new ReentrantLock();
protected static final ResourceLock lock = new ResourceLock();

static {
PropertyDefinition.registerPluginProperties(RoundRobinHostSelector.class);
Expand Down Expand Up @@ -76,8 +76,7 @@ public HostSpec getHost(
final @NonNull HostRole role,
final @Nullable Properties props) throws SQLException {

lock.lock();
try {
try (ResourceLock ignored = lock.obtain()) {
final List<HostSpec> eligibleHosts = hosts.stream()
.filter(hostSpec ->
role.equals(hostSpec.getRole()) && hostSpec.getAvailability().equals(HostAvailability.AVAILABLE))
Expand Down Expand Up @@ -125,8 +124,6 @@ public HostSpec getHost(

return eligibleHosts.get(targetHostIndex);

} finally {
lock.unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import software.amazon.jdbc.hostavailability.HostAvailability;
import software.amazon.jdbc.util.Messages;
import software.amazon.jdbc.util.ResourceLock;

public class WeightedRandomHostSelector implements HostSelector {
public static final AwsWrapperProperty WEIGHTED_RANDOM_HOST_WEIGHT_PAIRS = new AwsWrapperProperty(
Expand All @@ -45,7 +45,7 @@ public class WeightedRandomHostSelector implements HostSelector {
private String cachedHostWeightMapString;
private Random random;

private final ReentrantLock lock = new ReentrantLock();
private final ResourceLock lock = new ResourceLock();

public WeightedRandomHostSelector() {
this(new Random());
Expand Down Expand Up @@ -109,8 +109,7 @@ public HostSpec getHost(
}

private Map<String, Integer> getHostWeightPairMap(final String hostWeightMapString) throws SQLException {
try {
lock.lock();
try (ResourceLock ignored = lock.obtain()) {
if (this.cachedHostWeightMapString != null
&& this.cachedHostWeightMapString.trim().equals(hostWeightMapString.trim())
&& this.cachedHostWeightMap != null
Expand Down Expand Up @@ -148,8 +147,6 @@ private Map<String, Integer> getHostWeightPairMap(final String hostWeightMapStri
this.cachedHostWeightMap = hostWeightMap;
this.cachedHostWeightMapString = hostWeightMapString;
return hostWeightMap;
} finally {
lock.unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,33 @@
package software.amazon.jdbc.authentication;

import java.util.Properties;
import java.util.concurrent.locks.ReentrantLock;
import org.checkerframework.checker.nullness.qual.Nullable;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.jdbc.HostSpec;
import software.amazon.jdbc.PropertyDefinition;
import software.amazon.jdbc.util.ResourceLock;
import software.amazon.jdbc.util.StringUtils;

public class AwsCredentialsManager {
private static AwsCredentialsProviderHandler handler = null;

private static final ReentrantLock lock = new ReentrantLock();
private static final ResourceLock lock = new ResourceLock();

public static void setCustomHandler(final AwsCredentialsProviderHandler customHandler) {
lock.lock();
try {
try (ResourceLock ignored = lock.obtain()) {
handler = customHandler;
} finally {
lock.unlock();
}
}

public static void resetCustomHandler() {
lock.lock();
try {
try (ResourceLock ignored = lock.obtain()) {
handler = null;
} finally {
lock.unlock();
}
}

public static AwsCredentialsProvider getProvider(final HostSpec hostSpec, final Properties props) {
lock.lock();
try {
try (ResourceLock ignored = lock.obtain()) {
AwsCredentialsProvider provider = handler == null
? null
: handler.getAwsCredentialsProvider(hostSpec, props);
Expand All @@ -60,8 +53,6 @@ public static AwsCredentialsProvider getProvider(final HostSpec hostSpec, final
}

return provider;
} finally {
lock.unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.UUID;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand All @@ -52,6 +51,7 @@
import software.amazon.jdbc.util.Messages;
import software.amazon.jdbc.util.RdsUrlType;
import software.amazon.jdbc.util.RdsUtils;
import software.amazon.jdbc.util.ResourceLock;
import software.amazon.jdbc.util.StringUtils;
import software.amazon.jdbc.util.SynchronousExecutor;
import software.amazon.jdbc.util.Utils;
Expand Down Expand Up @@ -106,7 +106,7 @@ public class RdsHostListProvider implements DynamicHostListProvider {
protected List<HostSpec> initialHostList = new ArrayList<>();
protected HostSpec initialHostSpec;

protected final ReentrantLock lock = new ReentrantLock();
protected final ResourceLock lock = new ResourceLock();
protected String clusterId;
protected HostSpec clusterInstanceTemplate;

Expand Down Expand Up @@ -143,8 +143,7 @@ protected void init() throws SQLException {
return;
}

lock.lock();
try {
try (ResourceLock ignored = lock.obtain()) {
if (this.isInitialized) {
return;
}
Expand Down Expand Up @@ -210,8 +209,6 @@ protected void init() throws SQLException {
}

this.isInitialized = true;
} finally {
lock.unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand All @@ -53,6 +52,7 @@
import software.amazon.jdbc.util.Messages;
import software.amazon.jdbc.util.PropertyUtils;
import software.amazon.jdbc.util.RdsUtils;
import software.amazon.jdbc.util.ResourceLock;
import software.amazon.jdbc.util.ServiceUtility;
import software.amazon.jdbc.util.StringUtils;
import software.amazon.jdbc.util.SynchronousExecutor;
Expand Down Expand Up @@ -101,7 +101,7 @@ public class ClusterTopologyMonitorImpl extends AbstractMonitor implements Clust
protected final AtomicLong ignoreNewTopologyRequestsEndTimeNano = new AtomicLong(-1);
protected final ConcurrentHashMap<String, Boolean> submittedNodes = new ConcurrentHashMap<>();
protected ExecutorService nodeExecutorService = null;
protected final ReentrantLock nodeExecutorLock = new ReentrantLock();
protected final ResourceLock nodeExecutorLock = new ResourceLock();
protected final AtomicBoolean nodeThreadsStop = new AtomicBoolean(false);
protected final AtomicReference<Connection> nodeThreadsWriterConnection = new AtomicReference<>(null);
protected final AtomicReference<HostSpec> nodeThreadsWriterHostSpec = new AtomicReference<>(null);
Expand Down Expand Up @@ -506,8 +506,7 @@ public void processEvent(Event event) {
protected void shutdownNodeExecutorService() {
if (this.nodeExecutorService != null) {

this.nodeExecutorLock.lock();
try {
try (ResourceLock ignored = this.nodeExecutorLock.obtain()) {

if (this.nodeExecutorService == null) {
return;
Expand All @@ -526,18 +525,13 @@ protected void shutdownNodeExecutorService() {
}

this.nodeExecutorService = null;
} finally {
this.nodeExecutorLock.unlock();
}
}
}

protected void createNodeExecutorService() {
this.nodeExecutorLock.lock();
try {
try (ResourceLock ignored = this.nodeExecutorLock.obtain()) {
this.nodeExecutorService = ExecutorFactory.newCachedThreadPool("node");
} finally {
this.nodeExecutorLock.unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -59,6 +58,7 @@
import software.amazon.jdbc.util.Pair;
import software.amazon.jdbc.util.PropertyUtils;
import software.amazon.jdbc.util.RdsUtils;
import software.amazon.jdbc.util.ResourceLock;
import software.amazon.jdbc.util.StringUtils;
import software.amazon.jdbc.util.Utils;
import software.amazon.jdbc.util.events.MonitorResetEvent;
Expand Down Expand Up @@ -114,7 +114,7 @@ public class BlueGreenStatusProvider {
protected AtomicBoolean monitorResetOnTopologyCompleted = new AtomicBoolean(false);
protected final AtomicBoolean allGreenNodesChangedName = new AtomicBoolean(false);
protected long postStatusEndTimeNano = 0;
protected final ReentrantLock processStatusLock = new ReentrantLock();
protected final ResourceLock processStatusLock = new ResourceLock();

// Status check interval time in millis for each BlueGreenIntervalRate.
protected final Map<BlueGreenIntervalRate, Long> statusCheckIntervalMap = new HashMap<>();
Expand Down Expand Up @@ -202,8 +202,7 @@ protected void prepareStatus(
final @NonNull BlueGreenRole role,
final @NonNull BlueGreenInterimStatus interimStatus) {

this.processStatusLock.lock();
try {
try (ResourceLock ignored = this.processStatusLock.obtain()) {

// Detect changes
int statusHash = interimStatus.getCustomHashCode();
Expand Down Expand Up @@ -243,8 +242,6 @@ protected void prepareStatus(

this.resetContextWhenCompleted();

} finally {
this.processStatusLock.unlock();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
import java.sql.SQLException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;
import software.amazon.jdbc.util.ExecutorFactory;
import software.amazon.jdbc.util.Messages;
import software.amazon.jdbc.util.ResourceLock;
import software.amazon.jdbc.util.telemetry.TelemetryCounter;

/**
Expand Down Expand Up @@ -51,7 +51,7 @@ public class HostMonitorConnectionContext {
private long invalidNodeStartTimeNano; // Only accessed by monitor thread
private long failureCount; // Only accessed by monitor thread

private final ReentrantLock lock = new ReentrantLock();
private final ResourceLock lock = new ResourceLock();

/**
* Constructor.
Expand Down Expand Up @@ -246,7 +246,7 @@ void setConnectionValid(
new Object[] {hostName}));
}

public ReentrantLock getLock() {
public ResourceLock getLock() {
return this.lock;
}
}
Loading
Loading