Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recreate index, error 'resource_already_exists_exception' #111945

Closed
LiJie20190102 opened this issue Aug 16, 2024 · 5 comments
Closed

Recreate index, error 'resource_already_exists_exception' #111945

LiJie20190102 opened this issue Aug 16, 2024 · 5 comments
Labels
>bug needs:triage Requires assignment of a team area label

Comments

@LiJie20190102
Copy link

LiJie20190102 commented Aug 16, 2024

Elasticsearch Version

7.17.21

Installed Plugins

No response

Java Version

21.0.2

OS Version

3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Problem Description

My goal is to quickly clear the index, but I found that the 'delete-by_query' courier is slow, so I plan to delete it first and then create a new index like before, but I find that errors often occur:

image

Steps to Reproduce

  1. create index
    PUT /lj_test1 { "settings": { "number_of_shards": 10, "number_of_replicas": 6 }, "mappings": { "properties": { "name1": { "type": "text" }, "name2": { "type": "text" }, "name3": { "type": "text" }, "name4": { "type": "text" }, "name5": { "type": "text" }, "name6": { "type": "text" } } } }

  2. Delete index
    DELETE /lj_test1

  3. After completing step 2, quickly re execute step 1

note: If not reproduced, please increase the number of shards and replicas slightly

Logs (if relevant)

{
"error" : {
"root_cause" : [
{
"type" : "resource_already_exists_exception",
"reason" : "index [lj_test1/wWP6JOWvSL2mRePTHQ4YDQ] already exists",
"index_uuid" : "wWP6JOWvSL2mRePTHQ4YDQ",
"index" : "lj_test1"
}
],
"type" : "resource_already_exists_exception",
"reason" : "index [lj_test1/wWP6JOWvSL2mRePTHQ4YDQ] already exists",
"index_uuid" : "wWP6JOWvSL2mRePTHQ4YDQ",
"index" : "lj_test1"
},
"status" : 400
}

@LiJie20190102 LiJie20190102 added >bug needs:triage Requires assignment of a team area label labels Aug 16, 2024
@sudheer-pasala

This comment was marked as off-topic.

@LiJie20190102
Copy link
Author

const { Client } = require('@elastic/elasticsearch');

// Initialize the Elasticsearch client const client = new Client({ node: 'http://localhost:9200' });

const indexName = '1j_test1';

async function indexExists(index) { try { const { body } = await client.indices.exists({ index }); return body; } catch (error) { console.error(Error checking index existence: ${error.message}); return false; } }

async function deleteIndex(index) { try { if (await indexExists(index)) { const { body } = await client.indices.delete({ index }); if (body.acknowledged) { console.log(Index '${index}' deletion initiated.); } else { console.log(Failed to delete index '${index}'.); } } else { console.log(Index '${index}' does not exist.); } } catch (error) { console.error(Error deleting index: ${error.message}); } }

async function verifyIndexDeleted(index) { try { const exists = await indexExists(index); if (!exists) { console.log(Index '${index}' successfully deleted.); return true; } else { console.log(Index '${index}' still exists.); return false; } } catch (error) { console.error(Error verifying index deletion: ${error.message}); return false; } }

async function createIndex(index) { try { const { body } = await client.indices.create({ index, body: { settings: { number_of_shards: 1, number_of_replicas: 1 } } }); if (body.acknowledged) { console.log(Index '${index}' created successfully.); } else { console.log(Failed to create index '${index}'.); } } catch (error) { console.error(Error creating index: ${error.message}); } }

