Skip to content

WIP: Convert dynamic metadata to Filter state - #4873

Closed
rshriram wants to merge 3 commits into
envoyproxy:masterfrom
rshriram:metadata_in_fs
Closed

rshriram wants to merge 3 commits into
envoyproxy:masterfrom
rshriram:metadata_in_fs

Conversation

@rshriram

Copy link
Copy Markdown
Member

Since dynamic metadata is being used everywhere, there is a non trivial amount of code that needs to be rewired to use the FilterState abstractions. Existing code also relies on directly accessing the map inside the Metadata, something that FilterState does not offer.

So, as a temporary measure, I have re-routed all dynamic metadata calls to a well known mutable FilterState object (envoy.streaminfo.dynamicmetadata). Note that state inside this filter state object is still namespaced per usual conventions (reverse dns filter name, etc.). This redirection preserves the existing setDynamicMetadata(), and dynamicMetadata() interfaces, while still using the unified state management abstractions (aka filter state).

Thoughts @mattklein123 / @htuch before making this code compile?

Signed-off-by: Shriram Rajagopalan [email protected]

Signed-off-by: Shriram Rajagopalan <[email protected]>
namespace StreamInfo {

// Mutable filter state wrapping metadata object. State within this
// metadata should still be namespaced per usual reverse dns naming

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.

What's the tradeoff here between having the namespacing at the FilterState level and just embedding a Struct here? Shouldn't it be possible to still preserve the existing semantics this way?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Existing code also relies on directly accessing the map inside the Metadata, something that FilterState does not offer. All of this can be fixed, but its a lot of work across the code. Redirecting dynamic metadata calls to the state stored in filter state offers a compromise in that we still get to store this in a quarantined manner

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.

I'm less concerned about the churn to existing code as with getting the right long term abstraction in place. I do realize though that it's a bit unfair to make this statement without skin in the game, as I'm not doing the refactoring work. Would be interested in hearing from other folks like this, such as @mattklein123.

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.

Yeah tough call. What about one of these two options:

  1. Mark dynamicMetadata() as deprecated and subject to removal in a future version. No new code should use it.
  2. More aggressively, change the name to deprecatedDynamicMetadata() to really make sure people understand they shouldn't be using it? This would require some find/replace across the code and some trivial breakage to third party filters, but with some comments on the deprecated functions on how people should migrate away I think this would be a good thing? We could also open issues on all the existing call sites within the repo to be cleaned up as tech debt?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

That sounds good by me. That said, I would still propose to keep the well known FilterState called envoy.streaminfo.dynamicMetadata around. Allows people to pile stuff into envoy.streaminfo.dynamicmetadata filter State, where they still have to use soem well defined namespaces to put things in. At the same time, it allows consumers to inspect whats been stored in this dynamic filter state (map semantics), without needing to add all those interfaces to FilterState.

From that point of view, would it make sense to rename this to wellKnownDynamicFilterState() ? Its still the same find/replace operation with no tech debt

@htuch htuch Oct 29, 2018 •

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.

OK, I just chatted with @mattklein123 offline. I'm happy with a combination of #4873 (comment), adding a well known envoy.streaminfo.dynamicmetadata for those who really want it and a personal request to provide some well-throught out guidance on the pros and cons of using a global grab bag vs. namespaced data. I'm happy to sign-off if we have all three.

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.

I just talked to @htuch offline and I agree we should deprecate it, and file follow up tech debt issues to fix both access logging and Lua to specify both a namespace and item when access the struct.

Beyond this, if people want to use a grab bag in a well known namespace that is up to them.

Is that fair?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What to do about all the users of metadata match thats is a common user of the metadata struct obtained from Dynamic metadata? Provide it with full access to the internal state in the map? Even then, its going to be full of exception catch code if the FilterState doesn't match the Struct object 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.

I think for this case we can avoid exception catch by allowing code to test to see if the map contains a struct type.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, but I am not sure Randy and co would be happy with that given that they wanted super strict semantics on how data can be accessed across filters

@mattklein123 mattklein123 self-assigned this Oct 28, 2018
* @return a map of data names and their corresponding objects.
*/
const std::map<std::string, std::unique_ptr<FilterObject> getRawState() const PURE;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

So in the absence of a well known namespace, given that we have things like MetadataMatcher etc that rely on iterating through the map, I hope this interface is okay. If this is too much of a blasphemy for the filter state's strict write-once semantics, then I can split the write-many store out into a separate map inside filter state and return that map directly.

@rshriram

Copy link
Copy Markdown
Member Author

Having had some time to think about this, the root of the problem is the introduction of this internal typed filterState::Object, which prevents things like dumping the entire filter state on the wire, or allowing end users to abstractions like metadata matcher to match on the filter state.

What we need is namespaced filter state wrapping generic Structs so that we can do things like #4862 (that the Google folks themselves wanted), as well as metadata matchers in RBAC or other places. Something like #4862 is exactly what I am looking for in Istio as well. these are properties that dynamic metadata supports today. It has nothing to do with a global dumping ground, nor does dynamic metadata allow such a thing anyway.

If we can preserve these two properties (ability to dump the entire state on the wire and allowing end users to match on the state - which requires a flexible object, aka (sadly) struct -- then I am happy. There are two ways to go about this:

  1. shoehorn dynamic metadata semantics into FilterState: wrap Struct into FilterState Object, have some kludge that forces all consumers to iterate over filter state, type check for struct, and store struct objects into a new map at every call site
  1. Create a separate keyspace or a core::metadata within FilterState for holding the Structs, (map<string, Struct>) that allows consumers to dump the entire state on the wire, or use metadata matchers without any unnecessary boiler plate.

@rshriram

Copy link
Copy Markdown
Member Author

Either way, I would like some resolution asap as I have been on this for over a month, but still made no progress on the actual internal use case that requires access to all the info generated by all filters.

@htuch

htuch commented Oct 30, 2018

Copy link
Copy Markdown
Member

@rshriram can't we have some kind of Object subclass interface that supports the iteration properties you want? I don't think we need to do lots of try/catches either, we can do something smarter with RTTI.

@rshriram

Copy link
Copy Markdown
Member Author

I am not the C++ expert here. So I leave those bits to you guys. But here are example snippets of code that need to be supported:

access_log:

if (stream_info.dynamicMetadata().filter_metadata_size() > 0) {
   common_properties->mutable_metadata()->MergeFrom(stream_info.dynamicMetadata());
   // Gets all dynamic state and adds it to the access log message which has a metadata field
 }

(https://github.com/envoyproxy/envoy/blob/master/api/envoy/data/accesslog/v2/accesslog.proto#L133)

metadata matchers:
https://github.com/envoyproxy/envoy/blob/master/source/common/config/metadata.cc#L6

The entire set of functions here take envoy::api::core::metadata as argument. We could make it such that they take FilterState also as argument, and repeat the logic in metadataValue() in some other function..

I think the key is being able to get the entire dynamic state map (string, structs) and dumping it on the wire as is. I dont know how to achieve this with a map of custom types (filter state object or subtypes) without adding a lot of intermediaries, and custom iterators, on the fly map creations, etc.

@htuch

htuch commented Oct 30, 2018

Copy link
Copy Markdown
Member

@rshriram if you really are wanting to support existing Struct semantics with little change, then you are forced to embed a Struct in FilterState. Then you just want to iterate over it and do a dynamic_cast to determine which entries can be treated as google.protobuf.Struct.

If you want to be a bit more abstract, you could create an class IterablePropertyObject : public FilterState:Object which offered a C++ iterator interface with the ability to iterate over key-value properties or the like.

@rshriram

Copy link
Copy Markdown
Member Author

to clarify, streamInfo.dynamicMetadata returns envoy.api.v2.core.metadata which is a map<string, Struct> and wrapped into a Metadata message.

The fact that both sides are aware of this well defined proto makes it alluring when you want to dump the filter state on the wire. This has little to do with Struct, and more to do with protos, and the map semantics. Neither of which are offered by FilterState today (it requires typed C++ objects, and doesn't allow grabbing the entire map of <string, Struct> to be dumped on the wire).

@mattklein123

Copy link
Copy Markdown
Member

@rshriram @htuch what if we have a read-only iterator type function which will iterate through filter filter state map and return objects that are of a specific type using RTTI?

Something like this:

template<typename T>
iterate(std::function<void(std::string, const T&); 

Where the callback is called with each namespace that has an object of a specific type? Then I think everything else could be implemented on top of this from an iteration/matching perspective?

@rshriram

Copy link
Copy Markdown
Member Author

This was exactly my point. What is the trade off here? in the name of efficiency for a select set of internal use cases (needing to access typed filter state object), we are forcing all others (access logs, metadata matchers, etc.) to create maps on the fly, and populate them with strings, and structs so that they can serialize them on the wire. We have documented use of the map<string, Struct> all over envoy code today (from grpc acces log, rbac, header to metadata, etc..). All of these will suffer. Do we have documented use case of the need for this typed thing, that would other wise cause a bigger performance impact? (the only thing using immutable filter state is sni_cluster and some request_info filters).

@rshriram

Copy link
Copy Markdown
Member Author

Also keep in mind that metadataMatcher is used by other parts of envoy (ones dealing with statically defined metadata), and not just rbac which uses dynamic metadata. So, if we are to use that code as is, then we have to collect and create a metadata proto object before using metadata matcher. Or change the function signatures and impose this tax on all other users of metadata. This was also my point earlier -- as to how strongly do we feel about needing to ONLY use typed filter objects (not protos) and the code/perf overhead imposed on everybody else. If we separate the filter state into two maps, then we can allow the second map to be map<string, struct> and maintain the existing semantics.

@mattklein123

Copy link
Copy Markdown
Member

@rshriram why would it suffer? My point is we can offer the existing interface on top of the new interface without any major change to callers who want the map<string, struct> paradigm?

@rshriram

Copy link
Copy Markdown
Member Author

You would have to allocate the map on the fly right?

How would code like this get transformed? dynamicMetadata returns map<string, struct> (pre-allocated)

if (stream_info.dynamicMetadata().filter_metadata_size() > 0) {
   common_properties->mutable_metadata()->MergeFrom(stream_info.dynamicMetadata());
   // Gets all dynamic state and adds it to the access log message which has a metadata field
 }```

@htuch

htuch commented Oct 30, 2018

Copy link
Copy Markdown
Member

@rshriram it would be an iterator, returning references as it moves over the FilterState. In terms of whether you want to build an actual map, do we actually need that? It seems in the use cases you describe, iteration or path lookup would suffice.

@rshriram

rshriram commented Oct 30, 2018 •

Copy link
Copy Markdown
Member Author

https://github.com/envoyproxy/envoy/blob/master/source/common/config/metadata.cc#L6 is used by https://github.com/envoyproxy/envoy/blob/master/source/extensions/filters/common/rbac/engine_impl.cc#L20 called by https://github.com/envoyproxy/envoy/blob/master/source/extensions/filters/http/rbac/rbac_filter.cc#L100 which requires a metadata Object (map<string, struct>) where the query is determined by user input.

and then this:

if (stream_info.dynamicMetadata().filter_metadata_size() > 0) {
   common_properties->mutable_metadata()->MergeFrom(stream_info.dynamicMetadata());
   // Gets all dynamic state and adds it to the access log message which has a metadata field
 }

this can be fixed through the iterator but the former is not.

@htuch

htuch commented Oct 30, 2018

Copy link
Copy Markdown
Member

@rshriram yes, if you want to preserve the exact current implementation, you'll need to use Metadata. However, if you're willing to make some changes, I don't see how RBAC can be doing anything inside matches beyond effectively querying path properties (i.e. doing a lookup and comparing the result in the tree) or iterating. These can all be expressed abstractly in an interface and with an iterator.

@mattklein123

Copy link
Copy Markdown
Member

+1 to what @htuch is saying, but TBH at this point I think we are splitting hairs here and it doesn't really matter.

@rshriram

Copy link
Copy Markdown
Member Author

Metadata matcher is being used by other parts of envoy, not just RBAC. RBAC is simply piggy backing on it. So, yes its what I stated earlier. We duplicate the entire metadata match logic but without the metadata proto stuff, to decouple RBAC from metadata.

This seems like way too much work to preserve the typed C++ object stuff in filter state. I am going to have to close this PR as its more work than I originally estimated. I am happy to give pointers to someone interested in rewriting the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants