Search in sources :

Example 1 with ReadyResponse

use of com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse in project pgadapter by GoogleCloudPlatform.

the class SyncMessage method sendPayload.

@Override
protected void sendPayload() throws Exception {
    boolean inTransaction = connection.getSpannerConnection().isInTransaction();
    new ReadyResponse(this.outputStream, inTransaction ? Status.TRANSACTION : ReadyResponse.Status.IDLE).send();
}
Also used : ReadyResponse(com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse)

Example 2 with ReadyResponse

use of com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse in project pgadapter by GoogleCloudPlatform.

the class BootstrapMessage method sendStartupMessage.

/**
 * Expected PG start-up reply, including Auth approval, Key Data connection-specific info,
 * PGAdapter specific parameters, and a ready signal.
 *
 * @param output The data output stream to send results to.
 * @param connectionId The connection Id representing the current connection to send to client.
 * @param secret The secret apposite this connection
 * @throws Exception
 */
public static void sendStartupMessage(DataOutputStream output, int connectionId, int secret, OptionsMetadata options) throws Exception {
    new AuthenticationOkResponse(output).send();
    new KeyDataResponse(output, connectionId, secret).send();
    new ParameterStatusResponse(output, "server_version".getBytes(), options.getServerVersion().getBytes()).send();
    new ParameterStatusResponse(output, "application_name".getBytes(), "PGAdapter".getBytes()).send();
    new ParameterStatusResponse(output, "is_superuser".getBytes(), "false".getBytes()).send();
    new ParameterStatusResponse(output, "session_authorization".getBytes(), "PGAdapter".getBytes()).send();
    new ParameterStatusResponse(output, "integer_datetimes".getBytes(), "on".getBytes()).send();
    new ParameterStatusResponse(output, "server_encoding".getBytes(), "UTF8".getBytes()).send();
    new ParameterStatusResponse(output, "client_encoding".getBytes(), "UTF8".getBytes()).send();
    new ParameterStatusResponse(output, "DateStyle".getBytes(), "ISO,YMD".getBytes()).send();
    new ParameterStatusResponse(output, "IntervalStyle".getBytes(), "iso_8601".getBytes()).send();
    new ParameterStatusResponse(output, "standard_conforming_strings".getBytes(), "on".getBytes()).send();
    new ParameterStatusResponse(output, "TimeZone".getBytes(), TimeZone.getDefault().getDisplayName(false, TimeZone.SHORT).getBytes()).send();
    new ReadyResponse(output, Status.IDLE).send();
}
Also used : ReadyResponse(com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse) AuthenticationOkResponse(com.google.cloud.spanner.pgadapter.wireoutput.AuthenticationOkResponse) ParameterStatusResponse(com.google.cloud.spanner.pgadapter.wireoutput.ParameterStatusResponse) KeyDataResponse(com.google.cloud.spanner.pgadapter.wireoutput.KeyDataResponse)

Example 3 with ReadyResponse

use of com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse in project pgadapter by GoogleCloudPlatform.

the class CopyDoneMessage method sendPayload.

@Override
protected void sendPayload() throws Exception {
    // If backend error occurred during copy-in mode, drop any subsequent CopyDone messages.
    if (this.statement != null) {
        MutationWriter mutationWriter = this.statement.getMutationWriter();
        statement.close();
        if (!statement.hasException(0)) {
            try {
                long rowCount = this.statement.getUpdateCount();
                // Set the row count of number of rows copied.
                statement.setUpdateCount(0, rowCount);
                this.sendSpannerResult(0, this.statement, QueryMode.SIMPLE, 0L);
                this.outputStream.flush();
            } catch (Exception e) {
                // Spanner returned an error when trying to commit the batch of mutations.
                mutationWriter.writeErrorFile(e);
                mutationWriter.closeErrorFile();
                this.connection.setStatus(ConnectionStatus.IDLE);
                this.connection.removeActiveStatement(this.statement);
                throw e;
            }
        } else {
            mutationWriter.closeErrorFile();
        }
    }
    this.connection.setStatus(ConnectionStatus.IDLE);
    this.connection.removeActiveStatement(this.statement);
    new ReadyResponse(this.outputStream, ReadyResponse.Status.IDLE).send();
}
Also used : ReadyResponse(com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse) MutationWriter(com.google.cloud.spanner.pgadapter.utils.MutationWriter)

