Skip to content

Commit

Permalink
[CALCITE-6725] The caching mechanism key in ElasticsearchSchemaFactor…
Browse files Browse the repository at this point in the history
…y 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
  • Loading branch information
AstraHoo authored and NobiGo committed Dec 17, 2024
1 parent 87485a9 commit dbf4ff8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
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;
Expand Down Expand Up @@ -159,6 +160,8 @@ public ElasticsearchSchemaFactory() {
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
Expand All @@ -174,7 +177,7 @@ public ElasticsearchSchemaFactory() {
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);
Expand All @@ -183,6 +186,15 @@ public ElasticsearchSchemaFactory() {
}
}

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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
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;
Expand Down Expand Up @@ -53,6 +55,7 @@
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;
Expand Down Expand Up @@ -771,4 +774,21 @@ private static Consumer<ResultSet> sortedResultSetChecker(String column,
"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());
}

}

0 comments on commit dbf4ff8

Please sign in to comment.