Repository: hbase Updated Branches: refs/heads/branch-1.2 481d3f435 -> 303e49e89
HBASE-14361 ReplicationSink should create Connection instances lazily Project: http://git-wip-us.apache.org/repos/asf/hbase/repo Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/303e49e8 Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/303e49e8 Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/303e49e8 Branch: refs/heads/branch-1.2 Commit: 303e49e89a7eeda9c23a939e729927997f97dda9 Parents: 481d3f4 Author: stack <st...@apache.org> Authored: Tue Sep 15 05:15:47 2015 -0700 Committer: stack <st...@apache.org> Committed: Tue Sep 15 05:16:39 2015 -0700 ---------------------------------------------------------------------- .../regionserver/ReplicationSink.java | 27 +++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/303e49e8/hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationSink.java ---------------------------------------------------------------------- diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationSink.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationSink.java index 7d47677..4dd76cb 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationSink.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/replication/regionserver/ReplicationSink.java @@ -72,9 +72,12 @@ public class ReplicationSink { private static final Log LOG = LogFactory.getLog(ReplicationSink.class); private final Configuration conf; - private final Connection sharedHtableCon; + // Volatile because of note in here -- look for double-checked locking: + // http://www.oracle.com/technetwork/articles/javase/bloch-effective-08-qa-140880.html + private volatile Connection sharedHtableCon; private final MetricsSink metrics; private final AtomicLong totalReplicatedEdits = new AtomicLong(); + private final Object sharedHtableConLock = new Object(); /** * Create a sink for replication @@ -88,7 +91,6 @@ public class ReplicationSink { this.conf = HBaseConfiguration.create(conf); decorateConf(); this.metrics = new MetricsSink(); - this.sharedHtableCon = ConnectionFactory.createConnection(this.conf); } /** @@ -213,7 +215,14 @@ public class ReplicationSink { */ public void stopReplicationSinkServices() { try { - this.sharedHtableCon.close(); + if (this.sharedHtableCon != null) { + synchronized (sharedHtableConLock) { + if (this.sharedHtableCon != null) { + this.sharedHtableCon.close(); + this.sharedHtableCon = null; + } + } + } } catch (IOException e) { LOG.warn("IOException while closing the connection", e); // ignoring as we are closing. } @@ -232,7 +241,17 @@ public class ReplicationSink { } Table table = null; try { - table = this.sharedHtableCon.getTable(tableName); + // See https://en.wikipedia.org/wiki/Double-checked_locking + Connection connection = this.sharedHtableCon; + if (connection == null) { + synchronized (sharedHtableConLock) { + connection = this.sharedHtableCon; + if (connection == null) { + connection = this.sharedHtableCon = ConnectionFactory.createConnection(this.conf); + } + } + } + table = connection.getTable(tableName); for (List<Row> rows : allRows) { table.batch(rows); }