|
| 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", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.cluster.routing.allocation.allocator; |
| 11 | + |
| 12 | +import org.elasticsearch.cluster.metadata.ProjectId; |
| 13 | +import org.elasticsearch.cluster.node.DiscoveryNodeUtils; |
| 14 | +import org.elasticsearch.cluster.node.DiscoveryNodes; |
| 15 | +import org.elasticsearch.cluster.routing.GlobalRoutingTable; |
| 16 | +import org.elasticsearch.cluster.routing.IndexRoutingTable; |
| 17 | +import org.elasticsearch.cluster.routing.RecoverySource; |
| 18 | +import org.elasticsearch.cluster.routing.RoutingNodes; |
| 19 | +import org.elasticsearch.cluster.routing.RoutingTable; |
| 20 | +import org.elasticsearch.cluster.routing.ShardRouting; |
| 21 | +import org.elasticsearch.cluster.routing.UnassignedInfo; |
| 22 | +import org.elasticsearch.common.settings.ClusterSettings; |
| 23 | +import org.elasticsearch.common.settings.Settings; |
| 24 | +import org.elasticsearch.common.time.AdvancingTimeProvider; |
| 25 | +import org.elasticsearch.index.Index; |
| 26 | +import org.elasticsearch.index.shard.ShardId; |
| 27 | +import org.elasticsearch.test.ESTestCase; |
| 28 | + |
| 29 | +import java.util.stream.Collectors; |
| 30 | +import java.util.stream.IntStream; |
| 31 | +import java.util.stream.StreamSupport; |
| 32 | + |
| 33 | +public class UndesiredAllocationsTrackerTests extends ESTestCase { |
| 34 | + |
| 35 | + public void testNewestRecordsArePurgedWhenLimitIsDecreased() { |
| 36 | + final var initialMaximum = randomIntBetween(10, 20); |
| 37 | + final var clusterSettings = ClusterSettings.createBuiltInClusterSettings( |
| 38 | + Settings.builder().put(UndesiredAllocationsTracker.MAX_UNDESIRED_ALLOCATIONS_TO_TRACK.getKey(), initialMaximum).build() |
| 39 | + ); |
| 40 | + final var advancingTimeProvider = new AdvancingTimeProvider(); |
| 41 | + final var undesiredAllocationsTracker = new UndesiredAllocationsTracker(clusterSettings, advancingTimeProvider); |
| 42 | + final var indexName = randomIdentifier(); |
| 43 | + final var index = new Index(indexName, indexName); |
| 44 | + final var indexRoutingTableBuilder = IndexRoutingTable.builder(index); |
| 45 | + final var discoveryNodesBuilder = DiscoveryNodes.builder(); |
| 46 | + |
| 47 | + // The shards with the lowest IDs will have the earliest timestamps |
| 48 | + for (int i = 0; i < initialMaximum; i++) { |
| 49 | + final var routing = createAssignedRouting(index, i, discoveryNodesBuilder); |
| 50 | + indexRoutingTableBuilder.addShard(routing); |
| 51 | + undesiredAllocationsTracker.trackUndesiredAllocation(routing); |
| 52 | + } |
| 53 | + final var routingNodes = RoutingNodes.immutable( |
| 54 | + GlobalRoutingTable.builder().put(ProjectId.DEFAULT, RoutingTable.builder().add(indexRoutingTableBuilder).build()).build(), |
| 55 | + discoveryNodesBuilder.build() |
| 56 | + ); |
| 57 | + |
| 58 | + // Reduce the maximum |
| 59 | + final var reducedMaximum = randomIntBetween(1, initialMaximum); |
| 60 | + clusterSettings.applySettings( |
| 61 | + Settings.builder().put(UndesiredAllocationsTracker.MAX_UNDESIRED_ALLOCATIONS_TO_TRACK.getKey(), reducedMaximum).build() |
| 62 | + ); |
| 63 | + |
| 64 | + // We shouldn't purge the entries from the setting updater thread |
| 65 | + assertEquals(initialMaximum, undesiredAllocationsTracker.getUndesiredAllocations().size()); |
| 66 | + |
| 67 | + // We should purge the most recent entries in #cleanup |
| 68 | + undesiredAllocationsTracker.cleanup(routingNodes); |
| 69 | + assertEquals(reducedMaximum, undesiredAllocationsTracker.getUndesiredAllocations().size()); |
| 70 | + final var remainingShardIds = StreamSupport.stream(undesiredAllocationsTracker.getUndesiredAllocations().spliterator(), false) |
| 71 | + .map(olc -> olc.key.shardId().id()) |
| 72 | + .collect(Collectors.toSet()); |
| 73 | + assertEquals(IntStream.range(0, reducedMaximum).boxed().collect(Collectors.toSet()), remainingShardIds); |
| 74 | + } |
| 75 | + |
| 76 | + private ShardRouting createAssignedRouting(Index index, int shardId, DiscoveryNodes.Builder discoveryNodesBuilder) { |
| 77 | + final var nodeId = randomAlphaOfLength(10); |
| 78 | + discoveryNodesBuilder.add(DiscoveryNodeUtils.create(nodeId)); |
| 79 | + return ShardRouting.newUnassigned( |
| 80 | + new ShardId(index, shardId), |
| 81 | + true, |
| 82 | + RecoverySource.EmptyStoreRecoverySource.INSTANCE, |
| 83 | + new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, randomIdentifier()), |
| 84 | + randomFrom(ShardRouting.Role.DEFAULT, ShardRouting.Role.INDEX_ONLY) |
| 85 | + ).initialize(nodeId, null, randomNonNegativeLong()); |
| 86 | + } |
| 87 | +} |
0 commit comments