This is an automated email from the ASF dual-hosted git repository.

Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new fb0bd4fd89 fix(logging): retry index creation after check failure 
(#7104)
fb0bd4fd89 is described below

commit fb0bd4fd89784d5ec264e39664ce2e05ce9f79a8
Author: Liming Deng <[email protected]>
AuthorDate: Sun Oct 11 23:19:02 2026 +0800

    fix(logging): retry index creation after check failure (#7104)
---
 .../client/ElasticSearchLogCollectClient.java      | 11 +++-
 .../client/ElasticSearchLogCollectClientTest.java  | 63 ++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)

diff --git 
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/main/java/org/apache/shenyu/plugin/logging/elasticsearch/client/ElasticSearchLogCollectClient.java
 
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/main/java/org/apache/shenyu/plugin/logging/elasticsearch/client/ElasticSearchLogCollectClient.java
index 5a438e4eac..ed4764e604 100644
--- 
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/main/java/org/apache/shenyu/plugin/logging/elasticsearch/client/ElasticSearchLogCollectClient.java
+++ 
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/main/java/org/apache/shenyu/plugin/logging/elasticsearch/client/ElasticSearchLogCollectClient.java
@@ -18,6 +18,7 @@
 package org.apache.shenyu.plugin.logging.elasticsearch.client;
 
 import co.elastic.clients.elasticsearch.ElasticsearchClient;
+import co.elastic.clients.elasticsearch._types.ElasticsearchException;
 import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
 import co.elastic.clients.json.jackson.JacksonJsonpMapper;
 import co.elastic.clients.transport.ElasticsearchTransport;
@@ -53,6 +54,8 @@ import java.util.Objects;
 public class ElasticSearchLogCollectClient extends 
AbstractLogConsumeClient<ElasticSearchLogCollectConfig.ElasticSearchLogConfig, 
ShenyuRequestLog> {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(ElasticSearchLogCollectClient.class);
+
+    private static final String RESOURCE_ALREADY_EXISTS_EXCEPTION = 
"resource_already_exists_exception";
     
     private static final DateTimeFormatter DATE_FORMAT = 
DateTimeFormatter.ofPattern("yyyy-MM-dd");
 
@@ -140,7 +143,7 @@ public class ElasticSearchLogCollectClient extends 
AbstractLogConsumeClient<Elas
         } catch (Exception e) {
             LogUtils.error(LOG, "fail to check the index exists, error:", e);
         }
-        return true;
+        return false;
     }
 
     /**
@@ -156,6 +159,12 @@ public class ElasticSearchLogCollectClient extends 
AbstractLogConsumeClient<Elas
                     .properties("responseContentLength", property -> 
property.integer(number -> number.ignoreMalformed(true)))
                     .properties("status", property -> property.integer(number 
-> number.ignoreMalformed(true)))
                     .properties("upstreamResponseTime", property -> 
property.long_(number -> number.ignoreMalformed(true)))));
+        } catch (ElasticsearchException e) {
+            if (RESOURCE_ALREADY_EXISTS_EXCEPTION.equals(e.error().type())) {
+                LogUtils.info(LOG, "index {} already exists", indexName);
+                return;
+            }
+            throw e;
         } catch (IOException e) {
             LogUtils.error(LOG, "create index error:", e);
         }
diff --git 
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/client/ElasticSearchLogCollectClientTest.java
 
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/client/ElasticSearchLogCollectClientTest.java
index fa99ac30c8..8dc637bdc7 100644
--- 
a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/client/ElasticSearchLogCollectClientTest.java
+++ 
b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-elasticsearch/src/test/java/org/apache/shenyu/plugin/logging/elasticsearch/client/ElasticSearchLogCollectClientTest.java
@@ -17,6 +17,10 @@
 
 package org.apache.shenyu.plugin.logging.elasticsearch.client;
 
+import co.elastic.clients.elasticsearch.ElasticsearchClient;
+import co.elastic.clients.elasticsearch._types.ElasticsearchException;
+import co.elastic.clients.elasticsearch._types.ErrorResponse;
+import co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient;
 import org.apache.shenyu.common.dto.PluginData;
 import org.apache.shenyu.common.utils.GsonUtils;
 import org.apache.shenyu.plugin.logging.common.entity.ShenyuRequestLog;
@@ -25,8 +29,18 @@ import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 
+import java.io.IOException;
+import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.function.Function;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 /**
  * test cases for ElasticSearchLog.
@@ -76,4 +90,53 @@ public class ElasticSearchLogCollectClientTest {
         elasticSearchLogCollectClient.initClient(elasticSearchLogConfig);
         elasticSearchLogCollectClient.createIndex("test");
     }
+
+    @Test
+    public void testExistsIndexReturnsFalseWhenCheckFails() throws Exception {
+        ElasticsearchClient client = mock(ElasticsearchClient.class);
+        ElasticsearchIndicesClient indicesClient = 
mock(ElasticsearchIndicesClient.class);
+        when(client.indices()).thenReturn(indicesClient);
+        when(indicesClient.exists(any(Function.class))).thenThrow(new 
IOException("connection failed"));
+        setClient(client);
+
+        
assertFalse(elasticSearchLogCollectClient.existsIndex("missing-index"));
+    }
+
+    @Test
+    public void testCreateIndexIgnoresAlreadyExistsException() throws 
Exception {
+        ElasticsearchIndicesClient indicesClient = 
mock(ElasticsearchIndicesClient.class);
+        ElasticsearchException alreadyExists = 
elasticsearchException("resource_already_exists_exception");
+        
when(indicesClient.create(any(Function.class))).thenThrow(alreadyExists);
+        ElasticsearchClient client = mock(ElasticsearchClient.class);
+        when(client.indices()).thenReturn(indicesClient);
+        setClient(client);
+
+        assertDoesNotThrow(() -> 
elasticSearchLogCollectClient.createIndex("existing-index"));
+    }
+
+    @Test
+    public void testCreateIndexRethrowsOtherElasticsearchExceptions() throws 
Exception {
+        ElasticsearchIndicesClient indicesClient = 
mock(ElasticsearchIndicesClient.class);
+        ElasticsearchException unavailable = 
elasticsearchException("cluster_block_exception");
+        when(indicesClient.create(any(Function.class))).thenThrow(unavailable);
+        ElasticsearchClient client = mock(ElasticsearchClient.class);
+        when(client.indices()).thenReturn(indicesClient);
+        setClient(client);
+
+        assertThrows(ElasticsearchException.class,
+                () -> 
elasticSearchLogCollectClient.createIndex("blocked-index"));
+    }
+
+    private ElasticsearchException elasticsearchException(final String type) {
+        ErrorResponse response = ErrorResponse.of(builder -> builder
+                .status(400)
+                .error(error -> error.type(type).reason("test")));
+        return new ElasticsearchException("indices.create", response);
+    }
+
+    private void setClient(final ElasticsearchClient client) throws 
ReflectiveOperationException {
+        Field clientField = 
ElasticSearchLogCollectClient.class.getDeclaredField("client");
+        clientField.setAccessible(true);
+        clientField.set(elasticSearchLogCollectClient, client);
+    }
 }

Reply via email to