Skip to content

L10: A user friendly C++ gRPC API using Reactive Streams - #33

Closed
per-gron wants to merge 3 commits into
grpc:masterfrom
per-gron:rs-grpc
Closed

per-gron wants to merge 3 commits into
grpc:masterfrom
per-gron:rs-grpc

Conversation

@per-gron

@per-gron per-gron commented Sep 6, 2017

Copy link
Copy Markdown

Reviewing this proposal may take quite some time, especially if you do not know Reactive Streams, I am sorry about that. Hopefully it will be worth your time!

I would love to get any and all feedback, from anyone, about tiny details and the overall idea. Is it good? Bad? Irrelevant? Useless? Flames welcome.

@googlebot

Copy link
Copy Markdown

We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for the commit author(s). If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again.

@googlebot

Copy link
Copy Markdown

CLAs look good, thanks!

@googlebot googlebot added cla: yes and removed cla: no labels Sep 6, 2017
@per-gron per-gron changed the title A user friendly C++ gRPC API using Reactive Streams L10: A user friendly C++ gRPC API using Reactive Streams Sep 6, 2017

#### Threading model

The default threading model for a server that uses this library is:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not take an approach ala the ThreadManager used in the synchronous API to allow autosizing the thread count based upon active workload?

Such an approach would eliminate the need for line 313 in this document, and further simplify exposed API.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment! The threading model section is the one I have put the least effort in. I will read and think more about this tomorrow and update the doc and/or reply here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that such an approach would not have a negative performance impact? Every test and benchmark I have ever seen has clearly showed that having much more than 1 thread per core causes performance to degrade significantly when the server is under heavy load due to context switches.

I am pretty sure you know more about this stuff than I do so if you say so I will take your word for it but it does surprise me.

Or is the point that doing everything async is best, but if you do have blocking operations, you might as well do it on the same thread pool?


