SQL: Bring SQL Version in line with ES Version (elastic/x-pack-elasticsearch#3308)

It also makes it possible to use the Version class to parse the version that we get from Elasticsearch.

Original commit: elastic/x-pack-elasticsearch@73a3268b12
This commit is contained in:
Igor Motov 2017-12-14 22:01:19 -05:00 committed by GitHub
parent f2792b8d93
commit 49a036cc5f
11 changed files with 76 additions and 77 deletions

View File

@ -26,9 +26,6 @@ import org.elasticsearch.xpack.sql.client.shared.Version;
import java.io.IOException; import java.io.IOException;
import java.net.ConnectException; import java.net.ConnectException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.logging.LogManager; import java.util.logging.LogManager;
@ -140,7 +137,7 @@ public class Cli extends Command {
// Most likely we connected to an old version of Elasticsearch or not Elasticsearch at all // Most likely we connected to an old version of Elasticsearch or not Elasticsearch at all
throw new UserException(ExitCodes.DATA_ERROR, throw new UserException(ExitCodes.DATA_ERROR,
"Cannot communicate with the server " + con.connectionString() + "Cannot communicate with the server " + con.connectionString() +
". This version of CLI only works with Elasticsearch version " + Version.version()); ". This version of CLI only works with Elasticsearch version " + Version.CURRENT.toString());
} }
} }

View File

@ -65,8 +65,9 @@ public class CliSession {
throw new ClientException(ex); throw new ClientException(ex);
} }
// TODO: We can relax compatibility requirement later when we have a better idea about protocol compatibility guarantees // TODO: We can relax compatibility requirement later when we have a better idea about protocol compatibility guarantees
if (response.majorVersion != Version.versionMajor() || response.minorVersion != Version.versionMinor()) { if (response.majorVersion != Version.CURRENT.major || response.minorVersion != Version.CURRENT.minor) {
throw new ClientException("This alpha version of CLI is only compatible with Elasticsearch version " + Version.version()); throw new ClientException("This alpha version of CLI is only compatible with Elasticsearch version " +
Version.CURRENT.toString());
} }
} }
} }

View File

