You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by st...@apache.org on 2021/07/15 09:53:52 UTC
[phoenix] branch 4.16 updated: PHOENIX-6510 Double-Checked Locking
field must be volatile
This is an automated email from the ASF dual-hosted git repository.
stoty pushed a commit to branch 4.16
in repository https://gitbox.apache.org/repos/asf/phoenix.git
The following commit(s) were added to refs/heads/4.16 by this push:
new 089a784 PHOENIX-6510 Double-Checked Locking field must be volatile
089a784 is described below
commit 089a7840006c49fd1d9c01ceb2661475f9481e70
Author: Wei-Chiu Chuang <we...@apache.org>
AuthorDate: Sat Jul 3 00:45:21 2021 +0800
PHOENIX-6510 Double-Checked Locking field must be volatile
added volatile keyword to the variable to be doubly checked.
Do not use double check pattern in PhoenixConnection.
---
.../org/apache/phoenix/jdbc/PhoenixConnection.java | 29 +++++-----------------
.../org/apache/phoenix/log/TableLogWriter.java | 2 +-
.../java/org/apache/phoenix/schema/PNameImpl.java | 2 +-
3 files changed, 8 insertions(+), 25 deletions(-)
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.java
index ddc1dd3..cccf4be 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixConnection.java
@@ -175,8 +175,8 @@ public class PhoenixConnection implements Connection, MetaDataMutated, SQLClosea
private Double logSamplingRate;
private String sourceOfOperation;
- private final 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.
@@ -463,18 +463,10 @@ public class PhoenixConnection implements Connection, MetaDataMutated, SQLClosea
}
/**
- * 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);
}
@@ -484,9 +476,7 @@ public class PhoenixConnection implements Connection, MetaDataMutated, SQLClosea
* @param connection
*/
public void removeChildConnection(PhoenixConnection connection) {
- if (childConnections != null) {
- childConnections.remove(connection);
- }
+ childConnections.remove(connection);
}
/**
@@ -496,10 +486,7 @@ public class PhoenixConnection implements Connection, MetaDataMutated, SQLClosea
*/
@VisibleForTesting
public int getChildConnectionsCount() {
- if (childConnections != null) {
- return childConnections.size();
- }
- return 0;
+ return childConnections.size();
}
public Sampler<?> getSampler() {
@@ -716,11 +703,7 @@ public class PhoenixConnection implements Connection, MetaDataMutated, SQLClosea
traceScope.close();
}
closeStatements();
- synchronized (queueCreationLock) {
- if (childConnections != null) {
- SQLCloseables.closeAllQuietly(childConnections);
- }
- }
+ SQLCloseables.closeAllQuietly(childConnections);
} finally {
services.removeConnection(this);
}
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/log/TableLogWriter.java b/phoenix-core/src/main/java/org/apache/phoenix/log/TableLogWriter.java
index fbc6b2d..ee0d494 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/log/TableLogWriter.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/log/TableLogWriter.java
@@ -41,7 +41,7 @@ import com.google.common.collect.ImmutableMap;
*/
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;
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/PNameImpl.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/PNameImpl.java
index dd1f6ec..bccf7bf 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/schema/PNameImpl.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/PNameImpl.java
@@ -33,7 +33,7 @@ public class PNameImpl implements PName {
/** */
public byte[] bytesName;
/** */
- public ImmutableBytesPtr ptr;
+ public volatile ImmutableBytesPtr ptr;
/**
*