2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2011-03-18 23:37:00 +01:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
2014-09-05 08:14:37 +02:00
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
2014-08-13 20:57:40 +02:00
|
|
|
#define __STDC_FORMAT_MACROS
|
2014-09-05 08:14:37 +02:00
|
|
|
#endif
|
|
|
|
|
2012-11-30 02:28:37 +01:00
|
|
|
#include "db/filename.h"
|
2014-08-13 20:57:40 +02:00
|
|
|
#include <inttypes.h>
|
2012-11-30 02:28:37 +01:00
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
2014-07-02 18:54:20 +02:00
|
|
|
#include <vector>
|
2011-03-18 23:37:00 +01:00
|
|
|
#include "db/dbformat.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/env.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
#include "util/logging.h"
|
2015-01-22 20:43:38 +01:00
|
|
|
#include "util/stop_watch.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2012-09-06 02:44:13 +02:00
|
|
|
// Given a path, flatten the path name by replacing all chars not in
|
2014-08-14 19:05:16 +02:00
|
|
|
// {[0-9,a-z,A-Z,-,_,.]} with _. And append '_LOG\0' at the end.
|
2012-09-06 02:44:13 +02:00
|
|
|
// Return the number of chars stored in dest not including the trailing '\0'.
|
2014-08-14 19:05:16 +02:00
|
|
|
static size_t GetInfoLogPrefix(const std::string& path, char* dest, int len) {
|
|
|
|
const char suffix[] = "_LOG";
|
2012-09-06 02:44:13 +02:00
|
|
|
|
2014-08-14 19:05:16 +02:00
|
|
|
size_t write_idx = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
size_t src_len = path.size();
|
|
|
|
|
|
|
|
while (i < src_len && write_idx < len - sizeof(suffix)) {
|
2012-09-06 02:44:13 +02:00
|
|
|
if ((path[i] >= 'a' && path[i] <= 'z') ||
|
|
|
|
(path[i] >= '0' && path[i] <= '9') ||
|
|
|
|
(path[i] >= 'A' && path[i] <= 'Z') ||
|
|
|
|
path[i] == '-' ||
|
|
|
|
path[i] == '.' ||
|
|
|
|
path[i] == '_'){
|
|
|
|
dest[write_idx++] = path[i];
|
|
|
|
} else {
|
|
|
|
if (i > 0)
|
|
|
|
dest[write_idx++] = '_';
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2014-08-14 19:05:16 +02:00
|
|
|
assert(sizeof(suffix) <= len - write_idx);
|
|
|
|
// "\0" is automatically added by snprintf
|
|
|
|
snprintf(dest + write_idx, len - write_idx, suffix);
|
|
|
|
write_idx += sizeof(suffix) - 1;
|
2012-09-06 02:44:13 +02:00
|
|
|
return write_idx;
|
|
|
|
}
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
static std::string MakeFileName(const std::string& name, uint64_t number,
|
|
|
|
const char* suffix) {
|
|
|
|
char buf[100];
|
2013-08-30 21:16:45 +02:00
|
|
|
snprintf(buf, sizeof(buf), "/%06llu.%s",
|
2011-03-18 23:37:00 +01:00
|
|
|
static_cast<unsigned long long>(number),
|
|
|
|
suffix);
|
2013-08-30 21:16:45 +02:00
|
|
|
return name + buf;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string LogFileName(const std::string& name, uint64_t number) {
|
|
|
|
assert(number > 0);
|
|
|
|
return MakeFileName(name, number, "log");
|
|
|
|
}
|
|
|
|
|
2013-10-01 23:46:52 +02:00
|
|
|
std::string ArchivalDirectory(const std::string& dir) {
|
|
|
|
return dir + "/" + ARCHIVAL_DIR;
|
2012-12-08 01:30:22 +01:00
|
|
|
}
|
2012-11-30 02:28:37 +01:00
|
|
|
std::string ArchivedLogFileName(const std::string& name, uint64_t number) {
|
|
|
|
assert(number > 0);
|
2013-08-29 23:30:52 +02:00
|
|
|
return MakeFileName(name + "/" + ARCHIVAL_DIR, number, "log");
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
|
|
|
|
2014-07-02 18:54:20 +02:00
|
|
|
std::string MakeTableFileName(const std::string& path, uint64_t number) {
|
|
|
|
return MakeFileName(path, number, "sst");
|
|
|
|
}
|
|
|
|
|
CompactFiles, EventListener and GetDatabaseMetaData
Summary:
This diff adds three sets of APIs to RocksDB.
= GetColumnFamilyMetaData =
* This APIs allow users to obtain the current state of a RocksDB instance on one column family.
* See GetColumnFamilyMetaData in include/rocksdb/db.h
= EventListener =
* A virtual class that allows users to implement a set of
call-back functions which will be called when specific
events of a RocksDB instance happens.
* To register EventListener, simply insert an EventListener to ColumnFamilyOptions::listeners
= CompactFiles =
* CompactFiles API inputs a set of file numbers and an output level, and RocksDB
will try to compact those files into the specified level.
= Example =
* Example code can be found in example/compact_files_example.cc, which implements
a simple external compactor using EventListener, GetColumnFamilyMetaData, and
CompactFiles API.
Test Plan:
listener_test
compactor_test
example/compact_files_example
export ROCKSDB_TESTS=CompactFiles
db_test
export ROCKSDB_TESTS=MetaData
db_test
Reviewers: ljin, igor, rven, sdong
Reviewed By: sdong
Subscribers: MarkCallaghan, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D24705
2014-11-07 23:45:18 +01:00
|
|
|
uint64_t TableFileNameToNumber(const std::string& name) {
|
|
|
|
uint64_t number = 0;
|
|
|
|
uint64_t base = 1;
|
|
|
|
int pos = static_cast<int>(name.find_last_of('.'));
|
|
|
|
while (--pos >= 0 && name[pos] >= '0' && name[pos] <= '9') {
|
|
|
|
number += (name[pos] - '0') * base;
|
|
|
|
base *= 10;
|
|
|
|
}
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
2014-07-15 00:34:30 +02:00
|
|
|
std::string TableFileName(const std::vector<DbPath>& db_paths, uint64_t number,
|
|
|
|
uint32_t path_id) {
|
2011-03-18 23:37:00 +01:00
|
|
|
assert(number > 0);
|
2014-07-02 18:54:20 +02:00
|
|
|
std::string path;
|
|
|
|
if (path_id >= db_paths.size()) {
|
2014-07-15 00:34:30 +02:00
|
|
|
path = db_paths.back().path;
|
2014-07-02 18:54:20 +02:00
|
|
|
} else {
|
2014-07-15 00:34:30 +02:00
|
|
|
path = db_paths[path_id].path;
|
2014-07-02 18:54:20 +02:00
|
|
|
}
|
|
|
|
return MakeTableFileName(path, number);
|
|
|
|
}
|
|
|
|
|
2014-08-13 20:57:40 +02:00
|
|
|
const size_t kFormatFileNumberBufSize = 38;
|
|
|
|
|
|
|
|
void FormatFileNumber(uint64_t number, uint32_t path_id, char* out_buf,
|
|
|
|
size_t out_buf_size) {
|
2014-07-02 18:54:20 +02:00
|
|
|
if (path_id == 0) {
|
2014-08-13 20:57:40 +02:00
|
|
|
snprintf(out_buf, out_buf_size, "%" PRIu64, number);
|
2014-07-02 18:54:20 +02:00
|
|
|
} else {
|
2014-08-13 20:57:40 +02:00
|
|
|
snprintf(out_buf, out_buf_size, "%" PRIu64
|
|
|
|
"(path "
|
|
|
|
"%" PRIu32 ")",
|
|
|
|
number, path_id);
|
2014-07-02 18:54:20 +02:00
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string DescriptorFileName(const std::string& dbname, uint64_t number) {
|
|
|
|
assert(number > 0);
|
|
|
|
char buf[100];
|
|
|
|
snprintf(buf, sizeof(buf), "/MANIFEST-%06llu",
|
|
|
|
static_cast<unsigned long long>(number));
|
|
|
|
return dbname + buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CurrentFileName(const std::string& dbname) {
|
|
|
|
return dbname + "/CURRENT";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LockFileName(const std::string& dbname) {
|
|
|
|
return dbname + "/LOCK";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TempFileName(const std::string& dbname, uint64_t number) {
|
|
|
|
return MakeFileName(dbname, number, "dbtmp");
|
|
|
|
}
|
|
|
|
|
2014-08-14 19:05:16 +02:00
|
|
|
InfoLogPrefix::InfoLogPrefix(bool has_log_dir,
|
|
|
|
const std::string& db_absolute_path) {
|
|
|
|
if (!has_log_dir) {
|
|
|
|
const char kInfoLogPrefix[] = "LOG";
|
|
|
|
// "\0" is automatically added to the end
|
|
|
|
snprintf(buf, sizeof(buf), kInfoLogPrefix);
|
|
|
|
prefix = Slice(buf, sizeof(kInfoLogPrefix) - 1);
|
|
|
|
} else {
|
|
|
|
size_t len = GetInfoLogPrefix(db_absolute_path, buf, sizeof(buf));
|
|
|
|
prefix = Slice(buf, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-06 02:44:13 +02:00
|
|
|
std::string InfoLogFileName(const std::string& dbname,
|
|
|
|
const std::string& db_path, const std::string& log_dir) {
|
|
|
|
if (log_dir.empty())
|
|
|
|
return dbname + "/LOG";
|
|
|
|
|
2014-08-14 19:05:16 +02:00
|
|
|
InfoLogPrefix info_log_prefix(true, db_path);
|
|
|
|
return log_dir + "/" + info_log_prefix.buf;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the name of the old info log file for "dbname".
|
2012-09-06 02:44:13 +02:00
|
|
|
std::string OldInfoLogFileName(const std::string& dbname, uint64_t ts,
|
|
|
|
const std::string& db_path, const std::string& log_dir) {
|
2012-08-18 01:06:05 +02:00
|
|
|
char buf[50];
|
|
|
|
snprintf(buf, sizeof(buf), "%llu", static_cast<unsigned long long>(ts));
|
2012-09-06 02:44:13 +02:00
|
|
|
|
|
|
|
if (log_dir.empty())
|
|
|
|
return dbname + "/LOG.old." + buf;
|
|
|
|
|
2014-08-14 19:05:16 +02:00
|
|
|
InfoLogPrefix info_log_prefix(true, db_path);
|
|
|
|
return log_dir + "/" + info_log_prefix.buf + ".old." + buf;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2012-12-17 20:26:59 +01:00
|
|
|
std::string MetaDatabaseName(const std::string& dbname, uint64_t number) {
|
|
|
|
char buf[100];
|
|
|
|
snprintf(buf, sizeof(buf), "/METADB-%llu",
|
|
|
|
static_cast<unsigned long long>(number));
|
|
|
|
return dbname + buf;
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-10-18 23:50:54 +02:00
|
|
|
std::string IdentityFileName(const std::string& dbname) {
|
|
|
|
return dbname + "/IDENTITY";
|
|
|
|
}
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
// Owned filenames have the form:
|
2013-10-18 23:50:54 +02:00
|
|
|
// dbname/IDENTITY
|
2011-03-18 23:37:00 +01:00
|
|
|
// dbname/CURRENT
|
|
|
|
// dbname/LOCK
|
2014-08-14 19:05:16 +02:00
|
|
|
// dbname/<info_log_name_prefix>
|
|
|
|
// dbname/<info_log_name_prefix>.old.[0-9]+
|
2011-03-18 23:37:00 +01:00
|
|
|
// dbname/MANIFEST-[0-9]+
|
|
|
|
// dbname/[0-9]+.(log|sst)
|
2012-12-17 20:26:59 +01:00
|
|
|
// dbname/METADB-[0-9]+
|
2013-09-01 10:52:32 +02:00
|
|
|
// Disregards / at the beginning
|
2011-03-18 23:37:00 +01:00
|
|
|
bool ParseFileName(const std::string& fname,
|
|
|
|
uint64_t* number,
|
2013-10-24 08:39:23 +02:00
|
|
|
FileType* type,
|
|
|
|
WalFileType* log_type) {
|
2014-08-14 19:05:16 +02:00
|
|
|
return ParseFileName(fname, number, "", type, log_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParseFileName(const std::string& fname, uint64_t* number,
|
|
|
|
const Slice& info_log_name_prefix, FileType* type,
|
|
|
|
WalFileType* log_type) {
|
2011-03-18 23:37:00 +01:00
|
|
|
Slice rest(fname);
|
2013-09-01 10:52:32 +02:00
|
|
|
if (fname.length() > 1 && fname[0] == '/') {
|
|
|
|
rest.remove_prefix(1);
|
|
|
|
}
|
2013-10-18 23:50:54 +02:00
|
|
|
if (rest == "IDENTITY") {
|
|
|
|
*number = 0;
|
|
|
|
*type = kIdentityFile;
|
|
|
|
} else if (rest == "CURRENT") {
|
2011-03-18 23:37:00 +01:00
|
|
|
*number = 0;
|
|
|
|
*type = kCurrentFile;
|
|
|
|
} else if (rest == "LOCK") {
|
|
|
|
*number = 0;
|
|
|
|
*type = kDBLockFile;
|
2014-08-14 19:05:16 +02:00
|
|
|
} else if (info_log_name_prefix.size() > 0 &&
|
|
|
|
rest.starts_with(info_log_name_prefix)) {
|
|
|
|
rest.remove_prefix(info_log_name_prefix.size());
|
|
|
|
if (rest == "" || rest == ".old") {
|
|
|
|
*number = 0;
|
|
|
|
*type = kInfoLogFile;
|
|
|
|
} else if (rest.starts_with(".old.")) {
|
|
|
|
uint64_t ts_suffix;
|
|
|
|
// sizeof also counts the trailing '\0'.
|
|
|
|
rest.remove_prefix(sizeof(".old.") - 1);
|
|
|
|
if (!ConsumeDecimalNumber(&rest, &ts_suffix)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*number = ts_suffix;
|
|
|
|
*type = kInfoLogFile;
|
2012-08-18 01:06:05 +02:00
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
} else if (rest.starts_with("MANIFEST-")) {
|
|
|
|
rest.remove_prefix(strlen("MANIFEST-"));
|
|
|
|
uint64_t num;
|
|
|
|
if (!ConsumeDecimalNumber(&rest, &num)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!rest.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*type = kDescriptorFile;
|
|
|
|
*number = num;
|
2012-12-17 20:26:59 +01:00
|
|
|
} else if (rest.starts_with("METADB-")) {
|
|
|
|
rest.remove_prefix(strlen("METADB-"));
|
|
|
|
uint64_t num;
|
|
|
|
if (!ConsumeDecimalNumber(&rest, &num)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!rest.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*type = kMetaDatabase;
|
|
|
|
*number = num;
|
2011-03-18 23:37:00 +01:00
|
|
|
} else {
|
|
|
|
// Avoid strtoull() to keep filename format independent of the
|
|
|
|
// current locale
|
2013-10-24 08:39:23 +02:00
|
|
|
bool archive_dir_found = false;
|
|
|
|
if (rest.starts_with(ARCHIVAL_DIR)) {
|
|
|
|
if (rest.size() <= ARCHIVAL_DIR.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
rest.remove_prefix(ARCHIVAL_DIR.size() + 1); // Add 1 to remove / also
|
|
|
|
if (log_type) {
|
|
|
|
*log_type = kArchivedLogFile;
|
|
|
|
}
|
|
|
|
archive_dir_found = true;
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
uint64_t num;
|
|
|
|
if (!ConsumeDecimalNumber(&rest, &num)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Slice suffix = rest;
|
|
|
|
if (suffix == Slice(".log")) {
|
|
|
|
*type = kLogFile;
|
2013-10-24 08:39:23 +02:00
|
|
|
if (log_type && !archive_dir_found) {
|
|
|
|
*log_type = kAliveLogFile;
|
|
|
|
}
|
|
|
|
} else if (archive_dir_found) {
|
|
|
|
return false; // Archive dir can contain only log files
|
2011-03-18 23:37:00 +01:00
|
|
|
} else if (suffix == Slice(".sst")) {
|
|
|
|
*type = kTableFile;
|
|
|
|
} else if (suffix == Slice(".dbtmp")) {
|
|
|
|
*type = kTempFile;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*number = num;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SetCurrentFile(Env* env, const std::string& dbname,
|
2014-05-06 23:51:33 +02:00
|
|
|
uint64_t descriptor_number,
|
|
|
|
Directory* directory_to_fsync) {
|
2011-03-18 23:37:00 +01:00
|
|
|
// Remove leading "dbname/" and add newline to manifest file name
|
|
|
|
std::string manifest = DescriptorFileName(dbname, descriptor_number);
|
|
|
|
Slice contents = manifest;
|
|
|
|
assert(contents.starts_with(dbname + "/"));
|
|
|
|
contents.remove_prefix(dbname.size() + 1);
|
|
|
|
std::string tmp = TempFileName(dbname, descriptor_number);
|
2014-04-10 06:17:14 +02:00
|
|
|
Status s = WriteStringToFile(env, contents.ToString() + "\n", tmp, true);
|
2011-03-18 23:37:00 +01:00
|
|
|
if (s.ok()) {
|
|
|
|
s = env->RenameFile(tmp, CurrentFileName(dbname));
|
|
|
|
}
|
2014-05-06 23:51:33 +02:00
|
|
|
if (s.ok()) {
|
|
|
|
if (directory_to_fsync != nullptr) {
|
|
|
|
directory_to_fsync->Fsync();
|
|
|
|
}
|
|
|
|
} else {
|
2011-03-18 23:37:00 +01:00
|
|
|
env->DeleteFile(tmp);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-10-18 23:50:54 +02:00
|
|
|
Status SetIdentityFile(Env* env, const std::string& dbname) {
|
|
|
|
std::string id = env->GenerateUniqueId();
|
|
|
|
assert(!id.empty());
|
2013-10-23 19:59:08 +02:00
|
|
|
// Reserve the filename dbname/000000.dbtmp for the temporary identity file
|
|
|
|
std::string tmp = TempFileName(dbname, 0);
|
2014-04-10 06:17:14 +02:00
|
|
|
Status s = WriteStringToFile(env, id, tmp, true);
|
2013-10-18 23:50:54 +02:00
|
|
|
if (s.ok()) {
|
|
|
|
s = env->RenameFile(tmp, IdentityFileName(dbname));
|
|
|
|
}
|
|
|
|
if (!s.ok()) {
|
|
|
|
env->DeleteFile(tmp);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-01-22 20:43:38 +01:00
|
|
|
Status SyncManifest(Env* env, const DBOptions* db_options, WritableFile* file) {
|
|
|
|
if (db_options->disableDataSync) {
|
|
|
|
return Status::OK();
|
|
|
|
} else if (db_options->use_fsync) {
|
|
|
|
StopWatch sw(env, db_options->statistics.get(), MANIFEST_FILE_SYNC_MICROS);
|
|
|
|
return file->Fsync();
|
|
|
|
} else {
|
|
|
|
StopWatch sw(env, db_options->statistics.get(), MANIFEST_FILE_SYNC_MICROS);
|
|
|
|
return file->Sync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|