@ -5,11 +5,11 @@
*/ */
package org.elasticsearch.xpack.sql.cli; package org.elasticsearch.xpack.sql.cli;
import org.elasticsearch.Version;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.sql.cli.command.CliSession; import org.elasticsearch.xpack.sql.cli.command.CliSession;
import org.elasticsearch.xpack.sql.cli.net.protocol.InfoResponse; import org.elasticsearch.xpack.sql.cli.net.protocol.InfoResponse;
import org.elasticsearch.xpack.sql.client.shared.ClientException; import org.elasticsearch.xpack.sql.client.shared.ClientException;
import org.elasticsearch.xpack.sql.client.shared.Version;
import java.sql.SQLException; import java.sql.SQLException;
@ -24,7 +24,7 @@ public class CliSessionTests extends ESTestCase {
public void testProperConnection() throws Exception { public void testProperConnection() throws Exception {
CliHttpClient cliHttpClient = mock(CliHttpClient.class); CliHttpClient cliHttpClient = mock(CliHttpClient.class);
when(cliHttpClient.serverInfo()).thenReturn(new InfoResponse(randomAlphaOfLength(5), randomAlphaOfLength(5), when(cliHttpClient.serverInfo()).thenReturn(new InfoResponse(randomAlphaOfLength(5), randomAlphaOfLength(5),
(byte) Version.versionMajor(), (byte) Version.versionMinor(), Version.CURRENT.major, Version.CURRENT.minor,
randomAlphaOfLength(5), randomAlphaOfLength(5), randomAlphaOfLength(5))); randomAlphaOfLength(5), randomAlphaOfLength(5), randomAlphaOfLength(5)));
CliSession cliSession = new CliSession(cliHttpClient); CliSession cliSession = new CliSession(cliHttpClient);
cliSession.checkConnection(); cliSession.checkConnection();
@ -46,11 +46,11 @@ public class CliSessionTests extends ESTestCase {
byte minor; byte minor;
byte major; byte major;
if (randomBoolean()) { if (randomBoolean()) {
minor = (byte) Version.versionMinor(); minor = Version.CURRENT.minor;
major = (byte) (Version.versionMajor() + 1); major = (byte) (Version.CURRENT.major + 1);
} else { } else {
minor = (byte) (Version.versionMinor() + 1); minor = (byte) (Version.CURRENT.minor + 1);
major = (byte) Version.versionMajor(); major = Version.CURRENT.major;
} }
when(cliHttpClient.serverInfo()).thenReturn(new InfoResponse(randomAlphaOfLength(5), randomAlphaOfLength(5), when(cliHttpClient.serverInfo()).thenReturn(new InfoResponse(randomAlphaOfLength(5), randomAlphaOfLength(5),

View File

@ -12,10 +12,10 @@ public class VersionTests extends ESTestCase {
public void testVersionIsCurrent() { public void testVersionIsCurrent() {
/* This test will only work properly in gradle because in gradle we run the tests /* This test will only work properly in gradle because in gradle we run the tests
* using the jar. */ * using the jar. */
assertEquals(org.elasticsearch.Version.CURRENT.toString(), Version.versionNumber()); assertEquals(org.elasticsearch.Version.CURRENT.toString(), Version.CURRENT.version);
assertNotNull(Version.versionHash()); assertNotNull(Version.CURRENT.hash);
assertEquals(org.elasticsearch.Version.CURRENT.major, Version.versionMajor()); assertEquals(org.elasticsearch.Version.CURRENT.major, Version.CURRENT.major);
assertEquals(org.elasticsearch.Version.CURRENT.minor, Version.versionMinor()); assertEquals(org.elasticsearch.Version.CURRENT.minor, Version.CURRENT.minor);
} }
} }

View File

@ -62,7 +62,7 @@ public class JdbcConfiguration extends ConnectionConfiguration {
// typically this should have already happened but in case the // typically this should have already happened but in case the
// JdbcDriver/JdbcDataSource are not used and the impl. classes used directly // JdbcDriver/JdbcDataSource are not used and the impl. classes used directly
// this covers that case // this covers that case
Version.version(); Version.CURRENT.toString();
} }
// immutable properties // immutable properties

View File

@ -90,7 +90,7 @@ class JdbcDatabaseMetaData implements DatabaseMetaData, JdbcWrapper {
@Override @Override
public String getDatabaseProductVersion() throws SQLException { public String getDatabaseProductVersion() throws SQLException {
return Version.version(); return Version.CURRENT.toString();
} }
@Override @Override
@ -100,17 +100,17 @@ class JdbcDatabaseMetaData implements DatabaseMetaData, JdbcWrapper {
@Override @Override
public String getDriverVersion() throws SQLException { public String getDriverVersion() throws SQLException {
return Version.versionMajor() + "." + Version.versionMinor(); return Version.CURRENT.major + "." + Version.CURRENT.minor;
} }
@Override @Override
public int getDriverMajorVersion() { public int getDriverMajorVersion() {
return Version.versionMajor(); return Version.CURRENT.major;
} }
@Override @Override
public int getDriverMinorVersion() { public int getDriverMinorVersion() {
return Version.versionMinor(); return Version.CURRENT.minor;
} }
@Override @Override

View File

@ -25,7 +25,7 @@ public class JdbcDriver implements java.sql.Driver {
static { static {
// invoke Version to perform classpath/jar sanity checks // invoke Version to perform classpath/jar sanity checks
Version.version(); Version.CURRENT.toString();
try { try {
register(); register();
@ -103,12 +103,12 @@ public class JdbcDriver implements java.sql.Driver {
@Override @Override
public int getMajorVersion() { public int getMajorVersion() {
return Version.versionMajor(); return Version.CURRENT.major;
} }
@Override @Override
public int getMinorVersion() { public int getMinorVersion() {
return Version.versionMinor(); return Version.CURRENT.minor;
} }
@Override @Override

View File

@ -25,7 +25,7 @@ import javax.sql.DataSource;
public class JdbcDataSource implements DataSource, Wrapper { public class JdbcDataSource implements DataSource, Wrapper {
static { static {
Version.version(); Version.CURRENT.toString();
} }
private String url; private String url;

View File

@ -12,9 +12,9 @@ public class VersionTests extends ESTestCase {
public void testVersionIsCurrent() { public void testVersionIsCurrent() {
/* This test will only work properly in gradle because in gradle we run the tests /* This test will only work properly in gradle because in gradle we run the tests
* using the jar. */ * using the jar. */
assertEquals(org.elasticsearch.Version.CURRENT.toString(), Version.versionNumber()); assertEquals(org.elasticsearch.Version.CURRENT.toString(), Version.CURRENT.version);
assertNotNull(Version.versionHash()); assertNotNull(Version.CURRENT.hash);
assertEquals(org.elasticsearch.Version.CURRENT.major, Version.versionMajor()); assertEquals(org.elasticsearch.Version.CURRENT.major, Version.CURRENT.major);
assertEquals(org.elasticsearch.Version.CURRENT.minor, Version.versionMinor()); assertEquals(org.elasticsearch.Version.CURRENT.minor, Version.CURRENT.minor);
} }
} }

View File

@ -15,31 +15,46 @@ import java.util.Set;
import java.util.jar.JarInputStream; import java.util.jar.JarInputStream;
import java.util.jar.Manifest; import java.util.jar.Manifest;
public abstract class Version { public class Version {
private static final String VER;
private static final String SHORT_HASH;
private static final int VER_MAJ, VER_MIN, VER_REV; public static final Version CURRENT;
public final String version;
public final String hash;
public final byte major;
public final byte minor;
public final byte revision;
static int[] from(String ver) { private Version(String version, String hash, byte... parts) {
this.version = version;
this.hash = hash;
this.major = parts[0];
this.minor = parts[1];
this.revision = parts[2];
}
public static Version fromString(String version) {
return new Version(version, "Unknown", from(version));
}
static byte[] from(String ver) {
String[] parts = ver.split("[.-]"); String[] parts = ver.split("[.-]");
if (parts.length == 3 || parts.length == 4) { if (parts.length == 3 || parts.length == 4) {
return new int[] { Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]) }; return new byte[] { Byte.parseByte(parts[0]), Byte.parseByte(parts[1]), Byte.parseByte(parts[2]) };
} }
else { else {
throw new Error("Detected Elasticsearch SQL jar but found invalid version " + ver); throw new IllegalArgumentException("Invalid version " + ver);
} }
} }
static { static {
// check classpath // check classpath
String target = Version.class.getName().replace(".", "/").concat(".class"); String target = Version.class.getName().replace(".", "/").concat(".class");
Enumeration<URL> res = null; Enumeration<URL> res;
try { try {
res = Version.class.getClassLoader().getResources(target); res = Version.class.getClassLoader().getResources(target);
} catch (IOException ex) { } catch (IOException ex) {
throw new Error("Cannot detect Elasticsearch SQL jar; it typically indicates a deployment issue..."); throw new IllegalArgumentException("Cannot detect Elasticsearch SQL jar; it typically indicates a deployment issue...");
} }
if (res != null) { if (res != null) {
@ -62,7 +77,7 @@ public abstract class Version {
} }
} }
if (foundJars > 1) { if (foundJars > 1) {
throw new Error(sb.toString()); throw new IllegalArgumentException(sb.toString());
} }
} }
} }
@ -72,7 +87,7 @@ public abstract class Version {
URL url = Version.class.getProtectionDomain().getCodeSource().getLocation(); URL url = Version.class.getProtectionDomain().getCodeSource().getLocation();
String urlStr = url.toString(); String urlStr = url.toString();
int maj = 0, min = 0, rev = 0; byte maj = 0, min = 0, rev = 0;
String ver = "Unknown"; String ver = "Unknown";
String hash = ver; String hash = ver;
@ -81,43 +96,20 @@ public abstract class Version {
Manifest manifest = jar.getManifest(); Manifest manifest = jar.getManifest();
hash = manifest.getMainAttributes().getValue("Change"); hash = manifest.getMainAttributes().getValue("Change");
ver = manifest.getMainAttributes().getValue("X-Compile-Elasticsearch-Version"); ver = manifest.getMainAttributes().getValue("X-Compile-Elasticsearch-Version");
int[] vers = from(ver); byte[] vers = from(ver);
maj = vers[0]; maj = vers[0];
min = vers[1]; min = vers[1];
rev = vers[2]; rev = vers[2];
} catch (Exception ex) { } catch (Exception ex) {
throw new Error("Detected Elasticsearch SQL jar but cannot retrieve its version", ex); throw new IllegalArgumentException("Detected Elasticsearch SQL jar but cannot retrieve its version", ex);
} }
} }
VER_MAJ = maj; CURRENT = new Version(ver, hash, maj, min, rev);
VER_MIN = min;
VER_REV = rev;
VER = ver;
SHORT_HASH = hash;
} }
public static int versionMajor() { @Override
return VER_MAJ; public String toString() {
} return "v" + version + " [" + hash + "]";
public static int versionMinor() {
return VER_MIN;
}
public static int versionRevision() {
return VER_REV;
}
public static String version() {
return "v" + versionNumber() + " [" + versionHash() + "]";
}
public static String versionNumber() {
return VER;
}
public static String versionHash() {
return SHORT_HASH;
} }
public static int jdbcMajorVersion() { public static int jdbcMajorVersion() {

View File

@ -9,28 +9,37 @@ import org.elasticsearch.test.ESTestCase;
public class VersionTests extends ESTestCase { public class VersionTests extends ESTestCase {
public void test70Version() { public void test70Version() {
int[] ver = Version.from("7.0.0-alpha"); byte[] ver = Version.from("7.0.0-alpha");
assertEquals(7, ver[0]); assertEquals(7, ver[0]);
assertEquals(0, ver[1]); assertEquals(0, ver[1]);
assertEquals(0, ver[2]); assertEquals(0, ver[2]);
} }
public void test712Version() { public void test712Version() {
int[] ver = Version.from("7.1.2"); byte[] ver = Version.from("7.1.2");
assertEquals(7, ver[0]); assertEquals(7, ver[0]);
assertEquals(1, ver[1]); assertEquals(1, ver[1]);
assertEquals(2, ver[2]); assertEquals(2, ver[2]);
} }
public void testCurrent() { public void testCurrent() {
int[] ver = Version.from(org.elasticsearch.Version.CURRENT.toString()); Version ver = Version.fromString(org.elasticsearch.Version.CURRENT.toString());
assertEquals(org.elasticsearch.Version.CURRENT.major, ver[0]); assertEquals(org.elasticsearch.Version.CURRENT.major, ver.major);
assertEquals(org.elasticsearch.Version.CURRENT.minor, ver[1]); assertEquals(org.elasticsearch.Version.CURRENT.minor, ver.minor);
assertEquals(org.elasticsearch.Version.CURRENT.revision, ver[2]); assertEquals(org.elasticsearch.Version.CURRENT.revision, ver.revision);
}
public void testFromString() {
Version ver = Version.fromString("1.2.3");
assertEquals(1, ver.major);
assertEquals(2, ver.minor);
assertEquals(3, ver.revision);
assertEquals("Unknown", ver.hash);
assertEquals("1.2.3", ver.version);
} }
public void testInvalidVersion() { public void testInvalidVersion() {
Error err = expectThrows(Error.class, () -> Version.from("7.1")); IllegalArgumentException err = expectThrows(IllegalArgumentException.class, () -> Version.from("7.1"));
assertEquals("Detected Elasticsearch SQL jar but found invalid version 7.1", err.getMessage()); assertEquals("Invalid version 7.1", err.getMessage());
} }
} }