jobid format and parse

This commit is contained in:
Rongsong Shen 2025-11-19 14:44:49 +08:00
parent 26e3c21c87
commit 3488b62716
6 changed files with 212 additions and 15 deletions

View file

@ -4,6 +4,7 @@
#include <unity/unity.h>
#include "idgen.h"
#include "utils.h"
#include <errno.h>
static void test_idgen_invalid(void)
@ -35,9 +36,13 @@ static void test_idgen1(void)
TEST_ASSERT_TRUE(rc >= 0);
for (i = 0; i < 20; i++) {
char buf[256];
rc = idgen(&ctx, 0x0, 0x0, 1, &idx);
TEST_ASSERT_TRUE(rc >= 0);
printf("\tidx = %lx\n", idx);
memset(buf, 0x0, sizeof(buf));
fmt_jobid(buf, sizeof(buf), idx);
printf("\tjob = %s idx %lx\n", buf, idx);
}
idgen_ctx_fini(&ctx);
@ -56,12 +61,24 @@ static void test_idgen_array(void)
int k;
for (k = 0; k < 5; k++) {
int j;
rc = idgen(&ctx, 0x0, 0x0, i + 1, &idx);
TEST_ASSERT_TRUE(rc >= 0);
if (i >= 1) {
TEST_ASSERT_TRUE((idx & MAX_ARRAY_IDX) == 0);
}
printf("\tidx = %lx\n", idx);
for (j = 0; j < i; j++) {
char buf[256];
memset(buf, 0x0, sizeof(buf));
fmt_jobid(buf, sizeof(buf), idx + j);
printf("\tjob = %s idx %lx \n", buf, idx + j);
}
printf("\n");
}
}

View file

@ -99,6 +99,43 @@ void test_roundup(void)
TEST_ASSERT_TRUE(ROUNDUP(31, 5) == 32);
}
void test_extract_bits(void) {
uint64_t id = 0xfefefefeffff;
TEST_ASSERT_TRUE(EXTRACT_BITS(id, 0, 4) == 0xf);
TEST_ASSERT_TRUE(EXTRACT_BITS(id, 16, 4) == 0xe);
TEST_ASSERT_TRUE(EXTRACT_BITS(id, 16, 8) == 0xfe);
TEST_ASSERT_TRUE(EXTRACT_BITS(id, 20, 12) == 0xfef);
TEST_ASSERT_TRUE(EXTRACT_BITS(id, 44, 4) == 0xf);
TEST_ASSERT_TRUE(EXTRACT_BITS(id, 40, 8) == 0xfe);
}
static void do_parse_jobid_test(uint64_t jobid) {
uint64_t jobid2;
int rc;
char buf[256];
rc = fmt_jobid(buf, sizeof(buf), jobid);
printf("\t\tCheck jobid %s\n", buf);
TEST_ASSERT_TRUE(rc > 0);
rc = parse_jobid(&jobid2, buf);
TEST_ASSERT_TRUE(rc >= 0);
TEST_ASSERT_TRUE(jobid == jobid2);
}
void test_parse_jobid1(void) {
uint64_t jobid = 0x400691c2fd498003;
do_parse_jobid_test(jobid);
}
void test_parse_jobid2(void) {
uint64_t jobid = 0x691d3c1d00013;
do_parse_jobid_test(jobid);
}
void test_utils(void)
{
printf("\n>> utils test\n\n");
@ -106,4 +143,7 @@ void test_utils(void)
RUN_TEST(test_timewindow_config_dup);
RUN_TEST(test_dup_threshold);
RUN_TEST(test_roundup);
RUN_TEST(test_extract_bits);
RUN_TEST(test_parse_jobid1);
RUN_TEST(test_parse_jobid2);
}

View file

@ -15,6 +15,7 @@
#include <execinfo.h>
#endif
#include <regex.h>
#include <tlog.h>
#include "config.h"
@ -409,3 +410,110 @@ void free_strlist(xce_strlist_t* l)
FREEUP(l->data);
}
int fmt_jobid(char* buf, size_t sz, uint64_t jobid)
{
uint32_t tag, id_cluster, id_apiserver, id_timestamp;
char data[128];
int len = 0;
tag = EXTRACT_ID_TAG(jobid);
id_cluster = EXTRACT_ID_CLUSTER(jobid);
id_apiserver = EXTRACT_ID_APISERVER(jobid);
id_timestamp = EXTRACT_ID_TIMESTAMP(jobid);
memset(data, 0x0, sizeof(data));
if ((tag & TAG_JOB_ARRAY) != 0) {
uint32_t id_job, id_idx;
id_job = EXTRACT_ARRAY_JOB(jobid);
id_idx = EXTRACT_ARRAY_IDX(jobid);
snprintf(data, sizeof(data) - 1, "%d-%d-%d-%d[%d]", id_cluster,
id_apiserver, id_timestamp, id_job, id_idx);
} else {
uint32_t id_seq;
id_seq = EXTRACT_ID_SEQUENCE(jobid);
snprintf(data, sizeof(data) - 1, "%d-%d-%d-%d", id_cluster,
id_apiserver, id_timestamp, id_seq);
}
len = strlen(data);
if (len > sz - 1) {
return -1;
}
strncpy(buf, data, sz);
return len;
}
int parse_jobid(uint64_t* jobid, char* buf)
{
regex_t regex;
regmatch_t pmatch[N_MAX_FIELDS_JOBID + 1];
int rc;
int len = strlen(buf);
if ((jobid == NULL) || (buf == NULL)) {
return -EINVAL;
}
rc = regcomp(&regex, REGEX_JOBARRAY_ID, REG_EXTENDED);
if (rc != 0) {
return -errno;
}
*jobid = 0;
rc = regexec(&regex, buf, N_EL_ARRAY(pmatch), pmatch, 0);
if (rc == 0) {
regfree(&regex);
if ((pmatch[0].rm_so == 0) && (pmatch[0].rm_eo == len)) {
uint32_t tag, id_cluster, id_apiserver, id_timestamp, id_job,
id_idx;
tag = 0x01; /* job array */
id_cluster = atoi(&buf[pmatch[1].rm_so]);
id_apiserver = atoi(&buf[pmatch[2].rm_so]);
id_timestamp = atoi(&buf[pmatch[3].rm_so]);
id_job = atoi(&buf[pmatch[4].rm_so]);
id_idx = atoi(&buf[pmatch[5].rm_so]);
*jobid = MAKE_JOBID(id_cluster, id_apiserver, id_timestamp, tag,
id_job + id_idx);
return 1;
}
}
regfree(&regex);
rc = regcomp(&regex, REGEX_JOBID, REG_EXTENDED);
if (rc != 0) {
return -errno;
}
rc = regexec(&regex, buf, N_EL_ARRAY(pmatch), pmatch, 0);
if (rc == 0) {
uint32_t id_cluster, id_apiserver, id_timestamp, id_job;
if ((pmatch[0].rm_so != 0) || (pmatch[0].rm_eo != len)) {
regfree(&regex);
return -EINVAL;
}
id_cluster = atoi(&buf[pmatch[1].rm_so]);
id_apiserver = atoi(&buf[pmatch[2].rm_so]);
id_timestamp = atoi(&buf[pmatch[3].rm_so]);
id_job = atoi(&buf[pmatch[4].rm_so]);
*jobid
= MAKE_JOBID(id_cluster, id_apiserver, id_timestamp, 0x0, id_job);
regfree(&regex);
return 1;
}
regfree(&regex);
return -EINVAL;
}