Search in sources :

Example 1 with JdbcBulkLoadBatchRequest

use of org.apache.ignite.internal.processors.odbc.jdbc.JdbcBulkLoadBatchRequest in project ignite by apache.

the class JdbcThinStatement method sendFile.

/**
 * Sends a file to server in batches via multiple {@link JdbcBulkLoadBatchRequest}s.
 *
 * @param cmdRes Result of invoking COPY command: contains server-parsed
 *    bulk load parameters, such as file name and batch size.
 * @return Bulk load result.
 * @throws SQLException On error.
 */
private JdbcResult sendFile(JdbcBulkLoadAckResult cmdRes) throws SQLException {
    String fileName = cmdRes.params().localFileName();
    int batchSize = cmdRes.params().packetSize();
    int batchNum = 0;
    try {
        try (InputStream input = new BufferedInputStream(new FileInputStream(fileName))) {
            byte[] buf = new byte[batchSize];
            int readBytes;
            while ((readBytes = input.read(buf)) != -1) {
                if (readBytes == 0)
                    continue;
                JdbcResult res = conn.sendRequest(new JdbcBulkLoadBatchRequest(cmdRes.queryId(), batchNum++, JdbcBulkLoadBatchRequest.CMD_CONTINUE, readBytes == buf.length ? buf : Arrays.copyOf(buf, readBytes)));
                if (!(res instanceof JdbcQueryExecuteResult))
                    throw new SQLException("Unknown response sent by the server: " + res);
            }
            return conn.sendRequest(new JdbcBulkLoadBatchRequest(cmdRes.queryId(), batchNum++, JdbcBulkLoadBatchRequest.CMD_FINISHED_EOF));
        }
    } catch (Exception e) {
        try {
            conn.sendRequest(new JdbcBulkLoadBatchRequest(cmdRes.queryId(), batchNum, JdbcBulkLoadBatchRequest.CMD_FINISHED_ERROR));
        } catch (SQLException e1) {
            throw new SQLException("Cannot send finalization request: " + e1.getMessage(), e);
        }
        if (e instanceof SQLException)
            throw (SQLException) e;
        else
            throw new SQLException("Failed to read file: '" + fileName + "'", SqlStateCode.INTERNAL_ERROR, e);
    }
}
Also used : JdbcBulkLoadBatchRequest(org.apache.ignite.internal.processors.odbc.jdbc.JdbcBulkLoadBatchRequest) BufferedInputStream(java.io.BufferedInputStream) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JdbcQueryExecuteResult(org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteResult) JdbcResult(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResult) FileInputStream(java.io.FileInputStream) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) BatchUpdateException(java.sql.BatchUpdateException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException)

Example 2 with JdbcBulkLoadBatchRequest

use of org.apache.ignite.internal.processors.odbc.jdbc.JdbcBulkLoadBatchRequest in project ignite by apache.

the class JdbcThinStatement method sendFile.

/**
 * Sends a file to server in batches via multiple {@link JdbcBulkLoadBatchRequest}s.
 *
 * @param cmdRes Result of invoking COPY command: contains server-parsed
 *    bulk load parameters, such as file name and batch size.
 * @return Bulk load result.
 * @throws SQLException On error.
 */
