Skip to content

Commit

Permalink
ESQL: Default ESQL version on old clients (#107438)
Browse files Browse the repository at this point in the history
Soon we will require an ESQL version for all ESQL requests. Kibana is
sending it already. The official clients will start sending it in
version 8.14+. This defaults the version for the official clients before
8.14. It does so by reading a magic header the clients send,
`x-elastic-client-meta` and detecting the version string for any clients
that might possibly support ESQL - `es=8.11`, `es=8.12`, and `es=8.13`.
If we receive that we'll default to the first version of ESQL.

I've also made this work for kibana versions 8.12 and 8.13 - discover
will default to the old version of ESQL.
  • Loading branch information
nik9000 authored Apr 15, 2024
1 parent d2cdee3 commit e306dd1
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
esqlRequest = RequestXContent.parseAsync(parser);
}

RestEsqlQueryAction.defaultVersionForOldClients(esqlRequest, request);
LOGGER.info("Beginning execution of ESQL async query.\nQuery string: [{}]", esqlRequest.query());

return channel -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.elasticsearch.rest.ServerlessScope;
import org.elasticsearch.rest.action.RestCancellableNodeClient;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xpack.esql.version.EsqlVersion;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -45,6 +46,7 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
esqlRequest = RequestXContent.parseSync(parser);
}

defaultVersionForOldClients(esqlRequest, request);
LOGGER.info("Beginning execution of ESQL query.\nQuery string: [{}]", esqlRequest.query());

