Skip to content

Add an ability to run protobuf server as a Servlet 4 #1621

Closed

Description

@trknz

Pls add an ability to run a protobuf v3 server as a Servlet 4 on any server with the Servlet 4 support.

Activity

  1. ejona86 commented on Apr 1, 2016

    @ejona86
    Member

    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.

  2. added this to the Unscheduled milestone on Apr 1, 2016
  3. cs224 commented on Dec 14, 2017

    @cs224

    Could someone please give a status update on this topic? Thanks!

  4. ejona86 commented on Dec 14, 2017

    @ejona86
    Member

    No progress. I still don't know how to send trailers in this environment.

  5. jnehlmeier commented on Mar 19, 2018

    @jnehlmeier
  6. ejona86 commented on Mar 19, 2018

    @ejona86
    Member

    @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):

    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.

  7. hofmanndavid commented on Jun 12, 2018

    @hofmanndavid

    @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

  8. ejona86 commented on Jun 12, 2018

    @ejona86
    Member

    @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.

  9. hofmanndavid commented on Jun 12, 2018

    @hofmanndavid

    Okay, Will try to get back when I have something. give me 2 weeks.

  10. modified the milestones: Unscheduled, Next on Jun 12, 2018
  11. ejona86 commented on Jun 12, 2018

    @ejona86
    Member
  12. dapengzhang0 commented on Jun 13, 2018

    @dapengzhang0
    Contributor

    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.

  13. ejona86 commented on Jun 13, 2018

    @ejona86
    Member

    The "Trailer" header applies equally in HTTP/1.1 and HTTP/2. HTTP/2 didn't change those semantics.

  14. hofmanndavid commented on Jun 14, 2018

    @hofmanndavid

    Quoting from the protocol doc

    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:

    1. app1 exposes a grpc called GreetService and we will be able to route it properly as it owns the root path. OK.
    2. app1 exposes a grpc called InternalService and the servlet will route the request to app2. NOT_OK.
    3. 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:

    1. Allow the client to add a prefix to all service names. Preferably as an optional value set when creating the channel.
    2. 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?

  15. 26 remaining items

  16. dapengzhang0 commented on May 6, 2020

    @dapengzhang0
    Contributor

    Liberty partially supports #4738, but full duplex streaming isn't working.

    @wtlucy In what way full duplex streaming isn't working? Any error or hanging?

  17. dapengzhang0 commented on May 6, 2020

    @dapengzhang0
    Contributor

    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.

  18. rogeriob2br commented on May 6, 2020

    @rogeriob2br

    @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

    :(

  19. wtlucy commented on May 11, 2020

    @wtlucy

    @wtlucy In what way full duplex streaming isn't working? Any error or hanging?

    @dapengzhang0 there's an error - but the problem is with Liberty's async behavior. We're working on a fix on that side.

  20. niloc132 commented on Sep 23, 2021

    @niloc132
    Contributor

    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.

  21. joakime commented on Sep 23, 2021

    @joakime

    @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)

  22. niloc132 commented on Sep 23, 2021

    @niloc132
    Contributor

    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.

  23. joakime commented on Sep 23, 2021

    @joakime

    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.

  24. joakime commented on Sep 23, 2021

    @joakime

    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.

  25. ejona86 commented on Sep 23, 2021

    @ejona86
    Member

    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).

  26. niloc132 commented on Jan 20, 2023

    @niloc132
    Contributor

    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).

  27. modified the milestones: Next, 1.53 on Mar 13, 2023
  28. locked as resolved and limited conversation to collaborators on Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions