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/09/23 08:03:06 UTC

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

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



##########
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");
+
+        String timestamp = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date());
+        String basePath = BookieImpl.getCurrentDirectory(conf.getLedgerDirs()[0]).toString();
+        String tempLedgersSubPath = LedgersSubPath + ".TEMP-" + timestamp;
+        Path tempPath = FileSystems.getDefault().getPath(basePath, tempLedgersSubPath);
+        Path currentPath = FileSystems.getDefault().getPath(basePath, LedgersSubPath);
+
+        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, tempLedgersSubPath, DbConfigType.Small, conf);
+            LOG.info("Created ledgers index at temp location {}", tempPath);
+
+            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, the original index remains unchanged", t);
+            delete(tempPath);
+            return false;
+        }
+
+        // replace the existing index
+        try {
+            Path prevPath = FileSystems.getDefault().getPath(basePath, LedgersSubPath + ".PREV-" + timestamp);
+            Files.move(currentPath, prevPath);
+            Files.move(tempPath, currentPath);
+            LOG.info("Original index has been replaced with the new index. "
+                    + "The original index has been moved to {}", prevPath);
+        } catch (IOException e) {
+            LOG.error("Could not replace the existing index", e);

Review comment:
       drop temporary index? 

##########
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");
+
+        String timestamp = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date());
+        String basePath = BookieImpl.getCurrentDirectory(conf.getLedgerDirs()[0]).toString();
+        String tempLedgersSubPath = LedgersSubPath + ".TEMP-" + timestamp;
+        Path tempPath = FileSystems.getDefault().getPath(basePath, tempLedgersSubPath);
+        Path currentPath = FileSystems.getDefault().getPath(basePath, LedgersSubPath);
+
+        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(

Review comment:
       try with resources ? 

##########
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");
+
+        String timestamp = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date());
+        String basePath = BookieImpl.getCurrentDirectory(conf.getLedgerDirs()[0]).toString();
+        String tempLedgersSubPath = LedgersSubPath + ".TEMP-" + timestamp;
+        Path tempPath = FileSystems.getDefault().getPath(basePath, tempLedgersSubPath);
+        Path currentPath = FileSystems.getDefault().getPath(basePath, LedgersSubPath);
+
+        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, tempLedgersSubPath, DbConfigType.Small, conf);
+            LOG.info("Created ledgers index at temp location {}", tempPath);
+
+            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, the original index remains unchanged", t);
+            delete(tempPath);
+            return false;
+        }
+
+        // replace the existing index
+        try {
+            Path prevPath = FileSystems.getDefault().getPath(basePath, LedgersSubPath + ".PREV-" + timestamp);
+            Files.move(currentPath, prevPath);
+            Files.move(tempPath, currentPath);

Review comment:
       what if this instruction fails? we don't have a valid index file anymore? 




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