2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2014-10-28 19:54:33 +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.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-03-30 21:04:10 +02:00
|
|
|
#include "db/log_writer.h"
|
2017-10-06 03:00:38 +02:00
|
|
|
#include "db/column_family.h"
|
2014-10-28 19:54:33 +01:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-10-28 19:54:33 +01:00
|
|
|
|
|
|
|
class MemTable;
|
2017-10-06 03:00:38 +02:00
|
|
|
struct SuperVersion;
|
|
|
|
|
|
|
|
struct SuperVersionContext {
|
|
|
|
struct WriteStallNotification {
|
|
|
|
WriteStallInfo write_stall_info;
|
|
|
|
const ImmutableCFOptions* immutable_cf_options;
|
|
|
|
};
|
|
|
|
|
|
|
|
autovector<SuperVersion*> superversions_to_free;
|
2018-05-09 19:04:55 +02:00
|
|
|
#ifndef ROCKSDB_DISABLE_STALL_NOTIFICATION
|
2017-10-06 03:00:38 +02:00
|
|
|
autovector<WriteStallNotification> write_stall_notifications;
|
2018-05-09 19:04:55 +02:00
|
|
|
#endif
|
2018-11-09 20:17:34 +01:00
|
|
|
std::unique_ptr<SuperVersion>
|
|
|
|
new_superversion; // if nullptr no new superversion
|
2017-10-06 03:00:38 +02:00
|
|
|
|
|
|
|
explicit SuperVersionContext(bool create_superversion = false)
|
|
|
|
: new_superversion(create_superversion ? new SuperVersion() : nullptr) {}
|
|
|
|
|
2018-08-04 02:34:07 +02:00
|
|
|
explicit SuperVersionContext(SuperVersionContext&& other)
|
|
|
|
: superversions_to_free(std::move(other.superversions_to_free)),
|
|
|
|
#ifndef ROCKSDB_DISABLE_STALL_NOTIFICATION
|
|
|
|
write_stall_notifications(std::move(other.write_stall_notifications)),
|
|
|
|
#endif
|
|
|
|
new_superversion(std::move(other.new_superversion)) {
|
|
|
|
}
|
|
|
|
|
2017-10-06 03:00:38 +02:00
|
|
|
void NewSuperVersion() {
|
2018-11-09 20:17:34 +01:00
|
|
|
new_superversion = std::unique_ptr<SuperVersion>(new SuperVersion());
|
2017-10-06 03:00:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool HaveSomethingToDelete() const {
|
2018-05-09 19:04:55 +02:00
|
|
|
#ifndef ROCKSDB_DISABLE_STALL_NOTIFICATION
|
|
|
|
return !superversions_to_free.empty() ||
|
|
|
|
!write_stall_notifications.empty();
|
|
|
|
#else
|
|
|
|
return !superversions_to_free.empty();
|
|
|
|
#endif
|
2017-10-06 03:00:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PushWriteStallNotification(
|
|
|
|
WriteStallCondition old_cond, WriteStallCondition new_cond,
|
|
|
|
const std::string& name, const ImmutableCFOptions* ioptions) {
|
2018-05-09 19:04:55 +02:00
|
|
|
#if !defined(ROCKSDB_LITE) && !defined(ROCKSDB_DISABLE_STALL_NOTIFICATION)
|
2017-10-06 03:00:38 +02:00
|
|
|
WriteStallNotification notif;
|
|
|
|
notif.write_stall_info.cf_name = name;
|
|
|
|
notif.write_stall_info.condition.prev = old_cond;
|
|
|
|
notif.write_stall_info.condition.cur = new_cond;
|
|
|
|
notif.immutable_cf_options = ioptions;
|
|
|
|
write_stall_notifications.push_back(notif);
|
2018-04-13 02:55:14 +02:00
|
|
|
#else
|
|
|
|
(void)old_cond;
|
|
|
|
(void)new_cond;
|
|
|
|
(void)name;
|
|
|
|
(void)ioptions;
|
2018-05-09 19:04:55 +02:00
|
|
|
#endif // !defined(ROCKSDB_LITE) && !defined(ROCKSDB_DISABLE_STALL_NOTIFICATION)
|
2017-10-06 03:00:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Clean() {
|
2018-05-09 19:04:55 +02:00
|
|
|
#if !defined(ROCKSDB_LITE) && !defined(ROCKSDB_DISABLE_STALL_NOTIFICATION)
|
2017-10-06 03:00:38 +02:00
|
|
|
// notify listeners on changed write stall conditions
|
|
|
|
for (auto& notif : write_stall_notifications) {
|
2018-05-09 19:04:55 +02:00
|
|
|
for (auto& listener : notif.immutable_cf_options->listeners) {
|
2017-10-06 03:00:38 +02:00
|
|
|
listener->OnStallConditionsChanged(notif.write_stall_info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write_stall_notifications.clear();
|
|
|
|
#endif // !ROCKSDB_LITE
|
|
|
|
// free superversions
|
|
|
|
for (auto s : superversions_to_free) {
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
superversions_to_free.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
~SuperVersionContext() {
|
2018-05-09 19:04:55 +02:00
|
|
|
#ifndef ROCKSDB_DISABLE_STALL_NOTIFICATION
|
|
|
|
assert(write_stall_notifications.empty());
|
|
|
|
#endif
|
|
|
|
assert(superversions_to_free.empty());
|
2017-10-06 03:00:38 +02:00
|
|
|
}
|
|
|
|
};
|
2014-10-28 19:54:33 +01:00
|
|
|
|
|
|
|
struct JobContext {
|
|
|
|
inline bool HaveSomethingToDelete() const {
|
2020-04-30 20:23:32 +02:00
|
|
|
return !(full_scan_candidate_files.empty() && sst_delete_files.empty() &&
|
|
|
|
blob_delete_files.empty() && log_delete_files.empty() &&
|
|
|
|
manifest_delete_files.empty());
|
2018-01-12 22:16:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool HaveSomethingToClean() const {
|
2018-08-04 02:34:07 +02:00
|
|
|
bool sv_have_sth = false;
|
|
|
|
for (const auto& sv_ctx : superversion_contexts) {
|
|
|
|
if (sv_ctx.HaveSomethingToDelete()) {
|
|
|
|
sv_have_sth = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 22:16:39 +01:00
|
|
|
return memtables_to_free.size() > 0 || logs_to_free.size() > 0 ||
|
2018-08-04 02:34:07 +02:00
|
|
|
sv_have_sth;
|
2014-10-28 19:54:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Structure to store information for candidate files to delete.
|
|
|
|
struct CandidateFileInfo {
|
|
|
|
std::string file_name;
|
2018-04-06 04:49:06 +02:00
|
|
|
std::string file_path;
|
|
|
|
CandidateFileInfo(std::string name, std::string path)
|
2018-05-09 19:04:55 +02:00
|
|
|
: file_name(std::move(name)), file_path(std::move(path)) {}
|
2014-10-28 19:54:33 +01:00
|
|
|
bool operator==(const CandidateFileInfo& other) const {
|
2018-04-06 04:49:06 +02:00
|
|
|
return file_name == other.file_name &&
|
|
|
|
file_path == other.file_path;
|
2014-10-28 19:54:33 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-02-12 18:54:48 +01:00
|
|
|
// Unique job id
|
|
|
|
int job_id;
|
|
|
|
|
2014-10-28 19:54:33 +01:00
|
|
|
// a list of all files that we'll consider deleting
|
|
|
|
// (every once in a while this is filled up with all files
|
|
|
|
// in the DB directory)
|
2014-12-22 12:04:45 +01:00
|
|
|
// (filled only if we're doing full scan)
|
|
|
|
std::vector<CandidateFileInfo> full_scan_candidate_files;
|
2014-10-28 19:54:33 +01:00
|
|
|
|
|
|
|
// the list of all live sst files that cannot be deleted
|
2015-02-06 17:44:30 +01:00
|
|
|
std::vector<FileDescriptor> sst_live;
|
2014-10-28 19:54:33 +01:00
|
|
|
|
|
|
|
// a list of sst files that we need to delete
|
2018-04-06 04:49:06 +02:00
|
|
|
std::vector<ObsoleteFileInfo> sst_delete_files;
|
2014-10-28 19:54:33 +01:00
|
|
|
|
2020-04-30 20:23:32 +02:00
|
|
|
// the list of blob files that we need to delete
|
|
|
|
std::vector<ObsoleteBlobFileInfo> blob_delete_files;
|
|
|
|
|
2014-10-28 19:54:33 +01:00
|
|
|
// a list of log files that we need to delete
|
|
|
|
std::vector<uint64_t> log_delete_files;
|
2016-10-13 17:48:40 +02:00
|
|
|
|
|
|
|
// a list of log files that we need to preserve during full purge since they
|
|
|
|
// will be reused later
|
|
|
|
std::vector<uint64_t> log_recycle_files;
|
2014-10-28 19:54:33 +01:00
|
|
|
|
2016-03-11 03:16:21 +01:00
|
|
|
// a list of manifest files that we need to delete
|
|
|
|
std::vector<std::string> manifest_delete_files;
|
|
|
|
|
2014-10-28 19:54:33 +01:00
|
|
|
// a list of memtables to be free
|
|
|
|
autovector<MemTable*> memtables_to_free;
|
|
|
|
|
2018-08-04 02:34:07 +02:00
|
|
|
// contexts for installing superversions for multiple column families
|
|
|
|
std::vector<SuperVersionContext> superversion_contexts;
|
2014-10-28 19:54:33 +01:00
|
|
|
|
2015-03-30 21:04:10 +02:00
|
|
|
autovector<log::Writer*> logs_to_free;
|
|
|
|
|
2014-10-28 19:54:33 +01:00
|
|
|
// the current manifest_file_number, log_number and prev_log_number
|
|
|
|
// that corresponds to the set of files in 'live'.
|
2014-11-07 20:50:34 +01:00
|
|
|
uint64_t manifest_file_number;
|
|
|
|
uint64_t pending_manifest_file_number;
|
|
|
|
uint64_t log_number;
|
|
|
|
uint64_t prev_log_number;
|
|
|
|
|
|
|
|
uint64_t min_pending_output = 0;
|
2016-06-28 02:42:14 +02:00
|
|
|
uint64_t prev_total_log_size = 0;
|
|
|
|
size_t num_alive_log_files = 0;
|
|
|
|
uint64_t size_log_to_delete = 0;
|
2014-10-28 19:54:33 +01:00
|
|
|
|
2019-01-16 06:32:15 +01:00
|
|
|
// Snapshot taken before flush/compaction job.
|
|
|
|
std::unique_ptr<ManagedSnapshot> job_snapshot;
|
|
|
|
|
2018-08-04 02:34:07 +02:00
|
|
|
explicit JobContext(int _job_id, bool create_superversion = false) {
|
2015-02-12 18:54:48 +01:00
|
|
|
job_id = _job_id;
|
2014-10-28 19:54:33 +01:00
|
|
|
manifest_file_number = 0;
|
|
|
|
pending_manifest_file_number = 0;
|
|
|
|
log_number = 0;
|
|
|
|
prev_log_number = 0;
|
2018-08-04 02:34:07 +02:00
|
|
|
superversion_contexts.emplace_back(
|
|
|
|
SuperVersionContext(create_superversion));
|
2014-10-28 19:54:33 +01:00
|
|
|
}
|
|
|
|
|
2015-07-07 21:10:10 +02:00
|
|
|
// For non-empty JobContext Clean() has to be called at least once before
|
|
|
|
// before destruction (see asserts in ~JobContext()). Should be called with
|
|
|
|
// unlocked DB mutex. Destructor doesn't call Clean() to avoid accidentally
|
|
|
|
// doing potentially slow Clean() with locked DB mutex.
|
2014-11-15 00:43:10 +01:00
|
|
|
void Clean() {
|
2017-10-06 03:00:38 +02:00
|
|
|
// free superversions
|
2018-08-04 02:34:07 +02:00
|
|
|
for (auto& sv_context : superversion_contexts) {
|
|
|
|
sv_context.Clean();
|
|
|
|
}
|
2014-10-28 19:54:33 +01:00
|
|
|
// free pending memtables
|
|
|
|
for (auto m : memtables_to_free) {
|
|
|
|
delete m;
|
|
|
|
}
|
2015-03-30 21:04:10 +02:00
|
|
|
for (auto l : logs_to_free) {
|
|
|
|
delete l;
|
|
|
|
}
|
2014-11-15 00:43:10 +01:00
|
|
|
|
|
|
|
memtables_to_free.clear();
|
2015-03-30 21:04:10 +02:00
|
|
|
logs_to_free.clear();
|
2019-01-16 06:32:15 +01:00
|
|
|
job_snapshot.reset();
|
2014-11-15 00:43:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~JobContext() {
|
|
|
|
assert(memtables_to_free.size() == 0);
|
2015-07-07 21:10:10 +02:00
|
|
|
assert(logs_to_free.size() == 0);
|
2014-10-28 19:54:33 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|