-
Notifications
You must be signed in to change notification settings - Fork 745
SOLR-17804: re-create SolrClient if closed. #3411
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e72eef5
SOLR-17084: re-create SolrClient if closed.
sigram e3fb604
Tidy.
sigram 79aa86e
Add dependency.
sigram 31df342
Fix issues from review.
sigram 04c7c50
Tidy.
sigram 6cbd823
Override.
sigram f177aa0
Merge branch 'main' into jira/solr-17804
sigram 4c8fc12
Remove unused dependency.
sigram 99827a7
Remove unnecessary getter.
sigram 76533fa
Fix issues from review.
sigram 93ee33f
Tidy.
sigram File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.codahale.metrics.SharedMetricRegistries; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import java.lang.invoke.MethodHandles; | ||
import java.time.Duration; | ||
import java.util.Arrays; | ||
|
@@ -31,6 +32,8 @@ | |
import java.util.concurrent.Future; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.Supplier; | ||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
|
@@ -85,7 +88,7 @@ public class KafkaCrossDcConsumer extends Consumer.CrossDcConsumer { | |
private final int maxCollapseRecords; | ||
private final SolrMessageProcessor messageProcessor; | ||
|
||
protected final CloudSolrClient solrClient; | ||
protected SolrClientSupplier solrClientSupplier; | ||
|
||
private final ThreadPoolExecutor executor; | ||
|
||
|
@@ -95,6 +98,59 @@ public class KafkaCrossDcConsumer extends Consumer.CrossDcConsumer { | |
|
||
private final BlockingQueue<Runnable> queue = new BlockingQueue<>(10); | ||
|
||
public static class SolrClientSupplier implements Supplier<CloudSolrClient>, AutoCloseable { | ||
private final AtomicReference<CloudSolrClient> solrClientRef = new AtomicReference<>(); | ||
private final String zkConnectString; | ||
|
||
public SolrClientSupplier(String zkConnectString) { | ||
this.zkConnectString = zkConnectString; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
IOUtils.closeQuietly(solrClientRef.get()); | ||
} | ||
|
||
protected CloudSolrClient createSolrClient() { | ||
log.info("-- creating new SolrClient..."); | ||
return new CloudSolrClient.Builder( | ||
Collections.singletonList(zkConnectString), Optional.empty()) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public CloudSolrClient get() { | ||
CloudSolrClient existingClient = solrClientRef.get(); | ||
if (existingClient == null) { | ||
synchronized (solrClientRef) { | ||
if (solrClientRef.get() == null) { | ||
log.info("- initializing Solr client"); | ||
solrClientRef.set(createSolrClient()); | ||
} | ||
return solrClientRef.get(); | ||
} | ||
} | ||
if (existingClient.getClusterStateProvider().isClosed()) { | ||
// lock out other threads and re-open the client if its ClusterStateProvider was closed | ||
synchronized (solrClientRef) { | ||
// refresh and check again | ||
existingClient = solrClientRef.get(); | ||
if (existingClient.getClusterStateProvider().isClosed()) { | ||
log.info("- re-creating Solr client because its CSP was closed"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we make more normal log messages without There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This applies to all added log lines. |
||
CloudSolrClient newClient = createSolrClient(); | ||
solrClientRef.set(newClient); | ||
IOUtils.closeQuietly(existingClient); | ||
return newClient; | ||
} else { | ||
return existingClient; | ||
} | ||
} | ||
} else { | ||
return existingClient; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param conf The Kafka consumer configuration | ||
* @param startLatch To inform the caller when the Consumer has started | ||
|
@@ -157,7 +213,7 @@ public KafkaCrossDcConsumer(KafkaCrossDcConf conf, CountDownLatch startLatch) { | |
r -> new Thread(r, "KafkaCrossDcConsumerWorker")); | ||
executor.prestartAllCoreThreads(); | ||
|
||
solrClient = createSolrClient(conf); | ||
solrClientSupplier = createSolrClientSupplier(conf); | ||
|
||
messageProcessor = createSolrMessageProcessor(); | ||
|
||
|
@@ -170,15 +226,28 @@ public KafkaCrossDcConsumer(KafkaCrossDcConf conf, CountDownLatch startLatch) { | |
log.info("Created Kafka resubmit producer"); | ||
} | ||
|
||
protected SolrClientSupplier createSolrClientSupplier(KafkaCrossDcConf conf) { | ||
return new SolrClientSupplier(conf.get(KafkaCrossDcConf.ZK_CONNECT_STRING)); | ||
} | ||
|
||
protected SolrMessageProcessor createSolrMessageProcessor() { | ||
return new SolrMessageProcessor(solrClient, resubmitRequest -> 0L); | ||
return new SolrMessageProcessor(solrClientSupplier, resubmitRequest -> 0L); | ||
} | ||
|
||
public KafkaConsumer<String, MirroredSolrRequest<?>> createKafkaConsumer(Properties properties) { | ||
return new KafkaConsumer<>( | ||
properties, new StringDeserializer(), new MirroredSolrRequestSerializer()); | ||
} | ||
|
||
protected KafkaMirroringSink createKafkaMirroringSink(KafkaCrossDcConf conf) { | ||
return new KafkaMirroringSink(conf); | ||
} | ||
|
||
@VisibleForTesting | ||
public SolrClientSupplier getSolrClientSupplier() { | ||
return solrClientSupplier; | ||
} | ||
|
||
/** | ||
* This is where the magic happens. | ||
* | ||
|
@@ -219,7 +288,7 @@ public void run() { | |
log.warn("Failed to close kafka mirroring sink", e); | ||
} | ||
} finally { | ||
IOUtils.closeQuietly(solrClient); | ||
IOUtils.closeQuietly(solrClientSupplier); | ||
} | ||
} | ||
|
||
|
@@ -506,7 +575,7 @@ public final void shutdown() { | |
offsetCheckExecutor.shutdown(); | ||
offsetCheckExecutor.awaitTermination(30, TimeUnit.SECONDS); | ||
} | ||
solrClient.close(); | ||
IOUtils.closeQuietly(solrClientSupplier); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
log.warn("Interrupted while waiting for executor to shutdown"); | ||
|
@@ -516,15 +585,4 @@ public final void shutdown() { | |
Util.logMetrics(metrics); | ||
} | ||
} | ||
|
||
protected CloudSolrClient createSolrClient(KafkaCrossDcConf conf) { | ||
return new CloudSolrClient.Builder( | ||
Collections.singletonList(conf.get(KafkaCrossDcConf.ZK_CONNECT_STRING)), | ||
Optional.empty()) | ||
.build(); | ||
} | ||
|
||
protected KafkaMirroringSink createKafkaMirroringSink(KafkaCrossDcConf conf) { | ||
return new KafkaMirroringSink(conf); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need this anymore