Search in sources :

Example 36 with TrinoException

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);
}
Also used : ResponseHandler(io.airlift.http.client.ResponseHandler) StatusResponseHandler.createStatusResponseHandler(io.airlift.http.client.StatusResponseHandler.createStatusResponseHandler) Request(io.airlift.http.client.Request) PagesResponse.createEmptyPagesResponse(io.trino.operator.HttpPageBufferClient.PagesResponse.createEmptyPagesResponse) PagesResponse.createPagesResponse(io.trino.operator.HttpPageBufferClient.PagesResponse.createPagesResponse) URI(java.net.URI) TrinoException(io.trino.spi.TrinoException) TrinoTransportException(io.trino.spi.TrinoTransportException) IOException(java.io.IOException) ResponseTooLargeException(io.airlift.http.client.ResponseTooLargeException) PagesResponse.createEmptyPagesResponse(io.trino.operator.HttpPageBufferClient.PagesResponse.createEmptyPagesResponse) PagesResponse.createPagesResponse(io.trino.operator.HttpPageBufferClient.PagesResponse.createPagesResponse) Response(io.airlift.http.client.Response) StatusResponse(io.airlift.http.client.StatusResponseHandler.StatusResponse) PagesSerde(io.trino.execution.buffer.PagesSerde) TrinoException(io.trino.spi.TrinoException) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 37 with TrinoException

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);
    }
}
Also used : TrinoException(io.trino.spi.TrinoException) ArrayView(io.trino.operator.window.matcher.ArrayView)

Example 38 with TrinoException

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);
}
Also used : DateTimeException(java.time.DateTimeException) ZoneId(java.time.ZoneId) Matcher(java.util.regex.Matcher) TrinoException(io.trino.spi.TrinoException)

Example 39 with TrinoException

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()));
}
Also used : DateTimeException(java.time.DateTimeException) ZoneId(java.time.ZoneId) Matcher(java.util.regex.Matcher) TrinoException(io.trino.spi.TrinoException)

Example 40 with TrinoException

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);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) TrinoException(io.trino.spi.TrinoException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher)

Aggregations

TrinoException (io.trino.spi.TrinoException)623 IOException (java.io.IOException)151 ImmutableList (com.google.common.collect.ImmutableList)105 List (java.util.List)100 Type (io.trino.spi.type.Type)93 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)90 SchemaTableName (io.trino.spi.connector.SchemaTableName)83 Path (org.apache.hadoop.fs.Path)83 Optional (java.util.Optional)79 ArrayList (java.util.ArrayList)77 Map (java.util.Map)76 ImmutableMap (com.google.common.collect.ImmutableMap)70 Objects.requireNonNull (java.util.Objects.requireNonNull)69 ConnectorSession (io.trino.spi.connector.ConnectorSession)63 TableNotFoundException (io.trino.spi.connector.TableNotFoundException)62 ImmutableSet (com.google.common.collect.ImmutableSet)56 VarcharType (io.trino.spi.type.VarcharType)54 Set (java.util.Set)54 Slice (io.airlift.slice.Slice)53 Table (io.trino.plugin.hive.metastore.Table)52