Skip to content

Routing rules

Trino Gateway includes a routing rules engine.

By default, Trino Gateway reads the X-Trino-Routing-Group request header to route requests. Use the defaultRoutingGroup setting to specify a fallback group, defaults to the adhoc group.

routing:
    defaultRoutingGroup: "test-group"

Trino Gateway sends a request to the defaultRoutingGroup in two cases, each controlled by its own setting:

  • unmatchedRequestFallbackEnabled: a query submission matches no routing rule or routing group header. When set to false, such a query submission is rejected with HTTP 400, so every query submission must be explicitly routed. Other proxied requests, such as Web UI and OAuth2 requests, still go to the default routing group.
  • unavailableGroupFallbackEnabled: the request's routing group has no healthy backend, including a group with no backends configured. When set to false, such a request is rejected with HTTP 503 instead of running on a backend outside the group it asked for.

Both default to true. For example, to keep sending unmatched requests to the default group while never letting a request for a specific group spill over into it:

routing:
    unavailableGroupFallbackEnabled: false

The routing rules engine feature enables you to either write custom logic to route requests based on the request info such as any of the request headers, or set a URL address to make an HTTP POST request and route based on the returned result.

Routing rules are defined in a configuration file or implemented in separate, custom service application. The connection to the separate service is configured as a URL. It can implement any dynamic rule changes or other behavior.

Enabling the routing rules engine

To enable the routing rules engine, find the following lines in config.yaml:

  • Set rulesEngineEnabled to true, then rulesType as FILE or EXTERNAL.
  • If you set rulesType: FILE, then set rulesConfigPath to the path to your rules config file.
  • The rules file will be re-read every minute by default. You may change this by setting rulesRefreshPeriod: Duration, where duration is an airlift style Duration such as 30s.
  • If you set rulesType: EXTERNAL, set rulesExternalConfiguration to the URL of an external service for routing rules processing.
  • rulesType is by default FILE unless specified.
routingRules:
    rulesEngineEnabled: true
    rulesType: FILE
    rulesConfigPath: "app/config/routing_rules.yml" # replace with actual path to your rules config file
    rulesExternalConfiguration:
        urlPath: https://router.example.com/gateway-rules # replace with your own API path
        excludeHeaders:
            - 'Authorization'
            - 'Accept-Encoding'
        propagateErrors: false
  • Redirect URLs are not supported.
  • Optionally, add headers to the excludeHeaders list to exclude requests with corresponding header values from being sent in the POST request.
  • Check headers to exclude when making API requests, specifics depend on the network configuration.
  • Set propagateErrors to true to forward routing service errors to
    clients if present in the response.

If there is error parsing the routing rules configuration file, an error is logged, and requests are routed using the routing group header X-Trino-Routing-Group as default.

Configuring API requests with HTTP client config

You can configure the HTTP client by adding the following configuration to the serverConfig: section with the router prefix.

serverConfig:
    router.http-client.request-timeout: 1s

Please refer to the Trino HTTP client properties documentation for more.

Use an external service for routing rules

You can use an external service for processing your routing by setting the rulesType to EXTERNAL and configuring the rulesExternalConfiguration.

Trino Gateway then sends all headers, other than those specified in excludeHeaders, as a map in the body of a POST request to the external service. If requestAnalyzerConfig.analyzeRequest is set to true, TrinoRequestUser and TrinoQueryProperties are also included. Trino Gateway trusts the external service with these headers, including credentials, as described in Trusted external routing service.

Additionally, the following HTTP information is included:

  • remoteUser
  • method
  • requestURI
  • queryString
  • session
  • remoteAddr
  • remoteHost
  • parameterMap

The external service can process the information in any way desired and must return a result with the following criteria:

  • Response status code of OK (200)
  • Message in JSON format
  • Only one group can be returned
  • If errors is not null, the query is routed to the configured default group

Request headers modification

The external routing service can optionally return an externalHeaders map in its response to add or modify HTTP headers before the request is forwarded.

