2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00: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-30 00:43:37 +00: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 <atomic>
|
|
|
|
#include <deque>
|
|
|
|
#include <limits>
|
2022-11-02 21:34:24 +00:00
|
|
|
#include <memory>
|
2014-10-30 00:43:37 +00:00
|
|
|
#include <set>
|
2022-11-02 21:34:24 +00:00
|
|
|
#include <string>
|
2014-10-30 00:43:37 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-09-23 23:34:04 +00:00
|
|
|
#include "db/version_set.h"
|
2019-07-07 04:04:22 +00:00
|
|
|
#include "file/file_util.h"
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "options/db_options.h"
|
2014-10-30 00:43:37 +00:00
|
|
|
#include "port/port.h"
|
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/status.h"
|
2016-09-23 23:34:04 +00:00
|
|
|
#include "rocksdb/transaction_log.h"
|
|
|
|
#include "rocksdb/types.h"
|
2014-10-30 00:43:37 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-10-30 00:43:37 +00:00
|
|
|
|
2019-05-24 17:36:26 +00:00
|
|
|
|
|
|
|
// WAL manager provides the abstraction for reading the WAL files as a single
|
|
|
|
// unit. Internally, it opens and reads the files using Reader or Writer
|
|
|
|
// abstraction.
|
2014-10-30 00:43:37 +00:00
|
|
|
class WalManager {
|
|
|
|
public:
|
2016-09-23 23:34:04 +00:00
|
|
|
WalManager(const ImmutableDBOptions& db_options,
|
2020-08-13 00:28:10 +00:00
|
|
|
const FileOptions& file_options,
|
|
|
|
const std::shared_ptr<IOTracer>& io_tracer,
|
|
|
|
const bool seq_per_batch = false)
|
2014-10-30 00:43:37 +00:00
|
|
|
: db_options_(db_options),
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
file_options_(file_options),
|
2014-10-30 00:43:37 +00:00
|
|
|
env_(db_options.env),
|
2020-08-13 00:28:10 +00:00
|
|
|
fs_(db_options.fs, io_tracer),
|
2017-11-11 01:18:01 +00:00
|
|
|
purge_wal_files_last_run_(0),
|
2019-07-07 04:04:22 +00:00
|
|
|
seq_per_batch_(seq_per_batch),
|
2021-07-30 19:15:04 +00:00
|
|
|
wal_dir_(db_options_.GetWalDir()),
|
|
|
|
wal_in_db_path_(db_options_.IsWalDirSameAsDBPath()),
|
2020-08-13 00:28:10 +00:00
|
|
|
io_tracer_(io_tracer) {}
|
2014-10-30 00:43:37 +00:00
|
|
|
|
2014-11-10 22:39:38 +00:00
|
|
|
Status GetSortedWalFiles(VectorLogPtr& files);
|
2014-10-30 00:43:37 +00:00
|
|
|
|
2019-05-24 17:36:26 +00:00
|
|
|
// Allow user to tail transaction log to find all recent changes to the
|
|
|
|
// database that are newer than `seq_number`.
|
2014-11-10 22:39:38 +00:00
|
|
|
Status GetUpdatesSince(
|
2014-10-30 00:43:37 +00:00
|
|
|
SequenceNumber seq_number, std::unique_ptr<TransactionLogIterator>* iter,
|
|
|
|
const TransactionLogIterator::ReadOptions& read_options,
|
|
|
|
VersionSet* version_set);
|
|
|
|
|
|
|
|
void PurgeObsoleteWALFiles();
|
|
|
|
|
|
|
|
void ArchiveWALFile(const std::string& fname, uint64_t number);
|
|
|
|
|
2018-01-12 02:48:19 +00:00
|
|
|
Status DeleteFile(const std::string& fname, uint64_t number);
|
|
|
|
|
2019-09-04 19:08:56 +00:00
|
|
|
Status GetLiveWalFile(uint64_t number, std::unique_ptr<LogFile>* log_file);
|
|
|
|
|
2014-10-30 00:43:37 +00:00
|
|
|
Status TEST_ReadFirstRecord(const WalFileType type, const uint64_t number,
|
|
|
|
SequenceNumber* sequence) {
|
|
|
|
return ReadFirstRecord(type, number, sequence);
|
|
|
|
}
|
|
|
|
|
2016-09-15 16:55:02 +00:00
|
|
|
Status TEST_ReadFirstLine(const std::string& fname, const uint64_t number,
|
2014-10-30 00:43:37 +00:00
|
|
|
SequenceNumber* sequence) {
|
2016-09-15 16:55:02 +00:00
|
|
|
return ReadFirstLine(fname, number, sequence);
|
2014-10-30 00:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Status GetSortedWalsOfType(const std::string& path, VectorLogPtr& log_files,
|
|
|
|
WalFileType type);
|
|
|
|
// Requires: all_logs should be sorted with earliest log file first
|
|
|
|
// Retains all log files in all_logs which contain updates with seq no.
|
|
|
|
// Greater Than or Equal to the requested SequenceNumber.
|
|
|
|
Status RetainProbableWalFiles(VectorLogPtr& all_logs,
|
|
|
|
const SequenceNumber target);
|
|
|
|
|
2022-06-08 21:16:43 +00:00
|
|
|
// ReadFirstRecord checks the read_first_record_cache_ to see if the entry
|
|
|
|
// exists or not. If not, it will read the WAL file.
|
|
|
|
// In case of wal_compression, WAL contains a `kSetCompressionType` record
|
|
|
|
// which is not associated with any sequence number. So the sequence_number is
|
|
|
|
// set to 1 if that WAL doesn't include any other record (basically empty) in
|
|
|
|
// order to include that WAL and is inserted in read_first_record_cache_.
|
|
|
|
// Therefore, sequence_number is used as boolean if WAL should be included or
|
|
|
|
// not and that sequence_number shouldn't be use for any other purpose.
|
2014-10-30 00:43:37 +00:00
|
|
|
Status ReadFirstRecord(const WalFileType type, const uint64_t number,
|
|
|
|
SequenceNumber* sequence);
|
|
|
|
|
2022-06-08 21:16:43 +00:00
|
|
|
// In case of no wal_compression, ReadFirstLine returns status.ok() and
|
|
|
|
// sequence == 0 if the file exists, but is empty.
|
|
|
|
// In case of wal_compression, WAL contains
|
|
|
|
// `kSetCompressionType` record which is not associated with any sequence
|
|
|
|
// number if that WAL doesn't include any other record (basically empty). As
|
|
|
|
// result for an empty file, GetSortedWalsOfType() will skip these WALs
|
|
|
|
// causing the operations to fail. To avoid that, it sets sequence_number to
|
|
|
|
// 1 inorder to include that WAL.
|
2016-09-15 16:55:02 +00:00
|
|
|
Status ReadFirstLine(const std::string& fname, const uint64_t number,
|
|
|
|
SequenceNumber* sequence);
|
2014-10-30 00:43:37 +00:00
|
|
|
|
|
|
|
// ------- state from DBImpl ------
|
2016-09-23 23:34:04 +00:00
|
|
|
const ImmutableDBOptions& db_options_;
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 22:47:08 +00:00
|
|
|
const FileOptions file_options_;
|
2014-10-30 00:43:37 +00:00
|
|
|
Env* env_;
|
2020-08-13 00:28:10 +00:00
|
|
|
const FileSystemPtr fs_;
|
2014-10-30 00:43:37 +00:00
|
|
|
|
|
|
|
// ------- WalManager state -------
|
|
|
|
// cache for ReadFirstRecord() calls
|
|
|
|
std::unordered_map<uint64_t, SequenceNumber> read_first_record_cache_;
|
|
|
|
port::Mutex read_first_record_cache_mutex_;
|
|
|
|
|
|
|
|
// last time when PurgeObsoleteWALFiles ran.
|
|
|
|
uint64_t purge_wal_files_last_run_;
|
|
|
|
|
2017-11-11 01:18:01 +00:00
|
|
|
bool seq_per_batch_;
|
|
|
|
|
2021-07-30 19:15:04 +00:00
|
|
|
const std::string& wal_dir_;
|
|
|
|
|
2019-07-07 04:04:22 +00:00
|
|
|
bool wal_in_db_path_;
|
|
|
|
|
2014-10-30 00:43:37 +00:00
|
|
|
// obsolete files will be deleted every this seconds if ttl deletion is
|
|
|
|
// enabled and archive size_limit is disabled.
|
2022-04-15 00:12:16 +00:00
|
|
|
static constexpr uint64_t kDefaultIntervalToDeleteObsoleteWAL = 600;
|
2020-08-13 00:28:10 +00:00
|
|
|
|
|
|
|
std::shared_ptr<IOTracer> io_tracer_;
|
2014-10-30 00:43:37 +00:00
|
|
|
};
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|