Search in sources :

Example 26 with EUnexpected

use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.

the class JobApiService method submitForExecution.

private CompletionStage<JobState> submitForExecution(JobState jobState) {
    var jobKey = MetadataUtil.objectKey(jobState.jobId);
    try (var ctx = jobCache.useTicket(jobKey)) {
        // However if it does, we definitely want to report an error!
        if (ctx.superseded())
            throw new EUnexpected();
        jobState.jobKey = jobKey;
        jobState.statusCode = JobStatusCode.QUEUED;
        jobCache.createJob(jobKey, jobState, ctx.ticket());
        return CompletableFuture.completedFuture(jobState);
    }
}
Also used : EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Example 27 with EUnexpected

use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.

the class LocalFileReader method readChunk.

private void readChunk() {
    try {
        if (chunkInProgress)
            throw new EUnexpected();
        var chunk = allocator.ioBuffer(DEFAULT_CHUNK_SIZE);
        if (chunk.nioBufferCount() != 1)
            throw new EUnexpected();
        var nioChunk = chunk.nioBuffer(0, DEFAULT_CHUNK_SIZE);
        channel.read(nioChunk, bytesRead, chunk, readHandler);
        chunkInProgress = true;
    } catch (Exception e) {
        gotError = true;
        handleError(e);
    }
}
Also used : EUnexpected(com.accenture.trac.common.exception.EUnexpected) AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) ExecutionException(java.util.concurrent.ExecutionException)

Example 28 with EUnexpected

use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.

the class ArrowStreamDecoder method decodeChunk.

@Override
protected void decodeChunk(ByteBuf chunk) {
    try (var stream = new ByteSeekableChannel(chunk)) {
        // Arrow does not attempt to validate the stream before reading
        // This quick validation peeks at the start of the stream for a basic sanity check
        // It should be enough to flag e.g. if data has been sent in a totally different format
        // Make sure to do this check before setting up reader + root,
        // since that will trigger reading the initial schema message
        validateStartOfStream(stream);
        try (var reader = new ArrowStreamReader(stream, arrowAllocator);
            var root = reader.getVectorSchemaRoot()) {
            var schema = root.getSchema();
            emitBlock(DataBlock.forSchema(schema));
            var unloader = new VectorUnloader(root);
            while (reader.loadNextBatch()) {
                var batch = unloader.getRecordBatch();
                emitBlock(DataBlock.forRecords(batch));
                // Release memory retained in VSR (batch still has a reference)
                root.clear();
            }
        }
    } catch (NotAnArrowStream e) {
        // A nice clean validation exception
        var errorMessage = "Arrow stream decoding failed, content does not look like an Arrow stream";
        log.error(errorMessage, e);
        throw new EDataCorruption(errorMessage, e);
    } catch (IllegalArgumentException | IndexOutOfBoundsException | IOException e) {
        // These errors occur if the data stream contains bad values for vector sizes, offsets etc.
        // This may be as a result of a corrupt data stream, or a maliciously crafted message
        // Decoders work on a stream of buffers, "real" IO exceptions should not occur
        var errorMessage = "Arrow stream decoding failed, content is garbled";
        log.error(errorMessage, e);
        throw new EDataCorruption(errorMessage, e);
    } catch (Throwable e) {
        // Ensure unexpected errors are still reported to the Flow API
        log.error("Unexpected error in Arrow stream decoding", e);
        throw new EUnexpected(e);
    } finally {
        chunk.release();
    }
}
Also used : VectorUnloader(org.apache.arrow.vector.VectorUnloader) ByteSeekableChannel(com.accenture.trac.common.util.ByteSeekableChannel) ArrowStreamReader(org.apache.arrow.vector.ipc.ArrowStreamReader) EDataCorruption(com.accenture.trac.common.exception.EDataCorruption) IOException(java.io.IOException) EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Example 29 with EUnexpected

use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.

the class JsonDecoder method decodeStart.

