Skip to content

Commit 1d8c852

Browse files
authored
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 2637525 commit 1d8c852

File tree

10 files changed

+10
-124
lines changed

10 files changed

+10
-124
lines changed

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -523,16 +523,14 @@ enum ElasticsearchExceptionHandle {
523523
org.elasticsearch.index.shard.IndexShardRelocatedException::new, 45),
524524
NODE_SHOULD_NOT_CONNECT_EXCEPTION(org.elasticsearch.transport.NodeShouldNotConnectException.class,
525525
org.elasticsearch.transport.NodeShouldNotConnectException::new, 46),
526-
INDEX_TEMPLATE_ALREADY_EXISTS_EXCEPTION(org.elasticsearch.indices.IndexTemplateAlreadyExistsException.class,
527-
org.elasticsearch.indices.IndexTemplateAlreadyExistsException::new, 47),
526+
// 47 used to be for IndexTemplateAlreadyExistsException which was deprecated in 5.1 removed in 6.0
528527
TRANSLOG_CORRUPTED_EXCEPTION(org.elasticsearch.index.translog.TranslogCorruptedException.class,
529528
org.elasticsearch.index.translog.TranslogCorruptedException::new, 48),
530529
CLUSTER_BLOCK_EXCEPTION(org.elasticsearch.cluster.block.ClusterBlockException.class,
531530
org.elasticsearch.cluster.block.ClusterBlockException::new, 49),
532531
FETCH_PHASE_EXECUTION_EXCEPTION(org.elasticsearch.search.fetch.FetchPhaseExecutionException.class,
533532
org.elasticsearch.search.fetch.FetchPhaseExecutionException::new, 50),
534-
INDEX_SHARD_ALREADY_EXISTS_EXCEPTION(org.elasticsearch.index.IndexShardAlreadyExistsException.class,
535-
org.elasticsearch.index.IndexShardAlreadyExistsException::new, 51),
533+
// 51 used to be for IndexShardAlreadyExistsException which was deprecated in 5.1 removed in 6.0
536534
VERSION_CONFLICT_ENGINE_EXCEPTION(org.elasticsearch.index.engine.VersionConflictEngineException.class,
537535
org.elasticsearch.index.engine.VersionConflictEngineException::new, 52),
538536
ENGINE_EXCEPTION(org.elasticsearch.index.engine.EngineException.class, org.elasticsearch.index.engine.EngineException::new, 53),
@@ -553,7 +551,7 @@ enum ElasticsearchExceptionHandle {
553551
org.elasticsearch.common.io.stream.NotSerializableExceptionWrapper::new, 62),
554552
ALIAS_FILTER_PARSING_EXCEPTION(org.elasticsearch.indices.AliasFilterParsingException.class,
555553
org.elasticsearch.indices.AliasFilterParsingException::new, 63),
556-
// 64 was DeleteByQueryFailedEngineException, which was removed in 3.0
554+
// 64 was DeleteByQueryFailedEngineException, which was removed in 5.0
557555
GATEWAY_EXCEPTION(org.elasticsearch.gateway.GatewayException.class, org.elasticsearch.gateway.GatewayException::new, 65),
558556
INDEX_SHARD_NOT_RECOVERING_EXCEPTION(org.elasticsearch.index.shard.IndexShardNotRecoveringException.class,
559557
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
@@ -150,7 +150,7 @@ public Integer version() {
150150

151151
/**
152152
* Set to <tt>true</tt> to force only creation, not an update of an index template. If it already
153-
* exists, it will fail with an {@link org.elasticsearch.indices.IndexTemplateAlreadyExistsException}.
153+
* exists, it will fail with an {@link IllegalArgumentException}.
154154
*/
155155
public PutIndexTemplateRequest create(boolean create) {
156156
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
@@ -76,7 +76,7 @@ public PutIndexTemplateRequestBuilder setVersion(Integer version) {
7676

7777
/**
7878
* Set to <tt>true</tt> to force only creation, not an update of an index template. If it already
79-
* exists, it will fail with an {@link org.elasticsearch.indices.IndexTemplateAlreadyExistsException}.
79+
* exists, it will fail with an {@link IllegalArgumentException}.
8080
*/
8181
public PutIndexTemplateRequestBuilder setCreate(boolean create) {
8282
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

-36
This file was deleted.

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

-56
This file was deleted.

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/ExceptionSerializationTests.java

+2-13
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import org.elasticsearch.index.shard.IndexShardState;
5858
import org.elasticsearch.index.shard.ShardId;
5959
import org.elasticsearch.index.shard.TranslogRecoveryPerformer;
60-
import org.elasticsearch.indices.IndexTemplateAlreadyExistsException;
6160
import org.elasticsearch.indices.IndexTemplateMissingException;
6261
import org.elasticsearch.indices.InvalidIndexTemplateException;
6362
import org.elasticsearch.indices.recovery.RecoverFilesRecoveryException;
@@ -336,16 +335,6 @@ public void testRecoverFilesRecoveryException() throws IOException {
336335
assertTrue(ex.getCause() instanceof NullPointerException);
337336
}
338337

339-
public void testIndexTemplateAlreadyExistsException() throws IOException {
340-
IndexTemplateAlreadyExistsException ex = serialize(new IndexTemplateAlreadyExistsException("the dude abides!"));
341-
assertEquals("the dude abides!", ex.name());
342-
assertEquals("index_template [the dude abides!] already exists", ex.getMessage());
343-
344-
ex = serialize(new IndexTemplateAlreadyExistsException((String) null));
345-
assertNull(ex.name());
346-
assertEquals("index_template [null] already exists", ex.getMessage());
347-
}
348-
349338
public void testBatchOperationException() throws IOException {
350339
ShardId id = new ShardId("foo", "_na_", 1);
351340
TranslogRecoveryPerformer.BatchOperationException ex = serialize(
@@ -683,11 +672,11 @@ public void testIds() {
683672
ids.put(44, org.elasticsearch.indices.recovery.RecoveryFailedException.class);
684673
ids.put(45, org.elasticsearch.index.shard.IndexShardRelocatedException.class);
685674
ids.put(46, org.elasticsearch.transport.NodeShouldNotConnectException.class);
686-
ids.put(47, org.elasticsearch.indices.IndexTemplateAlreadyExistsException.class);
675+
ids.put(47, null);
687676
ids.put(48, org.elasticsearch.index.translog.TranslogCorruptedException.class);
688677
ids.put(49, org.elasticsearch.cluster.block.ClusterBlockException.class);
689678
ids.put(50, org.elasticsearch.search.fetch.FetchPhaseExecutionException.class);
690-
ids.put(51, org.elasticsearch.index.IndexShardAlreadyExistsException.class);
679+
ids.put(51, null);
691680
ids.put(52, org.elasticsearch.index.engine.VersionConflictEngineException.class);
692681
ids.put(53, org.elasticsearch.index.engine.EngineException.class);
693682
ids.put(54, null); // was DocumentAlreadyExistsException, which is superseded with VersionConflictEngineException

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;
@@ -109,7 +108,7 @@ public void testSimpleIndexTemplateTests() throws Exception {
109108
.addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties")
110109
.startObject("field2").field("type", "text").field("store", false).endObject()
111110
.endObject().endObject().endObject())
112-
, IndexTemplateAlreadyExistsException.class
111+
, IllegalArgumentException.class
113112
);
114113

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

0 commit comments

Comments
 (0)