You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by eo...@apache.org on 2018/07/23 14:39:52 UTC

[bookkeeper] branch master updated: Allow concurrent write requests for same entry in V2 protocol

This is an automated email from the ASF dual-hosted git repository.

eolivelli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 424204e  Allow concurrent write requests for same entry in V2 protocol
424204e is described below

commit 424204ebecb8ba2e4856cb2f570f3a8bf1589da8
Author: Ivan Kelly <iv...@ivankelly.net>
AuthorDate: Mon Jul 23 16:39:46 2018 +0200

    Allow concurrent write requests for same entry in V2 protocol
    
    With the V2 protocol we have previously allowed a single client to
    issue concurrent read requests for the same entry. We didn't allow the
    same for write requests, as we always assume single writer. This
    assumption isn't absolutely correct though. When opening a ledger, the
    recovery process will rewrite entries after the LAC. To
    PerChannelBookieClient these look no different to normal writes.
    
    If a client tries to open a ledger multiple times concurrently,
    currently some of the attempts can hang forever since there is no
    support for concurrent writes.
    
    This patch adds support for concurrent writes, so multiple concurrent
    ledger open attempts will always complete (though some may fail for
    unrelated reasons).
    
    Author: Ivan Kelly <iv...@ivankelly.net>
    
    Reviewers: Enrico Olivelli <eo...@gmail.com>, Sijie Guo <si...@apache.org>
    
    This closes #1547 from ivankelly/conc-open
---
 .../bookkeeper/proto/PerChannelBookieClient.java   |  47 +++++----
 .../client/ConcurrentV2RecoveryTest.java           | 113 +++++++++++++++++++++
 2 files changed, 141 insertions(+), 19 deletions(-)

diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java
index 51c875b..ba704e4 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/proto/PerChannelBookieClient.java
@@ -683,7 +683,7 @@ public class PerChannelBookieClient extends ChannelInboundHandlerAdapter {
                     .build();
         }
 
