private boolean canOptimizeAddDocument(Index index) { if (index.getAutoGeneratedIdTimestamp() != IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP) { assert index.getAutoGeneratedIdTimestamp() >= 0 : "autoGeneratedIdTimestamp must be positive but was: " + index.getAutoGeneratedIdTimestamp(); switch (index.origin()) { case PRIMARY: assertPrimaryCanOptimizeAddDocument(index); return true; case PEER_RECOVERY: case REPLICA: assert index.version() == 1 && index.versionType() == VersionType.EXTERNAL : "version: " + index.version() + " type: " + index.versionType(); return true; case LOCAL_TRANSLOG_RECOVERY: case LOCAL_RESET: assert index.isRetry(); return true; // allow to optimize in order to update the max safe time stamp default: throw new IllegalArgumentException("unknown origin " + index.origin()); } } return false; }
@Override public IndexResult index(Index index) throws IOException { assert Objects.equals(index.uid().field(), uidField) : index.uid().field(); final boolean doThrottle = index.origin().isRecovery() == false; try (ReleasableLock releasableLock = readLock.acquire()) { ensureOpen(); assert assertIncomingSequenceNumber(index.origin(), index.seqNo()); assert assertVersionType(index); try (Releasable ignored = versionMap.acquireLock(index.uid().bytes()); plan.versionForIndexing, getPrimaryTerm(), plan.seqNoForIndexing, plan.currentNotFoundOrDeleted); if (index.origin().isFromTranslog() == false) { final Translog.Location location; if (indexResult.getResultType() == Result.Type.SUCCESS) { } else if (indexResult.getSeqNo() != SequenceNumbers.UNASSIGNED_SEQ_NO) { final NoOp noOp = new NoOp(indexResult.getSeqNo(), index.primaryTerm(), index.origin(), index.startTime(), indexResult.getFailure().toString()); location = innerNoOp(noOp).getTranslogLocation();
@Override public void postIndex(ShardId shardId, Engine.Index index, Engine.IndexResult result) { switch (result.getResultType()) { case SUCCESS: if (index.origin().isRecovery() == false) { long took = result.getTook(); totalStats.indexMetric.inc(took); totalStats.indexCurrent.dec(); StatsHolder typeStats = typeStats(index.type()); typeStats.indexMetric.inc(took); typeStats.indexCurrent.dec(); } break; case FAILURE: postIndex(shardId, index, result.getFailure()); break; default: throw new IllegalArgumentException("unknown result type: " + result.getResultType()); } }
@Override public boolean index(Index index) throws EngineException { final boolean created; try (ReleasableLock lock = readLock.acquire()) { ensureOpen(); if (index.origin() == Operation.Origin.RECOVERY) { // Don't throttle recovery operations created = innerIndex(index); } else { try (Releasable r = throttle.acquireThrottle()) { created = innerIndex(index); } } } catch (OutOfMemoryError | IllegalStateException | IOException t) { maybeFailEngine("index", t); throw new IndexFailedEngineException(shardId, index.type(), index.id(), t); } checkVersionMapRefresh(); return created; }
@Override public void postIndex(ShardId shardId, Engine.Index index, Engine.IndexResult result) { switch (result.getResultType()) { case SUCCESS: if (index.origin().isRecovery() == false) { long took = result.getTook(); totalStats.indexMetric.inc(took); totalStats.indexCurrent.dec(); StatsHolder typeStats = typeStats(index.type()); typeStats.indexMetric.inc(took); typeStats.indexCurrent.dec(); } break; case FAILURE: postIndex(shardId, index, result.getFailure()); break; default: throw new IllegalArgumentException("unknown result type: " + result.getResultType()); } }
@Override public void postIndex(ShardId shardId, Engine.Index index, Engine.IndexResult result) { switch (result.getResultType()) { case SUCCESS: if (index.origin().isRecovery() == false) { long took = result.getTook(); totalStats.indexMetric.inc(took); totalStats.indexCurrent.dec(); StatsHolder typeStats = typeStats(index.type()); typeStats.indexMetric.inc(took); typeStats.indexCurrent.dec(); } break; case FAILURE: postIndex(shardId, index, result.getFailure()); break; default: throw new IllegalArgumentException("unknown result type: " + result.getResultType()); } }
@Override public void postIndex(ShardId shardId, Engine.Index index, Engine.IndexResult result) { if (result.hasFailure() == false) { if (!index.origin().isRecovery()) { long took = result.getTook(); totalStats.indexMetric.inc(took); totalStats.indexCurrent.dec(); StatsHolder typeStats = typeStats(index.type()); typeStats.indexMetric.inc(took); typeStats.indexCurrent.dec(); } } else { postIndex(shardId, index, result.getFailure()); } }
private long generateSeqNoForOperation(final Operation operation) { assert operation.origin() == Operation.Origin.PRIMARY; return doGenerateSeqNoForOperation(operation); }
@Override public Engine.Index preIndex(ShardId shardId, Engine.Index operation) { if (operation.origin().isRecovery() == false) { totalStats.indexCurrent.inc(); typeStats(operation.type()).indexCurrent.inc(); } return operation; }
@Override public void postIndex(ShardId shardId, Engine.Index index, Exception ex) { if (!index.origin().isRecovery()) { totalStats.indexCurrent.dec(); typeStats(index.type()).indexCurrent.dec(); totalStats.indexFailed.inc(); typeStats(index.type()).indexFailed.inc(); } }
@Override public void postIndex(ShardId shardId, Engine.Index index, Exception ex) { if (!index.origin().isRecovery()) { totalStats.indexCurrent.dec(); typeStats(index.type()).indexCurrent.dec(); totalStats.indexFailed.inc(); typeStats(index.type()).indexFailed.inc(); } }
@Override public Engine.Index preIndex(ShardId shardId, Engine.Index operation) { if (operation.origin().isRecovery() == false) { totalStats.indexCurrent.inc(); typeStats(operation.type()).indexCurrent.inc(); } return operation; }
protected IndexingStrategy indexingStrategyForOperation(final Index index) throws IOException { if (index.origin() == Operation.Origin.PRIMARY) { return planIndexingAsPrimary(index); } else { // non-primary mode (i.e., replica or recovery) return planIndexingAsNonPrimary(index); } }
@Override public void postIndex(ShardId shardId, Engine.Index index, Exception ex) { if (!index.origin().isRecovery()) { totalStats.indexCurrent.dec(); typeStats(index.type()).indexCurrent.dec(); totalStats.indexFailed.inc(); typeStats(index.type()).indexFailed.inc(); } }
@Override public Engine.Index preIndex(ShardId shardId, Engine.Index operation) { if (!operation.origin().isRecovery()) { totalStats.indexCurrent.inc(); typeStats(operation.type()).indexCurrent.inc(); } return operation; }
protected final IndexingStrategy planIndexingAsPrimary(Index index) throws IOException { assert index.origin() == Operation.Origin.PRIMARY : "planing as primary but origin isn't. got " + index.origin(); assert getMaxSeqNoOfUpdatesOrDeletes() != SequenceNumbers.UNASSIGNED_SEQ_NO : "max_seq_no_of_updates is not initialized"; final IndexingStrategy plan;
private IndexResult indexIntoLucene(Index index, IndexingStrategy plan) throws IOException { assert assertSequenceNumberBeforeIndexing(index.origin(), plan.seqNoForIndexing); assert plan.versionForIndexing >= 0 : "version must be set. got " + plan.versionForIndexing; assert plan.indexIntoLucene || plan.addStaleOpToLucene;
protected IndexingStrategy indexingStrategyForOperation(final Index index) throws IOException { if (index.origin() == Operation.Origin.PRIMARY) { return planIndexingAsPrimary(index); } else { // non-primary mode (i.e., replica or recovery) return planIndexingAsNonPrimary(index); } }
@Override public void postIndex(ShardId shardId, Engine.Index index, Exception ex) { if (!index.origin().isRecovery()) { totalStats.indexCurrent.dec(); typeStats(index.type()).indexCurrent.dec(); totalStats.indexFailed.inc(); typeStats(index.type()).indexFailed.inc(); } }
@Override public Engine.Index preIndex(ShardId shardId, Engine.Index operation) { if (operation.origin().isRecovery() == false) { totalStats.indexCurrent.inc(); typeStats(operation.type()).indexCurrent.inc(); } return operation; }