Example 4 with ReadyResponse

use of com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse in project pgadapter by GoogleCloudPlatform.

the class CopyFailMessage method sendPayload.

@Override
protected void sendPayload() throws Exception {
    // If backend error occurred during copy-in mode, drop any subsequent CopyFail messages.
    if (this.statement != null) {
        MutationWriter mutationWriter = this.statement.getMutationWriter();
        mutationWriter.rollback();
        mutationWriter.closeErrorFile();
        statement.close();
        if (!statement.hasException(0)) {
            new ErrorResponse(this.outputStream, new Exception(this.errorMessage), State.IOError).send();
        }
    }
    this.connection.setStatus(ConnectionStatus.IDLE);
    this.connection.removeActiveStatement(this.statement);
    new ReadyResponse(this.outputStream, ReadyResponse.Status.IDLE).send();
}
Also used : ReadyResponse(com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse) MutationWriter(com.google.cloud.spanner.pgadapter.utils.MutationWriter) ErrorResponse(com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse)

Example 5 with ReadyResponse

use of com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse in project pgadapter by GoogleCloudPlatform.

the class ConnectionHandler method handleError.

/**
 * Takes an Exception Object and relates its results to the user within the client.
 *
 * @param e The exception to be related.
 * @throws IOException if there is some issue in the sending of the error messages.
 */
private void handleError(DataOutputStream output, Exception e) throws Exception {
    logger.log(Level.WARNING, e, () -> String.format("Exception on connection handler with ID %s: %s", getName(), e));
    if (this.status == ConnectionStatus.TERMINATED) {
        new ErrorResponse(output, e, ErrorResponse.State.InternalError, Severity.FATAL).send();
        new TerminateResponse(output).send();
    } else if (this.status == ConnectionStatus.COPY_IN) {
        new ErrorResponse(output, e, ErrorResponse.State.InternalError).send();
    } else {
        this.status = ConnectionStatus.IDLE;
        new ErrorResponse(output, e, ErrorResponse.State.InternalError).send();
        new ReadyResponse(output, ReadyResponse.Status.IDLE).send();
    }
}
Also used : TerminateResponse(com.google.cloud.spanner.pgadapter.wireoutput.TerminateResponse) ReadyResponse(com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse) ErrorResponse(com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse)

Aggregations

ReadyResponse (com.google.cloud.spanner.pgadapter.wireoutput.ReadyResponse)7 ErrorResponse (com.google.cloud.spanner.pgadapter.wireoutput.ErrorResponse)3 MutationWriter (com.google.cloud.spanner.pgadapter.utils.MutationWriter)2 CopyStatement (com.google.cloud.spanner.pgadapter.statements.CopyStatement)1 AuthenticationOkResponse (com.google.cloud.spanner.pgadapter.wireoutput.AuthenticationOkResponse)1 CopyInResponse (com.google.cloud.spanner.pgadapter.wireoutput.CopyInResponse)1 EmptyQueryResponse (com.google.cloud.spanner.pgadapter.wireoutput.EmptyQueryResponse)1 KeyDataResponse (com.google.cloud.spanner.pgadapter.wireoutput.KeyDataResponse)1 ParameterStatusResponse (com.google.cloud.spanner.pgadapter.wireoutput.ParameterStatusResponse)1 RowDescriptionResponse (com.google.cloud.spanner.pgadapter.wireoutput.RowDescriptionResponse)1 TerminateResponse (com.google.cloud.spanner.pgadapter.wireoutput.TerminateResponse)1