Skip to content

Commit a900d4a

Browse files
committed
HSEARCH-5464 Continue factoring out the Client
1 parent d2d3769 commit a900d4a

File tree

124 files changed

+1210
-440
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+1210
-440
lines changed

backend/elasticsearch-aws/src/main/java/org/hibernate/search/backend/elasticsearch/aws/impl/ElasticsearchAwsBeanConfigurer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import org.hibernate.search.backend.elasticsearch.aws.cfg.ElasticsearchAwsCredentialsTypeNames;
88
import org.hibernate.search.backend.elasticsearch.aws.spi.ElasticsearchAwsCredentialsProvider;
9-
import org.hibernate.search.backend.elasticsearch.client.ElasticsearchHttpClientConfigurer;
9+
import org.hibernate.search.backend.elasticsearch.client.elasticsearch.lowlevel.ElasticsearchHttpClientConfigurer;
1010
import org.hibernate.search.engine.environment.bean.BeanHolder;
1111
import org.hibernate.search.engine.environment.bean.spi.BeanConfigurationContext;
1212
import org.hibernate.search.engine.environment.bean.spi.BeanConfigurer;

backend/elasticsearch-aws/src/main/java/org/hibernate/search/backend/elasticsearch/aws/impl/ElasticsearchAwsHttpClientConfigurer.java

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
*/
55
package org.hibernate.search.backend.elasticsearch.aws.impl;
66

7+
import java.util.Locale;
8+
import java.util.regex.Pattern;
9+
710
import org.hibernate.search.backend.elasticsearch.aws.cfg.ElasticsearchAwsBackendSettings;
811
import org.hibernate.search.backend.elasticsearch.aws.cfg.ElasticsearchAwsCredentialsTypeNames;
912
import org.hibernate.search.backend.elasticsearch.aws.logging.impl.AwsLog;
1013
import org.hibernate.search.backend.elasticsearch.aws.spi.ElasticsearchAwsCredentialsProvider;
11-
import org.hibernate.search.backend.elasticsearch.client.ElasticsearchHttpClientConfigurationContext;
12-
import org.hibernate.search.backend.elasticsearch.client.ElasticsearchHttpClientConfigurer;
14+
import org.hibernate.search.backend.elasticsearch.client.elasticsearch.lowlevel.ElasticsearchHttpClientConfigurationContext;
15+
import org.hibernate.search.backend.elasticsearch.client.elasticsearch.lowlevel.ElasticsearchHttpClientConfigurer;
1316
import org.hibernate.search.engine.cfg.ConfigurationPropertySource;
1417
import org.hibernate.search.engine.cfg.spi.ConfigurationProperty;
1518
import org.hibernate.search.engine.cfg.spi.OptionalConfigurationProperty;
@@ -20,11 +23,9 @@
2023
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
2124
import software.amazon.awssdk.regions.Region;
2225

23-
import java.util.regex.Pattern;
24-
2526
public class ElasticsearchAwsHttpClientConfigurer implements ElasticsearchHttpClientConfigurer {
26-
private static final Pattern DISTRIBUTION_NAME_PATTERN = Pattern.compile( "([^\\d]+)?(?:(?<=^)|(?=$)|(?<=.):(?=.))(.+)?" );
27-
private static final ConfigurationProperty<Boolean> SIGNING_ENABLED =
27+
private static final Pattern DISTRIBUTION_NAME_PATTERN = Pattern.compile( "([^\\d]+)?(?:(?<=^)|(?=$)|(?<=.):(?=.))(.+)?" );
28+
private static final ConfigurationProperty<Boolean> SIGNING_ENABLED =
2829
ConfigurationProperty.forKey( ElasticsearchAwsBackendSettings.SIGNING_ENABLED )
2930
.asBoolean()
3031
.withDefault( ElasticsearchAwsBackendSettings.Defaults.SIGNING_ENABLED )
@@ -52,9 +53,9 @@ public class ElasticsearchAwsHttpClientConfigurer implements ElasticsearchHttpCl
5253
.asString()
5354
.build();
5455

55-
static final OptionalConfigurationProperty<String> DISTRIBUTION_NAME =
56+
static final OptionalConfigurationProperty<String> DISTRIBUTION_NAME =
5657
ConfigurationProperty.forKey( "version" )
57-
.asString()
58+
.asString()
5859
.build();
5960

6061
@Override
@@ -69,21 +70,22 @@ public void configure(ElasticsearchHttpClientConfigurationContext context) {
6970
Region region = REGION.getAndMapOrThrow( propertySource, Region::of, AwsLog.INSTANCE::missingPropertyForSigning );
7071
String service;
7172

72-
String distributionName = DISTRIBUTION_NAME.getAndTransform(propertySource, v ->
73-
v.map(String::toLowerCase)
74-
.map(DISTRIBUTION_NAME_PATTERN::matcher)
75-
.map(matcher -> {
76-
if (matcher.matches()) {
77-
return matcher.group(1);
78-
}
79-
return null;
80-
}).orElse("opensearch"));
81-
82-
if ("amazon-opensearch-serverless".equals(distributionName)) {
83-
service = "aoss";
84-
} else {
85-
service = "es";
86-
}
73+
String distributionName = DISTRIBUTION_NAME.getAndTransform( propertySource,
74+
v -> v.map( ver -> ver.toLowerCase( Locale.ROOT ) )
75+
.map( DISTRIBUTION_NAME_PATTERN::matcher )
76+
.map( matcher -> {
77+
if ( matcher.matches() ) {
78+
return matcher.group( 1 );
79+
}
80+
return null;
81+
} ).orElse( "opensearch" ) );
82+
83+
if ( "amazon-opensearch-serverless".equals( distributionName ) ) {
84+
service = "aoss";
85+
}
86+
else {
87+
service = "es";
88+
}
8789

8890
AwsCredentialsProvider credentialsProvider = createCredentialsProvider( context.beanResolver(), propertySource );
8991

backend/elasticsearch-client/common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</parent>
1515
<artifactId>hibernate-search-backend-elasticsearch-client-common</artifactId>
1616

17-
<name>Hibernate Search Backend - Elasticsearch Client SPI</name>
17+
<name>Hibernate Search Backend - Elasticsearch Client Common</name>
1818
<description>Rest client SPI for the Elasticsearch backend</description>
1919

2020
<properties>
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.search.backend.elasticsearch.client.common.cfg;
6+
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
/**
11+
* Common configuration properties for the Elasticsearch backend's rest client.
12+
* <p>
13+
* Constants in this class are to be appended to a prefix to form a property key;
14+
* see {@link org.hibernate.search.engine.cfg.BackendSettings} for details.
15+
*
16+
* @author Gunnar Morling
17+
*/
18+
public final class ElasticsearchBackendClientCommonSettings {
19+
20+
private ElasticsearchBackendClientCommonSettings() {
21+
}
22+
23+
/**
24+
* The hostname and ports of the Elasticsearch servers to connect to.
25+
* <p>
26+
* Expects a String representing a hostname and port such as {@code localhost} or {@code es.mycompany.com:4400},
27+
* or a String containing multiple such hostname-and-port strings separated by commas,
28+
* or a {@code Collection<String>} containing such hostname-and-port strings.
29+
* <p>
30+
* Multiple servers may be specified for load-balancing: requests will be assigned to each host in turns.
31+
* <p>
32+
* Setting this property at the same time as {@link #URIS} will lead to an exception being thrown on startup.
33+
* <p>
34+
* Defaults to {@link Defaults#HOSTS}.
35+
*/
36+
public static final String HOSTS = "hosts";
37+
38+
/**
39+
* The protocol to use when connecting to the Elasticsearch servers.
40+
* <p>
41+
* Expects a String: either {@code http} or {@code https}.
42+
* <p>
43+
* Setting this property at the same time as {@link #URIS} will lead to an exception being thrown on startup.
44+
* <p>
45+
* Defaults to {@link Defaults#PROTOCOL}.
46+
*/
47+
public static final String PROTOCOL = "protocol";
48+
49+
/**
50+
* The protocol, hostname and ports of the Elasticsearch servers to connect to.
51+
* <p>
52+
* Expects either a String representing an URI such as {@code http://localhost}
53+
* or {@code https://es.mycompany.com:4400},
54+
* or a String containing multiple such URIs separated by commas,
55+
* or a {@code Collection<String>} containing such URIs.
56+
* <p>
57+
* All the URIs must specify the same protocol.
58+
* <p>
59+
* Setting this property at the same time as {@link #HOSTS} or {@link #PROTOCOL} will lead to an exception being thrown on startup.
60+
* <p>
61+
* Defaults to {@code http://localhost:9200}, unless {@link #HOSTS} or {@link #PROTOCOL} are set, in which case they take precedence.
62+
*/
63+
public static final String URIS = "uris";
64+
65+
/**
66+
* Property for specifying the path prefix prepended to the request end point.
67+
* Use the path prefix if your Elasticsearch instance is located at a specific context path.
68+
* <p>
69+
* Defaults to {@link Defaults#PATH_PREFIX}.
70+
*/
71+
public static final String PATH_PREFIX = "path_prefix";
72+
73+
/**
74+
* The username to send when connecting to the Elasticsearch servers (HTTP authentication).
75+
* <p>
76+
* Expects a String.
77+
* <p>
78+
* Defaults to no username (anonymous access).
79+
*/
80+
public static final String USERNAME = "username";
81+
82+
/**
83+
* The password to send when connecting to the Elasticsearch servers (HTTP authentication).
84+
* <p>
85+
* Expects a String.
86+
* <p>
87+
* Defaults to no username (anonymous access).
88+
*/
89+
public static final String PASSWORD = "password";
90+
91+
/**
92+
* The timeout when executing a request to an Elasticsearch server.
93+
* <p>
94+
* This includes the time needed to establish a connection, send the request and read the response.
95+
* <p>
96+
* Expects a positive Integer value in milliseconds, such as 60000,
97+
* or a String that can be parsed into such Integer value.
98+
* <p>
99+
* Defaults to no request timeout.
100+
*/
101+
public static final String REQUEST_TIMEOUT = "request_timeout";
102+
103+
/**
104+
* The timeout when reading responses from an Elasticsearch server.
105+
* <p>
106+
* Expects a positive Integer value in milliseconds, such as {@code 60000},
107+
* or a String that can be parsed into such Integer value.
108+
* <p>
109+
* Defaults to {@link Defaults#READ_TIMEOUT}.
110+
*/
111+
public static final String READ_TIMEOUT = "read_timeout";
112+
113+
/**
114+
* The timeout when establishing a connection to an Elasticsearch server.
115+
* <p>
116+
* Expects a positive Integer value in milliseconds, such as {@code 3000},
117+
* or a String that can be parsed into such Integer value.
118+
* <p>
119+
* Defaults to {@link Defaults#CONNECTION_TIMEOUT}.
120+
*/
121+
public static final String CONNECTION_TIMEOUT = "connection_timeout";
122+
123+
/**
124+
* The maximum number of simultaneous connections to the Elasticsearch cluster,
125+
* all hosts taken together.
126+
* <p>
127+
* Expects a positive Integer value, such as {@code 40},
128+
* or a String that can be parsed into such Integer value.
129+
* <p>
130+
* Defaults to {@link Defaults#MAX_CONNECTIONS}.
131+
*/
132+
public static final String MAX_CONNECTIONS = "max_connections";
133+
134+
/**
135+
* The maximum number of simultaneous connections to each host of the Elasticsearch cluster.
136+
* <p>
137+
* Expects a positive Integer value, such as {@code 20},
138+
* or a String that can be parsed into such Integer value.
139+
* <p>
140+
* Defaults to {@link Defaults#MAX_CONNECTIONS_PER_ROUTE}.
141+
*/
142+
public static final String MAX_CONNECTIONS_PER_ROUTE = "max_connections_per_route";
143+
144+
/**
145+
* Whether automatic discovery of nodes in the Elasticsearch cluster is enabled.
146+
* <p>
147+
* Expects a Boolean value such as {@code true} or {@code false},
148+
* or a string that can be parsed into a Boolean value.
149+
* <p>
150+
* Defaults to {@link Defaults#DISCOVERY_ENABLED}.
151+
*/
152+
public static final String DISCOVERY_ENABLED = "discovery.enabled";
153+
154+
/**
155+
* The time interval between two executions of the automatic discovery, if enabled.
156+
* <p>
157+
* Expects a positive Integer value in seconds, such as {@code 2},
158+
* or a String that can be parsed into such Integer value.
159+
* <p>
160+
* Defaults to {@link Defaults#DISCOVERY_REFRESH_INTERVAL}.
161+
*/
162+
public static final String DISCOVERY_REFRESH_INTERVAL = "discovery.refresh_interval";
163+
164+
/**
165+
* How long connections to the Elasticsearch cluster can be kept idle.
166+
* <p>
167+
* Expects a positive Long value of milliseconds, such as 60000,
168+
* or a String that can be parsed into such Integer value.
169+
* <p>
170+
* If the response from an Elasticsearch cluster contains a {@code Keep-Alive} header,
171+
* then the effective max idle time will be whichever is lower:
172+
* the duration from the {@code Keep-Alive} header or the value of this property (if set).
173+
* <p>
174+
* If this property is not set, only the {@code Keep-Alive} header is considered,
175+
* and if it's absent, idle connections will be kept forever.
176+
*/
177+
public static final String MAX_KEEP_ALIVE = "max_keep_alive";
178+
179+
/**
180+
* Default values for the different settings if no values are given.
181+
*/
182+
public static final class Defaults {
183+
184+
private Defaults() {
185+
}
186+
187+
public static final List<String> HOSTS = Collections.singletonList( "localhost:9200" );
188+
public static final String PROTOCOL = "http";
189+
public static final String PATH_PREFIX = "";
190+
public static final int READ_TIMEOUT = 30000;
191+
public static final int CONNECTION_TIMEOUT = 1000;
192+
public static final int MAX_CONNECTIONS = 40;
193+
public static final int MAX_CONNECTIONS_PER_ROUTE = 20;
194+
public static final boolean DISCOVERY_ENABLED = false;
195+
public static final int DISCOVERY_REFRESH_INTERVAL = 10;
196+
}
197+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.search.backend.elasticsearch.client.common.cfg.spi;
6+
7+
import org.hibernate.search.util.common.annotation.Incubating;
8+
9+
/**
10+
* Implementation-related settings.
11+
*/
12+
@Incubating
13+
public final class ElasticsearchBackendClientSpiSettings {
14+
15+
private ElasticsearchBackendClientSpiSettings() {
16+
}
17+
18+
public static final String CLIENT_FACTORY = "client_factory";
19+
20+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
6+
package org.hibernate.search.backend.elasticsearch.client.common.logging.spi;
7+
8+
import static org.jboss.logging.Logger.Level.TRACE;
9+
10+
import org.hibernate.search.util.common.logging.impl.MessageConstants;
11+
12+
import org.jboss.logging.annotations.LogMessage;
13+
import org.jboss.logging.annotations.Message;
14+
import org.jboss.logging.annotations.MessageLogger;
15+
import org.jboss.logging.annotations.ValidIdRange;
16+
import org.jboss.logging.annotations.ValidIdRanges;
17+
18+
@MessageLogger(projectCode = MessageConstants.PROJECT_CODE)
19+
@ValidIdRanges({
20+
@ValidIdRange(min = MessageConstants.BACKEND_ES_CLIENT_ID_RANGE_MIN,
21+
max = MessageConstants.BACKEND_ES_CLIENT_ID_RANGE_MAX),
22+
// Exceptions for legacy messages from Search 5 (engine module)
23+
@ValidIdRange(min = 35, max = 35),
24+
})
25+
public interface ElasticsearchClientCommonLog
26+
extends ElasticsearchRequestLog, ElasticsearchClientLog {
27+
28+
// -----------------------------------
29+
// Pre-existing messages from Search 5 (ES module)
30+
// DO NOT ADD ANY NEW MESSAGES HERE
31+
// -----------------------------------
32+
int ID_OFFSET_LEGACY_ES = MessageConstants.BACKEND_ES_ID_RANGE_MIN;
33+
34+
// -----------------------------------
35+
// New (old) messages from Search 6 onwards up until Search 8.2
36+
// -----------------------------------
37+
int ID_BACKEND_OFFSET = MessageConstants.BACKEND_ES_ID_RANGE_MIN + 500;
38+
39+
// -----------------------------------
40+
// New messages from Search 8.2 onwards
41+
// -----------------------------------
42+
int ID_OFFSET = MessageConstants.BACKEND_ES_CLIENT_ID_RANGE_MIN;
43+
44+
/**
45+
* Only here as a way to track the highest "already used id".
46+
* When adding a new exception or log message use this id and bump the one
47+
* here to the next value.
48+
*/
49+
@LogMessage(level = TRACE)
50+
@Message(id = ID_OFFSET + 1, value = "")
51+
void nextLoggerIdForConvenience();
52+
}

0 commit comments

Comments
 (0)