From 268df2094a595f0c00ef85400def9f672c45cb4c Mon Sep 17 00:00:00 2001 From: eolivelli Date: Fri, 11 Jan 2019 23:28:09 +0100 Subject: [PATCH 1/6] Read ExplicitLAC in asyncReadLastConfirmed - Readers will handle explicit LAC transparently - V2 Readers stay with old PiggyBackLAC --- .../java/org/apache/bookkeeper/client/LedgerHandle.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/LedgerHandle.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/LedgerHandle.java index 7e21f975230..f3639d67216 100644 --- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/LedgerHandle.java +++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/LedgerHandle.java @@ -1371,6 +1371,15 @@ synchronized void updateLastConfirmed(long lac, long len) { */ public void asyncReadLastConfirmed(final ReadLastConfirmedCallback cb, final Object ctx) { + if (clientCtx.getConf().useV2WireProtocol) { + // in v2 protocol we don't support readLAC RPC + asyncReadPiggybackLastConfirmed(cb, ctx); + } else { + asyncReadExplicitLastConfirmed(cb, ctx); + } + } + + private void asyncReadPiggybackLastConfirmed(final ReadLastConfirmedCallback cb, final Object ctx) { boolean isClosed; long lastEntryId; synchronized (this) { From d09ad2211d7e27ce0df846f2fe108a45a09e9af9 Mon Sep 17 00:00:00 2001 From: Enrico Olivelli Date: Tue, 27 Oct 2020 09:16:12 +0100 Subject: [PATCH 2/6] fix client mock tests --- .../client/MockBookKeeperTestCase.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java index fd0a2ba0a92..9fe0b77e4df 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java @@ -238,6 +238,7 @@ public ByteBufAllocator getByteBufAllocator() { setupBookieWatcherForNewEnsemble(); setupBookieWatcherForEnsembleChange(); setupBookieClientReadEntry(); + setupBookieClientReadLac(); setupBookieClientAddEntry(); setupBookieClientForceLedger(); } @@ -524,6 +525,32 @@ protected void setupBookieClientReadEntry() { any(), anyInt(), any(), anyBoolean()); } + @SuppressWarnings("unchecked") + protected void setupBookieClientReadLac() { + final Stubber stub = doAnswer(invokation -> { + Object[] args = invokation.getArguments(); + BookieId bookieSocketAddress = (BookieId) args[0]; + long ledgerId = (Long) args[1]; + final BookkeeperInternalCallbacks.ReadLacCallback callback = + (BookkeeperInternalCallbacks.ReadLacCallback) args[2]; + Object ctx = args[3]; + long entryId = BookieProtocol.LAST_ADD_CONFIRMED; + // simply use "readEntry" with LAST_ADD_CONFIRMED to get current LAC + // there is nothing that writes ExplicitLAC within MockBookKeeperTestCase + bookieClient.readEntry(bookieSocketAddress, ledgerId, entryId, new BookkeeperInternalCallbacks.ReadEntryCallback() { + @Override + public void readEntryComplete(int rc, long ledgerId, long entryId, ByteBuf buffer, Object ctx) { + callback.readLacComplete(rc, ledgerId, null, buffer, ctx); + } + }, ctx, BookieProtocol.FLAG_NONE); + return null; + }); + + stub.when(bookieClient).readLac(any(BookieId.class), anyLong(), + any(BookkeeperInternalCallbacks.ReadLacCallback.class), + any()); + } + private byte[] extractEntryPayload(long ledgerId, long entryId, ByteBufList toSend) throws BKException.BKDigestMatchException { ByteBuf toSendCopy = Unpooled.copiedBuffer(toSend.toArray()); From c9d3ceef2f673df06d9c949a655ae35a0a216135 Mon Sep 17 00:00:00 2001 From: Enrico Olivelli Date: Tue, 27 Oct 2020 10:17:42 +0100 Subject: [PATCH 3/6] fix checkstyle --- .../org/apache/bookkeeper/client/MockBookKeeperTestCase.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java index 9fe0b77e4df..e85771d252e 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java @@ -537,7 +537,8 @@ protected void setupBookieClientReadLac() { long entryId = BookieProtocol.LAST_ADD_CONFIRMED; // simply use "readEntry" with LAST_ADD_CONFIRMED to get current LAC // there is nothing that writes ExplicitLAC within MockBookKeeperTestCase - bookieClient.readEntry(bookieSocketAddress, ledgerId, entryId, new BookkeeperInternalCallbacks.ReadEntryCallback() { + bookieClient.readEntry(bookieSocketAddress, ledgerId, entryId, + new BookkeeperInternalCallbacks.ReadEntryCallback() { @Override public void readEntryComplete(int rc, long ledgerId, long entryId, ByteBuf buffer, Object ctx) { callback.readLacComplete(rc, ledgerId, null, buffer, ctx); From 40435c7cb592d499c6e0e7b36fa2f1b6417b3c24 Mon Sep 17 00:00:00 2001 From: Enrico Olivelli Date: Tue, 27 Oct 2020 12:08:07 +0100 Subject: [PATCH 4/6] add test case --- .../ExplicitLACWithWriteHandleAPITest.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java new file mode 100644 index 00000000000..be3307ba4a8 --- /dev/null +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java @@ -0,0 +1,84 @@ +/** + * + * 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.api; + +import java.util.concurrent.TimeUnit; +import java.util.logging.Level; +import org.apache.bookkeeper.conf.ClientConfiguration; +import org.apache.bookkeeper.test.BookKeeperClusterTestCase; +import org.apache.bookkeeper.util.TestUtils; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Tests about ExplicitLAC and {@link Handle} API. + */ +public class ExplicitLACWithWriteHandleAPITest extends BookKeeperClusterTestCase { + + private static final Logger LOG = LoggerFactory.getLogger(ExplicitLACWithWriteHandleAPITest.class); + + public ExplicitLACWithWriteHandleAPITest() { + super(1); + } + + @Test + public void testUseExplicitLAC() throws Exception { + ClientConfiguration conf = new ClientConfiguration(baseClientConf); + conf.setExplictLacInterval(1000); + try ( BookKeeper bkc = BookKeeper + .newBuilder(conf) + .build();) { + try ( WriteHandle writer = bkc.newCreateLedgerOp() + .withAckQuorumSize(1) + .withEnsembleSize(1) + .withPassword(new byte[0]) + .withWriteQuorumSize(1) + .execute() + .get();) { + writer.append("foo".getBytes("utf-8")); + writer.append("foo".getBytes("utf-8")); + writer.append("foo".getBytes("utf-8")); + long expectedLastAddConfirmed = writer.append("foo".getBytes("utf-8")); + + // since BK 4.12.0 the reader automaticallly leverages ExplicitLAC + try (ReadHandle r = bkc.newOpenLedgerOp() + .withRecovery(false) + .withPassword(new byte[0]) + .withLedgerId(writer.getId()) + .execute() + .get()) { + TestUtils.assertEventuallyTrue("ExplicitLAC did not ork", () -> { + try { + long value = r.readLastAddConfirmed(); + LOG.info("current value "+value+" vs "+expectedLastAddConfirmed); + return value == expectedLastAddConfirmed; + } catch (Exception ex) { + throw new RuntimeException(ex); + } + }, 30, TimeUnit.SECONDS); + } + + } + + } + } +} From 7bdf65d089c5f435b818b6d97c8cb25345eae56f Mon Sep 17 00:00:00 2001 From: Enrico Olivelli Date: Tue, 27 Oct 2020 12:09:26 +0100 Subject: [PATCH 5/6] fix checkstyle --- .../client/api/ExplicitLACWithWriteHandleAPITest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java index be3307ba4a8..9431e284dda 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java @@ -21,7 +21,6 @@ package org.apache.bookkeeper.client.api; import java.util.concurrent.TimeUnit; -import java.util.logging.Level; import org.apache.bookkeeper.conf.ClientConfiguration; import org.apache.bookkeeper.test.BookKeeperClusterTestCase; import org.apache.bookkeeper.util.TestUtils; @@ -44,10 +43,10 @@ public ExplicitLACWithWriteHandleAPITest() { public void testUseExplicitLAC() throws Exception { ClientConfiguration conf = new ClientConfiguration(baseClientConf); conf.setExplictLacInterval(1000); - try ( BookKeeper bkc = BookKeeper + try (BookKeeper bkc = BookKeeper .newBuilder(conf) .build();) { - try ( WriteHandle writer = bkc.newCreateLedgerOp() + try (WriteHandle writer = bkc.newCreateLedgerOp() .withAckQuorumSize(1) .withEnsembleSize(1) .withPassword(new byte[0]) @@ -69,7 +68,7 @@ public void testUseExplicitLAC() throws Exception { TestUtils.assertEventuallyTrue("ExplicitLAC did not ork", () -> { try { long value = r.readLastAddConfirmed(); - LOG.info("current value "+value+" vs "+expectedLastAddConfirmed); + LOG.info("current value " + value + " vs " + expectedLastAddConfirmed); return value == expectedLastAddConfirmed; } catch (Exception ex) { throw new RuntimeException(ex); From 3770d840787a1481219e2c706bb35917db67977f Mon Sep 17 00:00:00 2001 From: Enrico Olivelli Date: Tue, 27 Oct 2020 12:59:21 +0100 Subject: [PATCH 6/6] fix typo --- .../client/api/ExplicitLACWithWriteHandleAPITest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java index 9431e284dda..d68fa5119c4 100644 --- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java +++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/ExplicitLACWithWriteHandleAPITest.java @@ -58,7 +58,7 @@ public void testUseExplicitLAC() throws Exception { writer.append("foo".getBytes("utf-8")); long expectedLastAddConfirmed = writer.append("foo".getBytes("utf-8")); - // since BK 4.12.0 the reader automaticallly leverages ExplicitLAC + // since BK 4.12.0 the reader automatically uses ExplicitLAC try (ReadHandle r = bkc.newOpenLedgerOp() .withRecovery(false) .withPassword(new byte[0])