libsnapshot: update opcountcheck

Since variable block compresses blocks and there is no longer a 1:1
mapping between ops to blocks, we need to update this check in
EmitBlocks to the actual number of compressed blocks written.

Since single threaded + multi threaded + no compression invoke different
code paths. Ensure that that blocks written are still equivalent to
blocks.size(). Adding two test cases to cover these situations.

Test: th
Change-Id: If81eccf74333292a114268862dde0fe49681ef35
This commit is contained in:
Daniel Zheng 2024-02-08 12:35:33 -08:00
parent 18744c17c1
commit dccf1b6e39
2 changed files with 37 additions and 3 deletions

View file

@ -109,6 +109,40 @@ TEST_F(CowTestV3, MaxOp) {
ASSERT_EQ(reader.header_v3().op_count, 20);
}
TEST_F(CowTestV3, MaxOpSingleThreadCompression) {
CowOptions options;
options.op_count_max = 20;
options.num_compress_threads = 1;
options.compression_factor = 4096 * 8;
options.compression = "lz4";
auto writer = CreateCowWriter(3, options, GetCowFd());
ASSERT_TRUE(writer->AddZeroBlocks(1, 20));
std::string data = "This is some data, believe it";
data.resize(options.block_size, '\0');
ASSERT_FALSE(writer->AddRawBlocks(5, data.data(), data.size()));
ASSERT_TRUE(writer->Finalize());
}
TEST_F(CowTestV3, MaxOpMultiThreadCompression) {
CowOptions options;
options.op_count_max = 20;
options.num_compress_threads = 2;
options.compression_factor = 4096 * 8;
options.compression = "lz4";
auto writer = CreateCowWriter(3, options, GetCowFd());
ASSERT_TRUE(writer->AddZeroBlocks(1, 20));
std::string data = "This is some data, believe it";
data.resize(options.block_size, '\0');
ASSERT_FALSE(writer->AddRawBlocks(5, data.data(), data.size()));
ASSERT_TRUE(writer->Finalize());
}
TEST_F(CowTestV3, ZeroOp) {
CowOptions options;
options.op_count_max = 20;

View file

@ -356,6 +356,9 @@ bool CowWriterV3::ConstructCowOpCompressedBuffers(uint64_t new_block_start, cons
<< ", actual number of blocks received from compressor " << blocks.size();
return false;
}
if (!CheckOpCount(blocks.size())) {
return false;
}
size_t blocks_written = 0;
for (size_t blk_index = 0; blk_index < blocks.size(); blk_index++) {
CowOperation& op = cached_ops_.emplace_back();
@ -395,9 +398,6 @@ bool CowWriterV3::EmitBlocks(uint64_t new_block_start, const void* data, size_t
}
const auto bytes = reinterpret_cast<const uint8_t*>(data);
const size_t num_blocks = (size / header_.block_size);
if (!CheckOpCount(num_blocks)) {
return false;
}
for (size_t i = 0; i < num_blocks;) {
const size_t blocks_to_write =
std::min<size_t>(batch_size_ - cached_data_.size(), num_blocks - i);