Skip to content

Commit 799f731

Browse files
committed
Add dedicated exception
1 parent fea358b commit 799f731

File tree

7 files changed

+57
-10
lines changed

7 files changed

+57
-10
lines changed

server/src/main/java/org/elasticsearch/ElasticsearchException.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,12 @@ private enum ElasticsearchExceptionHandle {
10221022
org.elasticsearch.index.seqno.RetentionLeaseNotFoundException.class,
10231023
org.elasticsearch.index.seqno.RetentionLeaseNotFoundException::new,
10241024
154,
1025-
Version.V_6_7_0);
1025+
Version.V_6_7_0),
1026+
SHARD_NOT_IN_PRIMARY_MODE_EXCEPTION(
1027+
org.elasticsearch.index.shard.ShardNotInPrimaryModeException.class,
1028+
org.elasticsearch.index.shard.ShardNotInPrimaryModeException::new,
1029+
155,
1030+
Version.V_6_8_1);
10261031

10271032
final Class<? extends ElasticsearchException> exceptionClass;
10281033
final CheckedFunction<StreamInput, ? extends ElasticsearchException, IOException> constructor;

server/src/main/java/org/elasticsearch/action/support/replication/TransportReplicationAction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import org.elasticsearch.index.shard.ReplicationGroup;
6464
import org.elasticsearch.index.shard.ShardId;
6565
import org.elasticsearch.index.shard.ShardNotFoundException;
66+
import org.elasticsearch.index.shard.ShardNotInPrimaryModeException;
6667
import org.elasticsearch.indices.IndexClosedException;
6768
import org.elasticsearch.indices.IndicesService;
6869
import org.elasticsearch.node.NodeClosedException;
@@ -313,7 +314,7 @@ protected void doRun() throws Exception {
313314
ActionListener.wrap(
314315
releasable -> runWithPrimaryShardReference(new PrimaryShardReference(indexShard, releasable)),
315316
e -> {
316-
if (e instanceof IllegalStateException && e.getMessage().equals("shard is not in primary mode")) {
317+
if (e instanceof ShardNotInPrimaryModeException) {
317318
onFailure(new ReplicationOperation.RetryOnPrimaryException(shardId, "shard is not in primary mode", e));
318319
} else {
319320
onFailure(e);

server/src/main/java/org/elasticsearch/index/shard/IndexShard.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2522,7 +2522,7 @@ private ActionListener<Releasable> wrapPrimaryOperationPermitListener(final Acti
25222522
(l, r) -> {
25232523
if (replicationTracker.isPrimaryMode() == false) {
25242524
r.close();
2525-
l.onFailure(new IllegalStateException("shard is not in primary mode"));
2525+
l.onFailure(new ShardNotInPrimaryModeException(shardId, state));
25262526
} else {
25272527
l.onResponse(r);
25282528
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
20+
package org.elasticsearch.index.shard;
21+
22+
import org.elasticsearch.common.io.stream.StreamInput;
23+
24+
import java.io.IOException;
25+
26+
public class ShardNotInPrimaryModeException extends IllegalIndexShardStateException {
27+
28+
public ShardNotInPrimaryModeException(final ShardId shardId, final IndexShardState currentState) {
29+
super(shardId, currentState, "shard is not in primary mode");
30+
}
31+
32+
public ShardNotInPrimaryModeException(final StreamInput in) throws IOException {
33+
super(in);
34+
}
35+
36+
}

server/src/test/java/org/elasticsearch/ExceptionSerializationTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import org.elasticsearch.index.shard.IllegalIndexShardStateException;
6767
import org.elasticsearch.index.shard.IndexShardState;
6868
import org.elasticsearch.index.shard.ShardId;
69+
import org.elasticsearch.index.shard.ShardNotInPrimaryModeException;
6970
import org.elasticsearch.indices.IndexTemplateMissingException;
7071
import org.elasticsearch.indices.InvalidIndexTemplateException;
7172
import org.elasticsearch.indices.recovery.RecoverFilesRecoveryException;
@@ -816,6 +817,7 @@ public void testIds() {
816817
ids.put(152, NoSuchRemoteClusterException.class);
817818
ids.put(153, RetentionLeaseAlreadyExistsException.class);
818819
ids.put(154, RetentionLeaseNotFoundException.class);
820+
ids.put(155, ShardNotInPrimaryModeException.class);
819821

820822
Map<Class<? extends ElasticsearchException>, Integer> reverse = new HashMap<>();
821823
for (Map.Entry<Integer, Class<? extends ElasticsearchException>> entry : ids.entrySet()) {

server/src/test/java/org/elasticsearch/action/support/replication/TransportReplicationActionTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@
6464
import org.elasticsearch.index.IndexService;
6565
import org.elasticsearch.index.shard.IndexShard;
6666
import org.elasticsearch.index.shard.IndexShardClosedException;
67+
import org.elasticsearch.index.shard.IndexShardState;
6768
import org.elasticsearch.index.shard.ReplicationGroup;
6869
import org.elasticsearch.index.shard.ShardId;
6970
import org.elasticsearch.index.shard.ShardNotFoundException;
71+
import org.elasticsearch.index.shard.ShardNotInPrimaryModeException;
7072
import org.elasticsearch.indices.IndexClosedException;
7173
import org.elasticsearch.indices.IndicesService;
7274
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
@@ -423,7 +425,7 @@ protected void shardOperationOnPrimary(Request shardRequest, IndexShard primary,
423425
final ExecutionException e = expectThrows(ExecutionException.class, listener::get);
424426
assertThat(e.getCause(), instanceOf(ReplicationOperation.RetryOnPrimaryException.class));
425427
assertThat(e.getCause(), hasToString(containsString("shard is not in primary mode")));
426-
assertThat(e.getCause().getCause(), instanceOf(IllegalStateException.class));
428+
assertThat(e.getCause().getCause(), instanceOf(ShardNotInPrimaryModeException.class));
427429
assertThat(e.getCause().getCause(), hasToString(containsString("shard is not in primary mode")));
428430
}
429431

@@ -1310,14 +1312,15 @@ private IndexService mockIndexService(final IndexMetaData indexMetaData, Cluster
13101312
private IndexShard mockIndexShard(ShardId shardId, ClusterService clusterService) {
13111313
final IndexShard indexShard = mock(IndexShard.class);
13121314
when(indexShard.shardId()).thenReturn(shardId);
1315+
when(indexShard.state()).thenReturn(IndexShardState.STARTED);
13131316
doAnswer(invocation -> {
13141317
ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[0];
13151318
if (isPrimaryMode.get()) {
13161319
count.incrementAndGet();
13171320
callback.onResponse(count::decrementAndGet);
13181321

13191322
} else {
1320-
callback.onFailure(new IllegalStateException("shard is not in primary mode"));
1323+
callback.onFailure(new ShardNotInPrimaryModeException(shardId, IndexShardState.STARTED));
13211324
}
13221325
return null;
13231326
}).when(indexShard).acquirePrimaryOperationPermit(any(ActionListener.class), anyString(), anyObject());

server/src/test/java/org/elasticsearch/index/shard/IndexShardTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ public void onResponse(final Releasable releasable) {
688688

689689
@Override
690690
public void onFailure(final Exception e) {
691-
assertThat(e, instanceOf(IllegalStateException.class));
691+
assertThat(e, instanceOf(ShardNotInPrimaryModeException.class));
692692
assertThat(e, hasToString(containsString("shard is not in primary mode")));
693693
}
694694
},
@@ -705,7 +705,7 @@ public void onResponse(final Releasable releasable) {
705705

706706
@Override
707707
public void onFailure(final Exception e) {
708-
assertThat(e, instanceOf(IllegalStateException.class));
708+
assertThat(e, instanceOf(ShardNotInPrimaryModeException.class));
709709
assertThat(e, hasToString(containsString("shard is not in primary mode")));
710710
latch.countDown();
711711
}
@@ -1730,7 +1730,7 @@ public void testLockingBeforeAndAfterRelocated() throws Exception {
17301730
recoveryThread.join();
17311731
assertTrue(shard.isRelocatedPrimary());
17321732
final ExecutionException e = expectThrows(ExecutionException.class, () -> acquirePrimaryOperationPermitBlockingly(shard));
1733-
assertThat(e.getCause(), instanceOf(IllegalStateException.class));
1733+
assertThat(e.getCause(), instanceOf(ShardNotInPrimaryModeException.class));
17341734
assertThat(e.getCause(), hasToString(containsString("shard is not in primary mode")));
17351735

17361736
closeShards(shard);
@@ -1783,14 +1783,14 @@ public void onFailure(Exception e) {
17831783
onLockAcquired = new PlainActionFuture<>();
17841784
assertion = () -> {
17851785
final ExecutionException e = expectThrows(ExecutionException.class, () -> onLockAcquired.get(30, TimeUnit.SECONDS));
1786-
assertThat(e.getCause(), instanceOf(IllegalStateException.class));
1786+
assertThat(e.getCause(), instanceOf(ShardNotInPrimaryModeException.class));
17871787
assertThat(e.getCause(), hasToString(containsString("shard is not in primary mode")));
17881788
};
17891789
} else {
17901790
onLockAcquired = new PlainActionFuture<>();
17911791
assertion = () -> {
17921792
final ExecutionException e = expectThrows(ExecutionException.class, () -> onLockAcquired.get(30, TimeUnit.SECONDS));
1793-
assertThat(e.getCause(), instanceOf(IllegalStateException.class));
1793+
assertThat(e.getCause(), instanceOf(ShardNotInPrimaryModeException.class));
17941794
assertThat(e.getCause(), hasToString(containsString("shard is not in primary mode")));
17951795
};
17961796
}

0 commit comments

Comments
 (0)