You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2021/08/25 10:09:53 UTC

[GitHub] [bookkeeper] Vanlightly commented on a change in pull request #2774: ISSUE-2773: Add db ledgers index rebuild op

Vanlightly commented on a change in pull request #2774:
URL: https://github.com/apache/bookkeeper/pull/2774#discussion_r695603700



##########
File path: bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/LedgersIndexRebuildOp.java
##########
@@ -0,0 +1,214 @@
+/**
+ *
+ * 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.storage.ldb;
+
+import com.google.common.collect.Lists;
+import com.google.protobuf.ByteString;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import org.apache.bookkeeper.bookie.BookieImpl;
+import org.apache.bookkeeper.bookie.EntryLogger;
+import org.apache.bookkeeper.bookie.EntryLogger.EntryLogScanner;
+import org.apache.bookkeeper.bookie.Journal;
+import org.apache.bookkeeper.bookie.LedgerDirsManager;
+import org.apache.bookkeeper.bookie.storage.ldb.KeyValueStorageFactory.DbConfigType;
+import org.apache.bookkeeper.conf.ServerConfiguration;
+import org.apache.bookkeeper.util.BookKeeperConstants;
+import org.apache.bookkeeper.util.DiskChecker;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Scan all entries in the journal and entry log files then rebuilds the ledgers index.
+ * Notable stuff:
+ * - Fences every ledger as even if we check the metadata, we cannot guarantee that
+ *   a fence request was served while the rebuild was taking place (even if the bookie
+ *   is running in read-only mode).
+ *   Losing the fenced status of a ledger is UNSAFE.
+ * - Sets the master key as an empty byte array. This is correct as empty master keys
+ *   are overwritten and we cannot use the password from metadata, and cannot know 100%
+ *   for sure how a digest for the password was generated.
+ */
+public class LedgersIndexRebuildOp {
+    private static final Logger LOG = LoggerFactory.getLogger(LedgersIndexRebuildOp.class);
+
+    private final ServerConfiguration conf;
+    private final boolean verbose;
+    private static final String LedgersSubPath = "ledgers";
+
+    public LedgersIndexRebuildOp(ServerConfiguration conf, boolean verbose) {
+        this.conf = conf;
+        this.verbose = verbose;
+    }
+
+    public boolean initiate()  {
+        LOG.info("Starting ledger index rebuilding");
+
+        // Move locations index to a backup directory
+        String basePath = BookieImpl.getCurrentDirectory(conf.getLedgerDirs()[0]).toString();
+        Path currentPath = FileSystems.getDefault().getPath(basePath, LedgersSubPath);
+        String timestamp = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date());
+        Path backupPath = FileSystems.getDefault().getPath(basePath, LedgersSubPath + ".BACKUP-" + timestamp);
+        Path abortPath = FileSystems.getDefault().getPath(basePath, LedgersSubPath + ".ABORT-" + timestamp);
+        try {
+            Files.move(currentPath, backupPath);
+        } catch (IOException e) {
+            LOG.error("Could not move index to a backup location", e);
+            return false;
+        }
+
+        LOG.info("Created ledgers index backup at {}", backupPath);
+        LOG.info("Starting scan phase (scans journal and entry log files)");
+
+        try {
+            Set<Long> ledgers = new HashSet<>();
+            scanJournals(ledgers);
+            scanEntryLogFiles(ledgers);
+
+            LOG.info("Scan complete, found {} ledgers. "
+                    + "Starting to build a new ledgers index", ledgers.size());
+
+            KeyValueStorage newIndex = KeyValueStorageRocksDB.factory.newKeyValueStorage(
+                    basePath, LedgersSubPath, DbConfigType.Small, conf);
+
+            for (Long ledgerId : ledgers) {
+                DbLedgerStorageDataFormats.LedgerData ledgerData =
+                        DbLedgerStorageDataFormats.LedgerData.newBuilder()
+                                .setExists(true)
+                                .setFenced(true)
+                                .setMasterKey(ByteString.EMPTY).build();
+
+                byte[] ledgerArray = new byte[16];
+                ArrayUtil.setLong(ledgerArray, 0, ledgerId);
+                newIndex.put(ledgerArray, ledgerData.toByteArray());
+            }
+
+            newIndex.sync();
+            newIndex.close();
+        } catch (Throwable t) {
+            LOG.error("Error during rebuild", t);
+            restoreBackup(currentPath, backupPath, abortPath);

Review comment:
       Good point, I was copying from the LocationsIndexRebuildOp which does it that way. I have updated this rebuild to swap them at the end once a new index has been generated.




-- 
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: issues-unsubscribe@bookkeeper.apache.org

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