You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2018/07/23 14:39:55 UTC

[GitHub] eolivelli closed pull request #1547: Allow concurrent write requests for same entry in V2 protocol

eolivelli closed pull request #1547: Allow concurrent write requests for same entry in V2 protocol
URL: https://github.com/apache/bookkeeper/pull/1547
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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 51c875bd82..ba704e4be7 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 @@ void addEntry(final long ledgerId, byte[] masterKey, final long entryId, ByteBuf
                     .build();
         }
 
-        completionObjects.put(completionKey,
+        putCompletionKeyValue(completionKey,
                               acquireAddCompletion(completionKey,
                                                    cb, ctx, ledgerId, entryId));
         final Channel c = channel;
@@ -722,7 +722,7 @@ public void readLac(final long ledgerId, ReadLacCallback cb, Object ctx) {
                     .setReadLacRequest(readLacBuilder)
                     .build();
         }
-        completionObjects.put(completionKey,
+        putCompletionKeyValue(completionKey,
                               new ReadLacCompletion(completionKey, cb,
                                                     ctx, ledgerId));
         writeAndFlush(channel, completionKey, request);
@@ -831,13 +831,7 @@ private void readEntryInternal(final long ledgerId,
         }
 
         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 @@ private void readV2Response(final BookieProtocol.Response response) {
         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 @@ private int statusCodeToExceptionCode(StatusCode status) {
         }
     }
 
+    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 0000000000..9a94aafa12
--- /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();
+    }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services