You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by ec...@apache.org on 2015/10/30 21:42:10 UTC

hbase git commit: HBASE-14687 Un-synchronize BufferedMutator

Repository: hbase
Updated Branches:
  refs/heads/master b0ad82191 -> ec651bfc7


HBASE-14687 Un-synchronize BufferedMutator

Summary:
Use concurrent collections and atomic longs to keep track of edits in buffered mutator.
This keeps buffered mutator as thread safe but it means that shared buffered mutators are not contending on thread locking.

Test Plan: Unit Tests.

Differential Revision: https://reviews.facebook.net/D49467


Project: http://git-wip-us.apache.org/repos/asf/hbase/repo
Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/ec651bfc
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/ec651bfc
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/ec651bfc

Branch: refs/heads/master
Commit: ec651bfc7bb004c7a14503bd6ea5dc376975bd3e
Parents: b0ad821
Author: Elliott Clark <ec...@apache.org>
Authored: Mon Oct 26 14:47:36 2015 -0700
Committer: Elliott Clark <ec...@apache.org>
Committed: Fri Oct 30 13:27:25 2015 -0700

----------------------------------------------------------------------
 .../hbase/client/BufferedMutatorImpl.java       | 106 +++++++++++++------
 .../hadoop/hbase/client/TestAsyncProcess.java   |  32 +-----
 2 files changed, 77 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/ec651bfc/hbase-client/src/main/java/org/apache/hadoop/hbase/client/BufferedMutatorImpl.java
----------------------------------------------------------------------
diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/BufferedMutatorImpl.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/BufferedMutatorImpl.java
index 5341d47..ef3f7e9 100644
--- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/BufferedMutatorImpl.java
+++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/BufferedMutatorImpl.java
@@ -15,6 +15,7 @@
  */
 package org.apache.hadoop.hbase.client;
 
+import com.google.common.annotations.VisibleForTesting;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.hadoop.conf.Configuration;
@@ -23,15 +24,15 @@ import org.apache.hadoop.hbase.classification.InterfaceAudience;
 import org.apache.hadoop.hbase.classification.InterfaceStability;
 import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
 
