Skip to content

Commit

Permalink
Merge pull request #753 from fengzeroz/main
Browse files Browse the repository at this point in the history
fix(manager): refuse to delete single node
  • Loading branch information
fengzeroz authored Sep 2, 2022
2 parents 84309f1 + e014f77 commit 9723978
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 21 deletions.
1 change: 1 addition & 0 deletions ft/resource/error.resource
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ${NEU_ERR_NODE_IS_RUNNING} 2007
${NEU_ERR_NODE_NOT_RUNNING} 2008
${NEU_ERR_NODE_IS_STOPED} 2009
${NEU_ERR_NODE_NAME_TOO_LONG} 2010
${NEU_ERR_NODE_NOT_ALLOW_DELETE} 2011

${NEU_ERR_GROUP_ALREADY_SUBSCRIBED} 2101
${NEU_ERR_GROUP_NOT_SUBSCRIBE} 2102
Expand Down
1 change: 1 addition & 0 deletions include/neuron/errcodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ typedef enum {
NEU_ERR_NODE_NOT_RUNNING = 2008,
NEU_ERR_NODE_IS_STOPED = 2009,
NEU_ERR_NODE_NAME_TOO_LONG = 2010,
NEU_ERR_NODE_NOT_ALLOW_DELETE = 2011,

NEU_ERR_GROUP_ALREADY_SUBSCRIBED = 2101,
NEU_ERR_GROUP_NOT_SUBSCRIBE = 2102,
Expand Down
1 change: 1 addition & 0 deletions plugins/restful/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ int http_response(nng_aio *aio, neu_err_code_e code, char *content)
case NEU_ERR_GROUP_EXIST:
case NEU_ERR_GROUP_NOT_ALLOW:
case NEU_ERR_LIBRARY_NOT_ALLOW_CREATE_INSTANCE:
case NEU_ERR_NODE_NOT_ALLOW_DELETE:
status = NNG_HTTP_STATUS_CONFLICT;
break;
case NEU_ERR_TAG_ATTRIBUTE_NOT_SUPPORT:
Expand Down
54 changes: 33 additions & 21 deletions src/core/manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,34 +290,46 @@ static int manager_loop(enum neu_event_io_type type, int fd, void *usr_data)
neu_resp_error_t error = { 0 };
neu_adapter_t * adapter =
neu_node_manager_find(manager->node_manager, cmd->node);
bool single =
neu_node_manager_is_single(manager->node_manager, cmd->node);

strcpy(header->receiver, cmd->node);
if (adapter != NULL) {
manager_storage_del_node(manager, cmd->node);
header->type = NEU_REQ_NODE_UNINIT;
forward_msg(manager, msg, header->receiver);
if (neu_adapter_get_type(adapter) == NEU_NA_TYPE_DRIVER) {
UT_array *apps = neu_subscribe_manager_find_by_driver(
manager->subscribe_manager, cmd->node);

utarray_foreach(apps, neu_app_subscribe_t *, app)
{
neu_reqresp_node_deleted_t resp = { 0 };
header->type = NEU_REQRESP_NODE_DELETED;

strcpy(resp.node, header->receiver);
strcpy(header->receiver, app->app_name);
strcpy(header->sender, "manager");
reply(manager, header, &resp);
}
utarray_free(apps);
}
} else {
if (adapter == NULL) {
error.error = NEU_ERR_NODE_NOT_EXIST;
header->type = NEU_RESP_ERROR;
neu_msg_exchange(header);
reply(manager, header, &error);
break;
}

if (single) {
error.error = NEU_ERR_NODE_NOT_ALLOW_DELETE;
header->type = NEU_RESP_ERROR;
neu_msg_exchange(header);
reply(manager, header, &error);
break;
}

manager_storage_del_node(manager, cmd->node);
header->type = NEU_REQ_NODE_UNINIT;
forward_msg(manager, msg, header->receiver);
if (neu_adapter_get_type(adapter) == NEU_NA_TYPE_DRIVER) {
UT_array *apps = neu_subscribe_manager_find_by_driver(
manager->subscribe_manager, cmd->node);

utarray_foreach(apps, neu_app_subscribe_t *, app)
{
neu_reqresp_node_deleted_t resp = { 0 };
header->type = NEU_REQRESP_NODE_DELETED;

strcpy(resp.node, header->receiver);
strcpy(header->receiver, app->app_name);
strcpy(header->sender, "manager");
reply(manager, header, &resp);
}
utarray_free(apps);
}

break;
}
case NEU_RESP_NODE_UNINIT: {
Expand Down
12 changes: 12 additions & 0 deletions src/core/node_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ neu_adapter_t *neu_node_manager_find(neu_node_manager_t *mgr, const char *name)
return adapter;
}

bool neu_node_manager_is_single(neu_node_manager_t *mgr, const char *name)
{
node_entity_t *node = NULL;

HASH_FIND_STR(mgr->nodes, name, node);
if (node != NULL) {
return node->single;
}

return false;
}

UT_array *neu_node_manager_get_pipes(neu_node_manager_t *mgr,
neu_node_type_e type)
{
Expand Down
1 change: 1 addition & 0 deletions src/core/node_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ UT_array *neu_node_manager_get_all(neu_node_manager_t *mgr);
UT_array * neu_node_manager_get_adapter(neu_node_manager_t *mgr,
neu_node_type_e type);
neu_adapter_t *neu_node_manager_find(neu_node_manager_t *mgr, const char *name);
bool neu_node_manager_is_single(neu_node_manager_t *mgr, const char *name);

// nng_pipe array
UT_array *neu_node_manager_get_pipes(neu_node_manager_t *mgr,
Expand Down

0 comments on commit 9723978

Please sign in to comment.