Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


IDIEP-126
Author
Sponsor
Created 10.09.2024
Status
Status
colourGrey
titleDRAFT


Table of Contents

Motivation

When processing data, the user wants to have access to client attributes - an associative array specified on the client application side. Usage scenarios:

  1. To run certain business logic that depends on the value of the current session attribute (for example, the application language [2]).
  2. To audit changes in the database (for example, CacheInterceptor extracts the user from the session attrubutes and saves it along with the data).
  3. To optimize the transfer of parameters to processing functions (for example, multiple QuerySqlFunction use the same parameter and it is convenient to take it from the contextattributes, rather than transfer it separately to each function).

...

Ignite does not have a similar public API:

  1. ServiceCallContext helps solve similar problems, but has several limitations. The main one is that it is only available in calls via the IgniteService API and cannot be used for single SQL queries from the jdbc side.
  2. UserAttributes - allows you to set user attributes, but the attribute values ​​are fixed for the entire lifetime of the connection.

A similar context mechanism exists in Oracle [1] and is used by some of our customers. Implementing such an API would help users migrate applications from Oracle to Ignite.

Description

...

].

Description

Accessing attributes

Code running on the Ignite node has access to client attributes through a static method call:

Code Block
languagejava
/** SessionContext interface */
public class SessionContext {
    /** @return Client attributes set for current thread. */
    public static @Nullable Map<String, String> getClientAttributes();
 }
 
/** Example, use it in QuerySqlFunction. */
public static class UserDefinedFunctions {
    /** @return Session ID, client attribute. */
    @QuerySqlFunction
    public static String sessionId() {
        Map<String, String> clnAttrs = SessionContext.getClientAttributes();
 
        return clnAttrs == null ? null : clnInfo.getProperty(SESSION_ID);
    }
}

SessionContext can be further extended to retrieve other non-tabular information, such as SecuritySubject, Transaction, ServiceCallContext, etc.

Setting attributes

JDBC

The standard jdbc protocol describes the methods Connection#setClientInfo, which allow changing the values ​​of client attributes during the life of the connection [3]. Implementation features:

  1. The list of attributes that can be set using #setClientInfo is arbitrary. The documentation recommends strictly limiting the set of attributes, but this is not necessary. For example, Oracle does not have such a limitation [5]. The setClientInfo array is completely translated into the getClientAttributes array.
  2. The parameters set in setClientInfo are passed along with each JdbcRequest (this requires a new feature in JdbcThinFeature).
  3. The client must reset the set values ​​itself.


Code Block
languagejava
// JDBC connection to Ignite server.
try (Connection conn = DriverManager.getConnection(URL)) {
    conn.setClientInfo("SESSION_ID", "1234");
 
    ...
}

IgniteClient (Java)

Implementation features:

  1. The Java implementation of the thin client allows working in a multi-threaded environment, unlike the jdbc connection. It is suggested to bind the ClientAttributes setting to the current thread. In case of using listeners, it is necessary to reinstall the context in a new thread.
  2. The parameters set via the withClientAttributes call are sent along with each outgoing TcpClientChannel#send message in the BinaryOutputStream (a new feature is needed in the ClientBitmaskFeature protocol).


Code Block
languagejava
try (IgniteClient cln = Ignition.startClient(clnCfg)) {
    try (ClientAttributes clnAttrs = cln.withClientAttributes()) {
        clnAttrs.setAttribute("SESSION_ID", "1234");
 
        ...
    }
}

Ignite 

The Ignite server node, when receiving client messages, sets ClientAttributes in the ThreadLocal variable, and also handles context transfer to the calls it spawns (in threads and on remote nodes). The same can be done for operations called directly from Ignite:

Code Block
languagejava
try (Ignite ign = Ignition.localIgnite()) {
    try (ClientAttributes clnAttrs = ign.withClientAttributes()) {
        clnAttrs.setAttribute("SESSION_ID", "1234");
 
        ...
    }
}

Spreading context across cluster nodes

  1. Via the QueryStartRequest SQL message protocol.
  2. Via the discovery protocol for handling inserts (CacheInterceptor), since inserts are not always performed on the request initiator node.
    1. GridNearAtomicAbstractUpdateRequest messages about key changes must contain the ClientAttributes id.
    2. Can only be sent if the SQL query has an insert (since CacheInterceptor#onGet is not called in SQL)
    3. It is necessary to clean up contexts on nodes (after completing queries, disconnecting clients, or changing ClientAttributes on the client)

Risks and Assumptions

// Describe project risks, such as API or binary compatibility issues, major protocol changes, etc.

...