|
@@ -13,6 +13,7 @@ import org.elasticsearch.cluster.ClusterState;
|
|
|
import org.elasticsearch.cluster.DiskUsage;
|
|
|
import org.elasticsearch.cluster.metadata.Metadata;
|
|
|
import org.elasticsearch.cluster.node.DiscoveryNode;
|
|
|
+import org.elasticsearch.cluster.node.DiscoveryNodeRole;
|
|
|
import org.elasticsearch.cluster.node.DiscoveryNodes;
|
|
|
import org.elasticsearch.common.collect.ImmutableOpenMap;
|
|
|
import org.elasticsearch.common.collect.Tuple;
|
|
@@ -22,16 +23,19 @@ import org.elasticsearch.xpack.autoscaling.AutoscalingTestCase;
|
|
|
import org.elasticsearch.xpack.autoscaling.policy.AutoscalingPolicy;
|
|
|
import org.elasticsearch.xpack.autoscaling.policy.AutoscalingPolicyMetadata;
|
|
|
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
import java.util.SortedMap;
|
|
|
+import java.util.SortedSet;
|
|
|
import java.util.TreeMap;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.util.stream.IntStream;
|
|
|
+import java.util.stream.StreamSupport;
|
|
|
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
|
|
|
-public class AutoscalingDeciderResultServiceTests extends AutoscalingTestCase {
|
|
|
+public class AutoscalingCalculateCapacityServiceTests extends AutoscalingTestCase {
|
|
|
public void testMultiplePoliciesFixedCapacity() {
|
|
|
AutoscalingCalculateCapacityService service = new AutoscalingCalculateCapacityService(Set.of(new FixedAutoscalingDeciderService()));
|
|
|
Set<String> policyNames = IntStream.range(0, randomIntBetween(1, 10))
|
|
@@ -40,7 +44,7 @@ public class AutoscalingDeciderResultServiceTests extends AutoscalingTestCase {
|
|
|
|
|
|
SortedMap<String, AutoscalingPolicyMetadata> policies = new TreeMap<>(
|
|
|
policyNames.stream()
|
|
|
- .map(s -> Tuple.tuple(s, new AutoscalingPolicyMetadata(new AutoscalingPolicy(s, randomFixedDeciders()))))
|
|
|
+ .map(s -> Tuple.tuple(s, new AutoscalingPolicyMetadata(new AutoscalingPolicy(s, randomRoles(), randomFixedDeciders()))))
|
|
|
.collect(Collectors.toMap(Tuple::v1, Tuple::v2))
|
|
|
);
|
|
|
ClusterState state = ClusterState.builder(ClusterName.DEFAULT)
|
|
@@ -106,49 +110,70 @@ public class AutoscalingDeciderResultServiceTests extends AutoscalingTestCase {
|
|
|
}
|
|
|
|
|
|
public void testContext() {
|
|
|
- String tier = randomAlphaOfLength(5);
|
|
|
ClusterState state = ClusterState.builder(ClusterName.DEFAULT).build();
|
|
|
ClusterInfo info = ClusterInfo.EMPTY;
|
|
|
+ SortedSet<String> roleNames = randomRoles();
|
|
|
AutoscalingCalculateCapacityService.DefaultAutoscalingDeciderContext context =
|
|
|
- new AutoscalingCalculateCapacityService.DefaultAutoscalingDeciderContext(tier, state, info);
|
|
|
+ new AutoscalingCalculateCapacityService.DefaultAutoscalingDeciderContext(roleNames, state, info);
|
|
|
|
|
|
assertSame(state, context.state());
|
|
|
- // there is no nodes in any tier.
|
|
|
+
|
|
|
+ assertThat(context.nodes(), equalTo(Set.of()));
|
|
|
assertThat(context.currentCapacity(), equalTo(AutoscalingCapacity.ZERO));
|
|
|
|
|
|
- tier = "data";
|
|
|
+ Set<DiscoveryNodeRole> roles = roleNames.stream().map(DiscoveryNode::getRoleFromRoleName).collect(Collectors.toSet());
|
|
|
+ Set<DiscoveryNodeRole> otherRoles = mutateRoles(roleNames).stream()
|
|
|
+ .map(DiscoveryNode::getRoleFromRoleName)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
state = ClusterState.builder(ClusterName.DEFAULT)
|
|
|
- .nodes(DiscoveryNodes.builder().add(new DiscoveryNode("nodeId", buildNewFakeTransportAddress(), Version.CURRENT)))
|
|
|
+ .nodes(
|
|
|
+ DiscoveryNodes.builder().add(new DiscoveryNode("nodeId", buildNewFakeTransportAddress(), Map.of(), roles, Version.CURRENT))
|
|
|
+ )
|
|
|
.build();
|
|
|
- context = new AutoscalingCalculateCapacityService.DefaultAutoscalingDeciderContext(tier, state, info);
|
|
|
+ context = new AutoscalingCalculateCapacityService.DefaultAutoscalingDeciderContext(roleNames, state, info);
|
|
|
|
|
|
+ assertThat(context.nodes().size(), equalTo(1));
|
|
|
+ assertThat(context.nodes(), equalTo(StreamSupport.stream(state.nodes().spliterator(), false).collect(Collectors.toSet())));
|
|
|
assertNull(context.currentCapacity());
|
|
|
|
|
|
ImmutableOpenMap.Builder<String, DiskUsage> leastUsages = ImmutableOpenMap.<String, DiskUsage>builder();
|
|
|
ImmutableOpenMap.Builder<String, DiskUsage> mostUsages = ImmutableOpenMap.<String, DiskUsage>builder();
|
|
|
DiscoveryNodes.Builder nodes = DiscoveryNodes.builder();
|
|
|
+ Set<DiscoveryNode> expectedNodes = new HashSet<>();
|
|
|
long sumTotal = 0;
|
|
|
long maxTotal = 0;
|
|
|
for (int i = 0; i < randomIntBetween(1, 5); ++i) {
|
|
|
String nodeId = "nodeId" + i;
|
|
|
- nodes.add(new DiscoveryNode(nodeId, buildNewFakeTransportAddress(), Version.CURRENT));
|
|
|
+ boolean useOtherRoles = randomBoolean();
|
|
|
+ DiscoveryNode node = new DiscoveryNode(
|
|
|
+ nodeId,
|
|
|
+ buildNewFakeTransportAddress(),
|
|
|
+ Map.of(),
|
|
|
+ useOtherRoles ? otherRoles : roles,
|
|
|
+ Version.CURRENT
|
|
|
+ );
|
|
|
+ nodes.add(node);
|
|
|
|
|
|
long total = randomLongBetween(1, 1L << 40);
|
|
|
long total1 = randomBoolean() ? total : randomLongBetween(0, total);
|
|
|
long total2 = total1 != total ? total : randomLongBetween(0, total);
|
|
|
leastUsages.fPut(nodeId, new DiskUsage(nodeId, null, null, total1, randomLongBetween(0, total)));
|
|
|
mostUsages.fPut(nodeId, new DiskUsage(nodeId, null, null, total2, randomLongBetween(0, total)));
|
|
|
- sumTotal += total;
|
|
|
- maxTotal = Math.max(total, maxTotal);
|
|
|
+ if (useOtherRoles == false) {
|
|
|
+ sumTotal += total;
|
|
|
+ maxTotal = Math.max(total, maxTotal);
|
|
|
+ expectedNodes.add(node);
|
|
|
+ }
|
|
|
}
|
|
|
state = ClusterState.builder(ClusterName.DEFAULT).nodes(nodes).build();
|
|
|
info = new ClusterInfo(leastUsages.build(), mostUsages.build(), null, null, null);
|
|
|
- context = new AutoscalingCalculateCapacityService.DefaultAutoscalingDeciderContext(tier, state, info);
|
|
|
+ context = new AutoscalingCalculateCapacityService.DefaultAutoscalingDeciderContext(roleNames, state, info);
|
|
|
|
|
|
+ assertThat(context.nodes(), equalTo(expectedNodes));
|
|
|
AutoscalingCapacity capacity = context.currentCapacity();
|
|
|
assertThat(capacity.node().storage(), equalTo(new ByteSizeValue(maxTotal)));
|
|
|
assertThat(capacity.tier().storage(), equalTo(new ByteSizeValue(sumTotal)));
|
|
|
- // todo: fix these once we know memory of all node on master.
|
|
|
+ // todo: fix these once we know memory of all nodes on master.
|
|
|
assertThat(capacity.node().memory(), equalTo(ByteSizeValue.ZERO));
|
|
|
assertThat(capacity.tier().memory(), equalTo(ByteSizeValue.ZERO));
|
|
|
}
|