Skip to content

Commit

Permalink
Add exception handling support
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktooth committed Jul 7, 2022
1 parent d64d131 commit 06543a6
Show file tree
Hide file tree
Showing 39 changed files with 1,129 additions and 314 deletions.
10 changes: 0 additions & 10 deletions native-schema-registry/c/include/error_handling.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GLUE_SCHEMA_REGISTRY_DESERIALIZER_H

#include "glue_schema_registry_schema.h"
#include "glue_schema_registry_error.h"
#include "mutable_byte_array.h"
#include "read_only_byte_array.h"
#include <stdbool.h>
Expand All @@ -11,17 +12,21 @@ typedef struct glue_schema_registry_deserializer {
void *instance_context;
} glue_schema_registry_deserializer;

glue_schema_registry_deserializer *new_glue_schema_registry_deserializer(void);
glue_schema_registry_deserializer *new_glue_schema_registry_deserializer(glue_schema_registry_error **p_err);

void delete_glue_schema_registry_deserializer(glue_schema_registry_deserializer *deserializer);

mutable_byte_array *glue_schema_registry_deserializer_decode(glue_schema_registry_deserializer *deserializer,
read_only_byte_array *array);
read_only_byte_array *array,
glue_schema_registry_error **p_err);

glue_schema_registry_schema *glue_schema_registry_deserializer_decode_schema(glue_schema_registry_deserializer *deserializer,
read_only_byte_array *array);
glue_schema_registry_schema *
glue_schema_registry_deserializer_decode_schema(glue_schema_registry_deserializer *deserializer,
read_only_byte_array *array,
glue_schema_registry_error **p_err);

bool glue_schema_registry_deserializer_can_decode(glue_schema_registry_deserializer *deserializer,
read_only_byte_array *array);
read_only_byte_array *array,
glue_schema_registry_error **p_err);

#endif //GLUE_SCHEMA_REGISTRY_DESERIALIZER_H
39 changes: 39 additions & 0 deletions native-schema-registry/c/include/glue_schema_registry_error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef NATIVE_SCHEMA_REGISTRY_GLUE_SCHEMA_REGISTRY_ERROR_H
#define NATIVE_SCHEMA_REGISTRY_GLUE_SCHEMA_REGISTRY_ERROR_H

#include <stdio.h>

//Error codes are arbitrarily listed from 5000. No specific reason.
#define ERR_CODE_INVALID_STATE 5000
#define ERR_CODE_NULL_PARAMETERS 5001
#define ERR_CODE_GRAALVM_INIT_EXCEPTION 5002
#define ERR_CODE_GRAALVM_TEARDOWN_EXCEPTION 5003
#define ERR_CODE_INVALID_PARAMETERS 5004
#define ERR_CODE_RUNTIME_ERROR 5005

//TODO: Improve error reporting to respect logging levels.
#define log_warn(msg, code) fprintf(stderr, "WARN: %s, Code: %d\n", msg, code)

/** Defines the glue_schema_registry_error structure for holding error messages and codes
* resulting from function executions.
*/
typedef struct glue_schema_registry_error {
char * msg;
int code;
} glue_schema_registry_error;

glue_schema_registry_error * new_glue_schema_registry_error(const char * err_msg, int err_code);

void delete_glue_schema_registry_error(glue_schema_registry_error *error);

/**
* Creates an instance of glue_schema_registry_error and writes it to the given
* glue_schema_registry_error pointer holder (*p_err). It is expected that *p_err
* is initialized by caller.
* @param p_err Initialized glue_schema_registry_error pointer holder.
* @param msg Error message to write.
* @param code Non-zero error code.
*/
void throw_error(glue_schema_registry_error **p_err, const char *msg, int code);

#endif //NATIVE_SCHEMA_REGISTRY_GLUE_SCHEMA_REGISTRY_ERROR_H
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef GLUE_SCHEMA_REGISTRY_SCHEMA_H
#define GLUE_SCHEMA_REGISTRY_SCHEMA_H

#include "glue_schema_registry_error.h"

/*
* Glue Schema Registry Schema structure that represents
* schema object required by Glue Schema Registry Serializers / De-serializers.
Expand All @@ -21,7 +23,8 @@ typedef struct glue_schema_registry_schema {
glue_schema_registry_schema *new_glue_schema_registry_schema(
const char * schema_name,
const char * schema_def,
const char * data_format
const char * data_format,
glue_schema_registry_error ** p_err
);

//Deletes the glue schema registry schema.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define GLUE_SCHEMA_REGISTRY_SERIALIZER_H

#include "glue_schema_registry_schema.h"
#include "glue_schema_registry_error.h"
#include "mutable_byte_array.h"
#include "read_only_byte_array.h"

Expand All @@ -10,14 +11,15 @@ typedef struct glue_schema_registry_serializer {
void *instance_context;
} glue_schema_registry_serializer;

glue_schema_registry_serializer *new_glue_schema_registry_serializer(void);
glue_schema_registry_serializer *new_glue_schema_registry_serializer(glue_schema_registry_error **p_err);

void delete_glue_schema_registry_serializer(glue_schema_registry_serializer *serializer);

//Encodes the GSR Schema with a byte array.
mutable_byte_array *glue_schema_registry_serializer_encode(glue_schema_registry_serializer *serializer,
read_only_byte_array * array,
const char * transport_name,
glue_schema_registry_schema *gsr_schema);
glue_schema_registry_schema *gsr_schema,
glue_schema_registry_error **p_err);

#endif //GLUE_SCHEMA_REGISTRY_SERIALIZER_H
9 changes: 6 additions & 3 deletions native-schema-registry/c/include/mutable_byte_array.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef MUTABLE_BYTE_ARRAY_H
#define MUTABLE_BYTE_ARRAY_H
#include <stdlib.h>
#include "../include/glue_schema_registry_error.h"

//Integer.MAX_VALUE in Java
//This gives ~2.1Gb limit on a record.
Expand All @@ -16,8 +17,9 @@ typedef struct mutable_byte_array {
/**
* Initializes a mutable byte array of size `len`
* The data is initially set to '0'
* Caller can optionally provide pointer holder to glue_schema_registry_error to read error messages.
*/
mutable_byte_array * new_mutable_byte_array(size_t len);
mutable_byte_array * new_mutable_byte_array(size_t len, glue_schema_registry_error **p_err);

/**
* Free the data and the pointer to the mutable byte array.
Expand All @@ -32,11 +34,12 @@ unsigned char * mutable_byte_array_get_data(mutable_byte_array * array);
/**
* Writes a single byte at given index in the byte array.
*/
void mutable_byte_array_write(mutable_byte_array * array, size_t index, unsigned char byte);
void mutable_byte_array_write(mutable_byte_array * array, size_t index, unsigned char byte,
glue_schema_registry_error **p_err);

/**
* Return the len of the byte-array
*/
size_t mutable_byte_array_get_max_len(mutable_byte_array * array);

#endif //MUTABLE_BYTE_ARRAY_H
#endif //MUTABLE_BYTE_ARRAY_H
4 changes: 3 additions & 1 deletion native-schema-registry/c/include/read_only_byte_array.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef READ_ONLY_BYTE_ARRAY_H
#define READ_ONLY_BYTE_ARRAY_H
#include <stdlib.h>
#include "../include/glue_schema_registry_error.h"

