use of io.trino.spi.TrinoException in project trino by trinodb.
the class HttpPageBufferClient method sendGetResults.
private synchronized void sendGetResults() {
URI uri = HttpUriBuilder.uriBuilderFrom(location).appendPath(String.valueOf(token)).build();
HttpResponseFuture<PagesResponse> resultFuture = httpClient.executeAsync(prepareGet().setHeader(TRINO_MAX_SIZE, maxResponseSize.toString()).setUri(uri).build(), new PageResponseHandler(dataIntegrityVerification != DataIntegrityVerification.NONE));
future = resultFuture;
Futures.addCallback(resultFuture, new FutureCallback<>() {
@Override
public void onSuccess(PagesResponse result) {
assertNotHoldsLock(this);
backoff.success();
List<Slice> pages;
boolean pagesAccepted;
try {
if (result.isTaskFailed()) {
throw new TrinoException(REMOTE_TASK_FAILED, format("Remote task failed: %s", remoteTaskId));
}
boolean shouldAcknowledge = false;
synchronized (HttpPageBufferClient.this) {
if (taskInstanceId == null) {
taskInstanceId = result.getTaskInstanceId();
}
if (!isNullOrEmpty(taskInstanceId) && !result.getTaskInstanceId().equals(taskInstanceId)) {
throw new TrinoException(REMOTE_TASK_MISMATCH, format("%s (%s). Expected taskInstanceId: %s, received taskInstanceId: %s", REMOTE_TASK_MISMATCH_ERROR, fromUri(uri), taskInstanceId, result.getTaskInstanceId()));
}
if (result.getToken() == token) {
pages = result.getPages();
token = result.getNextToken();
shouldAcknowledge = pages.size() > 0;
} else {
pages = ImmutableList.of();
}
}
if (shouldAcknowledge && acknowledgePages) {
// Acknowledge token without handling the response.
// The next request will also make sure the token is acknowledged.
// This is to fast release the pages on the buffer side.
URI uri = HttpUriBuilder.uriBuilderFrom(location).appendPath(String.valueOf(result.getNextToken())).appendPath("acknowledge").build();
httpClient.executeAsync(prepareGet().setUri(uri).build(), new ResponseHandler<Void, RuntimeException>() {
@Override
public Void handleException(Request request, Exception exception) {
log.debug(exception, "Acknowledge request failed: %s", uri);
return null;
}
@Override
public Void handle(Request request, Response response) {
if (familyForStatusCode(response.getStatusCode()) != HttpStatus.Family.SUCCESSFUL) {
log.debug("Unexpected acknowledge response code: %s", response.getStatusCode());
}
return null;
}
});
}
// add pages:
// addPages must be called regardless of whether pages is an empty list because
// clientCallback can keep stats of requests and responses. For example, it may
// keep track of how often a client returns empty response and adjust request
// frequency or buffer size.
pagesAccepted = clientCallback.addPages(HttpPageBufferClient.this, pages);
} catch (TrinoException e) {
handleFailure(e, resultFuture);
return;
}
// update client stats
if (!pages.isEmpty()) {
int pageCount = pages.size();
long rowCount = pages.stream().mapToLong(PagesSerde::getSerializedPagePositionCount).sum();
if (pagesAccepted) {
pagesReceived.addAndGet(pageCount);
rowsReceived.addAndGet(rowCount);
} else {
pagesRejected.addAndGet(pageCount);
rowsRejected.addAndGet(rowCount);
}
}
requestsCompleted.incrementAndGet();
synchronized (HttpPageBufferClient.this) {
// client is complete, acknowledge it by sending it a delete in the next request
if (result.isClientComplete()) {
completed = true;
}
if (future == resultFuture) {
future = null;
}
lastUpdate = DateTime.now();
}
clientCallback.requestComplete(HttpPageBufferClient.this);
}
@Override
public void onFailure(Throwable t) {
log.debug("Request to %s failed %s", uri, t);
assertNotHoldsLock(this);
if (t instanceof ChecksumVerificationException) {
switch(dataIntegrityVerification) {
case NONE:
// In case of NONE, failure is possible in case of inconsistent cluster configuration, so we should not retry.
case ABORT:
// TrinoException will not be retried
t = new TrinoException(GENERIC_INTERNAL_ERROR, format("Checksum verification failure on %s when reading from %s: %s", selfAddress, uri, t.getMessage()), t);
break;
case RETRY:
log.warn("Checksum verification failure on %s when reading from %s, may be retried: %s", selfAddress, uri, t.getMessage());
break;
default:
throw new AssertionError("Unsupported option: " + dataIntegrityVerification);
}
}
t = rewriteException(t);
if (!(t instanceof TrinoException) && backoff.failure()) {
String message = format("%s (%s - %s failures, failure duration %s, total failed request time %s)", WORKER_NODE_ERROR, uri, backoff.getFailureCount(), backoff.getFailureDuration().convertTo(SECONDS), backoff.getFailureRequestTimeTotal().convertTo(SECONDS));
t = new PageTransportTimeoutException(fromUri(uri), message, t);
}
handleFailure(t, resultFuture);
}
}, pageBufferClientCallbackExecutor);
}
use of io.trino.spi.TrinoException in project trino by trinodb.
the class PatternRecognitionPartition method skipAfterMatch.
private void skipAfterMatch(MatchResult matchResult, int patternStart, int searchStart, int searchEnd) {
ArrayView labels = matchResult.getLabels();
switch(skipToPosition) {
case PAST_LAST:
lastSkippedPosition = patternStart + labels.length() - 1;
break;
case NEXT:
lastSkippedPosition = currentPosition;
break;
case LAST:
case FIRST:
checkState(skipToNavigation.isPresent(), "skip to navigation is missing for SKIP TO ", skipToPosition.name());
int position = skipToNavigation.get().resolvePosition(patternStart + labels.length() - 1, labels, searchStart, searchEnd, patternStart);
if (position == -1) {
throw new TrinoException(StandardErrorCode.GENERIC_USER_ERROR, "AFTER MATCH SKIP failed: pattern variable is not present in match");
}
if (position == patternStart) {
throw new TrinoException(StandardErrorCode.GENERIC_USER_ERROR, "AFTER MATCH SKIP failed: cannot skip to first row of match");
}
lastSkippedPosition = position - 1;
break;
default:
throw new IllegalStateException("unexpected SKIP TO position: " + skipToPosition);
}
}
use of io.trino.spi.TrinoException in project trino by trinodb.
the class VarcharToTimestampWithTimeZoneCast method toLong.
private static LongTimestampWithTimeZone toLong(int precision, String value, Function<String, ZoneId> zoneId) {
checkArgument(precision > MAX_SHORT_PRECISION && precision <= MAX_PRECISION, "precision out of range");
Matcher matcher = DateTimes.DATETIME_PATTERN.matcher(value);
if (!matcher.matches()) {
throw new TrinoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value);
}
String year = matcher.group("year");
String month = matcher.group("month");
String day = matcher.group("day");
String hour = matcher.group("hour");
String minute = matcher.group("minute");
String second = matcher.group("second");
String fraction = matcher.group("fraction");
String timezone = matcher.group("timezone");
ZoneId zone;
long epochSecond;
try {
zone = zoneId.apply(timezone);
epochSecond = ZonedDateTime.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day), hour == null ? 0 : Integer.parseInt(hour), minute == null ? 0 : Integer.parseInt(minute), second == null ? 0 : Integer.parseInt(second), 0, zone).toEpochSecond();
} catch (DateTimeException e) {
throw new TrinoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value, e);
}
int actualPrecision = 0;
long fractionValue = 0;
if (fraction != null) {
actualPrecision = fraction.length();
fractionValue = Long.parseLong(fraction);
}
if (actualPrecision > precision) {
fractionValue = round(fractionValue, actualPrecision - precision);
}
long fractionInPicos = rescale(fractionValue, actualPrecision, MAX_PRECISION);
return longTimestampWithTimeZone(epochSecond, fractionInPicos, zone);
}
use of io.trino.spi.TrinoException in project trino by trinodb.
the class VarcharToTimestampWithTimeZoneCast method toShort.
private static long toShort(int precision, String value, Function<String, ZoneId> zoneId) {
checkArgument(precision <= MAX_SHORT_PRECISION, "precision must be less than max short timestamp precision");
Matcher matcher = DateTimes.DATETIME_PATTERN.matcher(value);
if (!matcher.matches()) {
throw new TrinoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value);
}
String year = matcher.group("year");
String month = matcher.group("month");
String day = matcher.group("day");
String hour = matcher.group("hour");
String minute = matcher.group("minute");
String second = matcher.group("second");
String fraction = matcher.group("fraction");
String timezone = matcher.group("timezone");
ZoneId zone;
long epochSecond;
try {
zone = zoneId.apply(timezone);
epochSecond = ZonedDateTime.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day), hour == null ? 0 : Integer.parseInt(hour), minute == null ? 0 : Integer.parseInt(minute), second == null ? 0 : Integer.parseInt(second), 0, zone).toEpochSecond();
} catch (DateTimeException e) {
throw new TrinoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to timestamp: " + value, e);
}
int actualPrecision = 0;
long fractionValue = 0;
if (fraction != null) {
actualPrecision = fraction.length();
fractionValue = Long.parseLong(fraction);
}
if (actualPrecision > precision) {
fractionValue = round(fractionValue, actualPrecision - precision);
}
long millisOfSecond = rescale(fractionValue, actualPrecision, MAX_SHORT_PRECISION);
return packDateTimeWithZone(epochSecond * MILLISECONDS_PER_SECOND + millisOfSecond, getTimeZoneKey(zone.getId()));
}
use of io.trino.spi.TrinoException in project trino by trinodb.
the class AesSpillCipher method decrypt.
@Override
public int decrypt(byte[] encryptedData, int inputOffset, int length, byte[] destination, int destinationOffset) {
checkArgument(encryptedData.length - inputOffset >= length, "encryptedData too small for length argument");
checkArgument(destination.length - destinationOffset >= decryptedMaxLength(length), "destination buffer too small for decrypted output");
Cipher cipher = createDecryptCipher(key, new IvParameterSpec(encryptedData, inputOffset, ivBytes));
try {
// Do not refactor into single doFinal call, performance and allocation rate are significantly worse
// See https://github.com/trinodb/trino/pull/5557
int n = cipher.update(encryptedData, inputOffset + ivBytes, length - ivBytes, destination, destinationOffset);
return n + cipher.doFinal(destination, destinationOffset + n);
} catch (GeneralSecurityException e) {
throw new TrinoException(GENERIC_INTERNAL_ERROR, "Cannot decrypt previously encrypted data: " + e.getMessage(), e);
}
}
Aggregations