Skip to content

Commit 3cccc98

Browse files
committed
Merge remote-tracking branch 'es/6.x' into ccr-6.x
* es/6.x: Use correct formatting for links (#29460) Revert "Adds a new auto-interval date histogram (#28993)" Revert "fix typo" fix typo Adds a new auto-interval date histogram (#28993) [Rollup] Replace RollupIT with a ESRestTestCase version (#31977) [Rollup] Fix duplicate field names in test (#32075) [Tests] Fix failure due to changes exception message (#32036) [Test] Mute MlJobIT#testDeleteJobAfterMissingAliases Replace Ingest ScriptContext with Custom Interface (#32003) (#32060) Cleanup Duplication in `PainlessScriptEngine` (#31991) (#32061) HLRC: Add xpack usage api (#31975) Clean Up Snapshot Create Rest API (#31779)
2 parents 0a90962 + 9036c61 commit 3cccc98

File tree

39 files changed

+735
-736
lines changed

39 files changed

+735
-736
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/RequestConverters.java

+8
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
import org.elasticsearch.index.VersionType;
106106
import org.elasticsearch.index.rankeval.RankEvalRequest;
107107
import org.elasticsearch.protocol.xpack.XPackInfoRequest;
108+
import org.elasticsearch.protocol.xpack.XPackUsageRequest;
108109
import org.elasticsearch.rest.action.search.RestSearchAction;
109110
import org.elasticsearch.script.mustache.MultiSearchTemplateRequest;
110111
import org.elasticsearch.script.mustache.SearchTemplateRequest;
@@ -1092,6 +1093,13 @@ static Request xPackInfo(XPackInfoRequest infoRequest) {
10921093
return request;
10931094
}
10941095

1096+
static Request xpackUsage(XPackUsageRequest usageRequest) {
1097+
Request request = new Request(HttpGet.METHOD_NAME, "/_xpack/usage");
1098+
Params parameters = new Params(request);
1099+
parameters.withMasterTimeout(usageRequest.masterNodeTimeout());
1100+
return request;
1101+
}
1102+
10951103
private static HttpEntity createEntity(ToXContent toXContent, XContentType xContentType) throws IOException {
10961104
BytesRef source = XContentHelper.toXContent(toXContent, xContentType, false).toBytesRef();
10971105
return new ByteArrayEntity(source.bytes, source.offset, source.length, createContentType(xContentType));

client/rest-high-level/src/main/java/org/elasticsearch/client/SnapshotClient.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public void verifyRepositoryAsync(VerifyRepositoryRequest verifyRepositoryReques
174174
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.html"> Snapshot and Restore
175175
* API on elastic.co</a>
176176
*/
177-
public CreateSnapshotResponse createSnapshot(CreateSnapshotRequest createSnapshotRequest, RequestOptions options)
177+
public CreateSnapshotResponse create(CreateSnapshotRequest createSnapshotRequest, RequestOptions options)
178178
throws IOException {
179179
return restHighLevelClient.performRequestAndParseEntity(createSnapshotRequest, RequestConverters::createSnapshot, options,
180180
CreateSnapshotResponse::fromXContent, emptySet());
@@ -186,7 +186,7 @@ public CreateSnapshotResponse createSnapshot(CreateSnapshotRequest createSnapsho
186186
* See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-snapshots.html"> Snapshot and Restore
187187
* API on elastic.co</a>
188188
*/
189-
public void createSnapshotAsync(CreateSnapshotRequest createSnapshotRequest, RequestOptions options,
189+
public void createAsync(CreateSnapshotRequest createSnapshotRequest, RequestOptions options,
190190
ActionListener<CreateSnapshotResponse> listener) {
191191
restHighLevelClient.performRequestAsyncAndParseEntity(createSnapshotRequest, RequestConverters::createSnapshot, options,
192192
CreateSnapshotResponse::fromXContent, listener, emptySet());

client/rest-high-level/src/main/java/org/elasticsearch/client/XPackClient.java

+23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.elasticsearch.action.ActionListener;
2323
import org.elasticsearch.protocol.xpack.XPackInfoRequest;
2424
import org.elasticsearch.protocol.xpack.XPackInfoResponse;
25+
import org.elasticsearch.protocol.xpack.XPackUsageRequest;
26+
import org.elasticsearch.protocol.xpack.XPackUsageResponse;
2527

2628
import java.io.IOException;
2729

@@ -70,4 +72,25 @@ public void infoAsync(XPackInfoRequest request, RequestOptions options,
7072
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::xPackInfo, options,
7173
XPackInfoResponse::fromXContent, listener, emptySet());
7274
}
75+
76+
/**
77+
* Fetch usage information about X-Pack features from the cluster.
78+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
79+
* @return the response
80+
* @throws IOException in case there is a problem sending the request or parsing back the response
81+
*/
82+
public XPackUsageResponse usage(XPackUsageRequest request, RequestOptions options) throws IOException {
83+
return restHighLevelClient.performRequestAndParseEntity(request, RequestConverters::xpackUsage, options,
84+
XPackUsageResponse::fromXContent, emptySet());
85+
}
86+
87+
/**
88+
* Asynchronously fetch usage information about X-Pack features from the cluster.
89+
* @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized
90+
* @param listener the listener to be notified upon request completion
91+
*/
92+
public void usageAsync(XPackUsageRequest request, RequestOptions options, ActionListener<XPackUsageResponse> listener) {
93+
restHighLevelClient.performRequestAsyncAndParseEntity(request, RequestConverters::xpackUsage, options,
94+
XPackUsageResponse::fromXContent, listener, emptySet());
95+
}
7396
}

client/rest-high-level/src/test/java/org/elasticsearch/client/SnapshotIT.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ private PutRepositoryResponse createTestRepository(String repository, String typ
5757
private CreateSnapshotResponse createTestSnapshot(CreateSnapshotRequest createSnapshotRequest) throws IOException {
5858
// assumes the repository already exists
5959

60-
return execute(createSnapshotRequest, highLevelClient().snapshot()::createSnapshot,
61-
highLevelClient().snapshot()::createSnapshotAsync);
60+
return execute(createSnapshotRequest, highLevelClient().snapshot()::create,
61+
highLevelClient().snapshot()::createAsync);
6262
}
6363

6464
public void testCreateRepository() throws IOException {

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/MiscellaneousDocumentationIT.java

+49
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,17 @@
3535
import org.elasticsearch.protocol.xpack.XPackInfoResponse.BuildInfo;
3636
import org.elasticsearch.protocol.xpack.XPackInfoResponse.FeatureSetsInfo;
3737
import org.elasticsearch.protocol.xpack.XPackInfoResponse.LicenseInfo;
38+
import org.elasticsearch.protocol.xpack.XPackUsageRequest;
39+
import org.elasticsearch.protocol.xpack.XPackUsageResponse;
3840

3941
import java.io.IOException;
4042
import java.util.EnumSet;
43+
import java.util.Map;
4144
import java.util.concurrent.CountDownLatch;
4245
import java.util.concurrent.TimeUnit;
4346

47+
import static org.hamcrest.Matchers.is;
48+
4449
/**
4550
* Documentation for miscellaneous APIs in the high level java client.
4651
* Code wrapped in {@code tag} and {@code end} tags is included in the docs.
@@ -130,6 +135,50 @@ public void onFailure(Exception e) {
130135
}
131136
}
132137

138+
public void testXPackUsage() throws Exception {
139+
RestHighLevelClient client = highLevelClient();
140+
{
141+
//tag::x-pack-usage-execute
142+
XPackUsageRequest request = new XPackUsageRequest();
143+
XPackUsageResponse response = client.xpack().usage(request, RequestOptions.DEFAULT);
144+
//end::x-pack-usage-execute
145+
146+
//tag::x-pack-usage-response
147+
Map<String, Map<String, Object>> usages = response.getUsages();
148+
Map<String, Object> monitoringUsage = usages.get("monitoring");
149+
assertThat(monitoringUsage.get("available"), is(true));
150+
assertThat(monitoringUsage.get("enabled"), is(true));
151+
assertThat(monitoringUsage.get("collection_enabled"), is(false));
152+
//end::x-pack-usage-response
153+
}
154+
{
155+
XPackUsageRequest request = new XPackUsageRequest();
156+
// tag::x-pack-usage-execute-listener
157+
ActionListener<XPackUsageResponse> listener = new ActionListener<XPackUsageResponse>() {
158+
@Override
159+
public void onResponse(XPackUsageResponse response) {
160+
// <1>
161+
}
162+
163+
@Override
164+
public void onFailure(Exception e) {
165+
// <2>
166+
}
167+
};
168+
// end::x-pack-usage-execute-listener
169+
170+
// Replace the empty listener by a blocking listener in test
171+
final CountDownLatch latch = new CountDownLatch(1);
172+
listener = new LatchedActionListener<>(listener, latch);
173+
174+
// tag::x-pack-usage-execute-async
175+
client.xpack().usageAsync(request, RequestOptions.DEFAULT, listener); // <1>
176+
// end::x-pack-usage-execute-async
177+
178+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
179+
}
180+
}
181+
133182
public void testInitializationFromClientBuilder() throws IOException {
134183
//tag::rest-high-level-client-init
135184
RestHighLevelClient client = new RestHighLevelClient(

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SearchDocumentationIT.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,9 @@ public void testSearchRequestAggregations() throws IOException {
337337
Range range = aggregations.get("by_company"); // <1>
338338
// end::search-request-aggregations-get-wrongCast
339339
} catch (ClassCastException ex) {
340-
assertEquals("org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms"
341-
+ " cannot be cast to org.elasticsearch.search.aggregations.bucket.range.Range", ex.getMessage());
340+
String message = ex.getMessage();
341+
assertThat(message, containsString("org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms"));
342+
assertThat(message, containsString("org.elasticsearch.search.aggregations.bucket.range.Range"));
342343
}
343344
assertEquals(3, elasticBucket.getDocCount());
344345
assertEquals(30, avg, 0.0);

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SnapshotClientDocumentationIT.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,20 @@ public void testSnapshotCreate() throws IOException {
420420
// end::create-snapshot-request-waitForCompletion
421421

422422
// tag::create-snapshot-execute
423-
CreateSnapshotResponse response = client.snapshot().createSnapshot(request, RequestOptions.DEFAULT);
423+
CreateSnapshotResponse response = client.snapshot().create(request, RequestOptions.DEFAULT);
424424
// end::create-snapshot-execute
425425

426426
// tag::create-snapshot-response
427427
RestStatus status = response.status(); // <1>
428428
// end::create-snapshot-response
429429

430430
assertEquals(RestStatus.OK, status);
431+
432+
// tag::create-snapshot-response-snapshot-info
433+
SnapshotInfo snapshotInfo = response.getSnapshotInfo(); // <1>
434+
// end::create-snapshot-response-snapshot-info
435+
436+
assertNotNull(snapshotInfo);
431437
}
432438

433439
public void testSnapshotCreateAsync() throws InterruptedException {
@@ -455,7 +461,7 @@ public void onFailure(Exception exception) {
455461
listener = new LatchedActionListener<>(listener, latch);
456462

457463
// tag::create-snapshot-execute-async
458-
client.snapshot().createSnapshotAsync(request, RequestOptions.DEFAULT, listener); // <1>
464+
client.snapshot().createAsync(request, RequestOptions.DEFAULT, listener); // <1>
459465
// end::create-snapshot-execute-async
460466

461467
assertTrue(latch.await(30L, TimeUnit.SECONDS));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[[java-rest-high-x-pack-usage]]
2+
=== X-Pack Usage API
3+
4+
[[java-rest-high-x-pack-usage-execution]]
5+
==== Execution
6+
7+
Detailed information about the usage of features from {xpack} can be
8+
retrieved using the `usage()` method:
9+
10+
["source","java",subs="attributes,callouts,macros"]
11+
--------------------------------------------------
12+
include-tagged::{doc-tests}/MiscellaneousDocumentationIT.java[x-pack-usage-execute]
13+
--------------------------------------------------
14+
15+
[[java-rest-high-x-pack-info-response]]
16+
==== Response
17+
18+
The returned `XPackUsageResponse` contains a `Map` keyed by feature name.
19+
Every feature map has an `available` key, indicating whether that
20+
feature is available given the current license, and an `enabled` key,
21+
indicating whether that feature is currently enabled. Other keys
22+
are specific to each feature.
23+
24+
["source","java",subs="attributes,callouts,macros"]
25+
--------------------------------------------------
26+
include-tagged::{doc-tests}/MiscellaneousDocumentationIT.java[x-pack-usage-response]
27+
--------------------------------------------------
28+
29+
[[java-rest-high-x-pack-usage-async]]
30+
==== Asynchronous Execution
31+
32+
This request can be executed asynchronously:
33+
34+
["source","java",subs="attributes,callouts,macros"]
35+
--------------------------------------------------
36+
include-tagged::{doc-tests}/MiscellaneousDocumentationIT.java[x-pack-usage-execute-async]
37+
--------------------------------------------------
38+
<1> The call to execute the usage api and the `ActionListener` to use when
39+
the execution completes
40+
41+
The asynchronous method does not block and returns immediately. Once it is
42+
completed the `ActionListener` is called back using the `onResponse` method
43+
if the execution successfully completed or using the `onFailure` method if
44+
it failed.
45+
46+
A typical listener for `XPackUsageResponse` looks like:
47+
48+
["source","java",subs="attributes,callouts,macros"]
49+
--------------------------------------------------
50+
include-tagged::{doc-tests}/MiscellaneousDocumentationIT.java[x-pack-usage-execute-listener]
51+
--------------------------------------------------
52+
<1> Called when the execution is successfully completed. The response is
53+
provided as an argument
54+
<2> Called in case of failure. The raised exception is provided as an argument

docs/java-rest/high-level/snapshot/create_snapshot.asciidoc

+11
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,22 @@ include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[create-snapshot-r
7373
[[java-rest-high-snapshot-create-snapshot-sync]]
7474
==== Synchronous Execution
7575

76+
Execute a `CreateSnapshotRequest` synchronously to receive a `CreateSnapshotResponse`.
77+
7678
["source","java",subs="attributes,callouts,macros"]
7779
--------------------------------------------------
7880
include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[create-snapshot-execute]
7981
--------------------------------------------------
8082

83+
Retrieve the `SnapshotInfo` from a `CreateSnapshotResponse` when the snapshot is fully created.
84+
(The `waitForCompletion` parameter is `true`).
85+
86+
["source","java",subs="attributes,callouts,macros"]
87+
--------------------------------------------------
88+
include-tagged::{doc-tests}/SnapshotClientDocumentationIT.java[create-snapshot-response-snapshot-info]
89+
--------------------------------------------------
90+
<1> The `SnapshotInfo` object.
91+
8192
[[java-rest-high-snapshot-create-snapshot-async]]
8293
==== Asynchronous Execution
8394

docs/reference/how-to/recipes.asciidoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
This section includes a few recipes to help with common problems:
55

6-
* mixing-exact-search-with-stemming
7-
* consistent-scoring
6+
* <<mixing-exact-search-with-stemming>>
7+
* <<consistent-scoring>>
88

99
include::recipes/stemming.asciidoc[]
1010
include::recipes/scoring.asciidoc[]

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ScriptProcessor.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.elasticsearch.ingest.AbstractProcessor;
2727
import org.elasticsearch.ingest.IngestDocument;
2828
import org.elasticsearch.ingest.Processor;
29-
import org.elasticsearch.script.ExecutableScript;
29+
import org.elasticsearch.script.IngestScript;
3030
import org.elasticsearch.script.Script;
3131
import org.elasticsearch.script.ScriptException;
3232
import org.elasticsearch.script.ScriptService;
@@ -71,10 +71,8 @@ public final class ScriptProcessor extends AbstractProcessor {
7171
*/
7272
@Override
7373
public void execute(IngestDocument document) {
74-
ExecutableScript.Factory factory = scriptService.compile(script, ExecutableScript.INGEST_CONTEXT);
75-
ExecutableScript executableScript = factory.newInstance(script.getParams());
76-
executableScript.setNextVar("ctx", document.getSourceAndMetadata());
77-
executableScript.run();
74+
IngestScript.Factory factory = scriptService.compile(script, IngestScript.CONTEXT);
75+
factory.newInstance(script.getParams()).execute(document.getSourceAndMetadata());
7876
}
7977

8078
@Override
@@ -144,7 +142,7 @@ public ScriptProcessor create(Map<String, Processor.Factory> registry, String pr
144142

145143
// verify script is able to be compiled before successfully creating processor.
146144
try {
147-
scriptService.compile(script, ExecutableScript.INGEST_CONTEXT);
145+
scriptService.compile(script, IngestScript.CONTEXT);
148146
} catch (ScriptException e) {
149147
throw newConfigurationException(TYPE, processorTag, scriptPropertyUsed, e);
150148
}

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/IngestRestartIT.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ protected boolean ignoreExternalCluster() {
5858
public static class CustomScriptPlugin extends MockScriptPlugin {
5959
@Override
6060
protected Map<String, Function<Map<String, Object>, Object>> pluginScripts() {
61-
return Collections.singletonMap("my_script", script -> {
62-
@SuppressWarnings("unchecked")
63-
Map<String, Object> ctx = (Map) script.get("ctx");
61+
return Collections.singletonMap("my_script", ctx -> {
6462
ctx.put("z", 0);
6563
return null;
6664
});

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ScriptProcessorTests.java

+21-17
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,51 @@
1919

2020
package org.elasticsearch.ingest.common;
2121

22+
import java.util.Collections;
2223
import java.util.HashMap;
2324
import java.util.Map;
2425

26+
import org.elasticsearch.common.settings.Settings;
2527
import org.elasticsearch.ingest.IngestDocument;
2628
import org.elasticsearch.ingest.RandomDocumentPicks;
27-
import org.elasticsearch.script.ExecutableScript;
29+
import org.elasticsearch.script.MockScriptEngine;
2830
import org.elasticsearch.script.Script;
31+
import org.elasticsearch.script.ScriptModule;
2932
import org.elasticsearch.script.ScriptService;
33+
import org.elasticsearch.script.ScriptType;
3034
import org.elasticsearch.test.ESTestCase;
3135

3236
import static org.hamcrest.Matchers.hasKey;
3337
import static org.hamcrest.core.Is.is;
34-
import static org.mockito.Mockito.any;
35-
import static org.mockito.Mockito.doAnswer;
36-
import static org.mockito.Mockito.mock;
37-
import static org.mockito.Mockito.when;
3838

3939
public class ScriptProcessorTests extends ESTestCase {
4040

4141
public void testScripting() throws Exception {
4242
int randomBytesIn = randomInt();
4343
int randomBytesOut = randomInt();
4444
int randomBytesTotal = randomBytesIn + randomBytesOut;
45-
46-
ScriptService scriptService = mock(ScriptService.class);
47-
Script script = mockScript("_script");
48-
ExecutableScript.Factory factory = mock(ExecutableScript.Factory.class);
49-
ExecutableScript executableScript = mock(ExecutableScript.class);
50-
when(scriptService.compile(script, ExecutableScript.INGEST_CONTEXT)).thenReturn(factory);
51-
when(factory.newInstance(any())).thenReturn(executableScript);
45+
String scriptName = "script";
46+
ScriptService scriptService = new ScriptService(Settings.builder().build(),
47+
Collections.singletonMap(
48+
Script.DEFAULT_SCRIPT_LANG, new MockScriptEngine(
49+
Script.DEFAULT_SCRIPT_LANG,
50+
Collections.singletonMap(
51+
scriptName, ctx -> {
52+
ctx.put("bytes_total", randomBytesTotal);
53+
return null;
54+
}
55+
)
56+
)
57+
),
58+
new HashMap<>(ScriptModule.CORE_CONTEXTS)
59+
);
60+
Script script = new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptName, Collections.emptyMap());
5261

5362
Map<String, Object> document = new HashMap<>();
5463
document.put("bytes_in", randomInt());
5564
document.put("bytes_out", randomInt());
5665
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
5766

58-
doAnswer(invocationOnMock -> {
59-
ingestDocument.setFieldValue("bytes_total", randomBytesTotal);
60-
return null;
61-
}).when(executableScript).run();
62-
6367
ScriptProcessor processor = new ScriptProcessor(randomAlphaOfLength(10), script, scriptService);
6468

6569
processor.execute(ingestDocument);

0 commit comments

Comments
 (0)