This enables dynamic customization of request behavior, such as injecting session properties or setting client tags before the request reaches the Trino cluster.

{
    "routingGroup": "test-group",
    "errors": [
        "Error1",
        "Error2",
        "Error3"
    ],
    "externalHeaders": {
        "x-trino-client-tags": "['etl']",
        "x-trino-session": "query_max_memory=50GB,optimize_metadata_queries=false"
    }
}

Configure routing rules with a file

Rules consist of a name, description, condition, and list of actions. If the condition of a particular rule evaluates to true, its actions are fired. Rules are stored as a multi-document YAML file.

---
name: "airflow"
description: "if query from airflow, route to etl group"
condition: 'request.getHeader("X-Trino-Source") == "airflow"'
actions:
  - 'result.put("routingGroup", "etl")'
---
name: "airflow special"
description: "if query from airflow with special label, route to etl-special group"
condition: 'request.getHeader("X-Trino-Source") == "airflow" && request.getHeader("X-Trino-Client-Tags") contains "label=special"'
actions:
  - 'result.put("routingGroup", "etl-special")'

Three objects are available by default. They are

  • request, the incoming request as an HttpServletRequest
  • state, a HashMap<String, Object> that allows passing arbitrary state from one rule evaluation to the next
  • result, a HashMap<String, String> that is used to return the result of rule evaluation to the engine

