You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/06/19 17:46:11 UTC

[GitHub] [pulsar] asafm commented on a diff in pull request #15955: PIP-174: New managed ledger entry cache implementation

asafm commented on code in PR #15955:
URL: https://github.com/apache/pulsar/pull/15955#discussion_r901138209


##########
managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/cache/SharedCacheSegment.java:
##########
@@ -0,0 +1,94 @@
+/**
+ * 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 io.netty.buffer.ByteBuf;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.pulsar.common.allocator.PulsarByteBufAllocator;
+import org.apache.pulsar.common.util.collections.ConcurrentLongLongPairHashMap;
+
+class SharedCacheSegment implements AutoCloseable {
+
+    private final ByteBuf cacheBuffer;
+    private final AtomicInteger currentOffset = new AtomicInteger();
+    private final ConcurrentLongLongPairHashMap index;
+    private final int segmentSize;
+
+    private static final int ALIGN_64_MASK = ~(64 - 1);
+
+    SharedCacheSegment(int segmentSize) {
+        this.segmentSize = segmentSize;
+        this.cacheBuffer = PulsarByteBufAllocator.DEFAULT.buffer(segmentSize, segmentSize);
+        this.index = ConcurrentLongLongPairHashMap.newBuilder()
+                // We are going to often clear() the map, with the expectation that it's going to get filled again
+                // immediately after. In these conditions it does not make sense to shrink it each time.
+                .autoShrink(false)
+                .concurrencyLevel(Runtime.getRuntime().availableProcessors() * 2)
+                .build();
+    }
+
+    boolean insert(long ledgerId, long entryId, ByteBuf entry) {
+        int entrySize = entry.readableBytes();
+        int alignedSize = align64(entrySize);
+        int offset = currentOffset.getAndAdd(alignedSize);
+
+        if (offset + entrySize > segmentSize) {
+            // The segment is full
+            return false;
+        } else {
+            // Copy entry into read cache segment
+            cacheBuffer.setBytes(offset, entry, entry.readerIndex(), entry.readableBytes());
+            long value = offset << 32 | entrySize;
+            index.put(ledgerId, entryId, value, 0);
+            return true;
+        }
+    }
+
+    public ByteBuf get(long ledgerId, long entryId) {
+        long value = index.getFirstValue(ledgerId, entryId);
+        if (value >= 0) {
+            int offset = (int) (value >> 32);
+            int entryLen = (int) value;
+
+            ByteBuf entry = PulsarByteBufAllocator.DEFAULT.buffer(entryLen, entryLen);

Review Comment:
   Since it's draft PR, I'm writing here an idea that crossed my mind.
   On each get, we pay the penalty of creating ByteBuf, both heap object and direct memory allocation, then copying.
   If would return a ByteBuf which is a linked ByteBuf (view) to the original ByteBuf?
   It's still valid as long as we don't call clear().
   Perhaps we can maintain an ever-increasing version number, which upon clear we increase it.
   We can return a CachedByteBuf, which has a link to the cache and version it was cut from. It version got bigger, it means it's invalidated and can't be used anymore. 
   CachedByteBuf also be pooled if needed, since they are just long and a ByteBuf.
   Just an idea
   



-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org