|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.ml.integration; |
| 8 | + |
| 9 | +import org.elasticsearch.action.admin.cluster.node.info.NodeInfo; |
| 10 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 11 | +import org.elasticsearch.common.settings.Settings; |
| 12 | +import org.elasticsearch.common.unit.ByteSizeValue; |
| 13 | +import org.elasticsearch.common.unit.TimeValue; |
| 14 | +import org.elasticsearch.xpack.autoscaling.Autoscaling; |
| 15 | +import org.elasticsearch.xpack.autoscaling.action.GetAutoscalingCapacityAction; |
| 16 | +import org.elasticsearch.xpack.autoscaling.action.PutAutoscalingPolicyAction; |
| 17 | +import org.elasticsearch.xpack.autoscaling.capacity.AutoscalingDeciderResult; |
| 18 | +import org.elasticsearch.xpack.autoscaling.capacity.AutoscalingDeciderResults; |
| 19 | +import org.elasticsearch.xpack.core.ml.job.config.AnalysisConfig; |
| 20 | +import org.elasticsearch.xpack.core.ml.job.config.AnalysisLimits; |
| 21 | +import org.elasticsearch.xpack.core.ml.job.config.DataDescription; |
| 22 | +import org.elasticsearch.xpack.core.ml.job.config.Detector; |
| 23 | +import org.elasticsearch.xpack.core.ml.job.config.Job; |
| 24 | +import org.elasticsearch.xpack.ml.MachineLearning; |
| 25 | +import org.elasticsearch.xpack.ml.autoscaling.MlAutoscalingDeciderService; |
| 26 | +import org.elasticsearch.xpack.ml.autoscaling.NativeMemoryCapacity; |
| 27 | + |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.List; |
| 31 | +import java.util.SortedMap; |
| 32 | +import java.util.TreeMap; |
| 33 | +import java.util.TreeSet; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 37 | +import static org.hamcrest.Matchers.containsString; |
| 38 | +import static org.hamcrest.Matchers.equalTo; |
| 39 | +import static org.hamcrest.Matchers.hasKey; |
| 40 | + |
| 41 | +public class AutoscalingIT extends MlNativeAutodetectIntegTestCase { |
| 42 | + |
| 43 | + private static final long BASIC_REQUIREMENT_MB = 10; |
| 44 | + private static final long NATIVE_PROCESS_OVERHEAD_MB = 30; |
| 45 | + private static final long BASELINE_OVERHEAD_MB = BASIC_REQUIREMENT_MB + NATIVE_PROCESS_OVERHEAD_MB; |
| 46 | + |
| 47 | + // This test assumes that xpack.ml.max_machine_memory_percent is 30 |
| 48 | + // and that xpack.ml.use_auto_machine_memory_percent is false |
| 49 | + public void testMLAutoscalingCapacity() { |
| 50 | + SortedMap<String, Settings> deciders = new TreeMap<>(); |
| 51 | + deciders.put(MlAutoscalingDeciderService.NAME, |
| 52 | + Settings.builder().put(MlAutoscalingDeciderService.DOWN_SCALE_DELAY.getKey(), TimeValue.ZERO).build()); |
| 53 | + final PutAutoscalingPolicyAction.Request request = new PutAutoscalingPolicyAction.Request( |
| 54 | + "ml_test", |
| 55 | + new TreeSet<>(), |
| 56 | + deciders |
| 57 | + ); |
| 58 | + assertAcked(client().execute(PutAutoscalingPolicyAction.INSTANCE, request).actionGet()); |
| 59 | + |
| 60 | + assertMlCapacity( |
| 61 | + client().execute( |
| 62 | + GetAutoscalingCapacityAction.INSTANCE, |
| 63 | + new GetAutoscalingCapacityAction.Request() |
| 64 | + ).actionGet(), |
| 65 | + "Requesting scale down as tier and/or node size could be smaller", |
| 66 | + 0L, |
| 67 | + 0L); |
| 68 | + |
| 69 | + putJob("job1", 100); |
| 70 | + putJob("job2", 200); |
| 71 | + openJob("job1"); |
| 72 | + openJob("job2"); |
| 73 | + long expectedTierBytes = (long)Math.ceil( |
| 74 | + ByteSizeValue.ofMb(100 + BASELINE_OVERHEAD_MB + 200 + BASELINE_OVERHEAD_MB).getBytes() * 100 / 30.0 |
| 75 | + ); |
| 76 | + long expectedNodeBytes = (long)Math.ceil(ByteSizeValue.ofMb(200 + BASELINE_OVERHEAD_MB).getBytes() * 100 / 30.0); |
| 77 | + |
| 78 | + assertMlCapacity( |
| 79 | + client().execute( |
| 80 | + GetAutoscalingCapacityAction.INSTANCE, |
| 81 | + new GetAutoscalingCapacityAction.Request() |
| 82 | + ).actionGet(), |
| 83 | + "Requesting scale down as tier and/or node size could be smaller", |
| 84 | + expectedTierBytes, |
| 85 | + expectedNodeBytes); |
| 86 | + |
| 87 | + putJob("bigjob1", 60_000); |
| 88 | + putJob("bigjob2", 50_000); |
| 89 | + openJob("bigjob1"); |
| 90 | + openJob("bigjob2"); |
| 91 | + List<DiscoveryNode> mlNodes = admin() |
| 92 | + .cluster() |
| 93 | + .prepareNodesInfo() |
| 94 | + .all() |
| 95 | + .get() |
| 96 | + .getNodes() |
| 97 | + .stream() |
| 98 | + .map(NodeInfo::getNode) |
| 99 | + .filter(MachineLearning::isMlNode) |
| 100 | + .collect(Collectors.toList()); |
| 101 | + NativeMemoryCapacity currentScale = MlAutoscalingDeciderService.currentScale(mlNodes, 30, false); |
| 102 | + expectedTierBytes = (long)Math.ceil( |
| 103 | + (ByteSizeValue.ofMb(50_000 + BASIC_REQUIREMENT_MB + 60_000 + BASIC_REQUIREMENT_MB).getBytes() |
| 104 | + + currentScale.getTier() |
| 105 | + ) * 100 / 30.0 |
| 106 | + ); |
| 107 | + expectedNodeBytes = (long)Math.ceil(ByteSizeValue.ofMb(60_000 + BASELINE_OVERHEAD_MB).getBytes() * 100 / 30.0); |
| 108 | + |
| 109 | + |
| 110 | + assertMlCapacity( |
| 111 | + client().execute( |
| 112 | + GetAutoscalingCapacityAction.INSTANCE, |
| 113 | + new GetAutoscalingCapacityAction.Request() |
| 114 | + ).actionGet(), |
| 115 | + "requesting scale up as number of jobs in queues exceeded configured limit", |
| 116 | + expectedTierBytes, |
| 117 | + expectedNodeBytes); |
| 118 | + |
| 119 | + expectedTierBytes = (long)Math.ceil( |
| 120 | + ByteSizeValue.ofMb(100 + BASELINE_OVERHEAD_MB + 200 + BASELINE_OVERHEAD_MB).getBytes() * 100 / 30.0 |
| 121 | + ); |
| 122 | + expectedNodeBytes = (long)Math.ceil(ByteSizeValue.ofMb(200 + BASELINE_OVERHEAD_MB).getBytes() * 100 / 30.0); |
| 123 | + closeJob("bigjob1"); |
| 124 | + closeJob("bigjob2"); |
| 125 | + |
| 126 | + assertMlCapacity( |
| 127 | + client().execute( |
| 128 | + GetAutoscalingCapacityAction.INSTANCE, |
| 129 | + new GetAutoscalingCapacityAction.Request() |
| 130 | + ).actionGet(), |
| 131 | + "Requesting scale down as tier and/or node size could be smaller", |
| 132 | + expectedTierBytes, |
| 133 | + expectedNodeBytes); |
| 134 | + closeJob("job1"); |
| 135 | + closeJob("job2"); |
| 136 | + |
| 137 | + assertMlCapacity( |
| 138 | + client().execute( |
| 139 | + GetAutoscalingCapacityAction.INSTANCE, |
| 140 | + new GetAutoscalingCapacityAction.Request() |
| 141 | + ).actionGet(), |
| 142 | + "Requesting scale down as tier and/or node size could be smaller", |
| 143 | + 0L, |
| 144 | + 0L); |
| 145 | + } |
| 146 | + |
| 147 | + private void assertMlCapacity(GetAutoscalingCapacityAction.Response capacity, String reason, long tierBytes, long nodeBytes) { |
| 148 | + assertThat(capacity.getResults(), hasKey("ml_test")); |
| 149 | + AutoscalingDeciderResults autoscalingDeciderResults = capacity.getResults().get("ml_test"); |
| 150 | + assertThat(autoscalingDeciderResults.results(), hasKey("ml")); |
| 151 | + |
| 152 | + AutoscalingDeciderResult autoscalingDeciderResult = autoscalingDeciderResults.results().get("ml"); |
| 153 | + assertThat(autoscalingDeciderResult.reason().summary(), containsString(reason)); |
| 154 | + assertThat(autoscalingDeciderResult.requiredCapacity().tier().memory().getBytes(), equalTo(tierBytes)); |
| 155 | + assertThat(autoscalingDeciderResult.requiredCapacity().node().memory().getBytes(), equalTo(nodeBytes)); |
| 156 | + } |
| 157 | + |
| 158 | + private void putJob(String jobId, long limitMb) { |
| 159 | + Job.Builder job = |
| 160 | + new Job.Builder(jobId) |
| 161 | + .setAllowLazyOpen(true) |
| 162 | + .setAnalysisLimits(new AnalysisLimits(limitMb, null)) |
| 163 | + .setAnalysisConfig( |
| 164 | + new AnalysisConfig.Builder((List<Detector>) null) |
| 165 | + .setBucketSpan(TimeValue.timeValueHours(1)) |
| 166 | + .setDetectors( |
| 167 | + Collections.singletonList( |
| 168 | + new Detector.Builder("count", null) |
| 169 | + .setPartitionFieldName("user") |
| 170 | + .build()))) |
| 171 | + .setDataDescription( |
| 172 | + new DataDescription.Builder() |
| 173 | + .setTimeFormat("epoch")); |
| 174 | + |
| 175 | + registerJob(job); |
| 176 | + putJob(job); |
| 177 | + } |
| 178 | +} |
0 commit comments