|
| 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.autoscaling.storage; |
| 8 | + |
| 9 | +import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; |
| 10 | +import org.elasticsearch.action.index.IndexRequestBuilder; |
| 11 | +import org.elasticsearch.cluster.ClusterInfoService; |
| 12 | +import org.elasticsearch.cluster.DiskUsageIntegTestCase; |
| 13 | +import org.elasticsearch.cluster.InternalClusterInfoService; |
| 14 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 15 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 16 | +import org.elasticsearch.cluster.node.DiscoveryNodeRole; |
| 17 | +import org.elasticsearch.cluster.routing.allocation.DiskThresholdSettings; |
| 18 | +import org.elasticsearch.cluster.routing.allocation.decider.DiskThresholdDecider; |
| 19 | +import org.elasticsearch.common.settings.ClusterSettings; |
| 20 | +import org.elasticsearch.common.settings.Settings; |
| 21 | +import org.elasticsearch.plugins.Plugin; |
| 22 | +import org.elasticsearch.test.ESIntegTestCase; |
| 23 | +import org.elasticsearch.xpack.autoscaling.Autoscaling; |
| 24 | +import org.elasticsearch.xpack.autoscaling.LocalStateAutoscaling; |
| 25 | +import org.elasticsearch.xpack.autoscaling.action.GetAutoscalingCapacityAction; |
| 26 | +import org.elasticsearch.xpack.autoscaling.action.PutAutoscalingPolicyAction; |
| 27 | +import org.hamcrest.Matchers; |
| 28 | + |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.Collection; |
| 32 | +import java.util.Locale; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Set; |
| 35 | +import java.util.TreeMap; |
| 36 | +import java.util.TreeSet; |
| 37 | +import java.util.stream.Collectors; |
| 38 | +import java.util.stream.IntStream; |
| 39 | + |
| 40 | +import static org.elasticsearch.index.store.Store.INDEX_STORE_STATS_REFRESH_INTERVAL_SETTING; |
| 41 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 42 | + |
| 43 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST, numDataNodes = 0) |
| 44 | +public class ReactiveStorageIT extends DiskUsageIntegTestCase { |
| 45 | + |
| 46 | + private static final long WATERMARK_BYTES = 10240; |
| 47 | + |
| 48 | + @Override |
| 49 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 50 | + Collection<Class<? extends Plugin>> plugins = new ArrayList<>(super.nodePlugins()); |
| 51 | + plugins.add(LocalStateAutoscaling.class); |
| 52 | + return plugins; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected Settings nodeSettings(final int nodeOrdinal) { |
| 57 | + final Settings.Builder builder = Settings.builder().put(super.nodeSettings(nodeOrdinal)); |
| 58 | + builder.put(Autoscaling.AUTOSCALING_ENABLED_SETTING.getKey(), true) |
| 59 | + .put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_LOW_DISK_WATERMARK_SETTING.getKey(), (WATERMARK_BYTES * 2) + "b") |
| 60 | + .put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_HIGH_DISK_WATERMARK_SETTING.getKey(), WATERMARK_BYTES + "b") |
| 61 | + .put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_DISK_FLOOD_STAGE_WATERMARK_SETTING.getKey(), "0b") |
| 62 | + .put(DiskThresholdSettings.CLUSTER_ROUTING_ALLOCATION_REROUTE_INTERVAL_SETTING.getKey(), "0ms") |
| 63 | + .put(DiskThresholdDecider.ENABLE_FOR_SINGLE_DATA_NODE.getKey(), "true"); |
| 64 | + return builder.build(); |
| 65 | + } |
| 66 | + |
| 67 | + public void testScaleUp() throws InterruptedException { |
| 68 | + internalCluster().startMasterOnlyNode(); |
| 69 | + final String dataNodeName = internalCluster().startDataOnlyNode(); |
| 70 | + final String policyName = "test"; |
| 71 | + putAutoscalingPolicy(policyName); |
| 72 | + |
| 73 | + final String indexName = randomAlphaOfLength(10).toLowerCase(Locale.ROOT); |
| 74 | + createIndex( |
| 75 | + indexName, |
| 76 | + Settings.builder() |
| 77 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) |
| 78 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 6) |
| 79 | + .put(INDEX_STORE_STATS_REFRESH_INTERVAL_SETTING.getKey(), "0ms") |
| 80 | + .build() |
| 81 | + ); |
| 82 | + indexRandom( |
| 83 | + true, |
| 84 | + IntStream.range(1, 100) |
| 85 | + .mapToObj(i -> client().prepareIndex(indexName).setSource("field", randomAlphaOfLength(50))) |
| 86 | + .toArray(IndexRequestBuilder[]::new) |
| 87 | + ); |
| 88 | + forceMerge(); |
| 89 | + refresh(); |
| 90 | + |
| 91 | + // just check it does not throw when not refreshed. |
| 92 | + capacity(); |
| 93 | + |
| 94 | + IndicesStatsResponse stats = client().admin().indices().prepareStats(indexName).clear().setStore(true).get(); |
| 95 | + long used = stats.getTotal().getStore().getSizeInBytes(); |
| 96 | + long minShardSize = Arrays.stream(stats.getShards()).mapToLong(s -> s.getStats().getStore().sizeInBytes()).min().orElseThrow(); |
| 97 | + long maxShardSize = Arrays.stream(stats.getShards()).mapToLong(s -> s.getStats().getStore().sizeInBytes()).max().orElseThrow(); |
| 98 | + long enoughSpace = used + WATERMARK_BYTES + 1; |
| 99 | + |
| 100 | + setTotalSpace(dataNodeName, enoughSpace); |
| 101 | + GetAutoscalingCapacityAction.Response response = capacity(); |
| 102 | + assertThat(response.results().keySet(), Matchers.equalTo(Set.of(policyName))); |
| 103 | + assertThat(response.results().get(policyName).currentCapacity().total().storage().getBytes(), Matchers.equalTo(enoughSpace)); |
| 104 | + assertThat(response.results().get(policyName).requiredCapacity().total().storage().getBytes(), Matchers.equalTo(enoughSpace)); |
| 105 | + assertThat(response.results().get(policyName).requiredCapacity().node().storage().getBytes(), Matchers.equalTo(maxShardSize)); |
| 106 | + |
| 107 | + setTotalSpace(dataNodeName, enoughSpace - 2); |
| 108 | + response = capacity(); |
| 109 | + assertThat(response.results().keySet(), Matchers.equalTo(Set.of(policyName))); |
| 110 | + assertThat(response.results().get(policyName).currentCapacity().total().storage().getBytes(), Matchers.equalTo(enoughSpace - 2)); |
| 111 | + assertThat( |
| 112 | + response.results().get(policyName).requiredCapacity().total().storage().getBytes(), |
| 113 | + Matchers.greaterThan(enoughSpace - 2) |
| 114 | + ); |
| 115 | + assertThat( |
| 116 | + response.results().get(policyName).requiredCapacity().total().storage().getBytes(), |
| 117 | + Matchers.lessThanOrEqualTo(enoughSpace + minShardSize) |
| 118 | + ); |
| 119 | + assertThat(response.results().get(policyName).requiredCapacity().node().storage().getBytes(), Matchers.equalTo(maxShardSize)); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Verify that the list of roles includes all data roles to ensure we consider adding future data roles. |
| 124 | + */ |
| 125 | + public void testRoles() { |
| 126 | + // this has to be an integration test to ensure roles are available. |
| 127 | + internalCluster().startMasterOnlyNode(); |
| 128 | + ReactiveStorageDeciderService service = new ReactiveStorageDeciderService( |
| 129 | + Settings.EMPTY, |
| 130 | + new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), |
| 131 | + null |
| 132 | + ); |
| 133 | + assertThat( |
| 134 | + service.roles().stream().sorted().collect(Collectors.toList()), |
| 135 | + Matchers.equalTo( |
| 136 | + DiscoveryNode.getPossibleRoles().stream().filter(DiscoveryNodeRole::canContainData).sorted().collect(Collectors.toList()) |
| 137 | + ) |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + public void setTotalSpace(String dataNodeName, long totalSpace) { |
| 142 | + getTestFileStore(dataNodeName).setTotalSpace(totalSpace); |
| 143 | + final ClusterInfoService clusterInfoService = internalCluster().getCurrentMasterNodeInstance(ClusterInfoService.class); |
| 144 | + ((InternalClusterInfoService) clusterInfoService).refresh(); |
| 145 | + } |
| 146 | + |
| 147 | + public GetAutoscalingCapacityAction.Response capacity() { |
| 148 | + GetAutoscalingCapacityAction.Request request = new GetAutoscalingCapacityAction.Request(); |
| 149 | + GetAutoscalingCapacityAction.Response response = client().execute(GetAutoscalingCapacityAction.INSTANCE, request).actionGet(); |
| 150 | + return response; |
| 151 | + } |
| 152 | + |
| 153 | + private void putAutoscalingPolicy(String policyName) { |
| 154 | + final PutAutoscalingPolicyAction.Request request = new PutAutoscalingPolicyAction.Request( |
| 155 | + policyName, |
| 156 | + new TreeSet<>(Set.of("data")), |
| 157 | + new TreeMap<>(Map.of("reactive_storage", Settings.EMPTY)) |
| 158 | + ); |
| 159 | + assertAcked(client().execute(PutAutoscalingPolicyAction.INSTANCE, request).actionGet()); |
| 160 | + } |
| 161 | +} |
0 commit comments