Skip to content

Commit 0a71ff4

Browse files
authored
[ML] Refactor AutodetectMemoryLimitIT to use memory limit constants and improve model size assertions (#135526)
Previously, the upper bound for model memory checks was set in absolute terms, which is not easy to understand and is brittle. I adjusted the assertions to ensure that the memory usage does not exceed 5% of the memory limit. Additionally, on Linux, we now report the process size (see #131981), which includes approximately 20 MB of native code overhead. I made handling this overhead more explicit. More details: Removed muted tests for testManyDistinctOverFields and testTooManyByAndOverFields. Introduced constants for memory limits in AutodetectMemoryLimitIT. Updated assertions to check effective model size against calculated limits. Closes #132308 Closes #132310 Closes #132611
1 parent 18139f9 commit 0a71ff4

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

muted-tests.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,6 @@ tests:
318318
- class: org.elasticsearch.xpack.sql.qa.mixed_node.SqlCompatIT
319319
method: testNullsOrderWithMissingOrderSupportQueryingNewNode
320320
issue: https://github.com/elastic/elasticsearch/issues/132249
321-
- class: org.elasticsearch.xpack.ml.integration.AutodetectMemoryLimitIT
322-
method: testManyDistinctOverFields
323-
issue: https://github.com/elastic/elasticsearch/issues/132308
324-
- class: org.elasticsearch.xpack.ml.integration.AutodetectMemoryLimitIT
325-
method: testTooManyByAndOverFields
326-
issue: https://github.com/elastic/elasticsearch/issues/132310
327321
- class: org.elasticsearch.xpack.ml.integration.RevertModelSnapshotIT
328322
method: testRevertModelSnapshot_DeleteInterveningResults
329323
issue: https://github.com/elastic/elasticsearch/issues/132349

x-pack/plugin/ml/qa/native-multi-node-tests/src/javaRestTest/java/org/elasticsearch/xpack/ml/integration/AutodetectMemoryLimitIT.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
import static org.hamcrest.Matchers.anyOf;
2828
import static org.hamcrest.Matchers.equalTo;
29-
import static org.hamcrest.Matchers.greaterThan;
30-
import static org.hamcrest.Matchers.lessThan;
3129

3230
/**
3331
* A set of tests that ensure we comply to the model memory limit
@@ -40,6 +38,7 @@ public void cleanUpTest() {
4038
}
4139

4240
public void testTooManyPartitions() throws Exception {
41+
long memoryLimitMb = 30L;
4342
Detector.Builder detector = new Detector.Builder("count", null);
4443
detector.setPartitionFieldName("user");
4544

@@ -53,7 +52,7 @@ public void testTooManyPartitions() throws Exception {
5352
job.setDataDescription(dataDescription);
5453

5554
// Set the memory limit to 30MB
56-
AnalysisLimits limits = new AnalysisLimits(30L, null);
55+
AnalysisLimits limits = new AnalysisLimits(memoryLimitMb, null);
5756
job.setAnalysisLimits(limits);
5857

5958
putJob(job);
@@ -78,15 +77,15 @@ public void testTooManyPartitions() throws Exception {
7877
// Assert we haven't violated the limit too much
7978
GetJobsStatsAction.Response.JobStats jobStats = getJobStats(job.getId()).get(0);
8079
ModelSizeStats modelSizeStats = jobStats.getModelSizeStats();
81-
assertThat(modelSizeStats.getModelBytes(), lessThan(50300000L));
82-
assertThat(modelSizeStats.getModelBytes(), greaterThan(24000000L));
80+
8381
assertThat(
8482
modelSizeStats.getMemoryStatus(),
8583
anyOf(equalTo(ModelSizeStats.MemoryStatus.SOFT_LIMIT), equalTo(ModelSizeStats.MemoryStatus.HARD_LIMIT))
8684
);
8785
}
8886

8987
public void testTooManyByFields() throws Exception {
88+
long memoryLimitMb = 30L;
9089
Detector.Builder detector = new Detector.Builder("count", null);
9190
detector.setByFieldName("user");
9291

@@ -100,7 +99,7 @@ public void testTooManyByFields() throws Exception {
10099
job.setDataDescription(dataDescription);
101100

102101
// Set the memory limit to 30MB
103-
AnalysisLimits limits = new AnalysisLimits(30L, null);
102+
AnalysisLimits limits = new AnalysisLimits(memoryLimitMb, null);
104103
job.setAnalysisLimits(limits);
105104

106105
putJob(job);
@@ -125,12 +124,11 @@ public void testTooManyByFields() throws Exception {
125124
// Assert we haven't violated the limit too much
126125
GetJobsStatsAction.Response.JobStats jobStats = getJobStats(job.getId()).get(0);
127126
ModelSizeStats modelSizeStats = jobStats.getModelSizeStats();
128-
assertThat(modelSizeStats.getModelBytes(), lessThan(45000000L));
129-
assertThat(modelSizeStats.getModelBytes(), greaterThan(25000000L));
130127
assertThat(modelSizeStats.getMemoryStatus(), equalTo(ModelSizeStats.MemoryStatus.HARD_LIMIT));
131128
}
132129

133130
public void testTooManyByAndOverFields() throws Exception {
131+
long memoryLimitMb = 30L;
134132
Detector.Builder detector = new Detector.Builder("count", null);
135133
detector.setByFieldName("department");
136134
detector.setOverFieldName("user");
@@ -145,7 +143,7 @@ public void testTooManyByAndOverFields() throws Exception {
145143
job.setDataDescription(dataDescription);
146144

147145
// Set the memory limit to 30MB
148-
AnalysisLimits limits = new AnalysisLimits(30L, null);
146+
AnalysisLimits limits = new AnalysisLimits(memoryLimitMb, null);
149147
job.setAnalysisLimits(limits);
150148

151149
putJob(job);
@@ -176,12 +174,11 @@ public void testTooManyByAndOverFields() throws Exception {
176174
// Assert we haven't violated the limit too much
177175
GetJobsStatsAction.Response.JobStats jobStats = getJobStats(job.getId()).get(0);
178176
ModelSizeStats modelSizeStats = jobStats.getModelSizeStats();
179-
assertThat(modelSizeStats.getModelBytes(), lessThan(72000000L));
180-
assertThat(modelSizeStats.getModelBytes(), greaterThan(24000000L));
181177
assertThat(modelSizeStats.getMemoryStatus(), equalTo(ModelSizeStats.MemoryStatus.HARD_LIMIT));
182178
}
183179

184180
public void testManyDistinctOverFields() throws Exception {
181+
long memoryLimitMb = 100L;
185182
Detector.Builder detector = new Detector.Builder("sum", "value");
186183
detector.setOverFieldName("user");
187184

@@ -195,7 +192,7 @@ public void testManyDistinctOverFields() throws Exception {
195192
job.setDataDescription(dataDescription);
196193

197194
// Set the memory limit to 110MB
198-
AnalysisLimits limits = new AnalysisLimits(110L, null);
195+
AnalysisLimits limits = new AnalysisLimits(memoryLimitMb, null);
199196
job.setAnalysisLimits(limits);
200197

201198
putJob(job);
@@ -225,12 +222,11 @@ public void testManyDistinctOverFields() throws Exception {
225222
// Assert we haven't violated the limit too much
226223
GetJobsStatsAction.Response.JobStats jobStats = getJobStats(job.getId()).get(0);
227224
ModelSizeStats modelSizeStats = jobStats.getModelSizeStats();
228-
assertThat(modelSizeStats.getModelBytes(), lessThan(120500000L));
229-
assertThat(modelSizeStats.getModelBytes(), greaterThan(70000000L));
230225
assertThat(modelSizeStats.getMemoryStatus(), equalTo(ModelSizeStats.MemoryStatus.HARD_LIMIT));
231226
}
232227

233228
public void testOpenJobShouldHaveModelSizeStats() throws Exception {
229+
long memoryLimitMb = 110L;
234230
// When a job is opened, it should have non-zero model stats that indicate the memory limit and the assignment basis
235231
Detector.Builder detector = new Detector.Builder("sum", "value");
236232
detector.setOverFieldName("user");
@@ -245,7 +241,7 @@ public void testOpenJobShouldHaveModelSizeStats() throws Exception {
245241
job.setDataDescription(dataDescription);
246242

247243
// Set the memory limit to 110MB
248-
AnalysisLimits limits = new AnalysisLimits(110L, null);
244+
AnalysisLimits limits = new AnalysisLimits(memoryLimitMb, null);
249245
job.setAnalysisLimits(limits);
250246

251247
putJob(job);
@@ -255,7 +251,7 @@ public void testOpenJobShouldHaveModelSizeStats() throws Exception {
255251
closeJob(job.getId());
256252

257253
assertThat(modelSizeStats.getModelBytes(), equalTo(0L));
258-
assertThat(modelSizeStats.getModelBytesMemoryLimit(), equalTo(110L));
254+
assertThat(modelSizeStats.getModelBytesMemoryLimit(), equalTo(Long.valueOf(memoryLimitMb)));
259255
assertThat(modelSizeStats.getMemoryStatus(), equalTo(ModelSizeStats.MemoryStatus.OK));
260256
assertThat(modelSizeStats.getAssignmentMemoryBasis(), equalTo(ModelSizeStats.AssignmentMemoryBasis.MODEL_MEMORY_LIMIT));
261257

0 commit comments

Comments
 (0)