From 2ac7421f89d37d92cb25262f31bbee9c94d0a869 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Tue, 3 Sep 2013 12:16:43 +0200 Subject: [PATCH] Remove UUID class in favor of Strings util methods --- .../action/index/IndexRequest.java | 6 +- .../org/elasticsearch/common/Strings.java | 92 ++++-- .../java/org/elasticsearch/common/UUID.java | 302 ------------------ .../discovery/zen/ZenDiscovery.java | 20 +- .../benchmark/bloom/BloomBench.java | 9 +- .../test/integration/AbstractNodesTests.java | 3 +- .../test/integration/TestCluster.java | 8 +- .../allocation/ClusterRerouteTests.java | 1 - .../fullrestart/FullRestartStressTest.java | 6 +- .../RollingRestartStressTest.java | 14 +- 10 files changed, 113 insertions(+), 348 deletions(-) delete mode 100644 src/main/java/org/elasticsearch/common/UUID.java diff --git a/src/main/java/org/elasticsearch/action/index/IndexRequest.java b/src/main/java/org/elasticsearch/action/index/IndexRequest.java index b0441fb4e35..d31fe364dba 100644 --- a/src/main/java/org/elasticsearch/action/index/IndexRequest.java +++ b/src/main/java/org/elasticsearch/action/index/IndexRequest.java @@ -32,7 +32,7 @@ import org.elasticsearch.cluster.metadata.MappingMetaData; import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Required; -import org.elasticsearch.common.UUID; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; @@ -66,7 +66,7 @@ import static org.elasticsearch.action.ValidateActions.addValidationError; * @see org.elasticsearch.client.Client#index(IndexRequest) */ public class IndexRequest extends ShardReplicationOperationRequest { - + /** * Operation type controls if the type of the index operation. */ @@ -593,7 +593,7 @@ public class IndexRequest extends ShardReplicationOperationRequest // generate id if not already provided and id generation is allowed if (allowIdGeneration) { if (id == null) { - id(UUID.randomBase64UUID()); + id(Strings.randomBase64UUID()); // since we generate the id, change it to CREATE opType(IndexRequest.OpType.CREATE); } diff --git a/src/main/java/org/elasticsearch/common/Strings.java b/src/main/java/org/elasticsearch/common/Strings.java index 3eb912e7cf4..8b7c06aaa39 100644 --- a/src/main/java/org/elasticsearch/common/Strings.java +++ b/src/main/java/org/elasticsearch/common/Strings.java @@ -22,12 +22,14 @@ package org.elasticsearch.common; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import gnu.trove.set.hash.THashSet; - import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.UnicodeUtil; +import org.elasticsearch.ElasticSearchIllegalStateException; import org.elasticsearch.common.io.FastStringReader; import java.io.BufferedReader; +import java.io.IOException; +import java.security.SecureRandom; import java.util.*; /** @@ -48,24 +50,32 @@ public class Strings { private static final char EXTENSION_SEPARATOR = '.'; public static void tabify(int tabs, String from, StringBuilder to) throws Exception { - BufferedReader reader = new BufferedReader(new FastStringReader(from)); - String line; - while ((line = reader.readLine()) != null) { - for (int i = 0; i < tabs; i++) { - to.append('\t'); + final BufferedReader reader = new BufferedReader(new FastStringReader(from)); + try { + String line; + while ((line = reader.readLine()) != null) { + for (int i = 0; i < tabs; i++) { + to.append('\t'); + } + to.append(line).append('\n'); } - to.append(line).append('\n'); + } finally { + reader.close(); } } public static void spaceify(int spaces, String from, StringBuilder to) throws Exception { - BufferedReader reader = new BufferedReader(new FastStringReader(from)); - String line; - while ((line = reader.readLine()) != null) { - for (int i = 0; i < spaces; i++) { - to.append(' '); + final BufferedReader reader = new BufferedReader(new FastStringReader(from)); + try { + String line; + while ((line = reader.readLine()) != null) { + for (int i = 0; i < spaces; i++) { + to.append(' '); + } + to.append(line).append('\n'); } - to.append(line).append('\n'); + } finally { + reader.close(); } } @@ -1479,17 +1489,6 @@ public class Strings { return (array == null || array.length == 0); } - /** - * Return true if the supplied Collection is null - * or empty. Otherwise, return false. - * - * @param collection the Collection to check - * @return whether the given Collection is empty - */ - private static boolean isEmpty(Collection collection) { - return (collection == null || collection.isEmpty()); - } - private Strings() { } @@ -1504,4 +1503,49 @@ public class Strings { System.arraycopy(spare.bytes, spare.offset, bytes, 0, bytes.length); return bytes; } + + private static class SecureRandomHolder { + private static final SecureRandom INSTANCE = new SecureRandom(); + } + + /** + * Returns a Base64 encoded version of a Version 4.0 compatible UUID + * as defined here: http://www.ietf.org/rfc/rfc4122.txt + */ + public static String randomBase64UUID() { + return randomBase64UUID(SecureRandomHolder.INSTANCE); + } + + /** + * Returns a Base64 encoded version of a Version 4.0 compatible UUID + * randomly initialized by the given {@link Random} instance + * as defined here: http://www.ietf.org/rfc/rfc4122.txt + */ + public static String randomBase64UUID(Random random) { + final byte[] randomBytes = new byte[16]; + random.nextBytes(randomBytes); + + /* Set the version to version 4 (see http://www.ietf.org/rfc/rfc4122.txt) + * The randomly or pseudo-randomly generated version. + * The version number is in the most significant 4 bits of the time + * stamp (bits 4 through 7 of the time_hi_and_version field).*/ + randomBytes[6] &= 0x0f; /* clear the 4 most significant bits for the version */ + randomBytes[6] |= 0x40; /* set the version to 0100 / 0x40 */ + + /* Set the variant: + * The high field of th clock sequence multiplexed with the variant. + * We set only the MSB of the variant*/ + randomBytes[8] &= 0x3f; /* clear the 2 most significant bits */ + randomBytes[8] |= 0x80; /* set the variant (MSB is set)*/ + try { + byte[] encoded = Base64.encodeBytesToBytes(randomBytes, 0, randomBytes.length, Base64.URL_SAFE); + // we know the bytes are 16, and not a multi of 3, so remove the 2 padding chars that are added + assert encoded[encoded.length - 1] == '='; + assert encoded[encoded.length - 2] == '='; + // we always have padding of two at the end, encode it differently + return new String(encoded, 0, encoded.length - 2, Base64.PREFERRED_ENCODING); + } catch (IOException e) { + throw new ElasticSearchIllegalStateException("should not be thrown"); + } + } } diff --git a/src/main/java/org/elasticsearch/common/UUID.java b/src/main/java/org/elasticsearch/common/UUID.java deleted file mode 100644 index 8be5ec78975..00000000000 --- a/src/main/java/org/elasticsearch/common/UUID.java +++ /dev/null @@ -1,302 +0,0 @@ -/* - * Licensed to ElasticSearch and Shay Banon under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. ElasticSearch licenses this - * file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.common; - -import org.elasticsearch.ElasticSearchIllegalStateException; - -import java.io.IOException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; - -/** - * A UUID taken from java UUID that simply holds less data. - * - * - */ -public class UUID implements Comparable { - - /* - * The most significant 64 bits of this UUID. - * - * @serial - */ - private final long mostSigBits; - - /* - * The least significant 64 bits of this UUID. - * - * @serial - */ - private final long leastSigBits; - - /* - * The random number generator used by this class to create random - * based UUIDs. - */ - private static volatile SecureRandom numberGenerator = null; - - // Constructors and Factories - - /* - * Private constructor which uses a byte array to construct the new UUID. - */ - - private UUID(byte[] data) { - long msb = 0; - long lsb = 0; - assert data.length == 16; - for (int i = 0; i < 8; i++) - msb = (msb << 8) | (data[i] & 0xff); - for (int i = 8; i < 16; i++) - lsb = (lsb << 8) | (data[i] & 0xff); - this.mostSigBits = msb; - this.leastSigBits = lsb; - } - - /** - * Constructs a new UUID using the specified data. - * mostSigBits is used for the most significant 64 bits - * of the UUID and leastSigBits becomes the - * least significant 64 bits of the UUID. - * - * @param mostSigBits - * @param leastSigBits - */ - public UUID(long mostSigBits, long leastSigBits) { - this.mostSigBits = mostSigBits; - this.leastSigBits = leastSigBits; - } - - /** - * Static factory to retrieve a type 4 (pseudo randomly generated) UUID. - *

