lhotari commented on code in PR #26127: URL: https://github.com/apache/pulsar/pull/26127#discussion_r3505582646
########## managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/PositionRangeSet.java: ########## @@ -0,0 +1,518 @@ +/* + * 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; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.BoundType; +import com.google.common.collect.Range; +import io.github.merlimat.slog.Logger; +import java.util.ArrayList; +import java.util.BitSet; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Objects; +import java.util.TreeMap; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.bookkeeper.mledger.Position; +import org.apache.bookkeeper.mledger.PositionFactory; +import org.apache.commons.lang3.mutable.MutableInt; +import org.apache.pulsar.common.util.collections.LongPairRangeSet; +import org.roaringbitmap.RoaringBitSet; + +/** + * Tracks deleted-message positions as ranges of {@link Position}s. + * + * <p>The implementation stores positions in a two-level structure: + * the ledger id is used as the map key, and the corresponding entry ids are stored in a + * {@link RoaringBitSet}. Bit {@code n} in the bitmap of ledger {@code L} represents the position + * {@code (L, n)}. + * + * <p>This implementation replaces the previous {@code RangeSetWrapper<Position>} backed by + * {@code OpenLongPairRangeSet}. Keeping the storage layout ledger-oriented avoids the overhead of + * creating and comparing {@link Position} range objects while preserving the same + * {@link LongPairRangeSet} semantics. + * + * <h2>Thread Safety</h2> + * + * <p>This class is not thread-safe. All methods require the caller to provide external + * synchronization. In normal usage, callers must hold the owning {@link ManagedCursorImpl}'s + * cursor lock before accessing this class. + * + * <p>The class intentionally does not maintain internal locking. The cursor lock is the single + * synchronization boundary for both this structure and related cursor state. + * + * <h2>Persistence Compatibility</h2> + * + * <p>The persisted representation remains compatible with the existing format: + * {@link #toRanges(int)} and {@link #build(Map)} use the {@link BitSet#toLongArray()} / + * {@link BitSet#valueOf(long[])} contract. + * + * <p>The internal {@link RoaringBitSet} representation is an implementation detail and is not used + * for serialization, avoiding any wire-format change. + * + * <p>Entry ids are stored as bitmap indexes and therefore use {@code int} values. This is safe + * because {@code managedLedgerMaxEntriesPerLedger} is an {@code int}, so valid entry ids are within + * {@code [0, Integer.MAX_VALUE]}. + */ +class PositionRangeSet implements LongPairRangeSet<Position> { + + private static final Logger log = Logger.get(PositionRangeSet.class); + + private static final long EARLIEST_KEY = -1L; + private static final long EARLIEST_VALUE = -1L; + private static final long LATEST_KEY = Long.MAX_VALUE; + private static final long LATEST_VALUE = Long.MAX_VALUE; + + private final NavigableMap<Long, RoaringBitSet> rangeBitSetMap = new TreeMap<>(); + private final LongPairConsumer<Position> consumer; + private final boolean enableMultiEntry; + + private RoaringBitSet dirtyLedgers = new RoaringBitSet(); Review Comment: It's pretty surprising that current solution in `master` switches from `RoaringBitSet` to ordinary `BitSet`. This happens on all code paths when the cursor is restored and `managedLedgerUnackedRangesOpenCacheSetEnabled=true` (default). https://github.com/apache/pulsar/blob/19db575e2d9246dcbffd6c14d0c17d284de3ab10/managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/ManagedCursorImpl.java#L690-L696 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
