Skip to content

Commit

Permalink
SQL: Remove dependency on org.elasticsearch.Version (elastic#112094)
Browse files Browse the repository at this point in the history
This removes SQL's use of `org.elasticsearch.Version` class and usages replaced by `SqlVersion`.

All the currently considered released versions (`7.0.0` to `8.16.0`) have been declared as `SqlVersion` instances. These are still tested against.

The last "known release" (`8.16.0`) is considered the "server compatibility version" and all clients at or past this release are compatible with the server; notably, they can be also on a newer version than server's. 
Clients released before this "server compatibility version" respect existing compatibility requirements (must/can be older up to one major lower, but past `7.7.0`).
The "server compatibility version" will not be updated with newer stack releases (at least not until elastic#112745 is addressed).

Fixes elastic#102689
  • Loading branch information
bpintea authored Oct 11, 2024
1 parent ddd576e commit e56f24f
Show file tree
Hide file tree
Showing 43 changed files with 593 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class EsDataSource implements DataSource, Wrapper {

static {
// invoke Version to perform classpath/jar sanity checks
// invoke version to perform classpath/jar sanity checks
ClientVersion.CURRENT.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class EsDriver implements Driver {
private static final EsDriver INSTANCE = new EsDriver();

static {
// invoke Version to perform classpath/jar sanity checks
// invoke version to perform classpath/jar sanity checks
ClientVersion.CURRENT.toString();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package org.elasticsearch.xpack.sql.jdbc;

import org.elasticsearch.Version;
import org.elasticsearch.test.ESTestCase;

import java.security.AccessController;
Expand All @@ -27,8 +26,8 @@ public void testVersioning() throws Exception {
/* This test will only work properly in gradle because in gradle we run the tests
* using the jar. */

assertNotEquals(String.valueOf(Version.CURRENT.major), d.getMajorVersion());
assertNotEquals(String.valueOf(Version.CURRENT.minor), d.getMinorVersion());
assertNotEquals(String.valueOf(VersionTests.current().major), d.getMajorVersion());
assertNotEquals(String.valueOf(VersionTests.current().minor), d.getMinorVersion());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@

package org.elasticsearch.xpack.sql.jdbc;

import org.elasticsearch.Version;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.rest.root.MainResponse;
import org.elasticsearch.test.VersionUtils;
import org.elasticsearch.test.http.MockResponse;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xpack.sql.client.ClientVersion;
import org.elasticsearch.xpack.sql.proto.SqlVersion;
import org.elasticsearch.xpack.sql.proto.SqlVersions;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static org.elasticsearch.xpack.sql.proto.VersionCompatibility.INTRODUCING_VERSION_COMPATIBILITY;

/**
* Test class for JDBC-ES server versions checks.
Expand All @@ -29,15 +33,11 @@ public class VersionParityTests extends WebServerTestCase {

public void testExceptionThrownOnIncompatibleVersions() throws IOException, SQLException {
String url = JdbcConfiguration.URL_PREFIX + webServerAddress();
Version firstVersion = VersionUtils.getFirstVersion();
Version version = Version.V_7_7_0;
do {
version = VersionUtils.getPreviousVersion(version);
for (var version = SqlVersions.getFirstVersion(); version.onOrAfter(INTRODUCING_VERSION_COMPATIBILITY) == false; version =
SqlVersions.getNextVersion(version)) {
logger.info("Checking exception is thrown for version {}", version);

prepareResponse(version);
// Client's version is wired up to patch level, excluding the qualifier => generate the test version as the server does it.
String versionString = SqlVersion.fromString(version.toString()).toString();

SQLException ex = expectThrows(
SQLException.class,
Expand All @@ -48,27 +48,30 @@ public void testExceptionThrownOnIncompatibleVersions() throws IOException, SQLE
+ ClientVersion.CURRENT.majorMinorToString()
+ " or newer; attempting to connect to a server "
+ "version "
+ versionString,
+ version,
ex.getMessage()
);
} while (version.compareTo(firstVersion) > 0);
}
}

public void testNoExceptionThrownForCompatibleVersions() throws IOException {
String url = JdbcConfiguration.URL_PREFIX + webServerAddress();
Version version = Version.CURRENT;
try {
do {
List<SqlVersion> afterVersionCompatibility = SqlVersions.getAllVersions()
.stream()
.filter(v -> v.onOrAfter(INTRODUCING_VERSION_COMPATIBILITY))
.collect(Collectors.toCollection(ArrayList::new));
afterVersionCompatibility.add(VersionTests.current());
for (var version : afterVersionCompatibility) {
try {
prepareResponse(version);
new JdbcHttpClient(new JdbcConnection(JdbcConfiguration.create(url, null, 0), false));
version = VersionUtils.getPreviousVersion(version);
} while (version.compareTo(Version.V_7_7_0) >= 0);
} catch (SQLException sqle) {
fail("JDBC driver version and Elasticsearch server version should be compatible. Error: " + sqle);
} catch (SQLException sqle) {
fail("JDBC driver version and Elasticsearch server version should be compatible. Error: " + sqle);
}
}
}

void prepareResponse(Version version) throws IOException {
void prepareResponse(SqlVersion version) throws IOException {
MainResponse response = version == null ? createCurrentVersionMainResponse() : createMainResponse(version);
webServer().enqueue(
new MockResponse().setResponseCode(200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
*/
package org.elasticsearch.xpack.sql.jdbc;

import org.elasticsearch.Build;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.sql.client.ClientVersion;
import org.elasticsearch.xpack.sql.proto.SqlVersion;

public class VersionTests extends ESTestCase {
public void testVersionIsCurrent() {
/* This test will only work properly in gradle because in gradle we run the tests
* using the jar. */
assertEquals(org.elasticsearch.Version.CURRENT.major, ClientVersion.CURRENT.major);
assertEquals(org.elasticsearch.Version.CURRENT.minor, ClientVersion.CURRENT.minor);
assertEquals(org.elasticsearch.Version.CURRENT.revision, ClientVersion.CURRENT.revision);
assertEquals(current(), ClientVersion.CURRENT);
}

/** Returns the current stack version. Can be unreleased. */
public static SqlVersion current() {
return SqlVersion.fromString(Build.current().version());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
package org.elasticsearch.xpack.sql.jdbc;

import org.elasticsearch.Build;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.rest.root.MainResponse;
import org.elasticsearch.test.BuildUtils;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.http.MockWebServer;
import org.elasticsearch.xpack.sql.proto.SqlVersion;
import org.junit.After;
import org.junit.Before;

Expand Down Expand Up @@ -42,10 +42,10 @@ public MockWebServer webServer() {
}

MainResponse createCurrentVersionMainResponse() {
return createMainResponse(Version.CURRENT);
return createMainResponse(VersionTests.current());
}

MainResponse createMainResponse(Version version) {
MainResponse createMainResponse(SqlVersion version) {
// the SQL client only cares about node version,
// so ignore index & transport versions here (just set them to current)
String clusterUuid = randomAlphaOfLength(10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
*/
package org.elasticsearch.xpack.sql.qa.jdbc;

import org.elasticsearch.Version;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
Expand All @@ -22,8 +20,8 @@ public void testConnectionProperties() throws SQLException {
assertFalse(c.isClosed());
assertTrue(c.isReadOnly());
DatabaseMetaData md = c.getMetaData();
assertEquals(Version.CURRENT.major, md.getDatabaseMajorVersion());
assertEquals(Version.CURRENT.minor, md.getDatabaseMinorVersion());
assertEquals(JdbcTestUtils.CURRENT.major, md.getDatabaseMajorVersion());
assertEquals(JdbcTestUtils.CURRENT.minor, md.getDatabaseMinorVersion());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*/
package org.elasticsearch.xpack.sql.qa.jdbc;

import org.elasticsearch.Version;
import org.elasticsearch.Build;
import org.elasticsearch.xpack.sql.jdbc.EsType;
import org.elasticsearch.xpack.sql.proto.SqlVersion;
import org.elasticsearch.xpack.sql.proto.StringUtils;

import java.math.BigInteger;
Expand All @@ -26,10 +27,11 @@
import java.util.LinkedHashMap;
import java.util.Map;

import static org.elasticsearch.Version.V_8_2_0;
import static org.elasticsearch.Version.V_8_4_0;
import static org.elasticsearch.common.time.DateUtils.toMilliSeconds;
import static org.elasticsearch.test.ESTestCase.randomLongBetween;
import static org.elasticsearch.xpack.sql.proto.VersionCompatibility.supportsDateNanos;
import static org.elasticsearch.xpack.sql.proto.VersionCompatibility.supportsUnsignedLong;
import static org.elasticsearch.xpack.sql.proto.VersionCompatibility.supportsVersionType;

final class JdbcTestUtils {

Expand All @@ -43,7 +45,9 @@ private JdbcTestUtils() {}
static final LocalDate EPOCH = LocalDate.of(1970, 1, 1);

static final String UNSIGNED_LONG_TYPE_NAME = "UNSIGNED_LONG";
static final BigInteger UNSIGNED_LONG_MAX = BigInteger.ONE.shiftLeft(Long.SIZE).subtract(BigInteger.ONE);

// Build's version is always a SemVer in JDBC tests
public static final SqlVersion CURRENT = SqlVersion.fromString(Build.current().version());

/*
* The version of the driver that the QA (bwc-)tests run against.
Expand All @@ -59,12 +63,11 @@ private JdbcTestUtils() {}
* }
* </code>
*/
static final Version JDBC_DRIVER_VERSION;
static final SqlVersion JDBC_DRIVER_VERSION;

static {
// master's version is x.0.0-SNAPSHOT, tho Version#fromString() won't accept that back for recent versions
String jdbcDriverVersion = System.getProperty(DRIVER_VERSION_PROPERTY_NAME, "").replace("-SNAPSHOT", "");
JDBC_DRIVER_VERSION = Version.fromString(jdbcDriverVersion); // takes empty and null strings, resolves them to CURRENT
String jdbcDriverVersion = System.getProperty(DRIVER_VERSION_PROPERTY_NAME, "");
JDBC_DRIVER_VERSION = jdbcDriverVersion.isEmpty() ? CURRENT : SqlVersion.fromString(jdbcDriverVersion);

// Note: keep in sync with org.elasticsearch.xpack.sql.jdbc.TypeUtils#CLASS_TO_TYPE
Map<Class<?>, EsType> aMap = new LinkedHashMap<>();
Expand Down Expand Up @@ -150,15 +153,14 @@ static int extractNanosOnly(long nanos) {
}

static boolean versionSupportsDateNanos() {
return JDBC_DRIVER_VERSION.onOrAfter(Version.V_7_12_0);
return supportsDateNanos(JDBC_DRIVER_VERSION);
}

public static boolean isUnsignedLongSupported() {
return JDBC_DRIVER_VERSION.onOrAfter(V_8_2_0);
return supportsUnsignedLong(JDBC_DRIVER_VERSION);
}

public static boolean isVersionFieldTypeSupported() {
return JDBC_DRIVER_VERSION.onOrAfter(V_8_4_0);
return supportsVersionType(JDBC_DRIVER_VERSION);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package org.elasticsearch.xpack.sql.qa.jdbc;

import org.elasticsearch.Version;
import org.junit.Before;

import java.io.IOException;
Expand All @@ -20,14 +19,13 @@
import java.util.List;
import java.util.Properties;

import static org.elasticsearch.xpack.sql.proto.VersionCompatibility.INTRODUCING_WARNING_HANDLING;
import static org.elasticsearch.xpack.sql.qa.jdbc.JdbcTestUtils.JDBC_DRIVER_VERSION;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;

public abstract class JdbcWarningsTestCase extends JdbcIntegrationTestCase {

private static final Version WARNING_HANDLING_ADDED_VERSION = Version.V_8_2_0;

@Before
public void setupData() throws IOException {
index("test_data", b -> b.field("foo", 1));
Expand Down Expand Up @@ -89,7 +87,7 @@ public void testClearWarnings() throws SQLException {
}

private void assumeWarningHandlingDriverVersion() {
assumeTrue("Driver does not yet handle deprecation warnings", JDBC_DRIVER_VERSION.onOrAfter(WARNING_HANDLING_ADDED_VERSION));
assumeTrue("Driver does not yet handle deprecation warnings", JDBC_DRIVER_VERSION.onOrAfter(INTRODUCING_WARNING_HANDLING));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import org.apache.http.HttpHost;
import org.apache.lucene.sandbox.document.HalfFloatPoint;
import org.elasticsearch.Version;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
Expand All @@ -19,6 +18,7 @@
import org.elasticsearch.xcontent.json.JsonXContent;
import org.elasticsearch.xpack.ql.TestNode;
import org.elasticsearch.xpack.ql.TestNodes;
import org.elasticsearch.xpack.sql.proto.SqlVersion;
import org.junit.After;
import org.junit.Before;

Expand All @@ -36,13 +36,14 @@
import static java.util.Collections.unmodifiableMap;
import static org.elasticsearch.xpack.ql.TestUtils.buildNodeAndVersions;
import static org.elasticsearch.xpack.ql.TestUtils.readResource;
import static org.elasticsearch.xpack.sql.proto.VersionCompatibility.INTRODUCING_VERSION_FIELD_TYPE;

public class SqlSearchIT extends ESRestTestCase {

private static final String BWC_NODES_VERSION = System.getProperty("tests.bwc_nodes_version");

// TODO[lor]: replace this with feature-based checks when we have one
private static final boolean SUPPORTS_VERSION_FIELD_QL_INTRODUCTION = Version.fromString(BWC_NODES_VERSION).onOrAfter(Version.V_8_4_0);
private static final boolean SUPPORTS_VERSION_FIELD_QL_INTRODUCTION = SqlVersion.fromString(BWC_NODES_VERSION)
.onOrAfter(INTRODUCING_VERSION_FIELD_TYPE);

private static final String index = "test_sql_mixed_versions";
private static int numShards;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.elasticsearch.Version;
import org.elasticsearch.Build;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
Expand All @@ -20,6 +20,8 @@
import org.elasticsearch.xcontent.cbor.CborXContent;
import org.elasticsearch.xcontent.json.JsonXContent;
import org.elasticsearch.xpack.sql.proto.Mode;
import org.elasticsearch.xpack.sql.proto.SqlVersion;
import org.elasticsearch.xpack.sql.proto.SqlVersions;
import org.elasticsearch.xpack.sql.proto.StringUtils;

import java.io.IOException;
Expand Down Expand Up @@ -54,6 +56,11 @@ public abstract class BaseRestSqlTestCase extends RemoteClusterAwareSqlRestTestC

private static final String TEST_INDEX = "test";
private static final String DATA_STREAM_TEMPLATE = "test-ds-index-template";
/**
* What's the version of the server that the clients should be compatible with?
* This will be either the stack version, or SqlVersions.getLatestVersion() if the stack version is not available.
*/
private static final SqlVersion SERVER_COMPAT_VERSION = getServerCompatVersion();

public static class RequestObjectBuilder {
private StringBuilder request;
Expand Down Expand Up @@ -83,7 +90,7 @@ public RequestObjectBuilder mode(Object m) {
if (isQuery) {
Mode mode = (m instanceof Mode) ? (Mode) m : Mode.fromString(modeString);
if (Mode.isDedicatedClient(mode)) {
version(Version.CURRENT.toString());
version(SERVER_COMPAT_VERSION.toString());
}
}
return this;
Expand Down Expand Up @@ -301,4 +308,12 @@ public static Tuple<String, String> runSqlAsText(RequestObjectBuilder requestObj
response.getHeader("Cursor")
);
}

private static SqlVersion getServerCompatVersion() {
try {
return SqlVersion.fromString(Build.current().version());
} catch (Exception e) {
return SqlVersions.getLatestVersion();
}
}
}
Loading

0 comments on commit e56f24f

Please sign in to comment.