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
26 changes: 24 additions & 2 deletions solr/core/src/java/org/apache/solr/servlet/AdminServlet.java
@@ -1,3 +1,19 @@
/*
* 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 javax.servlet.ServletException;
Expand Down Expand Up @@ -29,7 +45,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
if (excludedPath(excludes,req,resp,null)) {
return;
}
super.doGet(req, resp);
ServletUtils.rateLimitRequest(req,resp,() -> {
// stuff from HttpSolrCallHere
},false);
// todo: enable tracing
}

@Override
Expand All @@ -39,7 +58,10 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S
if (excludedPath(excludes,req,resp,null)) {
return;
}
super.doPost(req, resp);
ServletUtils.rateLimitRequest(req,resp,() -> {
// stuff from HttpSolrCallHere
},false);
//todo: enable tracing
}

@Override
Expand Down
@@ -0,0 +1,25 @@
/*
* 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;

public class ExceptionWhileTracing extends RuntimeException {
public Exception e;

public ExceptionWhileTracing(Exception e) {
this.e = e;
}
}
16 changes: 16 additions & 0 deletions solr/core/src/java/org/apache/solr/servlet/PathExcluder.java
@@ -1,3 +1,19 @@
/*
* 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 java.util.ArrayList;
Expand Down
135 changes: 135 additions & 0 deletions solr/core/src/java/org/apache/solr/servlet/ServletUtils.java
Expand Up @@ -17,6 +17,20 @@

package org.apache.solr.servlet;

import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.noop.NoopSpan;
import io.opentracing.noop.NoopTracer;
import io.opentracing.propagation.Format;
import io.opentracing.tag.Tags;
import org.apache.http.HttpHeaders;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.request.SolrRequestInfo;
import org.apache.solr.util.tracing.HttpServletCarrier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.FilterChain;
import javax.servlet.ReadListener;
import javax.servlet.ServletException;
Expand All @@ -30,6 +44,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -38,6 +53,8 @@
* Various Util methods for interaction on servlet level, i.e. HttpServletRequest
*/
public abstract class ServletUtils {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

static String CLOSE_STREAM_MSG = "Attempted close of http request or response stream - in general you should not do this, "
+ "you may spoil connection reuse and possibly disrupt a client. If you must close without actually needing to close, "
+ "use a CloseShield*Stream. Closing or flushing the response stream commits the response and prevents us from modifying it. "
Expand Down Expand Up @@ -158,6 +175,124 @@ static void configExcludes(PathExcluder excluder, String patternConfig) {
}
}

static void rateLimitRequest(HttpServletRequest request, HttpServletResponse response, Runnable limitedExecution, boolean trace) throws ServletException, IOException {
boolean accepted = false;
RateLimitManager rateLimitManager = getRateLimitManager(request);
try {
try {
accepted = rateLimitManager.handleRequest(request);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new SolrException(ErrorCode.SERVER_ERROR, e.getMessage());
}

if (!accepted) {
String errorMessage = "Too many requests for this request type." +
"Please try after some time or increase the quota for this request type";

response.sendError(429, errorMessage);
}
// todo: this shouldn't be required, tracing and rate limiting should be independently composable
traceHttpRequestExecution2(request, response, limitedExecution, trace);
} finally {
if (accepted) {
rateLimitManager.decrementActiveRequests(request);
}
}
}

/**
* Sets up tracing for an HTTP request.
*
* @param tracedExecution the executed code
*/
private static void traceHttpRequestExecution2(HttpServletRequest request, HttpServletResponse response, Runnable tracedExecution, boolean required) throws ServletException, IOException {
Tracer tracer = getTracer(request);
if (tracer != null) {
Span span = buildSpan(tracer, request);

request.setAttribute(Span.class.getName(), span);
try (var scope = tracer.scopeManager().activate(span)) {

assert scope != null; // prevent javac warning about scope being unused
try {
tracedExecution.run();
} catch (ExceptionWhileTracing e) {
if (e.e instanceof SolrAuthenticationException) {
throw (SolrAuthenticationException) e.e;
}
if (e.e instanceof ServletException) {
throw (ServletException) e.e;
}
if (e.e instanceof IOException) {
throw (IOException) e.e;
}
if (e.e instanceof RuntimeException) {
throw (RuntimeException) e.e;
} else {
throw new RuntimeException(e.e);
}
}
} catch (SolrAuthenticationException e) {
// done, the response and status code have already been sent
} finally {
consumeInputFully(request, response);
SolrRequestInfo.reset();
SolrRequestParsers.cleanupMultipartFiles(request);


span.setTag(Tags.HTTP_STATUS, response.getStatus());
span.finish();
}
} else {
if (required) {
throw new IllegalStateException("Tracing required, but could not find Tracer in request attribute:" + SolrDispatchFilter.ATTR_TRACING_TRACER);
} else {
tracedExecution.run();
}
}
}

private static Tracer getTracer(HttpServletRequest req) {
return (Tracer) req.getAttribute(SolrDispatchFilter.ATTR_TRACING_TRACER);
}

private static RateLimitManager getRateLimitManager(HttpServletRequest req) {
return (RateLimitManager) req.getAttribute(SolrDispatchFilter.ATTR_RATELIMIT_MANAGER);
}

protected static Span buildSpan(Tracer tracer, HttpServletRequest request) {
if (tracer instanceof NoopTracer) {
return NoopSpan.INSTANCE;
}
Tracer.SpanBuilder spanBuilder = tracer.buildSpan("http.request") // will be changed later
.asChildOf(tracer.extract(Format.Builtin.HTTP_HEADERS, new HttpServletCarrier(request)))
.withTag(Tags.SPAN_KIND, Tags.SPAN_KIND_SERVER)
.withTag(Tags.HTTP_METHOD, request.getMethod())
.withTag(Tags.HTTP_URL, request.getRequestURL().toString());
if (request.getQueryString() != null) {
spanBuilder.withTag("http.params", request.getQueryString());
}
spanBuilder.withTag(Tags.DB_TYPE, "solr");
return spanBuilder.start();
}

// we make sure we read the full client request so that the client does
// not hit a connection reset and we can reuse the
// connection - see SOLR-8453 and SOLR-8683
private static void consumeInputFully(HttpServletRequest req, HttpServletResponse response) {
try {
ServletInputStream is = req.getInputStream();
while (!is.isFinished() && is.read() != -1) {}
} catch (IOException e) {
if (req.getHeader(HttpHeaders.EXPECT) != null && response.isCommitted()) {
log.debug("No input stream to consume from client");
} else {
log.info("Could not consume full client request", e);
}
}
}

public static class ClosedServletInputStream extends ServletInputStream {

public static final ClosedServletInputStream CLOSED_SERVLET_INPUT_STREAM = new ClosedServletInputStream();
Expand Down
@@ -0,0 +1,20 @@
/*
* 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;

public class SolrAuthenticationException extends Exception{
}