You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by xi...@apache.org on 2024/12/17 12:26:15 UTC
(calcite) branch main updated: [CALCITE-6725] The caching mechanism key in ElasticsearchSchemaFactory is affected by the order of hosts
This is an automated email from the ASF dual-hosted git repository.
xiong pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push:
new dbf4ff87ae [CALCITE-6725] The caching mechanism key in ElasticsearchSchemaFactory is affected by the order of hosts
dbf4ff87ae is described below
commit dbf4ff87ae34d7cb9e08037c15bd6fddfcaaef24
Author: Hoo <18...@163.com>
AuthorDate: Wed Dec 11 10:55:10 2024 +0800
[CALCITE-6725] The caching mechanism key in ElasticsearchSchemaFactory is affected by the order of hosts
Update ElasticsearchSchemaFactory.java
Add unit test
add unit test
Modify code implementation and unit test
Reformat code
Test unit is compatible with Java8
Reformat code
Reformat code
Reformat code
Add jira link
---
.../elasticsearch/ElasticsearchSchemaFactory.java | 14 +++++++++++++-
.../elasticsearch/ElasticSearchAdapterTest.java | 20 ++++++++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchSchemaFactory.java b/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchSchemaFactory.java
index b36ca1aa64..aeaec2fa0f 100644
--- a/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchSchemaFactory.java
+++ b/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchSchemaFactory.java
@@ -45,6 +45,7 @@ import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
+import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@@ -159,6 +160,8 @@ public class ElasticsearchSchemaFactory implements SchemaFactory {
throw new IllegalArgumentException
("Both 'coordinates' and 'hosts' is missing in configuration. Provide one of them.");
}
+ List<HttpHost> sortedHost = getSortedHost(hosts);
+
final String pathPrefix = (String) map.get("pathPrefix");
// Enable or Disable SSL Verification
@@ -174,7 +177,7 @@ public class ElasticsearchSchemaFactory implements SchemaFactory {
String username = (String) map.get("username");
String password = (String) map.get("password");
final RestClient client =
- connect(hosts, pathPrefix, username, password, disableSSLVerification);
+ connect(sortedHost, pathPrefix, username, password, disableSSLVerification);
final String index = (String) map.get("index");
return new ElasticsearchSchema(client, new ObjectMapper(), index);
@@ -183,6 +186,15 @@ public class ElasticsearchSchemaFactory implements SchemaFactory {
}
}
+ protected static List<HttpHost> getSortedHost(List<HttpHost> hosts) {
+ List<HttpHost> sortedHosts =
+ hosts
+ .stream()
+ .sorted(Comparator.comparing(HttpHost::toString, String::compareTo))
+ .collect(Collectors.toList());
+ return sortedHosts;
+ }
+
/**
* Builds Elastic rest client from user configuration.
*
diff --git a/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java b/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java
index fb18e5b9fa..578e0e7d63 100644
--- a/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java
+++ b/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java
@@ -26,6 +26,8 @@ import org.apache.calcite.test.ElasticsearchChecker;
import org.apache.calcite.util.Bug;
import org.apache.calcite.util.TestUtil;
+import org.apache.http.HttpHost;
+
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.ImmutableMap;
import com.google.common.io.LineProcessor;
@@ -53,6 +55,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.function.Consumer;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static java.util.Objects.requireNonNull;
@@ -771,4 +774,21 @@ class ElasticSearchAdapterTest {
"state=AR; EXPR$1=3; EXPR$2=3");
}
+ /**
+ * Test case for
+ * <a href="https://issues.apache.org/jira/browse/CALCITE-6725">[CALCITE-6725]
+ * The caching mechanism key in ElasticsearchSchemaFactory is affected by the order of hosts</a>.
+ */
+ @Test void testSortHosts() {
+ HttpHost host1 = HttpHost.create("192.168.1.200:8080");
+ HttpHost host2 = HttpHost.create("192.168.1.100:8080");
+ HttpHost host3 = HttpHost.create("192.168.1.150:8080");
+ List<HttpHost> hosts = Arrays.asList(host1, host2, host3);
+ List<HttpHost> sortedHosts = ElasticsearchSchemaFactory.getSortedHost(hosts);
+ assertEquals(3, sortedHosts.size());
+ assertEquals("http://192.168.1.100:8080", sortedHosts.get(0).toString());
+ assertEquals("http://192.168.1.150:8080", sortedHosts.get(1).toString());
+ assertEquals("http://192.168.1.200:8080", sortedHosts.get(2).toString());
+ }
+
}