From 36b9edba3ba806c3fc725b05dc4e952458904fb9 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Thu, 8 Dec 2016 09:42:07 -0500 Subject: [PATCH] Introduce translog no-op As the translog evolves towards a full operations log as part of the sequence numbers push, there is a need for the translog to be able to represent operations for which a sequence number was assigned, but the operation did not mutate the index. Examples of how this can arise are operations that fail after the sequence number is assigned, and gaps in this history that arise when an operation is assigned a sequence number but the operation never completed (e.g., a node crash). It is important that these operations appear in the history so that they can be replicated and replayed during recovery as otherwise the history will be incomplete and local checkpoints will not be able to advance. This commit introduces a no-op to the translog to set the stage for these efforts. --- .../common/io/stream/StreamOutput.java | 5 + .../elasticsearch/index/engine/Engine.java | 65 ++++++- .../index/engine/InternalEngine.java | 44 ++++- .../index/engine/ShadowEngine.java | 5 + .../shard/TranslogRecoveryPerformer.java | 24 ++- .../index/translog/Translog.java | 92 +++++++++- .../cluster/node/stats/NodeStatsTests.java | 77 ++++---- .../action/update/UpdateRequestTests.java | 2 +- .../common/FieldMemoryStatsTests.java | 2 +- .../common/unit/ByteSizeValueTests.java | 10 +- .../common/unit/SizeValueTests.java | 8 +- .../common/unit/TimeValueTests.java | 8 +- .../zen/ElectMasterServiceTests.java | 3 +- .../discovery/zen/UnicastZenPingTests.java | 4 +- .../fieldstats/FieldStatsTests.java | 48 ++--- .../index/engine/InternalEngineTests.java | 45 +++++ .../index/fielddata/FieldDataStatsTests.java | 2 +- .../index/get/GetResultTests.java | 4 +- .../index/mapper/DateFieldTypeTests.java | 2 +- .../index/mapper/RangeFieldTypeTests.java | 2 +- .../index/query/QueryShardContextTests.java | 2 +- .../GlobalCheckpointSyncActionTests.java | 6 +- .../suggest/stats/CompletionsStatsTests.java | 2 +- .../index/translog/TranslogTests.java | 167 ++++++++++++------ .../monitor/os/OsProbeTests.java | 2 +- .../monitor/os/OsStatsTests.java | 8 +- .../monitor/process/ProcessProbeTests.java | 2 +- .../nodesinfo/NodeInfoStreamingTests.java | 4 +- .../search/SearchRequestTests.java | 2 +- .../ShardSearchTransportRequestTests.java | 2 +- .../rescore/QueryRescoreBuilderTests.java | 2 +- .../search/sort/AbstractSortTestCase.java | 2 +- .../index/reindex/RoundTripTests.java | 4 +- .../geoip/GeoIpProcessorFactoryTests.java | 2 +- .../test/AbstractQueryTestCase.java | 2 +- .../org/elasticsearch/test/ESTestCase.java | 2 +- .../search/MockSearchServiceTests.java | 2 +- 37 files changed, 485 insertions(+), 180 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index 4fc253cf45d21..7a2cb02b650fb 100644 --- a/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/core/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -223,6 +223,11 @@ public void writeVLong(long i) throws IOException { writeByte((byte) i); } + public static int lengthVLong(long i) { + assert i >= 0; + return 1 + (int) Math.floor(Math.log(i) / Math.log(2)) / 7; + } + /** * Writes a long in a variable-length format. Writes between one and ten bytes. * Values are remapped by sliding the sign bit into the lsb and then encoded as an unsigned number diff --git a/core/src/main/java/org/elasticsearch/index/engine/Engine.java b/core/src/main/java/org/elasticsearch/index/engine/Engine.java index 0f1c05e59ee95..16940cb1e15e3 100644 --- a/core/src/main/java/org/elasticsearch/index/engine/Engine.java +++ b/core/src/main/java/org/elasticsearch/index/engine/Engine.java @@ -47,7 +47,6 @@ import org.elasticsearch.common.Nullable; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.ImmutableOpenMap; -import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.Writeable; @@ -296,6 +295,8 @@ public Condition newCondition() { */ public abstract DeleteResult delete(final Delete delete); + public abstract NoOpResult noOp(final NoOp noOp); + /** * Base class for index and delete operation results * Holds result meta data (e.g. translog location, updated version) @@ -382,6 +383,7 @@ void freeze() { } public static class IndexResult extends Result { + private final boolean created; public IndexResult(long version, long seqNo, boolean created) { @@ -397,9 +399,11 @@ public IndexResult(Exception failure, long version, long seqNo) { public boolean isCreated() { return created; } + } public static class DeleteResult extends Result { + private final boolean found; public DeleteResult(long version, long seqNo, boolean found) { @@ -415,6 +419,19 @@ public DeleteResult(Exception failure, long version, long seqNo) { public boolean isFound() { return found; } + + } + + static class NoOpResult extends Result { + + NoOpResult(long seqNo) { + super(Operation.TYPE.NO_OP, 0, seqNo); + } + + NoOpResult(long seqNo, Exception failure) { + super(Operation.TYPE.NO_OP, failure, 0, seqNo); + } + } /** @@ -910,7 +927,7 @@ public abstract static class Operation { /** type of operation (index, delete), subclasses use static types */ public enum TYPE { - INDEX, DELETE; + INDEX, DELETE, NO_OP; private final String lowercase; @@ -1114,6 +1131,50 @@ TYPE operationType() { public int estimatedSizeInBytes() { return (uid().field().length() + uid().text().length()) * 2 + 20; } + + } + + public static class NoOp extends Operation { + + private final String reason; + + public String reason() { + return reason; + } + + public NoOp( + final Term uid, + final long seqNo, + final long primaryTerm, + final long version, + final VersionType versionType, + final Origin origin, + final long startTime, + final String reason) { + super(uid, seqNo, primaryTerm, version, versionType, origin, startTime); + this.reason = reason; + } + + @Override + public String type() { + throw new UnsupportedOperationException(); + } + + @Override + String id() { + throw new UnsupportedOperationException(); + } + + @Override + TYPE operationType() { + return TYPE.NO_OP; + } + + @Override + public int estimatedSizeInBytes() { + return 2 * reason.length() + StreamOutput.lengthVLong(seqNo()) + StreamOutput.lengthVLong(primaryTerm()); + } + } public static class Get { diff --git a/core/src/main/java/org/elasticsearch/index/engine/InternalEngine.java b/core/src/main/java/org/elasticsearch/index/engine/InternalEngine.java index 2957b9ab06405..e142ac0f6f49e 100644 --- a/core/src/main/java/org/elasticsearch/index/engine/InternalEngine.java +++ b/core/src/main/java/org/elasticsearch/index/engine/InternalEngine.java @@ -277,7 +277,7 @@ private void recoverFromTranslog(TranslogRecoveryPerformer handler) throws IOExc } // flush if we recovered something or if we have references to older translogs // note: if opsRecovered == 0 and we have older translogs it means they are corrupted or 0 length. - assert pendingTranslogRecovery.get(): "translogRecovery is not pending but should be"; + assert pendingTranslogRecovery.get() : "translogRecovery is not pending but should be"; pendingTranslogRecovery.set(false); // we are good - now we can commit if (opsRecovered > 0) { logger.trace("flushing post recovery from translog. ops recovered [{}]. committed translog id [{}]. current id [{}]", @@ -375,7 +375,7 @@ private static SeqNoStats loadSeqNoStatsFromLuceneAndTranslog( * specified global checkpoint. * * @param globalCheckpoint the global checkpoint to use - * @param indexWriter the index writer (for the Lucene commit point) + * @param indexWriter the index writer (for the Lucene commit point) * @return the sequence number stats */ private static SeqNoStats loadSeqNoStatsFromLucene(final long globalCheckpoint, final IndexWriter indexWriter) { @@ -434,7 +434,7 @@ public GetResult get(Get get, Function searcherFactory) throws if (get.versionType().isVersionConflictForReads(versionValue.version(), get.version())) { Uid uid = Uid.createUid(get.uid().text()); throw new VersionConflictEngineException(shardId, uid.type(), uid.id(), - get.versionType().explainConflictForReads(versionValue.version(), get.version())); + get.versionType().explainConflictForReads(versionValue.version(), get.version())); } refresh("realtime_get"); } @@ -532,7 +532,7 @@ public IndexResult index(Index index) { * * @return failure if the failure is a document specific failure (e.g. analysis chain failure) * or throws Exception if the failure caused the engine to fail (e.g. out of disk, lucene tragic event) - * + *

* Note: pkg-private for testing */ final Exception checkIfDocumentFailureOrThrow(final Operation operation, final Exception failure) { @@ -577,7 +577,7 @@ private boolean canOptimizeAddDocument(Index index) { case PEER_RECOVERY: case REPLICA: assert index.version() == 1 && index.versionType() == VersionType.EXTERNAL - : "version: " + index.version() + " type: " + index.versionType(); + : "version: " + index.version() + " type: " + index.versionType(); return true; case LOCAL_TRANSLOG_RECOVERY: assert index.isRetry(); @@ -596,10 +596,10 @@ private boolean assertSequenceNumber(final Engine.Operation.Origin origin, final " index version: " + engineConfig.getIndexSettings().getIndexVersionCreated() + ". seq no: " + seqNo; } else if (origin == Operation.Origin.PRIMARY) { // sequence number should not be set when operation origin is primary - assert seqNo == SequenceNumbersService.UNASSIGNED_SEQ_NO : "primary ops should never an assigned seq no. got: " + seqNo; + assert seqNo == SequenceNumbersService.UNASSIGNED_SEQ_NO : "primary ops should never have an assigned seq no. got: " + seqNo; } else { // sequence number should be set when operation origin is not primary - assert seqNo >= 0 : "replica ops should an assigned seq no. origin: " + origin + + assert seqNo >= 0 : "recovery or replica ops should have an assigned seq no. origin: " + origin + " index version: " + engineConfig.getIndexSettings().getIndexVersionCreated(); } return true; @@ -651,7 +651,7 @@ private IndexResult innerIndex(Index index) throws IOException { if (deOptimizeTimestamp >= index.getAutoGeneratedIdTimestamp()) { break; } - } while(maxUnsafeAutoIdTimestamp.compareAndSet(deOptimizeTimestamp, + } while (maxUnsafeAutoIdTimestamp.compareAndSet(deOptimizeTimestamp, index.getAutoGeneratedIdTimestamp()) == false); assert maxUnsafeAutoIdTimestamp.get() >= index.getAutoGeneratedIdTimestamp(); } else { @@ -859,6 +859,34 @@ private boolean deleteIfFound(Term uid, long currentVersion, boolean deleted, Ve return found; } + @Override + public NoOpResult noOp(final NoOp noOp) { + NoOpResult noOpResult; + try (final ReleasableLock ignored = readLock.acquire()) { + noOpResult = innerNoOp(noOp); + } catch (final Exception e) { + noOpResult = new NoOpResult(noOp.seqNo(), e); + } + return noOpResult; + } + + private NoOpResult innerNoOp(final NoOp noOp) throws IOException { + assert noOp.seqNo() > SequenceNumbersService.NO_OPS_PERFORMED; + final long seqNo = noOp.seqNo(); + try { + final NoOpResult noOpResult = new NoOpResult(noOp.seqNo()); + final Translog.Location location = translog.add(new Translog.NoOp(noOp.seqNo(), noOp.primaryTerm(), noOp.reason())); + noOpResult.setTranslogLocation(location); + noOpResult.setTook(System.nanoTime() - noOp.startTime()); + noOpResult.freeze(); + return noOpResult; + } finally { + if (seqNo != SequenceNumbersService.UNASSIGNED_SEQ_NO) { + seqNoService().markSeqNoAsCompleted(seqNo); + } + } + } + @Override public void refresh(String source) throws EngineException { // we obtain a read lock here, since we don't want a flush to happen while we are refreshing diff --git a/core/src/main/java/org/elasticsearch/index/engine/ShadowEngine.java b/core/src/main/java/org/elasticsearch/index/engine/ShadowEngine.java index 39bff89b7def0..c7226fc8b0dfc 100644 --- a/core/src/main/java/org/elasticsearch/index/engine/ShadowEngine.java +++ b/core/src/main/java/org/elasticsearch/index/engine/ShadowEngine.java @@ -116,6 +116,11 @@ public DeleteResult delete(Delete delete) { throw new UnsupportedOperationException(shardId + " delete operation not allowed on shadow engine"); } + @Override + public NoOpResult noOp(NoOp noOp) { + throw new UnsupportedOperationException(shardId + " no-op operation not allowed on shadow engine"); + } + @Override public SyncedFlushResult syncFlush(String syncId, CommitId expectedCommitId) { throw new UnsupportedOperationException(shardId + " sync commit operation not allowed on shadow engine"); diff --git a/core/src/main/java/org/elasticsearch/index/shard/TranslogRecoveryPerformer.java b/core/src/main/java/org/elasticsearch/index/shard/TranslogRecoveryPerformer.java index f27958b71f5f9..b5c23d9dc467d 100644 --- a/core/src/main/java/org/elasticsearch/index/shard/TranslogRecoveryPerformer.java +++ b/core/src/main/java/org/elasticsearch/index/shard/TranslogRecoveryPerformer.java @@ -22,6 +22,7 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.index.VersionType; import org.elasticsearch.index.engine.Engine; import org.elasticsearch.index.engine.IgnoreOnRecoveryEngineException; import org.elasticsearch.index.mapper.DocumentMapperForType; @@ -29,6 +30,7 @@ import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.Mapping; import org.elasticsearch.index.mapper.Uid; +import org.elasticsearch.index.seqno.SequenceNumbersService; import org.elasticsearch.index.translog.Translog; import org.elasticsearch.rest.RestStatus; @@ -94,6 +96,7 @@ public int recoveryFromSnapshot(Engine engine, Translog.Snapshot snapshot) throw } } } + return opsRecovered; } @@ -158,22 +161,28 @@ private void performRecoveryOperation(Engine engine, Translog.Operation operatio .routing(index.routing()).parent(index.parent()), index.seqNo(), index.primaryTerm(), index.version(), index.versionType().versionTypeForReplicationAndRecovery(), origin, index.getAutoGeneratedIdTimestamp(), true); maybeAddMappingUpdate(engineIndex.type(), engineIndex.parsedDoc().dynamicMappingsUpdate(), engineIndex.id(), allowMappingUpdates); - if (logger.isTraceEnabled()) { - logger.trace("[translog] recover [index] op of [{}][{}]", index.type(), index.id()); - } + logger.trace("[translog] recover [index] op [({}, {})] of [{}][{}]", index.seqNo(), index.primaryTerm(), index.type(), index.id()); index(engine, engineIndex); break; case DELETE: Translog.Delete delete = (Translog.Delete) operation; Uid uid = Uid.createUid(delete.uid().text()); - if (logger.isTraceEnabled()) { - logger.trace("[translog] recover [delete] op of [{}][{}]", uid.type(), uid.id()); - } + logger.trace("[translog] recover [delete] op [({}, {})] of [{}][{}]", delete.seqNo(), delete.primaryTerm(), uid.type(), uid.id()); final Engine.Delete engineDelete = new Engine.Delete(uid.type(), uid.id(), delete.uid(), delete.seqNo(), delete.primaryTerm(), delete.version(), delete.versionType().versionTypeForReplicationAndRecovery(), origin, System.nanoTime()); delete(engine, engineDelete); break; + case NO_OP: + final Translog.NoOp noOp = (Translog.NoOp) operation; + final long seqNo = noOp.seqNo(); + final long primaryTerm = noOp.primaryTerm(); + final String reason = noOp.reason(); + logger.trace("[translog] recover [no_op] op [({}, {})] of [{}]", seqNo, primaryTerm, reason); + final Engine.NoOp engineNoOp = + new Engine.NoOp(null, seqNo, primaryTerm, 0, VersionType.INTERNAL, origin, System.nanoTime(), reason); + noOp(engine, engineNoOp); + break; default: throw new IllegalStateException("No operation defined for [" + operation + "]"); } @@ -206,6 +215,9 @@ protected void delete(Engine engine, Engine.Delete engineDelete) { engine.delete(engineDelete); } + protected void noOp(Engine engine, Engine.NoOp engineNoOp) { + engine.noOp(engineNoOp); + } /** * Called once for every processed operation by this recovery performer. diff --git a/core/src/main/java/org/elasticsearch/index/translog/Translog.java b/core/src/main/java/org/elasticsearch/index/translog/Translog.java index f7560960660ca..62287943a12af 100644 --- a/core/src/main/java/org/elasticsearch/index/translog/Translog.java +++ b/core/src/main/java/org/elasticsearch/index/translog/Translog.java @@ -55,6 +55,7 @@ import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.concurrent.atomic.AtomicBoolean; @@ -743,7 +744,8 @@ enum Type { @Deprecated CREATE((byte) 1), INDEX((byte) 2), - DELETE((byte) 3); + DELETE((byte) 3), + NO_OP((byte) 4); private final byte id; @@ -763,6 +765,8 @@ public static Type fromId(byte id) { return INDEX; case 3: return DELETE; + case 4: + return NO_OP; default: throw new IllegalArgumentException("No type mapped for [" + id + "]"); } @@ -786,9 +790,11 @@ static Operation readType(StreamInput input) throws IOException { // the deserialization logic in Index was identical to that of Create when create was deprecated return new Index(input); case DELETE: - return new Translog.Delete(input); + return new Delete(input); case INDEX: return new Index(input); + case NO_OP: + return new NoOp(input); default: throw new IOException("No type for [" + type + "]"); } @@ -805,6 +811,7 @@ static void writeType(Translog.Operation operation, StreamOutput output) throws } public static class Source { + public final BytesReference source; public final String routing; public final String parent; @@ -814,9 +821,11 @@ public Source(BytesReference source, String routing, String parent) { this.routing = routing; this.parent = parent; } + } public static class Index implements Operation { + public static final int FORMAT_2x = 6; // since 2.0-beta1 and 1.1 public static final int FORMAT_AUTO_GENERATED_IDS = 7; // since 5.0.0-beta1 public static final int FORMAT_SEQ_NO = FORMAT_AUTO_GENERATED_IDS + 1; // since 6.0.0 @@ -943,7 +952,7 @@ public void writeTo(StreamOutput out) throws IOException { out.writeOptionalString(routing); out.writeOptionalString(parent); out.writeLong(version); - + out.writeByte(versionType.getValue()); out.writeLong(autoGeneratedIdTimestamp); out.writeVLong(seqNo); @@ -1004,9 +1013,11 @@ public String toString() { public long getAutoGeneratedIdTimestamp() { return autoGeneratedIdTimestamp; } + } public static class Delete implements Operation { + private static final int FORMAT_5_X = 3; private static final int FORMAT_SEQ_NO = FORMAT_5_X + 1; public static final int SERIALIZATION_FORMAT = FORMAT_SEQ_NO; @@ -1127,6 +1138,81 @@ public String toString() { "uid=" + uid + '}'; } + + } + + public static class NoOp implements Operation { + + private final long seqNo; + private final long primaryTerm; + private final String reason; + + public long seqNo() { + return seqNo; + } + + public long primaryTerm() { + return primaryTerm; + } + + public String reason() { + return reason; + } + + NoOp(final StreamInput in) throws IOException { + seqNo = in.readVLong(); + primaryTerm = in.readVLong(); + reason = in.readString(); + } + + public NoOp(final long seqNo, final long primaryTerm, final String reason) { + assert seqNo > SequenceNumbersService.NO_OPS_PERFORMED; + assert primaryTerm >= 0; + assert reason != null; + this.seqNo = seqNo; + this.primaryTerm = primaryTerm; + this.reason = reason; + } + + @Override + public void writeTo(StreamOutput out) throws IOException { + out.writeVLong(seqNo); + out.writeVLong(primaryTerm); + out.writeString(reason); + } + + @Override + public Type opType() { + return Type.NO_OP; + } + + @Override + public long estimateSize() { + return 2 * reason.length() + StreamOutput.lengthVLong(seqNo) + StreamOutput.lengthVLong(primaryTerm); + } + + @Override + public Source getSource() { + throw new UnsupportedOperationException("source does not exist for a no-op"); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + final NoOp that = (NoOp) obj; + return seqNo == that.seqNo && primaryTerm == that.primaryTerm && reason.equals(that.reason); + } + + @Override + public int hashCode() { + return 31 * 31 * 31 + 31 * 31 * Long.hashCode(seqNo) + 31 * Long.hashCode(primaryTerm) + reason().hashCode(); + } + } diff --git a/core/src/test/java/org/elasticsearch/action/admin/cluster/node/stats/NodeStatsTests.java b/core/src/test/java/org/elasticsearch/action/admin/cluster/node/stats/NodeStatsTests.java index 6913abd81a4ae..d3d156ee2cdfd 100644 --- a/core/src/test/java/org/elasticsearch/action/admin/cluster/node/stats/NodeStatsTests.java +++ b/core/src/test/java/org/elasticsearch/action/admin/cluster/node/stats/NodeStatsTests.java @@ -291,41 +291,51 @@ private static NodeStats createNodeStats() { new OsStats.Swap(randomLong(), randomLong()), new OsStats.Cgroup( randomAsciiOfLength(8), - randomPositiveLong(), + randomNonNegativeLong(), randomAsciiOfLength(8), - randomPositiveLong(), - randomPositiveLong(), - new OsStats.Cgroup.CpuStat(randomPositiveLong(), randomPositiveLong(), randomPositiveLong()))); + randomNonNegativeLong(), + randomNonNegativeLong(), + new OsStats.Cgroup.CpuStat(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong()))); } - ProcessStats processStats = frequently() ? new ProcessStats(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - new ProcessStats.Cpu(randomShort(), randomPositiveLong()), - new ProcessStats.Mem(randomPositiveLong())) : null; + ProcessStats processStats = frequently() ? + new ProcessStats( + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong(), + new ProcessStats.Cpu(randomShort(), randomNonNegativeLong()), + new ProcessStats.Mem(randomNonNegativeLong())) : + null; JvmStats jvmStats = null; if (frequently()) { int numMemoryPools = randomIntBetween(0, 10); List memoryPools = new ArrayList<>(numMemoryPools); for (int i = 0; i < numMemoryPools; i++) { - memoryPools.add(new JvmStats.MemoryPool(randomAsciiOfLengthBetween(3, 10), randomPositiveLong(), - randomPositiveLong(), randomPositiveLong(), randomPositiveLong())); + memoryPools.add(new JvmStats.MemoryPool(randomAsciiOfLengthBetween(3, 10), randomNonNegativeLong(), + randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong())); } JvmStats.Threads threads = new JvmStats.Threads(randomIntBetween(1, 1000), randomIntBetween(1, 1000)); int numGarbageCollectors = randomIntBetween(0, 10); JvmStats.GarbageCollector[] garbageCollectorsArray = new JvmStats.GarbageCollector[numGarbageCollectors]; for (int i = 0; i < numGarbageCollectors; i++) { garbageCollectorsArray[i] = new JvmStats.GarbageCollector(randomAsciiOfLengthBetween(3, 10), - randomPositiveLong(), randomPositiveLong()); + randomNonNegativeLong(), randomNonNegativeLong()); } JvmStats.GarbageCollectors garbageCollectors = new JvmStats.GarbageCollectors(garbageCollectorsArray); int numBufferPools = randomIntBetween(0, 10); List bufferPoolList = new ArrayList<>(); for (int i = 0; i < numBufferPools; i++) { - bufferPoolList.add(new JvmStats.BufferPool(randomAsciiOfLengthBetween(3, 10), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong())); + bufferPoolList.add( + new JvmStats.BufferPool( + randomAsciiOfLengthBetween(3, 10), + randomNonNegativeLong(), + randomNonNegativeLong(), + randomNonNegativeLong())); } - JvmStats.Classes classes = new JvmStats.Classes(randomPositiveLong(), randomPositiveLong(), randomPositiveLong()); - jvmStats = frequently() ? new JvmStats(randomPositiveLong(), randomPositiveLong(), new JvmStats.Mem(randomPositiveLong(), - randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), memoryPools), threads, - garbageCollectors, randomBoolean() ? Collections.emptyList() : bufferPoolList, classes) : null; + JvmStats.Classes classes = new JvmStats.Classes(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong()); + jvmStats = + frequently() ? new JvmStats(randomNonNegativeLong(), randomNonNegativeLong(), new JvmStats.Mem(randomNonNegativeLong(), + randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), memoryPools), + threads, garbageCollectors, randomBoolean() ? Collections.emptyList() : bufferPoolList, classes) : null; } ThreadPoolStats threadPoolStats = null; if (frequently()) { @@ -333,7 +343,7 @@ private static NodeStats createNodeStats() { List threadPoolStatsList = new ArrayList<>(); for (int i = 0; i < numThreadPoolStats; i++) { threadPoolStatsList.add(new ThreadPoolStats.Stats(randomAsciiOfLengthBetween(3, 10), randomIntBetween(1, 1000), - randomIntBetween(1, 1000), randomIntBetween(1, 1000), randomPositiveLong(), + randomIntBetween(1, 1000), randomIntBetween(1, 1000), randomNonNegativeLong(), randomIntBetween(1, 1000), randomIntBetween(1, 1000))); } threadPoolStats = new ThreadPoolStats(threadPoolStatsList); @@ -345,50 +355,51 @@ private static NodeStats createNodeStats() { for (int i = 0; i < numDeviceStats; i++) { FsInfo.DeviceStats previousDeviceStats = randomBoolean() ? null : new FsInfo.DeviceStats(randomInt(), randomInt(), randomAsciiOfLengthBetween(3, 10), - randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), null); - deviceStatsArray[i] = new FsInfo.DeviceStats(randomInt(), randomInt(), randomAsciiOfLengthBetween(3, 10), - randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), previousDeviceStats); + randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), null); + deviceStatsArray[i] = + new FsInfo.DeviceStats(randomInt(), randomInt(), randomAsciiOfLengthBetween(3, 10), randomNonNegativeLong(), + randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), previousDeviceStats); } FsInfo.IoStats ioStats = new FsInfo.IoStats(deviceStatsArray); int numPaths = randomIntBetween(0, 10); FsInfo.Path[] paths = new FsInfo.Path[numPaths]; for (int i = 0; i < numPaths; i++) { paths[i] = new FsInfo.Path(randomAsciiOfLengthBetween(3, 10), randomBoolean() ? randomAsciiOfLengthBetween(3, 10) : null, - randomPositiveLong(), randomPositiveLong(), randomPositiveLong()); + randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong()); } - fsInfo = new FsInfo(randomPositiveLong(), ioStats, paths); + fsInfo = new FsInfo(randomNonNegativeLong(), ioStats, paths); } - TransportStats transportStats = frequently() ? new TransportStats(randomPositiveLong(), randomPositiveLong(), - randomPositiveLong(), randomPositiveLong(), randomPositiveLong()) : null; - HttpStats httpStats = frequently() ? new HttpStats(randomPositiveLong(), randomPositiveLong()) : null; + TransportStats transportStats = frequently() ? new TransportStats(randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong()) : null; + HttpStats httpStats = frequently() ? new HttpStats(randomNonNegativeLong(), randomNonNegativeLong()) : null; AllCircuitBreakerStats allCircuitBreakerStats = null; if (frequently()) { int numCircuitBreakerStats = randomIntBetween(0, 10); CircuitBreakerStats[] circuitBreakerStatsArray = new CircuitBreakerStats[numCircuitBreakerStats]; for (int i = 0; i < numCircuitBreakerStats; i++) { - circuitBreakerStatsArray[i] = new CircuitBreakerStats(randomAsciiOfLengthBetween(3, 10), randomPositiveLong(), - randomPositiveLong(), randomDouble(), randomPositiveLong()); + circuitBreakerStatsArray[i] = new CircuitBreakerStats(randomAsciiOfLengthBetween(3, 10), randomNonNegativeLong(), + randomNonNegativeLong(), randomDouble(), randomNonNegativeLong()); } allCircuitBreakerStats = new AllCircuitBreakerStats(circuitBreakerStatsArray); } - ScriptStats scriptStats = frequently() ? new ScriptStats(randomPositiveLong(), randomPositiveLong()) : null; + ScriptStats scriptStats = frequently() ? new ScriptStats(randomNonNegativeLong(), randomNonNegativeLong()) : null; DiscoveryStats discoveryStats = frequently() ? new DiscoveryStats(randomBoolean() ? new PendingClusterStateStats(randomInt(), randomInt(), randomInt()) : null) : null; IngestStats ingestStats = null; if (frequently()) { - IngestStats.Stats totalStats = new IngestStats.Stats(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong()); + IngestStats.Stats totalStats = new IngestStats.Stats(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong()); int numStatsPerPipeline = randomIntBetween(0, 10); Map statsPerPipeline = new HashMap<>(); for (int i = 0; i < numStatsPerPipeline; i++) { - statsPerPipeline.put(randomAsciiOfLengthBetween(3, 10), new IngestStats.Stats(randomPositiveLong(), - randomPositiveLong(), randomPositiveLong(), randomPositiveLong())); + statsPerPipeline.put(randomAsciiOfLengthBetween(3, 10), new IngestStats.Stats(randomNonNegativeLong(), + randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong())); } ingestStats = new IngestStats(totalStats, statsPerPipeline); } //TODO NodeIndicesStats are not tested here, way too complicated to create, also they need to be migrated to Writeable yet - return new NodeStats(node, randomPositiveLong(), null, osStats, processStats, jvmStats, threadPoolStats, fsInfo, + return new NodeStats(node, randomNonNegativeLong(), null, osStats, processStats, jvmStats, threadPoolStats, fsInfo, transportStats, httpStats, allCircuitBreakerStats, scriptStats, discoveryStats, ingestStats); } } diff --git a/core/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java b/core/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java index be95f8dc581b9..892401356c34e 100644 --- a/core/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java +++ b/core/src/test/java/org/elasticsearch/action/update/UpdateRequestTests.java @@ -292,7 +292,7 @@ public void testNowInScript() throws IOException { .upsert(indexRequest) .script(new Script(ScriptType.INLINE, "mock", "ctx._source.update_timestamp = ctx._now", Collections.emptyMap())) .scriptedUpsert(true); - long nowInMillis = randomPositiveLong(); + long nowInMillis = randomNonNegativeLong(); // We simulate that the document is not existing yet GetResult getResult = new GetResult("test", "type1", "2", 0, false, null, null); UpdateHelper.Result result = updateHelper.prepare(new ShardId("test", "_na_", 0), updateRequest, getResult, () -> nowInMillis); diff --git a/core/src/test/java/org/elasticsearch/common/FieldMemoryStatsTests.java b/core/src/test/java/org/elasticsearch/common/FieldMemoryStatsTests.java index 74427281894e9..6369ce2d6ea46 100644 --- a/core/src/test/java/org/elasticsearch/common/FieldMemoryStatsTests.java +++ b/core/src/test/java/org/elasticsearch/common/FieldMemoryStatsTests.java @@ -95,7 +95,7 @@ public static FieldMemoryStats randomFieldMemoryStats() { ObjectLongHashMap map = new ObjectLongHashMap<>(); int keys = randomIntBetween(1, 1000); for (int i = 0; i < keys; i++) { - map.put(randomRealisticUnicodeOfCodepointLengthBetween(1, 10), randomPositiveLong()); + map.put(randomRealisticUnicodeOfCodepointLengthBetween(1, 10), randomNonNegativeLong()); } return new FieldMemoryStats(map); } diff --git a/core/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java b/core/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java index 6ad5a1ce32a34..9d0a3e67e9d61 100644 --- a/core/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java +++ b/core/src/test/java/org/elasticsearch/common/unit/ByteSizeValueTests.java @@ -150,7 +150,7 @@ public void testNoDotsAllowed() { } public void testCompareEquality() { - long firstRandom = randomPositiveLong(); + long firstRandom = randomNonNegativeLong(); ByteSizeUnit randomUnit = randomFrom(ByteSizeUnit.values()); ByteSizeValue firstByteValue = new ByteSizeValue(firstRandom, randomUnit); ByteSizeValue secondByteValue = new ByteSizeValue(firstRandom, randomUnit); @@ -158,8 +158,8 @@ public void testCompareEquality() { } public void testCompareValue() { - long firstRandom = randomPositiveLong(); - long secondRandom = randomValueOtherThan(firstRandom, ESTestCase::randomPositiveLong); + long firstRandom = randomNonNegativeLong(); + long secondRandom = randomValueOtherThan(firstRandom, ESTestCase::randomNonNegativeLong); ByteSizeUnit unit = randomFrom(ByteSizeUnit.values()); ByteSizeValue firstByteValue = new ByteSizeValue(firstRandom, unit); ByteSizeValue secondByteValue = new ByteSizeValue(secondRandom, unit); @@ -168,7 +168,7 @@ public void testCompareValue() { } public void testCompareUnits() { - long number = randomPositiveLong(); + long number = randomNonNegativeLong(); ByteSizeUnit randomUnit = randomValueOtherThan(ByteSizeUnit.PB, ()->randomFrom(ByteSizeUnit.values())); ByteSizeValue firstByteValue = new ByteSizeValue(number, randomUnit); ByteSizeValue secondByteValue = new ByteSizeValue(number, ByteSizeUnit.PB); @@ -189,7 +189,7 @@ public void testConversionHashCode() { } public void testSerialization() throws IOException { - ByteSizeValue byteSizeValue = new ByteSizeValue(randomPositiveLong(), randomFrom(ByteSizeUnit.values())); + ByteSizeValue byteSizeValue = new ByteSizeValue(randomNonNegativeLong(), randomFrom(ByteSizeUnit.values())); try (BytesStreamOutput out = new BytesStreamOutput()) { byteSizeValue.writeTo(out); try (StreamInput in = out.bytes().streamInput()) { diff --git a/core/src/test/java/org/elasticsearch/common/unit/SizeValueTests.java b/core/src/test/java/org/elasticsearch/common/unit/SizeValueTests.java index 3a97a11308bbb..4fd2af3ce54f3 100644 --- a/core/src/test/java/org/elasticsearch/common/unit/SizeValueTests.java +++ b/core/src/test/java/org/elasticsearch/common/unit/SizeValueTests.java @@ -66,7 +66,7 @@ public void testThatNegativeValuesThrowException() { } public void testCompareEquality() { - long randomValue = randomPositiveLong(); + long randomValue = randomNonNegativeLong(); SizeUnit randomUnit = randomFrom(SizeUnit.values()); SizeValue firstValue = new SizeValue(randomValue, randomUnit); SizeValue secondValue = new SizeValue(randomValue, randomUnit); @@ -74,8 +74,8 @@ public void testCompareEquality() { } public void testCompareValue() { - long firstRandom = randomPositiveLong(); - long secondRandom = randomValueOtherThan(firstRandom, ESTestCase::randomPositiveLong); + long firstRandom = randomNonNegativeLong(); + long secondRandom = randomValueOtherThan(firstRandom, ESTestCase::randomNonNegativeLong); SizeUnit unit = randomFrom(SizeUnit.values()); SizeValue firstSizeValue = new SizeValue(firstRandom, unit); SizeValue secondSizeValue = new SizeValue(secondRandom, unit); @@ -84,7 +84,7 @@ public void testCompareValue() { } public void testCompareUnits() { - long number = randomPositiveLong(); + long number = randomNonNegativeLong(); SizeUnit randomUnit = randomValueOtherThan(SizeUnit.PETA, ()->randomFrom(SizeUnit.values())); SizeValue firstValue = new SizeValue(number, randomUnit); SizeValue secondValue = new SizeValue(number, SizeUnit.PETA); diff --git a/core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java b/core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java index 1f7e876f8568a..cbfd98aa3b74a 100644 --- a/core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java +++ b/core/src/test/java/org/elasticsearch/common/unit/TimeValueTests.java @@ -226,7 +226,7 @@ public void testToStringRep() { } public void testCompareEquality() { - long randomLong = randomPositiveLong(); + long randomLong = randomNonNegativeLong(); TimeUnit randomUnit = randomFrom(TimeUnit.values()); TimeValue firstValue = new TimeValue(randomLong, randomUnit); TimeValue secondValue = new TimeValue(randomLong, randomUnit); @@ -234,8 +234,8 @@ public void testCompareEquality() { } public void testCompareValue() { - long firstRandom = randomPositiveLong(); - long secondRandom = randomValueOtherThan(firstRandom, ESTestCase::randomPositiveLong); + long firstRandom = randomNonNegativeLong(); + long secondRandom = randomValueOtherThan(firstRandom, ESTestCase::randomNonNegativeLong); TimeUnit unit = randomFrom(TimeUnit.values()); TimeValue firstValue = new TimeValue(firstRandom, unit); TimeValue secondValue = new TimeValue(secondRandom, unit); @@ -244,7 +244,7 @@ public void testCompareValue() { } public void testCompareUnits() { - long number = randomPositiveLong(); + long number = randomNonNegativeLong(); TimeUnit randomUnit = randomValueOtherThan(TimeUnit.DAYS, ()->randomFrom(TimeUnit.values())); TimeValue firstValue = new TimeValue(number, randomUnit); TimeValue secondValue = new TimeValue(number, TimeUnit.DAYS); diff --git a/core/src/test/java/org/elasticsearch/discovery/zen/ElectMasterServiceTests.java b/core/src/test/java/org/elasticsearch/discovery/zen/ElectMasterServiceTests.java index 0c4862bf32a2b..f59cc1cc6fb0f 100644 --- a/core/src/test/java/org/elasticsearch/discovery/zen/ElectMasterServiceTests.java +++ b/core/src/test/java/org/elasticsearch/discovery/zen/ElectMasterServiceTests.java @@ -66,7 +66,8 @@ List generateRandomCandidates() { roles.add(DiscoveryNode.Role.MASTER); DiscoveryNode node = new DiscoveryNode("n_" + i, "n_" + i, buildNewFakeTransportAddress(), Collections.emptyMap(), roles, Version.CURRENT); - candidates.add(new MasterCandidate(node, randomBoolean() ? MasterCandidate.UNRECOVERED_CLUSTER_VERSION : randomPositiveLong())); + candidates.add( + new MasterCandidate(node, randomBoolean() ? MasterCandidate.UNRECOVERED_CLUSTER_VERSION : randomNonNegativeLong())); } Collections.shuffle(candidates, random()); diff --git a/core/src/test/java/org/elasticsearch/discovery/zen/UnicastZenPingTests.java b/core/src/test/java/org/elasticsearch/discovery/zen/UnicastZenPingTests.java index de8d1a562e803..75892af904e30 100644 --- a/core/src/test/java/org/elasticsearch/discovery/zen/UnicastZenPingTests.java +++ b/core/src/test/java/org/elasticsearch/discovery/zen/UnicastZenPingTests.java @@ -154,7 +154,7 @@ public void testSimplePings() throws IOException, InterruptedException { NetworkHandle handleD = startServices(settingsMismatch, threadPool, "UZP_D", versionD, supplier); closeables.push(handleD.transportService); - final ClusterState state = ClusterState.builder(new ClusterName("test")).version(randomPositiveLong()).build(); + final ClusterState state = ClusterState.builder(new ClusterName("test")).version(randomNonNegativeLong()).build(); Settings hostsSettings = Settings.builder() .putArray("discovery.zen.ping.unicast.hosts", @@ -304,7 +304,7 @@ public TransportAddress[] addressesFromString(String address, int perAddressLimi .put("cluster.name", "test") .build(); - final ClusterState state = ClusterState.builder(new ClusterName("test")).version(randomPositiveLong()).build(); + final ClusterState state = ClusterState.builder(new ClusterName("test")).version(randomNonNegativeLong()).build(); final UnicastZenPing zenPingA = new UnicastZenPing(hostsSettings, threadPool, handleA.transportService, EMPTY_HOSTS_PROVIDER); zenPingA.start(new PingContextProvider() { diff --git a/core/src/test/java/org/elasticsearch/fieldstats/FieldStatsTests.java b/core/src/test/java/org/elasticsearch/fieldstats/FieldStatsTests.java index b58502bffe950..4d1a3ec420a46 100644 --- a/core/src/test/java/org/elasticsearch/fieldstats/FieldStatsTests.java +++ b/core/src/test/java/org/elasticsearch/fieldstats/FieldStatsTests.java @@ -619,54 +619,54 @@ private FieldStats randomFieldStats(boolean withNullMinMax) throws UnknownHostEx switch (type) { case 0: if (withNullMinMax && randomBoolean()) { - return new FieldStats.Long(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong(), randomBoolean(), randomBoolean()); + return new FieldStats.Long(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong(), randomBoolean(), randomBoolean()); } else { - return new FieldStats.Long(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong(), randomBoolean(), randomBoolean(), randomLong(), randomLong()); + return new FieldStats.Long(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong(), randomBoolean(), randomBoolean(), randomLong(), randomLong()); } case 1: if (withNullMinMax && randomBoolean()) { - return new FieldStats.Double(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong(), randomBoolean(), randomBoolean()); + return new FieldStats.Double(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong(), randomBoolean(), randomBoolean()); } else { - return new FieldStats.Double(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong(), randomBoolean(), randomBoolean(), randomDouble(), randomDouble()); + return new FieldStats.Double(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong(), randomBoolean(), randomBoolean(), randomDouble(), randomDouble()); } case 2: if (withNullMinMax && randomBoolean()) { - return new FieldStats.Date(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong(), randomBoolean(), randomBoolean()); + return new FieldStats.Date(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong(), randomBoolean(), randomBoolean()); } else { - return new FieldStats.Date(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong(), randomBoolean(), randomBoolean(), Joda.forPattern("basicDate"), + return new FieldStats.Date(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong(), randomBoolean(), randomBoolean(), Joda.forPattern("basicDate"), new Date().getTime(), new Date().getTime()); } case 3: if (withNullMinMax && randomBoolean()) { - return new FieldStats.Text(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong(), randomBoolean(), randomBoolean()); + return new FieldStats.Text(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong(), randomBoolean(), randomBoolean()); } else { - return new FieldStats.Text(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong(), randomBoolean(), randomBoolean(), + return new FieldStats.Text(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong(), randomBoolean(), randomBoolean(), new BytesRef(randomAsciiOfLength(10)), new BytesRef(randomAsciiOfLength(20))); } case 4: if (withNullMinMax && randomBoolean()) { - return new FieldStats.Ip(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong(), randomBoolean(), randomBoolean()); + return new FieldStats.Ip(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong(), randomBoolean(), randomBoolean()); } else { - return new FieldStats.Ip(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong(), randomBoolean(), randomBoolean(), + return new FieldStats.Ip(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong(), randomBoolean(), randomBoolean(), InetAddress.getByName("::1"), InetAddress.getByName("::1")); } case 5: if (withNullMinMax && randomBoolean()) { - return new FieldStats.Ip(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong(), randomBoolean(), randomBoolean()); + return new FieldStats.Ip(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong(), randomBoolean(), randomBoolean()); } else { - return new FieldStats.Ip(randomPositiveLong(), randomPositiveLong(), randomPositiveLong(), - randomPositiveLong(), randomBoolean(), randomBoolean(), + return new FieldStats.Ip(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong(), + randomNonNegativeLong(), randomBoolean(), randomBoolean(), InetAddress.getByName("1.2.3.4"), InetAddress.getByName("1.2.3.4")); } default: diff --git a/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java b/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java index 76e1b37e4dc5b..065a6d74f07c3 100644 --- a/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java +++ b/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java @@ -157,6 +157,7 @@ import java.util.function.Supplier; import static java.util.Collections.emptyMap; +import static java.util.Collections.max; import static org.elasticsearch.index.engine.Engine.Operation.Origin.LOCAL_TRANSLOG_RECOVERY; import static org.elasticsearch.index.engine.Engine.Operation.Origin.PEER_RECOVERY; import static org.elasticsearch.index.engine.Engine.Operation.Origin.PRIMARY; @@ -3123,6 +3124,50 @@ public void testOutOfOrderSequenceNumbersWithVersionConflict() throws IOExceptio } } + /* + * This test tests that a no-op does not generate a new sequence number, that no-ops can advance the local checkpoint, and that no-ops + * are correctly added to the translog. + */ + public void testNoOps() throws IOException { + engine.close(); + InternalEngine noOpEngine = null; + final int maxSeqNo = randomIntBetween(0, 128); + final int localCheckpoint = randomIntBetween(0, maxSeqNo); + final int globalCheckpoint = randomIntBetween(0, localCheckpoint); + try { + final SequenceNumbersService seqNoService = + new SequenceNumbersService(shardId, defaultSettings, maxSeqNo, localCheckpoint, globalCheckpoint) { + @Override + public long generateSeqNo() { + throw new UnsupportedOperationException(); + } + }; + noOpEngine = createEngine(defaultSettings, store, primaryTranslogDir, newMergePolicy(), null, () -> seqNoService); + final long primaryTerm = randomNonNegativeLong(); + final String reason = randomAsciiOfLength(16); + noOpEngine.noOp( + new Engine.NoOp( + null, + maxSeqNo + 1, + primaryTerm, + 0, + VersionType.INTERNAL, + randomFrom(PRIMARY, REPLICA, PEER_RECOVERY, LOCAL_TRANSLOG_RECOVERY), + System.nanoTime(), + reason)); + assertThat(noOpEngine.seqNoService().getLocalCheckpoint(), equalTo((long) (maxSeqNo + 1))); + assertThat(noOpEngine.getTranslog().totalOperations(), equalTo(1)); + final Translog.Operation op = noOpEngine.getTranslog().newSnapshot().next(); + assertThat(op, instanceOf(Translog.NoOp.class)); + final Translog.NoOp noOp = (Translog.NoOp) op; + assertThat(noOp.seqNo(), equalTo((long) (maxSeqNo + 1))); + assertThat(noOp.primaryTerm(), equalTo(primaryTerm)); + assertThat(noOp.reason(), equalTo(reason)); + } finally { + IOUtils.close(noOpEngine); + } + } + /** * Return a tuple representing the sequence ID for the given {@code Get} * operation. The first value in the tuple is the sequence number, the diff --git a/core/src/test/java/org/elasticsearch/index/fielddata/FieldDataStatsTests.java b/core/src/test/java/org/elasticsearch/index/fielddata/FieldDataStatsTests.java index 54881bad88485..90995bec34d34 100644 --- a/core/src/test/java/org/elasticsearch/index/fielddata/FieldDataStatsTests.java +++ b/core/src/test/java/org/elasticsearch/index/fielddata/FieldDataStatsTests.java @@ -30,7 +30,7 @@ public class FieldDataStatsTests extends ESTestCase { public void testSerialize() throws IOException { FieldMemoryStats map = randomBoolean() ? null : FieldMemoryStatsTests.randomFieldMemoryStats(); - FieldDataStats stats = new FieldDataStats(randomPositiveLong(), randomPositiveLong(), map == null ? null : + FieldDataStats stats = new FieldDataStats(randomNonNegativeLong(), randomNonNegativeLong(), map == null ? null : map); BytesStreamOutput out = new BytesStreamOutput(); stats.writeTo(out); diff --git a/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java b/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java index d82c42214043b..a40644ea2cbee 100644 --- a/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java +++ b/core/src/test/java/org/elasticsearch/index/get/GetResultTests.java @@ -96,7 +96,7 @@ public static GetResult mutateGetResult(GetResult getResult) { getResult.isExists(), getResult.internalSourceRef(), getResult.getFields())); mutations.add(() -> new GetResult(getResult.getIndex(), getResult.getType(), randomUnicodeOfLength(15), getResult.getVersion(), getResult.isExists(), getResult.internalSourceRef(), getResult.getFields())); - mutations.add(() -> new GetResult(getResult.getIndex(), getResult.getType(), getResult.getId(), randomPositiveLong(), + mutations.add(() -> new GetResult(getResult.getIndex(), getResult.getType(), getResult.getId(), randomNonNegativeLong(), getResult.isExists(), getResult.internalSourceRef(), getResult.getFields())); mutations.add(() -> new GetResult(getResult.getIndex(), getResult.getType(), getResult.getId(), getResult.getVersion(), getResult.isExists() == false, getResult.internalSourceRef(), getResult.getFields())); @@ -117,7 +117,7 @@ public static Tuple randomGetResult(XContentType xContentT Map fields = null; Map expectedFields = null; if (frequently()) { - version = randomPositiveLong(); + version = randomNonNegativeLong(); exists = true; if (frequently()) { source = RandomObjects.randomSource(random()); diff --git a/core/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java index a93cf0153cf36..ee51730f0a91c 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java @@ -67,7 +67,7 @@ public void modify(MappedFieldType ft) { ((DateFieldType) ft).setDateTimeFormatter(Joda.forPattern("date_optional_time", Locale.CANADA)); } }); - nowInMillis = randomPositiveLong(); + nowInMillis = randomNonNegativeLong(); } public void testIsFieldWithinQueryEmptyReader() throws IOException { diff --git a/core/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java index da82234990935..a9f2c49f64d69 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java @@ -48,7 +48,7 @@ public class RangeFieldTypeTests extends FieldTypeTestCase { @Before public void setupProperties() { type = RandomPicks.randomFrom(random(), RangeType.values()); - nowInMillis = randomPositiveLong(); + nowInMillis = randomNonNegativeLong(); if (type == RangeType.DATE) { addModifier(new Modifier("format", true) { @Override diff --git a/core/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java b/core/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java index ccf514dd41c04..6dec150187d3e 100644 --- a/core/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java @@ -46,7 +46,7 @@ public void testFailIfFieldMappingNotFound() { IndexSettings indexSettings = new IndexSettings(indexMetadata.build(), Settings.EMPTY); MapperService mapperService = mock(MapperService.class); when(mapperService.getIndexSettings()).thenReturn(indexSettings); - final long nowInMillis = randomPositiveLong(); + final long nowInMillis = randomNonNegativeLong(); QueryShardContext context = new QueryShardContext( 0, indexSettings, null, null, mapperService, null, null, xContentRegistry(), null, null, null, () -> nowInMillis); diff --git a/core/src/test/java/org/elasticsearch/index/seqno/GlobalCheckpointSyncActionTests.java b/core/src/test/java/org/elasticsearch/index/seqno/GlobalCheckpointSyncActionTests.java index 510371c2d51b8..3f8b3a1d9e4d3 100644 --- a/core/src/test/java/org/elasticsearch/index/seqno/GlobalCheckpointSyncActionTests.java +++ b/core/src/test/java/org/elasticsearch/index/seqno/GlobalCheckpointSyncActionTests.java @@ -36,10 +36,7 @@ import org.elasticsearch.transport.Transport; import org.elasticsearch.transport.TransportService; -import java.io.IOException; -import java.io.UncheckedIOException; import java.util.Collections; -import java.util.HashSet; import static org.elasticsearch.mock.orig.Mockito.when; import static org.elasticsearch.test.ClusterServiceUtils.createClusterService; @@ -103,7 +100,8 @@ public void testTranslogSyncAfterGlobalCheckpointSync() throws Exception { if (randomBoolean()) { action.shardOperationOnPrimary(primaryRequest, indexShard); } else { - action.shardOperationOnReplica(new GlobalCheckpointSyncAction.ReplicaRequest(primaryRequest, randomPositiveLong()), indexShard); + action.shardOperationOnReplica( + new GlobalCheckpointSyncAction.ReplicaRequest(primaryRequest, randomNonNegativeLong()), indexShard); } verify(translog).sync(); diff --git a/core/src/test/java/org/elasticsearch/index/suggest/stats/CompletionsStatsTests.java b/core/src/test/java/org/elasticsearch/index/suggest/stats/CompletionsStatsTests.java index 71ffffdcbff6e..2d699b2c4571c 100644 --- a/core/src/test/java/org/elasticsearch/index/suggest/stats/CompletionsStatsTests.java +++ b/core/src/test/java/org/elasticsearch/index/suggest/stats/CompletionsStatsTests.java @@ -31,7 +31,7 @@ public class CompletionsStatsTests extends ESTestCase { public void testSerialize() throws IOException { FieldMemoryStats map = randomBoolean() ? null : FieldMemoryStatsTests.randomFieldMemoryStats(); - CompletionStats stats = new CompletionStats(randomPositiveLong(), map == null ? null : + CompletionStats stats = new CompletionStats(randomNonNegativeLong(), map == null ? null : map); BytesStreamOutput out = new BytesStreamOutput(); stats.writeTo(out); diff --git a/core/src/test/java/org/elasticsearch/index/translog/TranslogTests.java b/core/src/test/java/org/elasticsearch/index/translog/TranslogTests.java index 41118d6efa2e2..092bd8ee54b53 100644 --- a/core/src/test/java/org/elasticsearch/index/translog/TranslogTests.java +++ b/core/src/test/java/org/elasticsearch/index/translog/TranslogTests.java @@ -38,6 +38,7 @@ import org.elasticsearch.common.io.FileSystemUtils; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.StreamInput; +import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.ByteSizeUnit; import org.elasticsearch.common.unit.ByteSizeValue; @@ -53,6 +54,7 @@ import org.elasticsearch.index.translog.Translog.Location; import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.IndexSettingsModule; +import org.elasticsearch.test.rest.yaml.section.Assertion; import org.hamcrest.Matchers; import org.junit.After; import org.junit.Before; @@ -71,6 +73,7 @@ import java.util.Collection; import java.util.Collections; import java.util.HashSet; +import java.util.IllegalFormatCodePointException; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -88,9 +91,12 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.hasToString; +import static org.hamcrest.Matchers.typeCompatibleWith; @LuceneTestCase.SuppressFileSystems("ExtrasFS") public class TranslogTests extends ESTestCase { @@ -226,17 +232,28 @@ public void testSimpleOperations() throws IOException { assertThat(snapshot, SnapshotMatchers.equalsTo(ops)); assertThat(snapshot.totalOperations(), equalTo(ops.size())); + final long seqNo = randomNonNegativeLong(); + final long primaryTerm = randomNonNegativeLong(); + final String reason = randomAsciiOfLength(16); + addToTranslogAndList(translog, ops, new Translog.NoOp(seqNo, primaryTerm, reason)); + snapshot = translog.newSnapshot(); Translog.Index index = (Translog.Index) snapshot.next(); - assertThat(index != null, equalTo(true)); + assertNotNull(index); assertThat(BytesReference.toBytes(index.source()), equalTo(new byte[]{1})); Translog.Delete delete = (Translog.Delete) snapshot.next(); - assertThat(delete != null, equalTo(true)); + assertNotNull(delete); assertThat(delete.uid(), equalTo(newUid("2"))); - assertThat(snapshot.next(), equalTo(null)); + Translog.NoOp noOp = (Translog.NoOp) snapshot.next(); + assertNotNull(noOp); + assertThat(noOp.seqNo(), equalTo(seqNo)); + assertThat(noOp.primaryTerm(), equalTo(primaryTerm)); + assertThat(noOp.reason(), equalTo(reason)); + + assertNull(snapshot.next()); long firstId = translog.currentFileGeneration(); translog.prepareCommit(); @@ -268,70 +285,90 @@ protected TranslogStats stats() throws IOException { public void testStats() throws IOException { final long firstOperationPosition = translog.getFirstOperationPosition(); - TranslogStats stats = stats(); - assertThat(stats.estimatedNumberOfOperations(), equalTo(0L)); - long lastSize = stats.getTranslogSizeInBytes(); + { + final TranslogStats stats = stats(); + assertThat(stats.estimatedNumberOfOperations(), equalTo(0L)); + } assertThat((int) firstOperationPosition, greaterThan(CodecUtil.headerLength(TranslogWriter.TRANSLOG_CODEC))); - assertThat(lastSize, equalTo(firstOperationPosition)); - TranslogStats total = new TranslogStats(); translog.add(new Translog.Index("test", "1", new byte[]{1})); - stats = stats(); - total.add(stats); - assertThat(stats.estimatedNumberOfOperations(), equalTo(1L)); - assertThat(stats.getTranslogSizeInBytes(), greaterThan(lastSize)); - lastSize = stats.getTranslogSizeInBytes(); + + { + final TranslogStats stats = stats(); + assertThat(stats.estimatedNumberOfOperations(), equalTo(1L)); + assertThat(stats.getTranslogSizeInBytes(), equalTo(83L)); + } translog.add(new Translog.Delete(newUid("2"))); - stats = stats(); - total.add(stats); - assertThat(stats.estimatedNumberOfOperations(), equalTo(2L)); - assertThat(stats.getTranslogSizeInBytes(), greaterThan(lastSize)); - lastSize = stats.getTranslogSizeInBytes(); + { + final TranslogStats stats = stats(); + assertThat(stats.estimatedNumberOfOperations(), equalTo(2L)); + assertThat(stats.getTranslogSizeInBytes(), equalTo(111L)); + } translog.add(new Translog.Delete(newUid("3"))); - translog.prepareCommit(); - stats = stats(); - total.add(stats); - assertThat(stats.estimatedNumberOfOperations(), equalTo(3L)); - assertThat(stats.getTranslogSizeInBytes(), greaterThan(lastSize)); + { + final TranslogStats stats = stats(); + assertThat(stats.estimatedNumberOfOperations(), equalTo(3L)); + assertThat(stats.getTranslogSizeInBytes(), equalTo(139L)); + } - translog.commit(); - stats = stats(); - total.add(stats); - assertThat(stats.estimatedNumberOfOperations(), equalTo(0L)); - assertThat(stats.getTranslogSizeInBytes(), equalTo(firstOperationPosition)); - assertEquals(6, total.estimatedNumberOfOperations()); - assertEquals(419, total.getTranslogSizeInBytes()); + final long seqNo = 1; + final long primaryTerm = 1; + translog.add(new Translog.NoOp(seqNo, primaryTerm, randomAsciiOfLength(16))); + { + final TranslogStats stats = stats(); + assertThat(stats.estimatedNumberOfOperations(), equalTo(4L)); + assertThat( + stats.getTranslogSizeInBytes(), + equalTo(165L + StreamOutput.lengthVLong(seqNo) + StreamOutput.lengthVLong(primaryTerm))); + } - BytesStreamOutput out = new BytesStreamOutput(); - total.writeTo(out); - TranslogStats copy = new TranslogStats(); - copy.readFrom(out.bytes().streamInput()); + final long expectedSizeInBytes = 208L + StreamOutput.lengthVLong(seqNo) + StreamOutput.lengthVLong(primaryTerm); + translog.prepareCommit(); + { + final TranslogStats stats = stats(); + assertThat(stats.estimatedNumberOfOperations(), equalTo(4L)); + assertThat( + stats.getTranslogSizeInBytes(), + equalTo(expectedSizeInBytes)); + } + + { + final TranslogStats stats = stats(); + final BytesStreamOutput out = new BytesStreamOutput(); + stats.writeTo(out); + final TranslogStats copy = new TranslogStats(); + copy.readFrom(out.bytes().streamInput()); - assertEquals(6, copy.estimatedNumberOfOperations()); - assertEquals(419, copy.getTranslogSizeInBytes()); + assertThat(copy.estimatedNumberOfOperations(), equalTo(4L)); + assertThat(copy.getTranslogSizeInBytes(), equalTo(expectedSizeInBytes)); - try (XContentBuilder builder = XContentFactory.jsonBuilder()) { - builder.startObject(); - copy.toXContent(builder, ToXContent.EMPTY_PARAMS); - builder.endObject(); - assertEquals("{\"translog\":{\"operations\":6,\"size_in_bytes\":419}}", builder.string()); + try (final XContentBuilder builder = XContentFactory.jsonBuilder()) { + builder.startObject(); + copy.toXContent(builder, ToXContent.EMPTY_PARAMS); + builder.endObject(); + assertThat(builder.string(), equalTo("{\"translog\":{\"operations\":4,\"size_in_bytes\":" + expectedSizeInBytes + "}}")); + } } - try { - new TranslogStats(1, -1); - fail("must be positive"); - } catch (IllegalArgumentException ex) { - //all well - } - try { - new TranslogStats(-1, 1); - fail("must be positive"); - } catch (IllegalArgumentException ex) { - //all well + translog.commit(); + { + final TranslogStats stats = stats(); + assertThat(stats.estimatedNumberOfOperations(), equalTo(0L)); + assertThat(stats.getTranslogSizeInBytes(), equalTo(firstOperationPosition)); } } + public void testNegativeNumberOfOperations() { + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new TranslogStats(-1, 1)); + assertThat(e, hasToString(containsString("numberOfOperations must be >= 0"))); + } + + public void testNegativeSizeInBytes() { + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new TranslogStats(1, -1)); + assertThat(e, hasToString(containsString("translogSizeInBytes must be >= 0"))); + } + public void testSnapshot() throws IOException { ArrayList ops = new ArrayList<>(); Translog.Snapshot snapshot = translog.newSnapshot(); @@ -465,8 +502,15 @@ public void testConcurrentWritesWithVaryingSize() throws Throwable { assertEquals(expDelOp.version(), delOp.version()); assertEquals(expDelOp.versionType(), delOp.versionType()); break; + case NO_OP: + final Translog.NoOp noOp = (Translog.NoOp) op; + final Translog.NoOp expectedNoOp = (Translog.NoOp) expectedOp; + assertThat(noOp.seqNo(), equalTo(expectedNoOp.seqNo())); + assertThat(noOp.primaryTerm(), equalTo(expectedNoOp.primaryTerm())); + assertThat(noOp.reason(), equalTo(expectedNoOp.reason())); + break; default: - throw new ElasticsearchException("unsupported opType"); + throw new AssertionError("unsupported operation type [" + op.opType() + "]"); } } @@ -607,7 +651,9 @@ public void doRun() throws BrokenBarrierException, InterruptedException, IOExcep while (run.get()) { long id = idGenerator.incrementAndGet(); final Translog.Operation op; - switch (Translog.Operation.Type.values()[((int) (id % Translog.Operation.Type.values().length))]) { + final Translog.Operation.Type type = + Translog.Operation.Type.values()[((int) (id % Translog.Operation.Type.values().length))]; + switch (type) { case CREATE: case INDEX: op = new Translog.Index("type", "" + id, new byte[]{(byte) id}); @@ -615,8 +661,11 @@ public void doRun() throws BrokenBarrierException, InterruptedException, IOExcep case DELETE: op = new Translog.Delete(newUid("" + id)); break; + case NO_OP: + op = new Translog.NoOp(id, id, Long.toString(id)); + break; default: - throw new ElasticsearchException("unknown type"); + throw new AssertionError("unsupported operation type [" + type + "]"); } Translog.Location location = translog.add(op); Translog.Location existing = writtenOps.put(op, location); @@ -1293,7 +1342,8 @@ public void run() { downLatch.await(); for (int opCount = 0; opCount < opsPerThread; opCount++) { Translog.Operation op; - switch (randomFrom(Translog.Operation.Type.values())) { + final Translog.Operation.Type type = randomFrom(Translog.Operation.Type.values()); + switch (type) { case CREATE: case INDEX: op = new Translog.Index("test", threadId + "_" + opCount, @@ -1307,8 +1357,11 @@ public void run() { 1 + randomInt(100000), randomFrom(VersionType.values())); break; + case NO_OP: + op = new Translog.NoOp(randomNonNegativeLong(), randomNonNegativeLong(), randomAsciiOfLength(16)); + break; default: - throw new ElasticsearchException("not supported op type"); + throw new AssertionError("unsupported operation type [" + type + "]"); } Translog.Location loc = add(op); diff --git a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java index 9bd4ab3b90f33..5d817b6321d5b 100644 --- a/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java +++ b/core/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java @@ -41,7 +41,7 @@ public class OsProbeTests extends ESTestCase { public void testOsInfo() { int allocatedProcessors = randomIntBetween(1, Runtime.getRuntime().availableProcessors()); - long refreshInterval = randomBoolean() ? -1 : randomPositiveLong(); + long refreshInterval = randomBoolean() ? -1 : randomNonNegativeLong(); OsInfo info = probe.osInfo(refreshInterval, allocatedProcessors); assertNotNull(info); assertEquals(refreshInterval, info.getRefreshInterval()); diff --git a/core/src/test/java/org/elasticsearch/monitor/os/OsStatsTests.java b/core/src/test/java/org/elasticsearch/monitor/os/OsStatsTests.java index 8334f71e86a67..9f8b60a55a9d7 100644 --- a/core/src/test/java/org/elasticsearch/monitor/os/OsStatsTests.java +++ b/core/src/test/java/org/elasticsearch/monitor/os/OsStatsTests.java @@ -38,11 +38,11 @@ public void testSerialization() throws IOException { OsStats.Swap swap = new OsStats.Swap(randomLong(), randomLong()); OsStats.Cgroup cgroup = new OsStats.Cgroup( randomAsciiOfLength(8), - randomPositiveLong(), + randomNonNegativeLong(), randomAsciiOfLength(8), - randomPositiveLong(), - randomPositiveLong(), - new OsStats.Cgroup.CpuStat(randomPositiveLong(), randomPositiveLong(), randomPositiveLong())); + randomNonNegativeLong(), + randomNonNegativeLong(), + new OsStats.Cgroup.CpuStat(randomNonNegativeLong(), randomNonNegativeLong(), randomNonNegativeLong())); OsStats osStats = new OsStats(System.currentTimeMillis(), cpu, mem, swap, cgroup); try (BytesStreamOutput out = new BytesStreamOutput()) { diff --git a/core/src/test/java/org/elasticsearch/monitor/process/ProcessProbeTests.java b/core/src/test/java/org/elasticsearch/monitor/process/ProcessProbeTests.java index f8fc6a675cfd8..4a44c518051a2 100644 --- a/core/src/test/java/org/elasticsearch/monitor/process/ProcessProbeTests.java +++ b/core/src/test/java/org/elasticsearch/monitor/process/ProcessProbeTests.java @@ -36,7 +36,7 @@ public class ProcessProbeTests extends ESTestCase { private final ProcessProbe probe = ProcessProbe.getInstance(); public void testProcessInfo() { - long refreshInterval = randomPositiveLong(); + long refreshInterval = randomNonNegativeLong(); ProcessInfo info = probe.processInfo(refreshInterval); assertNotNull(info); assertEquals(refreshInterval, info.getRefreshInterval()); diff --git a/core/src/test/java/org/elasticsearch/nodesinfo/NodeInfoStreamingTests.java b/core/src/test/java/org/elasticsearch/nodesinfo/NodeInfoStreamingTests.java index 59077367904bf..9c36a7a649ad6 100644 --- a/core/src/test/java/org/elasticsearch/nodesinfo/NodeInfoStreamingTests.java +++ b/core/src/test/java/org/elasticsearch/nodesinfo/NodeInfoStreamingTests.java @@ -112,13 +112,13 @@ private static NodeInfo createNodeInfo() { if (randomBoolean()) { int availableProcessors = randomIntBetween(1, 64); int allocatedProcessors = randomIntBetween(1, availableProcessors); - long refreshInterval = randomBoolean() ? -1 : randomPositiveLong(); + long refreshInterval = randomBoolean() ? -1 : randomNonNegativeLong(); String name = randomAsciiOfLengthBetween(3, 10); String arch = randomAsciiOfLengthBetween(3, 10); String version = randomAsciiOfLengthBetween(3, 10); osInfo = new OsInfo(refreshInterval, availableProcessors, allocatedProcessors, name, arch, version); } - ProcessInfo process = randomBoolean() ? null : new ProcessInfo(randomInt(), randomBoolean(), randomPositiveLong()); + ProcessInfo process = randomBoolean() ? null : new ProcessInfo(randomInt(), randomBoolean(), randomNonNegativeLong()); JvmInfo jvm = randomBoolean() ? null : JvmInfo.jvmInfo(); ThreadPoolInfo threadPoolInfo = null; if (randomBoolean()) { diff --git a/core/src/test/java/org/elasticsearch/search/SearchRequestTests.java b/core/src/test/java/org/elasticsearch/search/SearchRequestTests.java index 031cab1286a7a..9e58bf26744d0 100644 --- a/core/src/test/java/org/elasticsearch/search/SearchRequestTests.java +++ b/core/src/test/java/org/elasticsearch/search/SearchRequestTests.java @@ -95,7 +95,7 @@ private SearchRequest mutate(SearchRequest searchRequest) throws IOException { mutators.add(() -> mutation.routing(randomValueOtherThan(searchRequest.routing(), () -> randomAsciiOfLengthBetween(3, 10)))); mutators.add(() -> mutation.requestCache((randomValueOtherThan(searchRequest.requestCache(), () -> randomBoolean())))); mutators.add(() -> mutation - .scroll(randomValueOtherThan(searchRequest.scroll(), () -> new Scroll(new TimeValue(randomPositiveLong() % 100000))))); + .scroll(randomValueOtherThan(searchRequest.scroll(), () -> new Scroll(new TimeValue(randomNonNegativeLong() % 100000))))); mutators.add(() -> mutation.searchType(randomValueOtherThan(searchRequest.searchType(), () -> randomFrom(SearchType.values())))); mutators.add(() -> mutation.source(randomValueOtherThan(searchRequest.source(), this::createSearchSourceBuilder))); randomFrom(mutators).run(); diff --git a/core/src/test/java/org/elasticsearch/search/internal/ShardSearchTransportRequestTests.java b/core/src/test/java/org/elasticsearch/search/internal/ShardSearchTransportRequestTests.java index 764744229612d..930e5173ed3d5 100644 --- a/core/src/test/java/org/elasticsearch/search/internal/ShardSearchTransportRequestTests.java +++ b/core/src/test/java/org/elasticsearch/search/internal/ShardSearchTransportRequestTests.java @@ -198,7 +198,7 @@ public void testSerialize50Request() throws IOException { .putAlias(AliasMetaData.newAliasMetaDataBuilder("UjLlLkjwWh").filter("{\"term\" : {\"foo\" : \"bar1\"}}")) .putAlias(AliasMetaData.newAliasMetaDataBuilder("uBpgtwuqDG").filter("{\"term\" : {\"foo\" : \"bar2\"}}")); IndexSettings indexSettings = new IndexSettings(indexMetadata.build(), Settings.EMPTY); - final long nowInMillis = randomPositiveLong(); + final long nowInMillis = randomNonNegativeLong(); QueryShardContext context = new QueryShardContext( 0, indexSettings, null, null, null, null, null, xContentRegistry(), queriesRegistry, null, null, () -> nowInMillis); readRequest.rewrite(context); diff --git a/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java b/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java index 160e539339100..8c31a9a759806 100644 --- a/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/search/rescore/QueryRescoreBuilderTests.java @@ -133,7 +133,7 @@ public void testFromXContent() throws IOException { * than the test builder */ public void testBuildRescoreSearchContext() throws ElasticsearchParseException, IOException { - final long nowInMillis = randomPositiveLong(); + final long nowInMillis = randomNonNegativeLong(); Settings indexSettings = Settings.builder() .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(randomAsciiOfLengthBetween(1, 10), indexSettings); diff --git a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java index 63e65a9106217..0cda5b07127f9 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java +++ b/core/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java @@ -208,7 +208,7 @@ public void onRemoval(ShardId shardId, Accountable accountable) { public void onCache(ShardId shardId, Accountable accountable) { } }); - long nowInMillis = randomPositiveLong(); + long nowInMillis = randomNonNegativeLong(); return new QueryShardContext(0, idxSettings, bitsetFilterCache, ifds, null, null, scriptService, xContentRegistry(), indicesQueriesRegistry, null, null, () -> nowInMillis) { @Override diff --git a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/RoundTripTests.java b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/RoundTripTests.java index 7c4b4e3274fab..b2e92b15116f2 100644 --- a/modules/reindex/src/test/java/org/elasticsearch/index/reindex/RoundTripTests.java +++ b/modules/reindex/src/test/java/org/elasticsearch/index/reindex/RoundTripTests.java @@ -217,7 +217,7 @@ public void testBulkByTaskStatus() throws IOException { } public void testReindexResponse() throws IOException { - BulkIndexByScrollResponse response = new BulkIndexByScrollResponse(timeValueMillis(randomPositiveLong()), randomStatus(), + BulkIndexByScrollResponse response = new BulkIndexByScrollResponse(timeValueMillis(randomNonNegativeLong()), randomStatus(), randomIndexingFailures(), randomSearchFailures(), randomBoolean()); BulkIndexByScrollResponse tripped = new BulkIndexByScrollResponse(); roundTrip(response, tripped); @@ -225,7 +225,7 @@ public void testReindexResponse() throws IOException { } public void testBulkIndexByScrollResponse() throws IOException { - BulkIndexByScrollResponse response = new BulkIndexByScrollResponse(timeValueMillis(randomPositiveLong()), randomStatus(), + BulkIndexByScrollResponse response = new BulkIndexByScrollResponse(timeValueMillis(randomNonNegativeLong()), randomStatus(), randomIndexingFailures(), randomSearchFailures(), randomBoolean()); BulkIndexByScrollResponse tripped = new BulkIndexByScrollResponse(); roundTrip(response, tripped); diff --git a/plugins/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorFactoryTests.java b/plugins/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorFactoryTests.java index ca0b5964dc202..0c80bcc71fd15 100644 --- a/plugins/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorFactoryTests.java +++ b/plugins/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/GeoIpProcessorFactoryTests.java @@ -60,7 +60,7 @@ public static void loadDatabaseReaders() throws IOException { Files.copy(new ByteArrayInputStream(StreamsUtils.copyToBytesFromClasspath("/GeoLite2-Country.mmdb.gz")), geoIpConfigDir.resolve("GeoLite2-Country.mmdb.gz")); - NodeCache cache = randomFrom(NoCache.getInstance(), new GeoIpCache(randomPositiveLong())); + NodeCache cache = randomFrom(NoCache.getInstance(), new GeoIpCache(randomNonNegativeLong())); databaseReaders = IngestGeoIpPlugin.loadDatabaseReaders(geoIpConfigDir, cache); } diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java index 2263825b6240c..b6cc2ff36d518 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java @@ -1026,7 +1026,7 @@ private static class ServiceHolder implements Closeable { private final BitsetFilterCache bitsetFilterCache; private final ScriptService scriptService; private final Client client; - private final long nowInMillis = randomPositiveLong(); + private final long nowInMillis = randomNonNegativeLong(); ServiceHolder(Settings nodeSettings, Settings indexSettings, Collection> plugins, AbstractQueryTestCase testCase) throws IOException { diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index a169274c172c6..7f64989147bbf 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -441,7 +441,7 @@ public static int randomInt() { return random().nextInt(); } - public static long randomPositiveLong() { + public static long randomNonNegativeLong() { long randomLong; do { randomLong = randomLong(); diff --git a/test/framework/src/test/java/org/elasticsearch/search/MockSearchServiceTests.java b/test/framework/src/test/java/org/elasticsearch/search/MockSearchServiceTests.java index b000c42d41a8b..d154b7d6873c6 100644 --- a/test/framework/src/test/java/org/elasticsearch/search/MockSearchServiceTests.java +++ b/test/framework/src/test/java/org/elasticsearch/search/MockSearchServiceTests.java @@ -33,7 +33,7 @@ public class MockSearchServiceTests extends ESTestCase { public void testAssertNoInFlightContext() { - final long nowInMillis = randomPositiveLong(); + final long nowInMillis = randomNonNegativeLong(); SearchContext s = new TestSearchContext(new QueryShardContext(0, new IndexSettings(IndexMetaData.PROTO, Settings.EMPTY), null, null, null, null, null, xContentRegistry(), null, null, null, () -> nowInMillis)) { @Override