2017-04-06 02:14:05 +02: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).
|
2017-04-06 02:14:05 +02: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.
|
|
|
|
#include "db/db_impl.h"
|
|
|
|
|
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
|
|
|
#define __STDC_FORMAT_MACROS
|
|
|
|
#endif
|
|
|
|
#include <inttypes.h>
|
2017-06-23 04:30:39 +02:00
|
|
|
#include "db/event_helpers.h"
|
2017-04-06 04:02:00 +02:00
|
|
|
#include "monitoring/perf_context_imp.h"
|
2017-04-07 05:06:34 +02:00
|
|
|
#include "options/options_helper.h"
|
2017-04-06 02:14:05 +02:00
|
|
|
#include "util/sync_point.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
// Convenience methods
|
|
|
|
Status DBImpl::Put(const WriteOptions& o, ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& key, const Slice& val) {
|
|
|
|
return DB::Put(o, column_family, key, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DBImpl::Merge(const WriteOptions& o, ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& key, const Slice& val) {
|
|
|
|
auto cfh = reinterpret_cast<ColumnFamilyHandleImpl*>(column_family);
|
|
|
|
if (!cfh->cfd()->ioptions()->merge_operator) {
|
|
|
|
return Status::NotSupported("Provide a merge_operator when opening DB");
|
|
|
|
} else {
|
|
|
|
return DB::Merge(o, column_family, key, val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DBImpl::Delete(const WriteOptions& write_options,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key) {
|
|
|
|
return DB::Delete(write_options, column_family, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DBImpl::SingleDelete(const WriteOptions& write_options,
|
|
|
|
ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& key) {
|
|
|
|
return DB::SingleDelete(write_options, column_family, key);
|
|
|
|
}
|
|
|
|
|
2018-03-28 21:01:09 +02:00
|
|
|
void DBImpl::SetRecoverableStatePreReleaseCallback(
|
|
|
|
PreReleaseCallback* callback) {
|
|
|
|
recoverable_state_pre_release_callback_.reset(callback);
|
|
|
|
}
|
|
|
|
|
2017-04-06 02:14:05 +02:00
|
|
|
Status DBImpl::Write(const WriteOptions& write_options, WriteBatch* my_batch) {
|
|
|
|
return WriteImpl(write_options, my_batch, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
Status DBImpl::WriteWithCallback(const WriteOptions& write_options,
|
|
|
|
WriteBatch* my_batch,
|
|
|
|
WriteCallback* callback) {
|
|
|
|
return WriteImpl(write_options, my_batch, callback, nullptr);
|
|
|
|
}
|
|
|
|
#endif // ROCKSDB_LITE
|
|
|
|
|
2017-12-01 08:39:56 +01:00
|
|
|
// The main write queue. This is the only write queue that updates LastSequence.
|
|
|
|
// When using one write queue, the same sequence also indicates the last
|
|
|
|
// published sequence.
|
2017-04-06 02:14:05 +02:00
|
|
|
Status DBImpl::WriteImpl(const WriteOptions& write_options,
|
|
|
|
WriteBatch* my_batch, WriteCallback* callback,
|
|
|
|
uint64_t* log_used, uint64_t log_ref,
|
2017-12-01 08:39:56 +01:00
|
|
|
bool disable_memtable, uint64_t* seq_used,
|
2018-02-06 03:32:54 +01:00
|
|
|
size_t batch_cnt,
|
2017-12-01 08:39:56 +01:00
|
|
|
PreReleaseCallback* pre_release_callback) {
|
2018-02-06 03:32:54 +01:00
|
|
|
assert(!seq_per_batch_ || batch_cnt != 0);
|
2017-04-06 02:14:05 +02:00
|
|
|
if (my_batch == nullptr) {
|
|
|
|
return Status::Corruption("Batch is nullptr!");
|
|
|
|
}
|
2017-10-29 05:56:50 +01:00
|
|
|
if (write_options.sync && write_options.disableWAL) {
|
|
|
|
return Status::InvalidArgument("Sync writes has to enable WAL.");
|
|
|
|
}
|
2017-11-11 02:18:01 +01:00
|
|
|
if (two_write_queues_ && immutable_db_options_.enable_pipelined_write) {
|
2017-06-24 23:06:43 +02:00
|
|
|
return Status::NotSupported(
|
|
|
|
"pipelined_writes is not compatible with concurrent prepares");
|
|
|
|
}
|
2017-09-18 23:36:53 +02:00
|
|
|
if (seq_per_batch_ && immutable_db_options_.enable_pipelined_write) {
|
2018-02-06 03:32:54 +01:00
|
|
|
// TODO(yiwu): update pipeline write with seq_per_batch and batch_cnt
|
2017-09-18 23:36:53 +02:00
|
|
|
return Status::NotSupported(
|
|
|
|
"pipelined_writes is not compatible with seq_per_batch");
|
|
|
|
}
|
2017-11-02 01:23:52 +01:00
|
|
|
// Otherwise IsLatestPersistentState optimization does not make sense
|
|
|
|
assert(!WriteBatchInternal::IsLatestPersistentState(my_batch) ||
|
|
|
|
disable_memtable);
|
2017-04-06 02:14:05 +02:00
|
|
|
|
2017-06-05 23:42:34 +02:00
|
|
|
Status status;
|
|
|
|
if (write_options.low_pri) {
|
|
|
|
status = ThrottleLowPriWritesIfNeeded(write_options, my_batch);
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-11 02:18:01 +01:00
|
|
|
if (two_write_queues_ && disable_memtable) {
|
2017-06-24 23:06:43 +02:00
|
|
|
return WriteImplWALOnly(write_options, my_batch, callback, log_used,
|
2018-02-06 03:32:54 +01:00
|
|
|
log_ref, seq_used, batch_cnt, pre_release_callback);
|
2017-06-24 23:06:43 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 23:24:23 +02:00
|
|
|
if (immutable_db_options_.enable_pipelined_write) {
|
|
|
|
return PipelinedWriteImpl(write_options, my_batch, callback, log_used,
|
2017-08-17 01:49:11 +02:00
|
|
|
log_ref, disable_memtable, seq_used);
|
2017-05-19 23:24:23 +02:00
|
|
|
}
|
|
|
|
|
2017-04-06 02:14:05 +02:00
|
|
|
PERF_TIMER_GUARD(write_pre_and_post_process_time);
|
|
|
|
WriteThread::Writer w(write_options, my_batch, callback, log_ref,
|
2018-02-06 03:32:54 +01:00
|
|
|
disable_memtable, batch_cnt, pre_release_callback);
|
2017-04-06 02:14:05 +02:00
|
|
|
|
|
|
|
if (!write_options.disableWAL) {
|
|
|
|
RecordTick(stats_, WRITE_WITH_WAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
StopWatch write_sw(env_, immutable_db_options_.statistics.get(), DB_WRITE);
|
|
|
|
|
|
|
|
write_thread_.JoinBatchGroup(&w);
|
2017-05-19 23:24:23 +02:00
|
|
|
if (w.state == WriteThread::STATE_PARALLEL_MEMTABLE_WRITER) {
|
2017-04-06 02:14:05 +02:00
|
|
|
// we are a non-leader in a parallel group
|
|
|
|
|
|
|
|
if (w.ShouldWriteToMemtable()) {
|
2018-04-24 02:53:27 +02:00
|
|
|
PERF_TIMER_STOP(write_pre_and_post_process_time);
|
|
|
|
PERF_TIMER_GUARD(write_memtable_time);
|
|
|
|
|
2017-04-06 02:14:05 +02:00
|
|
|
ColumnFamilyMemTablesImpl column_family_memtables(
|
|
|
|
versions_->GetColumnFamilySet());
|
|
|
|
w.status = WriteBatchInternal::InsertInto(
|
2017-05-31 19:45:47 +02:00
|
|
|
&w, w.sequence, &column_family_memtables, &flush_scheduler_,
|
2017-04-06 02:14:05 +02:00
|
|
|
write_options.ignore_missing_column_families, 0 /*log_number*/, this,
|
2018-02-06 03:32:54 +01:00
|
|
|
true /*concurrent_memtable_writes*/, seq_per_batch_, w.batch_cnt);
|
2018-04-24 02:53:27 +02:00
|
|
|
|
|
|
|
PERF_TIMER_START(write_pre_and_post_process_time);
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 23:24:23 +02:00
|
|
|
if (write_thread_.CompleteParallelMemTableWriter(&w)) {
|
2017-04-13 23:46:25 +02:00
|
|
|
// we're responsible for exit batch group
|
2017-12-01 08:39:56 +01:00
|
|
|
for (auto* writer : *(w.write_group)) {
|
|
|
|
if (!writer->CallbackFailed() && writer->pre_release_callback) {
|
|
|
|
assert(writer->sequence != kMaxSequenceNumber);
|
2018-03-22 22:27:44 +01:00
|
|
|
Status ws = writer->pre_release_callback->Callback(writer->sequence,
|
|
|
|
disable_memtable);
|
2017-12-01 08:39:56 +01:00
|
|
|
if (!ws.ok()) {
|
|
|
|
status = ws;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO(myabandeh): propagate status to write_group
|
2017-05-19 23:24:23 +02:00
|
|
|
auto last_sequence = w.write_group->last_sequence;
|
2017-04-06 02:14:05 +02:00
|
|
|
versions_->SetLastSequence(last_sequence);
|
2017-05-19 23:24:23 +02:00
|
|
|
MemTableInsertStatusCheck(w.status);
|
2017-04-13 23:46:25 +02:00
|
|
|
write_thread_.ExitAsBatchGroupFollower(&w);
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
assert(w.state == WriteThread::STATE_COMPLETED);
|
|
|
|
// STATE_COMPLETED conditional below handles exit
|
|
|
|
|
|
|
|
status = w.FinalStatus();
|
|
|
|
}
|
|
|
|
if (w.state == WriteThread::STATE_COMPLETED) {
|
|
|
|
if (log_used != nullptr) {
|
|
|
|
*log_used = w.log_used;
|
|
|
|
}
|
2017-08-17 01:49:11 +02:00
|
|
|
if (seq_used != nullptr) {
|
|
|
|
*seq_used = w.sequence;
|
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
// write is complete and leader has updated sequence
|
|
|
|
return w.FinalStatus();
|
|
|
|
}
|
|
|
|
// else we are the leader of the write batch group
|
|
|
|
assert(w.state == WriteThread::STATE_GROUP_LEADER);
|
|
|
|
|
|
|
|
// Once reaches this point, the current writer "w" will try to do its write
|
|
|
|
// job. It may also pick up some of the remaining writers in the "writers_"
|
|
|
|
// when it finds suitable, and finish them in the same write batch.
|
|
|
|
// This is how a write job could be done by the other writer.
|
|
|
|
WriteContext write_context;
|
2017-05-19 23:24:23 +02:00
|
|
|
WriteThread::WriteGroup write_group;
|
2017-04-13 23:46:25 +02:00
|
|
|
bool in_parallel_group = false;
|
2017-06-29 00:36:11 +02:00
|
|
|
uint64_t last_sequence = kMaxSequenceNumber;
|
2017-11-11 02:18:01 +01:00
|
|
|
if (!two_write_queues_) {
|
2017-06-24 23:06:43 +02:00
|
|
|
last_sequence = versions_->LastSequence();
|
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
|
|
|
|
mutex_.Lock();
|
|
|
|
|
2017-10-29 05:56:50 +01:00
|
|
|
bool need_log_sync = write_options.sync;
|
2017-04-06 02:14:05 +02:00
|
|
|
bool need_log_dir_sync = need_log_sync && !log_dir_synced_;
|
2017-11-11 02:18:01 +01:00
|
|
|
if (!two_write_queues_ || !disable_memtable) {
|
2017-06-24 23:06:43 +02:00
|
|
|
// With concurrent writes we do preprocess only in the write thread that
|
|
|
|
// also does write to memtable to avoid sync issue on shared data structure
|
|
|
|
// with the other thread
|
2018-04-24 02:53:27 +02:00
|
|
|
|
|
|
|
// PreprocessWrite does its own perf timing.
|
|
|
|
PERF_TIMER_STOP(write_pre_and_post_process_time);
|
|
|
|
|
2017-06-24 23:06:43 +02:00
|
|
|
status = PreprocessWrite(write_options, &need_log_sync, &write_context);
|
2018-04-24 02:53:27 +02:00
|
|
|
|
|
|
|
PERF_TIMER_START(write_pre_and_post_process_time);
|
2017-06-24 23:06:43 +02:00
|
|
|
}
|
|
|
|
log::Writer* log_writer = logs_.back().writer;
|
2017-04-06 02:14:05 +02:00
|
|
|
|
|
|
|
mutex_.Unlock();
|
|
|
|
|
|
|
|
// Add to log and apply to memtable. We can release the lock
|
|
|
|
// during this phase since &w is currently responsible for logging
|
|
|
|
// and protects against concurrent loggers and concurrent writes
|
|
|
|
// into memtables
|
|
|
|
|
2018-05-22 20:58:51 +02:00
|
|
|
TEST_SYNC_POINT("DBImpl::WriteImpl:BeforeLeaderEnters");
|
2017-04-06 02:14:05 +02:00
|
|
|
last_batch_group_size_ =
|
2017-05-19 23:24:23 +02:00
|
|
|
write_thread_.EnterAsBatchGroupLeader(&w, &write_group);
|
2017-04-06 02:14:05 +02:00
|
|
|
|
|
|
|
if (status.ok()) {
|
|
|
|
// Rules for when we can update the memtable concurrently
|
|
|
|
// 1. supported by memtable
|
|
|
|
// 2. Puts are not okay if inplace_update_support
|
2017-04-10 21:37:07 +02:00
|
|
|
// 3. Merges are not okay
|
2017-04-06 02:14:05 +02:00
|
|
|
//
|
2017-04-10 21:37:07 +02:00
|
|
|
// Rules 1..2 are enforced by checking the options
|
2017-04-06 02:14:05 +02:00
|
|
|
// during startup (CheckConcurrentWritesSupported), so if
|
|
|
|
// options.allow_concurrent_memtable_write is true then they can be
|
2017-04-10 21:37:07 +02:00
|
|
|
// assumed to be true. Rule 3 is checked for each batch. We could
|
|
|
|
// relax rules 2 if we could prevent write batches from referring
|
2017-04-06 02:14:05 +02:00
|
|
|
// more than once to a particular key.
|
|
|
|
bool parallel = immutable_db_options_.allow_concurrent_memtable_write &&
|
2017-05-19 23:24:23 +02:00
|
|
|
write_group.size > 1;
|
2017-09-18 23:36:53 +02:00
|
|
|
size_t total_count = 0;
|
2017-11-15 17:22:54 +01:00
|
|
|
size_t valid_batches = 0;
|
2018-03-06 21:27:07 +01:00
|
|
|
size_t total_byte_size = 0;
|
2017-05-19 23:24:23 +02:00
|
|
|
for (auto* writer : write_group) {
|
2017-04-06 02:14:05 +02:00
|
|
|
if (writer->CheckCallback(this)) {
|
2018-02-06 03:32:54 +01:00
|
|
|
valid_batches += writer->batch_cnt;
|
2017-04-06 02:14:05 +02:00
|
|
|
if (writer->ShouldWriteToMemtable()) {
|
|
|
|
total_count += WriteBatchInternal::Count(writer->batch);
|
|
|
|
parallel = parallel && !writer->batch->HasMerge();
|
|
|
|
}
|
|
|
|
|
2017-04-13 20:58:44 +02:00
|
|
|
total_byte_size = WriteBatchInternal::AppendedByteSize(
|
|
|
|
total_byte_size, WriteBatchInternal::ByteSize(writer->batch));
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-15 17:22:54 +01:00
|
|
|
// Note about seq_per_batch_: either disableWAL is set for the entire write
|
|
|
|
// group or not. In either case we inc seq for each write batch with no
|
|
|
|
// failed callback. This means that there could be a batch with
|
|
|
|
// disalbe_memtable in between; although we do not write this batch to
|
|
|
|
// memtable it still consumes a seq. Otherwise, if !seq_per_batch_, we inc
|
|
|
|
// the seq per valid written key to mem.
|
|
|
|
size_t seq_inc = seq_per_batch_ ? valid_batches : total_count;
|
2017-04-06 02:14:05 +02:00
|
|
|
|
2017-11-11 02:18:01 +01:00
|
|
|
const bool concurrent_update = two_write_queues_;
|
2017-04-06 02:14:05 +02:00
|
|
|
// Update stats while we are an exclusive group leader, so we know
|
|
|
|
// that nobody else can be writing to these particular stats.
|
|
|
|
// We're optimistic, updating the stats before we successfully
|
2017-04-13 23:46:25 +02:00
|
|
|
// commit. That lets us release our leader status early.
|
2017-04-06 02:14:05 +02:00
|
|
|
auto stats = default_cf_internal_stats_;
|
2017-06-30 01:57:13 +02:00
|
|
|
stats->AddDBStats(InternalStats::NUMBER_KEYS_WRITTEN, total_count,
|
|
|
|
concurrent_update);
|
2017-04-06 02:14:05 +02:00
|
|
|
RecordTick(stats_, NUMBER_KEYS_WRITTEN, total_count);
|
2017-06-30 01:57:13 +02:00
|
|
|
stats->AddDBStats(InternalStats::BYTES_WRITTEN, total_byte_size,
|
|
|
|
concurrent_update);
|
2017-04-06 02:14:05 +02:00
|
|
|
RecordTick(stats_, BYTES_WRITTEN, total_byte_size);
|
2017-06-30 01:57:13 +02:00
|
|
|
stats->AddDBStats(InternalStats::WRITE_DONE_BY_SELF, 1, concurrent_update);
|
2017-04-06 02:14:05 +02:00
|
|
|
RecordTick(stats_, WRITE_DONE_BY_SELF);
|
2017-05-19 23:24:23 +02:00
|
|
|
auto write_done_by_other = write_group.size - 1;
|
2017-04-06 02:14:05 +02:00
|
|
|
if (write_done_by_other > 0) {
|
2017-06-30 01:57:13 +02:00
|
|
|
stats->AddDBStats(InternalStats::WRITE_DONE_BY_OTHER, write_done_by_other,
|
|
|
|
concurrent_update);
|
2017-04-06 02:14:05 +02:00
|
|
|
RecordTick(stats_, WRITE_DONE_BY_OTHER, write_done_by_other);
|
|
|
|
}
|
|
|
|
MeasureTime(stats_, BYTES_PER_WRITE, total_byte_size);
|
|
|
|
|
|
|
|
if (write_options.disableWAL) {
|
|
|
|
has_unpersisted_data_.store(true, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
PERF_TIMER_STOP(write_pre_and_post_process_time);
|
|
|
|
|
2017-11-11 02:18:01 +01:00
|
|
|
if (!two_write_queues_) {
|
2017-06-24 23:06:43 +02:00
|
|
|
if (status.ok() && !write_options.disableWAL) {
|
|
|
|
PERF_TIMER_GUARD(write_wal_time);
|
|
|
|
status = WriteToWAL(write_group, log_writer, log_used, need_log_sync,
|
|
|
|
need_log_dir_sync, last_sequence + 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (status.ok() && !write_options.disableWAL) {
|
|
|
|
PERF_TIMER_GUARD(write_wal_time);
|
2017-11-11 02:18:01 +01:00
|
|
|
// LastAllocatedSequence is increased inside WriteToWAL under
|
2017-06-24 23:06:43 +02:00
|
|
|
// wal_write_mutex_ to ensure ordered events in WAL
|
|
|
|
status = ConcurrentWriteToWAL(write_group, log_used, &last_sequence,
|
2017-09-18 23:36:53 +02:00
|
|
|
seq_inc);
|
2017-06-24 23:06:43 +02:00
|
|
|
} else {
|
|
|
|
// Otherwise we inc seq number for memtable writes
|
2017-11-11 02:18:01 +01:00
|
|
|
last_sequence = versions_->FetchAddLastAllocatedSequence(seq_inc);
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-29 00:36:11 +02:00
|
|
|
assert(last_sequence != kMaxSequenceNumber);
|
2017-06-24 23:06:43 +02:00
|
|
|
const SequenceNumber current_sequence = last_sequence + 1;
|
2017-09-18 23:36:53 +02:00
|
|
|
last_sequence += seq_inc;
|
2017-04-06 02:14:05 +02:00
|
|
|
|
|
|
|
if (status.ok()) {
|
|
|
|
PERF_TIMER_GUARD(write_memtable_time);
|
|
|
|
|
|
|
|
if (!parallel) {
|
2017-11-15 17:22:54 +01:00
|
|
|
// w.sequence will be set inside InsertInto
|
2017-04-13 23:46:25 +02:00
|
|
|
w.status = WriteBatchInternal::InsertInto(
|
2017-04-06 02:14:05 +02:00
|
|
|
write_group, current_sequence, column_family_memtables_.get(),
|
|
|
|
&flush_scheduler_, write_options.ignore_missing_column_families,
|
2017-09-18 23:36:53 +02:00
|
|
|
0 /*recovery_log_number*/, this, parallel, seq_per_batch_);
|
2017-04-06 02:14:05 +02:00
|
|
|
} else {
|
2017-05-19 23:24:23 +02:00
|
|
|
SequenceNumber next_sequence = current_sequence;
|
2017-11-15 17:22:54 +01:00
|
|
|
// Note: the logic for advancing seq here must be consistent with the
|
|
|
|
// logic in WriteBatchInternal::InsertInto(write_group...) as well as
|
|
|
|
// with WriteBatchInternal::InsertInto(write_batch...) that is called on
|
|
|
|
// the merged batch during recovery from the WAL.
|
2017-05-19 23:24:23 +02:00
|
|
|
for (auto* writer : write_group) {
|
2017-11-15 17:22:54 +01:00
|
|
|
if (writer->CallbackFailed()) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-29 01:43:04 +02:00
|
|
|
writer->sequence = next_sequence;
|
2017-09-18 23:36:53 +02:00
|
|
|
if (seq_per_batch_) {
|
2018-02-06 03:32:54 +01:00
|
|
|
assert(writer->batch_cnt);
|
|
|
|
next_sequence += writer->batch_cnt;
|
2017-09-18 23:36:53 +02:00
|
|
|
} else if (writer->ShouldWriteToMemtable()) {
|
2017-05-19 23:24:23 +02:00
|
|
|
next_sequence += WriteBatchInternal::Count(writer->batch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write_group.last_sequence = last_sequence;
|
|
|
|
write_group.running.store(static_cast<uint32_t>(write_group.size),
|
|
|
|
std::memory_order_relaxed);
|
|
|
|
write_thread_.LaunchParallelMemTableWriters(&write_group);
|
2017-04-13 23:46:25 +02:00
|
|
|
in_parallel_group = true;
|
2017-04-06 02:14:05 +02:00
|
|
|
|
2017-04-10 21:37:07 +02:00
|
|
|
// Each parallel follower is doing each own writes. The leader should
|
|
|
|
// also do its own.
|
2017-04-06 02:14:05 +02:00
|
|
|
if (w.ShouldWriteToMemtable()) {
|
|
|
|
ColumnFamilyMemTablesImpl column_family_memtables(
|
|
|
|
versions_->GetColumnFamilySet());
|
|
|
|
assert(w.sequence == current_sequence);
|
|
|
|
w.status = WriteBatchInternal::InsertInto(
|
2017-05-31 19:45:47 +02:00
|
|
|
&w, w.sequence, &column_family_memtables, &flush_scheduler_,
|
2017-04-06 02:14:05 +02:00
|
|
|
write_options.ignore_missing_column_families, 0 /*log_number*/,
|
2018-02-06 03:32:54 +01:00
|
|
|
this, true /*concurrent_memtable_writes*/, seq_per_batch_,
|
|
|
|
w.batch_cnt);
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
2017-09-18 23:36:53 +02:00
|
|
|
}
|
|
|
|
if (seq_used != nullptr) {
|
|
|
|
*seq_used = w.sequence;
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PERF_TIMER_START(write_pre_and_post_process_time);
|
|
|
|
|
2017-05-19 23:24:23 +02:00
|
|
|
if (!w.CallbackFailed()) {
|
2018-03-22 23:56:52 +01:00
|
|
|
WriteStatusCheck(status);
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 23:24:23 +02:00
|
|
|
if (need_log_sync) {
|
2017-04-06 02:14:05 +02:00
|
|
|
mutex_.Lock();
|
|
|
|
MarkLogsSynced(logfile_number_, need_log_dir_sync, status);
|
|
|
|
mutex_.Unlock();
|
2017-11-11 02:18:01 +01:00
|
|
|
// Requesting sync with two_write_queues_ is expected to be very rare. We
|
2018-04-24 02:53:27 +02:00
|
|
|
// hence provide a simple implementation that is not necessarily efficient.
|
2017-11-11 02:18:01 +01:00
|
|
|
if (two_write_queues_) {
|
2017-06-24 23:06:43 +02:00
|
|
|
if (manual_wal_flush_) {
|
|
|
|
status = FlushWAL(true);
|
|
|
|
} else {
|
|
|
|
status = SyncWAL();
|
|
|
|
}
|
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 23:46:25 +02:00
|
|
|
bool should_exit_batch_group = true;
|
|
|
|
if (in_parallel_group) {
|
|
|
|
// CompleteParallelWorker returns true if this thread should
|
|
|
|
// handle exit, false means somebody else did
|
2017-05-19 23:24:23 +02:00
|
|
|
should_exit_batch_group = write_thread_.CompleteParallelMemTableWriter(&w);
|
2017-04-13 23:46:25 +02:00
|
|
|
}
|
|
|
|
if (should_exit_batch_group) {
|
2017-06-24 23:06:43 +02:00
|
|
|
if (status.ok()) {
|
2017-12-01 08:39:56 +01:00
|
|
|
for (auto* writer : write_group) {
|
|
|
|
if (!writer->CallbackFailed() && writer->pre_release_callback) {
|
|
|
|
assert(writer->sequence != kMaxSequenceNumber);
|
2018-03-22 22:27:44 +01:00
|
|
|
Status ws = writer->pre_release_callback->Callback(writer->sequence,
|
|
|
|
disable_memtable);
|
2017-12-01 08:39:56 +01:00
|
|
|
if (!ws.ok()) {
|
|
|
|
status = ws;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-24 23:06:43 +02:00
|
|
|
versions_->SetLastSequence(last_sequence);
|
|
|
|
}
|
2017-05-19 23:24:23 +02:00
|
|
|
MemTableInsertStatusCheck(w.status);
|
2017-11-28 20:40:40 +01:00
|
|
|
write_thread_.ExitAsBatchGroupLeader(write_group, status);
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 23:46:25 +02:00
|
|
|
if (status.ok()) {
|
|
|
|
status = w.FinalStatus();
|
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:24:23 +02:00
|
|
|
Status DBImpl::PipelinedWriteImpl(const WriteOptions& write_options,
|
|
|
|
WriteBatch* my_batch, WriteCallback* callback,
|
|
|
|
uint64_t* log_used, uint64_t log_ref,
|
2017-08-17 01:49:11 +02:00
|
|
|
bool disable_memtable, uint64_t* seq_used) {
|
2017-05-19 23:24:23 +02:00
|
|
|
PERF_TIMER_GUARD(write_pre_and_post_process_time);
|
|
|
|
StopWatch write_sw(env_, immutable_db_options_.statistics.get(), DB_WRITE);
|
|
|
|
|
|
|
|
WriteContext write_context;
|
|
|
|
|
|
|
|
WriteThread::Writer w(write_options, my_batch, callback, log_ref,
|
|
|
|
disable_memtable);
|
|
|
|
write_thread_.JoinBatchGroup(&w);
|
|
|
|
if (w.state == WriteThread::STATE_GROUP_LEADER) {
|
|
|
|
WriteThread::WriteGroup wal_write_group;
|
|
|
|
if (w.callback && !w.callback->AllowWriteBatching()) {
|
|
|
|
write_thread_.WaitForMemTableWriters();
|
|
|
|
}
|
|
|
|
mutex_.Lock();
|
|
|
|
bool need_log_sync = !write_options.disableWAL && write_options.sync;
|
|
|
|
bool need_log_dir_sync = need_log_sync && !log_dir_synced_;
|
2018-04-24 02:53:27 +02:00
|
|
|
// PreprocessWrite does its own perf timing.
|
|
|
|
PERF_TIMER_STOP(write_pre_and_post_process_time);
|
2017-05-19 23:24:23 +02:00
|
|
|
w.status = PreprocessWrite(write_options, &need_log_sync, &write_context);
|
2018-04-24 02:53:27 +02:00
|
|
|
PERF_TIMER_START(write_pre_and_post_process_time);
|
2017-06-24 23:06:43 +02:00
|
|
|
log::Writer* log_writer = logs_.back().writer;
|
2017-05-19 23:24:23 +02:00
|
|
|
mutex_.Unlock();
|
|
|
|
|
|
|
|
// This can set non-OK status if callback fail.
|
|
|
|
last_batch_group_size_ =
|
|
|
|
write_thread_.EnterAsBatchGroupLeader(&w, &wal_write_group);
|
|
|
|
const SequenceNumber current_sequence =
|
|
|
|
write_thread_.UpdateLastSequence(versions_->LastSequence()) + 1;
|
|
|
|
size_t total_count = 0;
|
|
|
|
size_t total_byte_size = 0;
|
|
|
|
|
|
|
|
if (w.status.ok()) {
|
|
|
|
SequenceNumber next_sequence = current_sequence;
|
|
|
|
for (auto writer : wal_write_group) {
|
|
|
|
if (writer->CheckCallback(this)) {
|
|
|
|
if (writer->ShouldWriteToMemtable()) {
|
|
|
|
writer->sequence = next_sequence;
|
|
|
|
size_t count = WriteBatchInternal::Count(writer->batch);
|
|
|
|
next_sequence += count;
|
|
|
|
total_count += count;
|
|
|
|
}
|
|
|
|
total_byte_size = WriteBatchInternal::AppendedByteSize(
|
|
|
|
total_byte_size, WriteBatchInternal::ByteSize(writer->batch));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (w.disable_wal) {
|
|
|
|
has_unpersisted_data_.store(true, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
write_thread_.UpdateLastSequence(current_sequence + total_count - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto stats = default_cf_internal_stats_;
|
|
|
|
stats->AddDBStats(InternalStats::NUMBER_KEYS_WRITTEN, total_count);
|
|
|
|
RecordTick(stats_, NUMBER_KEYS_WRITTEN, total_count);
|
|
|
|
stats->AddDBStats(InternalStats::BYTES_WRITTEN, total_byte_size);
|
|
|
|
RecordTick(stats_, BYTES_WRITTEN, total_byte_size);
|
2017-09-11 20:31:47 +02:00
|
|
|
MeasureTime(stats_, BYTES_PER_WRITE, total_byte_size);
|
2017-05-19 23:24:23 +02:00
|
|
|
|
|
|
|
PERF_TIMER_STOP(write_pre_and_post_process_time);
|
|
|
|
|
|
|
|
if (w.ShouldWriteToWAL()) {
|
|
|
|
PERF_TIMER_GUARD(write_wal_time);
|
|
|
|
stats->AddDBStats(InternalStats::WRITE_DONE_BY_SELF, 1);
|
|
|
|
RecordTick(stats_, WRITE_DONE_BY_SELF, 1);
|
|
|
|
if (wal_write_group.size > 1) {
|
|
|
|
stats->AddDBStats(InternalStats::WRITE_DONE_BY_OTHER,
|
|
|
|
wal_write_group.size - 1);
|
|
|
|
RecordTick(stats_, WRITE_DONE_BY_OTHER, wal_write_group.size - 1);
|
|
|
|
}
|
2017-06-24 23:06:43 +02:00
|
|
|
w.status = WriteToWAL(wal_write_group, log_writer, log_used,
|
|
|
|
need_log_sync, need_log_dir_sync, current_sequence);
|
2017-05-19 23:24:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!w.CallbackFailed()) {
|
2018-03-22 23:56:52 +01:00
|
|
|
WriteStatusCheck(w.status);
|
2017-05-19 23:24:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (need_log_sync) {
|
|
|
|
mutex_.Lock();
|
|
|
|
MarkLogsSynced(logfile_number_, need_log_dir_sync, w.status);
|
|
|
|
mutex_.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
write_thread_.ExitAsBatchGroupLeader(wal_write_group, w.status);
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteThread::WriteGroup memtable_write_group;
|
|
|
|
if (w.state == WriteThread::STATE_MEMTABLE_WRITER_LEADER) {
|
|
|
|
PERF_TIMER_GUARD(write_memtable_time);
|
|
|
|
assert(w.status.ok());
|
|
|
|
write_thread_.EnterAsMemTableWriter(&w, &memtable_write_group);
|
|
|
|
if (memtable_write_group.size > 1 &&
|
|
|
|
immutable_db_options_.allow_concurrent_memtable_write) {
|
|
|
|
write_thread_.LaunchParallelMemTableWriters(&memtable_write_group);
|
|
|
|
} else {
|
|
|
|
memtable_write_group.status = WriteBatchInternal::InsertInto(
|
|
|
|
memtable_write_group, w.sequence, column_family_memtables_.get(),
|
|
|
|
&flush_scheduler_, write_options.ignore_missing_column_families,
|
2017-09-18 23:36:53 +02:00
|
|
|
0 /*log_number*/, this, seq_per_batch_);
|
2017-05-19 23:24:23 +02:00
|
|
|
versions_->SetLastSequence(memtable_write_group.last_sequence);
|
|
|
|
write_thread_.ExitAsMemTableWriter(&w, memtable_write_group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (w.state == WriteThread::STATE_PARALLEL_MEMTABLE_WRITER) {
|
|
|
|
assert(w.ShouldWriteToMemtable());
|
|
|
|
ColumnFamilyMemTablesImpl column_family_memtables(
|
|
|
|
versions_->GetColumnFamilySet());
|
|
|
|
w.status = WriteBatchInternal::InsertInto(
|
2017-05-31 19:45:47 +02:00
|
|
|
&w, w.sequence, &column_family_memtables, &flush_scheduler_,
|
2017-05-19 23:24:23 +02:00
|
|
|
write_options.ignore_missing_column_families, 0 /*log_number*/, this,
|
|
|
|
true /*concurrent_memtable_writes*/);
|
|
|
|
if (write_thread_.CompleteParallelMemTableWriter(&w)) {
|
|
|
|
MemTableInsertStatusCheck(w.status);
|
|
|
|
versions_->SetLastSequence(w.write_group->last_sequence);
|
|
|
|
write_thread_.ExitAsMemTableWriter(&w, *w.write_group);
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 01:49:11 +02:00
|
|
|
if (seq_used != nullptr) {
|
|
|
|
*seq_used = w.sequence;
|
|
|
|
}
|
2017-05-19 23:24:23 +02:00
|
|
|
|
|
|
|
assert(w.state == WriteThread::STATE_COMPLETED);
|
2017-06-24 23:06:43 +02:00
|
|
|
return w.FinalStatus();
|
|
|
|
}
|
|
|
|
|
2017-12-01 08:39:56 +01:00
|
|
|
// The 2nd write queue. If enabled it will be used only for WAL-only writes.
|
|
|
|
// This is the only queue that updates LastPublishedSequence which is only
|
|
|
|
// applicable in a two-queue setting.
|
2017-06-24 23:06:43 +02:00
|
|
|
Status DBImpl::WriteImplWALOnly(const WriteOptions& write_options,
|
|
|
|
WriteBatch* my_batch, WriteCallback* callback,
|
2017-08-17 01:49:11 +02:00
|
|
|
uint64_t* log_used, uint64_t log_ref,
|
2018-02-06 03:32:54 +01:00
|
|
|
uint64_t* seq_used, size_t batch_cnt,
|
2017-12-01 08:39:56 +01:00
|
|
|
PreReleaseCallback* pre_release_callback) {
|
2017-06-24 23:06:43 +02:00
|
|
|
Status status;
|
|
|
|
PERF_TIMER_GUARD(write_pre_and_post_process_time);
|
|
|
|
WriteThread::Writer w(write_options, my_batch, callback, log_ref,
|
2018-02-06 03:32:54 +01:00
|
|
|
true /* disable_memtable */, batch_cnt,
|
|
|
|
pre_release_callback);
|
2017-06-24 23:06:43 +02:00
|
|
|
RecordTick(stats_, WRITE_WITH_WAL);
|
|
|
|
StopWatch write_sw(env_, immutable_db_options_.statistics.get(), DB_WRITE);
|
2017-05-19 23:24:23 +02:00
|
|
|
|
2017-06-24 23:06:43 +02:00
|
|
|
nonmem_write_thread_.JoinBatchGroup(&w);
|
|
|
|
assert(w.state != WriteThread::STATE_PARALLEL_MEMTABLE_WRITER);
|
|
|
|
if (w.state == WriteThread::STATE_COMPLETED) {
|
|
|
|
if (log_used != nullptr) {
|
|
|
|
*log_used = w.log_used;
|
|
|
|
}
|
2017-08-17 01:49:11 +02:00
|
|
|
if (seq_used != nullptr) {
|
|
|
|
*seq_used = w.sequence;
|
|
|
|
}
|
2017-06-24 23:06:43 +02:00
|
|
|
return w.FinalStatus();
|
|
|
|
}
|
|
|
|
// else we are the leader of the write batch group
|
|
|
|
assert(w.state == WriteThread::STATE_GROUP_LEADER);
|
|
|
|
WriteContext write_context;
|
|
|
|
WriteThread::WriteGroup write_group;
|
|
|
|
uint64_t last_sequence;
|
|
|
|
nonmem_write_thread_.EnterAsBatchGroupLeader(&w, &write_group);
|
|
|
|
// Note: no need to update last_batch_group_size_ here since the batch writes
|
|
|
|
// to WAL only
|
|
|
|
|
2018-03-06 21:27:07 +01:00
|
|
|
size_t total_byte_size = 0;
|
2017-06-24 23:06:43 +02:00
|
|
|
for (auto* writer : write_group) {
|
|
|
|
if (writer->CheckCallback(this)) {
|
|
|
|
total_byte_size = WriteBatchInternal::AppendedByteSize(
|
|
|
|
total_byte_size, WriteBatchInternal::ByteSize(writer->batch));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-30 01:57:13 +02:00
|
|
|
const bool concurrent_update = true;
|
2017-06-24 23:06:43 +02:00
|
|
|
// Update stats while we are an exclusive group leader, so we know
|
|
|
|
// that nobody else can be writing to these particular stats.
|
|
|
|
// We're optimistic, updating the stats before we successfully
|
|
|
|
// commit. That lets us release our leader status early.
|
|
|
|
auto stats = default_cf_internal_stats_;
|
2017-06-30 01:57:13 +02:00
|
|
|
stats->AddDBStats(InternalStats::BYTES_WRITTEN, total_byte_size,
|
|
|
|
concurrent_update);
|
2017-06-24 23:06:43 +02:00
|
|
|
RecordTick(stats_, BYTES_WRITTEN, total_byte_size);
|
2017-06-30 01:57:13 +02:00
|
|
|
stats->AddDBStats(InternalStats::WRITE_DONE_BY_SELF, 1, concurrent_update);
|
2017-06-24 23:06:43 +02:00
|
|
|
RecordTick(stats_, WRITE_DONE_BY_SELF);
|
|
|
|
auto write_done_by_other = write_group.size - 1;
|
|
|
|
if (write_done_by_other > 0) {
|
2017-06-30 01:57:13 +02:00
|
|
|
stats->AddDBStats(InternalStats::WRITE_DONE_BY_OTHER, write_done_by_other,
|
|
|
|
concurrent_update);
|
2017-06-24 23:06:43 +02:00
|
|
|
RecordTick(stats_, WRITE_DONE_BY_OTHER, write_done_by_other);
|
|
|
|
}
|
|
|
|
MeasureTime(stats_, BYTES_PER_WRITE, total_byte_size);
|
|
|
|
|
|
|
|
PERF_TIMER_STOP(write_pre_and_post_process_time);
|
|
|
|
|
|
|
|
PERF_TIMER_GUARD(write_wal_time);
|
2017-11-11 02:18:01 +01:00
|
|
|
// LastAllocatedSequence is increased inside WriteToWAL under
|
2017-06-24 23:06:43 +02:00
|
|
|
// wal_write_mutex_ to ensure ordered events in WAL
|
2018-02-06 03:32:54 +01:00
|
|
|
size_t seq_inc = 0 /* total_count */;
|
|
|
|
if (seq_per_batch_) {
|
|
|
|
size_t total_batch_cnt = 0;
|
|
|
|
for (auto* writer : write_group) {
|
|
|
|
assert(writer->batch_cnt);
|
|
|
|
total_batch_cnt += writer->batch_cnt;
|
|
|
|
}
|
|
|
|
seq_inc = total_batch_cnt;
|
|
|
|
}
|
2017-12-13 16:55:02 +01:00
|
|
|
if (!write_options.disableWAL) {
|
|
|
|
status =
|
|
|
|
ConcurrentWriteToWAL(write_group, log_used, &last_sequence, seq_inc);
|
|
|
|
} else {
|
|
|
|
// Otherwise we inc seq number to do solely the seq allocation
|
|
|
|
last_sequence = versions_->FetchAddLastAllocatedSequence(seq_inc);
|
|
|
|
}
|
2017-08-17 01:49:11 +02:00
|
|
|
auto curr_seq = last_sequence + 1;
|
|
|
|
for (auto* writer : write_group) {
|
2017-11-15 17:22:54 +01:00
|
|
|
if (writer->CallbackFailed()) {
|
|
|
|
continue;
|
2017-09-18 23:36:53 +02:00
|
|
|
}
|
2017-11-15 17:22:54 +01:00
|
|
|
writer->sequence = curr_seq;
|
2017-09-18 23:36:53 +02:00
|
|
|
if (seq_per_batch_) {
|
2018-02-06 03:32:54 +01:00
|
|
|
assert(writer->batch_cnt);
|
|
|
|
curr_seq += writer->batch_cnt;
|
2017-08-17 01:49:11 +02:00
|
|
|
}
|
2017-11-15 17:22:54 +01:00
|
|
|
// else seq advances only by memtable writes
|
2017-08-17 01:49:11 +02:00
|
|
|
}
|
2017-06-24 23:06:43 +02:00
|
|
|
if (status.ok() && write_options.sync) {
|
2017-12-13 16:55:02 +01:00
|
|
|
assert(!write_options.disableWAL);
|
2017-11-11 02:18:01 +01:00
|
|
|
// Requesting sync with two_write_queues_ is expected to be very rare. We
|
2017-06-24 23:06:43 +02:00
|
|
|
// hance provide a simple implementation that is not necessarily efficient.
|
|
|
|
if (manual_wal_flush_) {
|
|
|
|
status = FlushWAL(true);
|
|
|
|
} else {
|
|
|
|
status = SyncWAL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PERF_TIMER_START(write_pre_and_post_process_time);
|
|
|
|
|
|
|
|
if (!w.CallbackFailed()) {
|
2018-03-22 23:56:52 +01:00
|
|
|
WriteStatusCheck(status);
|
2017-06-24 23:06:43 +02:00
|
|
|
}
|
2017-12-01 08:39:56 +01:00
|
|
|
if (status.ok()) {
|
|
|
|
for (auto* writer : write_group) {
|
|
|
|
if (!writer->CallbackFailed() && writer->pre_release_callback) {
|
|
|
|
assert(writer->sequence != kMaxSequenceNumber);
|
2018-03-22 22:27:44 +01:00
|
|
|
const bool DISABLE_MEMTABLE = true;
|
|
|
|
Status ws = writer->pre_release_callback->Callback(writer->sequence,
|
|
|
|
DISABLE_MEMTABLE);
|
2017-12-01 08:39:56 +01:00
|
|
|
if (!ws.ok()) {
|
|
|
|
status = ws;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-28 20:40:40 +01:00
|
|
|
nonmem_write_thread_.ExitAsBatchGroupLeader(write_group, status);
|
2017-06-24 23:06:43 +02:00
|
|
|
if (status.ok()) {
|
|
|
|
status = w.FinalStatus();
|
|
|
|
}
|
2017-08-17 01:49:11 +02:00
|
|
|
if (seq_used != nullptr) {
|
|
|
|
*seq_used = w.sequence;
|
|
|
|
}
|
2017-06-24 23:06:43 +02:00
|
|
|
return status;
|
2017-05-19 23:24:23 +02:00
|
|
|
}
|
|
|
|
|
2018-03-22 23:56:52 +01:00
|
|
|
void DBImpl::WriteStatusCheck(const Status& status) {
|
2017-05-19 23:24:23 +02:00
|
|
|
// Is setting bg_error_ enough here? This will at least stop
|
|
|
|
// compaction and fail any further writes.
|
|
|
|
if (immutable_db_options_.paranoid_checks && !status.ok() &&
|
|
|
|
!status.IsBusy() && !status.IsIncomplete()) {
|
|
|
|
mutex_.Lock();
|
|
|
|
if (bg_error_.ok()) {
|
2017-06-23 04:30:39 +02:00
|
|
|
Status new_bg_error = status;
|
|
|
|
// may temporarily unlock and lock the mutex.
|
|
|
|
EventHelpers::NotifyOnBackgroundError(immutable_db_options_.listeners,
|
|
|
|
BackgroundErrorReason::kWriteCallback,
|
|
|
|
&new_bg_error, &mutex_);
|
|
|
|
if (!new_bg_error.ok()) {
|
|
|
|
bg_error_ = new_bg_error; // stop compaction & fail any further writes
|
|
|
|
}
|
2017-05-19 23:24:23 +02:00
|
|
|
}
|
|
|
|
mutex_.Unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DBImpl::MemTableInsertStatusCheck(const Status& status) {
|
2017-04-13 23:46:25 +02:00
|
|
|
// A non-OK status here indicates that the state implied by the
|
|
|
|
// WAL has diverged from the in-memory state. This could be
|
|
|
|
// because of a corrupt write_batch (very bad), or because the
|
|
|
|
// client specified an invalid column family and didn't specify
|
|
|
|
// ignore_missing_column_families.
|
2017-05-19 23:24:23 +02:00
|
|
|
if (!status.ok()) {
|
2017-04-13 23:46:25 +02:00
|
|
|
mutex_.Lock();
|
|
|
|
assert(bg_error_.ok());
|
2017-06-23 04:30:39 +02:00
|
|
|
Status new_bg_error = status;
|
|
|
|
// may temporarily unlock and lock the mutex.
|
|
|
|
EventHelpers::NotifyOnBackgroundError(immutable_db_options_.listeners,
|
|
|
|
BackgroundErrorReason::kMemTable,
|
|
|
|
&new_bg_error, &mutex_);
|
|
|
|
if (!new_bg_error.ok()) {
|
|
|
|
bg_error_ = new_bg_error; // stop compaction & fail any further writes
|
|
|
|
}
|
2017-04-13 23:46:25 +02:00
|
|
|
mutex_.Unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 02:14:05 +02:00
|
|
|
Status DBImpl::PreprocessWrite(const WriteOptions& write_options,
|
2017-05-19 23:24:23 +02:00
|
|
|
bool* need_log_sync,
|
2017-04-06 02:14:05 +02:00
|
|
|
WriteContext* write_context) {
|
|
|
|
mutex_.AssertHeld();
|
2017-05-19 23:24:23 +02:00
|
|
|
assert(write_context != nullptr && need_log_sync != nullptr);
|
2017-04-06 02:14:05 +02:00
|
|
|
Status status;
|
|
|
|
|
2018-04-24 02:53:27 +02:00
|
|
|
PERF_TIMER_GUARD(write_scheduling_flushes_compactions_time);
|
|
|
|
|
2017-04-06 02:14:05 +02:00
|
|
|
assert(!single_column_family_mode_ ||
|
|
|
|
versions_->GetColumnFamilySet()->NumberOfColumnFamilies() == 1);
|
|
|
|
if (UNLIKELY(status.ok() && !single_column_family_mode_ &&
|
|
|
|
total_log_size_ > GetMaxTotalWalSize())) {
|
2017-09-28 02:37:08 +02:00
|
|
|
status = SwitchWAL(write_context);
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (UNLIKELY(status.ok() && write_buffer_manager_->ShouldFlush())) {
|
|
|
|
// Before a new memtable is added in SwitchMemtable(),
|
|
|
|
// write_buffer_manager_->ShouldFlush() will keep returning true. If another
|
|
|
|
// thread is writing to another DB with the same write buffer, they may also
|
|
|
|
// be flushed. We may end up with flushing much more DBs than needed. It's
|
|
|
|
// suboptimal but still correct.
|
|
|
|
status = HandleWriteBufferFull(write_context);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UNLIKELY(status.ok() && !bg_error_.ok())) {
|
2017-12-20 17:10:19 +01:00
|
|
|
status = bg_error_;
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (UNLIKELY(status.ok() && !flush_scheduler_.Empty())) {
|
|
|
|
status = ScheduleFlushes(write_context);
|
|
|
|
}
|
|
|
|
|
2018-04-24 02:53:27 +02:00
|
|
|
PERF_TIMER_STOP(write_scheduling_flushes_compactions_time);
|
|
|
|
PERF_TIMER_GUARD(write_pre_and_post_process_time);
|
|
|
|
|
2017-04-06 02:14:05 +02:00
|
|
|
if (UNLIKELY(status.ok() && (write_controller_.IsStopped() ||
|
|
|
|
write_controller_.NeedsDelay()))) {
|
2018-04-24 02:53:27 +02:00
|
|
|
PERF_TIMER_STOP(write_pre_and_post_process_time);
|
2017-04-06 02:14:05 +02:00
|
|
|
PERF_TIMER_GUARD(write_delay_time);
|
|
|
|
// We don't know size of curent batch so that we always use the size
|
|
|
|
// for previous one. It might create a fairness issue that expiration
|
|
|
|
// might happen for smaller writes but larger writes can go through.
|
|
|
|
// Can optimize it if it is an issue.
|
|
|
|
status = DelayWrite(last_batch_group_size_, write_options);
|
2018-04-24 02:53:27 +02:00
|
|
|
PERF_TIMER_START(write_pre_and_post_process_time);
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 23:24:23 +02:00
|
|
|
if (status.ok() && *need_log_sync) {
|
2017-04-10 21:37:07 +02:00
|
|
|
// Wait until the parallel syncs are finished. Any sync process has to sync
|
|
|
|
// the front log too so it is enough to check the status of front()
|
|
|
|
// We do a while loop since log_sync_cv_ is signalled when any sync is
|
|
|
|
// finished
|
|
|
|
// Note: there does not seem to be a reason to wait for parallel sync at
|
|
|
|
// this early step but it is not important since parallel sync (SyncWAL) and
|
|
|
|
// need_log_sync are usually not used together.
|
2017-04-06 02:14:05 +02:00
|
|
|
while (logs_.front().getting_synced) {
|
|
|
|
log_sync_cv_.Wait();
|
|
|
|
}
|
|
|
|
for (auto& log : logs_) {
|
|
|
|
assert(!log.getting_synced);
|
2017-04-10 21:37:07 +02:00
|
|
|
// This is just to prevent the logs to be synced by a parallel SyncWAL
|
|
|
|
// call. We will do the actual syncing later after we will write to the
|
|
|
|
// WAL.
|
|
|
|
// Note: there does not seem to be a reason to set this early before we
|
|
|
|
// actually write to the WAL
|
2017-04-06 02:14:05 +02:00
|
|
|
log.getting_synced = true;
|
|
|
|
}
|
2017-05-19 23:24:23 +02:00
|
|
|
} else {
|
|
|
|
*need_log_sync = false;
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-06-24 23:06:43 +02:00
|
|
|
WriteBatch* DBImpl::MergeBatch(const WriteThread::WriteGroup& write_group,
|
2017-11-02 01:23:52 +01:00
|
|
|
WriteBatch* tmp_batch, size_t* write_with_wal,
|
|
|
|
WriteBatch** to_be_cached_state) {
|
2017-06-24 23:06:43 +02:00
|
|
|
assert(write_with_wal != nullptr);
|
|
|
|
assert(tmp_batch != nullptr);
|
2017-11-02 01:23:52 +01:00
|
|
|
assert(*to_be_cached_state == nullptr);
|
2017-04-06 02:14:05 +02:00
|
|
|
WriteBatch* merged_batch = nullptr;
|
2017-06-24 23:06:43 +02:00
|
|
|
*write_with_wal = 0;
|
2017-05-19 23:24:23 +02:00
|
|
|
auto* leader = write_group.leader;
|
2017-11-15 17:22:54 +01:00
|
|
|
assert(!leader->disable_wal); // Same holds for all in the batch group
|
|
|
|
if (write_group.size == 1 && !leader->CallbackFailed() &&
|
2017-05-19 23:24:23 +02:00
|
|
|
leader->batch->GetWalTerminationPoint().is_cleared()) {
|
2017-04-06 02:14:05 +02:00
|
|
|
// we simply write the first WriteBatch to WAL if the group only
|
|
|
|
// contains one batch, that batch should be written to the WAL,
|
|
|
|
// and the batch is not wanting to be truncated
|
2017-05-19 23:24:23 +02:00
|
|
|
merged_batch = leader->batch;
|
2017-11-02 01:23:52 +01:00
|
|
|
if (WriteBatchInternal::IsLatestPersistentState(merged_batch)) {
|
|
|
|
*to_be_cached_state = merged_batch;
|
|
|
|
}
|
2017-06-24 23:06:43 +02:00
|
|
|
*write_with_wal = 1;
|
2017-04-06 02:14:05 +02:00
|
|
|
} else {
|
|
|
|
// WAL needs all of the batches flattened into a single batch.
|
|
|
|
// We could avoid copying here with an iov-like AddRecord
|
|
|
|
// interface
|
2017-06-24 23:06:43 +02:00
|
|
|
merged_batch = tmp_batch;
|
2017-04-06 02:14:05 +02:00
|
|
|
for (auto writer : write_group) {
|
2017-11-15 17:22:54 +01:00
|
|
|
if (!writer->CallbackFailed()) {
|
2017-04-06 02:14:05 +02:00
|
|
|
WriteBatchInternal::Append(merged_batch, writer->batch,
|
|
|
|
/*WAL_only*/ true);
|
2017-11-02 01:23:52 +01:00
|
|
|
if (WriteBatchInternal::IsLatestPersistentState(writer->batch)) {
|
|
|
|
// We only need to cache the last of such write batch
|
|
|
|
*to_be_cached_state = writer->batch;
|
|
|
|
}
|
2017-06-24 23:06:43 +02:00
|
|
|
(*write_with_wal)++;
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-24 23:06:43 +02:00
|
|
|
return merged_batch;
|
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
|
2017-11-11 02:18:01 +01:00
|
|
|
// When two_write_queues_ is disabled, this function is called from the only
|
2017-06-30 18:30:03 +02:00
|
|
|
// write thread. Otherwise this must be called holding log_write_mutex_.
|
2017-06-24 23:06:43 +02:00
|
|
|
Status DBImpl::WriteToWAL(const WriteBatch& merged_batch,
|
|
|
|
log::Writer* log_writer, uint64_t* log_used,
|
|
|
|
uint64_t* log_size) {
|
|
|
|
assert(log_size != nullptr);
|
|
|
|
Slice log_entry = WriteBatchInternal::Contents(&merged_batch);
|
|
|
|
*log_size = log_entry.size();
|
2018-03-27 01:16:59 +02:00
|
|
|
// When two_write_queues_ WriteToWAL has to be protected from concurretn calls
|
|
|
|
// from the two queues anyway and log_write_mutex_ is already held. Otherwise
|
|
|
|
// if manual_wal_flush_ is enabled we need to protect log_writer->AddRecord
|
|
|
|
// from possible concurrent calls via the FlushWAL by the application.
|
|
|
|
const bool needs_locking = manual_wal_flush_ && !two_write_queues_;
|
|
|
|
// Due to performance cocerns of missed branch prediction penalize the new
|
|
|
|
// manual_wal_flush_ feature (by UNLIKELY) instead of the more common case
|
|
|
|
// when we do not need any locking.
|
|
|
|
if (UNLIKELY(needs_locking)) {
|
|
|
|
log_write_mutex_.Lock();
|
|
|
|
}
|
2017-06-24 23:06:43 +02:00
|
|
|
Status status = log_writer->AddRecord(log_entry);
|
2018-03-27 01:16:59 +02:00
|
|
|
if (UNLIKELY(needs_locking)) {
|
|
|
|
log_write_mutex_.Unlock();
|
|
|
|
}
|
2017-06-24 23:06:43 +02:00
|
|
|
if (log_used != nullptr) {
|
|
|
|
*log_used = logfile_number_;
|
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
total_log_size_ += log_entry.size();
|
2017-06-30 18:30:03 +02:00
|
|
|
// TODO(myabandeh): it might be unsafe to access alive_log_files_.back() here
|
|
|
|
// since alive_log_files_ might be modified concurrently
|
2017-04-06 02:14:05 +02:00
|
|
|
alive_log_files_.back().AddSize(log_entry.size());
|
|
|
|
log_empty_ = false;
|
2017-06-24 23:06:43 +02:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DBImpl::WriteToWAL(const WriteThread::WriteGroup& write_group,
|
|
|
|
log::Writer* log_writer, uint64_t* log_used,
|
|
|
|
bool need_log_sync, bool need_log_dir_sync,
|
|
|
|
SequenceNumber sequence) {
|
|
|
|
Status status;
|
|
|
|
|
2017-11-15 17:22:54 +01:00
|
|
|
assert(!write_group.leader->disable_wal);
|
|
|
|
// Same holds for all in the batch group
|
2017-06-24 23:06:43 +02:00
|
|
|
size_t write_with_wal = 0;
|
2017-11-02 01:23:52 +01:00
|
|
|
WriteBatch* to_be_cached_state = nullptr;
|
|
|
|
WriteBatch* merged_batch = MergeBatch(write_group, &tmp_batch_,
|
|
|
|
&write_with_wal, &to_be_cached_state);
|
2017-06-28 22:05:52 +02:00
|
|
|
if (merged_batch == write_group.leader->batch) {
|
|
|
|
write_group.leader->log_used = logfile_number_;
|
|
|
|
} else if (write_with_wal > 1) {
|
|
|
|
for (auto writer : write_group) {
|
|
|
|
writer->log_used = logfile_number_;
|
|
|
|
}
|
|
|
|
}
|
2017-06-24 23:06:43 +02:00
|
|
|
|
|
|
|
WriteBatchInternal::SetSequence(merged_batch, sequence);
|
|
|
|
|
|
|
|
uint64_t log_size;
|
|
|
|
status = WriteToWAL(*merged_batch, log_writer, log_used, &log_size);
|
2017-11-02 01:23:52 +01:00
|
|
|
if (to_be_cached_state) {
|
|
|
|
cached_recoverable_state_ = *to_be_cached_state;
|
|
|
|
cached_recoverable_state_empty_ = false;
|
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
|
|
|
|
if (status.ok() && need_log_sync) {
|
|
|
|
StopWatch sw(env_, stats_, WAL_FILE_SYNC_MICROS);
|
|
|
|
// It's safe to access logs_ with unlocked mutex_ here because:
|
|
|
|
// - we've set getting_synced=true for all logs,
|
|
|
|
// so other threads won't pop from logs_ while we're here,
|
|
|
|
// - only writer thread can push to logs_, and we're in
|
|
|
|
// writer thread, so no one will push to logs_,
|
|
|
|
// - as long as other threads don't modify it, it's safe to read
|
|
|
|
// from std::deque from multiple threads concurrently.
|
|
|
|
for (auto& log : logs_) {
|
|
|
|
status = log.writer->file()->Sync(immutable_db_options_.use_fsync);
|
|
|
|
if (!status.ok()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status.ok() && need_log_dir_sync) {
|
|
|
|
// We only sync WAL directory the first time WAL syncing is
|
|
|
|
// requested, so that in case users never turn on WAL sync,
|
|
|
|
// we can avoid the disk I/O in the write code path.
|
|
|
|
status = directories_.GetWalDir()->Fsync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (merged_batch == &tmp_batch_) {
|
|
|
|
tmp_batch_.Clear();
|
|
|
|
}
|
|
|
|
if (status.ok()) {
|
|
|
|
auto stats = default_cf_internal_stats_;
|
|
|
|
if (need_log_sync) {
|
|
|
|
stats->AddDBStats(InternalStats::WAL_FILE_SYNCED, 1);
|
|
|
|
RecordTick(stats_, WAL_FILE_SYNCED);
|
|
|
|
}
|
|
|
|
stats->AddDBStats(InternalStats::WAL_FILE_BYTES, log_size);
|
|
|
|
RecordTick(stats_, WAL_FILE_BYTES, log_size);
|
|
|
|
stats->AddDBStats(InternalStats::WRITE_WITH_WAL, write_with_wal);
|
|
|
|
RecordTick(stats_, WRITE_WITH_WAL, write_with_wal);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-06-24 23:06:43 +02:00
|
|
|
Status DBImpl::ConcurrentWriteToWAL(const WriteThread::WriteGroup& write_group,
|
|
|
|
uint64_t* log_used,
|
|
|
|
SequenceNumber* last_sequence,
|
2017-09-18 23:36:53 +02:00
|
|
|
size_t seq_inc) {
|
2017-06-24 23:06:43 +02:00
|
|
|
Status status;
|
|
|
|
|
2017-11-15 17:22:54 +01:00
|
|
|
assert(!write_group.leader->disable_wal);
|
|
|
|
// Same holds for all in the batch group
|
2017-06-24 23:06:43 +02:00
|
|
|
WriteBatch tmp_batch;
|
|
|
|
size_t write_with_wal = 0;
|
2017-11-02 01:23:52 +01:00
|
|
|
WriteBatch* to_be_cached_state = nullptr;
|
2017-06-24 23:06:43 +02:00
|
|
|
WriteBatch* merged_batch =
|
2017-11-02 01:23:52 +01:00
|
|
|
MergeBatch(write_group, &tmp_batch, &write_with_wal, &to_be_cached_state);
|
2017-06-24 23:06:43 +02:00
|
|
|
|
|
|
|
// We need to lock log_write_mutex_ since logs_ and alive_log_files might be
|
|
|
|
// pushed back concurrently
|
|
|
|
log_write_mutex_.Lock();
|
2017-06-28 22:05:52 +02:00
|
|
|
if (merged_batch == write_group.leader->batch) {
|
|
|
|
write_group.leader->log_used = logfile_number_;
|
|
|
|
} else if (write_with_wal > 1) {
|
|
|
|
for (auto writer : write_group) {
|
|
|
|
writer->log_used = logfile_number_;
|
|
|
|
}
|
|
|
|
}
|
2017-11-11 02:18:01 +01:00
|
|
|
*last_sequence = versions_->FetchAddLastAllocatedSequence(seq_inc);
|
2017-06-24 23:06:43 +02:00
|
|
|
auto sequence = *last_sequence + 1;
|
|
|
|
WriteBatchInternal::SetSequence(merged_batch, sequence);
|
|
|
|
|
|
|
|
log::Writer* log_writer = logs_.back().writer;
|
|
|
|
uint64_t log_size;
|
|
|
|
status = WriteToWAL(*merged_batch, log_writer, log_used, &log_size);
|
2017-11-02 01:23:52 +01:00
|
|
|
if (to_be_cached_state) {
|
|
|
|
cached_recoverable_state_ = *to_be_cached_state;
|
|
|
|
cached_recoverable_state_empty_ = false;
|
|
|
|
}
|
2017-06-24 23:06:43 +02:00
|
|
|
log_write_mutex_.Unlock();
|
|
|
|
|
|
|
|
if (status.ok()) {
|
2017-06-30 01:57:13 +02:00
|
|
|
const bool concurrent = true;
|
2017-06-24 23:06:43 +02:00
|
|
|
auto stats = default_cf_internal_stats_;
|
2017-06-30 01:57:13 +02:00
|
|
|
stats->AddDBStats(InternalStats::WAL_FILE_BYTES, log_size, concurrent);
|
2017-06-24 23:06:43 +02:00
|
|
|
RecordTick(stats_, WAL_FILE_BYTES, log_size);
|
2017-06-30 01:57:13 +02:00
|
|
|
stats->AddDBStats(InternalStats::WRITE_WITH_WAL, write_with_wal,
|
|
|
|
concurrent);
|
2017-06-24 23:06:43 +02:00
|
|
|
RecordTick(stats_, WRITE_WITH_WAL, write_with_wal);
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2017-11-02 01:23:52 +01:00
|
|
|
Status DBImpl::WriteRecoverableState() {
|
|
|
|
mutex_.AssertHeld();
|
|
|
|
if (!cached_recoverable_state_empty_) {
|
|
|
|
bool dont_care_bool;
|
|
|
|
SequenceNumber next_seq;
|
2017-11-11 02:18:01 +01:00
|
|
|
if (two_write_queues_) {
|
2017-11-02 01:23:52 +01:00
|
|
|
log_write_mutex_.Lock();
|
|
|
|
}
|
2018-03-29 23:30:37 +02:00
|
|
|
SequenceNumber seq;
|
|
|
|
if (two_write_queues_) {
|
|
|
|
seq = versions_->FetchAddLastAllocatedSequence(0);
|
|
|
|
} else {
|
|
|
|
seq = versions_->LastSequence();
|
|
|
|
}
|
2017-12-01 08:39:56 +01:00
|
|
|
WriteBatchInternal::SetSequence(&cached_recoverable_state_, seq + 1);
|
2017-11-02 01:23:52 +01:00
|
|
|
auto status = WriteBatchInternal::InsertInto(
|
|
|
|
&cached_recoverable_state_, column_family_memtables_.get(),
|
|
|
|
&flush_scheduler_, true, 0 /*recovery_log_number*/, this,
|
|
|
|
false /* concurrent_memtable_writes */, &next_seq, &dont_care_bool,
|
|
|
|
seq_per_batch_);
|
2017-12-01 08:39:56 +01:00
|
|
|
auto last_seq = next_seq - 1;
|
|
|
|
if (two_write_queues_) {
|
|
|
|
versions_->FetchAddLastAllocatedSequence(last_seq - seq);
|
2018-03-29 23:30:37 +02:00
|
|
|
versions_->SetLastPublishedSequence(last_seq);
|
2017-12-01 08:39:56 +01:00
|
|
|
}
|
|
|
|
versions_->SetLastSequence(last_seq);
|
2017-11-11 02:18:01 +01:00
|
|
|
if (two_write_queues_) {
|
2017-11-02 01:23:52 +01:00
|
|
|
log_write_mutex_.Unlock();
|
|
|
|
}
|
2018-03-28 21:01:09 +02:00
|
|
|
if (status.ok() && recoverable_state_pre_release_callback_) {
|
|
|
|
const bool DISABLE_MEMTABLE = true;
|
|
|
|
for (uint64_t sub_batch_seq = seq + 1;
|
|
|
|
sub_batch_seq < next_seq && status.ok(); sub_batch_seq++) {
|
|
|
|
status = recoverable_state_pre_release_callback_->Callback(
|
|
|
|
sub_batch_seq, !DISABLE_MEMTABLE);
|
|
|
|
}
|
|
|
|
}
|
2017-11-02 01:23:52 +01:00
|
|
|
if (status.ok()) {
|
|
|
|
cached_recoverable_state_.Clear();
|
|
|
|
cached_recoverable_state_empty_ = true;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2017-09-28 02:37:08 +02:00
|
|
|
Status DBImpl::SwitchWAL(WriteContext* write_context) {
|
2017-04-06 02:14:05 +02:00
|
|
|
mutex_.AssertHeld();
|
|
|
|
assert(write_context != nullptr);
|
|
|
|
Status status;
|
|
|
|
|
|
|
|
if (alive_log_files_.begin()->getting_flushed) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto oldest_alive_log = alive_log_files_.begin()->number;
|
Skip deleted WALs during recovery
Summary:
This patch record min log number to keep to the manifest while flushing SST files to ignore them and any WAL older than them during recovery. This is to avoid scenarios when we have a gap between the WAL files are fed to the recovery procedure. The gap could happen by for example out-of-order WAL deletion. Such gap could cause problems in 2PC recovery where the prepared and commit entry are placed into two separate WAL and gap in the WALs could result into not processing the WAL with the commit entry and hence breaking the 2PC recovery logic.
Before the commit, for 2PC case, we determined which log number to keep in FindObsoleteFiles(). We looked at the earliest logs with outstanding prepare entries, or prepare entries whose respective commit or abort are in memtable. With the commit, the same calculation is done while we apply the SST flush. Just before installing the flush file, we precompute the earliest log file to keep after the flush finishes using the same logic (but skipping the memtables just flushed), record this information to the manifest entry for this new flushed SST file. This pre-computed value is also remembered in memory, and will later be used to determine whether a log file can be deleted. This value is unlikely to change until next flush because the commit entry will stay in memtable. (In WritePrepared, we could have removed the older log files as soon as all prepared entries are committed. It's not yet done anyway. Even if we do it, the only thing we loss with this new approach is earlier log deletion between two flushes, which does not guarantee to happen anyway because the obsolete file clean-up function is only executed after flush or compaction)
This min log number to keep is stored in the manifest using the safely-ignore customized field of AddFile entry, in order to guarantee that the DB generated using newer release can be opened by previous releases no older than 4.2.
Closes https://github.com/facebook/rocksdb/pull/3765
Differential Revision: D7747618
Pulled By: siying
fbshipit-source-id: d00c92105b4f83852e9754a1b70d6b64cb590729
2018-05-04 00:35:11 +02:00
|
|
|
bool flush_wont_release_oldest_log = false;
|
|
|
|
if (allow_2pc()) {
|
|
|
|
auto oldest_log_with_uncommited_prep =
|
|
|
|
logs_with_prep_tracker_.FindMinLogContainingOutstandingPrep();
|
|
|
|
|
|
|
|
assert(oldest_log_with_uncommited_prep == 0 ||
|
|
|
|
oldest_log_with_uncommited_prep >= oldest_alive_log);
|
|
|
|
if (oldest_log_with_uncommited_prep > 0 &&
|
|
|
|
oldest_log_with_uncommited_prep == oldest_alive_log) {
|
|
|
|
if (unable_to_release_oldest_log_) {
|
2017-04-06 02:14:05 +02:00
|
|
|
// we already attempted to flush all column families dependent on
|
Skip deleted WALs during recovery
Summary:
This patch record min log number to keep to the manifest while flushing SST files to ignore them and any WAL older than them during recovery. This is to avoid scenarios when we have a gap between the WAL files are fed to the recovery procedure. The gap could happen by for example out-of-order WAL deletion. Such gap could cause problems in 2PC recovery where the prepared and commit entry are placed into two separate WAL and gap in the WALs could result into not processing the WAL with the commit entry and hence breaking the 2PC recovery logic.
Before the commit, for 2PC case, we determined which log number to keep in FindObsoleteFiles(). We looked at the earliest logs with outstanding prepare entries, or prepare entries whose respective commit or abort are in memtable. With the commit, the same calculation is done while we apply the SST flush. Just before installing the flush file, we precompute the earliest log file to keep after the flush finishes using the same logic (but skipping the memtables just flushed), record this information to the manifest entry for this new flushed SST file. This pre-computed value is also remembered in memory, and will later be used to determine whether a log file can be deleted. This value is unlikely to change until next flush because the commit entry will stay in memtable. (In WritePrepared, we could have removed the older log files as soon as all prepared entries are committed. It's not yet done anyway. Even if we do it, the only thing we loss with this new approach is earlier log deletion between two flushes, which does not guarantee to happen anyway because the obsolete file clean-up function is only executed after flush or compaction)
This min log number to keep is stored in the manifest using the safely-ignore customized field of AddFile entry, in order to guarantee that the DB generated using newer release can be opened by previous releases no older than 4.2.
Closes https://github.com/facebook/rocksdb/pull/3765
Differential Revision: D7747618
Pulled By: siying
fbshipit-source-id: d00c92105b4f83852e9754a1b70d6b64cb590729
2018-05-04 00:35:11 +02:00
|
|
|
// the oldest alive log but the log still contained uncommited
|
|
|
|
// transactions so there is still nothing that we can do.
|
2017-04-06 02:14:05 +02:00
|
|
|
return status;
|
Skip deleted WALs during recovery
Summary:
This patch record min log number to keep to the manifest while flushing SST files to ignore them and any WAL older than them during recovery. This is to avoid scenarios when we have a gap between the WAL files are fed to the recovery procedure. The gap could happen by for example out-of-order WAL deletion. Such gap could cause problems in 2PC recovery where the prepared and commit entry are placed into two separate WAL and gap in the WALs could result into not processing the WAL with the commit entry and hence breaking the 2PC recovery logic.
Before the commit, for 2PC case, we determined which log number to keep in FindObsoleteFiles(). We looked at the earliest logs with outstanding prepare entries, or prepare entries whose respective commit or abort are in memtable. With the commit, the same calculation is done while we apply the SST flush. Just before installing the flush file, we precompute the earliest log file to keep after the flush finishes using the same logic (but skipping the memtables just flushed), record this information to the manifest entry for this new flushed SST file. This pre-computed value is also remembered in memory, and will later be used to determine whether a log file can be deleted. This value is unlikely to change until next flush because the commit entry will stay in memtable. (In WritePrepared, we could have removed the older log files as soon as all prepared entries are committed. It's not yet done anyway. Even if we do it, the only thing we loss with this new approach is earlier log deletion between two flushes, which does not guarantee to happen anyway because the obsolete file clean-up function is only executed after flush or compaction)
This min log number to keep is stored in the manifest using the safely-ignore customized field of AddFile entry, in order to guarantee that the DB generated using newer release can be opened by previous releases no older than 4.2.
Closes https://github.com/facebook/rocksdb/pull/3765
Differential Revision: D7747618
Pulled By: siying
fbshipit-source-id: d00c92105b4f83852e9754a1b70d6b64cb590729
2018-05-04 00:35:11 +02:00
|
|
|
} else {
|
|
|
|
ROCKS_LOG_WARN(
|
|
|
|
immutable_db_options_.info_log,
|
|
|
|
"Unable to release oldest log due to uncommited transaction");
|
|
|
|
unable_to_release_oldest_log_ = true;
|
|
|
|
flush_wont_release_oldest_log = true;
|
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
Skip deleted WALs during recovery
Summary:
This patch record min log number to keep to the manifest while flushing SST files to ignore them and any WAL older than them during recovery. This is to avoid scenarios when we have a gap between the WAL files are fed to the recovery procedure. The gap could happen by for example out-of-order WAL deletion. Such gap could cause problems in 2PC recovery where the prepared and commit entry are placed into two separate WAL and gap in the WALs could result into not processing the WAL with the commit entry and hence breaking the 2PC recovery logic.
Before the commit, for 2PC case, we determined which log number to keep in FindObsoleteFiles(). We looked at the earliest logs with outstanding prepare entries, or prepare entries whose respective commit or abort are in memtable. With the commit, the same calculation is done while we apply the SST flush. Just before installing the flush file, we precompute the earliest log file to keep after the flush finishes using the same logic (but skipping the memtables just flushed), record this information to the manifest entry for this new flushed SST file. This pre-computed value is also remembered in memory, and will later be used to determine whether a log file can be deleted. This value is unlikely to change until next flush because the commit entry will stay in memtable. (In WritePrepared, we could have removed the older log files as soon as all prepared entries are committed. It's not yet done anyway. Even if we do it, the only thing we loss with this new approach is earlier log deletion between two flushes, which does not guarantee to happen anyway because the obsolete file clean-up function is only executed after flush or compaction)
This min log number to keep is stored in the manifest using the safely-ignore customized field of AddFile entry, in order to guarantee that the DB generated using newer release can be opened by previous releases no older than 4.2.
Closes https://github.com/facebook/rocksdb/pull/3765
Differential Revision: D7747618
Pulled By: siying
fbshipit-source-id: d00c92105b4f83852e9754a1b70d6b64cb590729
2018-05-04 00:35:11 +02:00
|
|
|
}
|
|
|
|
if (!flush_wont_release_oldest_log) {
|
2017-04-06 02:14:05 +02:00
|
|
|
// we only mark this log as getting flushed if we have successfully
|
|
|
|
// flushed all data in this log. If this log contains outstanding prepared
|
|
|
|
// transactions then we cannot flush this log until those transactions are commited.
|
Skip deleted WALs during recovery
Summary:
This patch record min log number to keep to the manifest while flushing SST files to ignore them and any WAL older than them during recovery. This is to avoid scenarios when we have a gap between the WAL files are fed to the recovery procedure. The gap could happen by for example out-of-order WAL deletion. Such gap could cause problems in 2PC recovery where the prepared and commit entry are placed into two separate WAL and gap in the WALs could result into not processing the WAL with the commit entry and hence breaking the 2PC recovery logic.
Before the commit, for 2PC case, we determined which log number to keep in FindObsoleteFiles(). We looked at the earliest logs with outstanding prepare entries, or prepare entries whose respective commit or abort are in memtable. With the commit, the same calculation is done while we apply the SST flush. Just before installing the flush file, we precompute the earliest log file to keep after the flush finishes using the same logic (but skipping the memtables just flushed), record this information to the manifest entry for this new flushed SST file. This pre-computed value is also remembered in memory, and will later be used to determine whether a log file can be deleted. This value is unlikely to change until next flush because the commit entry will stay in memtable. (In WritePrepared, we could have removed the older log files as soon as all prepared entries are committed. It's not yet done anyway. Even if we do it, the only thing we loss with this new approach is earlier log deletion between two flushes, which does not guarantee to happen anyway because the obsolete file clean-up function is only executed after flush or compaction)
This min log number to keep is stored in the manifest using the safely-ignore customized field of AddFile entry, in order to guarantee that the DB generated using newer release can be opened by previous releases no older than 4.2.
Closes https://github.com/facebook/rocksdb/pull/3765
Differential Revision: D7747618
Pulled By: siying
fbshipit-source-id: d00c92105b4f83852e9754a1b70d6b64cb590729
2018-05-04 00:35:11 +02:00
|
|
|
unable_to_release_oldest_log_ = false;
|
2017-04-06 02:14:05 +02:00
|
|
|
alive_log_files_.begin()->getting_flushed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ROCKS_LOG_INFO(immutable_db_options_.info_log,
|
|
|
|
"Flushing all column families with data in WAL number %" PRIu64
|
|
|
|
". Total log size is %" PRIu64
|
|
|
|
" while max_total_wal_size is %" PRIu64,
|
|
|
|
oldest_alive_log, total_log_size_.load(), GetMaxTotalWalSize());
|
|
|
|
// no need to refcount because drop is happening in write thread, so can't
|
|
|
|
// happen while we're in the write thread
|
|
|
|
for (auto cfd : *versions_->GetColumnFamilySet()) {
|
|
|
|
if (cfd->IsDropped()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (cfd->OldestLogToKeep() <= oldest_alive_log) {
|
|
|
|
status = SwitchMemtable(cfd, write_context);
|
|
|
|
if (!status.ok()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cfd->imm()->FlushRequested();
|
2018-02-09 21:09:55 +01:00
|
|
|
SchedulePendingFlush(cfd, FlushReason::kWriteBufferManager);
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
MaybeScheduleFlushOrCompaction();
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DBImpl::HandleWriteBufferFull(WriteContext* write_context) {
|
|
|
|
mutex_.AssertHeld();
|
|
|
|
assert(write_context != nullptr);
|
|
|
|
Status status;
|
|
|
|
|
|
|
|
// Before a new memtable is added in SwitchMemtable(),
|
|
|
|
// write_buffer_manager_->ShouldFlush() will keep returning true. If another
|
|
|
|
// thread is writing to another DB with the same write buffer, they may also
|
|
|
|
// be flushed. We may end up with flushing much more DBs than needed. It's
|
|
|
|
// suboptimal but still correct.
|
|
|
|
ROCKS_LOG_INFO(
|
|
|
|
immutable_db_options_.info_log,
|
|
|
|
"Flushing column family with largest mem table size. Write buffer is "
|
|
|
|
"using %" PRIu64 " bytes out of a total of %" PRIu64 ".",
|
|
|
|
write_buffer_manager_->memory_usage(),
|
|
|
|
write_buffer_manager_->buffer_size());
|
|
|
|
// no need to refcount because drop is happening in write thread, so can't
|
|
|
|
// happen while we're in the write thread
|
|
|
|
ColumnFamilyData* cfd_picked = nullptr;
|
|
|
|
SequenceNumber seq_num_for_cf_picked = kMaxSequenceNumber;
|
|
|
|
|
|
|
|
for (auto cfd : *versions_->GetColumnFamilySet()) {
|
|
|
|
if (cfd->IsDropped()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!cfd->mem()->IsEmpty()) {
|
|
|
|
// We only consider active mem table, hoping immutable memtable is
|
|
|
|
// already in the process of flushing.
|
|
|
|
uint64_t seq = cfd->mem()->GetCreationSeq();
|
|
|
|
if (cfd_picked == nullptr || seq < seq_num_for_cf_picked) {
|
|
|
|
cfd_picked = cfd;
|
|
|
|
seq_num_for_cf_picked = seq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cfd_picked != nullptr) {
|
FlushReason improvement
Summary:
Right now flush reason "SuperVersion Change" covers a few different scenarios which is a bit vague. For example, the following db_bench job should trigger "Write Buffer Full"
> $ TEST_TMPDIR=/dev/shm ./db_bench -benchmarks=fillrandom -write_buffer_size=1048576 -target_file_size_base=1048576 -max_bytes_for_level_base=4194304
$ grep 'flush_reason' /dev/shm/dbbench/LOG
...
2018/03/06-17:30:42.543638 7f2773b99700 EVENT_LOG_v1 {"time_micros": 1520386242543634, "job": 192, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018024, "flush_reason": "SuperVersion Change"}
2018/03/06-17:30:42.569541 7f2773b99700 EVENT_LOG_v1 {"time_micros": 1520386242569536, "job": 193, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018104, "flush_reason": "SuperVersion Change"}
2018/03/06-17:30:42.596396 7f2773b99700 EVENT_LOG_v1 {"time_micros": 1520386242596392, "job": 194, "event": "flush_started", "num_memtables": 1, "num_entries": 7008, "num_deletes": 0, "memory_usage": 1018048, "flush_reason": "SuperVersion Change"}
2018/03/06-17:30:42.622444 7f2773b99700 EVENT_LOG_v1 {"time_micros": 1520386242622440, "job": 195, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018104, "flush_reason": "SuperVersion Change"}
With the fix:
> 2018/03/19-14:40:02.341451 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602341444, "job": 98, "event": "flush_started", "num_memtables": 1, "num_entries": 7009, "num_deletes": 0, "memory_usage": 1018008, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.379655 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602379642, "job": 100, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018016, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.418479 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602418474, "job": 101, "event": "flush_started", "num_memtables": 1, "num_entries": 7009, "num_deletes": 0, "memory_usage": 1018104, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.455084 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602455079, "job": 102, "event": "flush_started", "num_memtables": 1, "num_entries": 7009, "num_deletes": 0, "memory_usage": 1018048, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.492293 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602492288, "job": 104, "event": "flush_started", "num_memtables": 1, "num_entries": 7007, "num_deletes": 0, "memory_usage": 1018056, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.528720 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602528715, "job": 105, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018104, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.566255 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602566238, "job": 107, "event": "flush_started", "num_memtables": 1, "num_entries": 7009, "num_deletes": 0, "memory_usage": 1018112, "flush_reason": "Write Buffer Full"}
Closes https://github.com/facebook/rocksdb/pull/3627
Differential Revision: D7328772
Pulled By: miasantreble
fbshipit-source-id: 67c94065fbdd36930f09930aad0aaa6d2c152bb8
2018-03-23 02:34:04 +01:00
|
|
|
status = SwitchMemtable(cfd_picked, write_context,
|
|
|
|
FlushReason::kWriteBufferFull);
|
2017-04-06 02:14:05 +02:00
|
|
|
if (status.ok()) {
|
|
|
|
cfd_picked->imm()->FlushRequested();
|
2018-02-09 21:09:55 +01:00
|
|
|
SchedulePendingFlush(cfd_picked, FlushReason::kWriteBufferFull);
|
2017-04-06 02:14:05 +02:00
|
|
|
MaybeScheduleFlushOrCompaction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t DBImpl::GetMaxTotalWalSize() const {
|
|
|
|
mutex_.AssertHeld();
|
|
|
|
return mutable_db_options_.max_total_wal_size == 0
|
|
|
|
? 4 * max_total_in_memory_state_
|
|
|
|
: mutable_db_options_.max_total_wal_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// REQUIRES: mutex_ is held
|
|
|
|
// REQUIRES: this thread is currently at the front of the writer queue
|
|
|
|
Status DBImpl::DelayWrite(uint64_t num_bytes,
|
|
|
|
const WriteOptions& write_options) {
|
|
|
|
uint64_t time_delayed = 0;
|
|
|
|
bool delayed = false;
|
|
|
|
{
|
|
|
|
StopWatch sw(env_, stats_, WRITE_STALL, &time_delayed);
|
|
|
|
uint64_t delay = write_controller_.GetDelay(env_, num_bytes);
|
|
|
|
if (delay > 0) {
|
|
|
|
if (write_options.no_slowdown) {
|
|
|
|
return Status::Incomplete();
|
|
|
|
}
|
|
|
|
TEST_SYNC_POINT("DBImpl::DelayWrite:Sleep");
|
|
|
|
|
|
|
|
mutex_.Unlock();
|
|
|
|
// We will delay the write until we have slept for delay ms or
|
|
|
|
// we don't need a delay anymore
|
|
|
|
const uint64_t kDelayInterval = 1000;
|
|
|
|
uint64_t stall_end = sw.start_time() + delay;
|
|
|
|
while (write_controller_.NeedsDelay()) {
|
|
|
|
if (env_->NowMicros() >= stall_end) {
|
|
|
|
// We already delayed this write `delay` microseconds
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
delayed = true;
|
|
|
|
// Sleep for 0.001 seconds
|
|
|
|
env_->SleepForMicroseconds(kDelayInterval);
|
|
|
|
}
|
|
|
|
mutex_.Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
while (bg_error_.ok() && write_controller_.IsStopped()) {
|
|
|
|
if (write_options.no_slowdown) {
|
|
|
|
return Status::Incomplete();
|
|
|
|
}
|
|
|
|
delayed = true;
|
|
|
|
TEST_SYNC_POINT("DBImpl::DelayWrite:Wait");
|
|
|
|
bg_cv_.Wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(!delayed || !write_options.no_slowdown);
|
|
|
|
if (delayed) {
|
|
|
|
default_cf_internal_stats_->AddDBStats(InternalStats::WRITE_STALL_MICROS,
|
|
|
|
time_delayed);
|
|
|
|
RecordTick(stats_, STALL_MICROS, time_delayed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bg_error_;
|
|
|
|
}
|
|
|
|
|
2017-06-05 23:42:34 +02:00
|
|
|
Status DBImpl::ThrottleLowPriWritesIfNeeded(const WriteOptions& write_options,
|
|
|
|
WriteBatch* my_batch) {
|
|
|
|
assert(write_options.low_pri);
|
|
|
|
// This is called outside the DB mutex. Although it is safe to make the call,
|
|
|
|
// the consistency condition is not guaranteed to hold. It's OK to live with
|
|
|
|
// it in this case.
|
|
|
|
// If we need to speed compaction, it means the compaction is left behind
|
|
|
|
// and we start to limit low pri writes to a limit.
|
|
|
|
if (write_controller_.NeedSpeedupCompaction()) {
|
|
|
|
if (allow_2pc() && (my_batch->HasCommit() || my_batch->HasRollback())) {
|
|
|
|
// For 2PC, we only rate limit prepare, not commit.
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
if (write_options.no_slowdown) {
|
|
|
|
return Status::Incomplete();
|
|
|
|
} else {
|
|
|
|
assert(my_batch != nullptr);
|
|
|
|
// Rate limit those writes. The reason that we don't completely wait
|
|
|
|
// is that in case the write is heavy, low pri writes may never have
|
|
|
|
// a chance to run. Now we guarantee we are still slowly making
|
|
|
|
// progress.
|
2018-04-24 02:53:27 +02:00
|
|
|
PERF_TIMER_GUARD(write_delay_time);
|
2017-06-13 23:51:22 +02:00
|
|
|
write_controller_.low_pri_rate_limiter()->Request(
|
|
|
|
my_batch->GetDataSize(), Env::IO_HIGH, nullptr /* stats */,
|
|
|
|
RateLimiter::OpType::kWrite);
|
2017-06-05 23:42:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2017-04-06 02:14:05 +02:00
|
|
|
Status DBImpl::ScheduleFlushes(WriteContext* context) {
|
|
|
|
ColumnFamilyData* cfd;
|
|
|
|
while ((cfd = flush_scheduler_.TakeNextColumnFamily()) != nullptr) {
|
FlushReason improvement
Summary:
Right now flush reason "SuperVersion Change" covers a few different scenarios which is a bit vague. For example, the following db_bench job should trigger "Write Buffer Full"
> $ TEST_TMPDIR=/dev/shm ./db_bench -benchmarks=fillrandom -write_buffer_size=1048576 -target_file_size_base=1048576 -max_bytes_for_level_base=4194304
$ grep 'flush_reason' /dev/shm/dbbench/LOG
...
2018/03/06-17:30:42.543638 7f2773b99700 EVENT_LOG_v1 {"time_micros": 1520386242543634, "job": 192, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018024, "flush_reason": "SuperVersion Change"}
2018/03/06-17:30:42.569541 7f2773b99700 EVENT_LOG_v1 {"time_micros": 1520386242569536, "job": 193, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018104, "flush_reason": "SuperVersion Change"}
2018/03/06-17:30:42.596396 7f2773b99700 EVENT_LOG_v1 {"time_micros": 1520386242596392, "job": 194, "event": "flush_started", "num_memtables": 1, "num_entries": 7008, "num_deletes": 0, "memory_usage": 1018048, "flush_reason": "SuperVersion Change"}
2018/03/06-17:30:42.622444 7f2773b99700 EVENT_LOG_v1 {"time_micros": 1520386242622440, "job": 195, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018104, "flush_reason": "SuperVersion Change"}
With the fix:
> 2018/03/19-14:40:02.341451 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602341444, "job": 98, "event": "flush_started", "num_memtables": 1, "num_entries": 7009, "num_deletes": 0, "memory_usage": 1018008, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.379655 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602379642, "job": 100, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018016, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.418479 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602418474, "job": 101, "event": "flush_started", "num_memtables": 1, "num_entries": 7009, "num_deletes": 0, "memory_usage": 1018104, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.455084 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602455079, "job": 102, "event": "flush_started", "num_memtables": 1, "num_entries": 7009, "num_deletes": 0, "memory_usage": 1018048, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.492293 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602492288, "job": 104, "event": "flush_started", "num_memtables": 1, "num_entries": 7007, "num_deletes": 0, "memory_usage": 1018056, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.528720 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602528715, "job": 105, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018104, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.566255 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602566238, "job": 107, "event": "flush_started", "num_memtables": 1, "num_entries": 7009, "num_deletes": 0, "memory_usage": 1018112, "flush_reason": "Write Buffer Full"}
Closes https://github.com/facebook/rocksdb/pull/3627
Differential Revision: D7328772
Pulled By: miasantreble
fbshipit-source-id: 67c94065fbdd36930f09930aad0aaa6d2c152bb8
2018-03-23 02:34:04 +01:00
|
|
|
auto status = SwitchMemtable(cfd, context, FlushReason::kWriteBufferFull);
|
2017-04-06 02:14:05 +02:00
|
|
|
if (cfd->Unref()) {
|
|
|
|
delete cfd;
|
|
|
|
}
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
2018-03-05 22:08:17 +01:00
|
|
|
void DBImpl::NotifyOnMemTableSealed(ColumnFamilyData* /*cfd*/,
|
2017-04-06 02:14:05 +02:00
|
|
|
const MemTableInfo& mem_table_info) {
|
|
|
|
if (immutable_db_options_.listeners.size() == 0U) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (shutting_down_.load(std::memory_order_acquire)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto listener : immutable_db_options_.listeners) {
|
|
|
|
listener->OnMemTableSealed(mem_table_info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // ROCKSDB_LITE
|
|
|
|
|
|
|
|
// REQUIRES: mutex_ is held
|
|
|
|
// REQUIRES: this thread is currently at the front of the writer queue
|
FlushReason improvement
Summary:
Right now flush reason "SuperVersion Change" covers a few different scenarios which is a bit vague. For example, the following db_bench job should trigger "Write Buffer Full"
> $ TEST_TMPDIR=/dev/shm ./db_bench -benchmarks=fillrandom -write_buffer_size=1048576 -target_file_size_base=1048576 -max_bytes_for_level_base=4194304
$ grep 'flush_reason' /dev/shm/dbbench/LOG
...
2018/03/06-17:30:42.543638 7f2773b99700 EVENT_LOG_v1 {"time_micros": 1520386242543634, "job": 192, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018024, "flush_reason": "SuperVersion Change"}
2018/03/06-17:30:42.569541 7f2773b99700 EVENT_LOG_v1 {"time_micros": 1520386242569536, "job": 193, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018104, "flush_reason": "SuperVersion Change"}
2018/03/06-17:30:42.596396 7f2773b99700 EVENT_LOG_v1 {"time_micros": 1520386242596392, "job": 194, "event": "flush_started", "num_memtables": 1, "num_entries": 7008, "num_deletes": 0, "memory_usage": 1018048, "flush_reason": "SuperVersion Change"}
2018/03/06-17:30:42.622444 7f2773b99700 EVENT_LOG_v1 {"time_micros": 1520386242622440, "job": 195, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018104, "flush_reason": "SuperVersion Change"}
With the fix:
> 2018/03/19-14:40:02.341451 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602341444, "job": 98, "event": "flush_started", "num_memtables": 1, "num_entries": 7009, "num_deletes": 0, "memory_usage": 1018008, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.379655 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602379642, "job": 100, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018016, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.418479 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602418474, "job": 101, "event": "flush_started", "num_memtables": 1, "num_entries": 7009, "num_deletes": 0, "memory_usage": 1018104, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.455084 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602455079, "job": 102, "event": "flush_started", "num_memtables": 1, "num_entries": 7009, "num_deletes": 0, "memory_usage": 1018048, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.492293 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602492288, "job": 104, "event": "flush_started", "num_memtables": 1, "num_entries": 7007, "num_deletes": 0, "memory_usage": 1018056, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.528720 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602528715, "job": 105, "event": "flush_started", "num_memtables": 1, "num_entries": 7006, "num_deletes": 0, "memory_usage": 1018104, "flush_reason": "Write Buffer Full"}
2018/03/19-14:40:02.566255 7f11dc257700 EVENT_LOG_v1 {"time_micros": 1521495602566238, "job": 107, "event": "flush_started", "num_memtables": 1, "num_entries": 7009, "num_deletes": 0, "memory_usage": 1018112, "flush_reason": "Write Buffer Full"}
Closes https://github.com/facebook/rocksdb/pull/3627
Differential Revision: D7328772
Pulled By: miasantreble
fbshipit-source-id: 67c94065fbdd36930f09930aad0aaa6d2c152bb8
2018-03-23 02:34:04 +01:00
|
|
|
Status DBImpl::SwitchMemtable(ColumnFamilyData* cfd, WriteContext* context,
|
|
|
|
FlushReason flush_reason) {
|
2017-04-06 02:14:05 +02:00
|
|
|
mutex_.AssertHeld();
|
2017-06-28 22:05:52 +02:00
|
|
|
WriteThread::Writer nonmem_w;
|
2017-11-11 02:18:01 +01:00
|
|
|
if (two_write_queues_) {
|
2017-06-30 18:30:03 +02:00
|
|
|
// SwitchMemtable is a rare event. To simply the reasoning, we make sure
|
|
|
|
// that there is no concurrent thread writing to WAL.
|
2017-06-28 22:05:52 +02:00
|
|
|
nonmem_write_thread_.EnterUnbatched(&nonmem_w, &mutex_);
|
|
|
|
}
|
|
|
|
|
2017-04-06 02:14:05 +02:00
|
|
|
unique_ptr<WritableFile> lfile;
|
|
|
|
log::Writer* new_log = nullptr;
|
|
|
|
MemTable* new_mem = nullptr;
|
|
|
|
|
2017-11-02 01:23:52 +01:00
|
|
|
// Recoverable state is persisted in WAL. After memtable switch, WAL might
|
|
|
|
// be deleted, so we write the state to memtable to be persisted as well.
|
|
|
|
Status s = WriteRecoverableState();
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-05-19 23:24:23 +02:00
|
|
|
// In case of pipelined write is enabled, wait for all pending memtable
|
|
|
|
// writers.
|
|
|
|
if (immutable_db_options_.enable_pipelined_write) {
|
|
|
|
write_thread_.WaitForMemTableWriters();
|
|
|
|
}
|
|
|
|
|
2017-04-06 02:14:05 +02:00
|
|
|
// Attempt to switch to a new memtable and trigger flush of old.
|
|
|
|
// Do this without holding the dbmutex lock.
|
|
|
|
assert(versions_->prev_log_number() == 0);
|
2017-11-11 02:18:01 +01:00
|
|
|
if (two_write_queues_) {
|
2017-06-30 18:30:03 +02:00
|
|
|
log_write_mutex_.Lock();
|
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
bool creating_new_log = !log_empty_;
|
2017-11-11 02:18:01 +01:00
|
|
|
if (two_write_queues_) {
|
2017-06-30 18:30:03 +02:00
|
|
|
log_write_mutex_.Unlock();
|
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
uint64_t recycle_log_number = 0;
|
|
|
|
if (creating_new_log && immutable_db_options_.recycle_log_file_num &&
|
2018-01-09 21:51:10 +01:00
|
|
|
!log_recycle_files_.empty()) {
|
|
|
|
recycle_log_number = log_recycle_files_.front();
|
|
|
|
log_recycle_files_.pop_front();
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
uint64_t new_log_number =
|
|
|
|
creating_new_log ? versions_->NewFileNumber() : logfile_number_;
|
|
|
|
const MutableCFOptions mutable_cf_options = *cfd->GetLatestMutableCFOptions();
|
|
|
|
|
2017-08-17 06:45:32 +02:00
|
|
|
// Set memtable_info for memtable sealed callback
|
2017-04-06 02:14:05 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
MemTableInfo memtable_info;
|
|
|
|
memtable_info.cf_name = cfd->GetName();
|
|
|
|
memtable_info.first_seqno = cfd->mem()->GetFirstSequenceNumber();
|
|
|
|
memtable_info.earliest_seqno = cfd->mem()->GetEarliestSequenceNumber();
|
|
|
|
memtable_info.num_entries = cfd->mem()->num_entries();
|
|
|
|
memtable_info.num_deletes = cfd->mem()->num_deletes();
|
|
|
|
#endif // ROCKSDB_LITE
|
|
|
|
// Log this later after lock release. It may be outdated, e.g., if background
|
|
|
|
// flush happens before logging, but that should be ok.
|
|
|
|
int num_imm_unflushed = cfd->imm()->NumNotFlushed();
|
|
|
|
DBOptions db_options =
|
|
|
|
BuildDBOptions(immutable_db_options_, mutable_db_options_);
|
|
|
|
const auto preallocate_block_size =
|
|
|
|
GetWalPreallocateBlockSize(mutable_cf_options.write_buffer_size);
|
2017-11-10 18:25:26 +01:00
|
|
|
auto write_hint = CalculateWALWriteHint();
|
2017-04-06 02:14:05 +02:00
|
|
|
mutex_.Unlock();
|
|
|
|
{
|
|
|
|
if (creating_new_log) {
|
|
|
|
EnvOptions opt_env_opt =
|
|
|
|
env_->OptimizeForLogWrite(env_options_, db_options);
|
|
|
|
if (recycle_log_number) {
|
|
|
|
ROCKS_LOG_INFO(immutable_db_options_.info_log,
|
|
|
|
"reusing log %" PRIu64 " from recycle list\n",
|
|
|
|
recycle_log_number);
|
|
|
|
s = env_->ReuseWritableFile(
|
|
|
|
LogFileName(immutable_db_options_.wal_dir, new_log_number),
|
|
|
|
LogFileName(immutable_db_options_.wal_dir, recycle_log_number),
|
|
|
|
&lfile, opt_env_opt);
|
|
|
|
} else {
|
|
|
|
s = NewWritableFile(
|
|
|
|
env_, LogFileName(immutable_db_options_.wal_dir, new_log_number),
|
|
|
|
&lfile, opt_env_opt);
|
|
|
|
}
|
|
|
|
if (s.ok()) {
|
|
|
|
// Our final size should be less than write_buffer_size
|
|
|
|
// (compression, etc) but err on the side of caution.
|
|
|
|
|
|
|
|
// use preallocate_block_size instead
|
|
|
|
// of calling GetWalPreallocateBlockSize()
|
|
|
|
lfile->SetPreallocationBlockSize(preallocate_block_size);
|
2017-11-10 18:25:26 +01:00
|
|
|
lfile->SetWriteLifeTimeHint(write_hint);
|
2017-04-06 02:14:05 +02:00
|
|
|
unique_ptr<WritableFileWriter> file_writer(
|
|
|
|
new WritableFileWriter(std::move(lfile), opt_env_opt));
|
2017-06-24 23:06:43 +02:00
|
|
|
new_log = new log::Writer(
|
|
|
|
std::move(file_writer), new_log_number,
|
|
|
|
immutable_db_options_.recycle_log_file_num > 0, manual_wal_flush_);
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.ok()) {
|
|
|
|
SequenceNumber seq = versions_->LastSequence();
|
|
|
|
new_mem = cfd->ConstructNewMemtable(mutable_cf_options, seq);
|
2017-10-06 03:00:38 +02:00
|
|
|
context->superversion_context.NewSuperVersion();
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
// PLEASE NOTE: We assume that there are no failable operations
|
|
|
|
// after lock is acquired below since we are already notifying
|
|
|
|
// client about mem table becoming immutable.
|
|
|
|
NotifyOnMemTableSealed(cfd, memtable_info);
|
|
|
|
#endif //ROCKSDB_LITE
|
|
|
|
}
|
|
|
|
ROCKS_LOG_INFO(immutable_db_options_.info_log,
|
|
|
|
"[%s] New memtable created with log file: #%" PRIu64
|
|
|
|
". Immutable memtables: %d.\n",
|
|
|
|
cfd->GetName().c_str(), new_log_number, num_imm_unflushed);
|
|
|
|
mutex_.Lock();
|
2018-02-05 22:48:25 +01:00
|
|
|
if (s.ok() && creating_new_log) {
|
2017-06-28 22:05:52 +02:00
|
|
|
log_write_mutex_.Lock();
|
2017-04-06 02:14:05 +02:00
|
|
|
logfile_number_ = new_log_number;
|
|
|
|
assert(new_log != nullptr);
|
|
|
|
log_empty_ = true;
|
|
|
|
log_dir_synced_ = false;
|
2017-06-24 23:06:43 +02:00
|
|
|
if (!logs_.empty()) {
|
|
|
|
// Alway flush the buffer of the last log before switching to a new one
|
|
|
|
log::Writer* cur_log_writer = logs_.back().writer;
|
2018-02-05 22:48:25 +01:00
|
|
|
s = cur_log_writer->WriteBuffer();
|
|
|
|
if (!s.ok()) {
|
|
|
|
ROCKS_LOG_WARN(immutable_db_options_.info_log,
|
|
|
|
"[%s] Failed to switch from #%" PRIu64 " to #%" PRIu64
|
|
|
|
" WAL file -- %s\n",
|
|
|
|
cfd->GetName().c_str(), cur_log_writer->get_log_number(),
|
|
|
|
new_log_number);
|
|
|
|
}
|
2017-06-24 23:06:43 +02:00
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
logs_.emplace_back(logfile_number_, new_log);
|
|
|
|
alive_log_files_.push_back(LogFileNumberSize(logfile_number_));
|
2017-06-24 23:06:43 +02:00
|
|
|
log_write_mutex_.Unlock();
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
2018-02-05 22:48:25 +01:00
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
// how do we fail if we're not creating new log?
|
|
|
|
assert(creating_new_log);
|
|
|
|
assert(!new_mem);
|
|
|
|
assert(!new_log);
|
|
|
|
if (two_write_queues_) {
|
|
|
|
nonmem_write_thread_.ExitUnbatched(&nonmem_w);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-04-06 02:14:05 +02:00
|
|
|
for (auto loop_cfd : *versions_->GetColumnFamilySet()) {
|
|
|
|
// all this is just optimization to delete logs that
|
|
|
|
// are no longer needed -- if CF is empty, that means it
|
|
|
|
// doesn't need that particular log to stay alive, so we just
|
|
|
|
// advance the log number. no need to persist this in the manifest
|
|
|
|
if (loop_cfd->mem()->GetFirstSequenceNumber() == 0 &&
|
|
|
|
loop_cfd->imm()->NumNotFlushed() == 0) {
|
|
|
|
if (creating_new_log) {
|
|
|
|
loop_cfd->SetLogNumber(logfile_number_);
|
|
|
|
}
|
|
|
|
loop_cfd->mem()->SetCreationSeq(versions_->LastSequence());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfd->mem()->SetNextLogNumber(logfile_number_);
|
|
|
|
cfd->imm()->Add(cfd->mem(), &context->memtables_to_free_);
|
|
|
|
new_mem->Ref();
|
|
|
|
cfd->SetMemtable(new_mem);
|
2017-10-06 03:00:38 +02:00
|
|
|
InstallSuperVersionAndScheduleWork(cfd, &context->superversion_context,
|
2018-03-24 01:21:47 +01:00
|
|
|
mutable_cf_options, flush_reason);
|
2017-11-11 02:18:01 +01:00
|
|
|
if (two_write_queues_) {
|
2017-06-28 22:05:52 +02:00
|
|
|
nonmem_write_thread_.ExitUnbatched(&nonmem_w);
|
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DBImpl::GetWalPreallocateBlockSize(uint64_t write_buffer_size) const {
|
|
|
|
mutex_.AssertHeld();
|
2018-03-06 21:27:07 +01:00
|
|
|
size_t bsize = static_cast<size_t>(
|
|
|
|
write_buffer_size / 10 + write_buffer_size);
|
2017-04-06 02:14:05 +02:00
|
|
|
// Some users might set very high write_buffer_size and rely on
|
|
|
|
// max_total_wal_size or other parameters to control the WAL size.
|
|
|
|
if (mutable_db_options_.max_total_wal_size > 0) {
|
2018-03-06 21:27:07 +01:00
|
|
|
bsize = std::min<size_t>(bsize, static_cast<size_t>(
|
|
|
|
mutable_db_options_.max_total_wal_size));
|
2017-04-06 02:14:05 +02:00
|
|
|
}
|
|
|
|
if (immutable_db_options_.db_write_buffer_size > 0) {
|
|
|
|
bsize = std::min<size_t>(bsize, immutable_db_options_.db_write_buffer_size);
|
|
|
|
}
|
|
|
|
if (immutable_db_options_.write_buffer_manager &&
|
|
|
|
immutable_db_options_.write_buffer_manager->enabled()) {
|
|
|
|
bsize = std::min<size_t>(
|
|
|
|
bsize, immutable_db_options_.write_buffer_manager->buffer_size());
|
|
|
|
}
|
|
|
|
|
|
|
|
return bsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default implementations of convenience methods that subclasses of DB
|
|
|
|
// can call if they wish
|
|
|
|
Status DB::Put(const WriteOptions& opt, ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& key, const Slice& value) {
|
|
|
|
// Pre-allocate size of write batch conservatively.
|
|
|
|
// 8 bytes are taken by header, 4 bytes for count, 1 byte for type,
|
|
|
|
// and we allocate 11 extra bytes for key length, as well as value length.
|
|
|
|
WriteBatch batch(key.size() + value.size() + 24);
|
2018-02-09 23:50:09 +01:00
|
|
|
Status s = batch.Put(column_family, key, value);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
return Write(opt, &batch);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DB::Delete(const WriteOptions& opt, ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& key) {
|
|
|
|
WriteBatch batch;
|
|
|
|
batch.Delete(column_family, key);
|
|
|
|
return Write(opt, &batch);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DB::SingleDelete(const WriteOptions& opt,
|
|
|
|
ColumnFamilyHandle* column_family, const Slice& key) {
|
|
|
|
WriteBatch batch;
|
|
|
|
batch.SingleDelete(column_family, key);
|
|
|
|
return Write(opt, &batch);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DB::DeleteRange(const WriteOptions& opt,
|
|
|
|
ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& begin_key, const Slice& end_key) {
|
|
|
|
WriteBatch batch;
|
|
|
|
batch.DeleteRange(column_family, begin_key, end_key);
|
|
|
|
return Write(opt, &batch);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status DB::Merge(const WriteOptions& opt, ColumnFamilyHandle* column_family,
|
|
|
|
const Slice& key, const Slice& value) {
|
|
|
|
WriteBatch batch;
|
2018-02-09 23:50:09 +01:00
|
|
|
Status s = batch.Merge(column_family, key, value);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
2017-04-06 02:14:05 +02:00
|
|
|
return Write(opt, &batch);
|
|
|
|
}
|
|
|
|
} // namespace rocksdb
|