diff --git a/db/db_iter.cc b/db/db_iter.cc index 3b2035e9e3..4d0f42e424 100644 --- a/db/db_iter.cc +++ b/db/db_iter.cc @@ -57,7 +57,7 @@ class DBIter: public Iterator { direction_(kForward), valid_(false), rnd_(seed), - bytes_counter_(RandomPeriod()) { + bytes_until_read_sampling_(RandomCompactionPeriod()) { } virtual ~DBIter() { delete iter_; @@ -103,8 +103,8 @@ class DBIter: public Iterator { } } - // Pick next gap with average value of config::kReadBytesPeriod. - ssize_t RandomPeriod() { + // Picks the number of bytes that can be read until a compaction is scheduled. + size_t RandomCompactionPeriod() { return rnd_.Uniform(2*config::kReadBytesPeriod); } @@ -120,7 +120,7 @@ class DBIter: public Iterator { bool valid_; Random rnd_; - ssize_t bytes_counter_; + size_t bytes_until_read_sampling_; // No copying allowed DBIter(const DBIter&); @@ -129,12 +129,15 @@ class DBIter: public Iterator { inline bool DBIter::ParseKey(ParsedInternalKey* ikey) { Slice k = iter_->key(); - ssize_t n = k.size() + iter_->value().size(); - bytes_counter_ -= n; - while (bytes_counter_ < 0) { - bytes_counter_ += RandomPeriod(); + + size_t bytes_read = k.size() + iter_->value().size(); + while (bytes_until_read_sampling_ < bytes_read) { + bytes_until_read_sampling_ += RandomCompactionPeriod(); db_->RecordReadSample(k); } + assert(bytes_until_read_sampling_ >= bytes_read); + bytes_until_read_sampling_ -= bytes_read; + if (!ParseInternalKey(k, ikey)) { status_ = Status::Corruption("corrupted internal key in DBIter"); return false; diff --git a/db/fault_injection_test.cc b/db/fault_injection_test.cc index 7894999ccf..b3429ac922 100644 --- a/db/fault_injection_test.cc +++ b/db/fault_injection_test.cc @@ -85,9 +85,9 @@ Status Truncate(const std::string& filename, uint64_t length) { struct FileState { std::string filename_; - ssize_t pos_; - ssize_t pos_at_last_sync_; - ssize_t pos_at_last_flush_; + int64_t pos_; + int64_t pos_at_last_sync_; + int64_t pos_at_last_flush_; FileState(const std::string& filename) : filename_(filename), @@ -360,7 +360,7 @@ void FaultInjectionTestEnv::WritableFileClosed(const FileState& state) { } Status FileState::DropUnsyncedData() const { - ssize_t sync_pos = pos_at_last_sync_ == -1 ? 0 : pos_at_last_sync_; + int64_t sync_pos = pos_at_last_sync_ == -1 ? 0 : pos_at_last_sync_; return Truncate(filename_, sync_pos); }