-import com.google.common.annotations.VisibleForTesting;
-
 import java.io.IOException;
 import java.io.InterruptedIOException;
 import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
 
 /**
  * <p>
@@ -40,6 +41,12 @@ import java.util.concurrent.TimeUnit;
  * a {@link Connection} and call {@link #close()} afterwards.
  * </p>
  *
+ * <p>
+ * While this can be used accross threads, great care should be used when doing so.
+ * Errors are global to the buffered mutator and the Exceptions can be thrown on any
+ * thread that causes the flush for requests.
+ * </p>
+ *
  * @see ConnectionFactory
  * @see Connection
  * @since 1.0.0
@@ -55,13 +62,18 @@ public class BufferedMutatorImpl implements BufferedMutator {
   protected ClusterConnection connection; // non-final so can be overridden in test
   private final TableName tableName;
   private volatile Configuration conf;
+
   @VisibleForTesting
-  List<Row> writeAsyncBuffer = new LinkedList<>();
+  final ConcurrentLinkedQueue<Mutation> writeAsyncBuffer = new ConcurrentLinkedQueue<Mutation>();
+  @VisibleForTesting
+  AtomicLong currentWriteBufferSize = new AtomicLong(0);
+
   private long writeBufferSize;
   private final int maxKeyValueSize;
-  protected long currentWriteBufferSize = 0;
   private boolean closed = false;
   private final ExecutorService pool;
+
+  @VisibleForTesting
   protected AsyncProcess ap; // non-final so can be overridden in test
 
   BufferedMutatorImpl(ClusterConnection conn, RpcRetryingCallerFactory rpcCallerFactory,
@@ -97,38 +109,41 @@ public class BufferedMutatorImpl implements BufferedMutator {
   }
 
   @Override
-  public synchronized void mutate(Mutation m) throws InterruptedIOException,
+  public void mutate(Mutation m) throws InterruptedIOException,
       RetriesExhaustedWithDetailsException {
     mutate(Arrays.asList(m));
   }
 
   @Override
-  public synchronized void mutate(List<? extends Mutation> ms) throws InterruptedIOException,
+  public void mutate(List<? extends Mutation> ms) throws InterruptedIOException,
       RetriesExhaustedWithDetailsException {
+
     if (closed) {
       throw new IllegalStateException("Cannot put when the BufferedMutator is closed.");
     }
 
+    long toAddSize = 0;
     for (Mutation m : ms) {
       if (m instanceof Put) {
         validatePut((Put) m);
       }
-        currentWriteBufferSize += m.heapSize();
+      toAddSize += m.heapSize();
     }
 
-      // This behavior is highly non-intuitive... it does not protect us against
-      // 94-incompatible behavior, which is a timing issue because hasError, the below code
-      // and setter of hasError are not synchronized. Perhaps it should be removed.
-      if (ap.hasError()) {
-        writeAsyncBuffer.addAll(ms);
-        backgroundFlushCommits(true);
-      } else {
-        writeAsyncBuffer.addAll(ms);
-      }
-
+    // This behavior is highly non-intuitive... it does not protect us against
+    // 94-incompatible behavior, which is a timing issue because hasError, the below code
+    // and setter of hasError are not synchronized. Perhaps it should be removed.
+    if (ap.hasError()) {
+      currentWriteBufferSize.addAndGet(toAddSize);
+      writeAsyncBuffer.addAll(ms);
+      backgroundFlushCommits(true);
+    } else {
+      currentWriteBufferSize.addAndGet(toAddSize);
+      writeAsyncBuffer.addAll(ms);
+    }
 
     // Now try and queue what needs to be queued.
-    while (currentWriteBufferSize > writeBufferSize) {
+    while (currentWriteBufferSize.get() > writeBufferSize) {
       backgroundFlushCommits(false);
     }
   }
@@ -140,15 +155,15 @@ public class BufferedMutatorImpl implements BufferedMutator {
 
   @Override
   public synchronized void close() throws IOException {
-    if (this.closed) {
-      return;
-    }
     try {
+      if (this.closed) {
+        return;
+      }
       // As we can have an operation in progress even if the buffer is empty, we call
       // backgroundFlushCommits at least one time.
       backgroundFlushCommits(true);
       this.pool.shutdown();
-      boolean terminated = false;
+      boolean terminated;
       int loopCnt = 0;
       do {
         // wait until the pool has terminated
@@ -159,8 +174,10 @@ public class BufferedMutatorImpl implements BufferedMutator {
           break;
         }
       } while (!terminated);
+
     } catch (InterruptedException e) {
       LOG.warn("waitForTermination interrupted");
+
     } finally {
       this.closed = true;
     }
@@ -182,19 +199,44 @@ public class BufferedMutatorImpl implements BufferedMutator {
    * @param synchronous - if true, sends all the writes and wait for all of them to finish before
    *        returning.
    */
