|
| 1 | +/* |
| 2 | + * Copyright contributors to Besu. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + * |
| 13 | + * SPDX-License-Identifier: Apache-2.0 |
| 14 | + */ |
| 15 | +package org.hyperledger.besu.plugin.services.storage.rocksdb.segmented; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | +import java.util.concurrent.Executors; |
| 19 | +import java.util.concurrent.ScheduledExecutorService; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 22 | + |
| 23 | +import org.rocksdb.ColumnFamilyDescriptor; |
| 24 | +import org.rocksdb.ColumnFamilyHandle; |
| 25 | +import org.rocksdb.DBOptions; |
| 26 | +import org.rocksdb.OptimisticTransactionDB; |
| 27 | +import org.rocksdb.RocksDBException; |
| 28 | +import org.rocksdb.TransactionDB; |
| 29 | +import org.rocksdb.TransactionDBOptions; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | + |
| 33 | +/** |
| 34 | + * Utility class for opening RocksDB instances with a warning mechanism. |
| 35 | + * |
| 36 | + * <p>This class provides methods to open RocksDB databases ({@link OptimisticTransactionDB} and |
| 37 | + * {@link TransactionDB}) while monitoring the operation's duration. If the database takes longer |
| 38 | + * than a predefined delay (default: 60 seconds) to open, a warning message is logged. This warning |
| 39 | + * helps identify potential delays caused by factors such as RocksDB compaction. |
| 40 | + */ |
| 41 | +public class RocksDBOpener { |
| 42 | + |
| 43 | + /** |
| 44 | + * Default delay (in seconds) after which a warning is logged if the database opening operation |
| 45 | + * has not completed. |
| 46 | + */ |
| 47 | + public static final int DEFAULT_DELAY = 60; |
| 48 | + |
| 49 | + /** |
| 50 | + * Warning message logged when the database opening operation takes longer than the predefined |
| 51 | + * delay. |
| 52 | + */ |
| 53 | + public static final String WARN_MESSAGE = |
| 54 | + "Opening RocksDB database is taking longer than 60 seconds... " |
| 55 | + + "This may be due to prolonged RocksDB compaction. Please wait until the end of the compaction. " |
| 56 | + + "No action is needed from the user."; |
| 57 | + |
| 58 | + private static final Logger LOG = LoggerFactory.getLogger(RocksDBOpener.class); |
| 59 | + |
| 60 | + /** |
| 61 | + * Default constructor. |
| 62 | + * |
| 63 | + * <p>This is a utility class and is not meant to be instantiated directly. |
| 64 | + */ |
| 65 | + private RocksDBOpener() { |
| 66 | + // Default constructor for RocksDBOpener |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Opens an {@link OptimisticTransactionDB} instance with a warning mechanism. |
| 71 | + * |
| 72 | + * <p>If the database opening operation takes longer than {@link #DEFAULT_DELAY} seconds, a |
| 73 | + * warning message is logged indicating a possible delay caused by compaction. |
| 74 | + * |
| 75 | + * @param options the {@link DBOptions} instance used to configure the database. |
| 76 | + * @param dbPath the file path to the RocksDB database directory. |
| 77 | + * @param columnDescriptors a list of {@link ColumnFamilyDescriptor} objects for the column |
| 78 | + * families to open. |
| 79 | + * @param columnHandles a list of {@link ColumnFamilyHandle} objects to be populated with the |
| 80 | + * opened column families. |
| 81 | + * @return an instance of {@link OptimisticTransactionDB}. |
| 82 | + * @throws RocksDBException if the database cannot be opened. |
| 83 | + */ |
| 84 | + public static OptimisticTransactionDB openOptimisticTransactionDBWithWarning( |
| 85 | + final DBOptions options, |
| 86 | + final String dbPath, |
| 87 | + final List<ColumnFamilyDescriptor> columnDescriptors, |
| 88 | + final List<ColumnFamilyHandle> columnHandles) |
| 89 | + throws RocksDBException { |
| 90 | + return openDBWithWarning( |
| 91 | + () -> OptimisticTransactionDB.open(options, dbPath, columnDescriptors, columnHandles)); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Opens a {@link TransactionDB} instance with a warning mechanism. |
| 96 | + * |
| 97 | + * <p>If the database opening operation takes longer than {@link #DEFAULT_DELAY} seconds, a |
| 98 | + * warning message is logged indicating a possible delay caused by compaction. |
| 99 | + * |
| 100 | + * @param options the {@link DBOptions} instance used to configure the database. |
| 101 | + * @param transactionDBOptions the {@link TransactionDBOptions} for transaction-specific |
| 102 | + * configuration. |
| 103 | + * @param dbPath the file path to the RocksDB database directory. |
| 104 | + * @param columnDescriptors a list of {@link ColumnFamilyDescriptor} objects for the column |
| 105 | + * families to open. |
| 106 | + * @param columnHandles a list of {@link ColumnFamilyHandle} objects to be populated with the |
| 107 | + * opened column families. |
| 108 | + * @return an instance of {@link TransactionDB}. |
| 109 | + * @throws RocksDBException if the database cannot be opened. |
| 110 | + */ |
| 111 | + public static TransactionDB openTransactionDBWithWarning( |
| 112 | + final DBOptions options, |
| 113 | + final TransactionDBOptions transactionDBOptions, |
| 114 | + final String dbPath, |
| 115 | + final List<ColumnFamilyDescriptor> columnDescriptors, |
| 116 | + final List<ColumnFamilyHandle> columnHandles) |
| 117 | + throws RocksDBException { |
| 118 | + return openDBWithWarning( |
| 119 | + () -> |
| 120 | + TransactionDB.open( |
| 121 | + options, transactionDBOptions, dbPath, columnDescriptors, columnHandles)); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * A generic method to open a RocksDB database with a warning mechanism. |
| 126 | + * |
| 127 | + * <p>If the operation takes longer than {@link #DEFAULT_DELAY} seconds, a warning message is |
| 128 | + * logged. |
| 129 | + * |
| 130 | + * @param dbOperation a lambda or method reference representing the database opening logic. |
| 131 | + * @param <T> the type of the database being opened (e.g., {@link OptimisticTransactionDB} or |
| 132 | + * {@link TransactionDB}). |
| 133 | + * @return an instance of the database being opened. |
| 134 | + * @throws RocksDBException if the database cannot be opened. |
| 135 | + */ |
| 136 | + private static <T> T openDBWithWarning(final DBOperation<T> dbOperation) throws RocksDBException { |
| 137 | + AtomicBoolean operationCompleted = new AtomicBoolean(false); |
| 138 | + ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); |
| 139 | + scheduler.schedule( |
| 140 | + () -> { |
| 141 | + if (!operationCompleted.get()) { |
| 142 | + LOG.warn(WARN_MESSAGE); |
| 143 | + } |
| 144 | + }, |
| 145 | + DEFAULT_DELAY, |
| 146 | + TimeUnit.SECONDS); |
| 147 | + |
| 148 | + try { |
| 149 | + T db = dbOperation.open(); |
| 150 | + operationCompleted.set(true); |
| 151 | + return db; |
| 152 | + } finally { |
| 153 | + scheduler.shutdown(); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Functional interface representing a database opening operation. |
| 159 | + * |
| 160 | + * @param <T> the type of the database being opened. |
| 161 | + */ |
| 162 | + @FunctionalInterface |
| 163 | + private interface DBOperation<T> { |
| 164 | + /** |
| 165 | + * Opens the database. |
| 166 | + * |
| 167 | + * @return the opened database instance. |
| 168 | + * @throws RocksDBException if an error occurs while opening the database. |
| 169 | + */ |
| 170 | + T open() throws RocksDBException; |
| 171 | + } |
| 172 | +} |
0 commit comments