@Override
protected void decodeStart() {
    try {
        var factory = new JsonFactory();
        var tableHandler = new JsonTableHandler(arrowAllocator, arrowSchema, CASE_INSENSITIVE, this::dispatchBatch, BATCH_SIZE);
        this.parser = new JsonStreamParser(factory, tableHandler);
        emitBlock(DataBlock.forSchema(this.arrowSchema));
    } catch (IOException e) {
        // Output stream is writing to memory buffers, IO errors are not expected
        log.error("Unexpected error writing to codec buffer: {}", e.getMessage(), e);
        throw new EUnexpected(e);
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) IOException(java.io.IOException) EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Example 30 with EUnexpected

use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.

the class CsvDecoder method decodeChunk.

@Override
protected void decodeChunk(ByteBuf chunk) {
    var csvFactory = new CsvFactory().enable(CsvParser.Feature.TRIM_SPACES).enable(CsvParser.Feature.FAIL_ON_MISSING_COLUMNS);
    try (var stream = new ByteBufInputStream(chunk);
        var parser = (CsvParser) csvFactory.createParser((InputStream) stream)) {
        var csvSchema = CsvSchemaMapping.arrowToCsv(this.arrowSchema).build();
        csvSchema = DEFAULT_HEADER_FLAG ? csvSchema.withHeader() : csvSchema.withoutHeader();
        parser.setSchema(csvSchema);
        var row = 0;
        var col = 0;
        JsonToken token;
        while ((token = parser.nextToken()) != null) {
            switch(token) {
                // For CSV files, a null field name is produced for every field
                case FIELD_NAME:
                    continue;
                case VALUE_NULL:
                case VALUE_TRUE:
                case VALUE_FALSE:
                case VALUE_STRING:
                case VALUE_NUMBER_INT:
                case VALUE_NUMBER_FLOAT:
                    var vector = root.getVector(col);
                    JacksonValues.parseAndSet(vector, row, parser, token);
                    col++;
                    break;
                case START_OBJECT:
                    if (row == 0)
                        for (var vector_ : root.getFieldVectors()) vector_.allocateNew();
                    break;
                case END_OBJECT:
                    row++;
                    col = 0;
                    if (row == BATCH_SIZE) {
                        root.setRowCount(row);
                        dispatchBatch(root);
                        row = 0;
                    }
                    break;
                default:
                    var msg = String.format("Unexpected token %s", token.name());
                    throw new CsvReadException(parser, msg, csvSchema);
            }
        }
        if (row > 0 || col > 0) {
            root.setRowCount(row);
            dispatchBatch(root);
        }
    } catch (JacksonException e) {
        // This exception is a "well-behaved" parse failure, parse location and message should be meaningful
        var errorMessage = String.format("CSV decoding failed on line %d: %s", e.getLocation().getLineNr(), e.getOriginalMessage());
        log.error(errorMessage, e);
        throw new EDataCorruption(errorMessage, e);
    } catch (IOException e) {
        // Decoders work on a stream of buffers, "real" IO exceptions should not occur
        // IO exceptions here indicate parse failures, not file/socket communication errors
        // This is likely to be a more "badly-behaved" failure, or at least one that was not anticipated
        var errorMessage = "CSV decoding failed, content is garbled: " + e.getMessage();
        log.error(errorMessage, e);
        throw new EDataCorruption(errorMessage, e);
    } catch (Throwable e) {
        // Ensure unexpected errors are still reported to the Flow API
        log.error("Unexpected error in CSV decoding", e);
        throw new EUnexpected(e);
    } finally {
        chunk.release();
    }
}
Also used : CsvFactory(com.fasterxml.jackson.dataformat.csv.CsvFactory) JacksonException(com.fasterxml.jackson.core.JacksonException) CsvReadException(com.fasterxml.jackson.dataformat.csv.CsvReadException) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) EDataCorruption(com.accenture.trac.common.exception.EDataCorruption) CsvParser(com.fasterxml.jackson.dataformat.csv.CsvParser) JsonToken(com.fasterxml.jackson.core.JsonToken) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException) EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Aggregations

EUnexpected (com.accenture.trac.common.exception.EUnexpected)44 IOException (java.io.IOException)8 EDataCorruption (com.accenture.trac.common.exception.EDataCorruption)4 ArrayList (java.util.ArrayList)3 ECacheTicket (com.accenture.trac.common.exception.ECacheTicket)2 EInputValidation (com.accenture.trac.common.exception.EInputValidation)2 ByteSeekableChannel (com.accenture.trac.common.util.ByteSeekableChannel)2 Http1to2Framing (com.accenture.trac.gateway.proxy.http.Http1to2Framing)2 JacksonException (com.fasterxml.jackson.core.JacksonException)2 JsonToken (com.fasterxml.jackson.core.JsonToken)2 ByteString (com.google.protobuf.ByteString)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)2 MetadataBatchRequest (com.accenture.trac.api.MetadataBatchRequest)1 MetadataBatchResponse (com.accenture.trac.api.MetadataBatchResponse)1 MetadataWriteRequest (com.accenture.trac.api.MetadataWriteRequest)1 TrustedMetadataApiGrpc (com.accenture.trac.api.TrustedMetadataApiGrpc)1 EMetadataNotFound (com.accenture.trac.common.exception.EMetadataNotFound)1 EPluginNotAvailable (com.accenture.trac.common.exception.EPluginNotAvailable)1 EStartup (com.accenture.trac.common.exception.EStartup)1