Skip to content

Commit

Permalink
SERVER-17331 RocksDB configuring and monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcanadi committed Feb 20, 2015
1 parent e191b45 commit b7fb7c4
Show file tree
Hide file tree
Showing 12 changed files with 408 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/mongo/db/storage/rocks/SConscript
Expand Up @@ -6,6 +6,7 @@ if has_option("rocksdb"):
env.Library(
target= 'storage_rocks_base',
source= [
'rocks_global_options.cpp',
'rocks_engine.cpp',
'rocks_record_store.cpp',
'rocks_recovery_unit.cpp',
Expand Down Expand Up @@ -34,7 +35,9 @@ if has_option("rocksdb"):
target= 'storage_rocks',
source= [
'rocks_init.cpp',
'rocks_options_init.cpp',
'rocks_record_store_mongod.cpp',
'rocks_server_status.cpp',
],
LIBDEPS= [
'storage_rocks_base',
Expand Down
39 changes: 30 additions & 9 deletions src/mongo/db/storage/rocks/rocks_engine.cpp
Expand Up @@ -44,12 +44,14 @@
#include <rocksdb/slice.h>
#include <rocksdb/options.h>
#include <rocksdb/table.h>
#include <rocksdb/utilities/convenience.h>
#include <rocksdb/filter_policy.h>
#include <rocksdb/utilities/write_batch_with_index.h>

#include "mongo/db/catalog/collection_options.h"
#include "mongo/db/index/index_descriptor.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/storage/rocks/rocks_global_options.h"
#include "mongo/db/storage/rocks/rocks_record_store.h"
#include "mongo/db/storage/rocks/rocks_recovery_unit.h"
#include "mongo/db/storage/rocks/rocks_index.h"
Expand Down Expand Up @@ -87,15 +89,17 @@ namespace mongo {
RocksEngine::RocksEngine(const std::string& path, bool durable)
: _path(path), _durable(durable) {
{ // create block cache
uint64_t cacheSizeGB = 0;
ProcessInfo pi;
unsigned long long memSizeMB = pi.getMemSizeMB();
if (memSizeMB > 0) {
double cacheMB = memSizeMB / 2;
cacheSizeGB = static_cast<uint64_t>(cacheMB / 1024);
}
if (cacheSizeGB < 1) {
cacheSizeGB = 1;
uint64_t cacheSizeGB = rocksGlobalOptions.cacheSizeGB;
if (cacheSizeGB == 0) {
ProcessInfo pi;
unsigned long long memSizeMB = pi.getMemSizeMB();
if (memSizeMB > 0) {
double cacheMB = memSizeMB / 2;
cacheSizeGB = static_cast<uint64_t>(cacheMB / 1024);
}
if (cacheSizeGB < 1) {
cacheSizeGB = 1;
}
}
_block_cache = rocksdb::NewLRUCache(cacheSizeGB * 1024 * 1024 * 1024LL);
}
Expand Down Expand Up @@ -252,6 +256,7 @@ namespace mongo {
}

rocksdb::Options RocksEngine::_options() const {
// default options
rocksdb::Options options;
rocksdb::BlockBasedTableOptions table_options;
table_options.block_cache = _block_cache;
Expand All @@ -269,10 +274,26 @@ namespace mongo {
options.max_bytes_for_level_base = 512 * 1024 * 1024; // 512 MB
options.max_open_files = 20000;

if (rocksGlobalOptions.compression == "snappy") {
options.compression = rocksdb::kSnappyCompression;
} else if (rocksGlobalOptions.compression == "zlib") {
options.compression = rocksdb::kZlibCompression;
} else if (rocksGlobalOptions.compression == "none") {
options.compression = rocksdb::kNoCompression;
} else {
log() << "Unknown compression, will use default (snappy)";
}

// create the DB if it's not already present
options.create_if_missing = true;
options.wal_dir = _path + "/journal";

// allow override
if (!rocksGlobalOptions.configString.empty()) {
rocksdb::Options base_options(options);
rocksdb::GetOptionsFromString(base_options, rocksGlobalOptions.configString, &options);
}

return options;
}

Expand Down
1 change: 1 addition & 0 deletions src/mongo/db/storage/rocks/rocks_engine.h
Expand Up @@ -128,6 +128,7 @@ namespace mongo {

rocksdb::DB* getDB() { return _db.get(); }
const rocksdb::DB* getDB() const { return _db.get(); }
size_t getBlockCacheUsage() const { return _block_cache->GetUsage(); }

private:
Status _createIdentPrefix(const StringData& ident);
Expand Down
83 changes: 83 additions & 0 deletions src/mongo/db/storage/rocks/rocks_global_options.cpp
@@ -0,0 +1,83 @@
/**
* Copyright (C) 2014 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kStorage

#include "mongo/platform/basic.h"

#include "mongo/base/status.h"
#include "mongo/db/storage/rocks/rocks_global_options.h"
#include "mongo/util/log.h"
#include "mongo/util/options_parser/constraints.h"

namespace mongo {

RocksGlobalOptions rocksGlobalOptions;

Status RocksGlobalOptions::add(moe::OptionSection* options) {
moe::OptionSection rocksOptions("RocksDB options");

rocksOptions.addOptionChaining("storage.rocksdb.cacheSizeGB", "rocksdbCacheSizeGB",
moe::Int,
"maximum amount of memory to allocate for cache; "
"defaults to 1/2 of physical RAM").validRange(1, 10000);
rocksOptions.addOptionChaining("storage.rocksdb.compression", "rocksdbCompression",
moe::String,
"block compression algorithm for collection data "
"[none|snappy|zlib]")
.format("(:?none)|(:?snappy)|(:?zlib)", "(none/snappy/zlib)")
.setDefault(moe::Value(std::string("snappy")));
rocksOptions.addOptionChaining("storage.rocksdb.configString", "rocksdbConfigString",
moe::String,
"RocksDB storage engine custom "
"configuration settings").hidden();

return options->addSection(rocksOptions);
}

Status RocksGlobalOptions::store(const moe::Environment& params,
const std::vector<std::string>& args) {
if (params.count("storage.rocksdb.cacheSizeGB")) {
rocksGlobalOptions.cacheSizeGB = params["storage.rocksdb.cacheSizeGB"].as<int>();
log() << "Block Cache Size GB: " << rocksGlobalOptions.cacheSizeGB;
}
if (params.count("storage.rocksdb.compression")) {
rocksGlobalOptions.compression =
params["storage.rocksdb.compression"].as<std::string>();
log() << "Compression: " << rocksGlobalOptions.compression;
}
if (params.count("storage.rocksdb.configString")) {
rocksGlobalOptions.configString =
params["storage.rocksdb.configString"].as<std::string>();
log() << "Engine custom option: " << rocksGlobalOptions.configString;
}

return Status::OK();
}

} // namespace mongo
52 changes: 52 additions & 0 deletions src/mongo/db/storage/rocks/rocks_global_options.h
@@ -0,0 +1,52 @@
/**
* Copyright (C) 2014 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/

#pragma once

#include "mongo/util/options_parser/startup_option_init.h"
#include "mongo/util/options_parser/startup_options.h"

namespace mongo {

namespace moe = mongo::optionenvironment;

class RocksGlobalOptions {
public:
RocksGlobalOptions() : cacheSizeGB(0) {}

Status add(moe::OptionSection* options);
Status store(const moe::Environment& params, const std::vector<std::string>& args);

size_t cacheSizeGB;

std::string compression;
std::string configString;
};

extern RocksGlobalOptions rocksGlobalOptions;
}
8 changes: 6 additions & 2 deletions src/mongo/db/storage/rocks/rocks_init.cpp
Expand Up @@ -30,6 +30,7 @@
#include "mongo/platform/basic.h"

#include "mongo/db/storage/rocks/rocks_engine.h"
#include "mongo/db/storage/rocks/rocks_server_status.h"

#include "mongo/base/init.h"
#include "mongo/db/global_environment_experiment.h"
Expand All @@ -51,8 +52,11 @@ namespace mongo {
options.forRepair = params.repair;
// Mongo keeps some files in params.dbpath. To avoid collision, put out files under
// db/ directory
return new KVStorageEngine(new RocksEngine(params.dbpath + "/db", params.dur),
options);
auto engine = new RocksEngine(params.dbpath + "/db", params.dur);
// Intentionally leaked.
auto leaked __attribute__((unused)) = new RocksServerStatusSection(engine);

return new KVStorageEngine(engine, options);
}

virtual StringData getCanonicalName() const {
Expand Down
56 changes: 56 additions & 0 deletions src/mongo/db/storage/rocks/rocks_options_init.cpp
@@ -0,0 +1,56 @@
/**
* Copyright (C) 2014 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the GNU Affero General Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/

#include "mongo/util/options_parser/startup_option_init.h"

#include <iostream>

#include "mongo/util/options_parser/startup_options.h"
#include "mongo/db/storage/rocks/rocks_global_options.h"

namespace mongo {

MONGO_MODULE_STARTUP_OPTIONS_REGISTER(RocksOptions)(InitializerContext* context) {
return rocksGlobalOptions.add(&moe::startupOptions);
}

MONGO_STARTUP_OPTIONS_VALIDATE(RocksOptions)(InitializerContext* context) {
return Status::OK();
}

MONGO_STARTUP_OPTIONS_STORE(RocksOptions)(InitializerContext* context) {
Status ret = rocksGlobalOptions.store(moe::startupOptionsParsed, context->args());
if (!ret.isOK()) {
std::cerr << ret.toString() << std::endl;
std::cerr << "try '" << context->args()[0] << " --help' for more information"
<< std::endl;
::_exit(EXIT_BADOPTIONS);
}
return Status::OK();
}
}
3 changes: 0 additions & 3 deletions src/mongo/db/storage/rocks/rocks_record_store.cpp
Expand Up @@ -578,9 +578,6 @@ namespace mongo {
result->appendIntOrLL("max", _cappedMaxDocs);
result->appendIntOrLL("maxSize", _cappedMaxSize / scale);
}
bool valid = _db->GetProperty("rocksdb.stats", &statsString);
invariant( valid );
result->append( "stats", statsString );
}

Status RocksRecordStore::oplogDiskLocRegister(OperationContext* txn, const OpTime& opTime) {
Expand Down
7 changes: 6 additions & 1 deletion src/mongo/db/storage/rocks/rocks_recovery_unit.cpp
Expand Up @@ -109,6 +109,8 @@ namespace mongo {

} // anonymous namespace

std::atomic<int> RocksRecoveryUnit::_totalLiveRecoveryUnits(0);

RocksRecoveryUnit::RocksRecoveryUnit(RocksTransactionEngine* transactionEngine, rocksdb::DB* db,
bool durable)
: _transactionEngine(transactionEngine),
Expand All @@ -118,10 +120,13 @@ namespace mongo {
_writeBatch(),
_snapshot(NULL),
_depth(0),
_myTransactionCount(1) {}
_myTransactionCount(1) {
RocksRecoveryUnit::_totalLiveRecoveryUnits.fetch_add(1, std::memory_order_relaxed);
}

RocksRecoveryUnit::~RocksRecoveryUnit() {
_abort();
RocksRecoveryUnit::_totalLiveRecoveryUnits.fetch_sub(1, std::memory_order_relaxed);
}

void RocksRecoveryUnit::beginUnitOfWork(OperationContext* opCtx) {
Expand Down
4 changes: 4 additions & 0 deletions src/mongo/db/storage/rocks/rocks_recovery_unit.h
Expand Up @@ -121,6 +121,8 @@ namespace mongo {

static RocksRecoveryUnit* getRocksRecoveryUnit(OperationContext* opCtx);

static int getTotalLiveRecoveryUnits() { return _totalLiveRecoveryUnits.load(); }

private:
void _releaseSnapshot();

Expand Down Expand Up @@ -148,6 +150,8 @@ namespace mongo {
uint64_t _myTransactionCount;

RecordId _oplogReadTill;

static std::atomic<int> _totalLiveRecoveryUnits;
};

}

0 comments on commit b7fb7c4

Please sign in to comment.