Skip to content

Commit

Permalink
PHOENIX-6510 Double-Checked Locking field must be volatile
Browse files Browse the repository at this point in the history
added volatile keyword to the variable to be doubly checked.
Do not use double check pattern in PhoenixConnection.
  • Loading branch information
jojochuang authored and stoty committed Jul 15, 2021
1 parent a073d5b commit 00d9031
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 25 deletions.
Expand Up @@ -177,8 +177,8 @@ public class PhoenixConnection implements Connection, MetaDataMutated, SQLClosea
private Double logSamplingRate;
private String sourceOfOperation;

private Object queueCreationLock = new Object(); // lock for the lazy init path of childConnections structure
private ConcurrentLinkedQueue<PhoenixConnection> childConnections = null;
private final ConcurrentLinkedQueue<PhoenixConnection> childConnections =
new ConcurrentLinkedQueue<>();

//For now just the copy constructor paths will have this as true as I don't want to change the
//public interfaces.
Expand Down Expand Up @@ -467,18 +467,10 @@ public boolean isInternalConnection() {
}

/**
* This method, and *only* this method is thread safe
* Add connection to the internal childConnections queue
* @param connection
*/
public void addChildConnection(PhoenixConnection connection) {
//double check for performance
if(childConnections == null) {
synchronized (queueCreationLock) {
if (childConnections == null) {
childConnections = new ConcurrentLinkedQueue<>();
}
}
}
childConnections.add(connection);
}

Expand All @@ -488,9 +480,7 @@ public void addChildConnection(PhoenixConnection connection) {
* @param connection
*/
public void removeChildConnection(PhoenixConnection connection) {
if (childConnections != null) {
childConnections.remove(connection);
}
childConnections.remove(connection);
}

/**
Expand All @@ -500,10 +490,7 @@ public void removeChildConnection(PhoenixConnection connection) {
*/
@VisibleForTesting
public int getChildConnectionsCount() {
if (childConnections != null) {
return childConnections.size();
}
return 0;
return childConnections.size();
}

public Sampler<?> getSampler() {
Expand Down Expand Up @@ -721,11 +708,7 @@ public void close() throws SQLException {
traceScope.close();
}
closeStatements();
synchronized (queueCreationLock) {
if (childConnections != null) {
SQLCloseables.closeAllQuietly(childConnections);
}
}
SQLCloseables.closeAllQuietly(childConnections);
} finally {
services.removeConnection(this);
}
Expand Down
Expand Up @@ -41,7 +41,7 @@
*/
public class TableLogWriter implements LogWriter {
private static final Logger LOGGER = LoggerFactory.getLogger(LogWriter.class);
private Connection connection;
private volatile Connection connection;
private boolean isClosed;
private PreparedStatement upsertStatement;
private Configuration config;
Expand Down
Expand Up @@ -33,7 +33,7 @@ private static class PNameImplData {
/** */
public byte[] bytesName;
/** */
public ImmutableBytesPtr ptr;
public volatile ImmutableBytesPtr ptr;

/**
*
Expand Down

0 comments on commit 00d9031

Please sign in to comment.