Skip to content

Commit 2373a1f

Browse files
ludochgae-java-bot
authored andcommitted
Copybara import of the project:
-- ca5ae88 by Lachlan Roberts <[email protected]>: Fixes to completion of RequestManager in HttpMode Signed-off-by: Lachlan Roberts <[email protected]> -- e4ac138 by Lachlan Roberts <[email protected]>: Fixes to completion of RequestManager in HttpMode Signed-off-by: Lachlan Roberts <[email protected]> -- 668201b by Lachlan Roberts <[email protected]>: ensure the environment is set for finishRequest Signed-off-by: Lachlan Roberts <[email protected]> -- e3c930f by Lachlan Roberts <[email protected]>: changes from review Signed-off-by: Lachlan Roberts <[email protected]> -- c669f96 by Lachlan Roberts <[email protected]>: resolve conflicts Signed-off-by: Lachlan Roberts <[email protected]> PiperOrigin-RevId: 699218224 Change-Id: I15a31a4519057e9af342edd00227a9042b215d05
1 parent 1a58a04 commit 2373a1f

File tree

4 files changed

+90
-31
lines changed

4 files changed

+90
-31
lines changed

runtime/runtime_impl_jetty12/src/main/java/com/google/apphosting/runtime/jetty/http/JettyHttpHandler.java

+36-17
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@
3333
import com.google.apphosting.runtime.RequestRunner.EagerRunner;
3434
import com.google.apphosting.runtime.ResponseAPIData;
3535
import com.google.apphosting.runtime.ServletEngineAdapter;
36+
import com.google.apphosting.runtime.anyrpc.AnyRpcServerContext;
3637
import com.google.apphosting.runtime.jetty.AppInfoFactory;
3738
import com.google.common.flogger.GoogleLogger;
3839
import java.io.PrintWriter;
3940
import java.io.StringWriter;
4041
import java.time.Duration;
4142
import java.util.concurrent.TimeoutException;
4243
import org.eclipse.jetty.server.Handler;
44+
import org.eclipse.jetty.server.HttpStream;
4345
import org.eclipse.jetty.server.Request;
4446
import org.eclipse.jetty.server.Response;
4547
import org.eclipse.jetty.server.Server;
@@ -108,8 +110,12 @@ public boolean handle(Request request, Response response, Callback callback) thr
108110
ApiProxy.Environment currentEnvironment = ApiProxy.getCurrentEnvironment();
109111
request.setAttribute(AppEngineConstants.ENVIRONMENT_ATTR, currentEnvironment);
110112

113+
// Only run code to finish request with the RequestManager once the stream is complete.
114+
Request.addCompletionListener(
115+
request, t -> finishRequest(currentEnvironment, requestToken, genericResponse, context));
116+
111117
try {
112-
handled = dispatchRequest(requestToken, genericRequest, genericResponse, callback);
118+
handled = dispatchRequest(requestToken, genericRequest, genericResponse);
113119
if (handled) {
114120
callback.succeeded();
115121
}
@@ -123,27 +129,41 @@ public boolean handle(Request request, Response response, Callback callback) thr
123129
handled = handleException(ex, requestToken, genericResponse);
124130
Response.writeError(request, response, callback, ex);
125131
} finally {
126-
requestManager.finishRequest(requestToken);
127-
}
128-
// Do not put this in a final block. If we propagate an
129-
// exception the callback will be invoked automatically.
130-
genericResponse.finishWithResponse(context);
131-
// We don't want threads used for background requests to go back
132-
// in the thread pool, because users may have stashed references
133-
// to them or may be expecting them to exit. Setting the
134-
// interrupt bit causes the pool to drop them.
135-
if (genericRequest.getRequestType() == RuntimePb.UPRequest.RequestType.BACKGROUND) {
136-
Thread.currentThread().interrupt();
132+
// We don't want threads used for background requests to go back
133+
// in the thread pool, because users may have stashed references
134+
// to them or may be expecting them to exit. Setting the
135+
// interrupt bit causes the pool to drop them.
136+
if (genericRequest.getRequestType() == RuntimePb.UPRequest.RequestType.BACKGROUND) {
137+
Thread.currentThread().interrupt();
138+
}
137139
}
138140

139141
return handled;
140142
}
141143