return channel -> {
Expand All @@ -61,4 +63,41 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
protected Set<String> responseParams() {
return Set.of(URL_PARAM_DELIMITER, EsqlQueryResponse.DROP_NULL_COLUMNS_OPTION);
}

static final String PRODUCT_ORIGIN = "x-elastic-product-origin";
static final String CLIENT_META = "x-elastic-client-meta";

/**
* Default the {@link EsqlQueryRequest#esqlVersion()} to the oldest version
* if we can detect that the request comes from an older version of the
* official client or an older version of kibana. These versions supported
* ESQL but ESQL was not GA, so, <strong>technically</strong> we can break
* them. But it's not hugely complicated to make them work smoothly on the
* upgrade that starts to require the {@code version} field. This does
* just that.
*/
static void defaultVersionForOldClients(EsqlQueryRequest esqlRequest, RestRequest restRequest) {
if (esqlRequest.esqlVersion() != null) {
return;
}
String clientMeta = restRequest.header(CLIENT_META);
if (clientMeta == null) {
return;
}
String product = restRequest.header(PRODUCT_ORIGIN);
if ("kibana".equals(product)) {
/*
* Kibana 8.11 to 8.13 used the 8.9 version of the javascript client.
* Kibana 8.14, the version we *want* to send the versions is on the
* 8.13 version of the javascript client.
*/
if (clientMeta.contains("es=8.9")) {
esqlRequest.esqlVersion(EsqlVersion.ROCKET.versionStringWithoutEmoji());
}
return;
}
if (clientMeta.contains("es=8.13") || clientMeta.contains("es=8.12") || clientMeta.contains("es=8.11")) {
esqlRequest.esqlVersion(EsqlVersion.ROCKET.versionStringWithoutEmoji());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.action;

import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.rest.FakeRestRequest;
import org.elasticsearch.xpack.esql.version.EsqlVersion;
import org.hamcrest.Matcher;

import java.util.List;
import java.util.function.Supplier;

import static org.elasticsearch.xpack.esql.action.RestEsqlQueryAction.CLIENT_META;
import static org.elasticsearch.xpack.esql.action.RestEsqlQueryAction.PRODUCT_ORIGIN;
import static org.elasticsearch.xpack.esql.action.RestEsqlQueryAction.defaultVersionForOldClients;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.nullValue;

public class RestEsqlQueryActionTests extends ESTestCase {
public void testNoVersionForNoClient() {
assertEsqlVersion(null, null, nullValue(String.class));
}

public void testNoVersionForAlreadySet() {
EsqlQueryRequest esqlRequest = new EsqlQueryRequest();
esqlRequest.esqlVersion("whatever");
FakeRestRequest restRequest = new FakeRestRequest();
Supplier<String> version = randomFrom(
() -> "es=8.1" + between(0, 3), // Versions we would rewrite.
() -> "es=8.1" + between(4, 9), // We wouldn't rewrite these anyway, but let's try it sometimes.
() -> "es=8." + between(0, 9) + between(0, 9), // These will rarely spit out versions we would rewrite. Either is fine.
() -> "es=" + between(0, 9) + "." + between(0, 9) + between(0, 9)
);
restRequest.getHttpRequest().getHeaders().put(CLIENT_META, List.of("es=8.13.0"));
defaultVersionForOldClients(esqlRequest, restRequest);
assertThat(esqlRequest.esqlVersion(), equalTo("whatever"));
}

public void testNoVersionForNewClient() {
Supplier<String> version = randomFrom(
() -> "es=8.14",
() -> "es=8.2" + between(0, 9),
() -> "es=8." + between(3, 9) + between(0, 9),
() -> "es=9." + between(0, 9) + between(0, 9),
() -> "es=" + between(0, 9) + between(0, 9) + "." + between(0, 9) + between(0, 9)
);
assertEsqlVersion(version.get(), randomProduct(), nullValue(String.class));
}

public void testAddsVersionForPython813() {
assertAddsOldest(
randomFrom(
"es=8.13.0,py=3.11.8,t=8.13.0,ur=2.2.1", // This is what the python client sent for me on 2024-4-12
"py=3.11.8,es=8.13.0,ur=2.2.1,t=8.13.0", // This is just a jumbled version of the above
"es=8.13" // This is all we need to trigger it
),
randomProduct()
);
}

public void testAddsVersionForPython812() {
assertAddsOldest(
randomFrom(
"es=8.12.0,py=3.11.8,t=8.13.0,ur=2.2.1", // This is what the python client sent for me on 2024-4-12
"py=3.11.8,t=8.13.0,es=8.12.0,ur=2.2.1", // This is just a jumbled version of the above
"es=8.12" // This is all we need to trigger it
),
randomProduct()
);
}

public void testNoVersionForKibana814() {
assertEsqlVersion("es=8.13", "kibana", nullValue(String.class));
}

public void testAddsVersionForKibana813() {
assertAddsOldest(
randomFrom(
"es=8.9.1p,js=20.12.2,t=8.3.3,hc=20.12.2", // This is what kibana sent on 2024-4-12
"js=20.12.2,es=8.9.1p,t=8.3.3,hc=20.12.2", // This is just a jumbled version of the above
"es=8.9" // This is all we need to trigger it
),
"kibana"
);
}

public void testAddsVersionForKibana812() {
assertAddsOldest(
randomFrom(
"es=8.9.1p,js=18.19.1,t=8.3.3,hc=18.19.1", // This is what kibana sent on 2024-4-12
"js=18.19.1,t=8.3.3,es=8.9.1p,hc=18.19.1", // This is just a jumbled version of the above
"es=8.9" // This is all we need to trigger it
),
"kibana"
);
}

public void testAddsVersionForKibana811() {
assertAddsOldest(
randomFrom(
"es=8.9.1p,js=18.18.2,t=8.3.3,hc=18.18.2", // This is what kibana sent on 2024-4-12
"js=18.18.2,es=8.9.1p,t=8.3.3,hc=18.18.2", // This is just a jumbled version of the above
"es=8.9" // This is all we need to trigger it
),
"kibana"
);
}

private void assertAddsOldest(String clientMeta, String elasticProductOrigin) {
assertEsqlVersion(clientMeta, elasticProductOrigin, equalTo(EsqlVersion.ROCKET.versionStringWithoutEmoji()));
}

private void assertEsqlVersion(String clientMeta, String elasticProductOrigin, Matcher<String> expectedEsqlVersion) {
EsqlQueryRequest esqlRequest = new EsqlQueryRequest();
FakeRestRequest restRequest = new FakeRestRequest();
if (clientMeta != null) {
restRequest.getHttpRequest().getHeaders().put(CLIENT_META, List.of(clientMeta));
}
if (elasticProductOrigin != null) {
restRequest.getHttpRequest().getHeaders().put(PRODUCT_ORIGIN, List.of(elasticProductOrigin));
}
defaultVersionForOldClients(esqlRequest, restRequest);
assertThat(esqlRequest.esqlVersion(), expectedEsqlVersion);
}

/**
* Returns {@code null} or a random string that <strong>isn't</strong> {@code kibana}.
*/
private String randomProduct() {
return randomBoolean() ? null : randomAlphaOfLength(3);
}
}

0 comments on commit e306dd1

Please sign in to comment.