- * The UUID is generated using a cryptographically strong - * pseudo random number generator. - * - * @return a randomly generated UUID. - */ - public static UUID randomUUID() { - SecureRandom ng = numberGenerator; - if (ng == null) { - numberGenerator = ng = new SecureRandom(); - } - - byte[] randomBytes = new byte[16]; - ng.nextBytes(randomBytes); - randomBytes[6] &= 0x0f; /* clear version */ - randomBytes[6] |= 0x40; /* set to version 4 */ - randomBytes[8] &= 0x3f; /* clear variant */ - randomBytes[8] |= 0x80; /* set to IETF variant */ - return new UUID(randomBytes); - } - - public static String randomBase64UUID() { - SecureRandom ng = numberGenerator; - if (ng == null) { - numberGenerator = ng = new SecureRandom(); - } - - byte[] randomBytes = new byte[16]; - ng.nextBytes(randomBytes); - randomBytes[6] &= 0x0f; /* clear version */ - randomBytes[6] |= 0x40; /* set to version 4 */ - randomBytes[8] &= 0x3f; /* clear variant */ - randomBytes[8] |= 0x80; /* set to IETF variant */ - - - try { - byte[] encoded = Base64.encodeBytesToBytes(randomBytes, 0, randomBytes.length, Base64.URL_SAFE); - // we know the bytes are 16, and not a multi of 3, so remove the 2 padding chars that are added - assert encoded[encoded.length - 1] == '='; - assert encoded[encoded.length - 2] == '='; - // we always have padding of two at the end, encode it differently - return new String(encoded, 0, encoded.length - 2, Base64.PREFERRED_ENCODING); - } catch (IOException e) { - throw new ElasticSearchIllegalStateException("should not be thrown"); - } - } - - /** - * Static factory to retrieve a type 3 (name based) UUID based on - * the specified byte array. - * - * @param name a byte array to be used to construct a UUID. - * @return a UUID generated from the specified array. - */ - public static UUID nameUUIDFromBytes(byte[] name) { - MessageDigest md; - try { - md = MessageDigest.getInstance("MD5"); - } catch (NoSuchAlgorithmException nsae) { - throw new InternalError("MD5 not supported"); - } - byte[] md5Bytes = md.digest(name); - md5Bytes[6] &= 0x0f; /* clear version */ - md5Bytes[6] |= 0x30; /* set to version 3 */ - md5Bytes[8] &= 0x3f; /* clear variant */ - md5Bytes[8] |= 0x80; /* set to IETF variant */ - return new UUID(md5Bytes); - } - - /** - * Creates a UUID from the string standard representation as - * described in the {@link #toString} method. - * - * @param name a string that specifies a UUID. - * @return a UUID with the specified value. - * @throws IllegalArgumentException if name does not conform to the - * string representation as described in {@link #toString}. - */ - public static UUID fromString(String name) { - String[] components = name.split("-"); - if (components.length != 5) - throw new IllegalArgumentException("Invalid UUID string: " + name); - for (int i = 0; i < 5; i++) - components[i] = "0x" + components[i]; - - long mostSigBits = Long.decode(components[0]).longValue(); - mostSigBits <<= 16; - mostSigBits |= Long.decode(components[1]).longValue(); - mostSigBits <<= 16; - mostSigBits |= Long.decode(components[2]).longValue(); - - long leastSigBits = Long.decode(components[3]).longValue(); - leastSigBits <<= 48; - leastSigBits |= Long.decode(components[4]).longValue(); - - return new UUID(mostSigBits, leastSigBits); - } - - // Field Accessor Methods - - /** - * Returns the least significant 64 bits of this UUID's 128 bit value. - * - * @return the least significant 64 bits of this UUID's 128 bit value. - */ - public long getLeastSignificantBits() { - return leastSigBits; - } - - /** - * Returns the most significant 64 bits of this UUID's 128 bit value. - * - * @return the most significant 64 bits of this UUID's 128 bit value. - */ - public long getMostSignificantBits() { - return mostSigBits; - } - - // Object Inherited Methods - - /** - * Returns a String object representing this - * UUID. - *

- *

The UUID string representation is as described by this BNF : - *

-     * {@code
-     * UUID                   =  "-"  "-"
-     *                           "-"
-     *                           "-"
-     *                          
-     * time_low               = 4*
-     * time_mid               = 2*
-     * time_high_and_version  = 2*
-     * variant_and_sequence   = 2*
-     * node                   = 6*
-     * hexOctet               = 
-     * hexDigit               =
-     *       "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
-     *       | "a" | "b" | "c" | "d" | "e" | "f"
-     *       | "A" | "B" | "C" | "D" | "E" | "F"
-     * }
- * - * @return a string representation of this UUID. - */ - public String toString() { - return (digits(mostSigBits >> 32, 8) + "-" + - digits(mostSigBits >> 16, 4) + "-" + - digits(mostSigBits, 4) + "-" + - digits(leastSigBits >> 48, 4) + "-" + - digits(leastSigBits, 12)); - } - - /** - * Returns val represented by the specified number of hex digits. - */ - private static String digits(long val, int digits) { - long hi = 1L << (digits * 4); - return Long.toHexString(hi | (val & (hi - 1))).substring(1); - } - - /** - * Returns a hash code for this UUID. - * - * @return a hash code value for this UUID. - */ - public int hashCode() { - return (int) ((mostSigBits >> 32) ^ - mostSigBits ^ - (leastSigBits >> 32) ^ - leastSigBits); - } - - /** - * Compares this object to the specified object. The result is - * true if and only if the argument is not - * null, is a UUID object, has the same variant, - * and contains the same value, bit for bit, as this UUID. - * - * @param obj the object to compare with. - * @return true if the objects are the same; - * false otherwise. - */ - public boolean equals(Object obj) { - if (!(obj instanceof UUID)) - return false; - UUID id = (UUID) obj; - return (mostSigBits == id.mostSigBits && - leastSigBits == id.leastSigBits); - } - - // Comparison Operations - - /** - * Compares this UUID with the specified UUID. - *

- *

The first of two UUIDs follows the second if the most significant - * field in which the UUIDs differ is greater for the first UUID. - * - * @param val UUID to which this UUID is to be compared. - * @return -1, 0 or 1 as this UUID is less than, equal - * to, or greater than val. - */ - public int compareTo(UUID val) { - // The ordering is intentionally set up so that the UUIDs - // can simply be numerically compared as two numbers - return (this.mostSigBits < val.mostSigBits ? -1 : - (this.mostSigBits > val.mostSigBits ? 1 : - (this.leastSigBits < val.leastSigBits ? -1 : - (this.leastSigBits > val.leastSigBits ? 1 : - 0)))); - } -} diff --git a/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java b/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java index 3ec6d7033fa..3bef69e4e91 100644 --- a/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java +++ b/src/main/java/org/elasticsearch/discovery/zen/ZenDiscovery.java @@ -35,7 +35,7 @@ import org.elasticsearch.cluster.routing.RoutingTable; import org.elasticsearch.cluster.routing.allocation.AllocationService; import org.elasticsearch.cluster.routing.allocation.RoutingAllocation; import org.elasticsearch.common.Priority; -import org.elasticsearch.common.UUID; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.component.AbstractLifecycleComponent; import org.elasticsearch.common.component.Lifecycle; import org.elasticsearch.common.inject.Inject; @@ -62,6 +62,7 @@ import org.elasticsearch.transport.*; import java.io.IOException; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicBoolean; @@ -112,6 +113,7 @@ public class ZenDiscovery extends AbstractLifecycleComponent implemen private volatile Thread currentJoinThread; private final AtomicBoolean initialStateSent = new AtomicBoolean(); + @Nullable private NodeService nodeService; @@ -153,7 +155,7 @@ public class ZenDiscovery extends AbstractLifecycleComponent implemen transportService.registerHandler(RejoinClusterRequestHandler.ACTION, new RejoinClusterRequestHandler()); } - + @Override public void setNodeService(@Nullable NodeService nodeService) { this.nodeService = nodeService; @@ -168,7 +170,7 @@ public class ZenDiscovery extends AbstractLifecycleComponent implemen protected void doStart() throws ElasticSearchException { Map nodeAttributes = discoveryNodeService.buildAttributes(); // note, we rely on the fact that its a new id each time we start, see FD and "kill -9" handling - String nodeId = UUID.randomBase64UUID(); + final String nodeId = getNodeUUID(settings); localNode = new DiscoveryNode(settings.get("name"), nodeId, transportService.boundAddress().publishAddress(), nodeAttributes, version); latestDiscoNodes = new DiscoveryNodes.Builder().put(localNode).localNodeId(localNode.id()).build(); nodesFD.updateNodes(latestDiscoNodes); @@ -339,9 +341,8 @@ public class ZenDiscovery extends AbstractLifecycleComponent implemen continue; } // send join request - ClusterState joinClusterStateX; try { - joinClusterStateX = membership.sendJoinRequestBlocking(masterNode, localNode, pingTimeout); + membership.sendJoinRequestBlocking(masterNode, localNode, pingTimeout); } catch (Exception e) { if (e instanceof ElasticSearchException) { logger.info("failed to send join request to master [{}], reason [{}]", masterNode, ((ElasticSearchException) e).getDetailedMessage()); @@ -888,5 +889,14 @@ public class ZenDiscovery extends AbstractLifecycleComponent implemen } } } + + private final String getNodeUUID(Settings settings) { + String seed = settings.get("discovery.id.seed"); + if (seed != null) { + logger.warn("using stable discover node UUIDs with seed: [{}]", seed); + Strings.randomBase64UUID(new Random(Long.parseLong(seed))); + } + return Strings.randomBase64UUID(); + } } diff --git a/src/test/java/org/elasticsearch/benchmark/bloom/BloomBench.java b/src/test/java/org/elasticsearch/benchmark/bloom/BloomBench.java index e9bb2e8551d..73a65c5aec4 100644 --- a/src/test/java/org/elasticsearch/benchmark/bloom/BloomBench.java +++ b/src/test/java/org/elasticsearch/benchmark/bloom/BloomBench.java @@ -2,16 +2,19 @@ package org.elasticsearch.benchmark.bloom; import org.apache.lucene.codecs.bloom.FuzzySet; import org.apache.lucene.util.BytesRef; -import org.elasticsearch.common.UUID; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.SizeValue; import org.elasticsearch.index.codec.postingsformat.BloomFilter; +import java.security.SecureRandom; + /** */ public class BloomBench { public static void main(String[] args) throws Exception { + SecureRandom random = new SecureRandom(); final int ELEMENTS = (int) SizeValue.parseSizeValue("1m").singles(); final double fpp = 0.01; BloomFilter gFilter = BloomFilter.create(ELEMENTS, fpp); @@ -21,7 +24,7 @@ public class BloomBench { //FuzzySet lFilter = FuzzySet.createSetBasedOnQuality(ELEMENTS, 0.97f); for (int i = 0; i < ELEMENTS; i++) { - BytesRef bytesRef = new BytesRef(UUID.randomBase64UUID()); + BytesRef bytesRef = new BytesRef(Strings.randomBase64UUID(random)); gFilter.put(bytesRef); lFilter.addValue(bytesRef); } @@ -29,7 +32,7 @@ public class BloomBench { int lFalse = 0; int gFalse = 0; for (int i = 0; i < ELEMENTS; i++) { - BytesRef bytesRef = new BytesRef(UUID.randomBase64UUID()); + BytesRef bytesRef = new BytesRef(Strings.randomBase64UUID(random)); if (gFilter.mightContain(bytesRef)) { gFalse++; } diff --git a/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java b/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java index 60da838b6ab..84c5a27e33b 100644 --- a/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java +++ b/src/test/java/org/elasticsearch/test/integration/AbstractNodesTests.java @@ -90,8 +90,9 @@ public abstract class AbstractNodesTests extends ElasticsearchTestCase { .put(getClassDefaultSettings()) .put(settings) .put("name", id) + .put("discovery.id.seed", randomLong()) .build(); - + if (finalSettings.get("gateway.type") == null) { // default to non gateway finalSettings = settingsBuilder().put(finalSettings).put("gateway.type", "none").build(); diff --git a/src/test/java/org/elasticsearch/test/integration/TestCluster.java b/src/test/java/org/elasticsearch/test/integration/TestCluster.java index b1830827485..d6f7296b823 100644 --- a/src/test/java/org/elasticsearch/test/integration/TestCluster.java +++ b/src/test/java/org/elasticsearch/test/integration/TestCluster.java @@ -50,8 +50,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import static com.google.common.collect.Maps.newHashMap; -import static org.elasticsearch.common.settings.ImmutableSettings.Builder.EMPTY_SETTINGS; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; +import static org.elasticsearch.common.settings.ImmutableSettings.Builder.EMPTY_SETTINGS; import static org.elasticsearch.node.NodeBuilder.nodeBuilder; public class TestCluster { @@ -169,7 +169,11 @@ public class TestCluster { ensureOpen(); String name = "node_" + nextNodeId.getAndIncrement(); String settingsSource = getClass().getName().replace('.', '/') + ".yml"; - Settings finalSettings = settingsBuilder().loadFromClasspath(settingsSource).put(defaultSettings).put(settings).put("name", name) + Settings finalSettings = settingsBuilder() + .loadFromClasspath(settingsSource) + .put(defaultSettings).put(settings) + .put("name", name) + .put("discovery.id.seed", random.nextLong()) .build(); Node node = nodeBuilder().settings(finalSettings).build(); nodes.put(name, new NodeAndClient(name, node, clientFactory)); diff --git a/src/test/java/org/elasticsearch/test/integration/cluster/allocation/ClusterRerouteTests.java b/src/test/java/org/elasticsearch/test/integration/cluster/allocation/ClusterRerouteTests.java index 8fa31110870..44f437f9cbc 100644 --- a/src/test/java/org/elasticsearch/test/integration/cluster/allocation/ClusterRerouteTests.java +++ b/src/test/java/org/elasticsearch/test/integration/cluster/allocation/ClusterRerouteTests.java @@ -42,7 +42,6 @@ import org.junit.Test; import java.io.File; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; -import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; /** diff --git a/src/test/java/org/elasticsearch/test/stress/fullrestart/FullRestartStressTest.java b/src/test/java/org/elasticsearch/test/stress/fullrestart/FullRestartStressTest.java index 2f3ceec8e2f..7365d06d93f 100644 --- a/src/test/java/org/elasticsearch/test/stress/fullrestart/FullRestartStressTest.java +++ b/src/test/java/org/elasticsearch/test/stress/fullrestart/FullRestartStressTest.java @@ -25,7 +25,7 @@ import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.count.CountResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Requests; -import org.elasticsearch.common.UUID; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.FileSystemUtils; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; @@ -40,6 +40,7 @@ import org.elasticsearch.node.NodeBuilder; import org.elasticsearch.node.internal.InternalNode; import java.io.File; +import java.util.Random; import java.util.concurrent.atomic.AtomicLong; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; @@ -114,6 +115,7 @@ public class FullRestartStressTest { public void run() throws Exception { long numberOfRounds = 0; + Random random = new Random(0); long testStart = System.currentTimeMillis(); while (true) { Node[] nodes = new Node[numberOfNodes]; @@ -180,7 +182,7 @@ public class FullRestartStressTest { int tokens = ThreadLocalRandom.current().nextInt() % textTokens; sb.setLength(0); for (int j = 0; j < tokens; j++) { - sb.append(UUID.randomBase64UUID()).append(' '); + sb.append(Strings.randomBase64UUID(random)).append(' '); } json.field("text_" + i, sb.toString()); } diff --git a/src/test/java/org/elasticsearch/test/stress/rollingrestart/RollingRestartStressTest.java b/src/test/java/org/elasticsearch/test/stress/rollingrestart/RollingRestartStressTest.java index 023610c9727..d2f234602aa 100644 --- a/src/test/java/org/elasticsearch/test/stress/rollingrestart/RollingRestartStressTest.java +++ b/src/test/java/org/elasticsearch/test/stress/rollingrestart/RollingRestartStressTest.java @@ -28,7 +28,7 @@ import org.elasticsearch.action.count.CountResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; -import org.elasticsearch.common.UUID; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.FileSystemUtils; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; @@ -45,6 +45,7 @@ import org.elasticsearch.search.SearchHit; import java.io.File; import java.util.Arrays; +import java.util.Random; import java.util.concurrent.atomic.AtomicLong; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; @@ -137,6 +138,8 @@ public class RollingRestartStressTest { } public void run() throws Exception { + Random random = new Random(0); + Node[] nodes = new Node[numberOfNodes]; for (int i = 0; i < nodes.length; i++) { nodes[i] = NodeBuilder.nodeBuilder().settings(settings).node(); @@ -150,7 +153,7 @@ public class RollingRestartStressTest { logger.info("********** [START] INDEXING INITIAL DOCS"); for (long i = 0; i < initialNumberOfDocs; i++) { - indexDoc(); + indexDoc(random); } logger.info("********** [DONE ] INDEXING INITIAL DOCS"); @@ -295,13 +298,14 @@ public class RollingRestartStressTest { @Override public void run() { + Random random = new Random(0); while (true) { if (close) { closed = true; return; } try { - indexDoc(); + indexDoc(random); Thread.sleep(indexerThrottle.millis()); } catch (Exception e) { logger.warn("failed to index / sleep", e); @@ -310,7 +314,7 @@ public class RollingRestartStressTest { } } - private void indexDoc() throws Exception { + private void indexDoc(Random random) throws Exception { StringBuilder sb = new StringBuilder(); XContentBuilder json = XContentFactory.jsonBuilder().startObject() .field("field", "value" + ThreadLocalRandom.current().nextInt()); @@ -321,7 +325,7 @@ public class RollingRestartStressTest { int tokens = ThreadLocalRandom.current().nextInt() % textTokens; sb.setLength(0); for (int j = 0; j < tokens; j++) { - sb.append(UUID.randomBase64UUID()).append(' '); + sb.append(Strings.randomBase64UUID(random)).append(' '); } json.field("text_" + i, sb.toString()); }