Skip to content

Commit c1f95f6

Browse files
Replace IndexAlreadyExistsException with ResourceAlreadyExistsException
1 parent 6c9ea08 commit c1f95f6

File tree

16 files changed

+42
-39
lines changed

16 files changed

+42
-39
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,9 @@ enum ElasticsearchExceptionHandle {
653653
org.elasticsearch.repositories.RepositoryVerificationException::new, 120),
654654
INVALID_AGGREGATION_PATH_EXCEPTION(org.elasticsearch.search.aggregations.InvalidAggregationPathException.class,
655655
org.elasticsearch.search.aggregations.InvalidAggregationPathException::new, 121),
656-
INDEX_ALREADY_EXISTS_EXCEPTION(org.elasticsearch.indices.IndexAlreadyExistsException.class,
657-
org.elasticsearch.indices.IndexAlreadyExistsException::new, 123),
656+
// 123 used to be IndexAlreadyExistsException and was renamed
657+
RESOURCE_ALREADY_EXISTS_EXCEPTION(ResourceAlreadyExistsException.class,
658+
ResourceAlreadyExistsException::new, 123),
658659
// 124 used to be Script.ScriptParseException
659660
HTTP_ON_TRANSPORT_EXCEPTION(TcpTransport.HttpOnTransportException.class,
660661
TcpTransport.HttpOnTransportException::new, 125),
@@ -694,6 +695,7 @@ enum ElasticsearchExceptionHandle {
694695
TASK_CANCELLED_EXCEPTION(org.elasticsearch.tasks.TaskCancelledException.class,
695696
org.elasticsearch.tasks.TaskCancelledException::new, 146);
696697

698+
697699
final Class<? extends ElasticsearchException> exceptionClass;
698700
final FunctionThatThrowsIOException<StreamInput, ? extends ElasticsearchException> constructor;
699701
final int id;

core/src/main/java/org/elasticsearch/indices/IndexAlreadyExistsException.java renamed to core/src/main/java/org/elasticsearch/ResourceAlreadyExistsException.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,26 @@
1717
* under the License.
1818
*/
1919

20-
package org.elasticsearch.indices;
20+
package org.elasticsearch;
2121

22-
import org.elasticsearch.ElasticsearchException;
2322
import org.elasticsearch.common.io.stream.StreamInput;
2423
import org.elasticsearch.index.Index;
2524
import org.elasticsearch.rest.RestStatus;
2625

2726
import java.io.IOException;
2827

29-
public class IndexAlreadyExistsException extends ElasticsearchException {
28+
public class ResourceAlreadyExistsException extends ElasticsearchException {
3029

31-
public IndexAlreadyExistsException(Index index) {
32-
this(index, "index " + index.toString() + " already exists");
30+
public ResourceAlreadyExistsException(Index index) {
31+
this("index {} already exists", index.toString());
32+
setIndex(index);
3333
}
3434

35-
public IndexAlreadyExistsException(Index index, String message) {
36-
super(message);
37-
setIndex(index);
35+
public ResourceAlreadyExistsException(String msg, Object... args) {
36+
super(msg, args);
3837
}
3938

40-
public IndexAlreadyExistsException(StreamInput in) throws IOException{
39+
public ResourceAlreadyExistsException(StreamInput in) throws IOException{
4140
super(in);
4241
}
4342

core/src/main/java/org/elasticsearch/action/admin/indices/rollover/TransportRolloverAction.java

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import org.elasticsearch.common.inject.Inject;
4545
import org.elasticsearch.common.settings.Settings;
4646
import org.elasticsearch.index.shard.DocsStats;
47-
import org.elasticsearch.indices.IndexAlreadyExistsException;
4847
import org.elasticsearch.threadpool.ThreadPool;
4948
import org.elasticsearch.transport.TransportService;
5049

core/src/main/java/org/elasticsearch/action/bulk/TransportBulkAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import org.elasticsearch.index.Index;
4949
import org.elasticsearch.index.IndexNotFoundException;
5050
import org.elasticsearch.index.shard.ShardId;
51-
import org.elasticsearch.indices.IndexAlreadyExistsException;
51+
import org.elasticsearch.ResourceAlreadyExistsException;
5252
import org.elasticsearch.indices.IndexClosedException;
5353
import org.elasticsearch.tasks.Task;
5454
import org.elasticsearch.threadpool.ThreadPool;
@@ -142,7 +142,7 @@ public void onResponse(CreateIndexResponse result) {
142142

143143
@Override
144144
public void onFailure(Exception e) {
145-
if (!(ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException)) {
145+
if (!(ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException)) {
146146
// fail all requests involving this index, if create didnt work
147147
for (int i = 0; i < bulkRequest.requests.size(); i++) {
148148
DocWriteRequest request = bulkRequest.requests.get(i);

core/src/main/java/org/elasticsearch/action/delete/TransportDeleteAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import org.elasticsearch.index.engine.Engine;
4040
import org.elasticsearch.index.shard.IndexShard;
4141
import org.elasticsearch.index.shard.ShardId;
42-
import org.elasticsearch.indices.IndexAlreadyExistsException;
42+
import org.elasticsearch.ResourceAlreadyExistsException;
4343
import org.elasticsearch.indices.IndicesService;
4444
import org.elasticsearch.tasks.Task;
4545
import org.elasticsearch.threadpool.ThreadPool;
@@ -81,7 +81,7 @@ public void onResponse(CreateIndexResponse result) {
8181

8282
@Override
8383
public void onFailure(Exception e) {
84-
if (ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException) {
84+
if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) {
8585
// we have the index, do it
8686
innerExecute(task, request, listener);
8787
} else {

core/src/main/java/org/elasticsearch/action/index/TransportIndexAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import org.elasticsearch.index.mapper.SourceToParse;
4545
import org.elasticsearch.index.shard.IndexShard;
4646
import org.elasticsearch.index.shard.ShardId;
47-
import org.elasticsearch.indices.IndexAlreadyExistsException;
47+
import org.elasticsearch.ResourceAlreadyExistsException;
4848
import org.elasticsearch.indices.IndicesService;
4949
import org.elasticsearch.tasks.Task;
5050
import org.elasticsearch.threadpool.ThreadPool;
@@ -101,7 +101,7 @@ public void onResponse(CreateIndexResponse result) {
101101

102102
@Override
103103
public void onFailure(Exception e) {
104-
if (ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException) {
104+
if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) {
105105
// we have the index, do it
106106
try {
107107
innerExecute(task, request, listener);

core/src/main/java/org/elasticsearch/action/update/TransportUpdateAction.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import org.elasticsearch.index.engine.VersionConflictEngineException;
5555
import org.elasticsearch.index.shard.IndexShard;
5656
import org.elasticsearch.index.shard.ShardId;
57-
import org.elasticsearch.indices.IndexAlreadyExistsException;
57+
import org.elasticsearch.ResourceAlreadyExistsException;
5858
import org.elasticsearch.indices.IndicesService;
5959
import org.elasticsearch.threadpool.ThreadPool;
6060
import org.elasticsearch.transport.TransportService;
@@ -127,7 +127,7 @@ public void onResponse(CreateIndexResponse result) {
127127

128128
@Override
129129
public void onFailure(Exception e) {
130-
if (unwrapCause(e) instanceof IndexAlreadyExistsException) {
130+
if (unwrapCause(e) instanceof ResourceAlreadyExistsException) {
131131
// we have the index, do it
132132
try {
133133
innerExecute(request, listener);

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
import com.carrotsearch.hppc.cursors.ObjectCursor;
2323
import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
24-
2524
import org.apache.logging.log4j.message.ParameterizedMessage;
2625
import org.apache.logging.log4j.util.Supplier;
2726
import org.apache.lucene.util.CollectionUtil;
2827
import org.elasticsearch.ElasticsearchException;
28+
import org.elasticsearch.ResourceAlreadyExistsException;
2929
import org.elasticsearch.Version;
3030
import org.elasticsearch.action.ActionListener;
3131
import org.elasticsearch.action.admin.indices.alias.Alias;
@@ -68,7 +68,6 @@
6868
import org.elasticsearch.index.mapper.MapperParsingException;
6969
import org.elasticsearch.index.mapper.MapperService;
7070
import org.elasticsearch.index.query.QueryShardContext;
71-
import org.elasticsearch.indices.IndexAlreadyExistsException;
7271
import org.elasticsearch.indices.IndexCreationException;
7372
import org.elasticsearch.indices.IndicesService;
7473
import org.elasticsearch.indices.InvalidIndexNameException;
@@ -137,10 +136,10 @@ public static void validateIndexName(String index, ClusterState state) {
137136
throw new InvalidIndexNameException(index, "must be lowercase");
138137
}
139138
if (state.routingTable().hasIndex(index)) {
140-
throw new IndexAlreadyExistsException(state.routingTable().index(index).getIndex());
139+
throw new ResourceAlreadyExistsException(state.routingTable().index(index).getIndex());
141140
}
142141
if (state.metaData().hasIndex(index)) {
143-
throw new IndexAlreadyExistsException(state.metaData().index(index).getIndex());
142+
throw new ResourceAlreadyExistsException(state.metaData().index(index).getIndex());
144143
}
145144
if (state.metaData().hasAlias(index)) {
146145
throw new InvalidIndexNameException(index, "already exists as alias");
@@ -453,7 +452,7 @@ public ClusterState execute(ClusterState currentState) throws Exception {
453452

454453
@Override
455454
public void onFailure(String source, Exception e) {
456-
if (e instanceof IndexAlreadyExistsException) {
455+
if (e instanceof ResourceAlreadyExistsException) {
457456
logger.trace((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to create", request.index()), e);
458457
} else {
459458
logger.debug((Supplier<?>) () -> new ParameterizedMessage("[{}] failed to create", request.index()), e);
@@ -520,7 +519,7 @@ static List<String> validateShrinkIndex(ClusterState state, String sourceIndex,
520519
Set<String> targetIndexMappingsTypes, String targetIndexName,
521520
Settings targetIndexSettings) {
522521
if (state.metaData().hasIndex(targetIndexName)) {
523-
throw new IndexAlreadyExistsException(state.metaData().index(targetIndexName).getIndex());
522+
throw new ResourceAlreadyExistsException(state.metaData().index(targetIndexName).getIndex());
524523
}
525524
final IndexMetaData sourceMetaData = state.metaData().index(sourceIndex);
526525
if (sourceMetaData == null) {

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.lucene.util.IOUtils;
2929
import org.apache.lucene.util.RamUsageEstimator;
3030
import org.elasticsearch.ElasticsearchException;
31+
import org.elasticsearch.ResourceAlreadyExistsException;
3132
import org.elasticsearch.action.admin.indices.stats.CommonStats;
3233
import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags;
3334
import org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag;
@@ -373,7 +374,7 @@ public IndexService indexServiceSafe(Index index) {
373374
* Creates a new {@link IndexService} for the given metadata.
374375
* @param indexMetaData the index metadata to create the index for
375376
* @param builtInListeners a list of built-in lifecycle {@link IndexEventListener} that should should be used along side with the per-index listeners
376-
* @throws IndexAlreadyExistsException if the index already exists.
377+
* @throws ResourceAlreadyExistsException if the index already exists.
377378
*/
378379
@Override
379380
public synchronized IndexService createIndex(IndexMetaData indexMetaData, List<IndexEventListener> builtInListeners, Consumer<ShardId> globalCheckpointSyncer) throws IOException {
@@ -383,7 +384,7 @@ public synchronized IndexService createIndex(IndexMetaData indexMetaData, List<I
383384
}
384385
final Index index = indexMetaData.getIndex();
385386
if (hasIndex(index)) {
386-
throw new IndexAlreadyExistsException(index);
387+
throw new ResourceAlreadyExistsException(index);
387388
}
388389
List<IndexEventListener> finalListeners = new ArrayList<>(builtInListeners);
389390
final IndexEventListener onStoreClose = new IndexEventListener() {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.logging.log4j.message.ParameterizedMessage;
2424
import org.apache.logging.log4j.util.Supplier;
2525
import org.apache.lucene.store.LockObtainFailedException;
26+
import org.elasticsearch.ResourceAlreadyExistsException;
2627
import org.elasticsearch.cluster.ClusterChangedEvent;
2728
import org.elasticsearch.cluster.ClusterState;
2829
import org.elasticsearch.cluster.ClusterStateListener;
@@ -59,7 +60,6 @@
5960
import org.elasticsearch.index.shard.IndexShardState;
6061
import org.elasticsearch.index.shard.ShardId;
6162
import org.elasticsearch.index.shard.ShardNotFoundException;
62-
import org.elasticsearch.indices.IndexAlreadyExistsException;
6363
import org.elasticsearch.indices.IndicesService;
6464
import org.elasticsearch.indices.flush.SyncedFlushService;
6565
import org.elasticsearch.indices.recovery.PeerRecoverySourceService;
@@ -767,7 +767,7 @@ public interface AllocatedIndices<T extends Shard, U extends AllocatedIndex<T>>
767767
* @param builtInIndexListener a list of built-in lifecycle {@link IndexEventListener} that should should be used along side with
768768
* the per-index listeners
769769
* @param globalCheckpointSyncer the global checkpoint syncer
770-
* @throws IndexAlreadyExistsException if the index already exists.
770+
* @throws ResourceAlreadyExistsException if the index already exists.
771771
*/
772772
U createIndex(IndexMetaData indexMetaData,
773773
List<IndexEventListener> builtInIndexListener,

core/src/main/java/org/elasticsearch/tasks/TaskResultsService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import org.elasticsearch.common.xcontent.ToXContent;
4343
import org.elasticsearch.common.xcontent.XContentBuilder;
4444
import org.elasticsearch.common.xcontent.XContentFactory;
45-
import org.elasticsearch.indices.IndexAlreadyExistsException;
45+
import org.elasticsearch.ResourceAlreadyExistsException;
4646

4747
import java.io.ByteArrayOutputStream;
4848
import java.io.IOException;
@@ -93,7 +93,7 @@ public void onResponse(CreateIndexResponse result) {
9393

9494
@Override
9595
public void onFailure(Exception e) {
96-
if (ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException) {
96+
if (ExceptionsHelper.unwrapCause(e) instanceof ResourceAlreadyExistsException) {
9797
// we have the index, do it
9898
try {
9999
doStoreResult(taskResult, listener);

core/src/test/java/org/elasticsearch/ESExceptionTests.java

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ public void testStatus() {
7878
exception = new RemoteTransportException("test", new ResourceNotFoundException("test"));
7979
assertThat(exception.status(), equalTo(RestStatus.NOT_FOUND));
8080

81+
exception = new RemoteTransportException("test", new ResourceAlreadyExistsException("test"));
82+
assertThat(exception.status(), equalTo(RestStatus.BAD_REQUEST));
83+
8184
exception = new RemoteTransportException("test", new IllegalArgumentException("foobar"));
8285
assertThat(exception.status(), equalTo(RestStatus.BAD_REQUEST));
8386

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ public void testIds() {
745745
ids.put(120, org.elasticsearch.repositories.RepositoryVerificationException.class);
746746
ids.put(121, org.elasticsearch.search.aggregations.InvalidAggregationPathException.class);
747747
ids.put(122, null);
748-
ids.put(123, org.elasticsearch.indices.IndexAlreadyExistsException.class);
748+
ids.put(123, org.elasticsearch.ResourceAlreadyExistsException.class);
749749
ids.put(124, null);
750750
ids.put(125, TcpTransport.HttpOnTransportException.class);
751751
ids.put(126, org.elasticsearch.index.mapper.MapperParsingException.class);

core/src/test/java/org/elasticsearch/action/admin/indices/rollover/RolloverIT.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.elasticsearch.cluster.metadata.IndexMetaData;
2626
import org.elasticsearch.common.settings.Settings;
2727
import org.elasticsearch.common.unit.TimeValue;
28-
import org.elasticsearch.indices.IndexAlreadyExistsException;
28+
import org.elasticsearch.ResourceAlreadyExistsException;
2929
import org.elasticsearch.plugins.Plugin;
3030
import org.elasticsearch.test.ESIntegTestCase;
3131
import org.elasticsearch.test.InternalSettingsPlugin;
@@ -171,7 +171,7 @@ public void testRolloverOnExistingIndex() throws Exception {
171171
try {
172172
client().admin().indices().prepareRolloverIndex("test_alias").get();
173173
fail("expected failure due to existing rollover index");
174-
} catch (IndexAlreadyExistsException e) {
174+
} catch (ResourceAlreadyExistsException e) {
175175
assertThat(e.getIndex().getName(), equalTo("test_index-000001"));
176176
}
177177
}

core/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import org.elasticsearch.common.Strings;
3636
import org.elasticsearch.common.settings.Settings;
3737
import org.elasticsearch.index.IndexNotFoundException;
38-
import org.elasticsearch.indices.IndexAlreadyExistsException;
38+
import org.elasticsearch.ResourceAlreadyExistsException;
3939
import org.elasticsearch.indices.InvalidIndexNameException;
4040
import org.elasticsearch.test.ESTestCase;
4141
import org.elasticsearch.test.gateway.TestGatewayAllocator;
@@ -78,7 +78,7 @@ public void testValidateShrinkIndex() {
7878
Settings.builder().put("index.blocks.write", true).build());
7979

8080
assertEquals("index [source] already exists",
81-
expectThrows(IndexAlreadyExistsException.class, () ->
81+
expectThrows(ResourceAlreadyExistsException.class, () ->
8282
MetaDataCreateIndexService.validateShrinkIndex(state, "target", Collections.emptySet(), "source", Settings.EMPTY)
8383
).getMessage());
8484

rest-api-spec/src/main/resources/rest-api-spec/test/indices.rollover/10_basic.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
index: logs-000002
125125

126126
- do:
127-
catch: /index_already_exists_exception/
127+
catch: /resource_already_exists_exception/
128128
indices.rollover:
129129
dry_run: true
130130
alias: "logs_search"
@@ -135,7 +135,7 @@
135135

136136
# also do it without dry_run
137137
- do:
138-
catch: /index_already_exists_exception/
138+
catch: /resource_already_exists_exception/
139139
indices.rollover:
140140
dry_run: false
141141
alias: "logs_search"

0 commit comments

Comments
 (0)