-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathincremental_iterator.cc
223 lines (193 loc) · 7.63 KB
/
incremental_iterator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
#include "incremental_iterator.h"
#include "comparator.h"
#include "encoding.h"
#include "protos/roachpb/errors.pb.h"
using namespace cockroach;
DBIncrementalIterator::DBIncrementalIterator(DBEngine* engine, DBIterOptions opts, DBKey start,
DBKey end, DBString* write_intent)
: engine(engine),
opts(opts),
valid(true),
status(kSuccess),
start(start),
end(end),
write_intent(write_intent) {
sanity_iter.reset(NULL);
start_time.set_wall_time(start.wall_time);
start_time.set_logical(start.logical);
end_time.set_wall_time(end.wall_time);
end_time.set_logical(end.logical);
// sanity_iter is only relevant if a time-bound iterator is required.
//
// It is necessary for correctness that sanity_iter be created before
// iter. This is because the provided Reader may not be a consistent
// snapshot, so the two could end up observing different information. The hack
// around sanityCheckMetadataKey only works properly if all possible
// discrepancies between the two iterators lead to intents and values falling
// outside of the timestamp range **from iter's perspective**. This allows us
// to simply ignore discrepancies that we notice in advance(). See #34819.
if (!EmptyTimestamp(opts.min_timestamp_hint) || !EmptyTimestamp(opts.max_timestamp_hint)) {
assert(!EmptyTimestamp(opts.min_timestamp_hint));
assert(!EmptyTimestamp(opts.max_timestamp_hint));
DBIterOptions nontimebound_opts;
nontimebound_opts.upper_bound = opts.upper_bound;
sanity_iter.reset(DBNewIter(engine, nontimebound_opts));
}
iter.reset(DBNewIter(engine, opts));
}
DBIncrementalIterator::~DBIncrementalIterator() {}
// sanityCheckMetadataKey looks up the current `iter` key using a normal,
// non-time-bound iterator and returns the value if the normal iterator also
// sees that exact key. Otherwise, it returns false. It's used in the workaround
// in `advanceKey` for a time-bound iterator bug.
rocksdb::Slice DBIncrementalIterator::sanityCheckMetadataKey() {
// If sanityIter is not set, it's because we're not using time-bound
// iterator and we don't need the sanity check.
if (sanity_iter == NULL) {
valid = true;
status = ToDBStatus(rocksdb::Status::OK());
return iter.get()->rep->value();
}
auto sanity_iter_rep = sanity_iter->rep.get();
sanity_iter_rep->Seek(iter->rep->key());
if (!sanity_iter_rep->status().ok()) {
valid = false;
status = ToDBStatus(sanity_iter_rep->status());
return NULL;
} else if (!sanity_iter_rep->Valid()) {
valid = false;
status = ToDBStatus(rocksdb::Status::OK());
return NULL;
} else if (kComparator.Compare(sanity_iter_rep->key(), iter->rep->key()) != 0) {
valid = false;
status = ToDBStatus(rocksdb::Status::OK());
return NULL;
}
valid = true;
status = ToDBStatus(rocksdb::Status::OK());
return sanity_iter.get()->rep->value();
}
// legacyTimestampIsLess compares the timestamps t1 and t2, and returns a
// boolean indicating whether t1 is less than t2.
bool DBIncrementalIterator::legacyTimestampIsLess(const cockroach::util::hlc::LegacyTimestamp& t1,
const cockroach::util::hlc::LegacyTimestamp& t2) {
return t1.wall_time() < t2.wall_time() ||
(t1.wall_time() == t2.wall_time() && t1.logical() < t2.logical());
}
// advanceKey finds the key and its appropriate version which lies in
// (start_time, end_time].
void DBIncrementalIterator::advanceKey() {
for (;;) {
if (!valid) {
return;
}
if (!iter.get()->rep->Valid()) {
status = ToDBStatus(iter.get()->rep->status());
valid = false;
return;
}
rocksdb::Slice key;
int64_t wall_time = 0;
int32_t logical = 0;
if (!DecodeKey(iter.get()->rep->key(), &key, &wall_time, &logical)) {
status = ToDBString("unable to decode key");
valid = false;
return;
}
cockroach::storage::engine::enginepb::MVCCMetadata meta;
if (wall_time != 0 || logical != 0) {
meta.mutable_timestamp()->set_wall_time(wall_time);
meta.mutable_timestamp()->set_logical(logical);
} else {
// HACK(dan): Work around a known bug in the time-bound iterator.
// For reasons described in #28358, a time-bound iterator will
// sometimes see an unresolved intent where there is none. A normal
// iterator doesn't have this problem, so we work around it by
// double checking all non-value keys. If sanityCheckMetadataKey
// returns false, then the intent was a phantom and we ignore it.
// NB: this could return a older/newer intent than the one the time-bound
// iterator saw; this is handled.
auto value = sanityCheckMetadataKey();
if (status.data != NULL) {
return;
} else if (!valid) {
valid = true;
DBIterNext(iter.get(), false);
continue;
}
if (!meta.ParseFromArray(value.data(), value.size())) {
status = ToDBString("failed to parse meta");
valid = false;
return;
}
}
// Check for an inline value, as these are only used in non-user data.
// They're not needed for backup, so they're not handled by this method.
// If one shows up, throw an error so it's obvious something is wrong.
if (meta.has_raw_bytes()) {
valid = false;
status = ToDBString("Inline values are unsupported by the IncrementalIterator");
return;
}
if (meta.has_txn()) {
if (legacyTimestampIsLess(start_time, meta.timestamp()) &&
!legacyTimestampIsLess(end_time, meta.timestamp())) {
cockroach::roachpb::WriteIntentError err;
cockroach::roachpb::Intent* intent = err.add_intents();
intent->mutable_span()->set_key(key.data(), key.size());
intent->mutable_txn()->CopyFrom(meta.txn());
status = ToDBString("WriteIntentError");
valid = false;
*write_intent = ToDBString(err.SerializeAsString());
return;
}
}
if (legacyTimestampIsLess(end_time, meta.timestamp())) {
DBIterNext(iter.get(), false);
continue;
} else if (!legacyTimestampIsLess(start_time, meta.timestamp())) {
DBIterNext(iter.get(), true);
continue;
}
break;
}
}
DBIterState DBIncrementalIterator::getState() {
DBIterState state = {};
state.valid = valid;
state.status = status;
if (state.valid) {
rocksdb::Slice key;
state.valid = DecodeKey(iter.get()->rep->key(), &key, &state.key.wall_time, &state.key.logical);
if (state.valid) {
state.key.key = ToDBSlice(key);
state.value = ToDBSlice(iter.get()->rep->value());
}
}
return state;
}
DBIterState DBIncrementalIterator::seek(DBKey key) {
DBIterSeek(iter.get(), key);
advanceKey();
return getState();
}
DBIterState DBIncrementalIterator::next(bool skip_current_key_versions) {
DBIterNext(iter.get(), skip_current_key_versions);
advanceKey();
return getState();
}
const rocksdb::Slice DBIncrementalIterator::key() { return iter.get()->rep->key(); }
const rocksdb::Slice DBIncrementalIterator::value() { return iter.get()->rep->value(); }