-        completionObjects.put(completionKey,
+        putCompletionKeyValue(completionKey,
                               acquireAddCompletion(completionKey,
                                                    cb, ctx, ledgerId, entryId));
         final Channel c = channel;
@@ -722,7 +722,7 @@ public class PerChannelBookieClient extends ChannelInboundHandlerAdapter {
                     .setReadLacRequest(readLacBuilder)
                     .build();
         }
-        completionObjects.put(completionKey,
+        putCompletionKeyValue(completionKey,
                               new ReadLacCompletion(completionKey, cb,
                                                     ctx, ledgerId));
         writeAndFlush(channel, completionKey, request);
@@ -831,13 +831,7 @@ public class PerChannelBookieClient extends ChannelInboundHandlerAdapter {
         }
 
         ReadCompletion readCompletion = new ReadCompletion(completionKey, cb, ctx, ledgerId, entryId);
-        CompletionValue existingValue = completionObjects.putIfAbsent(completionKey, readCompletion);
-        if (existingValue != null) {
-            // There's a pending read request on same ledger/entry. Use the multimap to track all of them
-            synchronized (completionObjectsV2Conflicts) {
-                completionObjectsV2Conflicts.put(completionKey, readCompletion);
-            }
-        }
+        putCompletionKeyValue(completionKey, readCompletion);
 
         writeAndFlush(channel, completionKey, request, allowFastFail);
     }
@@ -1200,17 +1194,8 @@ public class PerChannelBookieClient extends ChannelInboundHandlerAdapter {
         StatusCode status = getStatusCodeFromErrorCode(response.errorCode);
 
         CompletionKey key = acquireV2Key(response.ledgerId, response.entryId, operationType);
-        CompletionValue completionValue = completionObjects.remove(key);
+        CompletionValue completionValue = getCompletionValue(key);
         key.release();
-        if (completionValue == null) {
-            // If there's no completion object here, try in the multimap
-            synchronized (this) {
-                if (completionObjectsV2Conflicts.containsKey(key)) {
-                    completionValue = completionObjectsV2Conflicts.get(key).get(0);
-                    completionObjectsV2Conflicts.remove(key, completionValue);
-                }
-            }
-        }
 
         if (null == completionValue) {
             // Unexpected response, so log it. The txnId should have been present.
@@ -2085,6 +2070,30 @@ public class PerChannelBookieClient extends ChannelInboundHandlerAdapter {
         }
     }
 
+    private void putCompletionKeyValue(CompletionKey key, CompletionValue value) {
+        CompletionValue existingValue = completionObjects.putIfAbsent(key, value);
+        if (existingValue != null) { // will only happen for V2 keys, as V3 have unique txnid
+            // There's a pending read request on same ledger/entry. Use the multimap to track all of them
+            synchronized (completionObjectsV2Conflicts) {
+                completionObjectsV2Conflicts.put(key, value);
+            }
+        }
+    }
+
+    private CompletionValue getCompletionValue(CompletionKey key) {
+        CompletionValue completionValue = completionObjects.remove(key);
+        if (completionValue == null) {
+            // If there's no completion object here, try in the multimap
+            synchronized (this) {
+                if (completionObjectsV2Conflicts.containsKey(key)) {
+                    completionValue = completionObjectsV2Conflicts.get(key).get(0);
+                    completionObjectsV2Conflicts.remove(key, completionValue);
+                }
+            }
+        }
+        return completionValue;
+    }
+
     private long getTxnId() {
         return txnIdGenerator.incrementAndGet();
     }
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ConcurrentV2RecoveryTest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ConcurrentV2RecoveryTest.java
new file mode 100644
index 0000000..9a94aaf
--- /dev/null
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/ConcurrentV2RecoveryTest.java
@@ -0,0 +1,113 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.bookkeeper.client;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.bookkeeper.client.BookKeeper.DigestType;
+import org.apache.bookkeeper.conf.ClientConfiguration;
+import org.apache.bookkeeper.meta.HierarchicalLedgerManagerFactory;
+import org.apache.bookkeeper.test.BookKeeperClusterTestCase;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Tests concurrent attempts to open and recovery a ledger with V2 protocol.
+ */
+public class ConcurrentV2RecoveryTest extends BookKeeperClusterTestCase  {
+    private static final Logger LOG = LoggerFactory.getLogger(ConcurrentV2RecoveryTest.class);
+    private final DigestType digestType;
+
+    public ConcurrentV2RecoveryTest() {
+        super(4);
+        this.digestType = DigestType.CRC32;
+    }
+
+    @Test
+    public void testConcurrentOpen() throws Exception {
+        ClientConfiguration conf = new ClientConfiguration();
+        conf.setMetadataServiceUri(zkUtil.getMetadataServiceUri())
+            .setNumChannelsPerBookie(16)
+            .setUseV2WireProtocol(true)
+            .setZkTimeout(20000)
+            .setAddEntryTimeout(30)
+            .setReadEntryTimeout(30)
+            .setSpeculativeReadTimeout(0)
+            .setThrottleValue(0)
+            .setLedgerManagerFactoryClassName(HierarchicalLedgerManagerFactory.class.getName());
+
+        BookKeeper bkc = new BookKeeper(conf);
+
+        for (int j = 0; j < 10; j++) {
+            LedgerHandle lh = bkc.createLedger(DigestType.CRC32, "testPasswd".getBytes());
+            lh.addEntry("foobar".getBytes());
+
+            long ledgerId = lh.getId();
+            final long finalLedgerId = ledgerId;
+            ExecutorService executor = Executors.newFixedThreadPool(10);
+            List<Future<?>> futures = new ArrayList<>();
+            CountDownLatch latch = new CountDownLatch(1);
+            for (int i = 0; i < 5; i++) {
+                final CompletableFuture<LedgerHandle> future = new CompletableFuture<>();
+                executor.submit(() -> {
+                            latch.await();
+
+                            bkc.asyncOpenLedger(finalLedgerId,
+                                                DigestType.CRC32, "testPasswd".getBytes(),
+                                                (rc, handle, ctx) -> {
+                                                    if (rc != BKException.Code.OK) {
+                                                        future.completeExceptionally(BKException.create(rc));
+                                                    } else {
+                                                        future.complete(handle);
+                                                    }
+                                                }, null);
+                            return future;
+                        });
+                futures.add(future);
+            }
+
+            latch.countDown();
+            for (Future<?> f : futures) {
+                try {
+                    f.get(10, TimeUnit.SECONDS);
+                } catch (ExecutionException ee) {
+                    // also fine, recovery can currently fail because of metadata conflicts.
+                    // We should fix this at some point by making the metadata immutable,
+                    // and restarting the entire operation
+                    Assert.assertEquals(ee.getCause().getClass(), BKException.BKLedgerRecoveryException.class);
+                }
+            }
+        }
+        bkc.close();
+    }
+}