Skip to content

Commit

Permalink
Simplify loading sequence number stats from commit
Browse files Browse the repository at this point in the history
This commit simplifies the loading of sequence number stats from a
commit point by combining all the logic for loading into a single method
that returns an instance of SeqNoStats rather than three separate
methods that return longs.
  • Loading branch information
jasontedor committed Jun 20, 2016
1 parent 6e8f11a commit 5032fa7
Showing 1 changed file with 34 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.elasticsearch.index.mapper.internal.SeqNoFieldMapper;
import org.elasticsearch.index.merge.MergeStats;
import org.elasticsearch.index.merge.OnGoingMerge;
import org.elasticsearch.index.seqno.SeqNoStats;
import org.elasticsearch.index.seqno.SequenceNumbersService;
import org.elasticsearch.index.shard.DocsStats;
import org.elasticsearch.index.shard.ElasticsearchMergePolicy;
Expand All @@ -80,6 +81,8 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;

import static org.elasticsearch.index.seqno.SequenceNumbersService.NO_OPS_PERFORMED;

/**
*
*/
Expand Down Expand Up @@ -142,17 +145,21 @@ public InternalEngine(EngineConfig engineConfig) throws EngineException {
this.searcherFactory = new SearchFactory(logger, isClosed, engineConfig);
try {
writer = createWriter(openMode == EngineConfig.OpenMode.CREATE_INDEX_AND_TRANSLOG);
final long localCheckpoint = loadLocalCheckpointFromCommit(writer);
final long globalCheckpoint = loadGlobalCheckpointFromCommit(writer);
final long maxSeqNo = loadMaxSeqNoFromCommit(writer);
final SeqNoStats seqNoStats = loadSeqNoStatsFromCommit(writer);
if (logger.isTraceEnabled()) {
logger.trace(
"recovering local checkpoint: [{}], global checkpoint [{}], max sequence number [{}]",
localCheckpoint,
globalCheckpoint,
maxSeqNo);
"recovering max sequence number: [{}], local checkpoint: [{}], global checkpoint: [{}]",
seqNoStats.getMaxSeqNo(),
seqNoStats.getLocalCheckpoint(),
seqNoStats.getGlobalCheckpoint());
}
seqNoService = new SequenceNumbersService(shardId, engineConfig.getIndexSettings(), maxSeqNo, localCheckpoint, globalCheckpoint);
seqNoService =
new SequenceNumbersService(
shardId,
engineConfig.getIndexSettings(),
seqNoStats.getMaxSeqNo(),
seqNoStats.getLocalCheckpoint(),
seqNoStats.getGlobalCheckpoint());
indexWriter = writer;
translog = openTranslog(engineConfig, writer);
assert translog.getGeneration() != null;
Expand Down Expand Up @@ -303,33 +310,34 @@ private Translog.TranslogGeneration loadTranslogIdFromCommit(IndexWriter writer)
return null;
}

private long loadLocalCheckpointFromCommit(IndexWriter writer) {
private SeqNoStats loadSeqNoStatsFromCommit(IndexWriter writer) throws IOException {
final long maxSeqNo;
try (IndexReader reader = DirectoryReader.open(writer)) {
final FieldStats stats = SeqNoFieldMapper.Defaults.FIELD_TYPE.stats(reader);
if (stats != null) {
maxSeqNo = (long) stats.getMaxValue();
} else {
maxSeqNo = NO_OPS_PERFORMED;
}
}

final Map<String, String> commitUserData = writer.getCommitData();

final long localCheckpoint;
if (commitUserData.containsKey(LOCAL_CHECKPOINT_KEY)) {
return Long.parseLong(commitUserData.get(LOCAL_CHECKPOINT_KEY));
localCheckpoint = Long.parseLong(commitUserData.get(LOCAL_CHECKPOINT_KEY));
} else {
return SequenceNumbersService.NO_OPS_PERFORMED;
localCheckpoint = SequenceNumbersService.NO_OPS_PERFORMED;
}
}

private long loadGlobalCheckpointFromCommit(IndexWriter writer) {
final Map<String, String> commitUserData = writer.getCommitData();
final long globalCheckpoint;
if (commitUserData.containsKey(GLOBAL_CHECKPOINT_KEY)) {
return Long.parseLong(commitUserData.get(GLOBAL_CHECKPOINT_KEY));
globalCheckpoint = Long.parseLong(commitUserData.get(GLOBAL_CHECKPOINT_KEY));
} else {
return SequenceNumbersService.UNASSIGNED_SEQ_NO;
globalCheckpoint = SequenceNumbersService.UNASSIGNED_SEQ_NO;
}
}

private long loadMaxSeqNoFromCommit(IndexWriter writer) throws IOException {
try (IndexReader reader = DirectoryReader.open(writer)) {
final FieldStats stats = SeqNoFieldMapper.Defaults.FIELD_TYPE.stats(reader);
if (stats != null) {
return (long) stats.getMaxValue();
} else {
return SequenceNumbersService.NO_OPS_PERFORMED;
}
}
return new SeqNoStats(maxSeqNo, localCheckpoint, globalCheckpoint);
}

private SearcherManager createSearcherManager() throws EngineException {
Expand Down

0 comments on commit 5032fa7

Please sign in to comment.