144+
private void finishRequest(
145+
ApiProxy.Environment env,
146+
RequestManager.RequestToken requestToken,
147+
JettyResponseAPIData response,
148+
AnyRpcServerContext context) {
149+
150+
ApiProxy.Environment oldEnv = ApiProxy.getCurrentEnvironment();
151+
try {
152+
ApiProxy.setEnvironmentForCurrentThread(env);
153+
requestManager.finishRequest(requestToken);
154+
155+
// Do not put this in a final block. If we propagate an
156+
// exception the callback will be invoked automatically.
157+
response.finishWithResponse(context);
158+
} finally {
159+
ApiProxy.setEnvironmentForCurrentThread(oldEnv);
160+
}
161+
}
162+
142163
private boolean dispatchRequest(
143164
RequestManager.RequestToken requestToken,
144165
JettyRequestAPIData request,
145-
JettyResponseAPIData response,
146-
Callback callback)
166+
JettyResponseAPIData response)
147167
throws Throwable {
148168
switch (request.getRequestType()) {
149169
case SHUTDOWN:
@@ -154,14 +174,13 @@ private boolean dispatchRequest(
154174
dispatchBackgroundRequest(request, response);
155175
return true;
156176
case OTHER:
157-
return dispatchServletRequest(request, response, callback);
177+
return dispatchServletRequest(request, response);
158178
default:
159179
throw new IllegalStateException(request.getRequestType().toString());
160180
}
161181
}
162182

163-
private boolean dispatchServletRequest(
164-
JettyRequestAPIData request, JettyResponseAPIData response, Callback callback)
183+
private boolean dispatchServletRequest(JettyRequestAPIData request, JettyResponseAPIData response)
165184
throws Throwable {
166185
Request jettyRequest = request.getWrappedRequest();
167186
Response jettyResponse = response.getWrappedResponse();

runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyHttpHandler.java

+49-11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.google.apphosting.runtime.RequestRunner.EagerRunner;
3535
import com.google.apphosting.runtime.ResponseAPIData;
3636
import com.google.apphosting.runtime.ServletEngineAdapter;
37+
import com.google.apphosting.runtime.anyrpc.AnyRpcServerContext;
3738
import com.google.common.flogger.GoogleLogger;
3839
import java.io.IOException;
3940
import java.io.PrintWriter;
@@ -44,8 +45,10 @@
4445
import javax.servlet.http.HttpServletRequest;
4546
import javax.servlet.http.HttpServletResponse;
4647
import org.eclipse.jetty.server.Handler;
48+
import org.eclipse.jetty.server.HttpChannel;
4749
import org.eclipse.jetty.server.Request;
4850
import org.eclipse.jetty.server.Server;
51+
import org.eclipse.jetty.server.ServerConnector;
4952
import org.eclipse.jetty.server.handler.HandlerWrapper;
5053

5154
/**
@@ -62,6 +65,9 @@
6265
public class JettyHttpHandler extends HandlerWrapper {
6366
private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();
6467

68+
public static final String FINISH_REQUEST_ATTRIBUTE =
69+
"com.google.apphosting.runtime.jetty9.finishRequestAttribute";
70+
6571
private final boolean passThroughPrivateHeaders;
6672
private final AppInfoFactory appInfoFactory;
6773
private final AppVersionKey appVersionKey;
@@ -73,7 +79,8 @@ public JettyHttpHandler(
7379
ServletEngineAdapter.Config runtimeOptions,
7480
AppVersion appVersion,
7581
AppVersionKey appVersionKey,
76-
AppInfoFactory appInfoFactory) {
82+
AppInfoFactory appInfoFactory,
83+
ServerConnector connector) {
7784
this.passThroughPrivateHeaders = runtimeOptions.passThroughPrivateHeaders();
7885
this.appInfoFactory = appInfoFactory;
7986
this.appVersionKey = appVersionKey;
@@ -82,6 +89,7 @@ public JettyHttpHandler(
8289
ApiProxyImpl apiProxyImpl = (ApiProxyImpl) ApiProxy.getDelegate();
8390
coordinator = apiProxyImpl.getBackgroundRequestCoordinator();
8491
requestManager = (RequestManager) apiProxyImpl.getRequestThreadManager();
92+
connector.addBean(new CompletionListener());
8593
}
8694

8795
@Override
@@ -113,6 +121,10 @@ public void handle(
113121
ApiProxy.Environment currentEnvironment = ApiProxy.getCurrentEnvironment();
114122
request.setAttribute(AppEngineConstants.ENVIRONMENT_ATTR, currentEnvironment);
115123

124+
Runnable finishRequest =
125+
() -> finishRequest(currentEnvironment, requestToken, genericResponse, context);
126+
baseRequest.setAttribute(FINISH_REQUEST_ATTRIBUTE, finishRequest);
127+
116128
try {
117129
dispatchRequest(target, requestToken, genericRequest, genericResponse);
118130
if (!baseRequest.isHandled()) {
@@ -128,17 +140,32 @@ public void handle(
128140
handleException(ex, requestToken, genericResponse);
129141
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
130142
} finally {
131-
requestManager.finishRequest(requestToken);
143+
// We don't want threads used for background requests to go back
144+
// in the thread pool, because users may have stashed references
145+
// to them or may be expecting them to exit. Setting the
146+
// interrupt bit causes the pool to drop them.
147+
if (genericRequest.getRequestType() == RuntimePb.UPRequest.RequestType.BACKGROUND) {
148+
Thread.currentThread().interrupt();
149+
}
132150
}
133-
// Do not put this in a final block. If we propagate an
134-
// exception the callback will be invoked automatically.
135-
genericResponse.finishWithResponse(context);
136-
// We don't want threads used for background requests to go back
137-
// in the thread pool, because users may have stashed references
138-
// to them or may be expecting them to exit. Setting the
139-
// interrupt bit causes the pool to drop them.
140-
if (genericRequest.getRequestType() == RuntimePb.UPRequest.RequestType.BACKGROUND) {
141-
Thread.currentThread().interrupt();
151+
}
152+
153+
private void finishRequest(
154+
ApiProxy.Environment env,
155+
RequestManager.RequestToken requestToken,
156+
JettyResponseAPIData response,
157+
AnyRpcServerContext context) {
158+
159+
ApiProxy.Environment oldEnv = ApiProxy.getCurrentEnvironment();
160+
try {
161+
ApiProxy.setEnvironmentForCurrentThread(env);
162+
requestManager.finishRequest(requestToken);
163+
164+
// Do not put this in a final block. If we propagate an
165+
// exception the callback will be invoked automatically.
166+
response.finishWithResponse(context);
167+
} finally {
168+
ApiProxy.setEnvironmentForCurrentThread(oldEnv);
142169
}
143170
}
144171

@@ -288,4 +315,15 @@ private String getBackgroundRequestId(JettyRequestAPIData upRequest) {
288315
}
289316
return backgroundRequestId;
290317
}
318+
319+
private static class CompletionListener implements HttpChannel.Listener {
320+
@Override
321+
public void onComplete(Request request) {
322+
Runnable finishRequest =
323+
(Runnable) request.getAttribute(JettyHttpHandler.FINISH_REQUEST_ATTRIBUTE);
324+
if (finishRequest != null) {
325+
finishRequest.run();
326+
}
327+
}
328+
}
291329
}

runtime/runtime_impl_jetty9/src/main/java/com/google/apphosting/runtime/jetty9/JettyServletEngineAdapter.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.Optional;
4444
import javax.servlet.ServletException;
4545
import org.eclipse.jetty.server.Server;
46+
import org.eclipse.jetty.server.ServerConnector;
4647
import org.eclipse.jetty.server.handler.SizeLimitHandler;
4748
import org.eclipse.jetty.util.thread.QueuedThreadPool;
4849

@@ -143,10 +144,11 @@ public void start(String serverInfo, ServletEngineAdapter.Config runtimeOptions)
143144
logger.atInfo().log("Using HTTP_CONNECTOR_MODE to bypass RPC");
144145
appVersionKey = AppVersionKey.fromAppInfo(appinfo);
145146
AppVersion appVersion = appVersionHandlerMap.getAppVersion(appVersionKey);
147+
ServerConnector connector = JettyHttpProxy.newConnector(server, runtimeOptions);
148+
server.addConnector(connector);
146149
server.insertHandler(
147-
new JettyHttpHandler(runtimeOptions, appVersion, appVersionKey, appInfoFactory));
150+
new JettyHttpHandler(runtimeOptions, appVersion, appVersionKey, appInfoFactory, connector));
148151
JettyHttpProxy.insertHandlers(server, ignoreResponseSizeLimit);
149-
server.addConnector(JettyHttpProxy.newConnector(server, runtimeOptions));
150152
} else {
151153
server.setAttribute(
152154
"com.google.apphosting.runtime.jetty9.appYaml",

runtime/testapps/src/main/resources/com/google/apphosting/runtime/jetty9/gzipapp/jetty94/WEB-INF/appengine-web.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
<application>gzip</application>
2121
<threadsafe>true</threadsafe>
2222
<system-properties>
23-
<property name="appengine.use.EE8" value="true"/>
23+
<property name="appengine.use.EE8" value="false"/>
2424
</system-properties>
2525
</appengine-web-app>

0 commit comments

Comments
 (0)