-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ESQL: Default ESQL version on old clients (#107438)
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
Showing
3 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
...ugin/esql/src/test/java/org/elasticsearch/xpack/esql/action/RestEsqlQueryActionTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |