Skip to content

stateful session: new envelope stateful session extension - #39004

Merged
wbpcode merged 7 commits into
envoyproxy:mainfrom
wbpcode:dev-stateful-session-enhancement
May 12, 2025
Merged

wbpcode merged 7 commits into
envoyproxy:mainfrom
wbpcode:dev-stateful-session-enhancement

Conversation

@wbpcode

@wbpcode wbpcode commented Apr 3, 2025 •

Copy link
Copy Markdown
Member

Commit Message: stateful session: new packages mode for header based session
Additional Description:

To close #38983

This mode is useful when the server will initialize the session and session id at initial request, the client will send the id back without modification at following requests.

More specifically, when the initial request is sent, the request doesn't contain session header. And the selected server will add a session-id in the response. And at all following requests in this same session, the request will carry this header.

Then, envoy will works in following way:

sequenceDiagram
  client ->> envoy: no session-id
  envoy ->> server: no session-id
  server ->> envoy: session-id "xxx"
  envoy ->> client: session-id "1.2.3.4:80#59;origin:xxx"
  client ->> envoy: session-id "1.2.3.4:80#59;origin:xxx"
  envoy ->> server: session-id "xxx"
Loading

Note, this PR only cover the MCP 250326 spec. Because in the MCP 241105 spec, the session id generated by the server will be send back in the initial endpoint sse event. That means if we want to support MCP 241105, then we need to decode response body to append additional info to the session is and is a little more complex.

But this principle is same. If MCP 241105 spec is necessary, you can create a simple Lua script to do it. You can decode the initial sse event at the envoy_on_response and update the session id. And extract the target host and original session id at the envoy_on_request.

Risk Level: low.
Testing: unit, integration.
Docs Changes: added.
Release Notes: added.
Platform Specific Features: n/a.

@repokitteh-read-only

Copy link
Copy Markdown

CC @envoyproxy/api-shepherds: Your approval is needed for changes made to (api/envoy/|docs/root/api-docs/).
envoyproxy/api-shepherds assignee is @markdroth
CC @envoyproxy/api-watchers: FYI only for changes made to (api/envoy/|docs/root/api-docs/).

🐱

Caused by: #39004 was opened by wbpcode.

see: more, trace.

@wbpcode wbpcode assigned cpakulski and unassigned markdroth Apr 3, 2025

@cpakulski cpakulski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think I understand what you are trying to achieve here, but would you mind adding some examples to the PR description? What the client sends, what is received by the upstream server and what Envoy sends back to the client? Thanks!

@wbpcode

wbpcode commented Apr 4, 2025

Copy link
Copy Markdown
Member Author

@cpakulski thanks for the review. I will add a example for this. But if you get whatn I want to do now,would you mind also update the comments to see which comments are unnecessary to respond. Thanks.

@zhxie zhxie left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It would be pretty useful for cascaded stateful session scenario, and some minor questions here.

@wbpcode

wbpcode commented Apr 4, 2025 •

Copy link
Copy Markdown
Member Author

Hi, @adisuissa ,could you also take a look at this PR as api-shepherds, maintainer and the code owner of stateful session extension when you get some free time?

@wbpcode

wbpcode commented Apr 4, 2025

Copy link
Copy Markdown
Member Author

/retest

@adisuissa adisuissa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!
I've added some high-level comments to clarify the goal.

@wbpcode

wbpcode commented Apr 7, 2025

Copy link
Copy Markdown
Member Author

/retest

@cpakulski cpakulski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for adding diagram.

So, it seems that this is not really MCP specific thing. Currently, header based statefulness works only for a single hop. This mechanism allows to specify statefulness across 1+ hops, but can also be used to completely change the meaning of the header, from desired IP to session-id.

@adisuissa

Copy link
Copy Markdown
Contributor

Thanks for adding diagram.

So, it seems that this is not really MCP specific thing. Currently, header based statefulness works only for a single hop. This mechanism allows to specify statefulness across 1+ hops, but can also be used to completely change the meaning of the header, from desired IP to session-id.

I also think it is not an MCP specific seesion affinity issue, but more a backend-initiated-session case. If I'm missing anything let me know.
IMHO it would be good to frame the problem that this feature is trying to address using the same concepts.

@wbpcode

wbpcode commented Apr 7, 2025 •

Copy link
Copy Markdown
Member Author

cc @adisuissa @cpakulski

Yeah. it is inspired by the MCP and one of the core targets of this PR is to cover the requirements of MCP.

But it also could cover requirements of any protocol or scenarios that the same assumptions: the server will initialize the session and session id at initial request, the client will send the id back without modification at following requests.

This is why I didn't call it MCP_MODE or something, because it's supersets of MCP. I thinks this point is described clearly in the PR's description. In all related contexts, the MCP is only an example protocol that the new mode could covers.

@wbpcode

wbpcode commented Apr 8, 2025

Copy link
Copy Markdown
Member Author

By the way, the whole stateful session filter/feature could be treated as an extension for backend-initiated-session.
So, we always in this context.

@botengyao

botengyao commented Apr 9, 2025 •

Copy link
Copy Markdown
Member

This is why I didn't call it MCP_MODE or something, because it's supersets of MCP. I thinks this point is described clearly in the PR's description. In all related contexts, the MCP is only an example protocol that the new mode could covers.
By the way, the whole stateful session filter/feature could be treated as an extension for backend-initiated-session.
So, we always in this context.

As @adisuissa mentioned, in backend-initiated-session, it looks to me it is more complicated than the MCP case since it is using Set-Cookie header at first for the negotiation and then use the Cookie to do the session affinity. I don't think we need to cover this case in this PR, but providing some extensibility would be great in any possibility.

@yanavlasov yanavlasov self-assigned this Apr 15, 2025
//
// Only one of ``override_mode_config`` or ``packages_mode_config`` should be set. This is the
// default if no any mode config is set.
OverrideModeConfig override_mode_config = 2;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

style-nit: consider having both modes in a Mode message (similar to oneof, but without a oneof). IMO it makes it a bit more readable, and the penalty isn't large.

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.

SGTM.

// session-header: "MS4yLjMuNDo4MDtvcmlnaW46eHh4eHh4Cg==" # base64("1.2.3.4:80;origin:xxxxxx")
//
// The ``origin`` part is used to store the original header value of ``name`` field specified
// header. And the ``origin`` part should always be last part of the new header value.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the ordering requirement is unnecessary, and complicates the interfaces. Other than simplifying the code, what's the motivation for keeping it the last element? The main reason is that adding ";cluster" or any other future component may change the ordering.
Moreover, it might be that a concatenation of 2 "origins" (e.g., 1.2.3.4:80;origin:abc;origin:xyz in case the upstream set abc;origin:xyz as the value of the header), may cause some ambiguity.
IT may be possible to use base64 again, and use a delimiter which will not be part of the value.

@wbpcode wbpcode Apr 16, 2025 •

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.

keepping the origin at the last part is an easy way to ensure it could be safely to packages that multiple times by multiple envoy proxy.

for example: given a request with header value abc;origin:xyz;origin:xxx, the first envoy will split it to abc and xyz;origin:xxx and the second envoy will get xyz;origin:xxx from first envoy and split it to xyz and xxx.
And by this way we needn't to base64 the value from upstream again and again. This will simplify the debugging and mock test.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure I fully agree, and I think the current format adds a constraint on the allowed contents of the header value.
Say that the stateful-session-header name is ssheader, and that the upstream sends back the following ssheader value: ssheader:abc;origin:xyz (i.e., ssheader = abc;origin:xyz, which is a valid header value AFAIK).
This will be sent back to the downstream with the endpoint's address as follows 1.2.3.4:80;origin:ssheader:abc;origin:xyz. Now when the stateful-session filter parses this value, it will take the last part and forward the value of xyz.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I may be missing something, but was this addressed, or was there any followup to this comment?

}

