|
| 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.snapshots; |
| 11 | + |
| 12 | +import org.elasticsearch.cluster.SnapshotsInProgress; |
| 13 | +import org.elasticsearch.cluster.SnapshotsInProgress.ShardSnapshotStatus; |
| 14 | +import org.elasticsearch.cluster.metadata.ProjectId; |
| 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.test.ESTestCase; |
| 19 | + |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import static org.elasticsearch.snapshots.SnapshotsInProgressSerializationTests.randomSnapshot; |
| 23 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 24 | +import static org.hamcrest.Matchers.lessThan; |
| 25 | + |
| 26 | +public class PerNodeShardSnapshotCounterTests extends ESTestCase { |
| 27 | + |
| 28 | + public void testDisabledWhenLimitIsZero() { |
| 29 | + final var perNodeShardSnapshotCounter = new PerNodeShardSnapshotCounter( |
| 30 | + 0, |
| 31 | + SnapshotsInProgress.EMPTY, |
| 32 | + DiscoveryNodes.EMPTY_NODES, |
| 33 | + randomBoolean() |
| 34 | + ); |
| 35 | + assertTrue(perNodeShardSnapshotCounter.hasCapacityOnAnyNode()); |
| 36 | + assertTrue(perNodeShardSnapshotCounter.tryStartShardSnapshotOnNode(randomIdentifier())); |
| 37 | + assertTrue(perNodeShardSnapshotCounter.completeShardSnapshotOnNode(randomIdentifier())); |
| 38 | + } |
| 39 | + |
| 40 | + public void testNodesCapacities() { |
| 41 | + final var snapshotNodeIds = randomList(1, 5, () -> randomAlphaOfLength(16)); |
| 42 | + final var nonSnapshotNodeIds = randomList(0, 5, () -> randomAlphaOfLength(18)); |
| 43 | + |
| 44 | + final boolean isStateless = randomBoolean(); |
| 45 | + final Set<DiscoveryNodeRole> snapshotNodeRole; |
| 46 | + final Set<DiscoveryNodeRole> nonSnapshotNodeRole; |
| 47 | + if (isStateless) { |
| 48 | + snapshotNodeRole = Set.of(DiscoveryNodeRole.INDEX_ROLE); |
| 49 | + nonSnapshotNodeRole = Set.of(randomFrom(DiscoveryNodeRole.SEARCH_ROLE, DiscoveryNodeRole.ML_ROLE)); |
| 50 | + } else { |
| 51 | + snapshotNodeRole = Set.copyOf( |
| 52 | + randomNonEmptySubsetOf( |
| 53 | + Set.of( |
| 54 | + DiscoveryNodeRole.DATA_ROLE, |
| 55 | + DiscoveryNodeRole.DATA_HOT_NODE_ROLE, |
| 56 | + DiscoveryNodeRole.DATA_WARM_NODE_ROLE, |
| 57 | + DiscoveryNodeRole.DATA_COLD_NODE_ROLE, |
| 58 | + DiscoveryNodeRole.DATA_FROZEN_NODE_ROLE, |
| 59 | + DiscoveryNodeRole.DATA_CONTENT_NODE_ROLE |
| 60 | + ) |
| 61 | + ) |
| 62 | + ); |
| 63 | + nonSnapshotNodeRole = Set.copyOf( |
| 64 | + randomNonEmptySubsetOf( |
| 65 | + Set.of(DiscoveryNodeRole.MASTER_ROLE, DiscoveryNodeRole.ML_ROLE, DiscoveryNodeRole.VOTING_ONLY_NODE_ROLE) |
| 66 | + ) |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + final var nodesBuilder = DiscoveryNodes.builder(); |
| 71 | + for (var snapshotNodeId : snapshotNodeIds) { |
| 72 | + nodesBuilder.add(DiscoveryNodeUtils.builder(snapshotNodeId).name(snapshotNodeId).roles(snapshotNodeRole).build()); |
| 73 | + } |
| 74 | + for (var nonSnapshotNodeId : nonSnapshotNodeIds) { |
| 75 | + nodesBuilder.add(DiscoveryNodeUtils.builder(nonSnapshotNodeId).name(nonSnapshotNodeId).roles(nonSnapshotNodeRole).build()); |
| 76 | + } |
| 77 | + final DiscoveryNodes discoveryNodes = nodesBuilder.build(); |
| 78 | + |
| 79 | + final int perNodeLimit = between(1, 10); |
| 80 | + |
| 81 | + // Basic test when there is no ongoing snapshots |
| 82 | + { |
| 83 | + final var perNodeShardSnapshotCounter = new PerNodeShardSnapshotCounter( |
| 84 | + perNodeLimit, |
| 85 | + SnapshotsInProgress.EMPTY, |
| 86 | + discoveryNodes, |
| 87 | + isStateless |
| 88 | + ); |
| 89 | + assertTrue(perNodeShardSnapshotCounter.hasCapacityOnAnyNode()); |
| 90 | + |
| 91 | + // Always false for non-snapshotting nodes |
| 92 | + for (var nonSnapshotNodeId : nonSnapshotNodeIds) { |
| 93 | + assertFalse(perNodeShardSnapshotCounter.tryStartShardSnapshotOnNode(nonSnapshotNodeId)); |
| 94 | + assertFalse(perNodeShardSnapshotCounter.completeShardSnapshotOnNode(nonSnapshotNodeId)); |
| 95 | + } |
| 96 | + |
| 97 | + for (var snapshotNodeId : snapshotNodeIds) { |
| 98 | + assertTrue(perNodeShardSnapshotCounter.tryStartShardSnapshotOnNode(snapshotNodeId)); |
| 99 | + assertTrue(perNodeShardSnapshotCounter.completeShardSnapshotOnNode(snapshotNodeId)); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // random snapshots |
| 104 | + { |
| 105 | + final int numRepos = between(1, 3); |
| 106 | + SnapshotsInProgress snapshotsInProgress = SnapshotsInProgress.EMPTY; |
| 107 | + for (int i = 0; i < numRepos; i++) { |
| 108 | + final var numSnapshots = between(1, 5); |
| 109 | + for (int j = 0; j < numSnapshots; j++) { |
| 110 | + final var entry = randomSnapshot(ProjectId.DEFAULT, "repo-" + i, () -> randomFrom(snapshotNodeIds)); |
| 111 | + if (entry.hasAssignedQueuedShards()) { |
| 112 | + assertTrue(entry.shards().values().stream().anyMatch(ShardSnapshotStatus::isAssignedQueued)); |
| 113 | + } else { |
| 114 | + assertFalse(entry.shards().values().stream().anyMatch(ShardSnapshotStatus::isAssignedQueued)); |
| 115 | + } |
| 116 | + snapshotsInProgress = snapshotsInProgress.withAddedEntry(entry); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + final var perNodeShardSnapshotCounter = new PerNodeShardSnapshotCounter( |
| 121 | + perNodeLimit, |
| 122 | + snapshotsInProgress, |
| 123 | + discoveryNodes, |
| 124 | + isStateless |
| 125 | + ); |
| 126 | + |
| 127 | + // Always false for non-snapshotting nodes |
| 128 | + for (var nonSnapshotNodeId : nonSnapshotNodeIds) { |
| 129 | + assertFalse(perNodeShardSnapshotCounter.tryStartShardSnapshotOnNode(nonSnapshotNodeId)); |
| 130 | + assertFalse(perNodeShardSnapshotCounter.completeShardSnapshotOnNode(nonSnapshotNodeId)); |
| 131 | + } |
| 132 | + |
| 133 | + for (var snapshotNodeId : snapshotNodeIds) { |
| 134 | + final var numRunning = runningShardSnapshotsForNode(snapshotsInProgress, snapshotNodeId); |
| 135 | + final var started = perNodeShardSnapshotCounter.tryStartShardSnapshotOnNode(snapshotNodeId); |
| 136 | + if (started) { |
| 137 | + assertThat(numRunning, lessThan(perNodeLimit)); |
| 138 | + if (numRunning > 0) { |
| 139 | + assertTrue(perNodeShardSnapshotCounter.completeShardSnapshotOnNode(snapshotNodeId)); |
| 140 | + } else { |
| 141 | + assertFalse(perNodeShardSnapshotCounter.completeShardSnapshotOnNode(snapshotNodeId)); |
| 142 | + } |
| 143 | + } else { |
| 144 | + assertThat(numRunning, greaterThanOrEqualTo(perNodeLimit)); |
| 145 | + assertTrue(perNodeShardSnapshotCounter.completeShardSnapshotOnNode(snapshotNodeId)); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private int runningShardSnapshotsForNode(SnapshotsInProgress snapshotsInProgress, String nodeId) { |
| 152 | + int result = 0; |
| 153 | + final var allEntries = snapshotsInProgress.asStream().toList(); |
| 154 | + for (var entry : allEntries) { |
| 155 | + if (entry.isClone() == false && entry.state().completed() == false) { |
| 156 | + for (ShardSnapshotStatus shardSnapshot : entry.shards().values()) { |
| 157 | + if (nodeId.equals(shardSnapshot.nodeId())) { |
| 158 | + if (shardSnapshot.state() == SnapshotsInProgress.ShardState.INIT) { |
| 159 | + result++; |
| 160 | + } else if (shardSnapshot.state() == SnapshotsInProgress.ShardState.ABORTED |
| 161 | + && (shardSnapshot.reason() == null || shardSnapshot.reason().startsWith("assigned-queued aborted") == false)) { |
| 162 | + result++; |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + return result; |
| 169 | + } |
| 170 | +} |
0 commit comments