|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.action.admin.indices.close; |
| 20 | + |
| 21 | +import org.elasticsearch.action.admin.indices.flush.FlushRequest; |
| 22 | +import org.elasticsearch.action.support.ActionFilters; |
| 23 | +import org.elasticsearch.cluster.ClusterName; |
| 24 | +import org.elasticsearch.cluster.ClusterState; |
| 25 | +import org.elasticsearch.cluster.action.shard.ShardStateAction; |
| 26 | +import org.elasticsearch.cluster.block.ClusterBlocks; |
| 27 | +import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
| 28 | +import org.elasticsearch.cluster.service.ClusterService; |
| 29 | +import org.elasticsearch.common.settings.Settings; |
| 30 | +import org.elasticsearch.index.seqno.SeqNoStats; |
| 31 | +import org.elasticsearch.index.seqno.SequenceNumbers; |
| 32 | +import org.elasticsearch.index.shard.IndexShard; |
| 33 | +import org.elasticsearch.index.shard.ShardId; |
| 34 | +import org.elasticsearch.indices.IndicesService; |
| 35 | +import org.elasticsearch.test.ESTestCase; |
| 36 | +import org.elasticsearch.threadpool.ThreadPool; |
| 37 | +import org.elasticsearch.transport.TransportService; |
| 38 | +import org.junit.Before; |
| 39 | + |
| 40 | +import static org.elasticsearch.cluster.metadata.MetaDataIndexStateService.INDEX_CLOSED_BLOCK; |
| 41 | +import static org.hamcrest.Matchers.equalTo; |
| 42 | +import static org.mockito.Matchers.any; |
| 43 | +import static org.mockito.Mockito.mock; |
| 44 | +import static org.mockito.Mockito.times; |
| 45 | +import static org.mockito.Mockito.verify; |
| 46 | +import static org.mockito.Mockito.when; |
| 47 | + |
| 48 | +public class TransportVerifyShardBeforeCloseActionTests extends ESTestCase { |
| 49 | + |
| 50 | + private IndexShard indexShard; |
| 51 | + private TransportVerifyShardBeforeCloseAction action; |
| 52 | + private ClusterService clusterService; |
| 53 | + |
| 54 | + @Override |
| 55 | + @Before |
| 56 | + public void setUp() throws Exception { |
| 57 | + super.setUp(); |
| 58 | + |
| 59 | + indexShard = mock(IndexShard.class); |
| 60 | + when(indexShard.getActiveOperationsCount()).thenReturn(0); |
| 61 | + when(indexShard.getGlobalCheckpoint()).thenReturn(0L); |
| 62 | + when(indexShard.seqNoStats()).thenReturn(new SeqNoStats(0L, 0L, 0L)); |
| 63 | + |
| 64 | + final ShardId shardId = new ShardId("index", "_na_", randomIntBetween(0, 3)); |
| 65 | + when(indexShard.shardId()).thenReturn(shardId); |
| 66 | + |
| 67 | + clusterService = mock(ClusterService.class); |
| 68 | + when(clusterService.state()).thenReturn(new ClusterState.Builder(new ClusterName("test")) |
| 69 | + .blocks(ClusterBlocks.builder().addIndexBlock("index", INDEX_CLOSED_BLOCK).build()).build()); |
| 70 | + |
| 71 | + action = new TransportVerifyShardBeforeCloseAction(Settings.EMPTY, mock(TransportService.class), clusterService, |
| 72 | + mock(IndicesService.class), mock(ThreadPool.class), mock(ShardStateAction.class), mock(ActionFilters.class), |
| 73 | + mock(IndexNameExpressionResolver.class)); |
| 74 | + } |
| 75 | + |
| 76 | + private void executeOnPrimaryOrReplica() throws Exception { |
| 77 | + final TransportVerifyShardBeforeCloseAction.ShardCloseRequest request = |
| 78 | + new TransportVerifyShardBeforeCloseAction.ShardCloseRequest(indexShard.shardId()); |
| 79 | + if (randomBoolean()) { |
| 80 | + assertNotNull(action.shardOperationOnPrimary(request, indexShard)); |
| 81 | + } else { |
| 82 | + assertNotNull(action.shardOperationOnPrimary(request, indexShard)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public void testOperationSuccessful() throws Exception { |
| 87 | + executeOnPrimaryOrReplica(); |
| 88 | + verify(indexShard, times(1)).flush(any(FlushRequest.class)); |
| 89 | + } |
| 90 | + |
| 91 | + public void testOperationFailsWithOnGoingOps() { |
| 92 | + when(indexShard.getActiveOperationsCount()).thenReturn(randomIntBetween(1, 10)); |
| 93 | + |
| 94 | + IllegalStateException exception = expectThrows(IllegalStateException.class, this::executeOnPrimaryOrReplica); |
| 95 | + assertThat(exception.getMessage(), |
| 96 | + equalTo("On-going operations in progress while checking index shard " + indexShard.shardId() + " before closing")); |
| 97 | + verify(indexShard, times(0)).flush(any(FlushRequest.class)); |
| 98 | + } |
| 99 | + |
| 100 | + public void testOperationFailsWithNoBlock() { |
| 101 | + when(clusterService.state()).thenReturn(new ClusterState.Builder(new ClusterName("test")).build()); |
| 102 | + |
| 103 | + IllegalStateException exception = expectThrows(IllegalStateException.class, this::executeOnPrimaryOrReplica); |
| 104 | + assertThat(exception.getMessage(), |
| 105 | + equalTo("Index shard " + indexShard.shardId() + " must be blocked by " + INDEX_CLOSED_BLOCK + " before closing")); |
| 106 | + verify(indexShard, times(0)).flush(any(FlushRequest.class)); |
| 107 | + } |
| 108 | + |
| 109 | + public void testOperationFailsWithGlobalCheckpointNotCaughtUp() { |
| 110 | + final long maxSeqNo = randomLongBetween(SequenceNumbers.UNASSIGNED_SEQ_NO, Long.MAX_VALUE); |
| 111 | + final long localCheckpoint = randomLongBetween(SequenceNumbers.UNASSIGNED_SEQ_NO, maxSeqNo); |
| 112 | + final long globalCheckpoint = randomValueOtherThan(maxSeqNo, |
| 113 | + () -> randomLongBetween(SequenceNumbers.UNASSIGNED_SEQ_NO, localCheckpoint)); |
| 114 | + when(indexShard.seqNoStats()).thenReturn(new SeqNoStats(maxSeqNo, localCheckpoint, globalCheckpoint)); |
| 115 | + when(indexShard.getGlobalCheckpoint()).thenReturn(globalCheckpoint); |
| 116 | + |
| 117 | + IllegalStateException exception = expectThrows(IllegalStateException.class, this::executeOnPrimaryOrReplica); |
| 118 | + assertThat(exception.getMessage(), equalTo("Global checkpoint [" + globalCheckpoint + "] mismatches maximum sequence number [" |
| 119 | + + maxSeqNo + "] on index shard " + indexShard.shardId())); |
| 120 | + verify(indexShard, times(0)).flush(any(FlushRequest.class)); |
| 121 | + } |
| 122 | +} |
0 commit comments