sijie commented on a change in pull request #4403: File system offload URL: https://github.com/apache/pulsar/pull/4403#discussion_r293301102
########## File path: tiered-storage/file-system/src/main/java/org/apache/bookkeeper/mledger/offload/filesystem/impl/FileSystemManagedLedgerOffloader.java ########## @@ -0,0 +1,241 @@ +/** + * 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.filesystem.impl; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableMap; +import com.google.protobuf.ByteString; +import org.apache.bookkeeper.client.BookKeeper; +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.common.util.OrderedScheduler; +import org.apache.bookkeeper.mledger.LedgerOffloader; +import org.apache.bookkeeper.mledger.offload.filesystem.FileSystemLedgerOffloaderFactory; +import org.apache.bookkeeper.mledger.offload.filesystem.TieredStorageConfigurationData; +import org.apache.bookkeeper.net.BookieSocketAddress; +import org.apache.bookkeeper.proto.DataFormats; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.io.BytesWritable; +import org.apache.hadoop.io.IOUtils; +import org.apache.hadoop.io.LongWritable; +import org.apache.hadoop.io.MapFile; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.stream.Collectors; +import java.util.zip.Checksum; + + +public class FileSystemManagedLedgerOffloader implements LedgerOffloader { + + private static final Logger log = LoggerFactory.getLogger(FileSystemManagedLedgerOffloader.class); + private static final String STORAGE_BASE_PATH = "storageBasePath"; + private static final String[] DRIVER_NAMES = {"filesystem"}; + private final Configuration configuration; + private final String driverName; + private final String storageBasePath; + private final FileSystem fileSystem; + private OrderedScheduler scheduler; + private static final long ENTRIES_PER_READ = 100; + public static boolean driverSupported(String driver) { + return Arrays.stream(DRIVER_NAMES).anyMatch(d -> d.equalsIgnoreCase(driver)); + } + @Override + public String getOffloadDriverName() { + return driverName; + } + + public static FileSystemManagedLedgerOffloader create(TieredStorageConfigurationData conf, OrderedScheduler scheduler) throws IOException { + return new FileSystemManagedLedgerOffloader(conf, scheduler); + } + + private FileSystemManagedLedgerOffloader(TieredStorageConfigurationData conf, OrderedScheduler scheduler) throws IOException { + this.configuration = new Configuration(); + if (conf.getFileSystemProfilePath() != null) { + String[] paths = conf.getFileSystemProfilePath().split(","); + for (int i =0 ; i < paths.length; i++) { + configuration.addResource(new Path(paths[i])); + } + } + if (configuration.get("fs.hdfs.impl") == null) { + this.configuration.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem"); + } + this.configuration.setClassLoader(FileSystemLedgerOffloaderFactory.class.getClassLoader()); + this.driverName = conf.getManagedLedgerOffloadDriver(); + this.storageBasePath = configuration.get("hadoop.tmp.dir"); + this.scheduler = scheduler; + this.fileSystem = FileSystem.get(configuration); + } + @VisibleForTesting + public FileSystemManagedLedgerOffloader(TieredStorageConfigurationData conf, OrderedScheduler scheduler, String testHDFSPath, String baseDir) throws IOException { + this.configuration = new Configuration(); + this.configuration.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem"); + this.configuration.set("fs.defaultFS", testHDFSPath); + this.configuration.setClassLoader(FileSystemLedgerOffloaderFactory.class.getClassLoader()); + this.driverName = conf.getManagedLedgerOffloadDriver(); + this.configuration.set("hadoop.tmp.dir", baseDir); + this.storageBasePath = baseDir; + this.scheduler = scheduler; + this.fileSystem = FileSystem.get(configuration); + + } + + @Override + public Map<String, String> getOffloadDriverMetadata() { + return ImmutableMap.of( + STORAGE_BASE_PATH, storageBasePath + ); + } + + @Override + public CompletableFuture<Void> offload(ReadHandle readHandle, UUID uid, Map<String, String> extraMetadata) { + CompletableFuture<Void> promise = new CompletableFuture<>(); + scheduler.chooseThread(readHandle.getId()).submit(() -> { + if (readHandle.getLength() == 0 || !readHandle.isClosed() || readHandle.getLastAddConfirmed() < 0) { + promise.completeExceptionally( + new IllegalArgumentException("An empty or open ledger should never be offloaded")); + return; + } + long ledgerId = readHandle.getId(); + String storagePath = getStoragePath(storageBasePath, extraMetadata.get("ManagedLedgerName")); + String dataFilePath = getDataFilePath(storagePath, ledgerId, uid); + LongWritable key = new LongWritable(); + BytesWritable value = new BytesWritable(); + try { + MapFile.Writer dataWriter = new MapFile.Writer(configuration, + new Path(dataFilePath), + MapFile.Writer.keyClass(LongWritable.class), + MapFile.Writer.valueClass(BytesWritable.class)); + key.set(-1); + byte[] ledgerMetadata = buildLedgerMetadataFormat(readHandle.getLedgerMetadata()); + value.set(buildLedgerMetadataFormat(readHandle.getLedgerMetadata()), 0, ledgerMetadata.length); + dataWriter.append(key, value); + long haveOffloadEntryNumber = 0; + do { + long end = Math.min(haveOffloadEntryNumber + ENTRIES_PER_READ - 1, readHandle.getLastAddConfirmed()); + LedgerEntries ledgerEntriesOnce = readHandle.readAsync(haveOffloadEntryNumber, end).get(); + log.debug("read ledger entries. start: {}, end: {}", haveOffloadEntryNumber, end); + Iterator<LedgerEntry> iterator = ledgerEntriesOnce.iterator(); + while (iterator.hasNext()) { + LedgerEntry entry = iterator.next(); + long entryId = entry.getEntryId(); + key.set(entryId); + value.set(entry.getEntryBytes(), 0, entry.getEntryBytes().length); + dataWriter.append(key, value); + haveOffloadEntryNumber ++; + } + } while (haveOffloadEntryNumber - 1 < readHandle.getLastAddConfirmed()); + if (haveOffloadEntryNumber - 1 != readHandle.getLastAddConfirmed()) { + log.error("The expected number of entries in offload does not match the actual number"); + throw new IOException(); + } + IOUtils.closeStream(dataWriter); + promise.complete(null); + } catch (InterruptedException | ExecutionException | IOException e) { + log.error("Exception when get CompletableFuture<LedgerEntries>. ", e); + if (e instanceof InterruptedException) { + Thread.currentThread().interrupt(); + } + promise.completeExceptionally(e); + return; + } + }); + return promise; + } + + @Override + public CompletableFuture<ReadHandle> readOffloaded(long ledgerId, UUID uuid, Map<String, String> offloadDriverMetadata) { + + CompletableFuture<ReadHandle> promise = new CompletableFuture<>(); + String storagePath = getStoragePath(storageBasePath, offloadDriverMetadata.get("ManagedLedgerName")); + String dataFilePath = getDataFilePath(storagePath, ledgerId, uuid); + scheduler.chooseThread(ledgerId).submit(() -> { + try { + MapFile.Reader reader = new MapFile.Reader(new Path(dataFilePath), + configuration); + promise.complete(FileStoreBackedReadHandleImpl.open(scheduler.chooseThread(ledgerId), reader, ledgerId)); Review comment: It seems that we are sharing a scheduler for both reading and writing. If a ledger is offloading, then it will be blocked the reading operations. Can we use different executors for reading and writing? ---------------------------------------------------------------- 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] With regards, Apache Git Services
