-
Notifications
You must be signed in to change notification settings - Fork 5.5k
http_filter: add addEncodedTrailers and addDecodedTrailers #3980
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
Changes from 4 commits
34bd3d1
688bc4d
e0b74f3
acfa3d8
baee8aa
42443b0
52c5780
8495e41
36103a1
f6ca600
483b48b
5412bb8
cd25726
c3a900b
90c6233
3667d6a
7637fa3
f2b6210
815a07b
2321031
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -190,6 +190,11 @@ class StreamDecoderFilterCallbacks : public virtual StreamFilterCallbacks { | |
| */ | ||
| virtual void addDecodedData(Buffer::Instance& data, bool streaming_filter) PURE; | ||
|
|
||
| /** | ||
| * Adds decoded trailers. This will overwrite any existing trailers should any already exist. | ||
| */ | ||
| virtual void addDecodedTrailers(HeaderMapPtr&& trailers) PURE; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still -1 to moving a pointer here, and prefer the first revision:
@mattklein123 thoughts?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
IMO move semantics make it clear that the pointer is now owned by the callee. We do this in a variety of places.
I think this is a better argument. Assuming with the new semantics we have discussed it's just as easy to return a reference to either the existing trailers or a newly created trailers, it sounds reasonable to me to switch it back to what we had before.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The one thing that's unclear to me is what would
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I would throw some type of bad argument exception that will cause Envoy to crash and can be checked in tests. (I would probably not derive from EnvoyException and pick a std exception for this).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this out of date? With the code as it stands now, I think we can only call under decodeData with end_stream = true, and subsequent filters will never encounter that state (end stream will be false for future invocations) so should not be able to overwrite trailers. I do think we should document when this is safe to call but if you prefer a TODO while the design is being hashed out that's fine :-) |
||
|
|
||
| /** | ||
| * Create a locally generated response using the provided response_code and body_text parameters. | ||
| * If the request was a gRPC request the local reply will be encoded as a gRPC response with a 200 | ||
|
|
@@ -395,6 +400,11 @@ class StreamEncoderFilterCallbacks : public virtual StreamFilterCallbacks { | |
| */ | ||
| virtual void addEncodedData(Buffer::Instance& data, bool streaming_filter) PURE; | ||
|
|
||
| /** | ||
| * Adds encoded trailers. This will overwrite any existing trailers should any already exist. | ||
| */ | ||
| virtual void addEncodedTrailers(HeaderMapPtr&& trailers) PURE; | ||
|
|
||
| /** | ||
| * Called when an encoder filter goes over its high watermark. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -775,15 +775,36 @@ void ConnectionManagerImpl::ActiveStream::decodeData(ActiveStreamDecoderFilter* | |
|
|
||
| for (; entry != decoder_filters_.end(); entry++) { | ||
| ASSERT(!(state_.filter_call_state_ & FilterCallState::DecodeData)); | ||
|
|
||
| bool end_stream_no_trailers = end_stream && !request_trailers_; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mind adding a comment that we check the state of end stream inside the loop in case a filter adds trailers? |
||
| if (end_stream_no_trailers) { | ||
| state_.filter_call_state_ |= FilterCallState::LastDataFrame; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd argue changing HCM state probably bumps this up to a medium-risk change.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does HCM stand for in this context?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Http Connection Manager (HCM) |
||
| state_.filter_call_state_ |= FilterCallState::DecodeData; | ||
| FilterDataStatus status = (*entry)->handle_->decodeData(data, end_stream); | ||
| FilterDataStatus status = (*entry)->handle_->decodeData(data, end_stream_no_trailers); | ||
| state_.filter_call_state_ &= ~FilterCallState::DecodeData; | ||
| if (end_stream_no_trailers) { | ||
| state_.filter_call_state_ &= ~FilterCallState::LastDataFrame; | ||
| } | ||
| ENVOY_STREAM_LOG(trace, "decode data called: filter={} status={}", *this, | ||
| static_cast<const void*>((*entry).get()), static_cast<uint64_t>(status)); | ||
| if (!(*entry)->commonHandleAfterDataCallback(status, data, state_.decoder_filters_streaming_)) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (end_stream && request_trailers_) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again let's comment this is explicitly for the case that the remote side didn't send trailers but a filter added them. |
||
| decodeTrailers(filter, *request_trailers_); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we need to actually only send trailers to the filter that set the trailer and filters after it? I think we need to actually track which filter added the trailers... (See how this is handled in the headers/data case). |
||
| } | ||
| } | ||
|
|
||
| void ConnectionManagerImpl::ActiveStream::addDecodedTrailers(HeaderMapPtr&& trailers) { | ||
| if (state_.filter_call_state_ & FilterCallState::LastDataFrame) { | ||
| request_trailers_ = std::move(trailers); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm correct above that we can only set request_trailers_once now, can we also have an invariant ASSERT that they're empty when this is called? |
||
| } else { | ||
| // do nothing | ||
| // todo: should this throw/assert? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should :-) I also think we should have integration tests for both the happy path and over-enthusiastic filters. I think we can start on those now, because even if the APIs change a bit the end to end tests should stay the same
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 same as my comment elsewhere. We should be doing this in a bunch of places (see some other TODOs from me) so we might as well start doing it now w/ tests. |
||
| } | ||
| } | ||
|
|
||
| void ConnectionManagerImpl::ActiveStream::addDecodedData(ActiveStreamDecoderFilter& filter, | ||
|
|
@@ -1058,6 +1079,16 @@ void ConnectionManagerImpl::ActiveStream::encodeHeaders(ActiveStreamEncoderFilte | |
| } | ||
| } | ||
|
|
||
| void ConnectionManagerImpl::ActiveStream::addEncodedTrailers(HeaderMapPtr&& trailers) { | ||
| if (state_.filter_call_state_ & FilterCallState::LastDataFrame) { | ||
| response_trailers_ = std::move(trailers); | ||
| state_.local_complete_ = false; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was necessary because
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's worth a comment in the PR it's worth a comment in the code :-) |
||
| } else { | ||
| // do nothing | ||
| // todo: should this throw/assert? | ||
| } | ||
| } | ||
|
|
||
| void ConnectionManagerImpl::ActiveStream::addEncodedData(ActiveStreamEncoderFilter& filter, | ||
| Buffer::Instance& data, bool streaming) { | ||
| if (state_.filter_call_state_ == 0 || | ||
|
|
@@ -1085,9 +1116,17 @@ void ConnectionManagerImpl::ActiveStream::encodeData(ActiveStreamEncoderFilter* | |
| std::list<ActiveStreamEncoderFilterPtr>::iterator entry = commonEncodePrefix(filter, end_stream); | ||
| for (; entry != encoder_filters_.end(); entry++) { | ||
| ASSERT(!(state_.filter_call_state_ & FilterCallState::EncodeData)); | ||
|
|
||
| bool end_stream_no_trailers = end_stream && !response_trailers_; | ||
| state_.filter_call_state_ |= FilterCallState::EncodeData; | ||
| FilterDataStatus status = (*entry)->handle_->encodeData(data, end_stream); | ||
| if (end_stream_no_trailers) { | ||
| state_.filter_call_state_ |= FilterCallState::LastDataFrame; | ||
| } | ||
| FilterDataStatus status = (*entry)->handle_->encodeData(data, end_stream_no_trailers); | ||
| state_.filter_call_state_ &= ~FilterCallState::EncodeData; | ||
| if (end_stream_no_trailers) { | ||
| state_.filter_call_state_ &= ~FilterCallState::LastDataFrame; | ||
| } | ||
| ENVOY_STREAM_LOG(trace, "encode data called: filter={} status={}", *this, | ||
| static_cast<const void*>((*entry).get()), static_cast<uint64_t>(status)); | ||
| if (!(*entry)->commonHandleAfterDataCallback(status, data, state_.encoder_filters_streaming_)) { | ||
|
|
@@ -1099,8 +1138,13 @@ void ConnectionManagerImpl::ActiveStream::encodeData(ActiveStreamEncoderFilter* | |
| end_stream); | ||
|
|
||
| request_info_.addBytesSent(data.length()); | ||
| response_encoder_->encodeData(data, end_stream); | ||
| maybeEndEncode(end_stream); | ||
| if (end_stream && response_trailers_) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little confused about the semantics here. When is a filter expected to add trailers for the first time? Do we want to support setting trailers during the final data frame where end_stream was otherwise true? I don't think we want to support that and we probably do? Also where is the parallel to this code on the decoding side?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens right now (since you can override trailers whenever)
3 is definitely confusing, so I'd be happy to prevent that. The question to me that remains is whether we want trailers to be written out at the end of What steered me away from that originally was that was that it doesn't seem like So after thinking about it some, how about this: This does mean we get both a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think this is more on the right track.
I don't think this is going to work as it will completely confuse filters that do things when end_stream is true. I think this is the behavior you want:
I don't think this should be too hard to implement and should cleanly cover both encode/decode. Does that make sense? For the error checking side of things, IMO I would probably just block trailers being added in any context other than decodeData(..., true). Does it make sense anywhere else? The otehr thing to consider is what if someone calls this when a filter has been paused and then calls continue? Will it work correctly?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that makes sense for the most part. One question:
Do you mean
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No I meant what I said, that I think the only valid context to add trailers should be within a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, that makes perfect sense. Thanks for clarifying. |
||
| response_encoder_->encodeData(data, false); | ||
| encodeTrailers(filter, *response_trailers_); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here about keeping track of which filter added the trailers? As an aside, I'm a little confused as to why the logic in encodeHeaders() does not mirror decodeHeaders() WRT to handling continue/last filter. (And yes I know I wrote this code!) I can look into this more tomorrow or Friday, but if you feel like taking a look that would be appreciated.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked into it a little bit: adding the same last filter check to encodHeaders causes a test failure in which presumably comes from hitting this code more than once (towards the end of The test that's failing is Unclear whether this is why we don't special case the last filter in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, thanks for looking. I can take a look tomorrow or this weekend more. I wouldn't worry about it for now but it seemed strange to me that it wasn't symmetrical and I couldn't remember why. |
||
| } else { | ||
| response_encoder_->encodeData(data, end_stream && !response_trailers_); | ||
| maybeEndEncode(end_stream); | ||
| } | ||
| } | ||
|
|
||
| void ConnectionManagerImpl::ActiveStream::encodeTrailers(ActiveStreamEncoderFilter* filter, | ||
|
|
@@ -1380,6 +1424,10 @@ Buffer::WatermarkBufferPtr ConnectionManagerImpl::ActiveStreamDecoderFilter::cre | |
| return buffer; | ||
| } | ||
|
|
||
| void ConnectionManagerImpl::ActiveStreamDecoderFilter::addDecodedTrailers(HeaderMapPtr&& trailers) { | ||
| parent_.addDecodedTrailers(std::move(trailers)); | ||
| } | ||
|
|
||
| void ConnectionManagerImpl::ActiveStreamDecoderFilter::addDecodedData(Buffer::Instance& data, | ||
| bool streaming) { | ||
| parent_.addDecodedData(*this, data, streaming); | ||
|
|
@@ -1473,6 +1521,10 @@ void ConnectionManagerImpl::ActiveStreamEncoderFilter::addEncodedData(Buffer::In | |
| return parent_.addEncodedData(*this, data, streaming); | ||
| } | ||
|
|
||
| void ConnectionManagerImpl::ActiveStreamEncoderFilter::addEncodedTrailers(HeaderMapPtr&& trailers) { | ||
| parent_.addEncodedTrailers(std::move(trailers)); | ||
| } | ||
|
|
||
| void ConnectionManagerImpl::ActiveStreamEncoderFilter:: | ||
| onEncoderFilterAboveWriteBufferHighWatermark() { | ||
| ENVOY_STREAM_LOG(debug, "Disabling upstream stream due to filter callbacks.", parent_); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -162,6 +162,7 @@ class ConnectionManagerImpl : Logger::Loggable<Logger::Id::http>, | |
|
|
||
| // Http::StreamDecoderFilterCallbacks | ||
| void addDecodedData(Buffer::Instance& data, bool streaming) override; | ||
| void addDecodedTrailers(HeaderMapPtr&& trailers) override; | ||
| void continueDecoding() override; | ||
| const Buffer::Instance* decodingBuffer() override { | ||
| return parent_.buffered_request_data_.get(); | ||
|
|
@@ -229,6 +230,7 @@ class ConnectionManagerImpl : Logger::Loggable<Logger::Id::http>, | |
|
|
||
| // Http::StreamEncoderFilterCallbacks | ||
| void addEncodedData(Buffer::Instance& data, bool streaming) override; | ||
| void addEncodedTrailers(HeaderMapPtr&& trailers) override; | ||
| void onEncoderFilterAboveWriteBufferHighWatermark() override; | ||
| void onEncoderFilterBelowWriteBufferLowWatermark() override; | ||
| void setEncoderBufferLimit(uint32_t limit) override { parent_.setBufferLimit(limit); } | ||
|
|
@@ -267,11 +269,13 @@ class ConnectionManagerImpl : Logger::Loggable<Logger::Id::http>, | |
| commonEncodePrefix(ActiveStreamEncoderFilter* filter, bool end_stream); | ||
| const Network::Connection* connection(); | ||
| void addDecodedData(ActiveStreamDecoderFilter& filter, Buffer::Instance& data, bool streaming); | ||
| void addDecodedTrailers(HeaderMapPtr&& trailers); | ||
| void decodeHeaders(ActiveStreamDecoderFilter* filter, HeaderMap& headers, bool end_stream); | ||
| void decodeData(ActiveStreamDecoderFilter* filter, Buffer::Instance& data, bool end_stream); | ||
| void decodeTrailers(ActiveStreamDecoderFilter* filter, HeaderMap& trailers); | ||
| void maybeEndDecode(bool end_stream); | ||
| void addEncodedData(ActiveStreamEncoderFilter& filter, Buffer::Instance& data, bool streaming); | ||
| void addEncodedTrailers(HeaderMapPtr&& trailers); | ||
| void sendLocalReply(bool is_grpc_request, Code code, const std::string& body, | ||
| std::function<void(HeaderMap& headers)> modify_headers); | ||
| void encode100ContinueHeaders(ActiveStreamEncoderFilter* filter, HeaderMap& headers); | ||
|
|
@@ -339,6 +343,9 @@ class ConnectionManagerImpl : Logger::Loggable<Logger::Id::http>, | |
| // to verify we do not encode100Continue headers more than once per | ||
| // filter. | ||
| static constexpr uint32_t Encode100ContinueHeaders = 0x40; | ||
| // Used to indicate that we're processing the final [en|de]Code frame, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| // i.e. end_stream = true | ||
| static constexpr uint32_t LastDataFrame = 0x80; | ||
| }; | ||
| // clang-format on | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I should have called this out before, but please doc the return as @return here and below