typedef struct read_only_byte_array {
unsigned char * data;
Expand All @@ -12,8 +13,9 @@ typedef struct read_only_byte_array {
* provide a view over the data. Attempts to modify the data can result in
* unintended consequences or crashes.
* The caller must guarantee the memory is valid and is of exactly `len`
* Caller can optionally provide pointer holder to glue_schema_registry_error to read error messages.
*/
read_only_byte_array * new_read_only_byte_array(unsigned char *data, size_t len);
read_only_byte_array * new_read_only_byte_array(unsigned char *data, size_t len, glue_schema_registry_error **p_err);

/**
* Deletes the byte array instance but not the underlying data.
Expand Down
4 changes: 4 additions & 0 deletions native-schema-registry/c/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ execute_process(COMMAND sed -ie "s/<graal_isolate.h>/\"graal_isolate.h\"/" ${LIB

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ftest-coverage -fprofile-arcs -ggdb3 -O2 -Wall")

include_directories("include")

#Adding modules in the build order.
add_library(
${DATA_TYPES_MODULE_NAME} SHARED
glue_schema_registry_schema.c
read_only_byte_array.c
mutable_byte_array.c
glue_schema_registry_error.c
)
add_custom_command(
TARGET ${DATA_TYPES_MODULE_NAME}
Expand Down Expand Up @@ -38,6 +41,7 @@ add_library(

target_link_libraries(
${SERDE_MODULE_NAME}
${DATA_TYPES_MODULE_NAME}
${NATIVE_SCHEMA_REGISTRY_MODULE_NAME}
)

Expand Down
41 changes: 23 additions & 18 deletions native-schema-registry/c/src/glue_schema_registry_deserializer.c
Original file line number Diff line number Diff line change
@@ -1,80 +1,85 @@
#include "../include/glue_schema_registry_deserializer.h"
#include "../include/error_handling.h"
#include "../../target/libnativeschemaregistry.h"
#include <stdlib.h>

glue_schema_registry_deserializer * new_glue_schema_registry_deserializer() {
glue_schema_registry_deserializer * new_glue_schema_registry_deserializer(glue_schema_registry_error **p_err) {
glue_schema_registry_deserializer *deserializer = NULL;
deserializer =
(glue_schema_registry_deserializer *) malloc(sizeof(glue_schema_registry_deserializer));

int ret = graal_create_isolate(NULL, NULL, (graal_isolatethread_t **) &deserializer->instance_context);
if (ret != 0) {
log_error("Failed to initialize GraalVM isolate.", ERR_CODE_GRAALVM_INIT_EXCEPTION);
delete_glue_schema_registry_deserializer(deserializer);
throw_error(p_err, "Failed to initialize GraalVM isolate.", ERR_CODE_GRAALVM_INIT_EXCEPTION);
return NULL;
}
//TODO: Handle errors here.
//TODO: Handle errors here when configuration is added.
initialize_deserializer(deserializer->instance_context);
return deserializer;
}

void delete_glue_schema_registry_deserializer(glue_schema_registry_deserializer * deserializer) {
if (deserializer == NULL) {
log_error("Deserializer is NULL", ERR_CODE_NULL_PARAMETERS);
log_warn("Deserializer is NULL", ERR_CODE_NULL_PARAMETERS);
return;
}
if (deserializer->instance_context != NULL) {
int ret = graal_tear_down_isolate(deserializer->instance_context);
if (ret != 0) {
log_error("Error tearing down the graal isolate instance.", ERR_CODE_GRAALVM_TEARDOWN_EXCEPTION);
log_warn("Error tearing down the graal isolate instance.", ERR_CODE_GRAALVM_TEARDOWN_EXCEPTION);
}
deserializer->instance_context = NULL;
}

free(deserializer);
}

mutable_byte_array *glue_schema_registry_deserializer_decode(glue_schema_registry_deserializer * deserializer, read_only_byte_array *array) {
mutable_byte_array *glue_schema_registry_deserializer_decode(glue_schema_registry_deserializer * deserializer,
read_only_byte_array *array,
glue_schema_registry_error **p_err) {
if (deserializer == NULL || deserializer->instance_context == NULL) {
log_error("Deserializer instance or instance context is null.", ERR_CODE_INVALID_STATE);
throw_error(p_err, "Deserializer instance or instance context is null.", ERR_CODE_INVALID_STATE);
return NULL;
}

if (array == NULL || array->len == 0) {
log_error("Byte array cannot be null", ERR_CODE_NULL_PARAMETERS);
throw_error(p_err, "Byte array cannot be null", ERR_CODE_NULL_PARAMETERS);
return NULL;
}

return decode(deserializer->instance_context, array);
return decode(deserializer->instance_context, array, p_err);
}

glue_schema_registry_schema *glue_schema_registry_deserializer_decode_schema(glue_schema_registry_deserializer * deserializer, read_only_byte_array *array) {
glue_schema_registry_schema *glue_schema_registry_deserializer_decode_schema(glue_schema_registry_deserializer * deserializer,
read_only_byte_array *array,
glue_schema_registry_error **p_err) {
if (deserializer == NULL || deserializer->instance_context == NULL) {
log_error("Deserializer instance or instance context is null.", ERR_CODE_INVALID_STATE);
throw_error(p_err, "Deserializer instance or instance context is null.", ERR_CODE_INVALID_STATE);
return NULL;
}

if (array == NULL || array->len == 0) {
log_error("Byte array cannot be null", ERR_CODE_NULL_PARAMETERS);
throw_error(p_err, "Byte array cannot be null", ERR_CODE_NULL_PARAMETERS);
return NULL;
}

glue_schema_registry_schema * schema = decode_schema(deserializer->instance_context, array);
glue_schema_registry_schema * schema = decode_schema(deserializer->instance_context, array, p_err);
return schema;
}

bool glue_schema_registry_deserializer_can_decode(glue_schema_registry_deserializer * deserializer, read_only_byte_array *array) {
bool glue_schema_registry_deserializer_can_decode(glue_schema_registry_deserializer * deserializer,
read_only_byte_array *array,
glue_schema_registry_error **p_err) {
if (deserializer == NULL || deserializer->instance_context == NULL) {
log_error("Deserializer instance or instance context is null.", ERR_CODE_INVALID_STATE);
throw_error(p_err, "Deserializer instance or instance context is null.", ERR_CODE_INVALID_STATE);
return NULL;
}

if (array == NULL || array->len == 0) {
log_error("Byte array cannot be null", ERR_CODE_NULL_PARAMETERS);
throw_error(p_err, "Byte array cannot be null", ERR_CODE_NULL_PARAMETERS);
return NULL;
}

return can_decode(deserializer->instance_context, array);
return can_decode(deserializer->instance_context, array, p_err);
}

51 changes: 51 additions & 0 deletions native-schema-registry/c/src/glue_schema_registry_error.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <stdlib.h>
#include <string.h>
#include "../include/glue_schema_registry_error.h"

static int validate(const char *err_msg) {
if (err_msg == NULL) {
return 1;
}

return 0;
}

glue_schema_registry_error *new_glue_schema_registry_error(
const char *err_msg,
int err_code) {
if (validate(err_msg) != 0) {
log_warn("Error message cannot be null", ERR_CODE_NULL_PARAMETERS);
return NULL;
}
glue_schema_registry_error *error = NULL;
error = (glue_schema_registry_error *) malloc(sizeof(glue_schema_registry_error));
error->msg = strdup(err_msg);
error->code = err_code;

return error;
}

void delete_glue_schema_registry_error(glue_schema_registry_error *error) {
if (error == NULL) {
log_warn("Error pointer passed is NULL", ERR_CODE_NULL_PARAMETERS);
return;
}

if (error->msg != NULL) {
free(error->msg);
error->msg = NULL;
}
error->code = 0;

free(error);
}

//Create and set the error to the glue_schema_registry_error pointer holder.
void throw_error(glue_schema_registry_error **p_err, const char *msg, int code) {
if (p_err == NULL) {
return;
}

glue_schema_registry_error *err = new_glue_schema_registry_error(msg, code);
*p_err = err;
}
Loading

0 comments on commit 06543a6

Please sign in to comment.