|
| 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.searchablesnapshots.allocation.decider; |
| 9 | + |
| 10 | +import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 11 | +import org.elasticsearch.cluster.node.DiscoveryNode; |
| 12 | +import org.elasticsearch.cluster.routing.allocation.decider.Decision; |
| 13 | +import org.elasticsearch.index.IndexVersion; |
| 14 | +import org.elasticsearch.test.ESTestCase; |
| 15 | +import org.elasticsearch.xpack.searchablesnapshots.cache.shared.FrozenCacheInfoService; |
| 16 | +import org.elasticsearch.xpack.searchablesnapshots.cache.shared.FrozenCacheInfoService.NodeState; |
| 17 | + |
| 18 | +import static org.elasticsearch.index.IndexModule.INDEX_STORE_TYPE_SETTING; |
| 19 | +import static org.elasticsearch.snapshots.SearchableSnapshotsSettings.SEARCHABLE_SNAPSHOT_PARTIAL_SETTING_KEY; |
| 20 | +import static org.hamcrest.Matchers.equalTo; |
| 21 | +import static org.hamcrest.Matchers.is; |
| 22 | +import static org.mockito.ArgumentMatchers.any; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | + |
| 26 | +public class HasFrozenCacheAllocationDeciderTests extends ESTestCase { |
| 27 | + |
| 28 | + public void testCanAllocateToNode() { |
| 29 | + final var frozenCacheService = mock(FrozenCacheInfoService.class); |
| 30 | + final var decider = new HasFrozenCacheAllocationDecider(frozenCacheService); |
| 31 | + |
| 32 | + final var indexMetadata = IndexMetadata.builder(randomIdentifier()) |
| 33 | + .settings(indexSettings(IndexVersion.current(), between(1, 10), between(0, 2))) |
| 34 | + .build(); |
| 35 | + |
| 36 | + for (var nodeState : NodeState.values()) { |
| 37 | + when(frozenCacheService.getNodeState(any(DiscoveryNode.class))).thenReturn(nodeState); |
| 38 | + assertThat(decider.canAllocateToNode(indexMetadata, mock(DiscoveryNode.class)), equalTo(Decision.ALWAYS)); |
| 39 | + } |
| 40 | + |
| 41 | + final var partialSearchableSnapshotIndexMetadata = IndexMetadata.builder(randomIdentifier()) |
| 42 | + .settings( |
| 43 | + indexSettings(IndexVersion.current(), between(1, 10), between(0, 2)).put(INDEX_STORE_TYPE_SETTING.getKey(), "snapshot") |
| 44 | + .put(SEARCHABLE_SNAPSHOT_PARTIAL_SETTING_KEY, true) |
| 45 | + ) |
| 46 | + .build(); |
| 47 | + |
| 48 | + for (var nodeState : NodeState.values()) { |
| 49 | + when(frozenCacheService.getNodeState(any(DiscoveryNode.class))).thenReturn(nodeState); |
| 50 | + final Decision decision = decider.canAllocateToNode(partialSearchableSnapshotIndexMetadata, mock(DiscoveryNode.class)); |
| 51 | + final Decision.Type expectedType; |
| 52 | + if (nodeState == NodeState.HAS_CACHE) { |
| 53 | + expectedType = Decision.Type.YES; |
| 54 | + } else if (nodeState == NodeState.FETCHING) { |
| 55 | + expectedType = Decision.Type.THROTTLE; |
| 56 | + } else { |
| 57 | + expectedType = Decision.Type.NO; |
| 58 | + } |
| 59 | + assertThat("incorrect decision for " + nodeState, decision.type(), is(expectedType)); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments