-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow _update and upsert to read from the transaction log #29264
Changes from 1 commit
a8d3b73
3fa0fc0
41a3c32
3b84354
5be76c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,7 @@ | |
import org.elasticsearch.threadpool.ThreadPool; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
@@ -145,6 +146,7 @@ public class InternalEngine extends Engine { | |
* being indexed/deleted. | ||
*/ | ||
private final AtomicLong writingBytes = new AtomicLong(); | ||
private final AtomicBoolean trackTranslogLocation = new AtomicBoolean(false); | ||
|
||
@Nullable | ||
private final String historyUUID; | ||
|
@@ -558,6 +560,24 @@ public GetResult get(Get get, BiFunction<String, SearcherScope, Searcher> search | |
throw new VersionConflictEngineException(shardId, get.type(), get.id(), | ||
get.versionType().explainConflictForReads(versionValue.version, get.version())); | ||
} | ||
if (get.isReadFromTranslog()) { | ||
// this is only used for updates - API _GET calls will always read form a reader for consistency | ||
// the update call doesn't need the consistency since it's source only + _parent but parent can go away in 7.0 | ||
if (versionValue.getLocation() != null) { | ||
try { | ||
Translog.Operation operation = translog.readOperation(versionValue.getLocation()); | ||
if (operation != null) { | ||
TranslogLeafReader reader = new TranslogLeafReader((Translog.Index) operation); | ||
return new GetResult(new Searcher("realtime_get", new IndexSearcher(reader)), | ||
new VersionsAndSeqNoResolver.DocIdAndVersion(0, ((Translog.Index) operation).version(), reader, 0)); | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we seem to be using EngineException as a wrapper in other places? |
||
} | ||
} else { | ||
trackTranslogLocation.set(true); | ||
} | ||
} | ||
refresh("realtime_get", SearcherScope.INTERNAL); | ||
} | ||
scope = SearcherScope.INTERNAL; | ||
|
@@ -790,6 +810,10 @@ public IndexResult index(Index index) throws IOException { | |
} | ||
indexResult.setTranslogLocation(location); | ||
} | ||
if (plan.indexIntoLucene && indexResult.hasFailure() == false) { | ||
versionMap.maybePutUnderLock(index.uid().bytes(), | ||
getVersionValue(plan.versionForIndexing, plan.seqNoForIndexing, index.primaryTerm(), indexResult.getTranslogLocation())); | ||
} | ||
if (indexResult.getSeqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO) { | ||
localCheckpointTracker.markSeqNoAsCompleted(indexResult.getSeqNo()); | ||
} | ||
|
@@ -916,8 +940,6 @@ private IndexResult indexIntoLucene(Index index, IndexingStrategy plan) | |
assert assertDocDoesNotExist(index, canOptimizeAddDocument(index) == false); | ||
index(index.docs(), indexWriter); | ||
} | ||
versionMap.maybePutUnderLock(index.uid().bytes(), | ||
new VersionValue(plan.versionForIndexing, plan.seqNoForIndexing, index.primaryTerm())); | ||
return new IndexResult(plan.versionForIndexing, plan.seqNoForIndexing, plan.currentNotFoundOrDeleted); | ||
} catch (Exception ex) { | ||
if (indexWriter.getTragicException() == null) { | ||
|
@@ -941,6 +963,13 @@ private IndexResult indexIntoLucene(Index index, IndexingStrategy plan) | |
} | ||
} | ||
|
||
private VersionValue getVersionValue(long version, long seqNo, long term, Translog.Location location) { | ||
if (location != null && trackTranslogLocation.get()) { | ||
return new TranslogVersionValue(location, version, seqNo, term); | ||
} | ||
return new VersionValue(version, seqNo, term); | ||
} | ||
|
||
/** | ||
* returns true if the indexing operation may have already be processed by this engine. | ||
* Note that it is OK to rarely return true even if this is not the case. However a `false` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.index.engine; | ||
|
||
import org.apache.lucene.index.BinaryDocValues; | ||
import org.apache.lucene.index.DocValuesType; | ||
import org.apache.lucene.index.FieldInfo; | ||
import org.apache.lucene.index.FieldInfos; | ||
import org.apache.lucene.index.Fields; | ||
import org.apache.lucene.index.IndexOptions; | ||
import org.apache.lucene.index.LeafMetaData; | ||
import org.apache.lucene.index.LeafReader; | ||
import org.apache.lucene.index.NumericDocValues; | ||
import org.apache.lucene.index.PointValues; | ||
import org.apache.lucene.index.SortedDocValues; | ||
import org.apache.lucene.index.SortedNumericDocValues; | ||
import org.apache.lucene.index.SortedSetDocValues; | ||
import org.apache.lucene.index.StoredFieldVisitor; | ||
import org.apache.lucene.index.Terms; | ||
import org.apache.lucene.util.Bits; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.index.fielddata.AbstractSortedDocValues; | ||
import org.elasticsearch.index.fielddata.AbstractSortedSetDocValues; | ||
import org.elasticsearch.index.mapper.ParentFieldMapper; | ||
import org.elasticsearch.index.mapper.RoutingFieldMapper; | ||
import org.elasticsearch.index.mapper.SourceFieldMapper; | ||
import org.elasticsearch.index.translog.Translog; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Collections; | ||
|
||
/** | ||
* Internal class that mocks a single doc read from the transaction log as a leaf reader. | ||
*/ | ||
final class TranslogLeafReader extends LeafReader { | ||
|
||
private final Translog.Index operation; | ||
private static final FieldInfo FAKE_SOURCE_FIELD | ||
= new FieldInfo(SourceFieldMapper.NAME, 1, false, false, false, IndexOptions.NONE, DocValuesType.NONE, -1, Collections.emptyMap(), | ||
0,0); | ||
private static final FieldInfo FAKE_ROUTING_FIELD | ||
= new FieldInfo(RoutingFieldMapper.NAME, 2, false, false, false, IndexOptions.NONE, DocValuesType.NONE, -1, Collections.emptyMap(), | ||
0,0); | ||
|
||
TranslogLeafReader(Translog.Index operation) { | ||
this.operation = operation; | ||
} | ||
@Override | ||
public CacheHelper getCoreCacheHelper() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Terms terms(String field) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public NumericDocValues getNumericDocValues(String field) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public BinaryDocValues getBinaryDocValues(String field) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public SortedDocValues getSortedDocValues(String field) { | ||
// TODO this can be removed in 7.0 and upwards we don't support the parent field anymore | ||
if (field.startsWith(ParentFieldMapper.NAME + "#") && operation.parent() != null) { | ||
return new AbstractSortedDocValues() { | ||
@Override | ||
public int docID() { | ||
return 0; | ||
} | ||
|
||
private final BytesRef term = new BytesRef(operation.parent()); | ||
private int ord; | ||
@Override | ||
public boolean advanceExact(int docID) { | ||
if (docID != 0) { | ||
throw new IndexOutOfBoundsException("do such doc ID: " + docID); | ||
} | ||
ord = 0; | ||
return true; | ||
} | ||
|
||
@Override | ||
public int ordValue() { | ||
return ord; | ||
} | ||
|
||
@Override | ||
public BytesRef lookupOrd(int ord) { | ||
if (ord == 0) { | ||
return term; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public int getValueCount() { | ||
return 1; | ||
} | ||
}; | ||
} | ||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we throw an exception? |
||
} | ||
|
||
@Override | ||
public SortedNumericDocValues getSortedNumericDocValues(String field) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public SortedSetDocValues getSortedSetDocValues(String field) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public NumericDocValues getNormValues(String field) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public FieldInfos getFieldInfos() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Bits getLiveDocs() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public PointValues getPointValues(String field) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void checkIntegrity() { | ||
|
||
} | ||
|
||
@Override | ||
public LeafMetaData getMetaData() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Fields getTermVectors(int docID) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public int numDocs() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public int maxDoc() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public void document(int docID, StoredFieldVisitor visitor) throws IOException { | ||
if (docID != 0) { | ||
throw new IllegalArgumentException("no such doc ID " + docID); | ||
} | ||
if (visitor.needsField(FAKE_SOURCE_FIELD) == StoredFieldVisitor.Status.YES) { | ||
assert operation.source().toBytesRef().offset == 0; | ||
assert operation.source().toBytesRef().length == operation.source().toBytesRef().bytes.length; | ||
visitor.binaryField(FAKE_SOURCE_FIELD, operation.source().toBytesRef().bytes); | ||
} | ||
if (operation.routing() != null && visitor.needsField(FAKE_ROUTING_FIELD) == StoredFieldVisitor.Status.YES) { | ||
visitor.stringField(FAKE_ROUTING_FIELD, operation.routing().getBytes(StandardCharsets.UTF_8)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know some visitors won't like that _id is not seen, so maybe assert that |
||
} | ||
|
||
@Override | ||
protected void doClose() { | ||
|
||
} | ||
|
||
@Override | ||
public CacheHelper getReaderCacheHelper() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when do we expect this to be null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the translog generation is not available anymore I think it’s unlikely but I see a chance. I can add a comment in the code