Skip to content

Commit

Permalink
Add dedicated exception
Browse files Browse the repository at this point in the history
  • Loading branch information
jasontedor committed May 21, 2019
1 parent fea358b commit 799f731
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,12 @@ private enum ElasticsearchExceptionHandle {
org.elasticsearch.index.seqno.RetentionLeaseNotFoundException.class,
org.elasticsearch.index.seqno.RetentionLeaseNotFoundException::new,
154,
Version.V_6_7_0);
Version.V_6_7_0),
SHARD_NOT_IN_PRIMARY_MODE_EXCEPTION(
org.elasticsearch.index.shard.ShardNotInPrimaryModeException.class,
org.elasticsearch.index.shard.ShardNotInPrimaryModeException::new,
155,
Version.V_6_8_1);

final Class<? extends ElasticsearchException> exceptionClass;
final CheckedFunction<StreamInput, ? extends ElasticsearchException, IOException> constructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.elasticsearch.index.shard.ReplicationGroup;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.shard.ShardNotFoundException;
import org.elasticsearch.index.shard.ShardNotInPrimaryModeException;
import org.elasticsearch.indices.IndexClosedException;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.node.NodeClosedException;
Expand Down Expand Up @@ -313,7 +314,7 @@ protected void doRun() throws Exception {
ActionListener.wrap(
releasable -> runWithPrimaryShardReference(new PrimaryShardReference(indexShard, releasable)),
e -> {
if (e instanceof IllegalStateException && e.getMessage().equals("shard is not in primary mode")) {
if (e instanceof ShardNotInPrimaryModeException) {
onFailure(new ReplicationOperation.RetryOnPrimaryException(shardId, "shard is not in primary mode", e));
} else {
onFailure(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2522,7 +2522,7 @@ private ActionListener<Releasable> wrapPrimaryOperationPermitListener(final Acti
(l, r) -> {
if (replicationTracker.isPrimaryMode() == false) {
r.close();
l.onFailure(new IllegalStateException("shard is not in primary mode"));
l.onFailure(new ShardNotInPrimaryModeException(shardId, state));
} else {
l.onResponse(r);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.index.shard;

import org.elasticsearch.common.io.stream.StreamInput;

import java.io.IOException;

public class ShardNotInPrimaryModeException extends IllegalIndexShardStateException {

public ShardNotInPrimaryModeException(final ShardId shardId, final IndexShardState currentState) {
super(shardId, currentState, "shard is not in primary mode");
}

public ShardNotInPrimaryModeException(final StreamInput in) throws IOException {
super(in);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.elasticsearch.index.shard.IllegalIndexShardStateException;
import org.elasticsearch.index.shard.IndexShardState;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.shard.ShardNotInPrimaryModeException;
import org.elasticsearch.indices.IndexTemplateMissingException;
import org.elasticsearch.indices.InvalidIndexTemplateException;
import org.elasticsearch.indices.recovery.RecoverFilesRecoveryException;
Expand Down Expand Up @@ -816,6 +817,7 @@ public void testIds() {
ids.put(152, NoSuchRemoteClusterException.class);
ids.put(153, RetentionLeaseAlreadyExistsException.class);
ids.put(154, RetentionLeaseNotFoundException.class);
ids.put(155, ShardNotInPrimaryModeException.class);

Map<Class<? extends ElasticsearchException>, Integer> reverse = new HashMap<>();
for (Map.Entry<Integer, Class<? extends ElasticsearchException>> entry : ids.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.shard.IndexShard;
import org.elasticsearch.index.shard.IndexShardClosedException;
import org.elasticsearch.index.shard.IndexShardState;
import org.elasticsearch.index.shard.ReplicationGroup;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.shard.ShardNotFoundException;
import org.elasticsearch.index.shard.ShardNotInPrimaryModeException;
import org.elasticsearch.indices.IndexClosedException;
import org.elasticsearch.indices.IndicesService;
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
Expand Down Expand Up @@ -423,7 +425,7 @@ protected void shardOperationOnPrimary(Request shardRequest, IndexShard primary,
final ExecutionException e = expectThrows(ExecutionException.class, listener::get);
assertThat(e.getCause(), instanceOf(ReplicationOperation.RetryOnPrimaryException.class));
assertThat(e.getCause(), hasToString(containsString("shard is not in primary mode")));
assertThat(e.getCause().getCause(), instanceOf(IllegalStateException.class));
assertThat(e.getCause().getCause(), instanceOf(ShardNotInPrimaryModeException.class));
assertThat(e.getCause().getCause(), hasToString(containsString("shard is not in primary mode")));
}

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

} else {
callback.onFailure(new IllegalStateException("shard is not in primary mode"));
callback.onFailure(new ShardNotInPrimaryModeException(shardId, IndexShardState.STARTED));
}
return null;
}).when(indexShard).acquirePrimaryOperationPermit(any(ActionListener.class), anyString(), anyObject());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ public void onResponse(final Releasable releasable) {

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

@Override
public void onFailure(final Exception e) {
assertThat(e, instanceOf(IllegalStateException.class));
assertThat(e, instanceOf(ShardNotInPrimaryModeException.class));
assertThat(e, hasToString(containsString("shard is not in primary mode")));
latch.countDown();
}
Expand Down Expand Up @@ -1730,7 +1730,7 @@ public void testLockingBeforeAndAfterRelocated() throws Exception {
recoveryThread.join();
assertTrue(shard.isRelocatedPrimary());
final ExecutionException e = expectThrows(ExecutionException.class, () -> acquirePrimaryOperationPermitBlockingly(shard));
assertThat(e.getCause(), instanceOf(IllegalStateException.class));
assertThat(e.getCause(), instanceOf(ShardNotInPrimaryModeException.class));
assertThat(e.getCause(), hasToString(containsString("shard is not in primary mode")));

closeShards(shard);
Expand Down Expand Up @@ -1783,14 +1783,14 @@ public void onFailure(Exception e) {
onLockAcquired = new PlainActionFuture<>();
assertion = () -> {
final ExecutionException e = expectThrows(ExecutionException.class, () -> onLockAcquired.get(30, TimeUnit.SECONDS));
assertThat(e.getCause(), instanceOf(IllegalStateException.class));
assertThat(e.getCause(), instanceOf(ShardNotInPrimaryModeException.class));
assertThat(e.getCause(), hasToString(containsString("shard is not in primary mode")));
};
} else {
onLockAcquired = new PlainActionFuture<>();
assertion = () -> {
final ExecutionException e = expectThrows(ExecutionException.class, () -> onLockAcquired.get(30, TimeUnit.SECONDS));
assertThat(e.getCause(), instanceOf(IllegalStateException.class));
assertThat(e.getCause(), instanceOf(ShardNotInPrimaryModeException.class));
assertThat(e.getCause(), hasToString(containsString("shard is not in primary mode")));
};
}
Expand Down

0 comments on commit 799f731

Please sign in to comment.