|
| 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 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.downsample; |
| 9 | + |
| 10 | +import org.elasticsearch.action.downsample.DownsampleConfig; |
| 11 | +import org.elasticsearch.client.internal.Client; |
| 12 | +import org.elasticsearch.cluster.ClusterState; |
| 13 | +import org.elasticsearch.cluster.metadata.DataStreamTestHelper; |
| 14 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 15 | +import org.elasticsearch.cluster.node.DiscoveryNodeRole; |
| 16 | +import org.elasticsearch.cluster.node.DiscoveryNodeUtils; |
| 17 | +import org.elasticsearch.cluster.node.DiscoveryNodes; |
| 18 | +import org.elasticsearch.cluster.routing.IndexRoutingTable; |
| 19 | +import org.elasticsearch.cluster.routing.RoutingTable; |
| 20 | +import org.elasticsearch.common.Strings; |
| 21 | +import org.elasticsearch.common.UUIDs; |
| 22 | +import org.elasticsearch.core.Tuple; |
| 23 | +import org.elasticsearch.index.Index; |
| 24 | +import org.elasticsearch.index.shard.ShardId; |
| 25 | +import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; |
| 26 | +import org.elasticsearch.test.ESTestCase; |
| 27 | +import org.elasticsearch.xpack.core.downsample.DownsampleShardTask; |
| 28 | +import org.junit.Before; |
| 29 | + |
| 30 | +import java.time.Instant; |
| 31 | +import java.time.temporal.ChronoUnit; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Set; |
| 35 | +import java.util.concurrent.Executor; |
| 36 | + |
| 37 | +import static org.elasticsearch.cluster.routing.ShardRoutingState.STARTED; |
| 38 | +import static org.elasticsearch.cluster.routing.TestShardRouting.shardRoutingBuilder; |
| 39 | +import static org.hamcrest.Matchers.equalTo; |
| 40 | +import static org.mockito.Mockito.mock; |
| 41 | + |
| 42 | +public class DownsampleShardPersistentTaskExecutorTests extends ESTestCase { |
| 43 | + |
| 44 | + private ClusterState initialClusterState; |
| 45 | + private DownsampleShardPersistentTaskExecutor executor; |
| 46 | + |
| 47 | + @Before |
| 48 | + public void setup() { |
| 49 | + Instant now = Instant.now().truncatedTo(ChronoUnit.MILLIS); |
| 50 | + Instant start = now.minus(2, ChronoUnit.HOURS); |
| 51 | + Instant end = now.plus(40, ChronoUnit.MINUTES); |
| 52 | + initialClusterState = DataStreamTestHelper.getClusterStateWithDataStream("metrics-app1", List.of(new Tuple<>(start, end))); |
| 53 | + executor = new DownsampleShardPersistentTaskExecutor(mock(Client.class), DownsampleShardTask.TASK_NAME, mock(Executor.class)); |
| 54 | + } |
| 55 | + |
| 56 | + public void testGetAssignment() { |
| 57 | + var backingIndex = initialClusterState.metadata().dataStreams().get("metrics-app1").getWriteIndex(); |
| 58 | + var node = newNode(); |
| 59 | + var shardId = new ShardId(backingIndex, 0); |
| 60 | + var clusterState = ClusterState.builder(initialClusterState) |
| 61 | + .nodes(new DiscoveryNodes.Builder().add(node).build()) |
| 62 | + .routingTable( |
| 63 | + RoutingTable.builder() |
| 64 | + .add( |
| 65 | + IndexRoutingTable.builder(backingIndex) |
| 66 | + .addShard(shardRoutingBuilder(shardId, node.getId(), true, STARTED).withRecoverySource(null).build()) |
| 67 | + ) |
| 68 | + ) |
| 69 | + .build(); |
| 70 | + |
| 71 | + var params = new DownsampleShardTaskParams( |
| 72 | + new DownsampleConfig(new DateHistogramInterval("1h")), |
| 73 | + shardId.getIndexName(), |
| 74 | + 1, |
| 75 | + 1, |
| 76 | + shardId, |
| 77 | + Strings.EMPTY_ARRAY, |
| 78 | + Strings.EMPTY_ARRAY, |
| 79 | + Strings.EMPTY_ARRAY |
| 80 | + ); |
| 81 | + var result = executor.getAssignment(params, Set.of(node), clusterState); |
| 82 | + assertThat(result.getExecutorNode(), equalTo(node.getId())); |
| 83 | + } |
| 84 | + |
| 85 | + public void testGetAssignmentMissingIndex() { |
| 86 | + var backingIndex = initialClusterState.metadata().dataStreams().get("metrics-app1").getWriteIndex(); |
| 87 | + var node = newNode(); |
| 88 | + var shardId = new ShardId(backingIndex, 0); |
| 89 | + var clusterState = ClusterState.builder(initialClusterState) |
| 90 | + .nodes(new DiscoveryNodes.Builder().add(node).build()) |
| 91 | + .routingTable( |
| 92 | + RoutingTable.builder() |
| 93 | + .add( |
| 94 | + IndexRoutingTable.builder(backingIndex) |
| 95 | + .addShard(shardRoutingBuilder(shardId, node.getId(), true, STARTED).withRecoverySource(null).build()) |
| 96 | + ) |
| 97 | + ) |
| 98 | + .build(); |
| 99 | + |
| 100 | + var missingShardId = new ShardId(new Index("another_index", "uid"), 0); |
| 101 | + var params = new DownsampleShardTaskParams( |
| 102 | + new DownsampleConfig(new DateHistogramInterval("1h")), |
| 103 | + missingShardId.getIndexName(), |
| 104 | + 1, |
| 105 | + 1, |
| 106 | + missingShardId, |
| 107 | + Strings.EMPTY_ARRAY, |
| 108 | + Strings.EMPTY_ARRAY, |
| 109 | + Strings.EMPTY_ARRAY |
| 110 | + ); |
| 111 | + var result = executor.getAssignment(params, Set.of(node), clusterState); |
| 112 | + assertThat(result.getExecutorNode(), equalTo(node.getId())); |
| 113 | + assertThat(result.getExplanation(), equalTo("a node to fail and stop this persistent task")); |
| 114 | + } |
| 115 | + |
| 116 | + private static DiscoveryNode newNode() { |
| 117 | + return DiscoveryNodeUtils.create( |
| 118 | + "node_" + UUIDs.randomBase64UUID(random()), |
| 119 | + buildNewFakeTransportAddress(), |
| 120 | + Map.of(), |
| 121 | + DiscoveryNodeRole.roles() |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments