| 1 | TP2: Dynamically Generated Cacheable xDS Resources |
| 2 | ---- |
| 3 | * Author(s): markdroth, htuch |
| 4 | * Approver: htuch |
| 5 | * Implemented in: <xDS client, ...> |
| 6 | * Last updated: 2022-02-09 |
| 7 | |
| 8 | ## Abstract |
| 9 | |
| 10 | This xRFC proposes a new mechanism to allow xDS servers to |
| 11 | dynamically generate the contents of xDS resources for individual |
| 12 | clients while at the same time preserving cacheability. Unlike the |
| 13 | context parameter mechanism that is part of the new xDS naming scheme (see |
| 14 | [xRFC TP1](TP1-xds-transport-next.md)), the mechanism described in |
| 15 | this proposal is visible only to the transport protocol layer, not to the |
| 16 | data model layer. This means that if a resource has a parameter that |
| 17 | affects its contents, that parameter is not part of the resource's name, |
| 18 | which means that any other resources that refer to the resource do not |
| 19 | need to encode the parameter. Therefore, use of these parameters is |
| 20 | not viral, thus making the mechanism much easier to use. |
| 21 | |
| 22 | ## Background |
| 23 | |
| 24 | There are many use-cases where a control plane may need to |
| 25 | dynamically generate the contents of xDS resources to tailor the |
| 26 | resources for individual clients. One common case is where the |
| 27 | server has a list of routes to configure, but individual routes in |
| 28 | the list may be included or excluded based on the client's dynamic |
| 29 | selection parameters (today, conveyed as node metadata). Thus, |
| 30 | the server needs to generate a slightly different version of the |
| 31 | `RouteConfiguration` for clients based on the parameters they send. (See |
| 32 | https://cloud.google.com/traffic-director/docs/configure-advanced-traffic-management#config-filtering-metadata |
| 33 | for an example.) |
| 34 | |
| 35 | The new xDS naming scheme described in [xRFC TP1](TP1-xds-transport-next.md) |
| 36 | provides a mechanism called context parameters, which is intended to move all |
| 37 | parameters that affect resource contents into the resource name, thus adding |
| 38 | cacheability to the xDS ecosystem. However, this approach means that these |
| 39 | parameters become part of the resource graph on an individual client, which |
| 40 | causes a number of problems: |
| 41 | - Dynamic context parameters are viral, spreading from a given resource |
| 42 | to all earlier resources in the resource graph. For example, if |
| 43 | multiple variants of an EDS resource are needed, there need to be two |
| 44 | different instances of the resource with different names, |
| 45 | distinguished by a context parameter. But because the contents of the |
| 46 | CDS resource include the name of the corresponding EDS resource name, |
| 47 | that means that we also need two different versions of the CDS |
| 48 | resource, also distinguished by the same context parameter. And then |
| 49 | we need two different versions of the RDS resource, since that needs |
| 50 | to refer to the CDS resource. And then two different versions of the |
| 51 | LDS resource, which refers to the RDS resource. This causes a |
| 52 | combinatorial explosion in the number of resources needed, and it adds |
| 53 | complexity to xDS servers, which need to construct the right variants |
| 54 | of every resource and make sure that they refer to each other using |
| 55 | the right names. |
| 56 | - In the new xDS naming scheme, context parameters are exact-match-only. |
| 57 | This means that if a control plane wants to provide the same resource |
| 58 | both with and without a given parameter, it needs to publish two |
| 59 | versions of the resource, each with a different name, even though the |
| 60 | contents are the same, which can also cause unnecessarily poor cache |
| 61 | performance. For example, in the "dynamic route selection" use-case, |
| 62 | let's say that every client uses two different dynamic selection |
| 63 | parameters, `env` (which can have one of the values `prod`, `canary`, or |
| 64 | `test`) and `version` (which can have one of the values `v1`, `v2`, or |
| 65 | `v3`). Now let's say that there is a `RouteConfiguration` with one route |
| 66 | that should be selected via the parameter `env=prod` and another route that |
| 67 | should be selected via the parameter `version=v1`. This means that there |
| 68 | are only four variants of the `RouteConfiguration` resource (`{env!=prod, |
| 69 | version!=v1}`, `{env=prod, version!=v1}`, `{env!=prod, version=v1}`, and |
| 70 | `{env=prod, version=v1}`). However, the exact-match semantics means |
| 71 | that there will have to be nine different versions of this resource, |
| 72 | one for each combination of values of the two parameters. |
| 73 | |
| 74 | ### Related Proposals: |
| 75 | * [xRFC TP1: new xDS naming scheme](TP1-xds-transport-next.md) |
| 76 | |
| 77 | ## Proposal |
| 78 | |
| 79 | This document proposes an alternative approach. We start with the |
| 80 | observation that resource names are used in two places: |
| 81 | |
| 82 | - The **transport protocol** layer, which needs to identify the right |
| 83 | resource contents to send for a given resource name, often obtaining |
| 84 | those resource contents from a cache. |
| 85 | - The **resource graph** used on an individual client, where there are a |
| 86 | set of data model resources that refer to each other by name. For |
| 87 | example, a `RouteConfiguration` refers to individual `Cluster` resources |
| 88 | by name. |
| 89 | |
| 90 | The use-cases that we're aware of for dynamic resource selection have |
| 91 | an important property that we can take advantage of. When multiple |
| 92 | variants of a given resource exist, any given client will only ever use |
| 93 | one of those variants at a given time. That means that the parameters |
| 94 | that affect which variant of the resource is used are required by the |
| 95 | transport protocol, but they are not required by the client's data model. |
| 96 | |
| 97 | It should be noted that caching xDS proxies, unlike "leaf" clients, will |
| 98 | need to track multiple variants of each resource, since a given caching |
| 99 | proxy may be serving clients that need different variants of a given |
| 100 | resource. However, since caching xDS proxies deal with resources only |
| 101 | at the transport protocol layer, the resource graph layer is |
| 102 | essentially irrelevant in that case. |
| 103 | |
| 104 | ### Dynamic Parameters |
| 105 | |
| 106 | With the above property in mind, this document proposes the following |
| 107 | data structures: |
| 108 | - **Dynamic parameters**, which are a set of key/value pairs sent by the |
| 109 | client when subscribing to a resource. |
| 110 | - **Dynamic parameter constraints**, which are a set of criteria that |
| 111 | can be used to determine whether a set of dynamic parameters matches |
| 112 | the constraints. These constraints are considered part of the unique |
| 113 | identifier for an xDS resource (along with the resource name itself) |
| 114 | on xDS servers, xDS clients, and xDS caching proxies. This provides a |
| 115 | mechanism to represent multiple variants of a given resource in a |
| 116 | cacheable way. |
| 117 | |
| 118 | Both of these data structures are used in the xDS transport protocol, |
| 119 | but they are not part of the resource name and therefore do not appear as |
| 120 | part of the resource graph. |
| 121 | |
| 122 | When a client subscribes to a resource, it specifies a set of dynamic |
| 123 | parameters. In response, the server will send a resource whose dynamic |
| 124 | parameter constraints match the dynamic parameters in the subscription |
| 125 | request. A client that subscribes to multiple variants of a resource (such |
| 126 | as a caching xDS proxy) will use the dynamic parameter constraints on the |
| 127 | returned resource to determine which of its subscriptions the resource is |
| 128 | associated with. |
| 129 | |
| 130 | Dynamic parameters, unlike context parameters, will not be |
| 131 | exact-match-only. Dynamic parameter constraints will be able to represent |
| 132 | certain simple types of flexible matching, such as matching an exact |
| 133 | value or the existance of a key, and simple AND and OR combinations |
| 134 | of constraints. This flexible matching semantic means that there may be |
| 135 | ambiguities when determining which resources match which subscriptions, |
| 136 | which are discussed below. |
| 137 | |
| 138 | #### Constraints Representation |
| 139 | |
| 140 | Dynamic parameter constraints will be represented in protobuf form as follows: |
| 141 | |
| 142 | ```proto |
| 143 | message DynamicParameterConstraints { |
| 144 | // A single constraint for a given key. |
| 145 | message SingleConstraint { |
| 146 | message Exists {} |
| 147 | // The key to match against. |
| 148 | string key = 1; |
| 149 | // How to match. |
| 150 | oneof constraint_type { |
| 151 | // Matches this exact value. |
| 152 | string value = 2; |
| 153 | // Key is present (matches any value except for the key being absent). |
| 154 | Exists exists = 3; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | message ConstraintList { |
| 159 | repeated DynamicParameterConstraints constraints = 1; |
| 160 | } |
| 161 | |
| 162 | oneof type { |
| 163 | // A single constraint to evaluate. |
| 164 | SingleConstraint constraint = 1; |
| 165 | |
| 166 | // A list of constraints to be ORed together. |
| 167 | ConstraintList or_constraints = 2; |
| 168 | |
| 169 | // A list of constraints to be ANDed together. |
| 170 | ConstraintList and_constraints = 3; |
| 171 | |
| 172 | // The inverse (NOT) of a set of constraints. |
| 173 | DynamicParameterConstraints not_constraints = 4; |
| 174 | } |
| 175 | } |
| 176 | ``` |
| 177 | |
| 178 | #### Background: xDS Client and Server Architecture |
| 179 | |
| 180 | Before discussing where dynamic parameter matching is performed, it is |
| 181 | useful to provide some additional background on xDS client and server |
| 182 | architecture, independent of this design. |
| 183 | |
| 184 | The xDS transport protocol is fundamentally a mechanism that matches up |
| 185 | subscriptions provided by a client with resources provided by a server. |
| 186 | The client controls what it is subscribing to at any given time, |
| 187 | and the server must send the resources from its database that match the |
| 188 | currently active subscriptions. |
| 189 | |
| 190 | An xDS server may be thought of as containing a database of resources, |
| 191 | in which each resource has an associated list of clients that are currently |
| 192 | subscribed to that resource. Whenever a client subscribes to a resource, |
| 193 | the server will send the current version of that resource to the client, |
| 194 | and it will add the client to the list of clients currently subscribed to |
| 195 | that resource. Whenever the server receives a new version of that resource |
| 196 | in its database, it will send the update to all clients that are currently |
| 197 | subscribed to that resource. Whenever a client unsubscribes from a |
| 198 | resource, it is removed from the list of clients subscribed to that |
| 199 | resource, so that the server knows not to send it subsequent updates for |
| 200 | that resource. |
| 201 | |
| 202 | This same paradigm of matching up subscriptions with resources actually |
| 203 | applies to the xDS client as well. Because the xDS transport protocol |
| 204 | does not require a server to resend a resource unless its contents have |
| 205 | changed, clients need to cache the most recently seen value locally in |
| 206 | case they need it again. In general, the best way to structure an xDS |
| 207 | transport protocol client is as an API where the caller can start or |
| 208 | stop subscribing to a given resource at any time, and the xDS client will |
| 209 | handle the wire-level communication and cache the resources returned by |
| 210 | the server. The cache in the xDS client functions very similarly to the |
| 211 | database in an xDS server: each cache entry contains the current value |
| 212 | of the resource received from the xDS server and a list of subscribers to |
| 213 | that resource. When the xDS client sees the first subscription start for |
| 214 | a given resource, it will create the cache entry for that resource, add |
| 215 | the subscriber to the list of subscribers for that resource, and request |
| 216 | that resource from the xDS server. When it receives the resource from |
| 217 | the server, it will store the resource in the cache entry and deliver |
| 218 | it to all subscribers. When the xDS client sees a second subscription |
| 219 | start for the same resource, it will add the new subscriber to the list |
| 220 | of subscribers for that resource and immediately deliver the cached value |
| 221 | of the resource to the new subscriber. Whenever the server sends an |
| 222 | updated version of the resource, the xDS client will deliver the update |
| 223 | to all subscribers. When all subscriptions are stopped, the xDS client |
| 224 | will unsubscribe from the resource on the wire, so that the xDS server |
| 225 | knows to stop sending updates for that resource to the client. |
| 226 | |
| 227 | In effect, the logic in an xDS client is essentially the same as that in an |
| 228 | xDS server, with only two differences. First, subscriptions come from local |
| 229 | API callers instead of downstream RPC clients. And second, the database does |
| 230 | not contain the authoritative source of the resource contents but rather cached |
| 231 | values obtained from the server, and the database entries are removed when |
| 232 | the last subscription for a given resource is stopped. |
| 233 | |
| 234 | The logic in a caching xDS proxy is also essentially the same as that in an xDS |
| 235 | server, with only one difference. Just like an xDS client, the database |
| 236 | does not contain the authoritative source of the resource contents but |
| 237 | rather cached values obtained from the server. However, like an xDS |
| 238 | server, subscriptions do come from downstream RPC clients rather than local |
| 239 | API callers. |
| 240 | |
| 241 | The following table summarizes this structure: |
| 242 | |
| 243 | <table> |
| 244 | |
| 245 | <tr> |
| 246 | <th>xDS Node Type</th> |
| 247 | <th>Source of Subscriptions</th> |
| 248 | <th>Source of Resource Contents</th> |
| 249 | </tr> |
| 250 | |
| 251 | <tr> |
| 252 | <td>xDS Server</td> |
| 253 | <td>downstream xDS clients</td> |
| 254 | <td>authoritative data</td> |
| 255 | </tr> |
| 256 | |
| 257 | <tr> |
| 258 | <td>xDS Client</td> |
| 259 | <td>local API callers</td> |
| 260 | <td>cached data from upstream xDS server</td> |
| 261 | </tr> |
| 262 | |
| 263 | <tr> |
| 264 | <td>xDS Caching Proxy</td> |
| 265 | <td>downstream xDS clients</td> |
| 266 | <td>cached data from upstream xDS server</td> |
| 267 | </tr> |
| 268 | |
| 269 | </table> |
| 270 | |
| 271 | #### Where Dynamic Parameter Matching is Performed |
| 272 | |
| 273 | Because of the architecture described above, evaluation of matching between |
| 274 | a set of dynamic parameters and a set of constraints may need to be |
| 275 | performed by both xDS servers and xDS clients. |
| 276 | |
| 277 | xDS servers that support multiple variants of a resource perform this |
| 278 | matching when deciding which variant of a given resource to return for a |
| 279 | given subscription request. xDS servers that support multiple variants of |
| 280 | a resource MUST send the dynamic parameter constraints associated with a |
| 281 | resource variant to the client along with that variant. Any server |
| 282 | implementation that fails to do so is in violation of this specification. |
| 283 | |
| 284 | xDS caching proxies that support multiple variants of a resource also |
| 285 | perform this matching when deciding which variant of a given resource to |
| 286 | return for a given subscription request. Caching proxies MUST store the |
| 287 | dynamic parameter constraints obtained from the upstream server along with |
| 288 | each resource variant, which they will use when deciding which variant of a |
| 289 | given resource to return for a given subscription request from a downstream |
| 290 | xDS client. Caching proxies MUST send those dynamic parameter constraints to |
| 291 | the downstream client when sending that variant of the resource. |
| 292 | |
| 293 | Note this design assumes that a given leaf client will use a fixed set of |
| 294 | dynamic parameters, typically configured in a local bootstrap file, for all |
| 295 | subscriptions over its lifetime. Given that, it is not strictly necessary |
| 296 | for a leaf client to perform this matching, since it should only ever |
| 297 | receive a single variant of a given resource, which should always match the |
| 298 | dynamic parameters it subscribed with. However, clients MAY perform this |
| 299 | matching, which may be useful in cases where the same cache implementation |
| 300 | is used on both a leaf client and a caching proxy. |
| 301 | |
| 302 | It is important to note that the dynamic parameter matching behavior becomes |
| 303 | an inherent part of the xDS transport protocol. xDS servers that interact |
| 304 | only with leaf clients may be tempted not to send dynamic parameter |
| 305 | constraints to the client along with the chosen resource variant, and |
| 306 | leaf clients may accept that. However, as soon as that server wants to |
| 307 | start interacting with a caching proxy or a client that does verify the |
| 308 | constraints, it will run into problems. xDS server implementors are |
| 309 | strongly encouraged not to omit the dynamic parameter constraints in their |
| 310 | responses. |
| 311 | |
| 312 | #### Example: Basic Dynamic Parameters Usage |
| 313 | |
| 314 | Let's say that the clients are currently categorized by the parameter |
| 315 | `env`, whose value is either `prod` or `test`. So any given client will |
| 316 | send one of the following sets of dynamic parameters: |
| 317 | - `{env=prod}` |
| 318 | - `{env=test}` |
| 319 | |
| 320 | Now let's say that the server has two variants of a given resource, and |
| 321 | the variants have the following dynamic parameter constraints: |
| 322 | |
| 323 | ```textproto |
| 324 | // For {env=prod} |
| 325 | {constraint:{key:"env" value:"prod"}} |
| 326 | |
| 327 | // For {env=test} |
| 328 | {constraint:{key:"env" value:"test"}} |
| 329 | ``` |
| 330 | |
| 331 | When a client subscribes to this resource with dynamic parameters |
| 332 | `{env=prod}`, the server will return the first variant; when a client |
| 333 | subscribes to this resource with dynamic parameters `{env=test}`, the |
| 334 | server will return the second variant. When the client receives the |
| 335 | returned resource, it will verify that the dynamic parameters it sent |
| 336 | match the constraints of the returned resource. |
| 337 | |
| 338 | #### Unconstrained Parameters |
| 339 | |
| 340 | Note that clients may send dynamic parameters that are not specified in |
| 341 | the constraints on the resulting resource. If a set of constraints does |
| 342 | not specify any constraint for a given parameter sent by the client, that |
| 343 | parameter does not prevent the constraints from matching. This allows |
| 344 | clients to add new parameters before a server begins using them. |
| 345 | (In general, we expect clients to send a lot of keys that may not |
| 346 | actually be used by the server, since deployments often divide their |
| 347 | clients into categories before they have a need to differentiate the |
| 348 | configs for those categories.) |
| 349 | |
| 350 | Continuing the example above, if the server wanted to send the same |
| 351 | contents for a given resource to both `{env=prod}` and `{env=test}` clients, |
| 352 | it would have only a single variant of that resource, and that variant would |
| 353 | not have any constraints. The server would therefore send that variant to |
| 354 | all clients, and the clients would consider it a match for the constraints |
| 355 | that they subscribed with. |
| 356 | |
| 357 | #### Example: Transition Scenarios |
| 358 | |
| 359 | Consider what happens in transition scenarios, where a deployment initially |
| 360 | groups its clients on a single key but then wants to add a second key. |
| 361 | The second key needs to be added both in the constraints on the server |
| 362 | side and in the clients' configurations, but those two changes cannot |
| 363 | occur atomically. |
| 364 | |
| 365 | Let's start with the above example where the clients are already divided into |
| 366 | `env=prod` and `env=test`. Let's say that now the deployment wants to add |
| 367 | an additional key called `version`, whose value will be either `v1` or `v2`, |
| 368 | so that it can further subdivide its clients' configs. |
| 369 | |
| 370 | The first step is to add the new key on the clients, so that any given client |
| 371 | will send one of the following sets of dynamic parameters: |
| 372 | - `{env=prod, version=v1}` |
| 373 | - `{env=prod, version=v2}` |
| 374 | - `{env=test, version=v1}` |
| 375 | - `{env=test, version=v2}` |
| 376 | |
| 377 | At this point, the server still does not have a variant of any resource |
| 378 | that has constraints for the `version` key; it has only variants that |
| 379 | differentiate between `env=prod` and `env=test`. But the addition of |
| 380 | the new key on the clients will not affect which resource variant is |
| 381 | sent to each client, because it does not affect the matching. Clients |
| 382 | sending `{env=prod, version=v1}` or `{env=prod, version=v2}` will both get |
| 383 | the resource variant for `env=prod`, and clients sending |
| 384 | `{env=test, version=v1}` or `{env=test, version=v2}` will both get the |
| 385 | resource variant for `env=test`. |
| 386 | |
| 387 | Once the clients have all been updated to send the new key, then the |
| 388 | server can be updated to have different resource variants based on the |
| 389 | `version` key. For example, it may replace the single resource variant |
| 390 | for `env=prod` with the following two variants: |
| 391 | |
| 392 | ```textproto |
| 393 | // For {env=prod, version=v1} |
| 394 | {and_constraints:[ |
| 395 | {constraint:{key:"env" value:"prod"}}, |
| 396 | {constraint:{key:"version" value:"v1"}} |
| 397 | ]} |
| 398 | |
| 399 | // For {env=prod, version=v2} |
| 400 | {and_constraints:[ |
| 401 | {constraint:{key:"env" value:"prod"}}, |
| 402 | {constraint:{key:"version" value:"v2"}} |
| 403 | ]} |
| 404 | ``` |
| 405 | |
| 406 | Once that change happens on the server, the clients will start getting |
| 407 | the correct variant of the resource based on their `version` key. |
| 408 | |
| 409 | Note that in order to avoid causing matching ambiguity, the server must |
| 410 | handle this kind of change by sending the deletion of the original resource |
| 411 | variant and the creation of the replacement resource variants in a |
| 412 | single xDS response. This will allow the client to atomically apply the |
| 413 | change to its database. For any given subscriber, the client should |
| 414 | present the change as if there was only one variant of the resource and |
| 415 | that variant had just been updated. |
| 416 | |
| 417 | #### Matching Ambiguity |
| 418 | |
| 419 | As mentioned above, this design does introduce the possibility of |
| 420 | matching ambiguity in certain cases, where there may be more than one |
| 421 | variant of a resource that matches the dynamic parameters specified by |
| 422 | the client. |
| 423 | |
| 424 | If an xDS transport protocol implementation does encounter multiple |
| 425 | possible matching variants of a resource, its behavior is undefined. |
| 426 | In the following sections, we evaluate the cases where that can occur |
| 427 | and specify how each one will be addressed. |
| 428 | |
| 429 | ##### Adding a New Key on the Server First |
| 430 | |
| 431 | Consider what would happen in the above transition scenario if we changed |
| 432 | the server to have multiple variants of a resource differentiated by |
| 433 | the new `version` key before all of the clients were upgraded to use |
| 434 | that key. For clients sending `{env=prod}`, there would be two possible |
| 435 | matching variants of the resource, one for `version=v1` and another for |
| 436 | `version=v2`, and there would be no way to determine which variant to |
| 437 | use for that client. |
| 438 | |
| 439 | As stated above, we are optimizing for the case where new keys are added |
| 440 | on clients first, since that is expected to be the common scenario. |
| 441 | However, there may be cases where it is not feasible to have all clients |
| 442 | start sending a new key before the server needs to start making use of |
| 443 | that key. |
| 444 | |
| 445 | For example, let's say that this transition scenario is occurring in |
| 446 | an environment where the xDS server is controlled by one team and the |
| 447 | clients are controlled by various other teams, so it's not feasible to |
| 448 | force all clients to start sending the new `version` key all at once. |
| 449 | But there is one particular client team that is eager to start using |
| 450 | the new `version` key to differentiate the configs of their clients, |
| 451 | and they don't want to wait for all of the other client teams to start |
| 452 | sending the new key. |
| 453 | |
| 454 | Consider what happens if the server simply adds a variant of the |
| 455 | resource with the new key, while leaving the original resource variant |
| 456 | in place: |
| 457 | |
| 458 | ```textproto |
| 459 | // Existing variant for older clients that are not yet sending the |
| 460 | // version key. |
| 461 | {constraint:{key:"env" value:"prod"}} |
| 462 | |
| 463 | // New variant intended for clients sending the version key. |
| 464 | {and_constraints:[ |
| 465 | {constraint:{key:"env" value:"prod"}}, |
| 466 | {constraint:{key:"version" value:"v1"}} |
| 467 | ]} |
| 468 | ``` |
| 469 | |
| 470 | This will work fine for older clients that are not yet sending the |
| 471 | `version` key, because their dynamic parameters will not match the new |
| 472 | variant's constraints. However, newer clients that are sending dynamic |
| 473 | parameters `{env=prod, version=v1}` will run into ambiguity: those |
| 474 | parameters can match either of the above variants of the resource. |
| 475 | |
| 476 | This situation will be avoided via a best practice that all authoritative |
| 477 | xDS servers should have **all variants of a given resource specify |
| 478 | constraints for the same set of keys**. |
| 479 | |
| 480 | In order to make this work for the case where the server starts sending |
| 481 | the constraint on the new key before all clients are sending it, we |
| 482 | provide the `exists` matcher, which will allow the server to specify |
| 483 | a default explicitly for clients that are not yet sending a new key. |
| 484 | In this example, the server would actually have the following two |
| 485 | variants: |
| 486 | |
| 487 | ```textproto |
| 488 | // Existing variant for older clients that are not yet sending the |
| 489 | // version key. |
| 490 | {and_constraints:[ |
| 491 | {constraint:{key:"env" value:"prod"}}, |
| 492 | {not_constraint: |
| 493 | {constraint:{key:"version" exists:{}}} |
| 494 | } |
| 495 | ]} |
| 496 | |
| 497 | // New variant for clients sending the version key. |
| 498 | {and_constraints:[ |
| 499 | {constraint:{key:"env" value:"prod"}}, |
| 500 | {constraint:{key:"version" value:"v1"}} |
| 501 | ]} |
| 502 | ``` |
| 503 | |
| 504 | This allows maintaining the requirement that all variants of a given |
| 505 | resource have constraints on the same set of keys, while also allowing |
| 506 | the server to explicitly provide a result for older clients that do not |
| 507 | yet send the new key. |
| 508 | |
| 509 | ##### Variants With Overlapping Constraint Values |
| 510 | |
| 511 | There is also a possible ambiguity that can occur if a server provides |
| 512 | multiple variants of a resource whose constraints for a given key |
| 513 | overlap in terms of the values they can match. For example, let's say |
| 514 | that a server has the following two variants of a resource: |
| 515 | |
| 516 | ```textproto |
| 517 | // Matches {env=prod} or {env=test}. |
| 518 | {or_constraints:[ |
| 519 | {constraint:{key:"env" value:"prod"}}, |
| 520 | {constraint:{key:"env" value:"test"}} |
| 521 | ]} |
| 522 | |
| 523 | // Matches {env=qa} or {env=test}. |
| 524 | {or_constraints:[ |
| 525 | {constraint:{key:"env" value:"qa"}}, |
| 526 | {constraint:{key:"env" value:"test"}} |
| 527 | ]} |
| 528 | ``` |
| 529 | |
| 530 | Now consider what happens if a client subscribes with dynamic parameters |
| 531 | `{env=test}`. Those dynamic parameters can match either of the above |
| 532 | variants of the resource. |
| 533 | |
| 534 | This situation will be avoided via a best practice that all authoritative |
| 535 | xDS servers should have **all variants of a given resource specify |
| 536 | non-overlapping constraints for the same set of keys**. Control planes |
| 537 | must not accept a set of resources that violates this requirement. |
| 538 | |
| 539 | #### Matching Behavior and Best Practices |
| 540 | |
| 541 | We advise deployments to avoid ambiguity through the following best practices: |
| 542 | - Whenever there are multiple variants of a resource, all variants must |
| 543 | list the same set of keys. This allows the server to ignore constraints |
| 544 | on keys sent by the client that do not affect the choice of variant |
| 545 | without causing ambiguity in cache misses. Servers may use the |
| 546 | `exists` mechanism to provide backward compatibility for clients that |
| 547 | are not yet sending a newly added key. |
| 548 | - The constraints on each variant of a given resource must be mutually |
| 549 | exclusive. For example, if one variant of a resource matches a given key |
| 550 | with values "foo" or "bar", and another variant matches that same key |
| 551 | with values "bar" or "baz", that would cause ambiguity, because both |
| 552 | variants would match the value "bar". |
| 553 | - There must be a variant of the resource for every value of a key that is |
| 554 | going to be present. For example, if clients will send constraints on the |
| 555 | `env` key requiring the value to be one of `prod`, `test`, or `qa`, then |
| 556 | you must have each of those three variants of the resource. (Failure |
| 557 | to do this will result in the server acting as if the requested |
| 558 | resource does not exist.) |
| 559 | |
| 560 | #### Transport Protocol Changes |
| 561 | |
| 562 | The following message will be added to represent a subscription to a |
| 563 | resource by name with associated dynamic parameters: |
| 564 | |
| 565 | ```proto |
| 566 | // A specification of a resource used when subscribing or unsubscribing. |
| 567 | message ResourceLocator { |
| 568 | // The resource name to subscribe to. |
| 569 | string name = 1; |
| 570 | |
| 571 | // A set of dynamic parameters used to match against the dynamic parameter |
| 572 | // constraints on the resource. This allows clients to select between |
| 573 | // multiple variants of the same resource. |
| 574 | map<string, string> dynamic_parameters = 2; |
| 575 | } |
| 576 | ``` |
| 577 | |
| 578 | The following new field will be added to `DiscoveryRequest`, to allow clients |
| 579 | to specify dynamic parameters when subscribing to a resource: |
| 580 | |
| 581 | ```proto |
| 582 | // Alternative to resource_names field that allows specifying cache |
| 583 | // keys along with each resource name. Clients that populate this field |
| 584 | // must be able to handle responses from the server where resources are |
| 585 | // wrapped in a Resource message. |
| 586 | repeated ResourceLocator resource_locators = 7; |
| 587 | ``` |
| 588 | |
| 589 | Similarly, the following fields will be added to `DeltaDiscoveryRequest`: |
| 590 | |
| 591 | ```proto |
| 592 | // Alternative to resource_names_subscribe field that allows specifying cache |
| 593 | // keys along with each resource name. |
| 594 | repeated ResourceLocator resource_locators_subscribe = 8; |
| 595 | |
| 596 | // Alternative to resource_names_unsubscribe field that allows specifying cache |
| 597 | // keys along with each resource name. |
| 598 | repeated ResourceLocator resource_locators_unsubscribe = 9; |
| 599 | ``` |
| 600 | |
| 601 | The following message will be added to represent the name of a specific |
| 602 | variant of a resource: |
| 603 | |
| 604 | ```proto |
| 605 | // Specifies a concrete resource name. |
| 606 | message ResourceName { |
| 607 | // The name of the resource. |
| 608 | string name = 1; |
| 609 | |
| 610 | // Dynamic parameter constraints associated with this resource. To be used by |
| 611 | // client-side caches (including xDS proxies) when matching subscribed |
| 612 | // resource locators. |
| 613 | DynamicParameterConstraints dynamic_parameter_constraints = 2; |
| 614 | } |
| 615 | ``` |
| 616 | |
| 617 | The following field will be added to the `Resource` message, to allow the |
| 618 | server to return the dynamic parameters associated with each resource: |
| 619 | |
| 620 | ```proto |
| 621 | // Alternative to the *name* field, to be used when the server supports |
| 622 | // multiple variants of the named resource that are differentiated by |
| 623 | // dynamic parameter constraints. |
| 624 | // Only one of *name* or *resource_name* may be set. |
| 625 | ResourceName resource_name = 8; |
| 626 | ``` |
| 627 | |
| 628 | And finally, the following field will be added to `DeltaDiscoveryResponse`: |
| 629 | |
| 630 | ```proto |
| 631 | // Alternative to removed_resources that allows specifying which variant of |
| 632 | // a resource is being removed. This variant must be used for any resource |
| 633 | // for which dynamic parameter constraints were sent to the client. |
| 634 | repeated ResourceName removed_resource_names = 8; |
| 635 | ``` |
| 636 | |
| 637 | ### Client Configuration |
| 638 | |
| 639 | Client configuration is outside of the scope of this design. However, |
| 640 | this section lists some considerations for client implementors to take |
| 641 | into account. |
| 642 | |
| 643 | #### Configuring Dynamic Parameters |
| 644 | |
| 645 | Each leaf client should have a way of configuring the dynamic parameters |
| 646 | that it sends. |
| 647 | |
| 648 | For old-style resource names (those not using the new `xdstp` URI |
| 649 | scheme from [xRFC TP1](TP1-xds-transport-next.md)), clients should |
| 650 | send the same set of dynamic parameters for all resource subscriptions. |
| 651 | The client's configuration should allow setting these default dynamic |
| 652 | parameters globally. |
| 653 | |
| 654 | For new-style resource names, clients should send the same set of |
| 655 | dynamic parameters for all resource subscriptions in a given authority. |
| 656 | The client's configuration should allow setting the dymamic parameters to |
| 657 | use for each authority. |
| 658 | |
| 659 | #### Migrating From Node Metadata |
| 660 | |
| 661 | Today, the equivalent of dynamic parameter constraints is node metadata, |
| 662 | which can be used by servers to determine the set of resources to send |
| 663 | for LDS and CDS wildcard subscriptions or to determine the contents of |
| 664 | other resources (e.g., to select individual routes to be included in an |
| 665 | RDS resource). For transition purposes, this mechanism can continue |
| 666 | to be supported by the client performing direct translation of node |
| 667 | metadata to dynamic parameters. |
| 668 | |
| 669 | Any given xDS client may support either or both of these mechanisms. |
| 670 | |
| 671 | ### Considerations for Implementations |
| 672 | |
| 673 | This specification does not prescribe implementation details for xDS |
| 674 | clients or servers. However, for illustration purposes, this section |
| 675 | describes how a naive implementation might be structured. |
| 676 | |
| 677 | The database of an xDS server or cache of an xDS client can be thought |
| 678 | of as a map, keyed by resource type and resource name. Prior to this |
| 679 | specification, the value of the map would have been the current value of the |
| 680 | resource and a list of subscribers that need to be updated when the |
| 681 | resource changes. In C++ syntax, the data structure might look like this: |
| 682 | |
| 683 | ```c++ |
| 684 | // Represents a subscriber (either a downstream xDS client or a local API caller). |
| 685 | class Subscriber { |
| 686 | public: |
| 687 | // ... |
| 688 | }; |
| 689 | |
| 690 | struct DatabaseEntry { |
| 691 | // Current contents of resource. |
| 692 | // Whenever this changes, the change will be sent to all subscribers. |
| 693 | std::optional<google::protobuf::Any> resource_contents; |
| 694 | |
| 695 | // Current list of subscribers. |
| 696 | // Entries are added and removed as subscriptions are started and stopped. |
| 697 | std::set<Subscriber*> subscribers; |
| 698 | }; |
| 699 | |
| 700 | using Database = |
| 701 | std::map<std::string /*resource_type*/, |
| 702 | std::map<std::string /*resource_name*/, DatabaseEntry>; |
| 703 | ``` |
| 704 | |
| 705 | This design does not change the key structure of the map, but it does |
| 706 | change the structure of the value of the map. In particular, instead of |
| 707 | storing a single value for the resource contents, it will need to store |
| 708 | multiple values, keyed by the associated dynamic parameter constraints. |
| 709 | And for each subscriber, it will need to store the dynamic parameters that |
| 710 | the subscriber specified. In a naive implementation (not optimized at all), |
| 711 | the modified data structure may look like this: |
| 712 | |
| 713 | ```c++ |
| 714 | // Represents a subscriber (either a downstream xDS client or a local API caller). |
| 715 | class Subscriber { |
| 716 | public: |
| 717 | // ... |
| 718 | |
| 719 | // Returns the dynamic parameters specified for the subscription. |
| 720 | DynamicParameters dynamic_parameters() const; |
| 721 | }; |
| 722 | |
| 723 | struct DatabaseEntry { |
| 724 | // Resource contents for each variant of the resource, keyed by |
| 725 | // dynamic parameter constraints. |
| 726 | // Whenever a given variant of the resource changes, the change will be |
| 727 | // sent to all subscribers whose dynamic parameters match the constraints |
| 728 | // of the resource variant that changed. |
| 729 | std::map<DynamicParameterConstraints, |
| 730 | std::optional<google::protobuf::Any>> resource_contents; |
| 731 | |
| 732 | // Current list of subscribers. |
| 733 | // Entries are added and removed as subscriptions are started and stopped. |
| 734 | std::set<Subscriber*> subscribers; |
| 735 | }; |
| 736 | ``` |
| 737 | |
| 738 | When a variant of a resource is updated, the variant is stored in the map |
| 739 | based on its dynamic parameter constraints. The implementation will then |
| 740 | iterate through the list of subscribers, sending the updated resource |
| 741 | variant and its dynamic parameter constraints to each subscriber whose |
| 742 | dynamic parameters match those constraints. |
| 743 | |
| 744 | A more optimized implementation may instead choose to store a separate list |
| 745 | of subscribers for each resource variant, thus avoiding the need to perform |
| 746 | matching for every subscriber upon every update of a resource variant. |
| 747 | However, this would require moving subscribers from one variant to another |
| 748 | whenever the dynamic parameters change on the resource variants. |
| 749 | |
| 750 | ### Example |
| 751 | |
| 752 | This section shows how the mechanism described in this proposal can be |
| 753 | used to address the use-case described in the "Background" section above. |
| 754 | |
| 755 | Let's say that every client uses two different dynamic selection |
| 756 | parameters, `env` (which can have one of the values `prod`, `canary`, |
| 757 | or `test`) and `version` (which can have one of the values `v1`, `v2`, |
| 758 | or `v3`). Now let's say that there is a `RouteConfiguration` with one |
| 759 | route that should be selected via the parameter `env=prod` and another |
| 760 | route that should be selected via the parameter `version=v1`. Without |
| 761 | this design, the server would need to actually provide the cross-product |
| 762 | of these parameter values, so there will be 9 different variants of the |
| 763 | resource, even though there are only 4 unique contents for the resource. |
| 764 | However, this design instead allows the server to provide only the 4 |
| 765 | unique variants of the resource, with constraints allowing each client |
| 766 | to get the appropriate one: |
| 767 | |
| 768 | <table> |
| 769 | <tr> |
| 770 | <th>Dynamic Parameter Constraints on Resource</th> |
| 771 | <th>Resource Contents</th> |
| 772 | </tr> |
| 773 | |
| 774 | <tr> |
| 775 | <td> |
| 776 | <code>{and_constraints:[ |
| 777 | {not_constraints: |
| 778 | {constraint:{key:"env" value:"prod"}} |
| 779 | }, |
| 780 | {not_constraints: |
| 781 | {constraint:{key:"version" value:"v1"}} |
| 782 | } |
| 783 | ]}</code> |
| 784 | </td> |
| 785 | <td> |
| 786 | <ul> |
| 787 | <li>does <i>not</i> include the route for <code>env=prod</code> |
| 788 | <li>does <i>not</i> include the route for <code>version=v1</code> |
| 789 | </ul> |
| 790 | </td> |
| 791 | </tr> |
| 792 | |
| 793 | <tr> |
| 794 | <td> |
| 795 | <code>{and_constraints:[ |
| 796 | {constraint:{key:"env" value:"prod"}}, |
| 797 | {not_constraints: |
| 798 | {constraint:{key:"version" value:"v1"} |
| 799 | } |
| 800 | ]}</code> |
| 801 | </td> |
| 802 | <td> |
| 803 | <ul> |
| 804 | <li>does include the route for <code>env=prod</code> |
| 805 | <li>does <i>not</i> include the route for <code>version=v1</code> |
| 806 | </ul> |
| 807 | </td> |
| 808 | </tr> |
| 809 | |
| 810 | <tr> |
| 811 | <td> |
| 812 | <code>{and_constraints:[ |
| 813 | {not_constraints: |
| 814 | {constraint:{key:"env" value:"prod"}} |
| 815 | }, |
| 816 | {constraint:{key:"version" value:"v1"}} |
| 817 | ]}</code> |
| 818 | </td> |
| 819 | <td> |
| 820 | <ul> |
| 821 | <li>does <i>not</i> include the route for <code>env=prod</code> |
| 822 | <li>does include the route for <code>version=v1</code> |
| 823 | </ul> |
| 824 | </td> |
| 825 | </tr> |
| 826 | |
| 827 | <tr> |
| 828 | <td> |
| 829 | <code>{and_constraints:[ |
| 830 | {constraint:{key:"env" value:"prod"}}, |
| 831 | {constraint:{key:"version" value:"v1"}} |
| 832 | ]}</code> |
| 833 | </td> |
| 834 | <td> |
| 835 | <ul> |
| 836 | <li>does include the route for <code>env=prod</code> |
| 837 | <li>does include the route for <code>version=v1</code> |
| 838 | </ul> |
| 839 | </td> |
| 840 | </tr> |
| 841 | |
| 842 | </table> |
| 843 | |
| 844 | ## Rationale |
| 845 | |
| 846 | This section documents limitations and design alternatives that we |
| 847 | considered. |
| 848 | |
| 849 | ### Limitation on Enhancing Matching in the Future |
| 850 | |
| 851 | One limitation of this design is that, because all xDS transport protocol |
| 852 | implementations (clients, servers, and caching proxies) need to implement |
| 853 | this matching behavior, it will be very difficult to add new matching |
| 854 | behavior in the future. Doing so will probably require some sort of |
| 855 | client capability. This will make it feasible to expand this mechanism |
| 856 | in an environment where all of the caching xDS proxies are under centralized |
| 857 | control, but it will be quite difficult to deploy those changes in |
| 858 | environments that depend on distributed third-party caching xDS proxies. |
| 859 | |
| 860 | Because of this, reviewers of this design are encouraged to carefully |
| 861 | scrutinize the proposed matching semantics to ensure that they meet our |
| 862 | expected needs. |
| 863 | |
| 864 | ### Complexity of Constraint Expressions |
| 865 | |
| 866 | Although the `DynamicParameterConstraints` proto allows specifying |
| 867 | arbitrarily nested combinations of AND, OR, and NOT expressions, control |
| 868 | planes do not need to actually support that full arbitrary power. It is |
| 869 | possible to limit the sets of supported constraints to (e.g.) a |
| 870 | simple flat list of AND or OR expressions, which would make it easier |
| 871 | for a control plane to optimize its implementation. |
| 872 | |
| 873 | Simimarly, caching xDS proxies may be able to provide an optimized |
| 874 | implementation if all of the constraints that they see are limited to |
| 875 | some subset of the full flexibility allowed by the protocol. However, |
| 876 | any general-purpose caching proxy implementation will likely need to |
| 877 | support a less optimized implementation that does support the full |
| 878 | flexibility allowed by the protocol. |
| 879 | |
| 880 | ### Using Context Parameters |
| 881 | |
| 882 | We considered extending the context parameter mechanism from [xRFC |
| 883 | TP1](TP1-xds-transport-next.md) to support flexible matching semantics, |
| 884 | rather that its current exact-match semantics. However, that approach had |
| 885 | some down-sides: |
| 886 | - It would not have solved the virality problem described in the "Background" |
| 887 | section above. |
| 888 | - It would have made the new xDS naming scheme a prerequisite for using |
| 889 | the dynamic resource selection mechanism. (The mechanism described in |
| 890 | this doc is completely independent of the new xDS naming scheme; it can |
| 891 | be used with the legacy xDS naming scheme as well.) |
| 892 | |
| 893 | ### Stricter Matching to Avoid Ambiguity |
| 894 | |
| 895 | We could avoid much of the matching ambiguity described above by saying that |
| 896 | a set of constraints must specify all keys present in the subscription |
| 897 | request in order to match. However, this would mean that if the client |
| 898 | starts subscribing with a new key before the corresponding constraint is |
| 899 | added on the resources on the server, then it will fail to match the |
| 900 | existing resources. In other words, the process would be: |
| 901 | |
| 902 | 1. Add a variant of all resources on the server side with a constraint |
| 903 | for `version=v1` (in addition to all existing constraints). |
| 904 | 2. Change clients to start sending the new key. |
| 905 | 3. When all clients are updated, remove the resource variants that do |
| 906 | *not* have the new key. |
| 907 | |
| 908 | This will effectively require adding new keys on the server side first, |
| 909 | which seems like a large burden on users. It also seems fairly tricky |
| 910 | for most users to get the exactly correct set of dynamic parameters on |
| 911 | each resource variant, and if they fail to do it right, they will break |
| 912 | their existing configuration. |
| 913 | |
| 914 | Ultimately, although this approach is more semantically precise, it is |
| 915 | also considered too rigid and difficult for users to work with. |
| 916 | |
| 917 | ## Implementation |
| 918 | |
| 919 | TBD (Will probably be implemented in gRPC before Envoy) |
| 920 | |
| 921 | ## Open issues (if applicable) |
| 922 | |
| 923 | N/A |