2017-05-10 21:54:35 +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).
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2020-06-09 22:12:59 +00:00
|
|
|
#include "db/blob/blob_log_writer.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
2017-11-28 19:42:28 +00:00
|
|
|
|
2020-06-09 22:12:59 +00:00
|
|
|
#include "db/blob/blob_log_format.h"
|
2019-09-16 17:31:27 +00:00
|
|
|
#include "file/writable_file_writer.h"
|
2017-11-28 19:42:28 +00:00
|
|
|
#include "monitoring/statistics.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
#include "rocksdb/env.h"
|
2020-07-23 00:24:07 +00:00
|
|
|
#include "test_util/sync_point.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
#include "util/coding.h"
|
2017-11-28 19:42:28 +00:00
|
|
|
#include "util/stop_watch.h"
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2020-07-07 00:10:41 +00:00
|
|
|
BlobLogWriter::BlobLogWriter(std::unique_ptr<WritableFileWriter>&& dest,
|
|
|
|
Env* env, Statistics* statistics,
|
2020-07-14 20:50:26 +00:00
|
|
|
uint64_t log_number, bool use_fs, uint64_t boffset)
|
2017-05-10 21:54:35 +00:00
|
|
|
: dest_(std::move(dest)),
|
2017-11-28 19:42:28 +00:00
|
|
|
env_(env),
|
|
|
|
statistics_(statistics),
|
2017-05-10 21:54:35 +00:00
|
|
|
log_number_(log_number),
|
|
|
|
block_offset_(boffset),
|
|
|
|
use_fsync_(use_fs),
|
2017-10-27 20:14:34 +00:00
|
|
|
last_elem_type_(kEtNone) {}
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2020-07-07 00:10:41 +00:00
|
|
|
Status BlobLogWriter::Sync() {
|
2020-07-23 00:24:07 +00:00
|
|
|
TEST_SYNC_POINT("BlobLogWriter::Sync");
|
|
|
|
|
2017-11-28 19:42:28 +00:00
|
|
|
StopWatch sync_sw(env_, statistics_, BLOB_DB_BLOB_FILE_SYNC_MICROS);
|
2017-12-11 20:01:22 +00:00
|
|
|
Status s = dest_->Sync(use_fsync_);
|
2017-11-28 19:42:28 +00:00
|
|
|
RecordTick(statistics_, BLOB_DB_BLOB_FILE_SYNCED);
|
2017-12-11 20:01:22 +00:00
|
|
|
return s;
|
2017-11-28 19:42:28 +00:00
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
|
2020-07-07 00:10:41 +00:00
|
|
|
Status BlobLogWriter::WriteHeader(BlobLogHeader& header) {
|
2017-05-10 21:54:35 +00:00
|
|
|
assert(block_offset_ == 0);
|
|
|
|
assert(last_elem_type_ == kEtNone);
|
|
|
|
std::string str;
|
|
|
|
header.EncodeTo(&str);
|
|
|
|
|
|
|
|
Status s = dest_->Append(Slice(str));
|
|
|
|
if (s.ok()) {
|
|
|
|
block_offset_ += str.size();
|
|
|
|
s = dest_->Flush();
|
|
|
|
}
|
|
|
|
last_elem_type_ = kEtFileHdr;
|
2017-11-28 19:42:28 +00:00
|
|
|
RecordTick(statistics_, BLOB_DB_BLOB_FILE_BYTES_WRITTEN,
|
|
|
|
BlobLogHeader::kSize);
|
2017-05-10 21:54:35 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-07-07 00:10:41 +00:00
|
|
|
Status BlobLogWriter::AppendFooter(BlobLogFooter& footer) {
|
2017-05-10 21:54:35 +00:00
|
|
|
assert(block_offset_ != 0);
|
2017-10-17 19:11:52 +00:00
|
|
|
assert(last_elem_type_ == kEtFileHdr || last_elem_type_ == kEtRecord);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
std::string str;
|
|
|
|
footer.EncodeTo(&str);
|
|
|
|
|
|
|
|
Status s = dest_->Append(Slice(str));
|
|
|
|
if (s.ok()) {
|
|
|
|
block_offset_ += str.size();
|
2020-07-23 00:24:07 +00:00
|
|
|
s = Sync();
|
|
|
|
if (s.ok()) {
|
|
|
|
s = dest_->Close();
|
|
|
|
}
|
2017-05-10 21:54:35 +00:00
|
|
|
dest_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
last_elem_type_ = kEtFileFooter;
|
2017-11-28 19:42:28 +00:00
|
|
|
RecordTick(statistics_, BLOB_DB_BLOB_FILE_BYTES_WRITTEN,
|
|
|
|
BlobLogFooter::kSize);
|
2017-05-10 21:54:35 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-07-07 00:10:41 +00:00
|
|
|
Status BlobLogWriter::AddRecord(const Slice& key, const Slice& val,
|
|
|
|
uint64_t expiration, uint64_t* key_offset,
|
|
|
|
uint64_t* blob_offset) {
|
2017-05-10 21:54:35 +00:00
|
|
|
assert(block_offset_ != 0);
|
2017-10-17 19:11:52 +00:00
|
|
|
assert(last_elem_type_ == kEtFileHdr || last_elem_type_ == kEtRecord);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
std::string buf;
|
2017-10-27 20:14:34 +00:00
|
|
|
ConstructBlobHeader(&buf, key, val, expiration);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
Status s = EmitPhysicalRecord(buf, key, val, key_offset, blob_offset);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-07-07 00:10:41 +00:00
|
|
|
Status BlobLogWriter::AddRecord(const Slice& key, const Slice& val,
|
|
|
|
uint64_t* key_offset, uint64_t* blob_offset) {
|
2017-05-10 21:54:35 +00:00
|
|
|
assert(block_offset_ != 0);
|
2017-10-17 19:11:52 +00:00
|
|
|
assert(last_elem_type_ == kEtFileHdr || last_elem_type_ == kEtRecord);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
std::string buf;
|
2017-10-27 20:14:34 +00:00
|
|
|
ConstructBlobHeader(&buf, key, val, 0);
|
2017-05-10 21:54:35 +00:00
|
|
|
|
|
|
|
Status s = EmitPhysicalRecord(buf, key, val, key_offset, blob_offset);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-07-07 00:10:41 +00:00
|
|
|
void BlobLogWriter::ConstructBlobHeader(std::string* buf, const Slice& key,
|
|
|
|
const Slice& val, uint64_t expiration) {
|
2017-10-27 20:14:34 +00:00
|
|
|
BlobLogRecord record;
|
|
|
|
record.key = key;
|
|
|
|
record.value = val;
|
|
|
|
record.expiration = expiration;
|
|
|
|
record.EncodeHeaderTo(buf);
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 00:10:41 +00:00
|
|
|
Status BlobLogWriter::EmitPhysicalRecord(const std::string& headerbuf,
|
|
|
|
const Slice& key, const Slice& val,
|
|
|
|
uint64_t* key_offset,
|
|
|
|
uint64_t* blob_offset) {
|
2017-11-28 19:42:28 +00:00
|
|
|
StopWatch write_sw(env_, statistics_, BLOB_DB_BLOB_FILE_WRITE_MICROS);
|
2017-05-10 21:54:35 +00:00
|
|
|
Status s = dest_->Append(Slice(headerbuf));
|
|
|
|
if (s.ok()) {
|
|
|
|
s = dest_->Append(key);
|
2017-10-17 19:11:52 +00:00
|
|
|
}
|
|
|
|
if (s.ok()) {
|
|
|
|
s = dest_->Append(val);
|
|
|
|
}
|
|
|
|
if (s.ok()) {
|
|
|
|
s = dest_->Flush();
|
2017-05-10 21:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*key_offset = block_offset_ + BlobLogRecord::kHeaderSize;
|
|
|
|
*blob_offset = *key_offset + key.size();
|
|
|
|
block_offset_ = *blob_offset + val.size();
|
|
|
|
last_elem_type_ = kEtRecord;
|
2017-11-28 19:42:28 +00:00
|
|
|
RecordTick(statistics_, BLOB_DB_BLOB_FILE_BYTES_WRITTEN,
|
|
|
|
BlobLogRecord::kHeaderSize + key.size() + val.size());
|
2017-05-10 21:54:35 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|