diff --git a/table/sst_file_reader.cc b/table/sst_file_reader.cc index 9e3ba6eab..f7f22b061 100644 --- a/table/sst_file_reader.cc +++ b/table/sst_file_reader.cc @@ -7,6 +7,7 @@ #include "rocksdb/sst_file_reader.h" +#include "db/arena_wrapped_db_iter.h" #include "db/db_iter.h" #include "db/dbformat.h" #include "env/composite_env_wrapper.h" @@ -62,18 +63,23 @@ Status SstFileReader::Open(const std::string& file_path) { return s; } -Iterator* SstFileReader::NewIterator(const ReadOptions& options) { +Iterator* SstFileReader::NewIterator(const ReadOptions& roptions) { auto r = rep_.get(); - auto sequence = options.snapshot != nullptr - ? options.snapshot->GetSequenceNumber() + auto sequence = roptions.snapshot != nullptr + ? roptions.snapshot->GetSequenceNumber() : kMaxSequenceNumber; + ArenaWrappedDBIter* res = new ArenaWrappedDBIter(); + res->Init(r->options.env, roptions, r->ioptions, r->moptions, sequence, + r->moptions.max_sequential_skip_in_iterations, + 0 /* version_number */, nullptr /* read_callback */, + nullptr /* db_impl */, nullptr /* cfd */, false /* allow_blob */, + false /* allow_refresh */); auto internal_iter = r->table_reader->NewIterator( - options, r->moptions.prefix_extractor.get(), /*arena=*/nullptr, - /*skip_filters=*/false, TableReaderCaller::kSSTFileReader); - return NewDBIterator(r->options.env, options, r->ioptions, r->moptions, - r->ioptions.user_comparator, internal_iter, sequence, - r->moptions.max_sequential_skip_in_iterations, - nullptr /* read_callback */); + res->GetReadOptions(), r->moptions.prefix_extractor.get(), + res->GetArena(), false /* skip_filters */, + TableReaderCaller::kSSTFileReader); + res->SetIterUnderDBIter(internal_iter); + return res; } std::shared_ptr SstFileReader::GetTableProperties() diff --git a/table/sst_file_reader_test.cc b/table/sst_file_reader_test.cc index 6b1bbab84..8a63b69bd 100644 --- a/table/sst_file_reader_test.cc +++ b/table/sst_file_reader_test.cc @@ -128,6 +128,31 @@ TEST_F(SstFileReaderTest, Uint64Comparator) { CreateFileAndCheck(keys); } +TEST_F(SstFileReaderTest, ReadOptionsOutOfScope) { + // Repro a bug where the SstFileReader depended on its configured ReadOptions + // outliving it. + options_.comparator = test::Uint64Comparator(); + std::vector keys; + for (uint64_t i = 0; i < kNumKeys; i++) { + keys.emplace_back(EncodeAsUint64(i)); + } + CreateFile(sst_name_, keys); + + SstFileReader reader(options_); + ASSERT_OK(reader.Open(sst_name_)); + std::unique_ptr iter; + { + // Make sure ReadOptions go out of scope ASAP so we know the iterator + // operations do not depend on it. + ReadOptions ropts; + iter.reset(reader.NewIterator(ropts)); + } + iter->SeekToFirst(); + while (iter->Valid()) { + iter->Next(); + } +} + TEST_F(SstFileReaderTest, ReadFileWithGlobalSeqno) { std::vector keys; for (uint64_t i = 0; i < kNumKeys; i++) {