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