Skip to content

Commit

Permalink
store_avro_kafka: add naive defaults to avro schemas
Browse files Browse the repository at this point in the history
In order to support avro schema evolution, add naive default
values to each field (except the timestamp, which ldms metric
sets will always contain). Technically for the default form of
schema evolution in kafka, we only need to have defaults for
any _new_ fields that we add when evolving a schema. However,
we are not tracking the various iterations of the schema over
time, so just setting defaults for all fields allows us
greater flexibility.
  • Loading branch information
morrone authored and tom95858 committed Feb 1, 2024
1 parent e33463b commit 92830e9
Showing 1 changed file with 50 additions and 4 deletions.
54 changes: 50 additions & 4 deletions ldms/src/ldmsd/ldmsd_decomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,41 @@ static const char *col_type_str(enum ldms_value_type type)
return type_str[type];
}

static const char *col_default(enum ldms_value_type type)
{
static char *default_str[] = {
[LDMS_V_CHAR] = "\" \"",
[LDMS_V_U8] = "0",
[LDMS_V_S8] = "0",
[LDMS_V_U16] = "0",
[LDMS_V_S16] = "0",
[LDMS_V_U32] = "0",
[LDMS_V_S32] = "0",
[LDMS_V_U64] = "0",
[LDMS_V_S64] = "0",
[LDMS_V_F32] = "0.0",
[LDMS_V_D64] = "0.0",
[LDMS_V_CHAR_ARRAY] = "\"\"",
[LDMS_V_U8_ARRAY] = "[]",
[LDMS_V_S8_ARRAY] = "[]",
[LDMS_V_U16_ARRAY] = "[]",
[LDMS_V_S16_ARRAY] = "[]",
[LDMS_V_U32_ARRAY] = "[]",
[LDMS_V_S32_ARRAY] = "[]",
[LDMS_V_U64_ARRAY] = "[]",
[LDMS_V_S64_ARRAY] = "[]",
[LDMS_V_F32_ARRAY] = "[]",
[LDMS_V_D64_ARRAY] = "[]",
[LDMS_V_LIST] = "nosup",
[LDMS_V_LIST_ENTRY] = "nosup",
[LDMS_V_RECORD_TYPE] = "nosup",
[LDMS_V_RECORD_INST] = "nosup",
[LDMS_V_RECORD_ARRAY] = "nosup",
[LDMS_V_TIMESTAMP] = "0"
};
return default_str[type];
}

static char get_avro_char(char c)
{
if (isalnum(c))
Expand Down Expand Up @@ -844,6 +879,12 @@ int ldmsd_row_to_json_avro_schema(ldmsd_row_t row, char **str, size_t *len)
}
switch (col->type) {
case LDMS_V_TIMESTAMP:
rc = strbuf_printf(&h,
"{\"name\":\"%s\",\"type\":%s}",
avro_name, col_type_str(col->type));
if (rc)
goto err_0;
break;
case LDMS_V_CHAR:
case LDMS_V_U8:
case LDMS_V_S8:
Expand All @@ -856,8 +897,11 @@ int ldmsd_row_to_json_avro_schema(ldmsd_row_t row, char **str, size_t *len)
case LDMS_V_F32:
case LDMS_V_D64:
case LDMS_V_CHAR_ARRAY:
rc = strbuf_printf(&h, "{\"name\":\"%s\",\"type\":%s}",
avro_name, col_type_str(col->type));
rc = strbuf_printf(&h,
"{\"name\":\"%s\",\"type\":%s,"
"\"default\":%s}",
avro_name, col_type_str(col->type),
col_default(col->type));
if (rc)
goto err_0;
break;
Expand All @@ -873,8 +917,10 @@ int ldmsd_row_to_json_avro_schema(ldmsd_row_t row, char **str, size_t *len)
case LDMS_V_D64_ARRAY:
rc = strbuf_printf(&h,
"{\"name\":\"%s\","
"\"type\":{ \"type\" : \"array\", \"items\": %s }}",
avro_name, col_type_str(col->type));
"\"type\":{ \"type\" : \"array\", \"items\": %s },"
"\"default\":%s}",
avro_name, col_type_str(col->type),
col_default(col->type));
if (rc)
goto err_0;
break;
Expand Down

0 comments on commit 92830e9

Please sign in to comment.