Remove UUID class in favor of Strings util methods

This commit is contained in:
Simon Willnauer 2013-09-03 12:16:43 +02:00
parent 393c28bee4
commit 2ac7421f89
10 changed files with 113 additions and 348 deletions

View File

@ -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;
@ -593,7 +593,7 @@ public class IndexRequest extends ShardReplicationOperationRequest<IndexRequest>
// 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);
}

View File

@ -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,7 +50,8 @@ 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));
final BufferedReader reader = new BufferedReader(new FastStringReader(from));
try {
String line;
while ((line = reader.readLine()) != null) {
for (int i = 0; i < tabs; i++) {
@ -56,10 +59,14 @@ public class Strings {
}
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));
final BufferedReader reader = new BufferedReader(new FastStringReader(from));
try {
String line;
while ((line = reader.readLine()) != null) {
for (int i = 0; i < spaces; i++) {
@ -67,6 +74,9 @@ public class Strings {
}
to.append(line).append('\n');
}
} finally {
reader.close();
}
}
/**
@ -1479,17 +1489,6 @@ public class Strings {
return (array == null || array.length == 0);
}
/**
* Return <code>true</code> if the supplied Collection is <code>null</code>
* or empty. Otherwise, return <code>false</code>.
*
* @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");
}
}
}

View File

@ -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<UUID> {
/*
* 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 <tt>UUID</tt> using the specified data.
* <tt>mostSigBits</tt> is used for the most significant 64 bits
* of the <tt>UUID</tt> and <tt>leastSigBits</tt> becomes the
* least significant 64 bits of the <tt>UUID</tt>.
*
* @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.
* <p/>
* The <code>UUID</code> is generated using a cryptographically strong
* pseudo random number generator.
*
* @return a randomly generated <tt>UUID</tt>.
*/
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) <tt>UUID</tt> based on
* the specified byte array.
*
* @param name a byte array to be used to construct a <tt>UUID</tt>.
* @return a <tt>UUID</tt> 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 <tt>UUID</tt> from the string standard representation as
* described in the {@link #toString} method.
*
* @param name a string that specifies a <tt>UUID</tt>.
* @return a <tt>UUID</tt> 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 <code>String</code> object representing this
* <code>UUID</code>.
* <p/>
* <p>The UUID string representation is as described by this BNF :
* <blockquote><pre>
* {@code
* UUID = <time_low> "-" <time_mid> "-"
* <time_high_and_version> "-"
* <variant_and_sequence> "-"
* <node>
* time_low = 4*<hexOctet>
* time_mid = 2*<hexOctet>
* time_high_and_version = 2*<hexOctet>
* variant_and_sequence = 2*<hexOctet>
* node = 6*<hexOctet>
* hexOctet = <hexDigit><hexDigit>
* hexDigit =
* "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
* | "a" | "b" | "c" | "d" | "e" | "f"
* | "A" | "B" | "C" | "D" | "E" | "F"
* }</pre></blockquote>
*
* @return a string representation of this <tt>UUID</tt>.
*/
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 <code>UUID</code>.
*
* @return a hash code value for this <tt>UUID</tt>.
*/
public int hashCode() {
return (int) ((mostSigBits >> 32) ^
mostSigBits ^
(leastSigBits >> 32) ^
leastSigBits);
}
/**
* Compares this object to the specified object. The result is
* <tt>true</tt> if and only if the argument is not
* <tt>null</tt>, is a <tt>UUID</tt> object, has the same variant,
* and contains the same value, bit for bit, as this <tt>UUID</tt>.
*
* @param obj the object to compare with.
* @return <code>true</code> if the objects are the same;
* <code>false</code> 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.
* <p/>
* <p>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 <tt>UUID</tt> to which this <tt>UUID</tt> is to be compared.
* @return -1, 0 or 1 as this <tt>UUID</tt> is less than, equal
* to, or greater than <tt>val</tt>.
*/
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))));
}
}

View File

@ -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;
@ -113,6 +114,7 @@ public class ZenDiscovery extends AbstractLifecycleComponent<Discovery> implemen
private final AtomicBoolean initialStateSent = new AtomicBoolean();
@Nullable
private NodeService nodeService;
@ -168,7 +170,7 @@ public class ZenDiscovery extends AbstractLifecycleComponent<Discovery> implemen
protected void doStart() throws ElasticSearchException {
Map<String, String> 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<Discovery> 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());
@ -889,4 +890,13 @@ public class ZenDiscovery extends AbstractLifecycleComponent<Discovery> 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();
}
}

View File

@ -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++;
}

View File

@ -90,6 +90,7 @@ 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) {

View File

@ -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));

View File

@ -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;
/**

View File

@ -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());
}

View File

@ -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());
}