-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[feat][ml] Support Bookkeeper Batch Read API #25280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
185c321
bfdbea1
2f4be0d
a917891
0dfc986
e01bfff
026dfc2
136b23b
43189f0
9fc9f22
503e71b
5dab16e
bd34e54
392a0ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * 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.mledger.impl.cache; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| import static com.google.common.base.Preconditions.checkNotNull; | ||
| import io.netty.util.Recycler; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import org.apache.bookkeeper.client.api.LedgerEntries; | ||
| import org.apache.bookkeeper.client.api.LedgerEntry; | ||
|
|
||
| public class CompositeLedgerEntriesImpl implements LedgerEntries { | ||
| private List<LedgerEntry> entries; | ||
| private List<LedgerEntries> ledgerEntries; | ||
| private final Recycler.Handle<CompositeLedgerEntriesImpl> recyclerHandle; | ||
|
|
||
| private CompositeLedgerEntriesImpl(Recycler.Handle<CompositeLedgerEntriesImpl> recyclerHandle) { | ||
| this.recyclerHandle = recyclerHandle; | ||
| } | ||
|
|
||
| private static final Recycler<CompositeLedgerEntriesImpl> RECYCLER = new Recycler<>() { | ||
| @Override | ||
| protected CompositeLedgerEntriesImpl newObject(Recycler.Handle<CompositeLedgerEntriesImpl> handle) { | ||
| return new CompositeLedgerEntriesImpl(handle); | ||
| } | ||
| }; | ||
|
|
||
| public static LedgerEntries create(List<LedgerEntry> entries, List<LedgerEntries> ledgerEntries) { | ||
| checkArgument(!entries.isEmpty(), "entries for create should not be empty."); | ||
| checkArgument(!ledgerEntries.isEmpty(), "ledgerEntries for create should not be empty."); | ||
| CompositeLedgerEntriesImpl instance = RECYCLER.get(); | ||
| instance.entries = entries; | ||
| instance.ledgerEntries = ledgerEntries; | ||
| return instance; | ||
| } | ||
|
|
||
| private void recycle() { | ||
| if (ledgerEntries == null) { | ||
| return; | ||
| } | ||
| ledgerEntries.forEach(LedgerEntries::close); | ||
| entries = null; | ||
| ledgerEntries = null; | ||
| recyclerHandle.recycle(this); | ||
| } | ||
|
|
||
| @Override | ||
| public LedgerEntry getEntry(long entryId) { | ||
| checkNotNull(entries, "entries has been recycled"); | ||
| long firstId = entries.get(0).getEntryId(); | ||
| long lastId = entries.get(entries.size() - 1).getEntryId(); | ||
| if (entryId < firstId || entryId > lastId) { | ||
| throw new IndexOutOfBoundsException("required index: " + entryId | ||
| + " is out of bounds: [ " + firstId + ", " + lastId + " ]."); | ||
| } | ||
| int index = (int) (entryId - firstId); | ||
| LedgerEntry entry = entries.get(index); | ||
| if (entry.getEntryId() != entryId) { | ||
| throw new IllegalStateException("Non-contiguous entries detected: expected entryId " | ||
| + entryId + " at index " + index + " but found entryId " + entry.getEntryId()); | ||
| } | ||
| return entry; | ||
| } | ||
|
|
||
| @Override | ||
| public Iterator<LedgerEntry> iterator() { | ||
| checkNotNull(entries, "entries has been recycled"); | ||
| return entries.iterator(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| recycle(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,8 +18,11 @@ | |
| */ | ||
| package org.apache.bookkeeper.mledger.impl.cache; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import org.apache.bookkeeper.client.api.LedgerEntries; | ||
| import org.apache.bookkeeper.client.api.LedgerEntry; | ||
| import org.apache.bookkeeper.client.api.ReadHandle; | ||
| import org.apache.bookkeeper.mledger.ManagedLedger; | ||
| import org.apache.bookkeeper.mledger.ManagedLedgerException; | ||
|
|
@@ -28,6 +31,11 @@ class ReadEntryUtils { | |
|
|
||
| static CompletableFuture<LedgerEntries> readAsync(ManagedLedger ml, ReadHandle handle, long firstEntry, | ||
| long lastEntry) { | ||
| return readAsync(ml, handle, firstEntry, lastEntry, false, 0); | ||
| } | ||
|
|
||
| static CompletableFuture<LedgerEntries> readAsync(ManagedLedger ml, ReadHandle handle, long firstEntry, | ||
| long lastEntry, boolean batchReadEnabled, int batchReadMaxSize) { | ||
| if (ml.getOptionalLedgerInfo(handle.getId()).isEmpty()) { | ||
| // The read handle comes from another managed ledger, in this case, we can only compare the entry range with | ||
| // the LAC of that read handle. Specifically, it happens when this method is called by a | ||
|
|
@@ -49,6 +57,66 @@ static CompletableFuture<LedgerEntries> readAsync(ManagedLedger ml, ReadHandle h | |
| return CompletableFuture.failedFuture(new ManagedLedgerException("LastConfirmedEntry is " | ||
| + lastConfirmedEntry + " when reading entry " + lastEntry)); | ||
| } | ||
|
|
||
| int numberOfEntries = (int) (lastEntry - firstEntry + 1); | ||
|
|
||
| // Use batch read for multiple entries when enabled. | ||
| if (batchReadEnabled && numberOfEntries > 1 && batchReadMaxSize > 0) { | ||
| return batchReadUnconfirmed(handle, firstEntry, numberOfEntries, batchReadMaxSize); | ||
| } | ||
| return handle.readUnconfirmedAsync(firstEntry, lastEntry); | ||
| } | ||
|
|
||
| private static CompletableFuture<LedgerEntries> batchReadUnconfirmed( | ||
| ReadHandle handle, long firstEntry, int maxCount, int maxSize) { | ||
| CompletableFuture<LedgerEntries> future = new CompletableFuture<>(); | ||
| List<LedgerEntry> receivedEntries = new ArrayList<>(maxCount); | ||
| List<LedgerEntries> ledgerEntries = new ArrayList<>(4); | ||
| doBatchRead(handle, firstEntry, maxCount, maxSize, receivedEntries, ledgerEntries, future); | ||
| return future; | ||
| } | ||
|
|
||
| private static void doBatchRead(ReadHandle handle, long firstEntry, int maxCount, int maxSize, | ||
| List<LedgerEntry> receivedEntries, List<LedgerEntries> ledgerEntries, | ||
| CompletableFuture<LedgerEntries> future) { | ||
| handle.batchReadUnconfirmedAsync(firstEntry, maxCount - receivedEntries.size(), maxSize) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| .whenComplete((entries, throwable) -> { | ||
| if (throwable != null) { | ||
| onBatchReadComplete(handle, firstEntry, maxCount, receivedEntries, ledgerEntries, future, | ||
| throwable); | ||
| return; | ||
| } | ||
| long lastReceivedEntry = -1; | ||
| int prevReceivedCount = receivedEntries.size(); | ||
| for (LedgerEntry entry : entries) { | ||
| receivedEntries.add(entry); | ||
| lastReceivedEntry = entry.getEntryId(); | ||
| } | ||
| ledgerEntries.add(entries); | ||
| if (receivedEntries.size() >= maxCount || prevReceivedCount == receivedEntries.size()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can complete successfully with fewer entries than the caller requested. BK batch read may legally return a partial prefix, but the previous |
||
| onBatchReadComplete(handle, firstEntry, maxCount, receivedEntries, ledgerEntries, future, null); | ||
| return; | ||
| } | ||
| doBatchRead(handle, lastReceivedEntry + 1, maxCount, maxSize, | ||
| receivedEntries, ledgerEntries, future); | ||
| }); | ||
| } | ||
|
|
||
| private static void onBatchReadComplete(ReadHandle handle, long firstEntry, int maxCount, | ||
| List<LedgerEntry> receivedEntries, List<LedgerEntries> ledgerEntries, | ||
| CompletableFuture<LedgerEntries> future, Throwable error) { | ||
| if (error != null) { | ||
| ledgerEntries.forEach(LedgerEntries::close); | ||
| future.completeExceptionally(error); | ||
| return; | ||
| } | ||
| if (receivedEntries.isEmpty()) { | ||
| ledgerEntries.forEach(LedgerEntries::close); | ||
| future.completeExceptionally(new ManagedLedgerException( | ||
| "Batch read returned no entries for ledger " + handle.getId() | ||
| + " starting from entry " + firstEntry)); | ||
| return; | ||
| } | ||
| future.complete(CompositeLedgerEntriesImpl.create(receivedEntries, ledgerEntries)); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment says
batchReadMaxSizeBytes <= 0uses the Netty max frame size, butReadEntryUtilsonly enables batch read whenbatchReadMaxSize > 0. Please align the implementation and the config contract, either by passing non-positive values through to BK so it can apply its fallback, or by updating this documentation.