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();
}
}
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();
}
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;
}
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;
}
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;
}
Aggregations