sijie commented on a change in pull request #704: BOOKKEEPER-1040: Use separate log for compaction and add transaction support URL: https://github.com/apache/bookkeeper/pull/704#discussion_r149521739
########## File path: bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/TransactionalEntryLogCompactor.java ########## @@ -0,0 +1,387 @@ +/* + * + * 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.bookie; + +import java.io.File; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +import com.google.common.util.concurrent.RateLimiter; + +import org.apache.bookkeeper.bookie.EntryLogger.EntryLogScanner; +import org.apache.bookkeeper.util.HardLink; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class is used for compaction. Compaction is done in several transactional phases. + * Phase 1: Scan old entry log and compact entries to a new .compacting log file. + * Phase 2: Flush .compacting log to disk and it becomes .compacted log file when this completes. + * Phase 3: Flush ledger cache and .compacted file becomes .log file when this completes. Remove old + * entry log file afterwards. + */ +public class TransactionalEntryLogCompactor extends AbstractLogCompactor { + + private static final Logger LOG = LoggerFactory.getLogger(TransactionalEntryLogCompactor.class); + + final EntryLogger entryLogger; + final CompactableLedgerStorage ledgerStorage; + final List<EntryLocation> offsets = new ArrayList<>(); + + // compaction log file suffix + static final String COMPACTING_SUFFIX = ".log.compacting"; + // flushed compaction log file suffix + static final String COMPACTED_SUFFIX = ".compacted"; + + public TransactionalEntryLogCompactor(GarbageCollectorThread gcThread) { + super(gcThread); + this.entryLogger = gcThread.getEntryLogger(); + this.ledgerStorage = gcThread.getLedgerStorage(); + } + + /** + * Delete all previously incomplete compacting logs and recover the index for compacted logs. + */ + public void cleanUpAndRecover() { + // clean up compacting logs and recover index for already compacted logs + List<File> ledgerDirs = entryLogger.getLedgerDirsManager().getAllLedgerDirs(); + for (File dir : ledgerDirs) { + File[] compactingPhaseFiles = dir.listFiles(file -> file.getName().endsWith(COMPACTING_SUFFIX)); + if (compactingPhaseFiles != null) { + for (File file : compactingPhaseFiles) { + if (file.delete()) { + LOG.info("Deleted failed compaction file {}", file); Review comment: it doesn't really matter here. because next time when compaction runs, it will eventually clean up all the `compacting` files. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on 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
