Class ConnectionHandler (0.21.0)

public class ConnectionHandler extends Thread

Handles a connection from a client to Spanner. This ConnectionHandler uses WireMessage to receive and send all messages from and to the client, using the intermediate representation IntermediateStatement that servers as a middle layer between Postgres and Spanner.

Each ConnectionHandler is also a Thread. Although a TCP connection does not necessarily need to have its own thread, this makes the implementation more straightforward.

Inheritance

java.lang.Object > Thread > ConnectionHandler

Methods

cancelActiveStatement(int connectionId, int secret)

public boolean cancelActiveStatement(int connectionId, int secret)

To be used by a cancellation command to cancel a currently running statement, as contained in a specific connection identified by connectionId. Since cancellation is a flimsy contract at best, it is not imperative that the cancellation run, but it should be attempted nonetheless.

Parameters
Name Description
connectionId int

The connection whose statement must be cancelled.

secret int

The secret value linked to the connection that is being cancelled. If it does not match, we cannot cancel.

Returns
Type Description
boolean

true if the statement was cancelled.

cleanUp(IntermediateStatement statement)

public void cleanUp(IntermediateStatement statement)

Closes portals and statements if the result of an execute was the end of a transaction.

Parameter
Name Description
statement IntermediateStatement
Exceptions
Type Description
Exception

clearActiveCopyStatement()

public void clearActiveCopyStatement()

clearInvalidMessageCount()

public void clearInvalidMessageCount()

Clears the number of invalid messages that was received. This is called whenever a valid message is encountered.

closeAllPortals()

public void closeAllPortals()

Closes all named and unnamed portals on this connection.

closeAllStatements()

public void closeAllStatements()

closePortal(String portalName)

public void closePortal(String portalName)
Parameter
Name Description
portalName String
Exceptions
Type Description
Exception

closeStatement(String statementName)

public void closeStatement(String statementName)
Parameter
Name Description
statementName String

connectToSpanner(String database, Credentials credentials)

public void connectToSpanner(String database, Credentials credentials)
Parameters
Name Description
database String
credentials com.google.auth.Credentials

getActiveCopyStatement()

public CopyStatement getActiveCopyStatement()
Returns
Type Description
CopyStatement

getAutoDescribedStatement(String sql)

public Future<DescribeResult> getAutoDescribedStatement(String sql)

Returns the parameter types of a cached auto-described statement, or null if none is available in the cache.

Parameter
Name Description
sql String
Returns
Type Description
Future<DescribeResult>

getConnectionId()

public int getConnectionId()
Returns
Type Description
int

getConnectionMetadata()

public ConnectionMetadata getConnectionMetadata()
Returns
Type Description
ConnectionMetadata

getDatabaseId()

public DatabaseId getDatabaseId()
Returns
Type Description
com.google.cloud.spanner.DatabaseId

getExtendedQueryProtocolHandler()

public ExtendedQueryProtocolHandler getExtendedQueryProtocolHandler()
Returns
Type Description
ExtendedQueryProtocolHandler

getInvalidMessageCount()

public int getInvalidMessageCount()

Returns the number of invalid messages that this connection has received in a row. This can for example happen if a client has initiated a COPY operation and the copy operation fails on the server. The server will then respond with an error response, but if the client fails to read that message and continues to send copy data messages, the server could get flooded. This value is used to detect such a situation and breaks the connection if too many invalid messages in a row are received.

Returns
Type Description
int

getPortal(String portalName)

public IntermediatePortalStatement getPortal(String portalName)
Parameter
Name Description
portalName String
Returns
Type Description
IntermediatePortalStatement

getSecret()

public int getSecret()
Returns
Type Description
int

getServer()

public ProxyServer getServer()
Returns
Type Description
ProxyServer

getSpannerConnection()

public Connection getSpannerConnection()
Returns
Type Description
Connection

getStatement(String statementName)

public IntermediatePreparedStatement getStatement(String statementName)
Parameter
Name Description
statementName String
Returns
Type Description
IntermediatePreparedStatement

getStatus()

public synchronized ConnectionHandler.ConnectionStatus getStatus()
Returns
Type Description
ConnectionHandler.ConnectionStatus

getWellKnownClient()

public ClientAutoDetector.WellKnownClient getWellKnownClient()
Returns
Type Description
ClientAutoDetector.WellKnownClient

handleMessages()

public void handleMessages()

Reads and handles wire-protocol messages. This method is normally only called from this ConnectionHandler, but certain sub-protocols such as the COPY protocol also need to process messages in line.

Exceptions
Type Description
Exception

handleTerminate()

public void handleTerminate()

Called when a Terminate message is received. This closes this ConnectionHandler.

hasPortal(String portalName)

public boolean hasPortal(String portalName)
Parameter
Name Description
portalName String
Returns
Type Description
boolean

hasStatement(String statementName)

public boolean hasStatement(String statementName)
Parameter
Name Description
statementName String
Returns
Type Description
boolean

increaseInvalidMessageCount()

public void increaseInvalidMessageCount()

Increases the number of invalid messages that was received in a row by 1.

maybeDetermineWellKnownClient(Statement statement)

public void maybeDetermineWellKnownClient(Statement statement)

This is called by the simple com.google.cloud.spanner.pgadapter.wireprotocol.QueryMessage to give the connection the opportunity to determine the client that is connected based on the SQL string that is being executed.

Parameter
Name Description
statement com.google.cloud.spanner.Statement

maybeDetermineWellKnownClient(ParseMessage parseMessage)

public void maybeDetermineWellKnownClient(ParseMessage parseMessage)

This is called by the extended query protocol com.google.cloud.spanner.pgadapter.wireprotocol.ParseMessage to give the connection the opportunity to determine the client that is connected based on the data in the (first) parse messages.

Parameter
Name Description
parseMessage ParseMessage

registerAutoDescribedStatement(String sql, Future<DescribeResult> describeResult)

public void registerAutoDescribedStatement(String sql, Future<DescribeResult> describeResult)

Stores the parameter types of an auto-described statement in the cache.

Parameters
Name Description
sql String
describeResult Future<DescribeResult>

registerPortal(String portalName, IntermediatePortalStatement portal)

public void registerPortal(String portalName, IntermediatePortalStatement portal)
Parameters
Name Description
portalName String
portal IntermediatePortalStatement

registerStatement(String statementName, IntermediatePreparedStatement statement)

public void registerStatement(String statementName, IntermediatePreparedStatement statement)
Parameters
Name Description
statementName String
statement IntermediatePreparedStatement

run()

public void run()

Simple runner starts a loop which keeps taking inbound messages, processing them, sending them to Spanner, getting a result, processing that result, and replying to the client (in that order). Also instantiates input and output streams from the client and handles auth.

Overrides

setActiveCopyStatement(CopyStatement copyStatement)

public void setActiveCopyStatement(CopyStatement copyStatement)
Parameter
Name Description
copyStatement CopyStatement

setMessageState(WireMessage message)

public void setMessageState(WireMessage message)
Parameter
Name Description
message WireMessage

setStatus(ConnectionHandler.ConnectionStatus status)

public synchronized void setStatus(ConnectionHandler.ConnectionStatus status)
Parameter
Name Description
status ConnectionHandler.ConnectionStatus

setWellKnownClient(ClientAutoDetector.WellKnownClient wellKnownClient)

public void setWellKnownClient(ClientAutoDetector.WellKnownClient wellKnownClient)
Parameter
Name Description
wellKnownClient ClientAutoDetector.WellKnownClient