Skip to content

Commit 10a814d

Browse files
committed
feat(tag):supports renaming tags
1 parent 3f6517a commit 10a814d

File tree

26 files changed

+515
-3
lines changed

26 files changed

+515
-3
lines changed

include/neuron/msg.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ typedef enum neu_reqresp_type {
114114
NEU_RESP_UPDATE_TAG,
115115
NEU_REQ_GET_TAG,
116116
NEU_RESP_GET_TAG,
117+
NEU_REQ_RENAME_TAG,
118+
NEU_RESP_RENAME_TAG,
117119

118120
NEU_REQ_ADD_PLUGIN,
119121
NEU_REQ_DEL_PLUGIN,
@@ -169,6 +171,7 @@ typedef enum neu_reqresp_type {
169171
NEU_REQ_ADD_TAG_EVENT,
170172
NEU_REQ_DEL_TAG_EVENT,
171173
NEU_REQ_UPDATE_TAG_EVENT,
174+
NEU_REQ_RENAME_TAG_EVENT,
172175
NEU_REQ_IMPORT_TAGS_EVENT,
173176

174177
NEU_REQ_ADD_GTAG_EVENT,
@@ -279,6 +282,8 @@ static const char *neu_reqresp_type_string_t[] = {
279282
[NEU_RESP_UPDATE_TAG] = "NEU_RESP_UPDATE_TAG",
280283
[NEU_REQ_GET_TAG] = "NEU_REQ_GET_TAG",
281284
[NEU_RESP_GET_TAG] = "NEU_RESP_GET_TAG",
285+
[NEU_REQ_RENAME_TAG] = "NEU_REQ_RENAME_TAG",
286+
[NEU_RESP_RENAME_TAG] = "NEU_RESP_RENAME_TAG",
282287

283288
[NEU_REQ_ADD_PLUGIN] = "NEU_REQ_ADD_PLUGIN",
284289
[NEU_REQ_DEL_PLUGIN] = "NEU_REQ_DEL_PLUGIN",
@@ -333,6 +338,7 @@ static const char *neu_reqresp_type_string_t[] = {
333338
[NEU_REQ_ADD_TAG_EVENT] = "NEU_REQ_ADD_TAG_EVENT",
334339
[NEU_REQ_DEL_TAG_EVENT] = "NEU_REQ_DEL_TAG_EVENT",
335340
[NEU_REQ_UPDATE_TAG_EVENT] = "NEU_REQ_UPDATE_TAG_EVENT",
341+
[NEU_REQ_RENAME_TAG_EVENT] = "NEU_REQ_RENAME_TAG_EVENT",
336342
[NEU_REQ_IMPORT_TAGS_EVENT] = "NEU_REQ_IMPORT_TAGS_EVENT",
337343
[NEU_REQ_ADD_GTAG_EVENT] = "NEU_REQ_ADD_GTAG_EVENT",
338344
[NEU_REQ_ADD_PLUGIN_EVENT] = "NEU_REQ_ADD_PLUGIN_EVENT",
@@ -730,6 +736,17 @@ typedef struct neu_resp_get_tag {
730736
UT_array *tags; // array neu_datatag_t
731737
} neu_resp_get_tag_t;
732738

739+
typedef struct {
740+
char driver[NEU_NODE_NAME_LEN];
741+
char group[NEU_GROUP_NAME_LEN];
742+
char old_name[NEU_TAG_NAME_LEN];
743+
char new_name[NEU_TAG_NAME_LEN];
744+
} neu_req_rename_tag_t;
745+
746+
typedef struct {
747+
int error;
748+
} neu_resp_rename_tag_t;
749+
733750
typedef struct {
734751
char app[NEU_NODE_NAME_LEN];
735752
char driver[NEU_NODE_NAME_LEN];

include/neuron/persist/persist.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,17 @@ int neu_persister_update_tag_value(const char * driver_name,
333333
int neu_persister_delete_tag(const char *driver_name, const char *group_name,
334334
const char *tag_name);
335335

336+
/**
337+
* Rename node tag.
338+
* @param driver_name name of the driver who owns the tags
339+
* @param group_name name of the group
340+
* @param old_name old name of the tag
341+
* @param new_name new name of the tag
342+
* @return 0 on success, non-zero otherwise
343+
*/
344+
int neu_persister_rename_tag(const char *driver_name, const char *group_name,
345+
const char *old_name, const char *new_name);
346+
336347
/**
337348
* Persist subscriptions.
338349
* @param app_name name of the app node

persistence/sqlite.db

228 KB
Binary file not shown.

plugins/restful/datatag_handle.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,72 @@ void handle_update_tags_resp(nng_aio *aio, neu_resp_update_tag_t *resp)
398398
handle_add_tags_resp(aio, resp);
399399
}
400400

401+
void handle_rename_tag(nng_aio *aio)
402+
{
403+
neu_plugin_t *plugin = neu_rest_get_plugin();
404+
405+
NEU_PROCESS_HTTP_REQUEST_VALIDATE_JWT(
406+
aio, neu_json_rename_tag_req_t, neu_json_decode_rename_tag_req, {
407+
int ret = 0;
408+
neu_reqresp_head_t header = { 0 };
409+
neu_req_rename_tag_t cmd = { 0 };
410+
int err_type;
411+
412+
header.ctx = aio;
413+
header.type = NEU_REQ_RENAME_TAG;
414+
header.otel_trace_type = NEU_OTEL_TRACE_TYPE_REST_COMM;
415+
416+
if (strlen(req->node) >= NEU_NODE_NAME_LEN) {
417+
err_type = NEU_ERR_NODE_NAME_TOO_LONG;
418+
goto error;
419+
}
420+
421+
if (strlen(req->group) >= NEU_GROUP_NAME_LEN) {
422+
err_type = NEU_ERR_GROUP_NAME_TOO_LONG;
423+
goto error;
424+
}
425+
426+
if (strlen(req->old_name) == 0 || strlen(req->new_name) == 0) {
427+
err_type = NEU_ERR_PARAM_IS_WRONG;
428+
goto error;
429+
}
430+
431+
if (strlen(req->old_name) >= NEU_TAG_NAME_LEN) {
432+
err_type = NEU_ERR_TAG_NAME_TOO_LONG;
433+
goto error;
434+
}
435+
436+
if (strlen(req->new_name) >= NEU_TAG_NAME_LEN) {
437+
err_type = NEU_ERR_TAG_NAME_TOO_LONG;
438+
goto error;
439+
}
440+
441+
strcpy(cmd.driver, req->node);
442+
strcpy(cmd.group, req->group);
443+
strcpy(cmd.old_name, req->old_name);
444+
strcpy(cmd.new_name, req->new_name);
445+
446+
ret = neu_plugin_op(plugin, header, &cmd);
447+
if (ret != 0) {
448+
NEU_JSON_RESPONSE_ERROR(NEU_ERR_IS_BUSY, {
449+
neu_http_response(aio, NEU_ERR_IS_BUSY, result_error);
450+
});
451+
}
452+
goto success;
453+
454+
error:
455+
NEU_JSON_RESPONSE_ERROR(
456+
err_type, { neu_http_response(aio, err_type, result_error); });
457+
success:;
458+
})
459+
}
460+
461+
void handle_rename_tag_resp(nng_aio *aio, neu_resp_rename_tag_t *resp)
462+
{
463+
NEU_JSON_RESPONSE_ERROR(
464+
resp->error, { neu_http_response(aio, resp->error, result_error); });
465+
}
466+
401467
void handle_get_tags(nng_aio *aio)
402468
{
403469
neu_plugin_t * plugin = neu_rest_get_plugin();

plugins/restful/datatag_handle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ void handle_import_tags_resp(nng_aio *aio, neu_resp_add_tag_t *resp);
3333
void handle_del_tags(nng_aio *aio);
3434
void handle_update_tags(nng_aio *aio);
3535
void handle_update_tags_resp(nng_aio *aio, neu_resp_update_tag_t *resp);
36+
void handle_rename_tag(nng_aio *aio);
37+
void handle_rename_tag_resp(nng_aio *aio, neu_resp_rename_tag_t *resp);
3638
void handle_get_tags(nng_aio *aio);
3739
void handle_get_tags_resp(nng_aio *aio, neu_resp_get_tag_t *tags);
3840

plugins/restful/handle.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ static struct neu_http_handler cors_handler[] = {
212212
{
213213
.url = "/api/v2/simulator/export",
214214
},
215+
{
216+
.url = "/api/v2/tag/rename",
217+
},
215218
};
216219

217220
static struct neu_http_handler rest_handlers[] = {
@@ -294,6 +297,12 @@ static struct neu_http_handler rest_handlers[] = {
294297
.url = "/api/v2/tags",
295298
.value.handler = handle_del_tags,
296299
},
300+
{
301+
.method = NEU_HTTP_METHOD_PUT,
302+
.type = NEU_HTTP_HANDLER_FUNCTION,
303+
.url = "/api/v2/tag/rename",
304+
.value.handler = handle_rename_tag,
305+
},
297306
{
298307
.method = NEU_HTTP_METHOD_POST,
299308
.type = NEU_HTTP_HANDLER_FUNCTION,

plugins/restful/rest.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,13 @@ static int dashb_plugin_request(neu_plugin_t * plugin,
280280
scope, "error", ((neu_resp_update_tag_t *) data)->error);
281281
}
282282
break;
283+
case NEU_RESP_RENAME_TAG:
284+
handle_rename_tag_resp(header->ctx, (neu_resp_rename_tag_t *) data);
285+
if (neu_otel_control_is_started() && trace) {
286+
neu_otel_scope_add_span_attr_int(
287+
scope, "error", ((neu_resp_rename_tag_t *) data)->error);
288+
}
289+
break;
283290
case NEU_RESP_GET_TAG:
284291
handle_get_tags_resp(header->ctx, (neu_resp_get_tag_t *) data);
285292
if (neu_otel_control_is_started() && trace) {

src/adapter/adapter.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,11 @@ static int adapter_command(neu_adapter_t *adapter, neu_reqresp_head_t header,
548548
strcpy(pheader->receiver, cmd->driver);
549549
break;
550550
}
551+
case NEU_REQ_RENAME_TAG: {
552+
neu_req_rename_tag_t *cmd = (neu_req_rename_tag_t *) data;
553+
strcpy(pheader->receiver, cmd->driver);
554+
break;
555+
}
551556
case NEU_REQ_IMPORT_TAGS: {
552557
neu_req_import_tags_t *cmd = (neu_req_import_tags_t *) data;
553558
strcpy(pheader->receiver, cmd->node);
@@ -887,6 +892,7 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
887892
case NEU_RESP_ADD_GTAG:
888893
case NEU_RESP_IMPORT_TAGS:
889894
case NEU_RESP_UPDATE_TAG:
895+
case NEU_RESP_RENAME_TAG:
890896
case NEU_RESP_GET_TAG:
891897
case NEU_RESP_GET_NODE:
892898
case NEU_RESP_GET_PLUGIN:
@@ -912,6 +918,7 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
912918
case NEU_REQ_ADD_TAG_EVENT:
913919
case NEU_REQ_DEL_TAG_EVENT:
914920
case NEU_REQ_UPDATE_TAG_EVENT:
921+
case NEU_REQ_RENAME_TAG_EVENT:
915922
case NEU_REQ_ADD_GTAG_EVENT:
916923
case NEU_REQ_ADD_PLUGIN_EVENT:
917924
case NEU_REQ_DEL_PLUGIN_EVENT:
@@ -1772,6 +1779,41 @@ static int adapter_loop(enum neu_event_io_type type, int fd, void *usr_data)
17721779
reply(adapter, header, &resp);
17731780
break;
17741781
}
1782+
case NEU_REQ_RENAME_TAG: {
1783+
neu_req_rename_tag_t *cmd = (neu_req_rename_tag_t *) &header[1];
1784+
neu_resp_rename_tag_t resp = { 0 };
1785+
1786+
if (adapter->module->type == NEU_NA_TYPE_DRIVER) {
1787+
resp.error = neu_adapter_driver_rename_tag(
1788+
(neu_adapter_driver_t *) adapter, cmd->group, cmd->old_name,
1789+
cmd->new_name);
1790+
if (resp.error == 0) {
1791+
rv = adapter_storage_rename_tag(cmd->driver, cmd->group,
1792+
cmd->old_name,
1793+
cmd->new_name);
1794+
if (rv != 0) {
1795+
// rollback in-memory rename on persistence failure
1796+
nlog_error("persist rename failed, rolling back "
1797+
"tag:%s->%s node:%s grp:%s",
1798+
cmd->old_name, cmd->new_name, cmd->driver,
1799+
cmd->group);
1800+
neu_adapter_driver_rename_tag(
1801+
(neu_adapter_driver_t *) adapter, cmd->group,
1802+
cmd->new_name, cmd->old_name);
1803+
resp.error = NEU_ERR_EINTERNAL;
1804+
} else if (header->monitor) {
1805+
notify_monitor(adapter, NEU_REQ_RENAME_TAG_EVENT, cmd);
1806+
}
1807+
}
1808+
} else {
1809+
resp.error = NEU_ERR_GROUP_NOT_ALLOW;
1810+
}
1811+
1812+
neu_msg_exchange(header);
1813+
header->type = NEU_RESP_RENAME_TAG;
1814+
reply(adapter, header, &resp);
1815+
break;
1816+
}
17751817
case NEU_REQ_NODE_UNINIT: {
17761818
neu_req_node_uninit_t *cmd = (neu_req_node_uninit_t *) &header[1];
17771819
char name[NEU_NODE_NAME_LEN] = { 0 };

src/adapter/driver/cache.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ inline static tkey_t to_key(const char *group, const char *tag)
7171
{
7272
tkey_t key = { 0 };
7373

74-
strcpy(key.group, group);
75-
strcpy(key.tag, tag);
74+
strncpy(key.group, group, sizeof(key.group) - 1);
75+
strncpy(key.tag, tag, sizeof(key.tag) - 1);
7676

7777
return key;
7878
}
@@ -1266,3 +1266,21 @@ void neu_driver_cache_del(neu_driver_cache_t *cache, const char *group,
12661266

12671267
pthread_mutex_unlock(&cache->mtx);
12681268
}
1269+
1270+
void neu_driver_cache_rename(neu_driver_cache_t *cache, const char *group,
1271+
const char *old_tag, const char *new_tag)
1272+
{
1273+
struct elem *elem = NULL;
1274+
tkey_t key = to_key(group, old_tag);
1275+
1276+
pthread_mutex_lock(&cache->mtx);
1277+
HASH_FIND(hh, cache->table, &key, sizeof(tkey_t), elem);
1278+
1279+
if (elem != NULL) {
1280+
HASH_DEL(cache->table, elem);
1281+
elem->key = to_key(group, new_tag);
1282+
HASH_ADD(hh, cache->table, key, sizeof(tkey_t), elem);
1283+
}
1284+
1285+
pthread_mutex_unlock(&cache->mtx);
1286+
}

src/adapter/driver/cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ bool neu_driver_cache_update_change(neu_driver_cache_t *cache,
4343

4444
void neu_driver_cache_del(neu_driver_cache_t *cache, const char *group,
4545
const char *tag);
46+
void neu_driver_cache_rename(neu_driver_cache_t *cache, const char *group,
47+
const char *old_tag, const char *new_tag);
4648

4749
void neu_driver_cache_update_trace(neu_driver_cache_t *cache, const char *group,
4850
void *trace_ctx);

0 commit comments

Comments
 (0)