From b87c355772c0e1a643cbbd9799809d376073a6fe Mon Sep 17 00:00:00 2001
From: Yanqin Jin <yanqin@fb.com>
Date: Thu, 30 Jun 2022 10:16:03 -0700
Subject: [PATCH] Fix assertion error with read_opts.iter_start_ts (#10279)

Summary:
If the internal iterator is not valid, `SeekToLast` with iter_start_ts should have `valid_` is false without assertion failure.
Test plan
make check

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

Reviewed By: ltamasi

Differential Revision: D37539393

Pulled By: riversand963

fbshipit-source-id: 8e94057838f8a05144fad5768f4d62f1893ec315
---
 db/db_iter.cc                      | 2 +-
 db/db_with_timestamp_basic_test.cc | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/db/db_iter.cc b/db/db_iter.cc
index 1ffc823190..7f2cb8f490 100644
--- a/db/db_iter.cc
+++ b/db/db_iter.cc
@@ -1612,7 +1612,7 @@ void DBIter::SeekToLast() {
     SeekForPrev(*iterate_upper_bound_);
     const bool is_ikey = (timestamp_size_ > 0 && timestamp_lb_ != nullptr);
     Slice k = Valid() ? key() : Slice();
-    if (is_ikey) {
+    if (is_ikey && Valid()) {
       k.remove_suffix(kNumInternalBytes + timestamp_size_);
     }
     while (Valid() && 0 == user_comparator_.CompareWithoutTimestamp(
diff --git a/db/db_with_timestamp_basic_test.cc b/db/db_with_timestamp_basic_test.cc
index e08133cb71..593dda9039 100644
--- a/db/db_with_timestamp_basic_test.cc
+++ b/db/db_with_timestamp_basic_test.cc
@@ -1193,6 +1193,14 @@ TEST_F(DBBasicTestWithTimestamp, SimpleBackwardIterateLowerTsBound) {
     it->SeekToLast();
     CheckIterEntry(it.get(), "a", kTypeSingleDeletion, Slice(),
                    Timestamp(1, 0));
+
+    key_ub_str = "a";  // exclusive
+    key_ub = key_ub_str;
+    read_opts.iterate_upper_bound = &key_ub;
+    it.reset(db_->NewIterator(read_opts));
+    it->SeekToLast();
+    ASSERT_FALSE(it->Valid());
+    ASSERT_OK(it->status());
   }
 
   Close();