2017-12-12 00:16:37 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
//
|
|
|
|
// 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 "table/block_fetcher.h"
|
|
|
|
|
2019-06-06 22:52:39 +02:00
|
|
|
#include <cinttypes>
|
2019-03-28 00:13:08 +01:00
|
|
|
#include <string>
|
2017-12-12 00:16:37 +01:00
|
|
|
|
2019-06-01 02:19:43 +02:00
|
|
|
#include "logging/logging.h"
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "memory/memory_allocator.h"
|
2017-12-12 00:16:37 +01:00
|
|
|
#include "monitoring/perf_context_imp.h"
|
|
|
|
#include "rocksdb/env.h"
|
2019-05-30 23:47:29 +02:00
|
|
|
#include "table/block_based/block.h"
|
|
|
|
#include "table/block_based/block_based_table_reader.h"
|
2017-12-12 00:16:37 +01:00
|
|
|
#include "table/format.h"
|
2018-10-03 02:21:54 +02:00
|
|
|
#include "table/persistent_cache_helper.h"
|
2017-12-12 00:16:37 +01:00
|
|
|
#include "util/coding.h"
|
|
|
|
#include "util/compression.h"
|
|
|
|
#include "util/crc32c.h"
|
|
|
|
#include "util/stop_watch.h"
|
|
|
|
#include "util/string_util.h"
|
|
|
|
#include "util/xxhash.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-12-12 00:16:37 +01:00
|
|
|
|
2019-03-28 00:13:08 +01:00
|
|
|
inline void BlockFetcher::CheckBlockChecksum() {
|
2017-12-12 00:16:37 +01:00
|
|
|
// Check the crc of the type and the block contents
|
|
|
|
if (read_options_.verify_checksums) {
|
|
|
|
const char* data = slice_.data(); // Pointer to where Read put the data
|
|
|
|
PERF_TIMER_GUARD(block_checksum_time);
|
|
|
|
uint32_t value = DecodeFixed32(data + block_size_ + 1);
|
|
|
|
uint32_t actual = 0;
|
|
|
|
switch (footer_.checksum()) {
|
|
|
|
case kNoChecksum:
|
|
|
|
break;
|
|
|
|
case kCRC32c:
|
|
|
|
value = crc32c::Unmask(value);
|
|
|
|
actual = crc32c::Value(data, block_size_ + 1);
|
|
|
|
break;
|
|
|
|
case kxxHash:
|
|
|
|
actual = XXH32(data, static_cast<int>(block_size_) + 1, 0);
|
|
|
|
break;
|
2018-11-01 23:39:40 +01:00
|
|
|
case kxxHash64:
|
2019-03-28 00:13:08 +01:00
|
|
|
actual = static_cast<uint32_t>(
|
|
|
|
XXH64(data, static_cast<int>(block_size_) + 1, 0) &
|
|
|
|
uint64_t{0xffffffff});
|
2018-11-01 23:39:40 +01:00
|
|
|
break;
|
2017-12-12 00:16:37 +01:00
|
|
|
default:
|
|
|
|
status_ = Status::Corruption(
|
|
|
|
"unknown checksum type " + ToString(footer_.checksum()) + " in " +
|
|
|
|
file_->file_name() + " offset " + ToString(handle_.offset()) +
|
|
|
|
" size " + ToString(block_size_));
|
|
|
|
}
|
|
|
|
if (status_.ok() && actual != value) {
|
|
|
|
status_ = Status::Corruption(
|
|
|
|
"block checksum mismatch: expected " + ToString(actual) + ", got " +
|
|
|
|
ToString(value) + " in " + file_->file_name() + " offset " +
|
|
|
|
ToString(handle_.offset()) + " size " + ToString(block_size_));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 00:13:08 +01:00
|
|
|
inline bool BlockFetcher::TryGetUncompressBlockFromPersistentCache() {
|
2017-12-12 00:16:37 +01:00
|
|
|
if (cache_options_.persistent_cache &&
|
|
|
|
!cache_options_.persistent_cache->IsCompressed()) {
|
|
|
|
Status status = PersistentCacheHelper::LookupUncompressedPage(
|
|
|
|
cache_options_, handle_, contents_);
|
|
|
|
if (status.ok()) {
|
|
|
|
// uncompressed page is found for the block handle
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// uncompressed page is not found
|
|
|
|
if (ioptions_.info_log && !status.IsNotFound()) {
|
|
|
|
assert(!status.ok());
|
|
|
|
ROCKS_LOG_INFO(ioptions_.info_log,
|
|
|
|
"Error reading from persistent cache. %s",
|
|
|
|
status.ToString().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-28 00:13:08 +01:00
|
|
|
inline bool BlockFetcher::TryGetFromPrefetchBuffer() {
|
2017-12-12 00:16:37 +01:00
|
|
|
if (prefetch_buffer_ != nullptr &&
|
|
|
|
prefetch_buffer_->TryReadFromCache(
|
|
|
|
handle_.offset(),
|
2019-06-19 23:07:36 +02:00
|
|
|
static_cast<size_t>(handle_.size()) + kBlockTrailerSize, &slice_,
|
|
|
|
for_compaction_)) {
|
2017-12-12 00:16:37 +01:00
|
|
|
block_size_ = static_cast<size_t>(handle_.size());
|
|
|
|
CheckBlockChecksum();
|
|
|
|
if (!status_.ok()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
got_from_prefetch_buffer_ = true;
|
|
|
|
used_buf_ = const_cast<char*>(slice_.data());
|
|
|
|
}
|
|
|
|
return got_from_prefetch_buffer_;
|
|
|
|
}
|
|
|
|
|
2019-03-28 00:13:08 +01:00
|
|
|
inline bool BlockFetcher::TryGetCompressedBlockFromPersistentCache() {
|
2017-12-12 00:16:37 +01:00
|
|
|
if (cache_options_.persistent_cache &&
|
|
|
|
cache_options_.persistent_cache->IsCompressed()) {
|
|
|
|
// lookup uncompressed cache mode p-cache
|
2018-10-03 02:21:54 +02:00
|
|
|
std::unique_ptr<char[]> raw_data;
|
2017-12-12 00:16:37 +01:00
|
|
|
status_ = PersistentCacheHelper::LookupRawPage(
|
2018-10-03 02:21:54 +02:00
|
|
|
cache_options_, handle_, &raw_data, block_size_ + kBlockTrailerSize);
|
2017-12-12 00:16:37 +01:00
|
|
|
if (status_.ok()) {
|
2018-10-03 02:21:54 +02:00
|
|
|
heap_buf_ = CacheAllocationPtr(raw_data.release());
|
2017-12-12 00:16:37 +01:00
|
|
|
used_buf_ = heap_buf_.get();
|
|
|
|
slice_ = Slice(heap_buf_.get(), block_size_);
|
|
|
|
return true;
|
|
|
|
} else if (!status_.IsNotFound() && ioptions_.info_log) {
|
|
|
|
assert(!status_.ok());
|
|
|
|
ROCKS_LOG_INFO(ioptions_.info_log,
|
|
|
|
"Error reading from persistent cache. %s",
|
|
|
|
status_.ToString().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-28 00:13:08 +01:00
|
|
|
inline void BlockFetcher::PrepareBufferForBlockFromFile() {
|
2017-12-12 00:16:37 +01:00
|
|
|
// cache miss read from device
|
|
|
|
if (do_uncompress_ &&
|
|
|
|
block_size_ + kBlockTrailerSize < kDefaultStackBufferSize) {
|
|
|
|
// If we've got a small enough hunk of data, read it in to the
|
|
|
|
// trivially allocated stack buffer instead of needing a full malloc()
|
|
|
|
used_buf_ = &stack_buf_[0];
|
2018-11-29 02:58:08 +01:00
|
|
|
} else if (maybe_compressed_ && !do_uncompress_) {
|
|
|
|
compressed_buf_ = AllocateBlock(block_size_ + kBlockTrailerSize,
|
|
|
|
memory_allocator_compressed_);
|
|
|
|
used_buf_ = compressed_buf_.get();
|
2017-12-12 00:16:37 +01:00
|
|
|
} else {
|
2018-11-29 02:58:08 +01:00
|
|
|
heap_buf_ =
|
|
|
|
AllocateBlock(block_size_ + kBlockTrailerSize, memory_allocator_);
|
2017-12-12 00:16:37 +01:00
|
|
|
used_buf_ = heap_buf_.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 00:13:08 +01:00
|
|
|
inline void BlockFetcher::InsertCompressedBlockToPersistentCacheIfNeeded() {
|
2017-12-12 00:16:37 +01:00
|
|
|
if (status_.ok() && read_options_.fill_cache &&
|
|
|
|
cache_options_.persistent_cache &&
|
|
|
|
cache_options_.persistent_cache->IsCompressed()) {
|
|
|
|
// insert to raw cache
|
|
|
|
PersistentCacheHelper::InsertRawPage(cache_options_, handle_, used_buf_,
|
|
|
|
block_size_ + kBlockTrailerSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 00:13:08 +01:00
|
|
|
inline void BlockFetcher::InsertUncompressedBlockToPersistentCacheIfNeeded() {
|
2017-12-12 00:16:37 +01:00
|
|
|
if (status_.ok() && !got_from_prefetch_buffer_ && read_options_.fill_cache &&
|
|
|
|
cache_options_.persistent_cache &&
|
|
|
|
!cache_options_.persistent_cache->IsCompressed()) {
|
|
|
|
// insert to uncompressed cache
|
|
|
|
PersistentCacheHelper::InsertUncompressedPage(cache_options_, handle_,
|
|
|
|
*contents_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 02:58:08 +01:00
|
|
|
inline void BlockFetcher::CopyBufferToHeap() {
|
|
|
|
assert(used_buf_ != heap_buf_.get());
|
|
|
|
heap_buf_ = AllocateBlock(block_size_ + kBlockTrailerSize, memory_allocator_);
|
|
|
|
memcpy(heap_buf_.get(), used_buf_, block_size_ + kBlockTrailerSize);
|
|
|
|
}
|
|
|
|
|
2019-03-28 00:13:08 +01:00
|
|
|
inline void BlockFetcher::GetBlockContents() {
|
2017-12-12 00:16:37 +01:00
|
|
|
if (slice_.data() != used_buf_) {
|
|
|
|
// the slice content is not the buffer provided
|
2018-11-14 02:00:49 +01:00
|
|
|
*contents_ = BlockContents(Slice(slice_.data(), block_size_));
|
2017-12-12 00:16:37 +01:00
|
|
|
} else {
|
2018-07-06 22:09:57 +02:00
|
|
|
// page can be either uncompressed or compressed, the buffer either stack
|
|
|
|
// or heap provided. Refer to https://github.com/facebook/rocksdb/pull/4096
|
2017-12-12 00:16:37 +01:00
|
|
|
if (got_from_prefetch_buffer_ || used_buf_ == &stack_buf_[0]) {
|
2018-11-29 02:58:08 +01:00
|
|
|
CopyBufferToHeap();
|
|
|
|
} else if (used_buf_ == compressed_buf_.get()) {
|
|
|
|
if (compression_type_ == kNoCompression &&
|
|
|
|
memory_allocator_ != memory_allocator_compressed_) {
|
|
|
|
CopyBufferToHeap();
|
|
|
|
} else {
|
|
|
|
heap_buf_ = std::move(compressed_buf_);
|
|
|
|
}
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
2018-11-14 02:00:49 +01:00
|
|
|
*contents_ = BlockContents(std::move(heap_buf_), block_size_);
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
2018-11-14 02:00:49 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
contents_->is_raw_block = true;
|
|
|
|
#endif
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status BlockFetcher::ReadBlockContents() {
|
|
|
|
block_size_ = static_cast<size_t>(handle_.size());
|
|
|
|
|
|
|
|
if (TryGetUncompressBlockFromPersistentCache()) {
|
2018-11-14 02:00:49 +01:00
|
|
|
compression_type_ = kNoCompression;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
contents_->is_raw_block = true;
|
|
|
|
#endif // NDEBUG
|
2017-12-12 00:16:37 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
if (TryGetFromPrefetchBuffer()) {
|
|
|
|
if (!status_.ok()) {
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
} else if (!TryGetCompressedBlockFromPersistentCache()) {
|
|
|
|
PrepareBufferForBlockFromFile();
|
|
|
|
Status s;
|
|
|
|
|
|
|
|
{
|
|
|
|
PERF_TIMER_GUARD(block_read_time);
|
|
|
|
// Actual file read
|
|
|
|
status_ = file_->Read(handle_.offset(), block_size_ + kBlockTrailerSize,
|
2020-03-06 23:02:09 +01:00
|
|
|
&slice_, used_buf_, nullptr, for_compaction_);
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
|
|
|
PERF_COUNTER_ADD(block_read_count, 1);
|
2019-06-19 04:00:03 +02:00
|
|
|
|
|
|
|
// TODO: introduce dedicated perf counter for range tombstones
|
|
|
|
switch (block_type_) {
|
|
|
|
case BlockType::kFilter:
|
|
|
|
PERF_COUNTER_ADD(filter_block_read_count, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BlockType::kCompressionDictionary:
|
|
|
|
PERF_COUNTER_ADD(compression_dict_block_read_count, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BlockType::kIndex:
|
|
|
|
PERF_COUNTER_ADD(index_block_read_count, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Nothing to do here as we don't have counters for the other types.
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-12-12 00:16:37 +01:00
|
|
|
PERF_COUNTER_ADD(block_read_byte, block_size_ + kBlockTrailerSize);
|
|
|
|
if (!status_.ok()) {
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slice_.size() != block_size_ + kBlockTrailerSize) {
|
|
|
|
return Status::Corruption("truncated block read from " +
|
|
|
|
file_->file_name() + " offset " +
|
|
|
|
ToString(handle_.offset()) + ", expected " +
|
|
|
|
ToString(block_size_ + kBlockTrailerSize) +
|
|
|
|
" bytes, got " + ToString(slice_.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckBlockChecksum();
|
|
|
|
if (status_.ok()) {
|
|
|
|
InsertCompressedBlockToPersistentCacheIfNeeded();
|
|
|
|
} else {
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PERF_TIMER_GUARD(block_decompress_time);
|
|
|
|
|
2018-11-14 02:00:49 +01:00
|
|
|
compression_type_ = get_block_compression_type(slice_.data(), block_size_);
|
2017-12-12 00:16:37 +01:00
|
|
|
|
2018-11-14 02:00:49 +01:00
|
|
|
if (do_uncompress_ && compression_type_ != kNoCompression) {
|
2017-12-12 00:16:37 +01:00
|
|
|
// compressed page, uncompress, update cache
|
2019-01-19 04:10:17 +01:00
|
|
|
UncompressionContext context(compression_type_);
|
2019-01-24 03:11:08 +01:00
|
|
|
UncompressionInfo info(context, uncompression_dict_, compression_type_);
|
2019-01-19 04:10:17 +01:00
|
|
|
status_ = UncompressBlockContents(info, slice_.data(), block_size_,
|
|
|
|
contents_, footer_.version(), ioptions_,
|
|
|
|
memory_allocator_);
|
2018-11-14 02:00:49 +01:00
|
|
|
compression_type_ = kNoCompression;
|
2017-12-12 00:16:37 +01:00
|
|
|
} else {
|
|
|
|
GetBlockContents();
|
|
|
|
}
|
|
|
|
|
|
|
|
InsertUncompressedBlockToPersistentCacheIfNeeded();
|
|
|
|
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|