(async () => { try { await deleteIndex(indexName);

// Verify the index is deleted before creating a new one
if (await verifyIndexDeleted(indexName)) {
  await createIndex(indexName);
} else {
  console.log(`Cannot create index '${indexName}' because it still exists.`);
}

} catch (error) { console.error(Unexpected error: ${error.message}); } finally { // Close the client connection await client.close(); } })();

This function deletes and verifies the index is pressent are not and creates new one if index already presents then generates index already present `message```

I used your method from the beginning. I implemented it using Java code, but the exists method shows that the index does not exist. I think the delete method I am using is synchronous, and by the time delete is executed, the index will definitely no longer exist.
Meanwhile, the way this issue arises seems to be related to the shrad and replicas.

@DaveCTurner
Copy link
Contributor

I can't reproduce this. I wrote the following test case to do as you suggest and it does not fail:

diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java
index 27f0cd408e7f..59ce76130d43 100644
--- a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java
+++ b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java
@@ -21,6 +21,7 @@ import org.elasticsearch.action.support.ActiveShardCount;
 import org.elasticsearch.action.support.IndicesOptions;
 import org.elasticsearch.action.support.PlainActionFuture;
 import org.elasticsearch.action.support.master.AcknowledgedResponse;
+import org.elasticsearch.client.Request;
 import org.elasticsearch.client.Response;
 import org.elasticsearch.cluster.ClusterState;
 import org.elasticsearch.cluster.metadata.IndexMetadata;
@@ -36,10 +37,12 @@ import org.elasticsearch.index.IndexService;
 import org.elasticsearch.index.mapper.MapperParsingException;
 import org.elasticsearch.index.query.RangeQueryBuilder;
 import org.elasticsearch.indices.IndicesService;
+import org.elasticsearch.rest.RestStatus;
 import org.elasticsearch.test.ESIntegTestCase;
 import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
 import org.elasticsearch.test.ESIntegTestCase.Scope;
 import org.elasticsearch.test.rest.ESRestTestCase;
+import org.elasticsearch.xcontent.ToXContentFragment;
 import org.elasticsearch.xcontent.XContentFactory;

 import java.io.IOException;
@@ -418,4 +421,44 @@ public class CreateIndexIT extends ESIntegTestCase {
         assertTrue((boolean) extractValue("acknowledged", entityAsMap(response)));
     }

+    public void testAlreadyExistsAfterDelete() throws IOException {
+        final var restClient = getRestClient();
+
+        final ToXContentFragment createRequestBody = (builder, params) -> builder.startObject("settings")
+            .field("number_of_shards", 10)
+            .field("number_of_replicas", 6)
+            .endObject()
+            .startObject("mappings")
+            .startObject("properties")
+            .startObject("name1")
+            .field("type", "text")
+            .endObject()
+            .startObject("name2")
+            .field("type", "text")
+            .endObject()
+            .startObject("name3")
+            .field("type", "text")
+            .endObject()
+            .startObject("name4")
+            .field("type", "text")
+            .endObject()
+            .startObject("name5")
+            .field("type", "text")
+            .endObject()
+            .startObject("name6")
+            .field("type", "text")
+            .endObject()
+            .endObject()
+            .endObject();
+
+        for (int i = 0; i < 10; i++) {
+            final var createResponse = restClient.performRequest(
+                ESRestTestCase.newXContentRequest(HttpMethod.PUT, "/lj_test1", createRequestBody)
+            );
+            assertEquals(RestStatus.OK.getStatus(), createResponse.getStatusLine().getStatusCode());
+
+            final var deleteResponse = restClient.performRequest(new Request("DELETE", "/lj_test1"));
+            assertEquals(RestStatus.OK.getStatus(), deleteResponse.getStatusLine().getStatusCode());
+        }
+    }
 }

As it doesn't seem to be reproducible and could well be caused by an error in your test (e.g. not waiting for a successful response from the DELETE step), we'd like to direct these kinds of things to the Elasticsearch forum. If you can stop by there, we'd appreciate it. This allows us to use GitHub for verified bug reports, feature requests, and pull requests.

There's an active community in the forum that should be able to help get an answer to your question. As such, I hope you don't mind that I close this.

@DaveCTurner DaveCTurner closed this as not planned Won't fix, can't repro, duplicate, stale Aug 18, 2024
@LiJie20190102
Copy link
Author

I can't reproduce this. I wrote the following test case to do as you suggest and it does not fail:

diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java
index 27f0cd408e7f..59ce76130d43 100644
--- a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java
+++ b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/indices/create/CreateIndexIT.java
@@ -21,6 +21,7 @@ import org.elasticsearch.action.support.ActiveShardCount;
 import org.elasticsearch.action.support.IndicesOptions;
 import org.elasticsearch.action.support.PlainActionFuture;
 import org.elasticsearch.action.support.master.AcknowledgedResponse;
+import org.elasticsearch.client.Request;
 import org.elasticsearch.client.Response;
 import org.elasticsearch.cluster.ClusterState;
 import org.elasticsearch.cluster.metadata.IndexMetadata;
@@ -36,10 +37,12 @@ import org.elasticsearch.index.IndexService;
 import org.elasticsearch.index.mapper.MapperParsingException;
 import org.elasticsearch.index.query.RangeQueryBuilder;
 import org.elasticsearch.indices.IndicesService;
+import org.elasticsearch.rest.RestStatus;
 import org.elasticsearch.test.ESIntegTestCase;
 import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
 import org.elasticsearch.test.ESIntegTestCase.Scope;
 import org.elasticsearch.test.rest.ESRestTestCase;
+import org.elasticsearch.xcontent.ToXContentFragment;
 import org.elasticsearch.xcontent.XContentFactory;

 import java.io.IOException;
@@ -418,4 +421,44 @@ public class CreateIndexIT extends ESIntegTestCase {
         assertTrue((boolean) extractValue("acknowledged", entityAsMap(response)));
     }

+    public void testAlreadyExistsAfterDelete() throws IOException {
+        final var restClient = getRestClient();
+
+        final ToXContentFragment createRequestBody = (builder, params) -> builder.startObject("settings")
+            .field("number_of_shards", 10)
+            .field("number_of_replicas", 6)
+            .endObject()
+            .startObject("mappings")
+            .startObject("properties")
+            .startObject("name1")
+            .field("type", "text")
+            .endObject()
+            .startObject("name2")
+            .field("type", "text")
+            .endObject()
+            .startObject("name3")
+            .field("type", "text")
+            .endObject()
+            .startObject("name4")
+            .field("type", "text")
+            .endObject()
+            .startObject("name5")
+            .field("type", "text")
+            .endObject()
+            .startObject("name6")
+            .field("type", "text")
+            .endObject()
+            .endObject()
+            .endObject();
+
+        for (int i = 0; i < 10; i++) {
+            final var createResponse = restClient.performRequest(
+                ESRestTestCase.newXContentRequest(HttpMethod.PUT, "/lj_test1", createRequestBody)
+            );
+            assertEquals(RestStatus.OK.getStatus(), createResponse.getStatusLine().getStatusCode());
+
+            final var deleteResponse = restClient.performRequest(new Request("DELETE", "/lj_test1"));
+            assertEquals(RestStatus.OK.getStatus(), deleteResponse.getStatusLine().getStatusCode());
+        }
+    }
 }

As it doesn't seem to be reproducible and could well be caused by an error in your test (e.g. not waiting for a successful response from the DELETE step), we'd like to direct these kinds of things to the Elasticsearch forum. If you can stop by there, we'd appreciate it. This allows us to use GitHub for verified bug reports, feature requests, and pull requests.

There's an active community in the forum that should be able to help get an answer to your question. As such, I hope you don't mind that I close this.

I am certain that I waited for the delete to complete before continuing to create the index, which I believe may be related to my limited environmental resources. Because my previous code also waited for delete to complete before recreating the index. My overall goal is to clear the data in the index. Is my method the fastest in terms of performance

@elastic elastic locked as off-topic and limited conversation to collaborators Aug 19, 2024
@DaveCTurner
Copy link
Contributor

This isn't the right place to continue this discussion. Please use the forum.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
>bug needs:triage Requires assignment of a team area label
Projects
None yet
Development

No branches or pull requests

3 participants