Search in sources :

Example 1 with JdbcResponse

use of org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse 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)

Example 2 with JdbcResponse

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

the class JdbcThinTcpIo method sendRequest.

/**
     * @param req Request.
     * @param cap Initial ouput stream capacity.
     * @return Server response.
     * @throws IOException On IO error.
     * @throws IgniteCheckedException On error.
     */
@SuppressWarnings("unchecked")
public <R extends JdbcResult> R sendRequest(JdbcRequest req, int cap) throws IOException, IgniteCheckedException {
    BinaryWriterExImpl writer = new BinaryWriterExImpl(null, new BinaryHeapOutputStream(cap), null, null);
    req.writeBinary(writer);
    send(writer.array());
    BinaryReaderExImpl reader = new BinaryReaderExImpl(null, new BinaryHeapInputStream(read()), null, null, false);
    JdbcResponse res = new JdbcResponse();
    res.readBinary(reader);
    if (res.status() != SqlListenerResponse.STATUS_SUCCESS)
        throw new IgniteCheckedException("Error server response: [req=" + req + ", resp=" + res + ']');
    return (R) res.response();
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) JdbcResponse(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl)

Example 3 with JdbcResponse

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

the class JdbcThinTcpIo method sendRequest.

/**
 * @param req Request.
 * @return Server response.
 * @throws IOException In case of IO error.
 */
@SuppressWarnings("unchecked")
JdbcResponse sendRequest(JdbcRequest req) throws IOException {
    int cap = guessCapacity(req);
    BinaryWriterExImpl writer = new BinaryWriterExImpl(null, new BinaryHeapOutputStream(cap), null, null);
    req.writeBinary(writer);
    send(writer.array());
    BinaryReaderExImpl reader = new BinaryReaderExImpl(null, new BinaryHeapInputStream(read()), null, null, false);
    JdbcResponse res = new JdbcResponse();
    res.readBinary(reader);
    return res;
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryHeapOutputStream(org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) JdbcResponse(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse) BinaryWriterExImpl(org.apache.ignite.internal.binary.BinaryWriterExImpl) IpcClientTcpEndpoint(org.apache.ignite.internal.util.ipc.loopback.IpcClientTcpEndpoint)

Example 4 with JdbcResponse

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

the class JdbcThinTcpIo method readResponse.

/**
 * @return Server response.
 * @throws IOException In case of IO error.
 */
JdbcResponse readResponse() throws IOException {
    BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, new BinaryHeapInputStream(read()), null, true);
    JdbcResponse res = new JdbcResponse();
    res.readBinary(reader, protoCtx);
    return res;
}
Also used : BinaryReaderExImpl(org.apache.ignite.internal.binary.BinaryReaderExImpl) BinaryHeapInputStream(org.apache.ignite.internal.binary.streams.BinaryHeapInputStream) JdbcResponse(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse)

Example 5 with JdbcResponse

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

the class JdbcThinConnection method retrieveCacheDistribution.

/**
 * Retrieve cache distribution for specified cache Id.
 *
 * @param cacheId Cache Id.
 * @param partCnt Partitions count.
 * @return Partitions cache distribution.
 * @throws IOException If Exception occurred during the network partition distribution retrieval.
 */
private UUID[] retrieveCacheDistribution(int cacheId, int partCnt) throws IOException {
    UUID[] cacheDistr = affinityCache.cacheDistribution(cacheId);
    if (cacheDistr != null)
        return cacheDistr;
    JdbcResponse res;
    res = cliIo(null).sendRequest(new JdbcCachePartitionsRequest(Collections.singleton(cacheId)), null);
    assert res.status() == ClientListenerResponse.STATUS_SUCCESS;
    AffinityTopologyVersion resAffinityVer = res.affinityVersion();
    if (affinityCache.version().compareTo(resAffinityVer) < 0) {
        affinityCache = new AffinityCache(resAffinityVer, connProps.getPartitionAwarenessPartitionDistributionsCacheSize(), connProps.getPartitionAwarenessSqlCacheSize());
    } else if (affinityCache.version().compareTo(resAffinityVer) > 0) {
        // possible in single-threaded jdbc thin client, so it's a reserve for the future.
        return null;
    }
    List<JdbcThinPartitionAwarenessMappingGroup> mappings = ((JdbcCachePartitionsResult) res.response()).getMappings();
    // we might retrieve multiple caches but exactly with same distribution.
    assert mappings.size() == 1;
    JdbcThinPartitionAwarenessMappingGroup mappingGrp = mappings.get(0);
    cacheDistr = mappingGrp.revertMappings(partCnt);
    for (int mpCacheId : mappingGrp.cacheIds()) affinityCache.addCacheDistribution(mpCacheId, cacheDistr);
    return cacheDistr;
}
Also used : JdbcCachePartitionsRequest(org.apache.ignite.internal.processors.odbc.jdbc.JdbcCachePartitionsRequest) AffinityTopologyVersion(org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion) JdbcResponse(org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse) UUID(java.util.UUID) JdbcCachePartitionsResult(org.apache.ignite.internal.processors.odbc.jdbc.JdbcCachePartitionsResult) Savepoint(java.sql.Savepoint)

Aggregations

JdbcResponse (org.apache.ignite.internal.processors.odbc.jdbc.JdbcResponse)5 BinaryReaderExImpl (org.apache.ignite.internal.binary.BinaryReaderExImpl)3 BinaryHeapInputStream (org.apache.ignite.internal.binary.streams.BinaryHeapInputStream)3 Savepoint (java.sql.Savepoint)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 BinaryWriterExImpl (org.apache.ignite.internal.binary.BinaryWriterExImpl)2 BinaryHeapOutputStream (org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream)2 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 BatchUpdateException (java.sql.BatchUpdateException)1 SQLClientInfoException (java.sql.SQLClientInfoException)1 SQLException (java.sql.SQLException)1 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 UUID (java.util.UUID)1 ExecutionException (java.util.concurrent.ExecutionException)1 IgniteException (org.apache.ignite.IgniteException)1 BinaryObjectException (org.apache.ignite.binary.BinaryObjectException)1 ClientException (org.apache.ignite.client.ClientException)1 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)1