// The packages mode config for the header based session state extension.
message PackagesModeConfig {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Brainstorming:
I wonder if there could be a better name than Packages mode, which is not so intuitive to me.
The closest thing I can think of is Matryoshka, but I wouldn't name it this way.
Things I can think of are related to Wrapper, Capsule (encapsulation), and Envelope.
WDYT?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1. Other options suggested before were: CASCADE or EXPAND.

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.

I have no strong point on the naming, I think Envelope sgtm.

@wbpcode

wbpcode commented Apr 16, 2025

Copy link
Copy Markdown
Member Author

/retest

4 similar comments
@wbpcode

wbpcode commented Apr 16, 2025

Copy link
Copy Markdown
Member Author

/retest

@wbpcode

wbpcode commented Apr 16, 2025

Copy link
Copy Markdown
Member Author

/retest

@wbpcode

wbpcode commented Apr 17, 2025

Copy link
Copy Markdown
Member Author

/retest

@wbpcode

wbpcode commented Apr 17, 2025

Copy link
Copy Markdown
Member Author

/retest

@wbpcode

wbpcode commented Apr 22, 2025

Copy link
Copy Markdown
Member Author

/retest

@botengyao botengyao left a comment

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.

Thanks @wbpcode for adding this support, and put some thoughts to understand the high level usage of this feature.

@wbpcode

wbpcode commented Apr 30, 2025

Copy link
Copy Markdown
Member Author

/retest

@yanavlasov yanavlasov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

/wait-any

TEST(HeaderBasedSessionStateFactoryTest, SessionStateTestInEnvelopeMode) {
{
HeaderBasedSessionStateProto config;
config.set_name("session-header");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would it make more sense to make this feature into its own stateful session extension, rather than trying to cram new behavior into the existing header based extension? Server initiated stateful sessions are sufficiently different to warrant its own extension.

@wbpcode
wbpcode force-pushed the dev-stateful-session-enhancement branch from 9d1e3ca to 80e60e5 Compare May 7, 2025 11:48
@wbpcode wbpcode changed the title stateful session: new envelope mode for header based session stateful session: new envelope stateful session extension May 7, 2025
@wbpcode

wbpcode commented May 7, 2025 •

Copy link
Copy Markdown
Member Author

Force push becase i refactored everything.

@adisuissa adisuissa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

wbpcode and others added 4 commits May 8, 2025 09:54
Signed-off-by: wangbaiping(wbpcode) <[email protected]>
Signed-off-by: wangbaiping(wbpcode) <[email protected]>

@adisuissa adisuissa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!
Left a couple of minor comments, but otherwise LGTM.
/lgtm api

@repokitteh-read-only repokitteh-read-only Bot removed the api label May 8, 2025
@wbpcode

wbpcode commented May 9, 2025

Copy link
Copy Markdown
Member Author

/retest

1 similar comment
@wbpcode

wbpcode commented May 9, 2025

Copy link
Copy Markdown
Member Author

/retest

@adisuissa adisuissa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks!

@wbpcode
wbpcode merged commit eb0aa19 into envoyproxy:main May 12, 2025
@wbpcode
wbpcode deleted the dev-stateful-session-enhancement branch May 12, 2025 17:19
fishpan1209 pushed a commit to fishpan1209/envoy that referenced this pull request May 22, 2025
jue-yin added a commit to jue-yin/envoy that referenced this pull request Aug 24, 2025
…eful session envelope, aiming to cover MCP 250326 spec SSE

cherry-pick from envoyproxy#30573 envoyproxy@62f4a14#diff-c904f9b0d49cdd938ffaa952192372529415afa8602d7dc6acb904e61111d8a5 add strict mode to stateful session, return 503 when destination is not available

cherry-pick from envoyproxy#32093 envoyproxy@6354dcf support 0 ttl in cookie stateful sessiion to disable cookie expiration

Signed-off-by: 爵银 <[email protected]>
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.

stateful session enhancement: to support the MCP protocol session management

8 participants