Skip to content

Commit

Permalink
Refactor: tools: abstract controller IPC interface from crm_resource
Browse files Browse the repository at this point in the history
This is an initial step towards a public API in libcrmcommon.
  • Loading branch information
kgaillot committed Feb 14, 2020
1 parent 3ba5914 commit 079ba8e
Show file tree
Hide file tree
Showing 6 changed files with 757 additions and 198 deletions.
8 changes: 6 additions & 2 deletions tools/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if BUILD_SYSTEMD
systemdsystemunit_DATA = crm_mon.service
endif

noinst_HEADERS = crm_mon.h crm_resource.h
noinst_HEADERS = crm_mon.h crm_resource.h crm_resource_controller.h

pcmkdir = $(datadir)/$(PACKAGE)
pcmk_DATA = report.common report.collector
Expand Down Expand Up @@ -110,7 +110,11 @@ crm_attribute_LDADD = $(top_builddir)/lib/cluster/libcrmcluster.la \
$(top_builddir)/lib/cib/libcib.la \
$(top_builddir)/lib/common/libcrmcommon.la

crm_resource_SOURCES = crm_resource.c crm_resource_ban.c crm_resource_runtime.c crm_resource_print.c
crm_resource_SOURCES = crm_resource.c \
crm_resource_ban.c \
crm_resource_controller.c \
crm_resource_print.c \
crm_resource_runtime.c
crm_resource_LDADD = $(top_builddir)/lib/pengine/libpe_rules.la \
$(top_builddir)/lib/fencing/libstonithd.la \
$(top_builddir)/lib/lrmd/liblrmd.la \
Expand Down
149 changes: 61 additions & 88 deletions tools/crm_resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,50 +43,38 @@ resource_ipc_timeout(gpointer data)
}

static void
resource_ipc_connection_destroy(gpointer user_data)
handle_controller_reply(pcmk_controld_api_t *controld_api, void *api_data,
void *user_data)
{
crm_info("Connection to controller was terminated");
crm_exit(CRM_EX_DISCONNECT);
fprintf(stderr, ".");
if ((controld_api->replies_expected(controld_api) == 0)
&& mainloop && g_main_loop_is_running(mainloop)) {
fprintf(stderr, " OK\n");
crm_debug("Got all the replies we expected");
crm_exit(CRM_EX_OK);
}
}

static void
start_mainloop(void)
handle_controller_drop(pcmk_controld_api_t *controld_api, void *api_data,
void *user_data)
{
if (crmd_replies_needed == 0) {
return;
}

mainloop = g_main_loop_new(NULL, FALSE);
fprintf(stderr, "Waiting for %d %s from the controller",
crmd_replies_needed,
pcmk__plural_alt(crmd_replies_needed, "reply", "replies"));
crm_debug("Waiting for %d %s from the controller",
crmd_replies_needed,
pcmk__plural_alt(crmd_replies_needed, "reply", "replies"));

g_timeout_add(MESSAGE_TIMEOUT_S * 1000, resource_ipc_timeout, NULL);
g_main_loop_run(mainloop);
crm_info("Connection to controller was terminated");
crm_exit(CRM_EX_DISCONNECT);
}

static int
resource_ipc_callback(const char *buffer, ssize_t length, gpointer userdata)
static void
start_mainloop(pcmk_controld_api_t *controld_api)
{
xmlNode *msg = string2xml(buffer);

fprintf(stderr, ".");
crm_log_xml_trace(msg, "[inbound]");

crmd_replies_needed--;
if ((crmd_replies_needed == 0) && mainloop
&& g_main_loop_is_running(mainloop)) {

fprintf(stderr, " OK\n");
crm_debug("Got all the replies we expected");
crm_exit(CRM_EX_OK);
if (controld_api->replies_expected(controld_api) > 0) {
unsigned int count = controld_api->replies_expected(controld_api);

fprintf(stderr, "Waiting for %d %s from the controller",
count, pcmk__plural_alt(count, "reply", "replies"));
mainloop = g_main_loop_new(NULL, FALSE);
g_timeout_add(MESSAGE_TIMEOUT_S * 1000, resource_ipc_timeout, NULL);
g_main_loop_run(mainloop);
}

free_xml(msg);
return 0;
}

static int
Expand Down Expand Up @@ -115,12 +103,6 @@ build_constraint_list(xmlNode *root)
return retval;
}

struct ipc_client_callbacks crm_callbacks = {
.dispatch = resource_ipc_callback,
.destroy = resource_ipc_connection_destroy,
};


/* short option letters still available: eEJkKXyYZ */

/* *INDENT-OFF* */
Expand Down Expand Up @@ -484,13 +466,12 @@ main(int argc, char **argv)
GHashTable *override_params = NULL;

char *xml_file = NULL;
crm_ipc_t *crmd_channel = NULL;
pcmk_controld_api_t *controld_api = NULL;
pe_working_set_t *data_set = NULL;
xmlNode *cib_xml_copy = NULL;
cib_t *cib_conn = NULL;
resource_t *rsc = NULL;
bool recursive = FALSE;
char *our_pid = NULL;

bool validate_cmdline = FALSE; /* whether we are just validating based on command line options */
bool require_resource = TRUE; /* whether command requires that resource be specified */
Expand Down Expand Up @@ -924,8 +905,6 @@ main(int argc, char **argv)
crm_exit(CRM_EX_USAGE);
}

our_pid = crm_getpid_s();

if (do_force) {
crm_debug("Forcing...");
cib_options |= cib_quorum_override;
Expand Down Expand Up @@ -990,20 +969,26 @@ main(int argc, char **argv)

// Establish a connection to the controller if needed
if (require_crmd) {
xmlNode *xml = NULL;
mainloop_io_t *source =
mainloop_add_ipc_client(CRM_SYSTEM_CRMD, G_PRIORITY_DEFAULT, 0, NULL, &crm_callbacks);
crmd_channel = mainloop_get_ipc_client(source);

if (crmd_channel == NULL) {
CMD_ERR("Error connecting to the controller");
rc = -ENOTCONN;
char *client_uuid;
pcmk_controld_api_cb_t dispatch_cb = {
handle_controller_reply, NULL
};
pcmk_controld_api_cb_t destroy_cb = {
handle_controller_drop, NULL
};


client_uuid = crm_getpid_s();
controld_api = pcmk_new_controld_api(crm_system_name, client_uuid);
free(client_uuid);

rc = controld_api->connect(controld_api, true, &dispatch_cb,
&destroy_cb);
if (rc != pcmk_rc_ok) {
CMD_ERR("Error connecting to the controller: %s", pcmk_rc_str(rc));
rc = pcmk_rc2legacy(rc);
goto bail;
}

xml = create_hello_message(our_pid, crm_system_name, "0", "1");
crm_ipc_send(crmd_channel, xml, 0, 0, NULL);
free_xml(xml);
}

/* Handle rsc_cmd appropriately */
Expand Down Expand Up @@ -1086,10 +1071,11 @@ main(int argc, char **argv)
cli_resource_print_cts_constraints(data_set);

} else if (rsc_cmd == 'F') {
rc = cli_resource_fail(crmd_channel, host_uname, rsc_id, data_set);
if (rc == pcmk_ok) {
start_mainloop();
rc = cli_resource_fail(controld_api, host_uname, rsc_id, data_set);
if (rc == pcmk_rc_ok) {
start_mainloop(controld_api);
}
rc = pcmk_rc2legacy(rc);

} else if (rsc_cmd == 'O') {
rc = cli_resource_print_operations(rsc_id, host_uname, TRUE, data_set);
Expand Down Expand Up @@ -1283,53 +1269,49 @@ main(int argc, char **argv)
if (do_force == FALSE) {
rsc = uber_parent(rsc);
}
crmd_replies_needed = 0;

crm_debug("Erasing failures of %s (%s requested) on %s",
rsc->id, rsc_id, (host_uname? host_uname: "all nodes"));
rc = cli_resource_delete(crmd_channel, host_uname, rsc,
operation, interval_spec, TRUE, data_set);
rc = cli_resource_delete(controld_api, host_uname, rsc, operation,
interval_spec, TRUE, data_set);

if ((rc == pcmk_ok) && !BE_QUIET) {
// Show any reasons why resource might stay stopped
cli_resource_check(cib_conn, rsc);
}

if (rc == pcmk_ok) {
start_mainloop();
start_mainloop(controld_api);
}

} else if (rsc_cmd == 'C') {
rc = cli_cleanup_all(crmd_channel, host_uname, operation, interval_spec,
rc = cli_cleanup_all(controld_api, host_uname, operation, interval_spec,
data_set);
if (rc == pcmk_ok) {
start_mainloop();
start_mainloop(controld_api);
}

} else if ((rsc_cmd == 'R') && rsc) {
if (do_force == FALSE) {
rsc = uber_parent(rsc);
}
crmd_replies_needed = 0;

crm_debug("Re-checking the state of %s (%s requested) on %s",
rsc->id, rsc_id, (host_uname? host_uname: "all nodes"));
rc = cli_resource_delete(crmd_channel, host_uname, rsc,
NULL, 0, FALSE, data_set);
rc = cli_resource_delete(controld_api, host_uname, rsc, NULL, 0, FALSE,
data_set);

if ((rc == pcmk_ok) && !BE_QUIET) {
// Show any reasons why resource might stay stopped
cli_resource_check(cib_conn, rsc);
}

if (rc == pcmk_ok) {
start_mainloop();
start_mainloop(controld_api);
}

} else if (rsc_cmd == 'R') {
const char *router_node = host_uname;
xmlNode *msg_data = NULL;
xmlNode *cmd = NULL;
int attr_options = pcmk__node_attr_none;

if (host_uname) {
Expand All @@ -1348,35 +1330,24 @@ main(int argc, char **argv)
}
}

if (crmd_channel == NULL) {
if (controld_api == NULL) {
printf("Dry run: skipping clean-up of %s due to CIB_file\n",
host_uname? host_uname : "all nodes");
rc = pcmk_ok;
goto bail;
}

msg_data = create_xml_node(NULL, "crm-resource-reprobe-op");
crm_xml_add(msg_data, XML_LRM_ATTR_TARGET, host_uname);
if (safe_str_neq(router_node, host_uname)) {
crm_xml_add(msg_data, XML_LRM_ATTR_ROUTER_NODE, router_node);
}

cmd = create_request(CRM_OP_REPROBE, msg_data, router_node,
CRM_SYSTEM_CRMD, crm_system_name, our_pid);
free_xml(msg_data);

crm_debug("Re-checking the state of all resources on %s", host_uname?host_uname:"all nodes");

rc = pcmk_rc2legacy(pcmk__node_attr_request_clear(NULL, host_uname,
NULL, NULL, NULL,
NULL, attr_options));

if (crm_ipc_send(crmd_channel, cmd, 0, 0, NULL) > 0) {
start_mainloop();
if (controld_api->reprobe(controld_api, host_uname,
router_node) == pcmk_rc_ok) {
start_mainloop(controld_api);
}

free_xml(cmd);

} else if (rsc_cmd == 'D') {
xmlNode *msg_data = NULL;

Expand All @@ -1398,12 +1369,14 @@ main(int argc, char **argv)

bail:

free(our_pid);
pe_free_working_set(data_set);
if (cib_conn != NULL) {
cib_conn->cmds->signoff(cib_conn);
cib_delete(cib_conn);
}
if (controld_api != NULL) {
pcmk_free_controld_api(controld_api);
}

if (is_ocf_rc) {
exit_code = rc;
Expand Down
20 changes: 12 additions & 8 deletions tools/crm_resource.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2004-2019 the Pacemaker project contributors
* Copyright 2004-2020 the Pacemaker project contributors
*
* The version control history for this file may have further details.
*
Expand All @@ -21,6 +21,7 @@
#include <crm/pengine/status.h>
#include <crm/pengine/internal.h>
#include <pacemaker-internal.h>
#include "crm_resource_controller.h"

extern bool print_pending;

Expand All @@ -30,12 +31,13 @@ extern bool BE_QUIET;
extern int resource_verbose;

extern int cib_options;
extern int crmd_replies_needed;

extern char *move_lifetime;

extern const char *attr_set_type;

extern pcmk_controld_api_cb_t controld_api_cb;

/* ban */
int cli_resource_prefer(const char *rsc_id, const char *host, cib_t * cib_conn);
int cli_resource_ban(const char *rsc_id, const char *host, GListPtr allnodes, cib_t * cib_conn);
Expand All @@ -61,14 +63,16 @@ int cli_resource_print_operations(const char *rsc_id, const char *host_uname, bo

/* runtime */
void cli_resource_check(cib_t * cib, resource_t *rsc);
int cli_resource_fail(crm_ipc_t * crmd_channel, const char *host_uname, const char *rsc_id, pe_working_set_t * data_set);
int cli_resource_fail(pcmk_controld_api_t *controld_api,
const char *host_uname, const char *rsc_id,
pe_working_set_t *data_set);
int cli_resource_search(resource_t *rsc, const char *requested_name,
pe_working_set_t *data_set);
int cli_resource_delete(crm_ipc_t *crmd_channel, const char *host_uname,
resource_t *rsc, const char *operation,
const char *interval_spec, bool just_failures,
pe_working_set_t *data_set);
int cli_cleanup_all(crm_ipc_t *crmd_channel, const char *node_name,
int cli_resource_delete(pcmk_controld_api_t *controld_api,
const char *host_uname, pe_resource_t *rsc,
const char *operation, const char *interval_spec,
bool just_failures, pe_working_set_t *data_set);
int cli_cleanup_all(pcmk_controld_api_t *controld_api, const char *node_name,
const char *operation, const char *interval_spec,
pe_working_set_t *data_set);
int cli_resource_restart(pe_resource_t *rsc, const char *host, int timeout_ms,
Expand Down
Loading

0 comments on commit 079ba8e

Please sign in to comment.