In addition to the default objects, rules may optionally utilize trinoRequestUser and trinoQueryProperties , which provide information about the user and query respectively. You must include an action of the form result.put(\"routingGroup\", \"foo\") to trigger routing of a request that satisfies the condition to the specific routing group. Without this action, the configured default group is used and the whole routing rule is redundant.

The condition and actions are written in MVEL, an expression language with Java-like syntax. Classes from java.util, data-type classes from java.lang such as Integer and String, as well as java.lang.Math and java.lang.StrictMath are available in rules. The engine rejects expressions that use reflection, class loading, or process and runtime access, such as getClass, Class.forName, Runtime, and ProcessBuilder. It also rejects import statements and package-qualified names of other classes, such as java.io.File. An expression that uses any of these constructs is not accepted through the API and is not loaded from the file. This check runs when a rule is submitted through the API or loaded from the file, not when a request is routed, so it adds no per-request routing overhead.

MVEL expressions are functionally equivalent to Java, and MVEL does not provide a sandbox. Rejecting reflection and runtime access is hardening that blocks the common escape routes, not a guarantee of isolation. Treat the ability to author routing rules as equivalent to running code inside the Trino Gateway process, and restrict the ADMIN role and write access to the rules file accordingly.

In most cases, you can write conditions and actions in Java syntax and expect it to work. One exception is parametrized types. Exclude type parameters, for example to add a HashSet to the state variable, use an action such as:

actions:
  - |
    state.put("triggeredRules",new HashSet())
This is equivalent to new HashSet<Object>().

There are some MVEL-specific operators. For example, instead of doing a null-check before accessing the String.contains method like this:

condition: 'request.getHeader("X-Trino-Client-Tags") != null && request.getHeader("X-Trino-Client-Tags").contains("label=foo")'

You can use the contains operator

condition: 'request.getHeader("X-Trino-Client-Tags") contains "label=foo"'

If no rules match, then the request is routed to the configured default routing group.

TrinoStatus

The TrinoStatus class attempts to track the current state of the configured Trino clusters. The three possible states of these cluster are updated with every healthcheck:

  • PENDING: A Trino cluster shows this state when it is still starting up. It is treated as unhealthy by RoutingManager, and therefore requests are not be routed to these clusters.
  • HEALTHY: A Trino cluster shows this state when healthchecks report the cluster as healthy and ready. RoutingManager only routes requests to healthy clusters.
  • UNHEALTHY: A Trino cluster shows this state when healthchecks report the cluster as unhealthy. RoutingManager does not route requests to unhealthy clusters.

TrinoRequestUser

The TrinoRequestUser class attempts to extract user information from a request, in the following order:

  1. Mapped identity of a client certificate, if the client certificate JWT bridge is configured.
  2. X-Trino-User header.
  3. Authorization: Basic header.
  4. Authorization: Bearer header, as described in the following paragraph.
  5. Trino-UI-Token or __Secure-Trino-ID-Token cookie. The Trino web UI sets Trino-UI-Token when a user logs in to it through Trino Gateway with a password, and __Secure-Trino-ID-Token when the user logs in with OAuth 2.0. The browser stores them for the Trino Gateway host, and sends them with the following requests to the Trino web UI under /ui. Trino Gateway reads the tokenUserField claim from them without verifying the token. If both cookies are present, only Trino-UI-Token is read. Trino-UI-Token has the user name only in the sub claim, so no user is extracted from it with the default tokenUserField of email.

Kerberos authentication is not supported. If the request contains the Authorization: Bearer header, an attempt is made to treat the token as a JWT and deserialize it. If this is successful, the value of the claim named in requestAnalyzerConfig.tokenUserField is used as the username. By default, this is the email claim. Trino Gateway does not verify the signature of the JWT. If the token is a JWE issued by Trino, and requestAnalyzerConfig.oauthRefreshTokensSecretKey is configured, then the token is decrypted and the same claim is used. If no user is found this way, and requestAnalyzerConfig.oauthTokenInfoUrl is configured, then the token is exchanged with the Info URL, and the value of the claim named in requestAnalyzerConfig.oauthTokenInfoUserField is used as the username. By default, and when the response has no such claim, this is the sub claim. Responses are cached until a token is unused for 10 minutes, to avoid triggering rate limits.

You may call trinoRequestUser.getUser() and trinoRequestUser.getUserInfo() in your routing rules. If user information was not successfully extracted, trinoRequestUser.getUser() returns an empty Optional. trinoRequestUser.getUserInfo() returns an Optional<UserInfo>, with an OpenID Connect UserInfo if a token is successfully exchanged with the oauthTokenInfoUrl, and an empty Optional otherwise.

trinoRequestUser.userExistsAndEquals("usernameToTest") can be used to check a username against the extracted user. It returns false if a user has not been extracted.

User extraction is only available if enabled by configuring requestAnalyzerConfig.analyzeRequest = true

TrinoQueryProperties

The TrinoQueryProperties class attempts to parse the body of a request to determine the SQL statement and other information. Note that only a syntactic analysis is performed.

If a query references a view, then that view is not expanded, and tables referenced by the view are not recognized. Views and materialized views are treated as tables and added to the list of tables in all contexts, including statements such as CREATE VIEW.

A routing rule can call the following methods on the trinoQueryProperties object:

  • String errorMessage(): the error message, only if there was any error while creating the trinoQueryProperties object.
  • boolean isNewQuerySubmission(): boolean flag to indicate if the request is a POST to the v1/statement query endpoint.
  • String getQueryType(): the class name of the Statement, e.g. ShowCreate. Note that these are not mapped to the ResourceGroup query types. For a full list of potential query types, see the classes in STATEMENT_QUERY_TYPES
  • String getResourceGroupQueryType(): the Resource Group query type, for example SELECT, DATA_DEFINITION. For a full list see queryType in the Trino documentation
  • String getDefaultCatalog(): the default catalog, if set. It may or may not be referenced in the actual SQL
  • String getDefaultSchema(): the default schema, if set. It may or may not be referenced in the actual SQL
  • Set<String> getCatalogs(): the set of catalogs used in the query. Includes the default catalog if used by a non-fully qualified table reference
  • Set<String> getSchemas(): the set of schemas used in the query. Includes the default schema if used by a non-fully qualified table reference
  • Set<String> getCatalogSchemas() the set of qualified schemas used in the query, in the form catalog.schema
  • boolean tablesContains(String testName) returns true if the query contains a reference to the table testName.testName should be fully qualified, for example testcat.testschema.testtable
  • Set<QualifiedName> getTables(): the set of tables used in the query. These are fully qualified. Trino Gateway qualifies a partially qualified table reference in the SQL with the default catalog and schema. References that cannot be qualified are listed in getUnqualifiedNames() instead.
  • Map<String, Set<String>> getUnqualifiedNames(): names that cannot be qualified, because the request has no default catalog or schema for them. The map is keyed by the kind of reference: catalog, schema, table, function, or procedure. Each name keeps the parts written in the SQL, in the same form as getTables(), and is empty when the statement names nothing. For example:

    • {"table": ["my_table", "my_schema.my_table"]} for table references.
    • {"function": ["system.query"]} for a table function.
    • {"schema": [""]} for SHOW TABLES.

    Trino can still resolve these names with its server defaults, set in sql.default-catalog and sql.default-schema, so they do not cause query parsing to fail. They are not included in getTables(), getCatalogs(), or getSchemas(). An empty map means that every reference Trino Gateway extracts is qualified. A single-part table function name, such as exclude_columns, is neither listed here nor included in getTables(), because Trino resolves it through the SQL path, which Trino Gateway does not read.

  • String getBody(): the raw request body. For EXECUTE and EXECUTE IMMEDIATE, the statement being executed, with its parameter markers

  • Optional<String> getBoundBody(): for EXECUTE and EXECUTE IMMEDIATE, the statement in getBody() with the USING parameters bound in place of its parameter markers, for example SELECT * FROM t WHERE a = ? with USING 'x' becomes SELECT * FROM t WHERE (a = 'x'), spread over several lines. The statement is formatted with the Trino SQL formatter, so comments are dropped and the formatting differs from getBody(). An integer bound to an ORDER BY or GROUP BY marker is written as CAST(2 AS bigint), because Trino reads a plain 2 there as an output column position. Empty for other statements, when the number of parameters does not match the markers, or when the bound statement cannot be written as SQL, for example LIMIT ? bound to CAST(10 AS bigint)

Configuration

The trinoQueryProperties and the trinoRequestUser are configured under the requestAnalyzerConfig configuration node.

analyzeRequest:

Set to true to make trinoQueryProperties and trinoRequestUser available.

maxBodySize:

By default, the max body size is 1,000,000 characters. This can be modified by configuring maxBodySize. If the request body is greater or equal to this limit, Trino Gateway does not process the query. A buffer of length maxBodySize is allocated per query. Reduce this value if you observe excessive garbage collection at runtime. maxBodySize cannot be set to values larger than 2**31-1, the maximum size of a Java String.

Trino Gateway also does not process queries that nest parentheses, brackets, or CASE expressions more than 1,000 levels deep. For these queries, isQueryParsingSuccessful() returns false, and errorMessage() names the nesting depth.

isClientsUseV2Format:

Some commercial extensions to Trino use the V2 Request Structure V2 style request structure. Support for V2-style requests can be enabled by setting this property to true. If you use a commercial version of Trino, ask your vendor how to set this configuration.

tokenUserField:

When extracting the user from a JWT token, this field is used as the username. By default, the email claim is used.

oauthTokenInfoUrl:

If configured, Trino Gateway attempts to retrieve the user info by exchanging potential authorization tokens with this URL. Responses are cached for 10 minutes to avoid triggering rate limits.

oauthRequestTimeout:

Connect and read timeout of the request to oauthTokenInfoUrl. If the request fails or times out, no user is extracted from the token. Defaults to 5s.

oauthTokenInfoUserField:

When extracting the user from the response of oauthTokenInfoUrl, this claim is used as the username. If the claim is missing, blank, or not a string, the sub claim is used. Defaults to sub.

oauthRefreshTokensSecretKey:

If configured, Trino Gateway decrypts the tokens that Trino issues when OAuth2 refresh tokens are enabled, and uses the tokenUserField claim as the username.

Use the same key as http-server.authentication.oauth2.refresh-tokens.secret-key on the Trino coordinators, a base64-encoded key with 128, 192, or 256 bits. All coordinators must set this key to the same value. Set tokenUserField to the same claim as http-server.authentication.oauth2.principal-field. The key decrypts the tokens of all users. Keep it secret, for example in an environment variable.

Execution of rules

All rules whose conditions are satisfied fire. For example, in the "airflow" and "airflow special" example rules from the following rule priority section, a query with source airflow and label special satisfies both rules. The routingGroup is set to etl and then to etl-special because of the order in which the rules of defined. If you swap the order of the rules, then you get etl instead.

You can avoid this ordering issue by writing atomic rules, so any query matches exactly one rule. For example you can change the first rule to the following:

---
name: "airflow"
description: "if query from airflow, route to etl group"
condition: 'request.getHeader("X-Trino-Source") == "airflow" && request.getHeader("X-Trino-Client-Tags") == null'
actions:
  - 'result.put("routingGroup", "etl")'
---

This can difficult to maintain with more rules. To have better control over the execution of rules, we can use rule priorities. Overall, priorities and other constructs that MVEL support allows you to express your routing logic.

Rule priority

You can assign an integer value priority to a rule. The lower this integer is, the earlier it fires. If the priority is not specified, the priority defaults to INT_MAX. Following is an example with priorities:

---
name: "airflow"
description: "if query from airflow, route to etl group"
priority: 0
condition: 'request.getHeader("X-Trino-Source") == "airflow"'
actions:
  - 'result.put("routingGroup", "etl")'
---
name: "airflow special"
description: "if query from airflow with special label, route to etl-special group"
priority: 1
condition: 'request.getHeader("X-Trino-Source") == "airflow" && request.getHeader("X-Trino-Client-Tags") contains "label=special"'
actions:
  - 'result.put("routingGroup", "etl-special")'

Note that both rules still fire. The difference is that you are guaranteed that the first rule (priority 0) is fired before the second rule (priority 1). Thus routingGroup is set to etl and then to etl-special, so the routingGroup is always etl-special in the end.

More specific rules must be set to a higher priority so they are evaluated last to set a routingGroup.

Passing State

The state object may be used to pass information from one rule evaluation to the next. This allows an author to avoid duplicating logic in multiple rules. Priority should be used to ensure that state is updated before being used in downstream rules. For example, the atomic rules may be re-implemented as

---
name: "initialize state"
description: "Add a set to the state map to track rules that have evaluated to true"
priority: 0
condition: "true"
actions:
  - |
    state.put("triggeredRules",new HashSet())
  # MVEL does not support type parameters! HashSet<String>() would result in an error.
---
name: "airflow"
description: "if query from airflow, route to etl group"
priority: 1
condition: |
  request.getHeader("X-Trino-Source") == "airflow"
actions:
  - |
    result.put("routingGroup", "etl")
  - |
    state.get("triggeredRules").add("airflow")
---
name: "airflow special"
description: "if query from airflow with special label, route to etl-special group"
priority: 2
condition: |
  state.get("triggeredRules").contains("airflow") && request.getHeader("X-Trino-Client-Tags") contains "label=special"
actions:
  - |
    result.put("routingGroup", "etl-special")
If statements (MVEL Flow Control)

You can use MVEL support for if statements and other flow control. The following logic in pseudocode:

if source == "airflow":
  if clientTags["label"] == "foo":
    return "etl-foo"
  else if clientTags["label"] = "bar":
    return "etl-bar"
  else
    return "etl"

This logic Can be implemented with the following rules:

---
name: "airflow rules"
description: "if query from airflow"
condition: "request.getHeader(\"X-Trino-Source\") == \"airflow\""
actions:
  - "if (request.getHeader(\"X-Trino-Client-Tags\") contains \"label=foo\") {
      result.put(\"routingGroup\", \"etl-foo\")
    }
    else if (request.getHeader(\"X-Trino-Client-Tags\") contains \"label=bar\") {
      result.put(\"routingGroup\", \"etl-bar\")
    }
    else {
      result.put(\"routingGroup\", \"etl\")
    }"