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"
|
Improve / clean up meta block code & integrity (#9163)
Summary:
* Checksums are now checked on meta blocks unless specifically
suppressed or not applicable (e.g. plain table). (Was other way around.)
This means a number of cases that were not checking checksums now are,
including direct read TableProperties in Version::GetTableProperties
(fixed in meta_blocks ReadTableProperties), reading any block from
PersistentCache (fixed in BlockFetcher), read TableProperties in
SstFileDumper (ldb/sst_dump/BackupEngine) before table reader open,
maybe more.
* For that to work, I moved the global_seqno+TableProperties checksum
logic to the shared table/ code, because that is used by many utilies
such as SstFileDumper.
* Also for that to work, we have to know when we're dealing with a block
that has a checksum (trailer), so added that capability to Footer based
on magic number, and from there BlockFetcher.
* Knowledge of trailer presence has also fixed a problem where other
table formats were reading blocks including bytes for a non-existant
trailer--and awkwardly kind-of not using them, e.g. no shared code
checking checksums. (BlockFetcher compression type was populated
incorrectly.) Now we only read what is needed.
* Minimized code duplication and differing/incompatible/awkward
abstractions in meta_blocks.{cc,h} (e.g. SeekTo in metaindex block
without parsing block handle)
* Moved some meta block handling code from table_properties*.*
* Moved some code specific to block-based table from shared table/ code
to BlockBasedTable class. The checksum stuff means we can't completely
separate it, but things that don't need to be in shared table/ code
should not be.
* Use unique_ptr rather than raw ptr in more places. (Note: you can
std::move from unique_ptr to shared_ptr.)
Without enhancements to GetPropertiesOfAllTablesTest (see below),
net reduction of roughly 100 lines of code.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/9163
Test Plan:
existing tests and
* Enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to verify that
checksums are now checked on direct read of table properties by TableCache
(new test would fail before this change)
* Also enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to test
putting table properties under old meta name
* Also generally enhanced that same test to actually test what it was
supposed to be testing already, by kicking things out of table cache when
we don't want them there.
Reviewed By: ajkr, mrambacher
Differential Revision: D32514757
Pulled By: pdillinger
fbshipit-source-id: 507964b9311d186ae8d1131182290cbd97a99fa9
2021-11-18 20:42:12 +01:00
|
|
|
#include "rocksdb/compression_type.h"
|
2017-12-12 00:16:37 +01:00
|
|
|
#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"
|
Improve / clean up meta block code & integrity (#9163)
Summary:
* Checksums are now checked on meta blocks unless specifically
suppressed or not applicable (e.g. plain table). (Was other way around.)
This means a number of cases that were not checking checksums now are,
including direct read TableProperties in Version::GetTableProperties
(fixed in meta_blocks ReadTableProperties), reading any block from
PersistentCache (fixed in BlockFetcher), read TableProperties in
SstFileDumper (ldb/sst_dump/BackupEngine) before table reader open,
maybe more.
* For that to work, I moved the global_seqno+TableProperties checksum
logic to the shared table/ code, because that is used by many utilies
such as SstFileDumper.
* Also for that to work, we have to know when we're dealing with a block
that has a checksum (trailer), so added that capability to Footer based
on magic number, and from there BlockFetcher.
* Knowledge of trailer presence has also fixed a problem where other
table formats were reading blocks including bytes for a non-existant
trailer--and awkwardly kind-of not using them, e.g. no shared code
checking checksums. (BlockFetcher compression type was populated
incorrectly.) Now we only read what is needed.
* Minimized code duplication and differing/incompatible/awkward
abstractions in meta_blocks.{cc,h} (e.g. SeekTo in metaindex block
without parsing block handle)
* Moved some meta block handling code from table_properties*.*
* Moved some code specific to block-based table from shared table/ code
to BlockBasedTable class. The checksum stuff means we can't completely
separate it, but things that don't need to be in shared table/ code
should not be.
* Use unique_ptr rather than raw ptr in more places. (Note: you can
std::move from unique_ptr to shared_ptr.)
Without enhancements to GetPropertiesOfAllTablesTest (see below),
net reduction of roughly 100 lines of code.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/9163
Test Plan:
existing tests and
* Enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to verify that
checksums are now checked on direct read of table properties by TableCache
(new test would fail before this change)
* Also enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to test
putting table properties under old meta name
* Also generally enhanced that same test to actually test what it was
supposed to be testing already, by kicking things out of table cache when
we don't want them there.
Reviewed By: ajkr, mrambacher
Differential Revision: D32514757
Pulled By: pdillinger
fbshipit-source-id: 507964b9311d186ae8d1131182290cbd97a99fa9
2021-11-18 20:42:12 +01:00
|
|
|
#include "table/block_based/block_type.h"
|
2020-06-20 01:16:57 +02:00
|
|
|
#include "table/block_based/reader_common.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/compression.h"
|
|
|
|
#include "util/stop_watch.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2017-12-12 00:16:37 +01:00
|
|
|
|
Improve / clean up meta block code & integrity (#9163)
Summary:
* Checksums are now checked on meta blocks unless specifically
suppressed or not applicable (e.g. plain table). (Was other way around.)
This means a number of cases that were not checking checksums now are,
including direct read TableProperties in Version::GetTableProperties
(fixed in meta_blocks ReadTableProperties), reading any block from
PersistentCache (fixed in BlockFetcher), read TableProperties in
SstFileDumper (ldb/sst_dump/BackupEngine) before table reader open,
maybe more.
* For that to work, I moved the global_seqno+TableProperties checksum
logic to the shared table/ code, because that is used by many utilies
such as SstFileDumper.
* Also for that to work, we have to know when we're dealing with a block
that has a checksum (trailer), so added that capability to Footer based
on magic number, and from there BlockFetcher.
* Knowledge of trailer presence has also fixed a problem where other
table formats were reading blocks including bytes for a non-existant
trailer--and awkwardly kind-of not using them, e.g. no shared code
checking checksums. (BlockFetcher compression type was populated
incorrectly.) Now we only read what is needed.
* Minimized code duplication and differing/incompatible/awkward
abstractions in meta_blocks.{cc,h} (e.g. SeekTo in metaindex block
without parsing block handle)
* Moved some meta block handling code from table_properties*.*
* Moved some code specific to block-based table from shared table/ code
to BlockBasedTable class. The checksum stuff means we can't completely
separate it, but things that don't need to be in shared table/ code
should not be.
* Use unique_ptr rather than raw ptr in more places. (Note: you can
std::move from unique_ptr to shared_ptr.)
Without enhancements to GetPropertiesOfAllTablesTest (see below),
net reduction of roughly 100 lines of code.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/9163
Test Plan:
existing tests and
* Enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to verify that
checksums are now checked on direct read of table properties by TableCache
(new test would fail before this change)
* Also enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to test
putting table properties under old meta name
* Also generally enhanced that same test to actually test what it was
supposed to be testing already, by kicking things out of table cache when
we don't want them there.
Reviewed By: ajkr, mrambacher
Differential Revision: D32514757
Pulled By: pdillinger
fbshipit-source-id: 507964b9311d186ae8d1131182290cbd97a99fa9
2021-11-18 20:42:12 +01:00
|
|
|
inline void BlockFetcher::ProcessTrailerIfPresent() {
|
|
|
|
if (footer_.GetBlockTrailerSize() > 0) {
|
|
|
|
assert(footer_.GetBlockTrailerSize() == BlockBasedTable::kBlockTrailerSize);
|
|
|
|
if (read_options_.verify_checksums) {
|
|
|
|
io_status_ = status_to_io_status(
|
|
|
|
VerifyBlockChecksum(footer_.checksum(), slice_.data(), block_size_,
|
|
|
|
file_->file_name(), handle_.offset()));
|
|
|
|
}
|
|
|
|
compression_type_ =
|
|
|
|
BlockBasedTable::GetBlockCompressionType(slice_.data(), block_size_);
|
|
|
|
} else {
|
|
|
|
// E.g. plain table or cuckoo table
|
|
|
|
compression_type_ = kNoCompression;
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2021-04-26 21:43:02 +02:00
|
|
|
if (ioptions_.logger && !status.IsNotFound()) {
|
2017-12-12 00:16:37 +01:00
|
|
|
assert(!status.ok());
|
2021-04-26 21:43:02 +02:00
|
|
|
ROCKS_LOG_INFO(ioptions_.logger,
|
2017-12-12 00:16:37 +01:00
|
|
|
"Error reading from persistent cache. %s",
|
|
|
|
status.ToString().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-28 00:13:08 +01:00
|
|
|
inline bool BlockFetcher::TryGetFromPrefetchBuffer() {
|
2020-06-29 23:51:57 +02:00
|
|
|
if (prefetch_buffer_ != nullptr) {
|
|
|
|
IOOptions opts;
|
2021-04-01 19:06:55 +02:00
|
|
|
IOStatus io_s = file_->PrepareIOOptions(read_options_, opts);
|
2021-11-20 02:52:42 +01:00
|
|
|
if (io_s.ok() &&
|
|
|
|
prefetch_buffer_->TryReadFromCache(opts, file_, handle_.offset(),
|
|
|
|
block_size_with_trailer_, &slice_,
|
|
|
|
&io_s, for_compaction_)) {
|
Improve / clean up meta block code & integrity (#9163)
Summary:
* Checksums are now checked on meta blocks unless specifically
suppressed or not applicable (e.g. plain table). (Was other way around.)
This means a number of cases that were not checking checksums now are,
including direct read TableProperties in Version::GetTableProperties
(fixed in meta_blocks ReadTableProperties), reading any block from
PersistentCache (fixed in BlockFetcher), read TableProperties in
SstFileDumper (ldb/sst_dump/BackupEngine) before table reader open,
maybe more.
* For that to work, I moved the global_seqno+TableProperties checksum
logic to the shared table/ code, because that is used by many utilies
such as SstFileDumper.
* Also for that to work, we have to know when we're dealing with a block
that has a checksum (trailer), so added that capability to Footer based
on magic number, and from there BlockFetcher.
* Knowledge of trailer presence has also fixed a problem where other
table formats were reading blocks including bytes for a non-existant
trailer--and awkwardly kind-of not using them, e.g. no shared code
checking checksums. (BlockFetcher compression type was populated
incorrectly.) Now we only read what is needed.
* Minimized code duplication and differing/incompatible/awkward
abstractions in meta_blocks.{cc,h} (e.g. SeekTo in metaindex block
without parsing block handle)
* Moved some meta block handling code from table_properties*.*
* Moved some code specific to block-based table from shared table/ code
to BlockBasedTable class. The checksum stuff means we can't completely
separate it, but things that don't need to be in shared table/ code
should not be.
* Use unique_ptr rather than raw ptr in more places. (Note: you can
std::move from unique_ptr to shared_ptr.)
Without enhancements to GetPropertiesOfAllTablesTest (see below),
net reduction of roughly 100 lines of code.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/9163
Test Plan:
existing tests and
* Enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to verify that
checksums are now checked on direct read of table properties by TableCache
(new test would fail before this change)
* Also enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to test
putting table properties under old meta name
* Also generally enhanced that same test to actually test what it was
supposed to be testing already, by kicking things out of table cache when
we don't want them there.
Reviewed By: ajkr, mrambacher
Differential Revision: D32514757
Pulled By: pdillinger
fbshipit-source-id: 507964b9311d186ae8d1131182290cbd97a99fa9
2021-11-18 20:42:12 +01:00
|
|
|
ProcessTrailerIfPresent();
|
2021-04-01 19:06:55 +02:00
|
|
|
if (!io_status_.ok()) {
|
2020-06-29 23:51:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
got_from_prefetch_buffer_ = true;
|
|
|
|
used_buf_ = const_cast<char*>(slice_.data());
|
2021-04-01 19:06:55 +02:00
|
|
|
} else if (!io_s.ok()) {
|
|
|
|
io_status_ = io_s;
|
2020-12-30 18:24:04 +01:00
|
|
|
return true;
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
2021-04-01 19:06:55 +02:00
|
|
|
io_status_ = status_to_io_status(PersistentCacheHelper::LookupRawPage(
|
|
|
|
cache_options_, handle_, &raw_data, block_size_with_trailer_));
|
|
|
|
if (io_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_);
|
Improve / clean up meta block code & integrity (#9163)
Summary:
* Checksums are now checked on meta blocks unless specifically
suppressed or not applicable (e.g. plain table). (Was other way around.)
This means a number of cases that were not checking checksums now are,
including direct read TableProperties in Version::GetTableProperties
(fixed in meta_blocks ReadTableProperties), reading any block from
PersistentCache (fixed in BlockFetcher), read TableProperties in
SstFileDumper (ldb/sst_dump/BackupEngine) before table reader open,
maybe more.
* For that to work, I moved the global_seqno+TableProperties checksum
logic to the shared table/ code, because that is used by many utilies
such as SstFileDumper.
* Also for that to work, we have to know when we're dealing with a block
that has a checksum (trailer), so added that capability to Footer based
on magic number, and from there BlockFetcher.
* Knowledge of trailer presence has also fixed a problem where other
table formats were reading blocks including bytes for a non-existant
trailer--and awkwardly kind-of not using them, e.g. no shared code
checking checksums. (BlockFetcher compression type was populated
incorrectly.) Now we only read what is needed.
* Minimized code duplication and differing/incompatible/awkward
abstractions in meta_blocks.{cc,h} (e.g. SeekTo in metaindex block
without parsing block handle)
* Moved some meta block handling code from table_properties*.*
* Moved some code specific to block-based table from shared table/ code
to BlockBasedTable class. The checksum stuff means we can't completely
separate it, but things that don't need to be in shared table/ code
should not be.
* Use unique_ptr rather than raw ptr in more places. (Note: you can
std::move from unique_ptr to shared_ptr.)
Without enhancements to GetPropertiesOfAllTablesTest (see below),
net reduction of roughly 100 lines of code.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/9163
Test Plan:
existing tests and
* Enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to verify that
checksums are now checked on direct read of table properties by TableCache
(new test would fail before this change)
* Also enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to test
putting table properties under old meta name
* Also generally enhanced that same test to actually test what it was
supposed to be testing already, by kicking things out of table cache when
we don't want them there.
Reviewed By: ajkr, mrambacher
Differential Revision: D32514757
Pulled By: pdillinger
fbshipit-source-id: 507964b9311d186ae8d1131182290cbd97a99fa9
2021-11-18 20:42:12 +01:00
|
|
|
ProcessTrailerIfPresent();
|
2017-12-12 00:16:37 +01:00
|
|
|
return true;
|
2021-04-26 21:43:02 +02:00
|
|
|
} else if (!io_status_.IsNotFound() && ioptions_.logger) {
|
2021-04-01 19:06:55 +02:00
|
|
|
assert(!io_status_.ok());
|
2021-04-26 21:43:02 +02:00
|
|
|
ROCKS_LOG_INFO(ioptions_.logger,
|
2017-12-12 00:16:37 +01:00
|
|
|
"Error reading from persistent cache. %s",
|
2021-04-01 19:06:55 +02:00
|
|
|
io_status_.ToString().c_str());
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
2020-07-01 00:38:59 +02:00
|
|
|
if ((do_uncompress_ || ioptions_.allow_mmap_reads) &&
|
2020-04-25 00:30:12 +02:00
|
|
|
block_size_with_trailer_ < kDefaultStackBufferSize) {
|
2017-12-12 00:16:37 +01:00
|
|
|
// 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()
|
2020-07-01 00:38:59 +02:00
|
|
|
//
|
|
|
|
// `GetBlockContents()` cannot return this data as its lifetime is tied to
|
|
|
|
// this `BlockFetcher`'s lifetime. That is fine because this is only used
|
|
|
|
// in cases where we do not expect the `GetBlockContents()` result to be the
|
|
|
|
// same buffer we are assigning here. If we guess incorrectly, there will be
|
|
|
|
// a heap allocation and memcpy in `GetBlockContents()` to obtain the final
|
|
|
|
// result. Considering we are eliding a heap allocation here by using the
|
|
|
|
// stack buffer, the cost of guessing incorrectly here is one extra memcpy.
|
|
|
|
//
|
|
|
|
// When `do_uncompress_` is true, we expect the uncompression step will
|
|
|
|
// allocate heap memory for the final result. However this expectation will
|
|
|
|
// be wrong if the block turns out to already be uncompressed, which we
|
|
|
|
// won't know for sure until after reading it.
|
|
|
|
//
|
|
|
|
// When `ioptions_.allow_mmap_reads` is true, we do not expect the file
|
|
|
|
// reader to use the scratch buffer at all, but instead return a pointer
|
|
|
|
// into the mapped memory. This expectation will be wrong when using a
|
|
|
|
// file reader that does not implement mmap reads properly.
|
2017-12-12 00:16:37 +01:00
|
|
|
used_buf_ = &stack_buf_[0];
|
2018-11-29 02:58:08 +01:00
|
|
|
} else if (maybe_compressed_ && !do_uncompress_) {
|
2020-04-25 00:30:12 +02:00
|
|
|
compressed_buf_ = AllocateBlock(block_size_with_trailer_,
|
2018-11-29 02:58:08 +01:00
|
|
|
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_ =
|
2020-04-25 00:30:12 +02:00
|
|
|
AllocateBlock(block_size_with_trailer_, 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() {
|
2021-04-01 19:06:55 +02:00
|
|
|
if (io_status_.ok() && read_options_.fill_cache &&
|
2017-12-12 00:16:37 +01:00
|
|
|
cache_options_.persistent_cache &&
|
|
|
|
cache_options_.persistent_cache->IsCompressed()) {
|
|
|
|
// insert to raw cache
|
|
|
|
PersistentCacheHelper::InsertRawPage(cache_options_, handle_, used_buf_,
|
2020-04-25 00:30:12 +02:00
|
|
|
block_size_with_trailer_);
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 00:13:08 +01:00
|
|
|
inline void BlockFetcher::InsertUncompressedBlockToPersistentCacheIfNeeded() {
|
2021-04-01 19:06:55 +02:00
|
|
|
if (io_status_.ok() && !got_from_prefetch_buffer_ &&
|
|
|
|
read_options_.fill_cache && cache_options_.persistent_cache &&
|
2017-12-12 00:16:37 +01:00
|
|
|
!cache_options_.persistent_cache->IsCompressed()) {
|
|
|
|
// insert to uncompressed cache
|
|
|
|
PersistentCacheHelper::InsertUncompressedPage(cache_options_, handle_,
|
|
|
|
*contents_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 00:30:12 +02:00
|
|
|
inline void BlockFetcher::CopyBufferToHeapBuf() {
|
2018-11-29 02:58:08 +01:00
|
|
|
assert(used_buf_ != heap_buf_.get());
|
2020-04-25 00:30:12 +02:00
|
|
|
heap_buf_ = AllocateBlock(block_size_with_trailer_, memory_allocator_);
|
|
|
|
memcpy(heap_buf_.get(), used_buf_, block_size_with_trailer_);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
num_heap_buf_memcpy_++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void BlockFetcher::CopyBufferToCompressedBuf() {
|
|
|
|
assert(used_buf_ != compressed_buf_.get());
|
|
|
|
compressed_buf_ = AllocateBlock(block_size_with_trailer_,
|
|
|
|
memory_allocator_compressed_);
|
|
|
|
memcpy(compressed_buf_.get(), used_buf_, block_size_with_trailer_);
|
|
|
|
#ifndef NDEBUG
|
|
|
|
num_compressed_buf_memcpy_++;
|
|
|
|
#endif
|
2018-11-29 02:58:08 +01:00
|
|
|
}
|
|
|
|
|
2020-04-25 00:30:12 +02:00
|
|
|
// Entering this method means the block is not compressed or do not need to be
|
|
|
|
// uncompressed. The block can be in one of the following buffers:
|
|
|
|
// 1. prefetch buffer if prefetch is enabled and the block is prefetched before
|
|
|
|
// 2. stack_buf_ if block size is smaller than the stack_buf_ size and block
|
|
|
|
// is not compressed
|
|
|
|
// 3. heap_buf_ if the block is not compressed
|
|
|
|
// 4. compressed_buf_ if the block is compressed
|
|
|
|
// 5. direct_io_buf_ if direct IO is enabled
|
|
|
|
// After this method, if the block is compressed, it should be in
|
|
|
|
// compressed_buf_, otherwise should be in heap_buf_.
|
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]) {
|
2020-04-25 00:30:12 +02:00
|
|
|
CopyBufferToHeapBuf();
|
2018-11-29 02:58:08 +01:00
|
|
|
} else if (used_buf_ == compressed_buf_.get()) {
|
|
|
|
if (compression_type_ == kNoCompression &&
|
|
|
|
memory_allocator_ != memory_allocator_compressed_) {
|
2020-04-25 00:30:12 +02:00
|
|
|
CopyBufferToHeapBuf();
|
2018-11-29 02:58:08 +01:00
|
|
|
} else {
|
|
|
|
heap_buf_ = std::move(compressed_buf_);
|
|
|
|
}
|
2020-04-25 00:30:12 +02:00
|
|
|
} else if (direct_io_buf_.get() != nullptr) {
|
|
|
|
if (compression_type_ == kNoCompression) {
|
|
|
|
CopyBufferToHeapBuf();
|
|
|
|
} else {
|
|
|
|
CopyBufferToCompressedBuf();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-01 19:06:55 +02:00
|
|
|
IOStatus BlockFetcher::ReadBlockContents() {
|
2017-12-12 00:16:37 +01:00
|
|
|
if (TryGetUncompressBlockFromPersistentCache()) {
|
2018-11-14 02:00:49 +01:00
|
|
|
compression_type_ = kNoCompression;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
contents_->is_raw_block = true;
|
|
|
|
#endif // NDEBUG
|
2021-04-01 19:06:55 +02:00
|
|
|
return IOStatus::OK();
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
|
|
|
if (TryGetFromPrefetchBuffer()) {
|
2021-04-01 19:06:55 +02:00
|
|
|
if (!io_status_.ok()) {
|
|
|
|
return io_status_;
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
|
|
|
} else if (!TryGetCompressedBlockFromPersistentCache()) {
|
2020-04-30 23:48:51 +02:00
|
|
|
IOOptions opts;
|
2021-04-01 19:06:55 +02:00
|
|
|
io_status_ = file_->PrepareIOOptions(read_options_, opts);
|
2020-04-30 23:48:51 +02:00
|
|
|
// Actual file read
|
2021-04-01 19:06:55 +02:00
|
|
|
if (io_status_.ok()) {
|
2020-04-30 23:48:51 +02:00
|
|
|
if (file_->use_direct_io()) {
|
|
|
|
PERF_TIMER_GUARD(block_read_time);
|
2021-04-01 19:06:55 +02:00
|
|
|
io_status_ =
|
2020-04-30 23:48:51 +02:00
|
|
|
file_->Read(opts, handle_.offset(), block_size_with_trailer_,
|
|
|
|
&slice_, nullptr, &direct_io_buf_, for_compaction_);
|
|
|
|
PERF_COUNTER_ADD(block_read_count, 1);
|
|
|
|
used_buf_ = const_cast<char*>(slice_.data());
|
|
|
|
} else {
|
|
|
|
PrepareBufferForBlockFromFile();
|
|
|
|
PERF_TIMER_GUARD(block_read_time);
|
2021-04-01 19:06:55 +02:00
|
|
|
io_status_ =
|
|
|
|
file_->Read(opts, handle_.offset(), block_size_with_trailer_,
|
|
|
|
&slice_, used_buf_, nullptr, for_compaction_);
|
2020-04-30 23:48:51 +02:00
|
|
|
PERF_COUNTER_ADD(block_read_count, 1);
|
2020-04-25 00:30:12 +02:00
|
|
|
#ifndef NDEBUG
|
2020-07-01 00:38:59 +02:00
|
|
|
if (slice_.data() == &stack_buf_[0]) {
|
2020-04-30 23:48:51 +02:00
|
|
|
num_stack_buf_memcpy_++;
|
2020-07-01 00:38:59 +02:00
|
|
|
} else if (slice_.data() == heap_buf_.get()) {
|
2020-04-30 23:48:51 +02:00
|
|
|
num_heap_buf_memcpy_++;
|
2020-07-01 00:38:59 +02:00
|
|
|
} else if (slice_.data() == compressed_buf_.get()) {
|
2020-04-30 23:48:51 +02:00
|
|
|
num_compressed_buf_memcpy_++;
|
|
|
|
}
|
2020-04-25 00:30:12 +02:00
|
|
|
#endif
|
2020-04-30 23:48:51 +02:00
|
|
|
}
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-25 00:30:12 +02:00
|
|
|
PERF_COUNTER_ADD(block_read_byte, block_size_with_trailer_);
|
2021-04-01 19:06:55 +02:00
|
|
|
if (!io_status_.ok()) {
|
|
|
|
return io_status_;
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
|
|
|
|
2020-04-25 00:30:12 +02:00
|
|
|
if (slice_.size() != block_size_with_trailer_) {
|
2021-04-01 19:06:55 +02:00
|
|
|
return IOStatus::Corruption("truncated block read from " +
|
|
|
|
file_->file_name() + " offset " +
|
|
|
|
ToString(handle_.offset()) + ", expected " +
|
|
|
|
ToString(block_size_with_trailer_) +
|
|
|
|
" bytes, got " + ToString(slice_.size()));
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
|
|
|
|
Improve / clean up meta block code & integrity (#9163)
Summary:
* Checksums are now checked on meta blocks unless specifically
suppressed or not applicable (e.g. plain table). (Was other way around.)
This means a number of cases that were not checking checksums now are,
including direct read TableProperties in Version::GetTableProperties
(fixed in meta_blocks ReadTableProperties), reading any block from
PersistentCache (fixed in BlockFetcher), read TableProperties in
SstFileDumper (ldb/sst_dump/BackupEngine) before table reader open,
maybe more.
* For that to work, I moved the global_seqno+TableProperties checksum
logic to the shared table/ code, because that is used by many utilies
such as SstFileDumper.
* Also for that to work, we have to know when we're dealing with a block
that has a checksum (trailer), so added that capability to Footer based
on magic number, and from there BlockFetcher.
* Knowledge of trailer presence has also fixed a problem where other
table formats were reading blocks including bytes for a non-existant
trailer--and awkwardly kind-of not using them, e.g. no shared code
checking checksums. (BlockFetcher compression type was populated
incorrectly.) Now we only read what is needed.
* Minimized code duplication and differing/incompatible/awkward
abstractions in meta_blocks.{cc,h} (e.g. SeekTo in metaindex block
without parsing block handle)
* Moved some meta block handling code from table_properties*.*
* Moved some code specific to block-based table from shared table/ code
to BlockBasedTable class. The checksum stuff means we can't completely
separate it, but things that don't need to be in shared table/ code
should not be.
* Use unique_ptr rather than raw ptr in more places. (Note: you can
std::move from unique_ptr to shared_ptr.)
Without enhancements to GetPropertiesOfAllTablesTest (see below),
net reduction of roughly 100 lines of code.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/9163
Test Plan:
existing tests and
* Enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to verify that
checksums are now checked on direct read of table properties by TableCache
(new test would fail before this change)
* Also enhanced DBTablePropertiesTest.GetPropertiesOfAllTablesTest to test
putting table properties under old meta name
* Also generally enhanced that same test to actually test what it was
supposed to be testing already, by kicking things out of table cache when
we don't want them there.
Reviewed By: ajkr, mrambacher
Differential Revision: D32514757
Pulled By: pdillinger
fbshipit-source-id: 507964b9311d186ae8d1131182290cbd97a99fa9
2021-11-18 20:42:12 +01:00
|
|
|
ProcessTrailerIfPresent();
|
2021-04-01 19:06:55 +02:00
|
|
|
if (io_status_.ok()) {
|
2017-12-12 00:16:37 +01:00
|
|
|
InsertCompressedBlockToPersistentCacheIfNeeded();
|
|
|
|
} else {
|
2021-04-01 19:06:55 +02:00
|
|
|
return io_status_;
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 02:00:49 +01:00
|
|
|
if (do_uncompress_ && compression_type_ != kNoCompression) {
|
2020-04-07 21:51:34 +02:00
|
|
|
PERF_TIMER_GUARD(block_decompress_time);
|
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_);
|
2021-04-01 19:06:55 +02:00
|
|
|
io_status_ = status_to_io_status(UncompressBlockContents(
|
|
|
|
info, slice_.data(), block_size_, contents_, footer_.version(),
|
|
|
|
ioptions_, memory_allocator_));
|
2020-04-25 00:30:12 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
num_heap_buf_memcpy_++;
|
|
|
|
#endif
|
2018-11-14 02:00:49 +01:00
|
|
|
compression_type_ = kNoCompression;
|
2017-12-12 00:16:37 +01:00
|
|
|
} else {
|
|
|
|
GetBlockContents();
|
|
|
|
}
|
|
|
|
|
|
|
|
InsertUncompressedBlockToPersistentCacheIfNeeded();
|
|
|
|
|
2021-04-01 19:06:55 +02:00
|
|
|
return io_status_;
|
2017-12-12 00:16:37 +01:00
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|