Renkai commented on a change in pull request #9096:
URL: https://github.com/apache/pulsar/pull/9096#discussion_r558010345



##########
File path: 
tiered-storage/jcloud/src/main/java/org/apache/bookkeeper/mledger/offload/jcloud/impl/StreamingBlobStoreBackedReadHandleImpl.java
##########
@@ -0,0 +1,284 @@
+/**
+ * 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.offload.jcloud.impl;
+
+import com.google.common.base.Preconditions;
+import io.netty.buffer.ByteBuf;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ScheduledExecutorService;
+import lombok.val;
+import org.apache.bookkeeper.client.BKException;
+import org.apache.bookkeeper.client.api.LastConfirmedAndEntry;
+import org.apache.bookkeeper.client.api.LedgerEntries;
+import org.apache.bookkeeper.client.api.LedgerEntry;
+import org.apache.bookkeeper.client.api.LedgerMetadata;
+import org.apache.bookkeeper.client.api.ReadHandle;
+import org.apache.bookkeeper.client.impl.LedgerEntriesImpl;
+import org.apache.bookkeeper.client.impl.LedgerEntryImpl;
+import org.apache.bookkeeper.mledger.offload.jcloud.BackedInputStream;
+import org.apache.bookkeeper.mledger.offload.jcloud.StreamingOffloadIndexBlock;
+import 
org.apache.bookkeeper.mledger.offload.jcloud.StreamingOffloadIndexBlockBuilder;
+import 
org.apache.bookkeeper.mledger.offload.jcloud.impl.DataBlockUtils.VersionCheck;
+import org.apache.pulsar.common.allocator.PulsarByteBufAllocator;
+import org.jclouds.blobstore.BlobStore;
+import org.jclouds.blobstore.domain.Blob;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class StreamingBlobStoreBackedReadHandleImpl implements ReadHandle {
+    private static final Logger log = 
LoggerFactory.getLogger(StreamingBlobStoreBackedReadHandleImpl.class);
+
+    private final long ledgerId;
+    private final List<StreamingOffloadIndexBlock> indices;
+    private final List<BackedInputStream> inputStreams;
+    private final List<DataInputStream> dataStreams;
+    private final ExecutorService executor;
+
+    static class GroupedReader {
+        long ledgerId;
+        long firstEntry;
+        long lastEntry;
+        StreamingOffloadIndexBlock index;
+        BackedInputStream inputStream;
+        DataInputStream dataStream;
+
+        public GroupedReader(long ledgerId, long firstEntry, long lastEntry,
+                             StreamingOffloadIndexBlock index,
+                             BackedInputStream inputStream, DataInputStream 
dataStream) {
+            this.ledgerId = ledgerId;
+            this.firstEntry = firstEntry;
+            this.lastEntry = lastEntry;
+            this.index = index;
+            this.inputStream = inputStream;
+            this.dataStream = dataStream;
+        }
+    }
+
+    private StreamingBlobStoreBackedReadHandleImpl(long ledgerId, 
List<StreamingOffloadIndexBlock> indices,
+                                                   List<BackedInputStream> 
inputStreams,
+                                                   ExecutorService executor) {
+        this.ledgerId = ledgerId;
+        this.indices = indices;
+        this.inputStreams = inputStreams;
+        this.dataStreams = new LinkedList<>();
+        for (BackedInputStream inputStream : inputStreams) {
+            dataStreams.add(new DataInputStream(inputStream));
+        }
+        this.executor = executor;
+    }
+
+    @Override
+    public long getId() {
+        return ledgerId;
+    }
+
+    @Override
+    public LedgerMetadata getLedgerMetadata() {
+        return indices.get(0).getLedgerMetadata(ledgerId);
+    }
+
+    @Override
+    public CompletableFuture<Void> closeAsync() {
+        CompletableFuture<Void> promise = new CompletableFuture<>();
+        executor.submit(() -> {
+            try {
+                for (StreamingOffloadIndexBlock indexBlock : indices) {
+                    indexBlock.close();
+                }
+                for (BackedInputStream inputStream : inputStreams) {
+                    inputStream.close();
+                }
+                promise.complete(null);
+            } catch (IOException t) {
+                promise.completeExceptionally(t);
+            }
+        });
+        return promise;
+    }
+
+    @Override
+    public CompletableFuture<LedgerEntries> readAsync(long firstEntry, long 
lastEntry) {
+        log.debug("Ledger {}: reading {} - {}", getId(), firstEntry, 
lastEntry);

Review comment:
       @sijie I don't think put `log.debug` into `if (log.isDebugEnabled()) { 
... }` is a good practice today. It was originally designed to reduce string 
construction cost in log4j1, but today every log framework implemented slf4j 
provided a lazy formatter to prevent string construction when debug is not 
enabled. It can not only reduce boilerplate code and avoid check the log level 
twice.
   
   `if (log.isDebugEnabled()) { ... }` should only happened when we have extra 
computation for debug.
   
   See
   https://logging.apache.org/log4j/2.x/manual/api.html (Section: Substituting 
Parameters)
   https://logging.apache.org/log4j/log4j-2.3/performance.html




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to