Search in sources :

Example 1 with EUnexpected

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

the class JsonDecoder method close.

@Override
public void close() {
    try {
        if (parser != null) {
            parser.close();
            parser = null;
        }
    } catch (IOException e) {
        // Ensure unexpected errors are still reported to the Flow API
        log.error("Unexpected error closing decoder: {}", e.getMessage(), e);
        throw new EUnexpected(e);
    } finally {
        super.close();
    }
}
Also used : IOException(java.io.IOException) EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Example 2 with EUnexpected

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

the class JsonDecoder method decodeChunk.

@Override
protected void decodeChunk(ByteBuf chunk) {
    try {
        var bytes = new byte[chunk.readableBytes()];
        chunk.readBytes(bytes);
        parser.feedInput(bytes, 0, bytes.length);
        JsonToken token;
        while ((token = parser.nextToken()) != JsonToken.NOT_AVAILABLE) parser.acceptToken(token);
    } catch (JacksonException e) {
        // This exception is a "well-behaved" parse failure, parse location and message should be meaningful
        var errorMessage = String.format("JSON 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 = "JSON 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 during decoding", e);
        throw new EUnexpected(e);
    } finally {
        chunk.release();
    }
}
Also used : JacksonException(com.fasterxml.jackson.core.JacksonException) EDataCorruption(com.accenture.trac.common.exception.EDataCorruption) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Example 3 with EUnexpected

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

the class ArrowFileDecoder method decodeChunk.

@Override
protected void decodeChunk(ByteBuf chunk) {
    try (var stream = new ByteSeekableChannel(chunk);
        var reader = new ArrowFileReader(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));
        }
    } catch (InvalidArrowFileException e) {
        // A nice clean validation failure from the Arrow framework
        // E.g. missing / incorrect magic number at the start (or end) of the file
        var errorMessage = "Arrow file decoding failed, file is invalid: " + e.getMessage();
        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 file 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 file decoding", e);
        throw new EUnexpected(e);
    } finally {
        chunk.release();
    }
}
Also used : VectorUnloader(org.apache.arrow.vector.VectorUnloader) ByteSeekableChannel(com.accenture.trac.common.util.ByteSeekableChannel) ArrowFileReader(org.apache.arrow.vector.ipc.ArrowFileReader) InvalidArrowFileException(org.apache.arrow.vector.ipc.InvalidArrowFileException) EDataCorruption(com.accenture.trac.common.exception.EDataCorruption) IOException(java.io.IOException) EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Example 4 with EUnexpected

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

the class ArrowSchema method tracToArrow.

public static Schema tracToArrow(SchemaDefinition tracSchema) {
    // Unexpected error - TABLE is the only TRAC schema type currently available
    if (tracSchema.getSchemaType() != SchemaType.TABLE)
        throw new EUnexpected();
    var tracTableSchema = tracSchema.getTable();
    var arrowFields = new ArrayList<Field>(tracTableSchema.getFieldsCount());
    for (var tracField : tracTableSchema.getFieldsList()) {
        var fieldName = tracField.getFieldName();
        // only business keys are not nullable
        var nullable = !tracField.getBusinessKey();
        var arrowType = TRAC_ARROW_TYPE_MAPPING.get(tracField.getFieldType());
        // Unexpected error - All TRAC primitive types are mapped
        if (arrowType == null)
            throw new EUnexpected();
        var arrowFieldType = new FieldType(nullable, arrowType, /* dictionary = */
        null);
        var arrowField = new Field(fieldName, arrowFieldType, /* children = */
        null);
        arrowFields.add(arrowField);
    }
    return new Schema(arrowFields);
}
Also used : ArrayList(java.util.ArrayList) EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Example 5 with EUnexpected

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

the class ConfigManagerTest method setup.

@BeforeEach
void setup() throws Exception {
    plugins = new PluginManager();
    plugins.initConfigPlugins();
    Files.createDirectory(tempDir.resolve("config_dir"));
    for (var sourceFile : CONFIG_SAMPLES) {
        var fileName = Paths.get(sourceFile).getFileName();
        var targetPath = tempDir.resolve("config_dir").resolve(fileName);
        try (var inputStream = getClass().getResourceAsStream(sourceFile);
            var outputStream = Files.newOutputStream(targetPath)) {
            if (inputStream == null)
                throw new EUnexpected();
            inputStream.transferTo(outputStream);
        }
    }
    TestConfigPlugin.setCurrentTempDir(tempDir);
}
Also used : PluginManager(com.accenture.trac.common.plugin.PluginManager) EUnexpected(com.accenture.trac.common.exception.EUnexpected) BeforeEach(org.junit.jupiter.api.BeforeEach)

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