rocksdb/table
Chdy 21d5a8f54f Fix a bug in sst_dump when parsing PlainTable (#12223)
Summary:
### Summary: The sst_dump tool occur IO Error when reading data in PlainTable, as shown in the follow
```bash
❯ ./sst_dump --file=/tmp/write_example  --command=scan --show_properties --verify_checksum
options.env is 0x60000282dc00
Process /tmp/write_example/001630.sst
Sst file format: plain table
/tmp/filepicker_example/001630.sst: IO error: While pread offset 0 len 758: /tmp/filepicker_example/001630.sst: Bad address
Process /tmp/filepicker_example/001624.sst
```

#### Reason
The root cause is that `fopts.use_mmap_reads` is false, `NewRandomAccessFile` will produce an `PosixRandomAccessFile` file. but `soptions_.use_mmap_reads` is true, This will result in unexpected calls in the `MmapDataIfNeeded` function.
```c++
Status SstFileDumper::GetTableReader(const std::string& file_path) {
	...

  if (s.ok()) {
    if (magic_number == kPlainTableMagicNumber ||
        magic_number == kLegacyPlainTableMagicNumber ||
			  magic_number == kCuckooTableMagicNumber) {
      soptions_.use_mmap_reads = true;
     ...

     // WARN: fopts.use_mmap_reads is false
      fs->NewRandomAccessFile(file_path, fopts, &file, nullptr);
      file_.reset(new RandomAccessFileReader(std::move(file), file_path));
    }
    ...

  }

  if (s.ok()) {
    // soptions_.use_mmap_reads is true
    s = NewTableReader(ioptions_, soptions_, internal_comparator_, file_size,
                       &table_reader_);
  }
  return s;
}
```

The following read logic was executed on a `PosixRandomAccessFile` file, Eventually, `PosixRandomAccessFile::Read` will be called with a `nullptr` `scratch`
```c++
Status PlainTableReader::MmapDataIfNeeded() {
  if (file_info_.is_mmap_mode) {
    // Get mmapped memory.
    // Executing the following logic on the PosixRandomAccessFile file is incorrect
    return file_info_.file->Read(
        IOOptions(), 0, static_cast<size_t>(file_size_), &file_info_.file_data,
        nullptr, nullptr, Env::IO_TOTAL /* rate_limiter_priority */);
  }
  return Status::OK();
}
```

#### Fix:
When parsing PlainTable, set the variable `fopts.use_mmap_reads` equal `soptions_.use_mmap_reads`,  When the `soptions_.use_mmap_reads` is true, `NewRandomAccessFile` will produce an `PosixMmapReadableFile` file. This will work correctly in the `MmapDataIfNeeded` function
```
❯ ./sst_dump --file=/tmp/write_example  --command=scan --show_properties --verify_checksum
options.env is 0x6000009323e0
Process /tmp/write_example/001630.sst
Sst file format: plain table
from [] to []
'keys496' seq:0, type:1 => values1496
'keys497' seq:0, type:1 => values1497
'keys498' seq:0, type:1 => values1498
Table Properties:
------------------------------
  # data blocks: 1
  # entries: 3
  # deletions: 0
  # merge operands: 0
  # range deletions: 0
  raw key size: 45
  raw average key size: 15.000000
  raw value size: 42
```

Pull Request resolved: https://github.com/facebook/rocksdb/pull/12223

Reviewed By: cbi42

Differential Revision: D52706238

Pulled By: ajkr

fbshipit-source-id: 2f9f518ec81d1cbde00bd65ab6bd304796836c0a
2024-01-12 14:56:10 -08:00
..
adaptive Remove RocksDB LITE (#11147) 2023-01-27 13:14:19 -08:00
block_based Refactor FilePrefetchBuffer code (#12097) 2024-01-05 09:29:01 -08:00
cuckoo Group SST write in flush, compaction and db open with new stats (#11910) 2023-12-29 15:29:23 -08:00
plain Group SST write in flush, compaction and db open with new stats (#11910) 2023-12-29 15:29:23 -08:00
block_fetcher.cc Refactor FilePrefetchBuffer code (#12097) 2024-01-05 09:29:01 -08:00
block_fetcher.h Support compressed and local flash secondary cache stacking (#11812) 2023-09-21 20:30:53 -07:00
block_fetcher_test.cc Group SST write in flush, compaction and db open with new stats (#11910) 2023-12-29 15:29:23 -08:00
cleanable_test.cc Eliminate unnecessary (slow) block cache Ref()ing in MultiGet (#9899) 2022-04-26 21:59:24 -07:00
compaction_merging_iterator.cc Fix a bug where iterator status is not checked (#11782) 2023-09-01 09:34:08 -07:00
compaction_merging_iterator.h Refactor AddRangeDels() + consider range tombstone during compaction file cutting (#11113) 2023-02-22 12:28:18 -08:00
format.cc sst_dump: display metaindex_handle and the index_handle's offset and size in footer information (#12204) 2024-01-04 14:11:15 -08:00
format.h format_version=6 and context-aware block checksums (#9058) 2023-07-30 16:40:01 -07:00
get_context.cc Eliminate some code duplication in MergeHelper (#12121) 2023-12-05 14:07:42 -08:00
get_context.h Fix rowcache get returning incorrect timestamp (#11952) 2023-11-21 20:39:33 -08:00
internal_iterator.h Add new Iterator API Refresh(const snapshot*) (#10594) 2023-09-15 10:44:43 -07:00
iter_heap.h Format files under table/ by clang-format (#10852) 2022-10-25 11:50:38 -07:00
iterator.cc Format files under table/ by clang-format (#10852) 2022-10-25 11:50:38 -07:00
iterator_wrapper.h Enforce status checking after Valid() returns false for IteratorWrapper (#11975) 2023-10-18 09:38:38 -07:00
merger_test.cc internal_repo_rocksdb (435146444452818992) (#12115) 2023-12-01 11:15:17 -08:00
merging_iterator.cc Add new Iterator API Refresh(const snapshot*) (#10594) 2023-09-15 10:44:43 -07:00
merging_iterator.h Improve documentation for MergingIterator (#11161) 2023-03-03 12:17:30 -08:00
meta_blocks.cc Ensure and clarify how RocksDB calls TablePropertiesCollector's functions (#12053) 2023-11-08 14:00:36 -08:00
meta_blocks.h Ensure and clarify how RocksDB calls TablePropertiesCollector's functions (#12053) 2023-11-08 14:00:36 -08:00
mock_table.cc Group SST write in flush, compaction and db open with new stats (#11910) 2023-12-29 15:29:23 -08:00
mock_table.h Align compaction output file boundaries to the next level ones (#10655) 2022-09-29 19:43:55 -07:00
multiget_context.h Add a new MultiGetEntity API (#11222) 2023-02-15 09:34:17 -08:00
persistent_cache_helper.cc Format files under table/ by clang-format (#10852) 2022-10-25 11:50:38 -07:00
persistent_cache_helper.h Change internal headers with duplicate names (#11408) 2023-05-17 11:27:09 -07:00
persistent_cache_options.h Change internal headers with duplicate names (#11408) 2023-05-17 11:27:09 -07:00
scoped_arena_iterator.h Format files under table/ by clang-format (#10852) 2022-10-25 11:50:38 -07:00
sst_file_dumper.cc Fix a bug in sst_dump when parsing PlainTable (#12223) 2024-01-12 14:56:10 -08:00
sst_file_dumper.h sst_dump support cuckoo table (#12098) 2023-11-30 08:06:37 -08:00
sst_file_reader.cc internal_repo_rocksdb (435146444452818992) (#12115) 2023-12-01 11:15:17 -08:00
sst_file_reader_test.cc Enforce status checking after Valid() returns false for IteratorWrapper (#11975) 2023-10-18 09:38:38 -07:00
sst_file_writer.cc Group SST write in flush, compaction and db open with new stats (#11910) 2023-12-29 15:29:23 -08:00
sst_file_writer_collectors.h Refactor to avoid confusing "raw block" (#10408) 2022-09-22 11:25:32 -07:00
table_builder.h Group SST write in flush, compaction and db open with new stats (#11910) 2023-12-29 15:29:23 -08:00
table_factory.cc Remove FactoryFunc from LoadXXXObject (#11203) 2023-02-17 12:54:07 -08:00
table_properties.cc Log user_defined_timestamps_persisted flag in event logger (#11683) 2023-08-08 12:25:21 -07:00
table_properties_internal.h Improve / clean up meta block code & integrity (#9163) 2021-11-18 11:43:44 -08:00
table_reader.h Add new Iterator API Refresh(const snapshot*) (#10594) 2023-09-15 10:44:43 -07:00
table_reader_bench.cc Group SST write in flush, compaction and db open with new stats (#11910) 2023-12-29 15:29:23 -08:00
table_test.cc Refactor FilePrefetchBuffer code (#12097) 2024-01-05 09:29:01 -08:00
two_level_iterator.cc internal_repo_rocksdb (435146444452818992) (#12115) 2023-12-01 11:15:17 -08:00
two_level_iterator.h Format files under table/ by clang-format (#10852) 2022-10-25 11:50:38 -07:00
unique_id.cc internal_repo_rocksdb (435146444452818992) (#12115) 2023-12-01 11:15:17 -08:00
unique_id_impl.h Derive cache keys from SST unique IDs (#10394) 2022-08-12 13:49:49 -07:00