You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add an ability to run protobuf server as a Servlet 4 #1621
Pls add an ability to run a protobuf v3 server as a Servlet 4 on any server with the Servlet 4 support.
Reacted by Artur Krysiak, Michael Paesold, Simon, Diego Vélez Gil, bsandoval, Marcos Céspedes, David Hofmann, aria, Krishna Raj, Nicholas DiPiazza and 26 more
Dump from what I wrote on SO to have the information in one place:
It does seem feasible to use the async servlet APIs to implement a gRPC server (as an alternative to the Netty server), except possibly for trailers. It's not 100% clear how to send trailers when the server is HTTP/2, since a common technique with HTTP/1 was for the servlet to manually perform chunked encoding which does not exist in HTTP/2.
I'll note one part of the docs that could be an annoyance, but I think they are talking about the remote, not the servlet (in which case we're fine to ignore it):
The RFC requires the name of every key that is to be in the supplied Map is included in the comma separated list that is the value of the "Trailer" response header. The application is responsible for ensuring this requirement is met. Failure to do so may lead to interoperability failures.
If it is for servlet interoperability we can still live with it, but it requires the application to help us out. Go already has to deal with something similar when using the Go-provided http/2 server.
@ejona86 can you elaborate what exactly you don't know how to do using servlets? I want to help implement this feature.
Is not this what you are referring to?
@WebServlet(asyncSupported = true)
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
AsyncContext asyncContext = req.startAsync(req, resp);
// here you pass this asyncContext to any thread worker who
// will be querying the REQ data
// then you can send a response like
((HttpServletResponse)asyncContext.getResponse()).setHeader("Transfer-Encoding","chunked");
asyncContext.getResponse().getOutputStream().write(new byte[]{});// write your bytes here
asyncContext.getResponse().getOutputStream().flush();
((HttpServletResponse)asyncContext.getResponse()).setTrailerFields(() ->{
return new HashMap<>(); // add any trailing headers
});
}
}
If there is something specific that servlet is not doing for you lets discus explicitly and I'll help.
Even if servlet spec does not allow to run gRPC server, we can still provide enough adapters so that it can run on top of jetty, undertow and tomcat. In that way at least servlets and gRPC can share the same port
An example of what I mentioned was already in the works, see wildfly mailing list
@hofmanndavid, I think it would be (relatively) easy to make a servlet that would handle the I/O for gRPC. Any questions remaining in my mind are mainly on the nicest way of configuring the thing.
It would implement InternalServer and call ServerListener each inbound RPC. There's a handful of HTTP-isms that we've not split out of NettyServerHandler.onHeadersRead like we have with Http2ClientStreamTransportState, but that's not too big of a deal. For the while it could just be duplicated code.
The "Trailer" in the headers is required by RFC7230 (HTTP/1.1) . So hopefully the "interoperability failure" will only occur for HTTP/1.1 clients, and HTTP/2 clients can still interoperate.
The RFC requires the name of every key that is to be in the supplied Map is included in the comma separated list that is the value of the "Trailer" response header. The application is responsible for ensuring this requirement is met. Failure to do so may lead to interoperability failures.
If it is for servlet interoperability we can still live with it, but it requires the application to help us out. Go already has to deal with something similar when using the Go-provided http/2 server.
Some gRPC implementations may allow the Path format shown above to be overridden, but this functionality is strongly discouraged. gRPC does not go out of its way to break users that are using this kind of override, but we do not actively support it, and some functionality (e.g., service config support) will not work when the path is not of the form shown above.
The initial problem I am seeing when trying to use a servlet container instead of netty is that netty owns the root path /*
In a servlet container only one deployed artifact will be able to access the root path /*, the others will have a 'contextPath' of their own.
So. We are dealing with multiple applications running in a servlet container. eg.
/ --> one-roor-app.war (app1)
/InternalService/ --> internal-app.war (app2)
There is a rule for request mapping in servlets and is that the longest path that matches is the one used to route a request to the proper app.
In case both apps will use grpc, and is what should be supported. This will happen:
app1 exposes a grpc called GreetService and we will be able to route it properly as it owns the root path. OK.
app1 exposes a grpc called InternalService and the servlet will route the request to app2. NOT_OK.
app2 will want to expose GreetService too!, but it will always be routed to app1. NOT_OK.
For grpc to work in a servlet container the proper way to support it IMHO is to:
Allow the client to add a prefix to all service names. Preferably as an optional value set when creating the channel.
Allow the server to be aware of that prefix.
It would be desirable to have an optional parameter in the proto file that will trigger small client code generation for adding the prefix (maybe not optional if defined in the proto file)
This will lower the entry barrier for grpc usage inside enterprises that rely a lot in application servers.
Is the reasoning correct or is there a fundamental problem?
I was left with a doubt, if I keep the root path only for my gRPC implementations, and the contexts for HTTP1 implementations, will I encounter problems?
If your application server supports both HTTP/2 and HTTP1 traffic, then it should work for grpc client to grpc path and normal HTTP/1 client to other path.
@rogeriob2br the grpc maintainers haven't given any updates on this issue in some time. There shouldn't be any problem keeping "the root path only for my gRPC implementations". WAS 9 isn't going to support this at all, since it doesn't provide an HTTP/2 implementation. Liberty partially supports #4738, but full duplex streaming isn't working. From the OpenLiberty side, we're working on new features to directly support grpc - see OpenLiberty/open-liberty#8637
I've been working on this branch recently, and have it updated to latest grpc (as well as intermediate versions in case someone happens to need it backported), with all tests passing in undertow and jetty (see jetty/jetty.project#6917 for the ability to run this also in Jetty 9.4.x), and two failing in tomcat. Is there interest still in accepting this upstream, or should I explore options to release this directly when I reach some milestone?
@niloc132 If you want Servlet 4.0 behavior of HttpServletResponse.setTrailerFields(Supplier<Map<String, String>> supplier) and HttpServletResponse.getTrailerFields(), then you should be using Jetty 10.0.x which supports Servlet 4.0 (and those two methods).
Jetty 9.4.x is in maintenance mode now, only handling bug fixes and security fixes, not new functionality.
Jetty 10.0.x (for javax.servlet.* users) and Jetty 11.0.x (for jakarta.servlet.* users) are the mainline codebases on Jetty now.
Note that javax.servlet.* is now dead, no new features on that namespace ever again.
Code should start migrating to jakarta.servlet.* soon. (Many 3rd party libraries have already migrated, with spring being the last big holdout, but even they have started the migration of their codebase)
Thanks for taking a look here @joakime - as grpc-java builds with baseline support for Java 8, it can only conditionally run the integration tests if Java 11+ happens to be available. Separately, I'm investigating using eclipse's transformer tool to enable several projects I use to ease this migration.
Java 8 is scheduled to end it's "Premier Support" on March 2022 (6 months from now), going to "Extended Support" model until Dec 2030 at which point it will enter into the "Lifetime Support" mode.
If past JVMs are any indication, once this transition from "Premier" to "Extended" occurs, the public JVMs are no longer available (JVM updates are only available to subscribers paying for support).
In my mind, this lack of public JVMs, makes Java 8 not a viable choice for open source projects that need/rely on SSL/TLS. (but we'll see what the world of OpenJDK 8 brings to the table in this regard)
As we have all learned, the SSL/TLS space changes quite rapidly and you'll be quickly out of date and unable to support an ever increasing set of users and devices.
I don't see this as a good thing for something that relies on networking, like grpc or Jetty.
Separately, I'm investigating using eclipse's transformer tool to enable several projects I use to ease this migration.
Don't waste your time, the upcoming Jakarta Servlet 6.0 (due out in the next couple of months) has already removed long deprecated classes and methods.
Most Jakarta EE projects are taking Jakarta EE 10 as the point in time to remove deprecated classes and methods.
All of the transformer tools that currently exist cannot cope with that level of API change.
as grpc-java builds with baseline support for Java 8, it can only conditionally run the integration tests if Java 11+ happens to be available
Using Assume to skip certain tests on Java 8 is fine. No big deal.
As it happens, we are still supporting Java 7. We hope to drop that "soon" (#4671). I've heard orphaned projects (including "v2 is a completely different API and v1 is now dead and I hope you like rewriting") have made it a "process" to upgrade, especially for things like application containers that can be picky about their JRE. That's a different sort of problem compared to Java 8 vs 11 where Java 11 is slower for some workloads (both JIT and GC can contribute).
This has been merged in 706646f, with support for both javax and jakarta servlet apis. Jetty seems to have the best support, though jetty/jetty.project#8405 may cause errors for grpc streams that don't respond within 30s - I'll follow up soon with the workaround for this that we're using in production.
Outside from that I'm still planning to continue with the notes in #4823 (comment) - landing an in-process grpc-web proxy, and hopefully other outright offering the websocket transport we use (compatible with https://github.com/improbable-eng/grpc-web/), or some simple changes to the ServletAdapter so that the ServerTransportListener, etc can be shared between multiple endpoints, which enables custom transports (use case being streaming data where tls isn't available, like localhost development).
Reacted by ZHANG Dapeng, 宁培培, Joakim Erdfelt, Bruce Clounie and Richard North
Activity
ejona86 commentedon Apr 1, 2016
Dump from what I wrote on SO to have the information in one place:
cs224 commentedon Dec 14, 2017
Could someone please give a status update on this topic? Thanks!
ejona86 commentedon Dec 14, 2017
No progress. I still don't know how to send trailers in this environment.
jnehlmeier commentedon Mar 19, 2018
JavaDoc for Java EE 8 / Servlet 4.0 describes an API to set trailers on a response.
https://javaee.github.io/javaee-spec/javadocs/javax/servlet/http/HttpServletResponse.html#setTrailerFields-java.util.function.Supplier-
ejona86 commentedon Mar 19, 2018
@jnehlmeier, thank you!
I'll note one part of the docs that could be an annoyance, but I think they are talking about the remote, not the servlet (in which case we're fine to ignore it):
If it is for servlet interoperability we can still live with it, but it requires the application to help us out. Go already has to deal with something similar when using the Go-provided http/2 server.
hofmanndavid commentedon Jun 12, 2018
@ejona86 can you elaborate what exactly you don't know how to do using servlets? I want to help implement this feature.
Is not this what you are referring to?
If there is something specific that servlet is not doing for you lets discus explicitly and I'll help.
Even if servlet spec does not allow to run gRPC server, we can still provide enough adapters so that it can run on top of jetty, undertow and tomcat. In that way at least servlets and gRPC can share the same port
An example of what I mentioned was already in the works, see wildfly mailing list
ejona86 commentedon Jun 12, 2018
@hofmanndavid, I think it would be (relatively) easy to make a servlet that would handle the I/O for gRPC. Any questions remaining in my mind are mainly on the nicest way of configuring the thing.
It would implement
InternalServerand callServerListenereach inbound RPC. There's a handful of HTTP-isms that we've not split out of NettyServerHandler.onHeadersRead like we have with Http2ClientStreamTransportState, but that's not too big of a deal. For the while it could just be duplicated code.hofmanndavid commentedon Jun 12, 2018
Okay, Will try to get back when I have something. give me 2 weeks.
ejona86 commentedon Jun 12, 2018
CC @dapengzhang0
dapengzhang0 commentedon Jun 13, 2018
The "Trailer" in the headers is required by RFC7230 (HTTP/1.1) . So hopefully the "interoperability failure" will only occur for HTTP/1.1 clients, and HTTP/2 clients can still interoperate.
ejona86 commentedon Jun 13, 2018
The "Trailer" header applies equally in HTTP/1.1 and HTTP/2. HTTP/2 didn't change those semantics.
hofmanndavid commentedon Jun 14, 2018
Quoting from the protocol doc
The initial problem I am seeing when trying to use a servlet container instead of netty is that netty owns the root path /*
In a servlet container only one deployed artifact will be able to access the root path /*, the others will have a 'contextPath' of their own.
So. We are dealing with multiple applications running in a servlet container. eg.
/ --> one-roor-app.war (app1)
/InternalService/ --> internal-app.war (app2)
There is a rule for request mapping in servlets and is that the longest path that matches is the one used to route a request to the proper app.
In case both apps will use grpc, and is what should be supported. This will happen:
For grpc to work in a servlet container the proper way to support it IMHO is to:
It would be desirable to have an optional parameter in the proto file that will trigger small client code generation for adding the prefix (maybe not optional if defined in the proto file)
This will lower the entry barrier for grpc usage inside enterprises that rely a lot in application servers.
Is the reasoning correct or is there a fundamental problem?
26 remaining items
dapengzhang0 commentedon May 6, 2020
@wtlucy In what way full duplex streaming isn't working? Any error or hanging?
dapengzhang0 commentedon May 6, 2020
If your application server supports both HTTP/2 and HTTP1 traffic, then it should work for grpc client to grpc path and normal HTTP/1 client to other path.
rogeriob2br commentedon May 6, 2020
:(
wtlucy commentedon May 11, 2020
@dapengzhang0 there's an error - but the problem is with Liberty's async behavior. We're working on a fix on that side.
niloc132 commentedon Sep 23, 2021
I've been working on this branch recently, and have it updated to latest grpc (as well as intermediate versions in case someone happens to need it backported), with all tests passing in undertow and jetty (see jetty/jetty.project#6917 for the ability to run this also in Jetty 9.4.x), and two failing in tomcat. Is there interest still in accepting this upstream, or should I explore options to release this directly when I reach some milestone?
I'll soon be exploring #4823 as well and possibly a servlet based implementation of the websocket handling present in https://github.com/improbable-eng/grpc-web/tree/master/go/grpcweb for non-tls streaming browser connections, and would love any feedback on if that work belongs in the grpc-servlet project, or as an external component. I'm happy to have discussion take place either here or 4823 on this topic.
joakime commentedon Sep 23, 2021
@niloc132 If you want Servlet 4.0 behavior of
HttpServletResponse.setTrailerFields(Supplier<Map<String, String>> supplier)andHttpServletResponse.getTrailerFields(), then you should be using Jetty 10.0.x which supports Servlet 4.0 (and those two methods).Jetty 9.4.x is in maintenance mode now, only handling bug fixes and security fixes, not new functionality.
Jetty 10.0.x (for
javax.servlet.*users) and Jetty 11.0.x (forjakarta.servlet.*users) are the mainline codebases on Jetty now.Note that
javax.servlet.*is now dead, no new features on that namespace ever again.Code should start migrating to
jakarta.servlet.*soon. (Many 3rd party libraries have already migrated, with spring being the last big holdout, but even they have started the migration of their codebase)niloc132 commentedon Sep 23, 2021
Thanks for taking a look here @joakime - as grpc-java builds with baseline support for Java 8, it can only conditionally run the integration tests if Java 11+ happens to be available. Separately, I'm investigating using eclipse's transformer tool to enable several projects I use to ease this migration.
joakime commentedon Sep 23, 2021
Why Java 8 still?
Java 8 is scheduled to end it's "Premier Support" on March 2022 (6 months from now), going to "Extended Support" model until Dec 2030 at which point it will enter into the "Lifetime Support" mode.
If past JVMs are any indication, once this transition from "Premier" to "Extended" occurs, the public JVMs are no longer available (JVM updates are only available to subscribers paying for support).
In my mind, this lack of public JVMs, makes Java 8 not a viable choice for open source projects that need/rely on SSL/TLS. (but we'll see what the world of OpenJDK 8 brings to the table in this regard)
As we have all learned, the SSL/TLS space changes quite rapidly and you'll be quickly out of date and unable to support an ever increasing set of users and devices.
I don't see this as a good thing for something that relies on networking, like grpc or Jetty.
joakime commentedon Sep 23, 2021
Don't waste your time, the upcoming Jakarta Servlet 6.0 (due out in the next couple of months) has already removed long deprecated classes and methods.
Most Jakarta EE projects are taking Jakarta EE 10 as the point in time to remove deprecated classes and methods.
All of the transformer tools that currently exist cannot cope with that level of API change.
ejona86 commentedon Sep 23, 2021
Using Assume to skip certain tests on Java 8 is fine. No big deal.
As it happens, we are still supporting Java 7. We hope to drop that "soon" (#4671). I've heard orphaned projects (including "v2 is a completely different API and v1 is now dead and I hope you like rewriting") have made it a "process" to upgrade, especially for things like application containers that can be picky about their JRE. That's a different sort of problem compared to Java 8 vs 11 where Java 11 is slower for some workloads (both JIT and GC can contribute).
niloc132 commentedon Jan 20, 2023
This has been merged in 706646f, with support for both javax and jakarta servlet apis. Jetty seems to have the best support, though jetty/jetty.project#8405 may cause errors for grpc streams that don't respond within 30s - I'll follow up soon with the workaround for this that we're using in production.
Outside from that I'm still planning to continue with the notes in #4823 (comment) - landing an in-process grpc-web proxy, and hopefully other outright offering the websocket transport we use (compatible with https://github.com/improbable-eng/grpc-web/), or some simple changes to the ServletAdapter so that the ServerTransportListener, etc can be shared between multiple endpoints, which enables custom transports (use case being streaming data where tls isn't available, like localhost development).