Skip to content

Commit 39ef2c4

Browse files
committed
Refactor internal engine
This commit is a minor refactoring of internal engine to move hooks for generating sequence numbers into the engine itself. As such, we refactor tests that relied on this hook to use the new hook, and remove the hook from the sequence number service itself. Relates #27082
1 parent 966bd7e commit 39ef2c4

File tree

7 files changed

+433
-271
lines changed

7 files changed

+433
-271
lines changed

core/src/main/java/org/elasticsearch/index/engine/Engine.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,11 @@ public CommitStats commitStats() {
572572
return new CommitStats(getLastCommittedSegmentInfos());
573573
}
574574

575-
/** get the sequence number service */
575+
/**
576+
* The sequence number service for this engine.
577+
*
578+
* @return the sequence number service
579+
*/
576580
public abstract SequenceNumbersService seqNoService();
577581

578582
/**

core/src/main/java/org/elasticsearch/index/engine/InternalEngine.java

+83-64
Large diffs are not rendered by default.

core/src/main/java/org/elasticsearch/index/seqno/SequenceNumbersService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public SequenceNumbersService(
6666
*
6767
* @return the next assigned sequence number
6868
*/
69-
public long generateSeqNo() {
69+
public final long generateSeqNo() {
7070
return localCheckpointTracker.generateSeqNo();
7171
}
7272

core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java

+197-204
Large diffs are not rendered by default.

core/src/test/java/org/elasticsearch/index/replication/IndexLevelReplicationTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public long addDocument(Iterable<? extends IndexableField> doc) throws IOExcepti
316316
assert documentFailureMessage != null;
317317
throw new IOException(documentFailureMessage);
318318
}
319-
}, null, config);
319+
}, null, null, config);
320320
}
321321
}
322322