* **The current synchronous API:** Too slow for many use cases.
* **The current asynchronous API:** Too low level for many use cases.
* **Futures, as suggested in [issue #7352](https://github.com/grpc/grpc/issues/7352):** Futures typically do not have a concept of cancellation and they do not map naturally to streams.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a more fleshed out proposal for futures internally, and cancellation is fully supported. Hoping @vjpai can take some time to publish it soon.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds very interesting! I would love to read that.


[Reactive Streams](http://www.reactive-streams.org/) is currently defined only for Java and Javascript. This project needs a C++ version.

In preparation for this RPC the author has written [a Reactive Streams library in C++ called rs](https://github.com/per-gron/shuriken/tree/master/src/rs). rs is well documented, well tested and implements many of the [ReactiveX operators](http://reactivex.io/documentation/operators.html). Reading rs' documentation may help explain parts of this RFC.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would accepting this proposal imply taking a dependency on rs?

Additional forced dependencies on users may be a deal breaker for us.

@per-gron per-gron Sep 6, 2017 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see a few ways to do this:

  1. Since rs is not used anywhere else and was made basically for this purpose, rs could be made a part of this and included with the rest of the code.

  2. If rs is deemed too heavy of a dependency (the implementation + test suite is currently ~7.5kLOC counted with wc -l), then we could include only the interface part of rs and leave out all of the operators (they are most of the code). This seems a bit awkward to me but is certainly doable.

  3. Given that Publisher is a "concept" (as in forward iterator etc), it is probably technically possible to not have a dependency to rs at all. However, in practice you would have to depend on rs to get things done.

* `ConcatMap` is similar to `Map` but it lets the mapper function return a Publisher instead of a value directly. I would personally call it `FlatMap` but I've stuck to ReactiveX's naming (there is a `FlatMap` in ReactiveX, but it behaves in an unintuitive way and its implementation requires an unbounded buffer).


#### Client streaming: Sum

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide an example whereby a client makes calls to multiple backends, with variants:

  1. waits for all responses to come back, and then forms a coalesced response
  2. waits for one response to come back, forwards it, and cancels all other requests

These patterns are incredibly common in real systems, and providing good support for them needs to be a requirement for any API addition.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will update with examples demonstrating these use cases soon.

@per-gron per-gron Sep 6, 2017 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added those examples now. For 1, see RenderShoppingCart. For 2, see CountryByUseridRace.

Notes:

If you don't like Zip returning a tuple and the Splat helper, Zip could be changed so that its last parameter is a function with one parameter per input stream that gets to combine the values (RxJava does this). Personally I can't decide which I prefer.

rs does not currently have variants of Zip and Merge that take a list (of dynamic size) of input streams; they are simply variadic functions. This is not a fundamental limitation, I just haven't written them yet.

It's a very contrived example, but you might be interested in knowing that the pattern of making one request twice and letting them race can be implemented as an operator (I'm cheating with C++14 here but it can be done in C++11 as well with AnyPublisher (with some runtime overhead) or more verbosity):

template <typename Input>
auto TwinRace(const Input &input) {
  return Pipe(
      Merge(input, input),
      Take(1));
}

which could then be used as an operator like any other. With the addition of a sleep/delay operator (not in the current implementation), you could do speculative retries:

template <typename Input>
auto SpeculativeRetry(const Input &input) {
  using namespace std::chrono_literals;
  return Pipe(
      Merge(input, Pipe(input, Delay(100ms)),
      Take(1));
}

Perhaps even better than Take(1) would be to use the race operator but rs doesn't have that currently.


With this I consider this issue resolved, let me know if you disagree.


Alternate approaches for a C++ for gRPC include

* **The current synchronous API:** Too slow for many use cases.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The synchronous API does have some performance issues right now... if these were addressed (they are addressable), would you advocate for a new API?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Without the performance aspect the case for a new API certainly becomes weaker.

Personally I would still prefer this style of API because I like the model of thinking it promotes. It seems like this style of API could be more powerful in terms of expressing concurrency as well but I am not entirely sure.

What do you think?

Comment thread L10-c++-reactive-streams.md Outdated
* [rs](https://github.com/per-gron/shuriken/tree/master/src/rs) breaks Google's code style in the following ways. I am unsure about how exactly to fix this.
* **It uses C++14:** This is mostly for ease of implementation; it should be possible to backport to C++11.
* **It uses rvalue references:** These could mostly be changed to by-value instead, but I have a hard time justifying the performance penalty.
* **It uses `std::exception_ptr`:** Reactive Streams has built in error handling that is based on having a generic type for errors. `std::exception_ptr` is C++'s type for that. rs could use something else, but the chances of being able to convince the Reactive Streams people that rs should become the official C++ version of Reactive Streams is likely low without `std::exception_ptr`. Please note that rs never throws, it uses `std::exception_ptr` only as a container type.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grpc also has a generic container for error handling (grpc::Status).

It's hard to justify introducing a wrinkle that forces conversion.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that. I am not opposed to changing this part of rs.

Perhaps it would be possible to change it so that the error type is configurable via template or something. But if it becomes too complicated I think it could be okay to tie it to gRPC's types.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a bullet point under the "Implementation" section to change rs so that grpc::Status can be used as the error type.

With this I consider this issue resolved, let me know if you disagree.

Comment thread L10-c++-reactive-streams.md Outdated
* There are certain details in the current gRPC C++ API that seem to be missing: For example, the author could not find how get a signal in the server when a unary RPCs is cancelled.
* The rs library is a fairly minimal implementation of Reactive Streams. One thing that many Rx libraries have that rs does not is a concept for a Publisher that emits only one value. This would increase the complexity of the library a bit, but it could be worth it for the additional type safety and performance benefits it would bring.
* [rs](https://github.com/per-gron/shuriken/tree/master/src/rs) breaks Google's code style in the following ways. I am unsure about how exactly to fix this.
* **It uses C++14:** This is mostly for ease of implementation; it should be possible to backport to C++11.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to stay with C++11 for some time.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand. If people actually are interested in this I will definitely consider backporting it. It's just a bit of a pain to do and parts of the implementation would be uglier/harder to read.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a bullet point under the "Implementation" section to backport to C++11.

With this I consider this issue resolved, let me know if you disagree.

The main drawbacks of this proposal are:

* It is easy to use only after you have spent some time learning Reactive Streams' concepts: You must change mindset to thinking about data flows.
* There are features in gRPC that don't obviously fit Reactive Streams, for example metadata.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There needs to be a story for metadata.

@per-gron per-gron Sep 6, 2017 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I will read and think about this more. This might take some time for me to fix however; this is more work than your other comments so far.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth looking at how grpc-java, grpc-objective-c deal with this, as they each have reactive stream allowances in their API's.

@ejona86, @jcanizales care to provide pointers?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have a look. I don't think this will necessarily be a hard problem to solve. I have simply assumed that it should be possible somehow and not thought about it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grpc-java doesn't expose Metadata on the stubs; Metadata is only exposed to interceptors.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ctiller I have read a little bit about metadata and how interceptors work in grpc-java. I can see a few different directions that I could go designing this, and would appreciate advice on which I should try first. We could do more than one or even all of them at once, but for the sake of simplicity and MVP I would prefer trying only one at first if possible. The main directions I can think of are

  1. Expose metadata directly to individual rpc handlers (aka what grpc-java does not do): This is probably the most conceptually lightweight approach and is appropriate if metadata is often used for RPC-specific purposes.
  2. Introduce some kind of Reactive Streams style Interceptor API. This is good for doing middleware-style things such as auth, logging, metrics, tracing etc.
  3. Add an Interceptor API to the async C++ API (perhaps it can even be done strictly as a layer outside of it). I think this could be done without adding too much complexity to the current async API. It might lead to lower level code in middleware than option 2. However, middlewares wouldn't need to depend on if the application uses Reactive Streams or not. It might even be possible to make the same Interceptor interface work with the sync API.

I like option number 3 even though it seems like the most amount of work, because I think middleware writers would benefit from it. What do you think?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Objective-C, request and response metadata are exposed as properties of the "call" object. This call object acts as an Rx-style source of response messages and a sink of request messages.


* Prepare the [rs](https://github.com/per-gron/shuriken/tree/master/src/rs) library so that it can be accepted as gRPC code:
* Backport to C++11.
* Remove usage of `std::exception_ptr` and make it possible to use `grpc::Status` instead.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ctiller you said (starting a new thread because the other one is hidden by the Github interface)

grpc also has a generic container for error handling (grpc::Status). It's hard to justify introducing a wrinkle that forces conversion.

Would you use grpc::Status for non-gRPC errors also, like HTTP errors or Cassandra errors? A strength of Reactive Streams is that it provides a uniform API style. Unless you would advocate for wrapping non-gRPC errors in grpc::Status, I think there might be a good case for wrapping errors, to be able to handle different kinds.

The syntactic overhead does not have to be that large. In the current version of rs, you usually catch errors like this: Catch([](std::exception_ptr error) { ... }). Instead of this, Catch could allow you to specify what you want to catch:

Catch<grpc::Status>([](grpc::Status status) { ... })

That way, you could also catch your CassandraErrors or HttpErrors or whatever, like you would with normal try/catch statements. The only extra code the user has to write is <grpc::Status>. Since the benefit of doing it like this is to not force users to encode non-gRPC errors in grpc::Status, it could be worth it.

This type of try/catch style with type matching could be done using std::exception_ptr but it could also without too much effort be done with an rs wrapper type.

@per-gron

Copy link
Copy Markdown
Author

@a11r what is the next step? Based on how the process is described, it seems like I should be waiting for this RFC to be assigned an APPROVER, no?

@per-gron

per-gron commented Oct 2, 2017

Copy link
Copy Markdown
Author

@a11r @ctiller As I am awaiting a response to the question of what the next step forward is for this RFC, I have been hacking on the prototype implementation. I have fixed the most serious bugs and written a protoc plugin. It is now possible to write real services using the prototype even though it does not support all gRPC features. See for example this test and its .proto file.

At the moment, I am writing some real world-like services to see what it's like and to find and fix the most glaring usability issues.

@ctiller

ctiller commented Oct 2, 2017 via email

Copy link
Copy Markdown
Member

@hsaliak

hsaliak commented Feb 2, 2018

Copy link
Copy Markdown
Contributor

@ctiller /cc @a11r @vjpai what's next for this RFC. Should someone else pick up the review here and make a determination on whether this can be supported?

@thelinuxfoundation

Copy link
Copy Markdown

Thank you for your pull request. Before we can look at your contribution, we need to ensure all contributors are covered by a Contributor License Agreement.

After the following items are addressed, please respond with a new comment here, and the automated system will re-verify.

Regards,
CLA GitHub bot

@per-gron

per-gron commented Feb 7, 2018

Copy link
Copy Markdown
Author

The CLA should be sorted now

@per-gron

per-gron commented Jul 3, 2018

Copy link
Copy Markdown
Author

Closing because I think this proposal maybe adds a bit too much conceptual overhead.

@per-gron per-gron closed this Jul 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants