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

SOLR-17541: LBSolrClient implementations should agree on 'getClient()' semantics #2899

Merged
merged 35 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5261cd3
Initial development
jdyer1 Dec 4, 2024
bc8c888
fix bugs
jdyer1 Dec 5, 2024
a249169
javadoc modifications
jdyer1 Dec 5, 2024
1f8a307
tidy / remove nocommit
jdyer1 Dec 5, 2024
90bf9e2
remove obsolete test
jdyer1 Dec 6, 2024
51676f5
Fix bug: (1) Only generate a client per base url, not base url+core.…
jdyer1 Dec 6, 2024
db76064
tidy
jdyer1 Dec 6, 2024
605bdc7
Only hold the internal builder
jdyer1 Dec 6, 2024
508af0d
remove override base url from jdk client
jdyer1 Dec 6, 2024
39177b4
fix test
jdyer1 Dec 9, 2024
35651b8
Merge branch 'main' into feature/SOLR-17541
jdyer1 Dec 9, 2024
19577dc
add missing @Override
jdyer1 Dec 9, 2024
ca59536
implement code review suggestions
jdyer1 Dec 10, 2024
67c8b8f
fix getClient/buildClient
dsmiley Dec 12, 2024
7b90862
more
dsmiley Dec 12, 2024
1539d97
PKI Authentication Plugin to add the listener factory to subsequently…
jdyer1 Dec 13, 2024
8f17279
@lucene.experimental annotations
jdyer1 Dec 13, 2024
effedb9
tidy
jdyer1 Dec 13, 2024
29a3445
remove system.out.println
jdyer1 Dec 13, 2024
50470cd
httpClientBuilder > httpSolrClientBuilder
jdyer1 Dec 16, 2024
f423bd2
API change to Http2SolrClient.Builder:
jdyer1 Dec 16, 2024
6176a99
Remove unnecessary null check
jdyer1 Dec 16, 2024
783f815
tidy
jdyer1 Dec 16, 2024
e8c998d
small change to doc comment
jdyer1 Dec 16, 2024
e035a0c
fix typo
jdyer1 Dec 17, 2024
99282aa
Better variable names
jdyer1 Dec 17, 2024
45b9e10
remove silly duplicate method
jdyer1 Dec 18, 2024
7df3c3b
variable rename
jdyer1 Dec 19, 2024
90bd2ec
Merge branch 'main' into feature/SOLR-17541
jdyer1 Dec 19, 2024
f984d12
TODO on HttpClientBuilderPlugin
jdyer1 Dec 19, 2024
28aec3b
CHANGES.txt
jdyer1 Dec 19, 2024
30dcdb3
tidy
jdyer1 Dec 19, 2024
730aad0
CHANGES.txt deprecation notice for Solr 9.9
jdyer1 Dec 19, 2024
071fb04
CHANGES.txt: mention rename LBHttp2SolrClient.Builder#withListenerFac…
jdyer1 Dec 19, 2024
05eb1ea
Merge branch 'main' into feature/SOLR-17541
jdyer1 Dec 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions solr/core/src/java/org/apache/solr/cloud/ZkController.java
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,6 @@ public String toString() {
public final ZkStateReader zkStateReader;
private SolrCloudManager cloudManager;

// only for internal usage
private Http2SolrClient http2SolrClient;

private CloudHttp2SolrClient cloudSolrClient;

private final String zkServerAddress; // example: 127.0.0.1:54062/solr
Expand Down Expand Up @@ -776,7 +773,6 @@ public void close() {
sysPropsCacher.close();
customThreadPool.execute(() -> IOUtils.closeQuietly(cloudManager));
customThreadPool.execute(() -> IOUtils.closeQuietly(cloudSolrClient));
customThreadPool.execute(() -> IOUtils.closeQuietly(http2SolrClient));

try {
try {
Expand Down Expand Up @@ -872,15 +868,14 @@ public SolrCloudManager getSolrCloudManager() {
if (cloudManager != null) {
return cloudManager;
}
http2SolrClient =
var httpClientBuilder =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, should have a name like "solrClientBuilder" (maybe adding "http") to clarify this is building a SolrClient not an HttpClient.

new Http2SolrClient.Builder()
.withHttpClient(cc.getDefaultHttpSolrClient())
.withIdleTimeout(30000, TimeUnit.MILLISECONDS)
.withConnectionTimeout(15000, TimeUnit.MILLISECONDS)
.build();
.withConnectionTimeout(15000, TimeUnit.MILLISECONDS);
cloudSolrClient =
new CloudHttp2SolrClient.Builder(new ZkClientClusterStateProvider(zkStateReader))
.withHttpClient(http2SolrClient)
.withInternalClientBuilder(httpClientBuilder)
.build();
cloudManager = new SolrClientCloudManager(cloudSolrClient, cc.getObjectCache());
cloudManager.getClusterStateProvider().connect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public class HttpShardHandler extends ShardHandler {
protected AtomicInteger pending;

private final Map<String, List<String>> shardToURLs;
protected LBHttp2SolrClient<Http2SolrClient> lbClient;
protected LBHttp2SolrClient<Http2SolrClient.Builder> lbClient;

public HttpShardHandler(HttpShardHandlerFactory httpShardHandlerFactory) {
this.httpShardHandlerFactory = httpShardHandlerFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class HttpShardHandlerFactory extends ShardHandlerFactory

protected volatile Http2SolrClient defaultClient;
protected InstrumentedHttpListenerFactory httpListenerFactory;
protected LBHttp2SolrClient<Http2SolrClient> loadbalancer;
protected LBHttp2SolrClient<Http2SolrClient.Builder> loadbalancer;

int corePoolSize = 0;
int maximumPoolSize = Integer.MAX_VALUE;
Expand Down Expand Up @@ -305,16 +305,17 @@ public void init(PluginInfo info) {
sb);
int soTimeout =
getParameter(args, HttpClientUtil.PROP_SO_TIMEOUT, HttpClientUtil.DEFAULT_SO_TIMEOUT, sb);

this.defaultClient =
var defaultClientBuilder =
new Http2SolrClient.Builder()
.withConnectionTimeout(connectionTimeout, TimeUnit.MILLISECONDS)
.withIdleTimeout(soTimeout, TimeUnit.MILLISECONDS)
.withExecutor(commExecutor)
.withMaxConnectionsPerHost(maxConnectionsPerHost)
.build();
.withMaxConnectionsPerHost(maxConnectionsPerHost);
this.defaultClient = defaultClientBuilder.build();

this.defaultClient.addListenerFactory(this.httpListenerFactory);
this.loadbalancer = new LBHttp2SolrClient.Builder<Http2SolrClient>(defaultClient).build();
this.loadbalancer =
new LBHttp2SolrClient.Builder<Http2SolrClient.Builder>(defaultClientBuilder).build();

initReplicaListTransformers(getParameter(args, "replicaRouting", null, sb));

Expand Down
9 changes: 4 additions & 5 deletions solr/core/src/test/org/apache/solr/cloud/OverseerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1945,17 +1945,16 @@ public Void answer(InvocationOnMock invocation) {
}

private SolrCloudManager getCloudDataProvider(ZkStateReader zkStateReader) {
var httpSolrClient =
var httpSolrClientBuilder =
new Http2SolrClient.Builder()
.withIdleTimeout(30000, TimeUnit.MILLISECONDS)
.withConnectionTimeout(15000, TimeUnit.MILLISECONDS)
.build();
.withConnectionTimeout(15000, TimeUnit.MILLISECONDS);
var cloudSolrClient =
new CloudHttp2SolrClient.Builder(new ZkClientClusterStateProvider(zkStateReader))
.withHttpClient(httpSolrClient)
.withInternalClientBuilder(httpSolrClientBuilder)
.build();
solrClients.add(cloudSolrClient);
solrClients.add(httpSolrClient);
solrClients.add(httpSolrClientBuilder.build());
SolrClientCloudManager sccm = new SolrClientCloudManager(cloudSolrClient, null);
sccm.getClusterStateProvider().connect();
return sccm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@
public class CloudHttp2SolrClient extends CloudSolrClient {

private final ClusterStateProvider stateProvider;
private final LBHttp2SolrClient<Http2SolrClient> lbClient;
private final LBHttp2SolrClient<Http2SolrClient.Builder> lbClient;
private final Http2SolrClient myClient;
private final boolean clientIsInternal;

/**
* Create a new client object that connects to Zookeeper and is always aware of the SolrCloud
Expand All @@ -54,8 +53,8 @@ public class CloudHttp2SolrClient extends CloudSolrClient {
*/
protected CloudHttp2SolrClient(Builder builder) {
super(builder.shardLeadersOnly, builder.parallelUpdates, builder.directUpdatesToLeadersOnly);
this.clientIsInternal = builder.httpClient == null;
this.myClient = createOrGetHttpClientFromBuilder(builder);
var httpClientBuilder = createOrGetHttpClientBuilder(builder);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

httpSolrClientBuilder

this.myClient = httpClientBuilder.build();
this.stateProvider = createClusterStateProvider(builder);
this.retryExpiryTimeNano = builder.retryExpiryTimeNano;
this.defaultCollection = builder.defaultCollection;
Expand All @@ -73,16 +72,14 @@ protected CloudHttp2SolrClient(Builder builder) {
// locks.
this.locks = objectList(builder.parallelCacheRefreshesLocks);

this.lbClient = new LBHttp2SolrClient.Builder<Http2SolrClient>(myClient).build();
this.lbClient = new LBHttp2SolrClient.Builder<>(httpClientBuilder).build();
}

private Http2SolrClient createOrGetHttpClientFromBuilder(Builder builder) {
if (builder.httpClient != null) {
return builder.httpClient;
} else if (builder.internalClientBuilder != null) {
return builder.internalClientBuilder.build();
private Http2SolrClient.Builder createOrGetHttpClientBuilder(Builder builder) {
if (builder.internalClientBuilder != null) {
return builder.internalClientBuilder;
} else {
return new Http2SolrClient.Builder().build();
return new Http2SolrClient.Builder();
}
}

Expand Down Expand Up @@ -129,7 +126,7 @@ private ClusterStateProvider createHttp2ClusterStateProvider(

private void closeMyClientIfNeeded() {
try {
if (clientIsInternal && myClient != null) {
if (myClient != null) {
myClient.close();
}
} catch (Exception e) {
Expand All @@ -148,7 +145,7 @@ public void close() throws IOException {
}

@Override
public LBHttp2SolrClient<Http2SolrClient> getLbClient() {
public LBHttp2SolrClient<Http2SolrClient.Builder> getLbClient() {
return lbClient;
}

Expand All @@ -171,7 +168,6 @@ public static class Builder {
protected Collection<String> zkHosts = new ArrayList<>();
protected List<String> solrUrls = new ArrayList<>();
protected String zkChroot;
protected Http2SolrClient httpClient;
protected boolean shardLeadersOnly = true;
protected boolean directUpdatesToLeadersOnly = false;
protected boolean parallelUpdates = true;
Expand Down Expand Up @@ -404,23 +400,6 @@ public Builder withCollectionCacheTtl(long timeToLive, TimeUnit unit) {
return this;
}

/**
* Set the internal http client.
*
* <p>Note: closing the httpClient instance is at the responsibility of the caller.
*
* @param httpClient http client
* @return this
*/
public Builder withHttpClient(Http2SolrClient httpClient) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure we want to take this away from users

Copy link
Contributor Author

@jdyer1 jdyer1 Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the most controversial part of this PR, and I do not want to merge a potentially disruptive API change like this without buy-in. I do not think this is much of a burden though, because the Builder has withInternalClientBuilder, which users should be able to easily migrate to use.

The purpose of forcing this is that CloudHttp2SolrClient in turn passes this Client(Builder) to LBHttp2SolrClient. But as noted before, LBHttp2SolrClient doesn't really use this Client either; it clones it using Http2SolrClient.builder#withHttpClient. Yet we've made LBHttp2SolrClient generic so this means that any subclass of HttpSolrClientBase needs to also have a way to be cloned. Personally I think clone methods like this are brittle and a maintenance burden, and I don't want to need to write one for HttpJdkSolrClient. It also seems like a better API: by telling the user we want a Builder, we signal that we will create Clients as needed. Passing in an already-built Client signals that we will use that particular one, which is not always the case here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it clones it using Http2SolrClient.builder#withHttpClient

which still exists, so if a user somehow has a client but not a builder but needs one due to the API change you introduce, they have this option.

by telling the user we want a Builder, we signal that we will create Clients as needed

Well written James!

+1

if (this.internalClientBuilder != null) {
throw new IllegalStateException(
"The builder can't accept an httpClient AND an internalClientBuilder, only one of those can be provided");
}
this.httpClient = httpClient;
return this;
}

/**
* If provided, the CloudHttp2SolrClient will build it's internal Http2SolrClient using this
* builder (instead of the empty default one). Providing this builder allows users to configure
Expand All @@ -430,10 +409,6 @@ public Builder withHttpClient(Http2SolrClient httpClient) {
* @return this
*/
public Builder withInternalClientBuilder(Http2SolrClient.Builder internalClientBuilder) {
if (this.httpClient != null) {
throw new IllegalStateException(
"The builder can't accept an httpClient AND an internalClientBuilder, only one of those can be provided");
}
this.internalClientBuilder = internalClientBuilder;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ protected HttpJdkSolrClient(String serverBaseUrl, HttpJdkSolrClient.Builder buil
public CompletableFuture<NamedList<Object>> requestAsync(
final SolrRequest<?> solrRequest, String collection) {
try {
PreparedRequest pReq = prepareRequest(solrRequest, collection, null);
PreparedRequest pReq = prepareRequest(solrRequest, collection);
return httpClient
.sendAsync(pReq.reqb.build(), HttpResponse.BodyHandlers.ofInputStream())
.thenApply(
Expand All @@ -157,10 +157,10 @@ public CompletableFuture<NamedList<Object>> requestAsync(
}
}

protected NamedList<Object> requestWithBaseUrl(
String baseUrl, SolrRequest<?> solrRequest, String collection)
@Override
public NamedList<Object> request(SolrRequest<?> solrRequest, String collection)
throws SolrServerException, IOException {
PreparedRequest pReq = prepareRequest(solrRequest, collection, baseUrl);
PreparedRequest pReq = prepareRequest(solrRequest, collection);
HttpResponse<InputStream> response = null;
try {
response = httpClient.send(pReq.reqb.build(), HttpResponse.BodyHandlers.ofInputStream());
Expand Down Expand Up @@ -192,25 +192,13 @@ protected NamedList<Object> requestWithBaseUrl(
}
}

@Override
public NamedList<Object> request(SolrRequest<?> solrRequest, String collection)
throws SolrServerException, IOException {
return requestWithBaseUrl(null, solrRequest, collection);
}

private PreparedRequest prepareRequest(
SolrRequest<?> solrRequest, String collection, String overrideBaseUrl)
private PreparedRequest prepareRequest(SolrRequest<?> solrRequest, String collection)
throws SolrServerException, IOException {
checkClosed();
if (ClientUtils.shouldApplyDefaultCollection(collection, solrRequest)) {
collection = defaultCollection;
}
String url;
if (overrideBaseUrl != null) {
url = ClientUtils.buildRequestUrl(solrRequest, overrideBaseUrl, collection);
} else {
url = getRequestUrl(solrRequest, collection);
}
String url = getRequestUrl(solrRequest, collection);
ResponseParser parserToUse = responseParser(solrRequest);
ModifiableSolrParams queryParams = initializeSolrParams(solrRequest, parserToUse);
var reqb = HttpRequest.newBuilder();
Expand Down
Loading
Loading