Class ConnectionHandler (0.6.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

addActiveStatement(IntermediateStatement statement)

public synchronized void addActiveStatement(IntermediateStatement statement)

Add a currently executing statement to a buffer. This is only used in case a statement in flight is cancelled. It must be saved separately, as a new connection is spawned to issue a cancellation (as per Postgres protocol standard). This means some sort of IPC is required, which in this case is a global Map.

Parameter
NameDescription
statementIntermediateStatement

Currently executing statement to be saved.

cancelActiveStatement(int connectionId, int secret)

public synchronized void 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
NameDescription
connectionIdint

The connection owhose statement must be cancelled.

secretint

The secret value linked to this connection. If it does not match, we cannot cancel.

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
NameDescription
statementIntermediateStatement
Exceptions
TypeDescription
Exception

clearInvalidMessageCount()

public void clearInvalidMessageCount()

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

closePortal(String portalName)

public void closePortal(String portalName)
Parameter
NameDescription
portalNameString

closeStatement(String statementName)

public void closeStatement(String statementName)
Parameter
NameDescription
statementNameString

connectToSpanner(String database, GoogleCredentials credentials)

public void connectToSpanner(String database, GoogleCredentials credentials)
Parameters
NameDescription
databaseString
credentialscom.google.auth.oauth2.GoogleCredentials

getActiveStatement()

public synchronized IntermediateStatement getActiveStatement()
Returns
TypeDescription
IntermediateStatement

getConnectionId()

public int getConnectionId()
Returns
TypeDescription
int

getConnectionMetadata()

public ConnectionMetadata getConnectionMetadata()
Returns
TypeDescription
ConnectionMetadata

getDatabaseId()

public DatabaseId getDatabaseId()
Returns
TypeDescription
com.google.cloud.spanner.DatabaseId

getExtendedQueryProtocolHandler()

public ExtendedQueryProtocolHandler getExtendedQueryProtocolHandler()
Returns
TypeDescription
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
TypeDescription
int

getPortal(String portalName)

public IntermediatePortalStatement getPortal(String portalName)
Parameter
NameDescription
portalNameString
Returns
TypeDescription
IntermediatePortalStatement

getSecret()

public int getSecret()
Returns
TypeDescription
int

getServer()

public ProxyServer getServer()
Returns
TypeDescription
ProxyServer

getSpannerConnection()

public Connection getSpannerConnection()
Returns
TypeDescription
Connection

getStatement(String statementName)

public IntermediatePreparedStatement getStatement(String statementName)
Parameter
NameDescription
statementNameString
Returns
TypeDescription
IntermediatePreparedStatement

getStatus()

public synchronized ConnectionHandler.ConnectionStatus getStatus()
Returns
TypeDescription
ConnectionHandler.ConnectionStatus

getWellKnownClient()

public ClientAutoDetector.WellKnownClient getWellKnownClient()
Returns
TypeDescription
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
TypeDescription
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
NameDescription
portalNameString
Returns
TypeDescription
boolean

hasStatement(String statementName)

public boolean hasStatement(String statementName)
Parameter
NameDescription
statementNameString
Returns
TypeDescription
boolean

increaseInvalidMessageCount()

public void increaseInvalidMessageCount()

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

registerPortal(String portalName, IntermediatePortalStatement portal)

public void registerPortal(String portalName, IntermediatePortalStatement portal)
Parameters
NameDescription
portalNameString
portalIntermediatePortalStatement

registerStatement(String statementName, IntermediatePreparedStatement statement)

public void registerStatement(String statementName, IntermediatePreparedStatement statement)
Parameters
NameDescription
statementNameString
statementIntermediatePreparedStatement

removeActiveStatement(IntermediateStatement statement)

public synchronized void removeActiveStatement(IntermediateStatement statement)

Remove a statement from the buffer if it is currently executing. For more information on this use-case, read addActiveStatement comment.

Parameter
NameDescription
statementIntermediateStatement

The statement to be removed.

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

setMessageState(WireMessage message)

public void setMessageState(WireMessage message)
Parameter
NameDescription
messageWireMessage

setStatus(ConnectionHandler.ConnectionStatus status)

public synchronized void setStatus(ConnectionHandler.ConnectionStatus status)
Parameter
NameDescription
statusConnectionHandler.ConnectionStatus

setWellKnownClient(ClientAutoDetector.WellKnownClient wellKnownClient)

public void setWellKnownClient(ClientAutoDetector.WellKnownClient wellKnownClient)
Parameter
NameDescription
wellKnownClientClientAutoDetector.WellKnownClient