private JdbcResult sendFile(JdbcBulkLoadAckResult cmdRes, JdbcThinTcpIo stickyIO) throws SQLException {
    String fileName = cmdRes.params().localFileName();
    int batchSize = cmdRes.params().packetSize();
    int batchNum = 0;
    try {
        try (InputStream input = new BufferedInputStream(new FileInputStream(fileName))) {
            byte[] buf = new byte[batchSize];
            int readBytes;
            int timeSpendMillis = 0;
            while ((readBytes = input.read(buf)) != -1) {
                long startTime = System.currentTimeMillis();
                if (readBytes == 0)
                    continue;
                if (reqTimeout != JdbcThinConnection.NO_TIMEOUT)
                    reqTimeout -= timeSpendMillis;
                JdbcResult res = conn.sendRequest(new JdbcBulkLoadBatchRequest(cmdRes.cursorId(), batchNum++, JdbcBulkLoadBatchRequest.CMD_CONTINUE, readBytes == buf.length ? buf : Arrays.copyOf(buf, readBytes)), this, stickyIO).response();
                if (!(res instanceof JdbcQueryExecuteResult))
                    throw new SQLException("Unknown response sent by the server: " + res);
                timeSpendMillis = (int) (System.currentTimeMillis() - startTime);
            }
            if (reqTimeout != JdbcThinConnection.NO_TIMEOUT)
                reqTimeout -= timeSpendMillis;
            return conn.sendRequest(new JdbcBulkLoadBatchRequest(cmdRes.cursorId(), batchNum++, JdbcBulkLoadBatchRequest.CMD_FINISHED_EOF), this, stickyIO).response();
        }
    } catch (Exception e) {
        if (e instanceof SQLTimeoutException)
            throw (SQLTimeoutException) e;
        try {
            conn.sendRequest(new JdbcBulkLoadBatchRequest(cmdRes.cursorId(), batchNum, JdbcBulkLoadBatchRequest.CMD_FINISHED_ERROR), this, stickyIO);
        } catch (SQLException e1) {
            throw new SQLException("Cannot send finalization request: " + e1.getMessage(), e);
        }
        if (e instanceof SQLException)
            throw (SQLException) e;
        else
            throw new SQLException("Failed to read file: '" + fileName + "'", SqlStateCode.INTERNAL_ERROR, e);
    }
}
Also used : JdbcBulkLoadBatchRequest(org.apache.ignite.internal.processors.odbc.jdbc.JdbcBulkLoadBatchRequest) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JdbcQueryExecuteResult(org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteResult) FileInputStream(java.io.FileInputStream) SqlParseException(org.apache.ignite.internal.sql.SqlParseException) BatchUpdateException(java.sql.BatchUpdateException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLTimeoutException(java.sql.SQLTimeoutException) SQLException(java.sql.SQLException) IgniteSQLException(org.apache.ignite.internal.processors.query.IgniteSQLException) BufferedInputStream(java.io.BufferedInputStream) SQLTimeoutException(java.sql.SQLTimeoutException) JdbcResult(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResult)

Example 3 with JdbcBulkLoadBatchRequest

use of org.apache.ignite.internal.processors.odbc.jdbc.JdbcBulkLoadBatchRequest in project ignite by apache.

the class JdbcThinConnection method sendRequest.

/**
 * Send request for execution via corresponding singleIo from {@link #ios} or sticky singleIo.
 *
 * @param req Request.
 * @param stmt Jdbc thin statement.
 * @param stickyIo Sticky ignite endpoint.
 * @return Server response.
 * @throws SQLException On any error.
 */
JdbcResultWithIo sendRequest(JdbcRequest req, JdbcThinStatement stmt, @Nullable JdbcThinTcpIo stickyIo) throws SQLException {
    RequestTimeoutTask reqTimeoutTask = null;
    acquireMutex();
    try {
        int retryAttemptsLeft = 1;
        Exception lastE = null;
        while (retryAttemptsLeft > 0) {
            JdbcThinTcpIo cliIo = null;
            ensureConnected();
            try {
                cliIo = (stickyIo == null || !stickyIo.connected()) ? cliIo(calculateNodeIds(req)) : stickyIo;
                if (stmt != null && stmt.requestTimeout() != NO_TIMEOUT) {
                    reqTimeoutTask = new RequestTimeoutTask(req instanceof JdbcBulkLoadBatchRequest ? stmt.currentRequestId() : req.requestId(), cliIo, stmt.requestTimeout());
                    qryTimeoutScheduledFut = maintenanceExecutor.scheduleAtFixedRate(reqTimeoutTask, 0, REQUEST_TIMEOUT_PERIOD, TimeUnit.MILLISECONDS);
                }
                JdbcQueryExecuteRequest qryReq = null;
                if (req instanceof JdbcQueryExecuteRequest)
                    qryReq = (JdbcQueryExecuteRequest) req;
                JdbcResponse res = cliIo.sendRequest(req, stmt);
                txIo = res.activeTransaction() ? cliIo : null;
                if (res.status() == IgniteQueryErrorCode.QUERY_CANCELED && stmt != null && stmt.requestTimeout() != NO_TIMEOUT && reqTimeoutTask != null && reqTimeoutTask.expired.get()) {
                    int qryTimeout = stmt.getQueryTimeout();
                    throw new SQLTimeoutException(getTimeoutDescription(qryTimeout, cliIo), SqlStateCode.QUERY_CANCELLED, IgniteQueryErrorCode.QUERY_CANCELED);
                } else if (res.status() != ClientListenerResponse.STATUS_SUCCESS)
                    throw new SQLException(res.error(), IgniteQueryErrorCode.codeToSqlState(res.status()), res.status());
                updateAffinityCache(qryReq, res);
                return new JdbcResultWithIo(res.response(), cliIo);
            } catch (SQLException e) {
                if (LOG.isLoggable(Level.FINE))
                    LOG.log(Level.FINE, "Exception during sending an sql request.", e);
                throw e;
            } catch (Exception e) {
                if (LOG.isLoggable(Level.FINE))
                    LOG.log(Level.FINE, "Exception during sending an sql request.", e);
                // for the first time and should skip it during next processing
                if (cliIo != null && cliIo.connected())
                    onDisconnect(cliIo);
                if (e instanceof SocketTimeoutException)
                    throw new SQLException("Connection timed out.", CONNECTION_FAILURE, e);
                else {
                    if (lastE == null) {
                        retryAttemptsLeft = calculateRetryAttemptsCount(stickyIo, req);
                        lastE = e;
                    } else
                        retryAttemptsLeft--;
                }
            }
        }
        throw new SQLException("Failed to communicate with Ignite cluster.", CONNECTION_FAILURE, lastE);
    } finally {
        if (stmt != null && stmt.requestTimeout() != NO_TIMEOUT && reqTimeoutTask != null)
            qryTimeoutScheduledFut.cancel(false);
        releaseMutex();
    }
}
Also used : JdbcQueryExecuteRequest(org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteRequest) JdbcBulkLoadBatchRequest(org.apache.ignite.internal.processors.odbc.jdbc.JdbcBulkLoadBatchRequest) SocketTimeoutException(java.net.SocketTimeoutException) SQLException(java.sql.SQLException) JdbcResponse(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse) SQLTimeoutException(java.sql.SQLTimeoutException) JdbcResultWithIo(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResultWithIo) Savepoint(java.sql.Savepoint) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) ClientException(org.apache.ignite.client.ClientException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BatchUpdateException(java.sql.BatchUpdateException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SQLTimeoutException(java.sql.SQLTimeoutException) IgniteException(org.apache.ignite.IgniteException) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Aggregations

BatchUpdateException (java.sql.BatchUpdateException)3 SQLException (java.sql.SQLException)3 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)3 JdbcBulkLoadBatchRequest (org.apache.ignite.internal.processors.odbc.jdbc.JdbcBulkLoadBatchRequest)3 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 SQLTimeoutException (java.sql.SQLTimeoutException)2 JdbcQueryExecuteResult (org.apache.ignite.internal.processors.odbc.jdbc.JdbcQueryExecuteResult)2 JdbcResult (org.apache.ignite.internal.processors.odbc.jdbc.JdbcResult)2 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)2 SqlParseException (org.apache.ignite.internal.sql.SqlParseException)2 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 SQLClientInfoException (java.sql.SQLClientInfoException)1 Savepoint (java.sql.Savepoint)1 ExecutionException (java.util.concurrent.ExecutionException)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteException (org.apache.ignite.IgniteException)1 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)1