core/src/test/java/org/elasticsearch/index/replication/RecoveryDuringReplicationTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,7 @@ public long addDocument(final Iterable<? extends IndexableField> doc) throws IOE
637637
}
638638
},
639639
null,
640+
null,
640641
config);
641642
}
642643

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.engine;
21+
22+
import org.apache.lucene.analysis.standard.StandardAnalyzer;
23+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
24+
import org.elasticsearch.common.xcontent.XContentFactory;
25+
import org.elasticsearch.index.IndexSettings;
26+
import org.elasticsearch.index.analysis.AnalyzerScope;
27+
import org.elasticsearch.index.analysis.IndexAnalyzers;
28+
import org.elasticsearch.index.analysis.NamedAnalyzer;
29+
import org.elasticsearch.index.mapper.DocumentMapper;
30+
import org.elasticsearch.index.mapper.DocumentMapperForType;
31+
import org.elasticsearch.index.mapper.MapperService;
32+
import org.elasticsearch.index.mapper.Mapping;
33+
import org.elasticsearch.index.mapper.RootObjectMapper;
34+
import org.elasticsearch.index.shard.IndexShard;
35+
import org.elasticsearch.index.similarity.SimilarityService;
36+
import org.elasticsearch.index.translog.Translog;
37+
import org.elasticsearch.indices.IndicesModule;
38+
import org.elasticsearch.indices.mapper.MapperRegistry;
39+
40+
import java.io.IOException;
41+
import java.util.HashMap;
42+
import java.util.Map;
43+
import java.util.concurrent.atomic.AtomicLong;
44+
45+
import static java.util.Collections.emptyList;
46+
import static java.util.Collections.emptyMap;
47+
import static org.elasticsearch.index.mapper.SourceToParse.source;
48+
49+
public class TranslogHandler implements EngineConfig.TranslogRecoveryRunner {
50+
51+
private final MapperService mapperService;
52+
public Mapping mappingUpdate = null;
53+
private final Map<String, Mapping> recoveredTypes = new HashMap<>();
54+
55+
private final AtomicLong appliedOperations = new AtomicLong();
56+
57+
long appliedOperations() {
58+
return appliedOperations.get();
59+
}
60+
61+
public TranslogHandler(NamedXContentRegistry xContentRegistry, IndexSettings indexSettings) {
62+
NamedAnalyzer defaultAnalyzer = new NamedAnalyzer("default", AnalyzerScope.INDEX, new StandardAnalyzer());
63+
IndexAnalyzers indexAnalyzers =
64+
new IndexAnalyzers(indexSettings, defaultAnalyzer, defaultAnalyzer, defaultAnalyzer, emptyMap(), emptyMap());
65+
SimilarityService similarityService = new SimilarityService(indexSettings, null, emptyMap());
66+
MapperRegistry mapperRegistry = new IndicesModule(emptyList()).getMapperRegistry();
67+
mapperService = new MapperService(indexSettings, indexAnalyzers, xContentRegistry, similarityService, mapperRegistry,
68+
() -> null);
69+
}
70+
71+
private DocumentMapperForType docMapper(String type) {
72+
RootObjectMapper.Builder rootBuilder = new RootObjectMapper.Builder(type);
73+
DocumentMapper.Builder b = new DocumentMapper.Builder(rootBuilder, mapperService);
74+
return new DocumentMapperForType(b.build(mapperService), mappingUpdate);
75+
}
76+
77+
private void applyOperation(Engine engine, Engine.Operation operation) throws IOException {
78+
switch (operation.operationType()) {
79+
case INDEX:
80+
Engine.Index engineIndex = (Engine.Index) operation;
81+
Mapping update = engineIndex.parsedDoc().dynamicMappingsUpdate();
82+
if (engineIndex.parsedDoc().dynamicMappingsUpdate() != null) {
83+
recoveredTypes.compute(engineIndex.type(), (k, mapping) -> mapping == null ? update : mapping.merge(update, false));
84+
}
85+
engine.index(engineIndex);
86+
break;
87+
case DELETE:
88+
engine.delete((Engine.Delete) operation);
89+
break;
90+
case NO_OP:
91+
engine.noOp((Engine.NoOp) operation);
92+
break;
93+
default:
94+
throw new IllegalStateException("No operation defined for [" + operation + "]");
95+
}
96+
}
97+
98+
/**
99+
* Returns the recovered types modifying the mapping during the recovery
100+
*/
101+
public Map<String, Mapping> getRecoveredTypes() {
102+
return recoveredTypes;
103+
}
104+
105+
@Override
106+
public int run(Engine engine, Translog.Snapshot snapshot) throws IOException {
107+
int opsRecovered = 0;
108+
Translog.Operation operation;
109+
while ((operation = snapshot.next()) != null) {
110+
applyOperation(engine, convertToEngineOp(operation, Engine.Operation.Origin.LOCAL_TRANSLOG_RECOVERY));
111+
opsRecovered++;
112+
appliedOperations.incrementAndGet();
113+
}
114+
return opsRecovered;
115+
}
116+
117+
private Engine.Operation convertToEngineOp(Translog.Operation operation, Engine.Operation.Origin origin) {
118+
switch (operation.opType()) {
119+
case INDEX:
120+
final Translog.Index index = (Translog.Index) operation;
121+
final String indexName = mapperService.index().getName();
122+
final Engine.Index engineIndex = IndexShard.prepareIndex(docMapper(index.type()),
123+
mapperService.getIndexSettings().getIndexVersionCreated(),
124+
source(indexName, index.type(), index.id(), index.source(), XContentFactory.xContentType(index.source()))
125+
.routing(index.routing()).parent(index.parent()), index.seqNo(), index.primaryTerm(),
126+
index.version(), index.versionType().versionTypeForReplicationAndRecovery(), origin,
127+
index.getAutoGeneratedIdTimestamp(), true);
128+
return engineIndex;
129+
case DELETE:
130+
final Translog.Delete delete = (Translog.Delete) operation;
131+
final Engine.Delete engineDelete = new Engine.Delete(delete.type(), delete.id(), delete.uid(), delete.seqNo(),
132+
delete.primaryTerm(), delete.version(), delete.versionType().versionTypeForReplicationAndRecovery(),
133+
origin, System.nanoTime());
134+
return engineDelete;
135+
case NO_OP:
136+
final Translog.NoOp noOp = (Translog.NoOp) operation;
137+
final Engine.NoOp engineNoOp =
138+
new Engine.NoOp(noOp.seqNo(), noOp.primaryTerm(), origin, System.nanoTime(), noOp.reason());
139+
return engineNoOp;
140+
default:
141+
throw new IllegalStateException("No operation defined for [" + operation + "]");
142+
}
143+
}
144+
145+
}

0 commit comments

Comments
 (0)