|
| 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.impl; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.Comparator; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.Iterator; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Objects; |
| 29 | +import org.apache.bookkeeper.common.annotation.InterfaceAudience; |
| 30 | +import org.apache.bookkeeper.mledger.ManagedLedger; |
| 31 | +import org.apache.bookkeeper.mledger.Position; |
| 32 | +import org.apache.bookkeeper.mledger.proto.ManagedLedgerInfo.LedgerInfo; |
| 33 | + |
| 34 | +/** Estimates subscription backlog sizes together using one scan of their ledger metadata prefix. */ |
| 35 | +@InterfaceAudience.Private |
| 36 | +public final class ManagedLedgerBacklogSizeEstimator { |
| 37 | + private ManagedLedgerBacklogSizeEstimator() { |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Estimate a batch of positions captured before calling this method. |
| 42 | + * The results have the same approximate semantics as {@link ManagedLedger#getEstimatedBacklogSize(Position)}. |
| 43 | + * Results are keyed by their input positions; duplicate positions share one result. |
| 44 | + * An empty position collection produces an empty result. |
| 45 | + * |
| 46 | + * @throws NullPointerException if the ledger, position collection, or any position is null |
| 47 | + */ |
| 48 | + public static Map<Position, Long> estimate(ManagedLedger ledger, Collection<Position> positions) { |
| 49 | + List<Position> sortedPositions = new ArrayList<>(positions.size()); |
| 50 | + for (Position position : positions) { |
| 51 | + sortedPositions.add(Objects.requireNonNull(position, "position")); |
| 52 | + } |
| 53 | + Map<Position, Long> estimates = new HashMap<>(); |
| 54 | + // Subclasses can change ledger metadata independently of this implementation's synchronization. |
| 55 | + if (ledger.getClass() != ManagedLedgerImpl.class || sortedPositions.size() <= 1) { |
| 56 | + for (Position position : sortedPositions) { |
| 57 | + estimates.put(position, ledger.getEstimatedBacklogSize(position)); |
| 58 | + } |
| 59 | + return estimates; |
| 60 | + } |
| 61 | + |
| 62 | + sortedPositions.sort(Comparator.comparingLong(Position::getLedgerId)); |
| 63 | + int positionCount = sortedPositions.size(); |
| 64 | + long[] ledgerIds = new long[positionCount]; |
| 65 | + int[] positionLedgerIndexes = new int[positionCount]; |
| 66 | + int queriedLedgerCount = 0; |
| 67 | + for (int i = 0; i < positionCount; i++) { |
| 68 | + long ledgerId = sortedPositions.get(i).getLedgerId(); |
| 69 | + if (queriedLedgerCount == 0 || ledgerIds[queriedLedgerCount - 1] != ledgerId) { |
| 70 | + ledgerIds[queriedLedgerCount++] = ledgerId; |
| 71 | + } |
| 72 | + positionLedgerIndexes[i] = queriedLedgerCount - 1; |
| 73 | + } |
| 74 | + // Allocate by query count before acquiring the writer monitor. Only the strict prefix sum |
| 75 | + // and the queried ledger's counters are needed, not a copy of every ledger in that prefix. |
| 76 | + long[] prefixSizes = new long[queriedLedgerCount]; |
| 77 | + long[] ledgerSizes = new long[queriedLedgerCount]; |
| 78 | + long[] ledgerEntries = new long[queriedLedgerCount]; |
| 79 | + boolean[] ledgerPresent = new boolean[queriedLedgerCount]; |
| 80 | + |
| 81 | + ManagedLedgerImpl impl = (ManagedLedgerImpl) ledger; |
| 82 | + final long totalSize; |
| 83 | + final long currentLedgerId; |
| 84 | + final boolean hasCurrentLedger; |
| 85 | + final long currentSize; |
| 86 | + final long currentEntries; |
| 87 | + synchronized (impl) { |
| 88 | + // LedgerInfo IDs match the map keys. Iterating values avoids allocating an entry wrapper |
| 89 | + // per ledger in ConcurrentSkipListMap's submap iterator. |
| 90 | + Iterator<LedgerInfo> metadata = impl.ledgers |
| 91 | + .headMap(ledgerIds[queriedLedgerCount - 1], true).values().iterator(); |
| 92 | + LedgerInfo info = metadata.hasNext() ? metadata.next() : null; |
| 93 | + long prefixSize = 0; |
| 94 | + for (int i = 0; i < queriedLedgerCount; i++) { |
| 95 | + while (info != null && info.getLedgerId() < ledgerIds[i]) { |
| 96 | + prefixSize += info.getSize(); |
| 97 | + info = metadata.hasNext() ? metadata.next() : null; |
| 98 | + } |
| 99 | + prefixSizes[i] = prefixSize; |
| 100 | + if (info != null && info.getLedgerId() == ledgerIds[i]) { |
| 101 | + ledgerPresent[i] = true; |
| 102 | + ledgerSizes[i] = info.getSize(); |
| 103 | + ledgerEntries[i] = info.getEntries(); |
| 104 | + } |
| 105 | + } |
| 106 | + totalSize = impl.getTotalSize(); |
| 107 | + hasCurrentLedger = impl.currentLedger != null; |
| 108 | + currentLedgerId = hasCurrentLedger ? impl.currentLedger.getId() : 0; |
| 109 | + currentSize = impl.currentLedgerSize; |
| 110 | + currentEntries = impl.currentLedgerEntries; |
| 111 | + } |
| 112 | + |
| 113 | + for (int i = 0; i < positionCount; i++) { |
| 114 | + Position position = sortedPositions.get(i); |
| 115 | + int ledgerIndex = positionLedgerIndexes[i]; |
| 116 | + long size = totalSize - prefixSizes[ledgerIndex]; |
| 117 | + if (ledgerPresent[ledgerIndex]) { |
| 118 | + if (hasCurrentLedger && ledgerIds[ledgerIndex] == currentLedgerId) { |
| 119 | + size -= ManagedLedgerImpl.consumedLedgerSize(currentSize, currentEntries, position.getEntryId()); |
| 120 | + } else { |
| 121 | + size -= ManagedLedgerImpl.consumedLedgerSize( |
| 122 | + ledgerSizes[ledgerIndex], ledgerEntries[ledgerIndex], position.getEntryId()); |
| 123 | + } |
| 124 | + } |
| 125 | + estimates.put(position, size); |
| 126 | + } |
| 127 | + return estimates; |
| 128 | + } |
| 129 | +} |