Merge branch 'master' into plugin-api

Includes porting changes made to LicenseVersion in master

Original commit: elastic/x-pack-elasticsearch@2d9919487e
This commit is contained in:
Konrad Beiske 2015-07-02 14:08:36 +02:00
commit 7975e56f1e
5 changed files with 60 additions and 239 deletions

View File

@ -35,18 +35,7 @@
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resource>
</resources>
<plugins>
<plugin>
<groupId>com.carrotsearch.randomizedtesting</groupId>
<artifactId>junit4-maven-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -62,7 +62,7 @@ public class LicenseVersion implements Serializable {
return LicenseVersion.CURRENT;
}
String[] parts = version.split("\\.");
String[] parts = version.split("\\.|\\-");
if (parts.length < 3 || parts.length > 4) {
throw new IllegalArgumentException("the version needs to contain major, minor and revision, and optionally the build");
}
@ -76,10 +76,10 @@ public class LicenseVersion implements Serializable {
int build = 99;
if (parts.length == 4) {
String buildStr = parts[3];
if (buildStr.startsWith("Beta")) {
if (buildStr.startsWith("beta")) {
build = Integer.parseInt(buildStr.substring(4));
}
if (buildStr.startsWith("RC")) {
if (buildStr.startsWith("rc")) {
build = Integer.parseInt(buildStr.substring(2)) + 50;
}
}
@ -155,9 +155,9 @@ public class LicenseVersion implements Serializable {
StringBuilder sb = new StringBuilder();
sb.append(major).append('.').append(minor).append('.').append(revision);
if (build < 50) {
sb.append(".Beta").append(build);
sb.append("-beta").append(build);
} else if (build < 99) {
sb.append(".RC").append(build - 50);
sb.append("-rc").append(build - 50);
}
return sb.toString();
}

View File

@ -0,0 +1,54 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.license.plugin;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.junit.Test;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
/**
*
*/
public class LicenseVersionTests extends ElasticsearchTestCase {
@Test
public void testStrings() throws Exception {
boolean beta = randomBoolean();
int buildNumber = beta ? randomIntBetween(0, 49) : randomIntBetween(0, 48);
int major = randomIntBetween(0, 20);
int minor = randomIntBetween(0, 20);
int revision = randomIntBetween(0, 20);
String build = buildNumber == 0 ? "" :
beta ? "-beta" + buildNumber : "-rc" + buildNumber;
String versionName = new StringBuilder()
.append(major)
.append(".").append(minor)
.append(".").append(revision)
.append(build).toString();
LicenseVersion version = LicenseVersion.fromString(versionName);
logger.info("version: {}", versionName);
assertThat(version.major, is((byte) major));
assertThat(version.minor, is((byte) minor));
assertThat(version.revision, is((byte) revision));
if (buildNumber == 0) {
assertThat(version.build, is((byte) 99));
} else if (beta) {
assertThat(version.build, is((byte) buildNumber));
} else {
assertThat(version.build, is((byte) (buildNumber + 50)));
}
assertThat(version.number(), equalTo(versionName));
}
}

View File

@ -1,191 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.license.plugin;
import org.elasticsearch.Version;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.license.core.License;
import java.io.IOException;
import java.io.Serializable;
@SuppressWarnings("deprecation")
public class LicenseVersion implements Serializable {
// The logic for ID is: XXYYZZAA, where XX is major version, YY is minor version, ZZ is revision, and AA is Beta/RC indicator
// AA values below 50 are beta builds, and below 99 are RC builds, with 99 indicating a release
// the (internal) format of the id is there so we can easily do after/before checks on the id
public static final int V_1_0_0_ID = /*00*/1000099;
public static final int V_2_0_0_ID = /*00*/2000099;
public static final LicenseVersion V_1_0_0 = new LicenseVersion(V_1_0_0_ID, false, License.VERSION_START, Version.V_1_4_0_Beta1);
public static final LicenseVersion V_2_0_0 = new LicenseVersion(V_2_0_0_ID, true, License.VERSION_START, Version.V_2_0_0);
public static final LicenseVersion CURRENT = V_2_0_0;
public static LicenseVersion readVersion(StreamInput in) throws IOException {
return fromId(in.readVInt());
}
public static LicenseVersion fromId(int id) {
switch (id) {
case V_1_0_0_ID:
return V_1_0_0;
default:
return new LicenseVersion(id, null, License.VERSION_CURRENT, Version.CURRENT);
}
}
public static void writeVersion(LicenseVersion version, StreamOutput out) throws IOException {
out.writeVInt(version.id);
}
/**
* Returns the smallest version between the 2.
*/
public static LicenseVersion smallest(LicenseVersion version1, LicenseVersion version2) {
return version1.id < version2.id ? version1 : version2;
}
/**
* Returns the version given its string representation, current version if the argument is null or empty
*/
public static LicenseVersion fromString(String version) {
if (!Strings.hasLength(version)) {
return LicenseVersion.CURRENT;
}
String[] parts = version.split("\\.");
if (parts.length < 3 || parts.length > 4) {
throw new IllegalArgumentException("the version needs to contain major, minor and revision, and optionally the build");
}
try {
//we reverse the version id calculation based on some assumption as we can't reliably reverse the modulo
int major = Integer.parseInt(parts[0]) * 1000000;
int minor = Integer.parseInt(parts[1]) * 10000;
int revision = Integer.parseInt(parts[2]) * 100;
int build = 99;
if (parts.length == 4) {
String buildStr = parts[3];
if (buildStr.startsWith("Beta")) {
build = Integer.parseInt(buildStr.substring(4));
}
if (buildStr.startsWith("RC")) {
build = Integer.parseInt(buildStr.substring(2)) + 50;
}
}
return fromId(major + minor + revision + build);
} catch(NumberFormatException e) {
throw new IllegalArgumentException("unable to parse version " + version, e);
}
}
public final int id;
public final byte major;
public final byte minor;
public final byte revision;
public final byte build;
public final Boolean snapshot;
public final int minSignatureVersion;
public final Version minEsCompatibilityVersion;
LicenseVersion(int id, @Nullable Boolean snapshot, int minSignatureVersion, Version minEsCompatibilityVersion) {
this.id = id;
this.major = (byte) ((id / 1000000) % 100);
this.minor = (byte) ((id / 10000) % 100);
this.revision = (byte) ((id / 100) % 100);
this.build = (byte) (id % 100);
this.snapshot = snapshot;
this.minSignatureVersion = minSignatureVersion;
this.minEsCompatibilityVersion = minEsCompatibilityVersion;
}
public boolean snapshot() {
return snapshot != null && snapshot;
}
public boolean after(LicenseVersion version) {
return version.id < id;
}
public boolean before(LicenseVersion version) {
return version.id > id;
}
/**
* Returns the minimum compatible version based on the current
* version. Ie a node needs to have at least the return version in order
* to communicate with a node running the current version. The returned version
* is in most of the cases the smallest major version release unless the current version
* is a beta or RC release then the version itself is returned.
*/
public LicenseVersion minimumCompatibilityVersion() {
return LicenseVersion.smallest(this, fromId(major * 1000000 + 99));
}
/**
* @return The minimum elasticsearch version this license version is compatible with.
*/
public Version minimumEsCompatiblityVersion() {
return minEsCompatibilityVersion;
}
/**
* @return The minimum license signature version this license plugin is compatible with.
*/
public int minimumSignatureVersion() {
return minSignatureVersion;
}
/**
* Just the version number (without -SNAPSHOT if snapshot).
*/
public String number() {
StringBuilder sb = new StringBuilder();
sb.append(major).append('.').append(minor).append('.').append(revision);
if (build < 50) {
sb.append(".Beta").append(build);
} else if (build < 99) {
sb.append(".RC").append(build - 50);
}
return sb.toString();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(number());
if (snapshot()) {
sb.append("-SNAPSHOT");
}
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LicenseVersion that = (LicenseVersion) o;
if (id != that.id) return false;
return true;
}
@Override
public int hashCode() {
return id;
}
}

View File

@ -1,31 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.license.plugin.core;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.rest.RestStatus;
/**
* Exception to be thrown when a feature action requires a valid license
*/
public class LicenseExpiredException extends ElasticsearchException {
private final String feature;
public LicenseExpiredException(String feature) {
super("license expired for feature [" + feature + "]");
this.feature = feature;
}
@Override
public RestStatus status() {
return RestStatus.UNAUTHORIZED;
}
public String feature() {
return feature;
}
}