2017-10-13 21:36:36 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// 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).
|
|
|
|
#pragma once
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
|
|
|
#include <atomic>
|
2019-11-26 21:16:39 +00:00
|
|
|
#include <limits>
|
2017-10-13 21:36:36 +00:00
|
|
|
#include <memory>
|
2019-11-11 22:00:25 +00:00
|
|
|
#include <unordered_set>
|
2017-10-13 21:36:36 +00:00
|
|
|
|
2019-09-16 17:31:27 +00:00
|
|
|
#include "file/random_access_file_reader.h"
|
2017-10-13 21:36:36 +00:00
|
|
|
#include "port/port.h"
|
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "utilities/blob_db/blob_log_format.h"
|
2017-10-27 20:14:34 +00:00
|
|
|
#include "utilities/blob_db/blob_log_reader.h"
|
2017-10-13 21:36:36 +00:00
|
|
|
#include "utilities/blob_db/blob_log_writer.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
namespace blob_db {
|
|
|
|
|
2017-10-27 20:14:34 +00:00
|
|
|
class BlobDBImpl;
|
|
|
|
|
2017-10-13 21:36:36 +00:00
|
|
|
class BlobFile {
|
|
|
|
friend class BlobDBImpl;
|
2018-06-26 05:32:29 +00:00
|
|
|
friend struct BlobFileComparator;
|
|
|
|
friend struct BlobFileComparatorTTL;
|
2017-10-13 21:36:36 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// access to parent
|
2019-11-26 21:16:39 +00:00
|
|
|
const BlobDBImpl* parent_{nullptr};
|
2017-10-13 21:36:36 +00:00
|
|
|
|
|
|
|
// path to blob directory
|
|
|
|
std::string path_to_dir_;
|
|
|
|
|
|
|
|
// the id of the file.
|
|
|
|
// the above 2 are created during file creation and never changed
|
|
|
|
// after that
|
2019-11-26 21:16:39 +00:00
|
|
|
uint64_t file_number_{0};
|
2017-10-13 21:36:36 +00:00
|
|
|
|
2019-11-11 22:00:25 +00:00
|
|
|
// The file numbers of the SST files whose oldest blob file reference
|
|
|
|
// points to this blob file.
|
|
|
|
std::unordered_set<uint64_t> linked_sst_files_;
|
|
|
|
|
2017-12-11 20:01:22 +00:00
|
|
|
// Info log.
|
2019-11-26 21:16:39 +00:00
|
|
|
Logger* info_log_{nullptr};
|
2017-12-11 20:01:22 +00:00
|
|
|
|
|
|
|
// Column family id.
|
2019-11-26 21:16:39 +00:00
|
|
|
uint32_t column_family_id_{std::numeric_limits<uint32_t>::max()};
|
2017-10-27 20:14:34 +00:00
|
|
|
|
2017-11-08 01:40:44 +00:00
|
|
|
// Compression type of blobs in the file
|
2019-11-26 21:16:39 +00:00
|
|
|
CompressionType compression_{kNoCompression};
|
2017-11-08 01:40:44 +00:00
|
|
|
|
2017-12-11 20:01:22 +00:00
|
|
|
// If true, the keys in this file all has TTL. Otherwise all keys don't
|
|
|
|
// have TTL.
|
2019-11-26 21:16:39 +00:00
|
|
|
bool has_ttl_{false};
|
|
|
|
|
|
|
|
// TTL range of blobs in the file.
|
|
|
|
ExpirationRange expiration_range_;
|
2017-12-11 20:01:22 +00:00
|
|
|
|
2017-10-13 21:36:36 +00:00
|
|
|
// number of blobs in the file
|
2019-11-26 21:16:39 +00:00
|
|
|
std::atomic<uint64_t> blob_count_{0};
|
2017-10-13 21:36:36 +00:00
|
|
|
|
|
|
|
// size of the file
|
2019-11-26 21:16:39 +00:00
|
|
|
std::atomic<uint64_t> file_size_{0};
|
2017-10-13 21:36:36 +00:00
|
|
|
|
|
|
|
BlobLogHeader header_;
|
|
|
|
|
|
|
|
// closed_ = true implies the file is no more mutable
|
|
|
|
// no more blobs will be appended and the footer has been written out
|
2019-11-26 21:16:39 +00:00
|
|
|
std::atomic<bool> closed_{false};
|
2017-10-13 21:36:36 +00:00
|
|
|
|
2019-11-19 00:28:04 +00:00
|
|
|
// The latest sequence number when the file was closed/made immutable.
|
2019-11-26 21:16:39 +00:00
|
|
|
SequenceNumber immutable_sequence_{0};
|
2019-11-19 00:28:04 +00:00
|
|
|
|
2017-10-13 21:36:36 +00:00
|
|
|
// has a pass of garbage collection successfully finished on this file
|
2017-11-02 22:50:30 +00:00
|
|
|
// obsolete_ still needs to do iterator/snapshot checks
|
2019-11-26 21:16:39 +00:00
|
|
|
std::atomic<bool> obsolete_{false};
|
2017-11-02 22:50:30 +00:00
|
|
|
|
|
|
|
// The last sequence number by the time the file marked as obsolete.
|
|
|
|
// Data in this file is visible to a snapshot taken before the sequence.
|
2019-11-26 21:16:39 +00:00
|
|
|
SequenceNumber obsolete_sequence_{0};
|
2017-10-13 21:36:36 +00:00
|
|
|
|
|
|
|
// Sequential/Append writer for blobs
|
|
|
|
std::shared_ptr<Writer> log_writer_;
|
|
|
|
|
|
|
|
// random access file reader for GET calls
|
|
|
|
std::shared_ptr<RandomAccessFileReader> ra_file_reader_;
|
|
|
|
|
|
|
|
// This Read-Write mutex is per file specific and protects
|
|
|
|
// all the datastructures
|
|
|
|
mutable port::RWMutex mutex_;
|
|
|
|
|
|
|
|
// time when the random access reader was last created.
|
2019-11-26 21:16:39 +00:00
|
|
|
std::atomic<std::int64_t> last_access_{-1};
|
2017-10-13 21:36:36 +00:00
|
|
|
|
|
|
|
// last time file was fsync'd/fdatasyncd
|
2019-11-26 21:16:39 +00:00
|
|
|
std::atomic<uint64_t> last_fsync_{0};
|
2017-10-13 21:36:36 +00:00
|
|
|
|
2019-11-26 21:16:39 +00:00
|
|
|
bool header_valid_{false};
|
2017-10-13 21:36:36 +00:00
|
|
|
|
2019-11-26 21:16:39 +00:00
|
|
|
bool footer_valid_{false};
|
2017-12-11 20:01:22 +00:00
|
|
|
|
2017-10-13 21:36:36 +00:00
|
|
|
public:
|
2019-11-26 21:16:39 +00:00
|
|
|
BlobFile() = default;
|
2017-10-13 21:36:36 +00:00
|
|
|
|
2017-12-11 20:01:22 +00:00
|
|
|
BlobFile(const BlobDBImpl* parent, const std::string& bdir, uint64_t fnum,
|
|
|
|
Logger* info_log);
|
2017-10-13 21:36:36 +00:00
|
|
|
|
2019-11-26 21:16:39 +00:00
|
|
|
BlobFile(const BlobDBImpl* parent, const std::string& bdir, uint64_t fnum,
|
|
|
|
Logger* info_log, uint32_t column_family_id,
|
|
|
|
CompressionType compression, bool has_ttl,
|
|
|
|
const ExpirationRange& expiration_range);
|
|
|
|
|
2017-10-13 21:36:36 +00:00
|
|
|
~BlobFile();
|
|
|
|
|
2019-11-27 21:07:28 +00:00
|
|
|
uint32_t GetColumnFamilyId() const;
|
2017-10-13 21:36:36 +00:00
|
|
|
|
2017-12-11 20:01:22 +00:00
|
|
|
// Returns log file's absolute pathname.
|
2017-10-13 21:36:36 +00:00
|
|
|
std::string PathName() const;
|
|
|
|
|
|
|
|
// Primary identifier for blob file.
|
|
|
|
// once the file is created, this never changes
|
|
|
|
uint64_t BlobFileNumber() const { return file_number_; }
|
|
|
|
|
2019-11-11 22:00:25 +00:00
|
|
|
// Get the set of SST files whose oldest blob file reference points to
|
|
|
|
// this file.
|
|
|
|
const std::unordered_set<uint64_t>& GetLinkedSstFiles() const {
|
|
|
|
return linked_sst_files_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Link an SST file whose oldest blob file reference points to this file.
|
|
|
|
void LinkSstFile(uint64_t sst_file_number) {
|
|
|
|
assert(linked_sst_files_.find(sst_file_number) == linked_sst_files_.end());
|
|
|
|
linked_sst_files_.insert(sst_file_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlink an SST file whose oldest blob file reference points to this file.
|
|
|
|
void UnlinkSstFile(uint64_t sst_file_number) {
|
|
|
|
auto it = linked_sst_files_.find(sst_file_number);
|
|
|
|
assert(it != linked_sst_files_.end());
|
|
|
|
linked_sst_files_.erase(it);
|
|
|
|
}
|
|
|
|
|
2017-10-13 21:36:36 +00:00
|
|
|
// the following functions are atomic, and don't need
|
|
|
|
// read lock
|
|
|
|
uint64_t BlobCount() const {
|
|
|
|
return blob_count_.load(std::memory_order_acquire);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DumpState() const;
|
|
|
|
|
2017-12-11 20:01:22 +00:00
|
|
|
// if the file is not taking any more appends.
|
|
|
|
bool Immutable() const { return closed_.load(); }
|
|
|
|
|
|
|
|
// Mark the file as immutable.
|
|
|
|
// REQUIRES: write lock held, or access from single thread (on DB open).
|
2019-11-19 00:28:04 +00:00
|
|
|
void MarkImmutable(SequenceNumber sequence) {
|
|
|
|
closed_ = true;
|
|
|
|
immutable_sequence_ = sequence;
|
|
|
|
}
|
|
|
|
|
|
|
|
SequenceNumber GetImmutableSequence() const {
|
|
|
|
assert(Immutable());
|
|
|
|
return immutable_sequence_;
|
|
|
|
}
|
2017-12-11 20:01:22 +00:00
|
|
|
|
2017-10-13 21:36:36 +00:00
|
|
|
// if the file has gone through GC and blobs have been relocated
|
2017-11-02 22:50:30 +00:00
|
|
|
bool Obsolete() const {
|
|
|
|
assert(Immutable() || !obsolete_.load());
|
|
|
|
return obsolete_.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark file as obsolete by garbage collection. The file is not visible to
|
|
|
|
// snapshots with sequence greater or equal to the given sequence.
|
|
|
|
void MarkObsolete(SequenceNumber sequence);
|
|
|
|
|
|
|
|
SequenceNumber GetObsoleteSequence() const {
|
|
|
|
assert(Obsolete());
|
|
|
|
return obsolete_sequence_;
|
|
|
|
}
|
2017-10-13 21:36:36 +00:00
|
|
|
|
|
|
|
// we will assume this is atomic
|
|
|
|
bool NeedsFsync(bool hard, uint64_t bytes_per_sync) const;
|
|
|
|
|
2017-12-11 20:01:22 +00:00
|
|
|
Status Fsync();
|
2017-11-02 22:50:30 +00:00
|
|
|
|
2017-10-13 21:36:36 +00:00
|
|
|
uint64_t GetFileSize() const {
|
|
|
|
return file_size_.load(std::memory_order_acquire);
|
|
|
|
}
|
|
|
|
|
|
|
|
// All Get functions which are not atomic, will need ReadLock on the mutex
|
|
|
|
|
2017-10-27 20:14:34 +00:00
|
|
|
ExpirationRange GetExpirationRange() const { return expiration_range_; }
|
2017-10-13 21:36:36 +00:00
|
|
|
|
2017-10-27 20:14:34 +00:00
|
|
|
void ExtendExpirationRange(uint64_t expiration) {
|
|
|
|
expiration_range_.first = std::min(expiration_range_.first, expiration);
|
|
|
|
expiration_range_.second = std::max(expiration_range_.second, expiration);
|
2017-10-13 21:36:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-27 20:14:34 +00:00
|
|
|
bool HasTTL() const { return has_ttl_; }
|
|
|
|
|
|
|
|
void SetHasTTL(bool has_ttl) { has_ttl_ = has_ttl; }
|
|
|
|
|
2019-11-27 21:07:28 +00:00
|
|
|
CompressionType GetCompressionType() const { return compression_; }
|
2017-11-08 01:40:44 +00:00
|
|
|
|
2017-10-13 21:36:36 +00:00
|
|
|
std::shared_ptr<Writer> GetWriter() const { return log_writer_; }
|
|
|
|
|
2017-12-11 20:01:22 +00:00
|
|
|
// Read blob file header and footer. Return corruption if file header is
|
|
|
|
// malform or incomplete. If footer is malform or incomplete, set
|
|
|
|
// footer_valid_ to false and return Status::OK.
|
|
|
|
Status ReadMetadata(Env* env, const EnvOptions& env_options);
|
|
|
|
|
2018-09-20 23:50:07 +00:00
|
|
|
Status GetReader(Env* env, const EnvOptions& env_options,
|
|
|
|
std::shared_ptr<RandomAccessFileReader>* reader,
|
|
|
|
bool* fresh_open);
|
|
|
|
|
2017-10-13 21:36:36 +00:00
|
|
|
private:
|
2018-06-24 06:08:33 +00:00
|
|
|
std::shared_ptr<Reader> OpenRandomAccessReader(
|
2017-10-13 21:36:36 +00:00
|
|
|
Env* env, const DBOptions& db_options,
|
|
|
|
const EnvOptions& env_options) const;
|
|
|
|
|
|
|
|
Status ReadFooter(BlobLogFooter* footer);
|
|
|
|
|
2019-11-19 00:28:04 +00:00
|
|
|
Status WriteFooterAndCloseLocked(SequenceNumber sequence);
|
2017-10-13 21:36:36 +00:00
|
|
|
|
|
|
|
void CloseRandomAccessLocked();
|
|
|
|
|
|
|
|
// this is used, when you are reading only the footer of a
|
|
|
|
// previously closed file
|
|
|
|
Status SetFromFooterLocked(const BlobLogFooter& footer);
|
|
|
|
|
2017-10-27 20:14:34 +00:00
|
|
|
void set_expiration_range(const ExpirationRange& expiration_range) {
|
|
|
|
expiration_range_ = expiration_range;
|
|
|
|
}
|
2017-10-13 21:36:36 +00:00
|
|
|
|
|
|
|
// The following functions are atomic, and don't need locks
|
|
|
|
void SetFileSize(uint64_t fs) { file_size_ = fs; }
|
|
|
|
|
|
|
|
void SetBlobCount(uint64_t bc) { blob_count_ = bc; }
|
|
|
|
};
|
|
|
|
} // namespace blob_db
|
|
|
|
} // namespace rocksdb
|
|
|
|
#endif // ROCKSDB_LITE
|