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-15590 - Context Listener to start CoreContainer #397

Closed
wants to merge 10 commits into from
Expand Up @@ -23,6 +23,7 @@
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -69,6 +70,7 @@
import org.apache.solr.handler.admin.CoreAdminOperation;
import org.apache.solr.handler.admin.LukeRequestHandler;
import org.apache.solr.metrics.SolrMetricManager;
import org.apache.solr.servlet.CoreService;
import org.apache.solr.servlet.SolrDispatchFilter;
import org.apache.solr.util.TimeOut;
import org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory;
Expand Down Expand Up @@ -142,6 +144,7 @@ public class JettySolrRunner {
private String host;

private volatile boolean started = false;
private CoreService coreService;

public static class DebugFilter implements Filter {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
Expand Down Expand Up @@ -364,7 +367,9 @@ public void lifeCycleStopping(LifeCycle arg0) {
}

@Override
public void lifeCycleStopped(LifeCycle arg0) {}
public void lifeCycleStopped(LifeCycle arg0) {
coreService.close();
}

@Override
public void lifeCycleStarting(LifeCycle arg0) {
Expand All @@ -373,7 +378,6 @@ public void lifeCycleStarting(LifeCycle arg0) {

@Override
public void lifeCycleStarted(LifeCycle arg0) {

jettyPort = getFirstConnectorPort();
int port = jettyPort;
if (proxyPort != -1) port = proxyPort;
Expand All @@ -382,7 +386,8 @@ public void lifeCycleStarted(LifeCycle arg0) {

root.getServletContext().setAttribute(SolrDispatchFilter.PROPERTIES_ATTRIBUTE, nodeProperties);
root.getServletContext().setAttribute(SolrDispatchFilter.SOLRHOME_ATTRIBUTE, solrHome);

coreService = new CoreService();
coreService.init(root.getServletContext());
log.info("Jetty properties: {}", nodeProperties);

debugFilter = root.addFilter(DebugFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST) );
Expand Down Expand Up @@ -455,10 +460,14 @@ protected HandlerWrapper injectJettyHandlers(HandlerWrapper chain) {
* @return the {@link CoreContainer} for this node
*/
public CoreContainer getCoreContainer() {
if (getSolrDispatchFilter() == null || getSolrDispatchFilter().getCores() == null) {
return null;
try {
if (getSolrDispatchFilter() == null || getSolrDispatchFilter().getCores() == null) {
return null;
}
return getSolrDispatchFilter().getCores();
} catch (UnavailableException e) {
throw new RuntimeException(e);
}
return getSolrDispatchFilter().getCores();
}

public String getNodeName() {
Expand Down Expand Up @@ -527,7 +536,7 @@ public void start(boolean reusePort) throws Exception {
}
synchronized (JettySolrRunner.this) {
int cnt = 0;
while (!waitOnSolr || !dispatchFilter.isRunning() || getCoreContainer() == null) {
while (!waitOnSolr || !dispatchFilter.isRunning() ) {
this.wait(100);
if (cnt++ == 15) {
throw new RuntimeException("Jetty/Solr unresponsive");
Expand Down Expand Up @@ -561,7 +570,7 @@ public void start(boolean reusePort) throws Exception {


private void setProtocolAndHost() {
String protocol = null;
String protocol;

Connector[] conns = server.getConnectors();
if (0 == conns.length) {
Expand All @@ -575,7 +584,7 @@ private void setProtocolAndHost() {
this.host = c.getHost();
}

private void retryOnPortBindFailure(int portRetryTime, int port) throws Exception, InterruptedException {
private void retryOnPortBindFailure(int portRetryTime, int port) throws Exception {
TimeOut timeout = new TimeOut(portRetryTime, TimeUnit.SECONDS, TimeSource.NANO_TIME);
int tryCnt = 1;
while (true) {
Expand Down Expand Up @@ -638,19 +647,15 @@ public void stop() throws Exception {
if (sdf != null) {
customThreadPool = ExecutorUtil.newMDCAwareCachedThreadPool(new SolrNamedThreadFactory("jettyShutDown"));

sdf.closeOnDestroy(false);
sdf.closeOnDestroy();
// customThreadPool.submit(() -> {
// try {
// sdf.close();
// } catch (Throwable t) {
// log.error("Error shutting down Solr", t);
// }
// });
try {
sdf.close();
} catch (Throwable t) {
log.error("Error shutting down Solr", t);
}

}

QueuedThreadPool qtp = (QueuedThreadPool) server.getThreadPool();
gus-asf marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -687,8 +692,7 @@ public void stop() throws Exception {
rte.stop();

TimeOut timeout = new TimeOut(30, TimeUnit.SECONDS, TimeSource.NANO_TIME);
timeout.waitFor("Timeout waiting for reserved executor to stop.", ()
-> rte.isStopped());
timeout.waitFor("Timeout waiting for reserved executor to stop.", rte::isStopped);
}

if (customThreadPool != null) {
Expand Down Expand Up @@ -750,12 +754,11 @@ public void dumpCoresInfo(PrintStream pw) throws IOException {
NamedList<Object> coreStatus = CoreAdminOperation.getCoreStatus(getCoreContainer(), core.getName(), false);
core.withSearcher(solrIndexSearcher -> {
SimpleOrderedMap<Object> lukeIndexInfo = LukeRequestHandler.getIndexInfo(solrIndexSearcher.getIndexReader());
@SuppressWarnings({"unchecked", "rawtypes"})
Map<String,Object> indexInfoMap = coreStatus.toMap(new LinkedHashMap<>());
indexInfoMap.putAll(lukeIndexInfo.toMap(new LinkedHashMap<>()));
pw.println(JSONUtil.toJSON(indexInfoMap, 2));

pw.println("");
pw.println();
return null;
});
}
Expand Down Expand Up @@ -897,7 +900,12 @@ public Properties getNodeProperties() {
private void waitForLoadingCoresToFinish(long timeoutMs) {
if (dispatchFilter != null) {
SolrDispatchFilter solrFilter = (SolrDispatchFilter) dispatchFilter.getFilter();
CoreContainer cores = solrFilter.getCores();
CoreContainer cores;
try {
cores = solrFilter.getCores();
} catch (UnavailableException e) {
throw new IllegalStateException("The CoreContainer is unavailable!");
}
if (cores != null) {
cores.waitForLoadingCoresToFinish(timeoutMs);
} else {
Expand Down
@@ -0,0 +1,23 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.servlet;

import org.apache.solr.core.CoreContainer;

public interface CoreContainerProvider {
gus-asf marked this conversation as resolved.
Show resolved Hide resolved
CoreContainer getCores();
}