|
76 | 76 | import org.apache.bookkeeper.client.BKException.Code; |
77 | 77 | import org.apache.bookkeeper.client.BookKeeper; |
78 | 78 | import org.apache.bookkeeper.client.BookKeeper.DigestType; |
| 79 | +import org.apache.bookkeeper.client.BookKeeperClientConfigAccessor; |
79 | 80 | import org.apache.bookkeeper.client.LedgerHandle; |
80 | 81 | import org.apache.bookkeeper.client.api.LedgerEntry; |
81 | 82 | import org.apache.bookkeeper.client.api.LedgerMetadata; |
@@ -161,6 +162,8 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback { |
161 | 162 | private static final Logger slog = Logger.get(ManagedLedgerImpl.class); |
162 | 163 |
|
163 | 164 | protected final BookKeeper bookKeeper; |
| 165 | + /** Whether the BookKeeper client can batch read: the v2 wire protocol with batch reads enabled. */ |
| 166 | + private final boolean batchReadSupported; |
164 | 167 | protected final String name; |
165 | 168 | protected final Logger log; |
166 | 169 |
|
@@ -386,6 +389,7 @@ public ManagedLedgerImpl(ManagedLedgerFactoryImpl factory, BookKeeper bookKeeper |
386 | 389 | } |
387 | 390 | this.factory = factory; |
388 | 391 | this.bookKeeper = bookKeeper; |
| 392 | + this.batchReadSupported = BookKeeperClientConfigAccessor.supportsBatchRead(bookKeeper); |
389 | 393 | this.config = config; |
390 | 394 | this.store = store; |
391 | 395 | this.name = name; |
@@ -1934,6 +1938,14 @@ synchronized void addEntryFailedDueToConcurrentlyModified(final LedgerHandle cur |
1934 | 1938 | + " position when the ledger was concurrently modified" |
1935 | 1939 | + " (the ledger may be closed by auto-replication)"); |
1936 | 1940 | ledgerClosed(currentLedger, lh.getLastAddConfirmed()); |
| 1941 | + // Close the abandoned write handle, or it leaks with its periodic explicit-LAC flush task. |
| 1942 | + currentLedger.asyncClose((closeRc, closedLedger, closeCtx) -> { |
| 1943 | + if (closeRc != Code.OK) { |
| 1944 | + log.debug().attr("ledgerId", currentLedger.getId()) |
| 1945 | + .attr("status", BKException.getMessage(closeRc)) |
| 1946 | + .log("Error when closing ledger after it was concurrently modified"); |
| 1947 | + } |
| 1948 | + }, null); |
1937 | 1949 | } else { |
1938 | 1950 | log.error().attr("ledgerId", currentLedger.getId()) |
1939 | 1951 | .attr("lastAddConfirmed", currentLedger.getLastAddConfirmed()) |
@@ -2492,12 +2504,14 @@ protected void asyncReadEntry(ReadHandle ledger, long firstEntry, long lastEntry |
2492 | 2504 | if (config.getReadEntryTimeoutSeconds() > 0) { |
2493 | 2505 | ReadEntryCallbackWrapper readCallback = ReadEntryCallbackWrapper.create(this, ledger.getId(), firstEntry, |
2494 | 2506 | opReadEntry, ctx, timeoutAtNanos(config.getReadEntryTimeoutSeconds())); |
2495 | | - entryCache.asyncReadEntry(ledger, firstEntry, lastEntry, expectedReadCount, readCallback, ctx); |
| 2507 | + entryCache.asyncReadEntry(ledger, firstEntry, lastEntry, opReadEntry.maxSizeBytes, expectedReadCount, |
| 2508 | + readCallback, ctx); |
2496 | 2509 | if (readCallback.registerTimeout()) { |
2497 | 2510 | factory.getReadEntryTimeoutTracker().add(readCallback); |
2498 | 2511 | } |
2499 | 2512 | } else { |
2500 | | - entryCache.asyncReadEntry(ledger, firstEntry, lastEntry, expectedReadCount, opReadEntry, ctx); |
| 2513 | + entryCache.asyncReadEntry(ledger, firstEntry, lastEntry, opReadEntry.maxSizeBytes, expectedReadCount, |
| 2514 | + opReadEntry, ctx); |
2501 | 2515 | } |
2502 | 2516 | } |
2503 | 2517 |
|
@@ -4562,6 +4576,14 @@ public ManagedLedgerConfig getConfig() { |
4562 | 4576 | return config; |
4563 | 4577 | } |
4564 | 4578 |
|
| 4579 | + /** |
| 4580 | + * Whether storage reads use the BookKeeper batch read API: it must be enabled in the config and supported by |
| 4581 | + * the BookKeeper client (v2 wire protocol with batch reads enabled in its configuration). |
| 4582 | + */ |
| 4583 | + public boolean isBatchReadEnabled() { |
| 4584 | + return batchReadSupported && config.isBatchReadEnabled(); |
| 4585 | + } |
| 4586 | + |
4565 | 4587 | @Override |
4566 | 4588 | public void setConfig(ManagedLedgerConfig config) { |
4567 | 4589 | this.config = config; |
|
0 commit comments