-
Notifications
You must be signed in to change notification settings - Fork 0
Message Flow Overview
Garth Goodson edited this page Dec 11, 2025
·
1 revision
The ClientSession and ServerSession classes in the Springtail project facilitate communication between the client and the database server. This communication is essential for managing queries, state transitions, and failover scenarios.
-
Manages Client Connections: Handles incoming client requests and routes them to the appropriate
ServerSession. - State Management: Tracks the state of the client connection (e.g., STARTUP, READY, QUERY).
- Failover Handling: Manages failover notifications and transitions to replica servers when necessary.
-
Query Parsing and Execution: Processes client queries and forwards them to the
ServerSession.
- Manages Server Connections: Handles communication with the database server.
-
Batch Processing: Queues and processes batches of messages from the
ClientSession. - Error Recovery: Implements mechanisms to recover from errors and maintain session integrity.
- State Management: Tracks the state of the server connection (e.g., STARTUP, DEPENDENCIES, QUERY).
-
Client Request Handling:
- The
ClientSessionreceives a request from the client. - It parses the request and determines the appropriate action (e.g., query execution, transaction management).
- The
-
Server Session Selection:
- The
ClientSessionselects or creates aServerSessionbased on the request type and session state. - It may reuse an existing
ServerSessionor create a new one for failover scenarios.
- The
-
Message Forwarding:
- The
ClientSessionforwards the parsed query or command to theServerSession. - The
ServerSessionprocesses the message and communicates with the database server.
- The
-
Response Handling:
- The
ServerSessionreceives the response from the database server. - It forwards the response back to the
ClientSession, which sends it to the client.
- The
-
Error and Failover Management:
- If an error occurs, the
ServerSessionattempts to recover and notifies theClientSession. - In failover scenarios, the
ClientSessiontransitions to a replica server and replays pending state.
- If an error occurs, the
This section describes how server-side connections are accepted, scheduled, processed, and how replies are returned to the client.
-
Connection Acceptance:
- Server accepts new TCP connections and wraps them in connection objects
- Creates an in-memory session to track the socket and connection state
- Registers the session with the event infrastructure for monitoring
-
Event Detection:
- Main event loop monitors registered sockets for readability
- When a socket becomes readable, the client session associated with socket is obtained
- All sockets associated with the client session (the client session and all server session sockets) are removed from the poll set
- The client session is added to a runnable set; if the server session is in a RESET state, then there is no associated client session and it is added to the runnable set
-
Worker Processing
- Runnable sessions are submitted to a worker pool
- A worker thread picks up the session and begins processing
- Worker reads incoming bytes, assembles protocol messages, and processes them
-
Message Handling
- Client session first checks for data on the client session socket, then for data on the server session sockets
- Client-facing sessions interpret the client protocol
- Parse message payloads and forward commands to the server-side session
- Manage transactional state and buffering as required
-
Reply Delivery
- Server-side session receives responses from the database; the server session socket will have data available
- Prepares outgoing buffers and writes to the client's socket
- All traffic is logged by the request/response logging subsystem
-
Session Re-queueing
- Worker clears temporary state after processing
- Re-registers the session with the event loop if connection remains open
- Performs cleanup if the session is closing or errors occur