Skip to content

Commit d80562d

Browse files
committed
Remove IndexTemplateAlreadyExistsException and IndexShardAlreadyExistsException (#21539)
Both exception can be replaced with java built-in exception, IAE and ISE respectively. This should be back ported partially to 5.x which the transport layer code should be preserved. Relates to #21494
1 parent 4f82898 commit d80562d

File tree

9 files changed

+11
-17
lines changed

9 files changed

+11
-17
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ enum ElasticsearchExceptionHandle {
554554
org.elasticsearch.common.io.stream.NotSerializableExceptionWrapper::new, 62),
555555
ALIAS_FILTER_PARSING_EXCEPTION(org.elasticsearch.indices.AliasFilterParsingException.class,
556556
org.elasticsearch.indices.AliasFilterParsingException::new, 63),
557-
// 64 was DeleteByQueryFailedEngineException, which was removed in 3.0
557+
// 64 was DeleteByQueryFailedEngineException, which was removed in 5.0
558558
GATEWAY_EXCEPTION(org.elasticsearch.gateway.GatewayException.class, org.elasticsearch.gateway.GatewayException::new, 65),
559559
INDEX_SHARD_NOT_RECOVERING_EXCEPTION(org.elasticsearch.index.shard.IndexShardNotRecoveringException.class,
560560
org.elasticsearch.index.shard.IndexShardNotRecoveringException::new, 66),

core/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public Integer version() {
142142

143143
/**
144144
* Set to <tt>true</tt> to force only creation, not an update of an index template. If it already
145-
* exists, it will fail with an {@link org.elasticsearch.indices.IndexTemplateAlreadyExistsException}.
145+
* exists, it will fail with an {@link IllegalArgumentException}.
146146
*/
147147
public PutIndexTemplateRequest create(boolean create) {
148148
this.create = create;

core/src/main/java/org/elasticsearch/action/admin/indices/template/put/PutIndexTemplateRequestBuilder.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public PutIndexTemplateRequestBuilder setVersion(Integer version) {
6767

6868
/**
6969
* Set to <tt>true</tt> to force only creation, not an update of an index template. If it already
70-
* exists, it will fail with an {@link org.elasticsearch.indices.IndexTemplateAlreadyExistsException}.
70+
* exists, it will fail with an {@link IllegalArgumentException}.
7171
*/
7272
public PutIndexTemplateRequestBuilder setCreate(boolean create) {
7373
request.create(create);

core/src/main/java/org/elasticsearch/cluster/metadata/MetaDataIndexTemplateService.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.elasticsearch.index.IndexService;
4141
import org.elasticsearch.index.mapper.MapperParsingException;
4242
import org.elasticsearch.index.mapper.MapperService;
43-
import org.elasticsearch.indices.IndexTemplateAlreadyExistsException;
4443
import org.elasticsearch.indices.IndexTemplateMissingException;
4544
import org.elasticsearch.indices.IndicesService;
4645
import org.elasticsearch.indices.InvalidIndexTemplateException;
@@ -161,7 +160,7 @@ public void onFailure(String source, Exception e) {
161160
@Override
162161
public ClusterState execute(ClusterState currentState) throws Exception {
163162
if (request.create && currentState.metaData().templates().containsKey(request.name)) {
164-
throw new IndexTemplateAlreadyExistsException(request.name);
163+
throw new IllegalArgumentException("index_template [" + request.name + "] already exists");
165164
}
166165

167166
validateAndAddTemplate(request, templateBuilder, indicesService);

core/src/main/java/org/elasticsearch/index/IndexService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ public synchronized IndexShard createShard(ShardRouting routing) throws IOExcept
330330
}
331331

332332
if (shards.containsKey(shardId.id())) {
333-
throw new IndexShardAlreadyExistsException(shardId + " already exists");
333+
throw new IllegalStateException(shardId + " already exists");
334334
}
335335

336336
logger.debug("creating shard_id {}", shardId);

core/src/main/java/org/elasticsearch/index/IndexShardAlreadyExistsException.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
/**
2828
*
29+
* @deprecated use {@link IllegalStateException} instead
2930
*/
31+
@Deprecated
3032
public class IndexShardAlreadyExistsException extends ElasticsearchException {
3133

3234
public IndexShardAlreadyExistsException(String message) {
@@ -36,4 +38,4 @@ public IndexShardAlreadyExistsException(String message) {
3638
public IndexShardAlreadyExistsException(StreamInput in) throws IOException {
3739
super(in);
3840
}
39-
}
41+
}

core/src/main/java/org/elasticsearch/indices/IndexTemplateAlreadyExistsException.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
import java.io.IOException;
2727

2828
/**
29-
*
29+
* @deprecated use {@link IllegalArgumentException} instead
3030
*/
31+
@Deprecated
3132
public class IndexTemplateAlreadyExistsException extends ElasticsearchException {
3233

3334
private final String name;

core/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java

-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.elasticsearch.cluster.metadata.IndexMetaData;
3232
import org.elasticsearch.cluster.node.DiscoveryNode;
3333
import org.elasticsearch.cluster.node.DiscoveryNodes;
34-
import org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource;
3534
import org.elasticsearch.cluster.routing.RecoverySource.Type;
3635
import org.elasticsearch.cluster.routing.RoutingNode;
3736
import org.elasticsearch.cluster.routing.RoutingTable;
@@ -40,7 +39,6 @@
4039
import org.elasticsearch.common.Nullable;
4140
import org.elasticsearch.common.component.AbstractLifecycleComponent;
4241
import org.elasticsearch.common.inject.Inject;
43-
import org.elasticsearch.common.lucene.Lucene;
4442
import org.elasticsearch.common.settings.Settings;
4543
import org.elasticsearch.common.unit.TimeValue;
4644
import org.elasticsearch.common.util.Callback;
@@ -52,7 +50,6 @@
5250
import org.elasticsearch.index.IndexComponent;
5351
import org.elasticsearch.index.IndexService;
5452
import org.elasticsearch.index.IndexSettings;
55-
import org.elasticsearch.index.IndexShardAlreadyExistsException;
5653
import org.elasticsearch.index.shard.IndexEventListener;
5754
import org.elasticsearch.index.shard.IndexShard;
5855
import org.elasticsearch.index.shard.IndexShardRelocatedException;
@@ -532,10 +529,6 @@ private void createShard(DiscoveryNodes nodes, RoutingTable routingTable, ShardR
532529
RecoveryState recoveryState = new RecoveryState(shardRouting, nodes.getLocalNode(), sourceNode);
533530
indicesService.createShard(shardRouting, recoveryState, recoveryTargetService, new RecoveryListener(shardRouting),
534531
repositoriesService, failedShardHandler);
535-
} catch (IndexShardAlreadyExistsException e) {
536-
// ignore this, the method call can happen several times
537-
logger.debug("Trying to create shard that already exists", e);
538-
assert false;
539532
} catch (Exception e) {
540533
failAndRemoveShard(shardRouting, true, "failed to create shard", e);
541534
}

core/src/test/java/org/elasticsearch/indices/template/SimpleIndexTemplateIT.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import org.elasticsearch.common.xcontent.XContentFactory;
3535
import org.elasticsearch.index.mapper.MapperParsingException;
3636
import org.elasticsearch.index.query.QueryBuilders;
37-
import org.elasticsearch.indices.IndexTemplateAlreadyExistsException;
3837
import org.elasticsearch.indices.InvalidAliasNameException;
3938
import org.elasticsearch.search.SearchHit;
4039
import org.elasticsearch.test.ESIntegTestCase;
@@ -110,7 +109,7 @@ public void testSimpleIndexTemplateTests() throws Exception {
110109
.addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties")
111110
.startObject("field2").field("type", "text").field("store", false).endObject()
112111
.endObject().endObject().endObject())
113-
, IndexTemplateAlreadyExistsException.class
112+
, IllegalArgumentException.class
114113
);
115114

116115
response = client().admin().indices().prepareGetTemplates().get();

0 commit comments

Comments
 (0)