/
SnapshotIterator.java
296 lines (254 loc) · 9.09 KB
/
SnapshotIterator.java
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to you 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.
*/
package org.apache.fluo.accumulo.iterators;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import org.apache.accumulo.core.client.IteratorSetting;
import org.apache.accumulo.core.data.ArrayByteSequence;
import org.apache.accumulo.core.data.ByteSequence;
import org.apache.accumulo.core.data.Key;
import org.apache.accumulo.core.data.PartialKey;
import org.apache.accumulo.core.data.Range;
import org.apache.accumulo.core.data.Value;
import org.apache.accumulo.core.iterators.IteratorEnvironment;
import org.apache.accumulo.core.iterators.SortedKeyValueIterator;
import org.apache.fluo.accumulo.util.ColumnConstants;
import org.apache.fluo.accumulo.util.ColumnType;
import org.apache.fluo.accumulo.values.WriteValue;
public class SnapshotIterator implements SortedKeyValueIterator<Key, Value> {
@VisibleForTesting
static final String TIMESTAMP_OPT = "timestampOpt";
static final String RETURN_READLOCK_PRESENT_OPT = "rrlpOpt";
private static final ByteSequence NOTIFY_CF_BS =
new ArrayByteSequence(ColumnConstants.NOTIFY_CF.toArray());
static final Set<ByteSequence> NOTIFY_CF_SET = Collections.singleton(NOTIFY_CF_BS);
private TimestampSkippingIterator source;
private long snaptime;
private boolean hasTop = false;
private boolean returnReadLockPresent = false;
private final Key curCol = new Key();
private Key readLockIgnore;
private Key readLockKey;
private Value readLockValue;
private void rememberReadLock(Key key, Value val) {
Preconditions.checkState(readLockKey == null && readLockValue == null);
if (readLockIgnore == null
|| !key.equals(readLockIgnore, PartialKey.ROW_COLFAM_COLQUAL_COLVIS)) {
readLockKey = new Key(key);
readLockValue = new Value(val);
}
}
private void ignoreReadLock(Key key) {
readLockIgnore = key;
}
private void clearReadLock() {
readLockKey = null;
readLockValue = null;
readLockIgnore = null;
}
private void findTop() throws IOException {
outer: while (source.hasTop() && readLockKey == null) {
long invalidationTime = -1;
long dataPointer = -1;
if (source.getTopKey().getColumnFamilyData().equals(NOTIFY_CF_BS)) {
throw new IllegalStateException("seeing notifications during snapshot iteration");
}
curCol.set(source.getTopKey());
while (source.hasTop()
&& curCol.equals(source.getTopKey(), PartialKey.ROW_COLFAM_COLQUAL_COLVIS)) {
ColumnType colType = ColumnType.from(source.getTopKey());
long ts = source.getTopKey().getTimestamp() & ColumnConstants.TIMESTAMP_MASK;
switch (colType) {
case TX_DONE: {
source.skipToPrefix(curCol, ColumnType.WRITE);
continue;
}
case WRITE: {
long timePtr = WriteValue.getTimestamp(source.getTopValue().get());
if (timePtr > invalidationTime) {
invalidationTime = timePtr;
}
if (dataPointer == -1) {
if (ts <= snaptime) {
dataPointer = timePtr;
source.skipToPrefix(curCol, ColumnType.DEL_LOCK);
continue;
} else {
source.skipToTimestamp(curCol, ColumnType.WRITE.encode(snaptime));
continue;
}
}
break;
}
case DEL_LOCK: {
if (ts > invalidationTime) {
invalidationTime = ts;
}
if (returnReadLockPresent) {
source.skipToPrefix(curCol, ColumnType.RLOCK);
} else {
source.skipToPrefix(curCol, ColumnType.LOCK);
}
continue;
}
case RLOCK: {
if (returnReadLockPresent) {
rememberReadLock(source.getTopKey(), source.getTopValue());
}
source.skipToPrefix(curCol, ColumnType.LOCK);
continue;
}
case LOCK: {
if (ts > invalidationTime && ts <= snaptime) {
// nothing supersedes this lock, therefore the column is locked
return;
} else {
if (dataPointer == -1) {
source.skipColumn(curCol);
continue outer;
} else {
source.skipToTimestamp(curCol, ColumnType.DATA.encode(dataPointer));
continue;
}
}
}
case DATA: {
if (dataPointer == ts) {
// found data for this column
return;
}
if (ts < dataPointer || dataPointer == -1) {
source.skipColumn(curCol);
continue outer;
}
if (ts > dataPointer) {
source.skipToTimestamp(curCol, ColumnType.DATA.encode(dataPointer));
continue;
}
break;
}
case ACK: {
if (dataPointer == -1) {
source.skipColumn(curCol);
continue outer;
} else {
source.skipToTimestamp(curCol, ColumnType.DATA.encode(dataPointer));
continue;
}
}
default:
throw new IllegalArgumentException();
}
// TODO handle case where dataPointer >=0, but no data was found
source.next();
}
}
}
@Override
public void init(SortedKeyValueIterator<Key, Value> source, Map<String, String> options,
IteratorEnvironment env) throws IOException {
this.source = new TimestampSkippingIterator(source);
this.snaptime = Long.parseLong(options.get(TIMESTAMP_OPT));
this.returnReadLockPresent =
Boolean.parseBoolean(options.getOrDefault(RETURN_READLOCK_PRESENT_OPT, "false"));
// TODO could require client to send version as a sanity check
}
@Override
public boolean hasTop() {
return hasTop && (readLockKey != null || source.hasTop());
}
@Override
public void next() throws IOException {
if (readLockKey != null) {
clearReadLock();
} else {
curCol.set(source.getTopKey());
source.skipColumn(curCol);
findTop();
}
}
@Override
public void seek(Range range, Collection<ByteSequence> columnFamilies, boolean inclusive)
throws IOException {
Range newRange = range;
Collection<ByteSequence> cols;
boolean inc;
clearReadLock();
// handle continue case
hasTop = true;
if (range.getStartKey() != null && range.getStartKey().getTimestamp() != Long.MAX_VALUE
&& !range.isStartKeyInclusive()) {
if (ColumnType.from(range.getStartKey()) == ColumnType.RLOCK) {
Key currCol = new Key(range.getStartKey());
currCol.setTimestamp(Long.MAX_VALUE);
newRange = new Range(currCol, true, range.getEndKey(), range.isEndKeyInclusive());
ignoreReadLock(currCol);
} else {
Key nextCol = range.getStartKey().followingKey(PartialKey.ROW_COLFAM_COLQUAL_COLVIS);
if (range.afterEndKey(nextCol)) {
hasTop = false;
return;
} else {
newRange = new Range(nextCol, true, range.getEndKey(), range.isEndKeyInclusive());
}
}
} else {
newRange = range;
}
if (columnFamilies.size() == 0 && inclusive == false) {
cols = NOTIFY_CF_SET;
inc = false;
} else {
cols = columnFamilies;
inc = inclusive;
}
source.seek(newRange, cols, inc);
findTop();
}
@Override
public Key getTopKey() {
if (readLockKey != null) {
return readLockKey;
} else {
return source.getTopKey();
}
}
@Override
public Value getTopValue() {
if (readLockValue != null) {
return readLockValue;
} else {
return source.getTopValue();
}
}
@Override
public SortedKeyValueIterator<Key, Value> deepCopy(IteratorEnvironment env) {
throw new UnsupportedOperationException();
}
public static void setSnaptime(IteratorSetting cfg, long time) {
if (time < 0 || (ColumnConstants.PREFIX_MASK & time) != 0) {
throw new IllegalArgumentException();
}
cfg.addOption(TIMESTAMP_OPT, time + "");
}
public static void setReturnReadLockPresent(IteratorSetting cfg, boolean rrlp) {
cfg.addOption(RETURN_READLOCK_PRESENT_OPT, rrlp + "");
}
}