-  private void backgroundFlushCommits(boolean synchronous) throws InterruptedIOException,
+  private void backgroundFlushCommits(boolean synchronous) throws
+      InterruptedIOException,
       RetriesExhaustedWithDetailsException {
+
+    LinkedList<Mutation> buffer = new LinkedList<>();
+    // Keep track of the size so that this thread doesn't spin forever
+    long dequeuedSize = 0;
+
     try {
+      // Grab all of the available mutations.
+      Mutation m;
+
+      // If there's no buffer size drain everything. If there is a buffersize drain up to twice
+      // that amount. This should keep the loop from continually spinning if there are threads
+      // that keep adding more data to the buffer.
+      while (
+          (writeBufferSize <= 0 || dequeuedSize < (writeBufferSize * 2) || synchronous)
+              && (m = writeAsyncBuffer.poll()) != null) {
+        buffer.add(m);
+        long size = m.heapSize();
+        dequeuedSize += size;
+        currentWriteBufferSize.addAndGet(-size);
+      }
+
+      if (!synchronous && dequeuedSize == 0) {
+        return;
+      }
+
       if (!synchronous) {
-        ap.submit(tableName, writeAsyncBuffer, true, null, false);
+        ap.submit(tableName, buffer, true, null, false);
         if (ap.hasError()) {
           LOG.debug(tableName + ": One or more of the operations have failed -"
               + " waiting for all operation in progress to finish (successfully or not)");
         }
       }
       if (synchronous || ap.hasError()) {
-        while (!writeAsyncBuffer.isEmpty()) {
-          ap.submit(tableName, writeAsyncBuffer, true, null, false);
+        while (!buffer.isEmpty()) {
+          ap.submit(tableName, buffer, true, null, false);
         }
         RetriesExhaustedWithDetailsException error = ap.waitForAllPreviousOpsAndReset(null);
         if (error != null) {
@@ -206,11 +248,11 @@ public class BufferedMutatorImpl implements BufferedMutator {
         }
       }
     } finally {
-      currentWriteBufferSize = 0;
-      for (Row mut : writeAsyncBuffer) {
-        if (mut instanceof Mutation) {
-          currentWriteBufferSize += ((Mutation) mut).heapSize();
-        }
+      for (Mutation mut : buffer) {
+        long size = mut.heapSize();
+        currentWriteBufferSize.addAndGet(size);
+        dequeuedSize -= size;
+        writeAsyncBuffer.add(mut);
       }
     }
   }
@@ -224,7 +266,7 @@ public class BufferedMutatorImpl implements BufferedMutator {
   public void setWriteBufferSize(long writeBufferSize) throws RetriesExhaustedWithDetailsException,
       InterruptedIOException {
     this.writeBufferSize = writeBufferSize;
-    if (currentWriteBufferSize > writeBufferSize) {
+    if (currentWriteBufferSize.get() > writeBufferSize) {
       flush();
     }
   }

http://git-wip-us.apache.org/repos/asf/hbase/blob/ec651bfc/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestAsyncProcess.java
----------------------------------------------------------------------
diff --git a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestAsyncProcess.java b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestAsyncProcess.java
index b784f7a..dc74d3d 100644
--- a/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestAsyncProcess.java
+++ b/hbase-client/src/test/java/org/apache/hadoop/hbase/client/TestAsyncProcess.java
@@ -408,7 +408,7 @@ public class TestAsyncProcess {
   }
 
   @Rule
-  public Timeout timeout = new Timeout(10000); // 10 seconds max per method tested
+  public Timeout timeout = Timeout.millis(10000); // 10 seconds max per method tested
 
   @Test
   public void testSubmit() throws Exception {
@@ -698,7 +698,7 @@ public class TestAsyncProcess {
 
     Put put = createPut(1, false);
 
-    Assert.assertEquals(0L, ht.mutator.currentWriteBufferSize);
+    Assert.assertEquals(0L, ht.mutator.currentWriteBufferSize.get());
     try {
       ht.put(put);
       if (bufferOn) {
@@ -707,7 +707,7 @@ public class TestAsyncProcess {
       Assert.fail();
     } catch (RetriesExhaustedException expected) {
     }
-    Assert.assertEquals(0L, ht.mutator.currentWriteBufferSize);
+    Assert.assertEquals(0L, ht.mutator.currentWriteBufferSize.get());
     // The table should have sent one request, maybe after multiple attempts
     AsyncRequestFuture ars = null;
     for (AsyncRequestFuture someReqs : ap.allReqs) {
@@ -760,32 +760,6 @@ public class TestAsyncProcess {
     Assert.assertEquals("the put should not been inserted.", 0, mutator.writeAsyncBuffer.size());
   }
 
-
-/*
-  @Test
-  public void testWithNoClearOnFail() throws IOException {
-    HTable ht = new HTable();
-    ht.ap = new MyAsyncProcess(createHConnection(), conf, true);
-    ht.setAutoFlushTo(false);
-
-    Put p = createPut(1, false);
-    ht.put(p);
-    Assert.assertEquals(0, ht.writeAsyncBuffer.size());
-
-    try {
-      ht.flushCommits();
-    } catch (RetriesExhaustedWithDetailsException expected) {
-    }
-    Assert.assertEquals(1, ht.writeAsyncBuffer.size());
-
-    try {
-      ht.close();
-    } catch (RetriesExhaustedWithDetailsException expected) {
-    }
-    Assert.assertEquals(1, ht.writeAsyncBuffer.size());
-  }
-  */
-
   @Test
   public void testBatch() throws IOException, InterruptedException {
     ClusterConnection conn = new MyConnectionImpl(conf);