|
9 | 9 | package org.elasticsearch.indices;
|
10 | 10 |
|
11 | 11 | import org.elasticsearch.action.ActionListener;
|
| 12 | +import org.elasticsearch.action.support.PlainActionFuture; |
12 | 13 | import org.elasticsearch.cluster.metadata.IndexMetadata;
|
13 | 14 | import org.elasticsearch.cluster.node.DiscoveryNode;
|
14 | 15 | import org.elasticsearch.cluster.node.DiscoveryNodeUtils;
|
|
24 | 25 | import org.elasticsearch.index.seqno.RetentionLeaseSyncer;
|
25 | 26 | import org.elasticsearch.index.shard.IndexEventListener;
|
26 | 27 | import org.elasticsearch.index.shard.IndexShard;
|
| 28 | +import org.elasticsearch.index.shard.IndexShardState; |
27 | 29 | import org.elasticsearch.index.shard.IndexShardTestCase;
|
28 | 30 | import org.elasticsearch.index.shard.ShardId;
|
29 | 31 | import org.elasticsearch.indices.cluster.IndexRemovalReason;
|
30 | 32 | import org.elasticsearch.indices.recovery.RecoveryState;
|
31 | 33 | import org.elasticsearch.test.ESSingleNodeTestCase;
|
32 | 34 |
|
33 | 35 | import java.util.Arrays;
|
| 36 | +import java.util.concurrent.TimeUnit; |
34 | 37 | import java.util.concurrent.atomic.AtomicInteger;
|
| 38 | +import java.util.concurrent.locks.LockSupport; |
35 | 39 |
|
36 | 40 | import static java.util.Collections.emptySet;
|
37 | 41 | import static org.elasticsearch.indices.cluster.IndexRemovalReason.DELETED;
|
38 | 42 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
| 43 | +import static org.hamcrest.Matchers.equalTo; |
39 | 44 |
|
40 | 45 | public class IndicesLifecycleListenerSingleNodeTests extends ESSingleNodeTestCase {
|
41 | 46 |
|
@@ -106,7 +111,6 @@ public void afterIndexRemoved(Index index, IndexSettings indexSettings, IndexRem
|
106 | 111 | assertEquals(9, counter.get());
|
107 | 112 | counter.incrementAndGet();
|
108 | 113 | }
|
109 |
| - |
110 | 114 | };
|
111 | 115 | indicesService.removeIndex(idx, DELETED, "simon says", EsExecutors.DIRECT_EXECUTOR_SERVICE, ActionListener.noop());
|
112 | 116 | try {
|
@@ -136,4 +140,48 @@ public void afterIndexRemoved(Index index, IndexSettings indexSettings, IndexRem
|
136 | 140 | assertEquals(10, counter.get());
|
137 | 141 | }
|
138 | 142 |
|
| 143 | + public void testAfterRecoveryCallbackTriggeredWhileStillInRecoveryState() throws Throwable { |
| 144 | + IndicesService indicesService = getInstanceFromNode(IndicesService.class); |
| 145 | + assertAcked(client().admin().indices().prepareCreate("test").setSettings(indexSettings(1, 0))); |
| 146 | + ensureGreen(); |
| 147 | + Index idx = resolveIndex("test"); |
| 148 | + IndexMetadata metadata = indicesService.indexService(idx).getMetadata(); |
| 149 | + ShardRouting shardRouting = indicesService.indexService(idx).getShard(0).routingEntry(); |
| 150 | + PlainActionFuture<Void> recoveryTriggered = new PlainActionFuture<>(); |
| 151 | + IndexEventListener recoveryListener = new IndexEventListener() { |
| 152 | + |
| 153 | + @Override |
| 154 | + public void afterIndexShardRecovery(IndexShard indexShard, ActionListener<Void> listener) { |
| 155 | + // Pause to ensure we do not transition to post recovery until after the recovery is complete |
| 156 | + LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(30)); |
| 157 | + assertThat(indexShard.recoveryState().getStage(), equalTo(RecoveryState.Stage.DONE)); |
| 158 | + assertThat(indexShard.state(), equalTo(IndexShardState.RECOVERING)); |
| 159 | + recoveryTriggered.onResponse(null); |
| 160 | + listener.onResponse(null); |
| 161 | + } |
| 162 | + }; |
| 163 | + indicesService.removeIndex(idx, DELETED, "delete", EsExecutors.DIRECT_EXECUTOR_SERVICE, ActionListener.noop()); |
| 164 | + try { |
| 165 | + IndexService index = indicesService.createIndex(metadata, Arrays.asList(recoveryListener), false); |
| 166 | + idx = index.index(); |
| 167 | + ShardRouting newRouting = shardRouting; |
| 168 | + String nodeId = newRouting.currentNodeId(); |
| 169 | + UnassignedInfo unassignedInfo = new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "boom"); |
| 170 | + newRouting = newRouting.moveToUnassigned(unassignedInfo) |
| 171 | + .updateUnassigned(unassignedInfo, RecoverySource.EmptyStoreRecoverySource.INSTANCE); |
| 172 | + newRouting = ShardRoutingHelper.initialize(newRouting, nodeId); |
| 173 | + IndexShard shard = index.createShard(newRouting, IndexShardTestCase.NOOP_GCP_SYNCER, RetentionLeaseSyncer.EMPTY); |
| 174 | + IndexShardTestCase.updateRoutingEntry(shard, newRouting); |
| 175 | + final DiscoveryNode localNode = DiscoveryNodeUtils.builder("foo").roles(emptySet()).build(); |
| 176 | + shard.markAsRecovering("store", new RecoveryState(newRouting, localNode, null)); |
| 177 | + IndexShardTestCase.recoverFromStore(shard); |
| 178 | + assertBusy(() -> assertThat(shard.state(), equalTo(IndexShardState.POST_RECOVERY))); |
| 179 | + newRouting = ShardRoutingHelper.moveToStarted(newRouting); |
| 180 | + IndexShardTestCase.updateRoutingEntry(shard, newRouting); |
| 181 | + recoveryTriggered.actionGet(); |
| 182 | + assertBusy(() -> assertThat(shard.state(), equalTo(IndexShardState.STARTED))); |
| 183 | + } finally { |
| 184 | + indicesService.removeIndex(idx, DELETED, "simon says", EsExecutors.DIRECT_EXECUTOR_SERVICE, ActionListener.noop()); |
| 185 | + } |
| 186 | + } |
139 | 187 | }
|
0 commit comments