Skip to content

Commit c6a25a8

Browse files
authored
Add docs for HLRC for Estimate memory usage API (#45538)
1 parent df01766 commit c6a25a8

File tree

4 files changed

+112
-6
lines changed

4 files changed

+112
-6
lines changed

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.elasticsearch.client.ml.DeleteJobRequest;
4949
import org.elasticsearch.client.ml.DeleteJobResponse;
5050
import org.elasticsearch.client.ml.DeleteModelSnapshotRequest;
51+
import org.elasticsearch.client.ml.EstimateMemoryUsageResponse;
5152
import org.elasticsearch.client.ml.EvaluateDataFrameRequest;
5253
import org.elasticsearch.client.ml.EvaluateDataFrameResponse;
5354
import org.elasticsearch.client.ml.FindFileStructureRequest;
@@ -194,11 +195,13 @@
194195
import java.util.concurrent.TimeUnit;
195196
import java.util.stream.Collectors;
196197

198+
import static org.hamcrest.Matchers.allOf;
197199
import static org.hamcrest.Matchers.closeTo;
198200
import static org.hamcrest.Matchers.containsInAnyOrder;
199201
import static org.hamcrest.Matchers.equalTo;
200202
import static org.hamcrest.Matchers.greaterThan;
201203
import static org.hamcrest.Matchers.hasSize;
204+
import static org.hamcrest.Matchers.lessThan;
202205
import static org.hamcrest.core.Is.is;
203206

204207
public class MlClientDocumentationIT extends ESRestHighLevelClientTestCase {
@@ -3262,6 +3265,72 @@ public void onFailure(Exception e) {
32623265
}
32633266
}
32643267

3268+
public void testEstimateMemoryUsage() throws Exception {
3269+
createIndex("estimate-test-source-index");
3270+
BulkRequest bulkRequest =
3271+
new BulkRequest("estimate-test-source-index")
3272+
.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
3273+
for (int i = 0; i < 10; ++i) {
3274+
bulkRequest.add(new IndexRequest().source(XContentType.JSON, "timestamp", 123456789L, "total", 10L));
3275+
}
3276+
RestHighLevelClient client = highLevelClient();
3277+
client.bulk(bulkRequest, RequestOptions.DEFAULT);
3278+
{
3279+
// tag::estimate-memory-usage-request
3280+
DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.builder()
3281+
.setSource(DataFrameAnalyticsSource.builder().setIndex("estimate-test-source-index").build())
3282+
.setAnalysis(OutlierDetection.createDefault())
3283+
.build();
3284+
PutDataFrameAnalyticsRequest request = new PutDataFrameAnalyticsRequest(config); // <1>
3285+
// end::estimate-memory-usage-request
3286+
3287+
// tag::estimate-memory-usage-execute
3288+
EstimateMemoryUsageResponse response = client.machineLearning().estimateMemoryUsage(request, RequestOptions.DEFAULT);
3289+
// end::estimate-memory-usage-execute
3290+
3291+
// tag::estimate-memory-usage-response
3292+
ByteSizeValue expectedMemoryWithoutDisk = response.getExpectedMemoryWithoutDisk(); // <1>
3293+
ByteSizeValue expectedMemoryWithDisk = response.getExpectedMemoryWithDisk(); // <2>
3294+
// end::estimate-memory-usage-response
3295+
3296+
// We are pretty liberal here as this test does not aim at verifying concrete numbers but rather end-to-end user workflow.
3297+
ByteSizeValue lowerBound = new ByteSizeValue(1, ByteSizeUnit.KB);
3298+
ByteSizeValue upperBound = new ByteSizeValue(1, ByteSizeUnit.GB);
3299+
assertThat(expectedMemoryWithoutDisk, allOf(greaterThan(lowerBound), lessThan(upperBound)));
3300+
assertThat(expectedMemoryWithDisk, allOf(greaterThan(lowerBound), lessThan(upperBound)));
3301+
}
3302+
{
3303+
DataFrameAnalyticsConfig config = DataFrameAnalyticsConfig.builder()
3304+
.setSource(DataFrameAnalyticsSource.builder().setIndex("estimate-test-source-index").build())
3305+
.setAnalysis(OutlierDetection.createDefault())
3306+
.build();
3307+
PutDataFrameAnalyticsRequest request = new PutDataFrameAnalyticsRequest(config);
3308+
// tag::estimate-memory-usage-execute-listener
3309+
ActionListener<EstimateMemoryUsageResponse> listener = new ActionListener<>() {
3310+
@Override
3311+
public void onResponse(EstimateMemoryUsageResponse response) {
3312+
// <1>
3313+
}
3314+
3315+
@Override
3316+
public void onFailure(Exception e) {
3317+
// <2>
3318+
}
3319+
};
3320+
// end::estimate-memory-usage-execute-listener
3321+
3322+
// Replace the empty listener by a blocking listener in test
3323+
final CountDownLatch latch = new CountDownLatch(1);
3324+
listener = new LatchedActionListener<>(listener, latch);
3325+
3326+
// tag::estimate-memory-usage-execute-async
3327+
client.machineLearning().estimateMemoryUsageAsync(request, RequestOptions.DEFAULT, listener); // <1>
3328+
// end::estimate-memory-usage-execute-async
3329+
3330+
assertTrue(latch.await(30L, TimeUnit.SECONDS));
3331+
}
3332+
}
3333+
32653334
public void testCreateFilter() throws Exception {
32663335
RestHighLevelClient client = highLevelClient();
32673336
{
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--
2+
:api: estimate-memory-usage
3+
:request: PutDataFrameAnalyticsRequest
4+
:response: EstimateMemoryUsageResponse
5+
--
6+
[id="{upid}-{api}"]
7+
=== Estimate memory usage API
8+
9+
The Estimate memory usage API is used to estimate memory usage of {dfanalytics}.
10+
Estimation results can be used when deciding the appropriate value for `model_memory_limit` setting later on.
11+
12+
The API accepts an +{request}+ object and returns an +{response}+.
13+
14+
[id="{upid}-{api}-request"]
15+
==== Estimate memory usage Request
16+
17+
["source","java",subs="attributes,callouts,macros"]
18+
--------------------------------------------------
19+
include-tagged::{doc-tests-file}[{api}-request]
20+
--------------------------------------------------
21+
<1> Constructing a new request containing a {dataframe-analytics-config} for which memory usage estimation should be performed
22+
23+
include::../execution.asciidoc[]
24+
25+
[id="{upid}-{api}-response"]
26+
==== Response
27+
28+
The returned +{response}+ contains the memory usage estimates.
29+
30+
["source","java",subs="attributes,callouts,macros"]
31+
--------------------------------------------------
32+
include-tagged::{doc-tests-file}[{api}-response]
33+
--------------------------------------------------
34+
<1> Estimated memory usage under the assumption that the whole {dfanalytics} should happen in memory (i.e. without overflowing to disk).
35+
<2> Estimated memory usage under the assumption that overflowing to disk is allowed during {dfanalytics}.

docs/java-rest/high-level/supported-apis.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ The Java High Level REST Client supports the following Machine Learning APIs:
295295
* <<{upid}-start-data-frame-analytics>>
296296
* <<{upid}-stop-data-frame-analytics>>
297297
* <<{upid}-evaluate-data-frame>>
298+
* <<{upid}-estimate-memory-usage>>
298299
* <<{upid}-put-filter>>
299300
* <<{upid}-get-filters>>
300301
* <<{upid}-update-filter>>
@@ -346,6 +347,7 @@ include::ml/delete-data-frame-analytics.asciidoc[]
346347
include::ml/start-data-frame-analytics.asciidoc[]
347348
include::ml/stop-data-frame-analytics.asciidoc[]
348349
include::ml/evaluate-data-frame.asciidoc[]
350+
include::ml/estimate-memory-usage.asciidoc[]
349351
include::ml/put-filter.asciidoc[]
350352
include::ml/get-filters.asciidoc[]
351353
include::ml/update-filter.asciidoc[]

docs/reference/ml/df-analytics/apis/estimate-memory-usage-dfanalytics.asciidoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ Serves as an advice on how to set `model_memory_limit` when creating {dfanalytic
4242
[[ml-estimate-memory-usage-dfanalytics-results]]
4343
==== {api-response-body-title}
4444

45-
`expected_memory_usage_with_one_partition`::
45+
`expected_memory_without_disk`::
4646
(string) Estimated memory usage under the assumption that the whole {dfanalytics} should happen in memory
4747
(i.e. without overflowing to disk).
4848

49-
`expected_memory_usage_with_max_partitions`::
49+
`expected_memory_with_disk`::
5050
(string) Estimated memory usage under the assumption that overflowing to disk is allowed during {dfanalytics}.
51-
`expected_memory_usage_with_max_partitions` is usually smaller than `expected_memory_usage_with_one_partition`
52-
as using disk allows to limit the main memory needed to perform {dfanalytics}.
51+
`expected_memory_with_disk` is usually smaller than `expected_memory_without_disk` as using disk allows to
52+
limit the main memory needed to perform {dfanalytics}.
5353

5454
[[ml-estimate-memory-usage-dfanalytics-example]]
5555
==== {api-examples-title}
@@ -76,8 +76,8 @@ The API returns the following results:
7676
[source,js]
7777
----
7878
{
79-
"expected_memory_usage_with_one_partition": "128MB",
80-
"expected_memory_usage_with_max_partitions": "32MB"
79+
"expected_memory_without_disk": "128MB",
80+
"expected_memory_with_disk": "32MB"
8181
}
8282
----
8383
// TESTRESPONSE

0 commit comments

Comments
 (0)