|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.bookkeeper.mledger; |
| 20 | + |
| 21 | +import static org.apache.bookkeeper.mledger.util.ManagedLedgerUtils.readEntries; |
| 22 | +import java.util.Optional; |
| 23 | +import java.util.concurrent.CompletableFuture; |
| 24 | +import java.util.concurrent.Executor; |
| 25 | +import lombok.Getter; |
| 26 | +import lombok.RequiredArgsConstructor; |
| 27 | +import lombok.extern.slf4j.Slf4j; |
| 28 | +import org.jspecify.annotations.Nullable; |
| 29 | + |
| 30 | +/** |
| 31 | + * The task to perform replay on the whole managed ledger from a given position. |
| 32 | + */ |
| 33 | +@RequiredArgsConstructor |
| 34 | +@Slf4j |
| 35 | +public class ManagedLedgerReplayTask { |
| 36 | + |
| 37 | + private final String name; |
| 38 | + private final Executor executor; // run user-provided processor on entry |
| 39 | + private final int maxEntriesPerRead; |
| 40 | + @Getter // NOTE: the getter must be called in the callback of `replay` for thread safety |
| 41 | + private int numEntriesProcessed = 0; |
| 42 | + |
| 43 | + /** |
| 44 | + * This method will read entries from `cursor` until the last confirmed entry. `processor` will be applied on each |
| 45 | + * entry. |
| 46 | + * |
| 47 | + * @param cursor the managed cursor to read entries |
| 48 | + * @param processor the user-provided processor that accepts the position and data buffer of the entry |
| 49 | + * @return the future of the optional last position processed: |
| 50 | + * 1. If there is no more entry to read, return an empty optional. |
| 51 | + * 2. Otherwise, if no exception was thrown, it will always be the position of the last entry. |
| 52 | + * 3. If any exception is thrown from {@link EntryProcessor#process}, it will be the position of the last |
| 53 | + * entry that has been processed successfully. |
| 54 | + * 4. If an unexpected exception is thrown, the future will complete exceptionally. |
| 55 | + * @apiNote The implementation of `processor` should not call `release()` on the buffer because this method will |
| 56 | + * eventually release the buffer after it's processed. |
| 57 | + */ |
| 58 | + public CompletableFuture<Optional<Position>> replay(ManagedCursor cursor, EntryProcessor processor) { |
| 59 | + try { |
| 60 | + numEntriesProcessed = 0; |
| 61 | + cursor.setAlwaysInactive(); // don't cache the replayed entries |
| 62 | + if (!cursor.hasMoreEntries()) { |
| 63 | + return CompletableFuture.completedFuture(Optional.empty()); |
| 64 | + } |
| 65 | + return readAndProcess(cursor, null, processor); |
| 66 | + } catch (Throwable throwable) { |
| 67 | + return CompletableFuture.failedFuture(throwable); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private CompletableFuture<Optional<Position>> readAndProcess( |
| 72 | + ManagedCursor cursor, @Nullable Position lastProcessedPosition, EntryProcessor processor) { |
| 73 | + return readEntries(cursor, maxEntriesPerRead, PositionFactory.LATEST).thenComposeAsync(entries -> { |
| 74 | + try { |
| 75 | + Position processedPosition = lastProcessedPosition; |
| 76 | + for (final var entry : entries) { |
| 77 | + final var position = entry.getPosition(); |
| 78 | + final var buffer = entry.getDataBuffer(); |
| 79 | + // Pass a duplicated buffer to `processor` in case the buffer is retained and stored somewhere else |
| 80 | + // and then process all buffers in batch. |
| 81 | + try { |
| 82 | + processor.process(position, buffer); |
| 83 | + } catch (Throwable throwable) { |
| 84 | + log.error("[{}] Failed to process entry {}", name, position, throwable); |
| 85 | + return CompletableFuture.completedFuture(Optional.ofNullable(processedPosition)); |
| 86 | + } |
| 87 | + // It does not need to be atomic because the update happens before the future completes |
| 88 | + numEntriesProcessed++; |
| 89 | + processedPosition = position; |
| 90 | + } |
| 91 | + if (cursor.hasMoreEntries()) { |
| 92 | + return readAndProcess(cursor, processedPosition, processor); |
| 93 | + } else { |
| 94 | + return CompletableFuture.completedFuture(Optional.ofNullable(processedPosition)); |
| 95 | + } |
| 96 | + } finally { |
| 97 | + entries.forEach(Entry::release); |
| 98 | + } |
| 99 | + }, executor); |
| 100 | + } |
| 101 | +} |
0 commit comments