From 236f0050881384fcfc6d7c2c1209b80ae225e472 Mon Sep 17 00:00:00 2001 From: stack Date: Tue, 15 Sep 2015 05:15:47 -0700 Subject: [PATCH] HBASE-14361 ReplicationSink should create Connection instances lazily --- .../regionserver/ReplicationSink.java | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) 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 7d476774ba0..4dd76cbb928 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 rows : allRows) { table.batch(rows); }