forked from FoundationDB/fdb-record-layer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndexingByIndex.java
253 lines (218 loc) · 12.5 KB
/
IndexingByIndex.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
/*
* IndexingByIndex.java
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2015-2020 Apple Inc. and the FoundationDB project 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.
*/
package com.apple.foundationdb.record.provider.foundationdb;
import com.apple.foundationdb.Range;
import com.apple.foundationdb.annotation.API;
import com.apple.foundationdb.async.AsyncIterator;
import com.apple.foundationdb.async.AsyncUtil;
import com.apple.foundationdb.async.RangeSet;
import com.apple.foundationdb.record.ExecuteProperties;
import com.apple.foundationdb.record.IndexBuildProto;
import com.apple.foundationdb.record.IndexScanType;
import com.apple.foundationdb.record.IsolationLevel;
import com.apple.foundationdb.record.RecordCursor;
import com.apple.foundationdb.record.RecordCursorResult;
import com.apple.foundationdb.record.RecordMetaData;
import com.apple.foundationdb.record.ScanProperties;
import com.apple.foundationdb.record.TupleRange;
import com.apple.foundationdb.record.logging.LogMessageKeys;
import com.apple.foundationdb.record.metadata.Index;
import com.apple.foundationdb.record.metadata.IndexTypes;
import com.apple.foundationdb.record.metadata.RecordType;
import com.apple.foundationdb.subspace.Subspace;
import com.apple.foundationdb.tuple.Tuple;
import com.google.protobuf.ByteString;
import com.google.protobuf.Message;
import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
/**
* This indexer scans records by a source index.
*/
@API(API.Status.INTERNAL)
public class IndexingByIndex extends IndexingBase {
// LOGGER here?
private IndexBuildProto.IndexBuildIndexingStamp myIndexingTypeStamp = null;
IndexingByIndex(@Nonnull IndexingCommon common,
@Nonnull OnlineIndexer.IndexingPolicy policy) {
super(common, policy);
}
@Override
@Nonnull
IndexBuildProto.IndexBuildIndexingStamp getIndexingTypeStamp(FDBRecordStore store) {
if ( myIndexingTypeStamp == null) {
Index srcIndex = getSourceIndex(store.getRecordMetaData());
myIndexingTypeStamp = compileIndexingTypeStamp(srcIndex);
}
return myIndexingTypeStamp;
}
@Nonnull
private static IndexBuildProto.IndexBuildIndexingStamp compileIndexingTypeStamp(Index srcIndex) {
return IndexBuildProto.IndexBuildIndexingStamp.newBuilder()
.setMethod(IndexBuildProto.IndexBuildIndexingStamp.Method.BY_INDEX)
.setSourceIndexSubspaceKey(ByteString.copyFrom(Tuple.from(srcIndex.getSubspaceKey()).pack()))
.setSourceIndexLastModifiedVersion(srcIndex.getLastModifiedVersion())
.build();
}
@Override
List<Object> indexingLogMessageKeyValues() {
return Arrays.asList(
LogMessageKeys.INDEXING_METHOD, "by index",
LogMessageKeys.SOURCE_INDEX, policy.getSourceIndex(),
LogMessageKeys.SUBSPACE_KEY, policy.getSourceIndexSubspaceKey()
);
}
@Nonnull
private Index getSourceIndex(RecordMetaData metaData) {
if (policy.getSourceIndexSubspaceKey() != null) {
return metaData.getIndexFromSubspaceKey(policy.getSourceIndexSubspaceKey());
}
if (policy.getSourceIndex() != null) {
return metaData.getIndex(policy.getSourceIndex());
}
throw new ValidationException("no source index",
LogMessageKeys.INDEX_NAME, common.getIndex().getName(),
LogMessageKeys.SOURCE_INDEX, policy.getSourceIndex(),
LogMessageKeys.INDEXER_ID, common.getUuid());
}
@Nonnull
@Override
CompletableFuture<Void> buildIndexInternalAsync() {
return getRunner().runAsync(context -> openRecordStore(context)
.thenCompose( store -> {
// first validate that both src and tgt are of a single, similar, type
final RecordMetaData metaData = store.getRecordMetaData();
final Index srcIndex = getSourceIndex(metaData);
final Collection<RecordType> srcRecordTypes = metaData.recordTypesForIndex(srcIndex);
validateOrThrowEx(common.getAllRecordTypes().size() == 1, "target index has multiple types");
validateOrThrowEx(srcRecordTypes.size() == 1, "source index has multiple types");
validateOrThrowEx(!srcIndex.getRootExpression().createsDuplicates(), "source index creates duplicates");
validateOrThrowEx(IndexTypes.VALUE.equals(srcIndex.getType()), "source index is not a VALUE index");
validateOrThrowEx(common.getAllRecordTypes().containsAll(srcRecordTypes), "source index's type is not equal to target index's");
// all valid; back to the future. Note that for practical reasons, readability and idempotency will be validated later
return context.getReadVersionAsync()
.thenCompose(vignore -> {
SubspaceProvider subspaceProvider = common.getRecordStoreBuilder().getSubspaceProvider();
return subspaceProvider.getSubspaceAsync(context)
.thenCompose(subspace -> buildIndexFromIndex(subspaceProvider, subspace));
});
}), common.indexLogMessageKeyValues("IndexingByIndex::buildIndexInternalAsync"));
}
@Nonnull
private CompletableFuture<Void> buildIndexFromIndex(@Nonnull SubspaceProvider subspaceProvider, @Nonnull Subspace subspace) {
final List<Object> additionalLogMessageKeyValues = Arrays.asList(LogMessageKeys.CALLING_METHOD, "buildIndexFromIndex");
return iterateAllRanges(additionalLogMessageKeyValues,
(store, recordsScanned) -> buildRangeOnly(store, recordsScanned),
subspaceProvider, subspace);
}
@Nonnull
private CompletableFuture<Boolean> buildRangeOnly(@Nonnull FDBRecordStore store, @Nonnull AtomicLong recordsScanned) {
// return false when done
validateSameMetadataOrThrow(store);
final Index index = common.getIndex();
final IndexMaintainer maintainer = store.getIndexMaintainer(index);
// idempotence - We could have verified it at the first iteration only, but the repeating checks seem harmless
// validateOrThrowEx(maintainer.isIdempotent(), "target index is not idempotent");
// readability - This method shouldn't block if one has already opened the record store (as we did)
Index srcIndex = getSourceIndex(store.getRecordMetaData());
validateOrThrowEx(store.isIndexScannable(srcIndex), "source index is not scannable");
RangeSet rangeSet = new RangeSet(store.indexRangeSubspace(index));
AsyncIterator<Range> ranges = rangeSet.missingRanges(store.ensureContextActive()).iterator();
final ExecuteProperties.Builder executeProperties = ExecuteProperties.newBuilder()
.setIsolationLevel(maintainer.isIdempotent() ? IsolationLevel.SNAPSHOT : IsolationLevel.SERIALIZABLE)
.setReturnedRowLimit(getLimit() + 1); // respect limit in this path; +1 allows a continuation item
final ScanProperties scanProperties = new ScanProperties(executeProperties.build());
return ranges.onHasNext().thenCompose(hasNext -> {
if (Boolean.FALSE.equals(hasNext)) {
return AsyncUtil.READY_FALSE; // no more missing ranges - all done
}
final Range range = ranges.next();
final Tuple rangeStart = RangeSet.isFirstKey(range.begin) ? null : Tuple.fromBytes(range.begin);
final Tuple rangeEnd = RangeSet.isFinalKey(range.end) ? null : Tuple.fromBytes(range.end);
final TupleRange tupleRange = TupleRange.between(rangeStart, rangeEnd);
RecordCursor<FDBIndexedRecord<Message>> cursor =
store.scanIndexRecords(srcIndex.getName(), IndexScanType.BY_VALUE, tupleRange, null, scanProperties);
final AtomicReference<RecordCursorResult<FDBIndexedRecord<Message>>> lastResult = new AtomicReference<>(RecordCursorResult.exhausted());
final AtomicBoolean hasMore = new AtomicBoolean(true);
return iterateRangeOnly(store, cursor,
this::getRecordIfTypeMatch,
lastResult, hasMore, recordsScanned, maintainer.isIdempotent())
.thenApply(vignore -> hasMore.get() ?
lastResult.get().get().getIndexEntry().getKey() :
rangeEnd)
.thenCompose(cont -> rangeSet.insertRange(store.ensureContextActive(), packOrNull(rangeStart), packOrNull(cont), true)
.thenApply(ignore -> !allRangesExhausted(cont, rangeEnd)));
});
}
@Nonnull
@SuppressWarnings("unused")
private CompletableFuture<FDBStoredRecord<Message>> getRecordIfTypeMatch(FDBRecordStore store, @Nonnull RecordCursorResult<FDBIndexedRecord<Message>> cursorResult) {
FDBIndexedRecord<Message> indexResult = cursorResult.get();
FDBStoredRecord<Message> rec = indexResult == null ? null : indexResult.getStoredRecord();
return recordIfInIndexedTypes(rec);
}
// support rebuildIndexAsync
@Nonnull
@Override
CompletableFuture<Void> rebuildIndexInternalAsync(FDBRecordStore store) {
AtomicReference<Tuple> nextResultCont = new AtomicReference<>();
AtomicLong recordScanned = new AtomicLong();
return AsyncUtil.whileTrue(() ->
rebuildRangeOnly(store, nextResultCont.get(), recordScanned).thenApply(cont -> {
if (cont == null) {
return false;
}
nextResultCont.set(cont);
return true;
}), store.getExecutor());
}
@Nonnull
@SuppressWarnings("PMD.CloseResource")
private CompletableFuture<Tuple> rebuildRangeOnly(@Nonnull FDBRecordStore store, Tuple cont, @Nonnull AtomicLong recordsScanned) {
validateSameMetadataOrThrow(store);
final Index index = common.getIndex();
final IndexMaintainer maintainer = store.getIndexMaintainer(index);
// idempotence - We could have verified it at the first iteration only, but the repeating checks seem harmless
validateOrThrowEx(maintainer.isIdempotent(), "target index is not idempotent");
// readability - This method shouldn't block if one has already opened the record store (as we did)
final Index srcIndex = getSourceIndex(store.getRecordMetaData());
validateOrThrowEx(store.isIndexScannable(srcIndex), "source index is not scannable");
final ExecuteProperties.Builder executeProperties = ExecuteProperties.newBuilder()
.setIsolationLevel(IsolationLevel.SNAPSHOT);
final ScanProperties scanProperties = new ScanProperties(executeProperties.build());
final TupleRange tupleRange = TupleRange.between(cont, null);
RecordCursor<FDBIndexedRecord<Message>> cursor =
store.scanIndexRecords(srcIndex.getName(), IndexScanType.BY_VALUE, tupleRange, null, scanProperties);
final AtomicReference<RecordCursorResult<FDBIndexedRecord<Message>>> lastResult = new AtomicReference<>(RecordCursorResult.exhausted());
final AtomicBoolean hasMore = new AtomicBoolean(true);
final boolean isIdempotent = true ; // Note that currently indexing by index is online implemented for idempotent indexes
return iterateRangeOnly(store, cursor,
this::getRecordIfTypeMatch,
lastResult, hasMore, recordsScanned, isIdempotent
).thenApply(vignore -> hasMore.get() ?
lastResult.get().get().getIndexEntry().getKey() :
null );
}
}