diff --git a/crm/pengine/stages.c b/crm/pengine/stages.c index 37c2f0c124..e4a68d4d14 100644 --- a/crm/pengine/stages.c +++ b/crm/pengine/stages.c @@ -1,465 +1,480 @@ -/* $Id: stages.c,v 1.49 2005/04/08 17:35:09 andrew Exp $ */ +/* $Id: stages.c,v 1.50 2005/04/08 19:10:14 andrew Exp $ */ /* * Copyright (C) 2004 Andrew Beekhof * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include node_t *choose_fencer(action_t *stonith, node_t *node, GListPtr resources); void order_actions(action_t *lh, action_t *rh, order_constraint_t *order); int order_id = 1; int max_valid_nodes = 0; GListPtr agent_defaults = NULL; gboolean have_quorum = FALSE; gboolean require_quorum = FALSE; gboolean stonith_enabled = FALSE; gboolean symmetric_cluster = TRUE; char *dc_uuid = NULL; const char* transition_timeout = "60000"; /* 1 minute */ /* * Unpack everything * At the end you'll have: * - A list of nodes * - A list of resources (each with any dependancies on other resources) * - A list of constraints between resources and nodes * - A list of constraints between start/stop actions * - A list of nodes that need to be stonith'd * - A list of nodes that need to be shutdown * - A list of the possible stop/start actions (without dependancies) */ gboolean stage0(crm_data_t * cib, GListPtr *resources, GListPtr *nodes, GListPtr *placement_constraints, GListPtr *actions, GListPtr *ordering_constraints, GListPtr *stonith_list, GListPtr *shutdown_list) { /* int lpc; */ crm_data_t * cib_nodes = get_object_root( XML_CIB_TAG_NODES, cib); crm_data_t * cib_status = get_object_root( XML_CIB_TAG_STATUS, cib); crm_data_t * cib_resources = get_object_root( XML_CIB_TAG_RESOURCES, cib); crm_data_t * cib_constraints = get_object_root( XML_CIB_TAG_CONSTRAINTS, cib); crm_data_t * config = get_object_root( XML_CIB_TAG_CRMCONFIG, cib); crm_data_t * agent_defaults = NULL; /*get_object_root(XML_CIB_TAG_RA_DEFAULTS, cib); */ crm_free(dc_uuid); dc_uuid = NULL; if(cib != NULL && crm_element_value(cib, XML_ATTR_DC_UUID) != NULL) { /* this should always be present */ dc_uuid = crm_element_value_copy(cib, XML_ATTR_DC_UUID); } /* reset remaining global variables */ num_synapse = 0; max_valid_nodes = 0; order_id = 1; action_id = 1; color_id = 0; have_quorum = FALSE; require_quorum = FALSE; stonith_enabled = FALSE; unpack_config(config); if(require_quorum) { const char *value = crm_element_value(cib, XML_ATTR_HAVE_QUORUM); if(value != NULL) { crm_str_to_boolean(value, &have_quorum); } if(have_quorum == FALSE) { crm_warn("We do not have quorum" " - fencing and resource management disabled"); } } unpack_global_defaults(agent_defaults); unpack_nodes(cib_nodes, nodes); unpack_resources(cib_resources, resources, actions, ordering_constraints, placement_constraints, *nodes); unpack_status(cib_status, *nodes, *resources, actions, placement_constraints); unpack_constraints(cib_constraints, *nodes, *resources, placement_constraints, ordering_constraints); return TRUE; } /* * Count how many valid nodes we have (so we know the maximum number of * colors we can resolve). * * Apply node constraints (ie. filter the "allowed_nodes" part of resources */ gboolean stage1(GListPtr placement_constraints, GListPtr nodes, GListPtr resources) { crm_devel("Processing stage 1"); slist_iter( node, node_t, nodes, lpc, if(node == NULL) { /* error */ } else if(node->weight >= 0.0 /* global weight */ && node->details->online && node->details->type == node_member) { max_valid_nodes++; } ); apply_placement_constraints(placement_constraints, nodes); /* will also filter -ve "final" weighted nodes from resources' * allowed lists while we are there */ apply_agent_constraints(resources); return TRUE; } /* * Choose a color for all resources from highest priority and XML_STRENGTH_VAL_MUST * dependancies to lowest, creating new colors as necessary (returned * as "colors"). * * Some nodes may be colored as a "no_color" meaning that it was unresolvable * given the current node stati and constraints. */ gboolean stage2(GListPtr sorted_rscs, GListPtr sorted_nodes, GListPtr *colors) { crm_devel("Processing stage 2"); if(no_color != NULL) { crm_free(no_color->details); crm_free(no_color); } crm_trace("create \"no color\""); no_color = create_color(NULL, NULL, NULL); /* Take (next) highest resource */ slist_iter( lh_resource, resource_t, sorted_rscs, lpc, /* if resource.provisional == FALSE, repeat */ if(lh_resource->provisional == FALSE) { /* already processed this resource */ continue; } color_resource(lh_resource, colors, sorted_rscs); /* next resource */ ); return TRUE; } /* * not sure if this is a good idea or not, but eventually we might like * to utilize as many nodes as possible... and this might be a convienient * hook */ gboolean stage3(GListPtr colors) { crm_devel("Processing stage 3"); /* not sure if this is a good idea or not */ if((ssize_t)g_list_length(colors) > max_valid_nodes) { /* we need to consolidate some */ } else if((ssize_t)g_list_length(colors) < max_valid_nodes) { /* we can create a few more */ } return TRUE; } /* * Choose a node for each (if possible) color */ gboolean stage4(GListPtr colors) { crm_devel("Processing stage 4"); slist_iter( color, color_t, colors, lpc, crm_devel("assigning node to color %d", color->id); if(color == NULL) { crm_err("NULL color detected"); continue; } else if(color->details->pending == FALSE) { continue; } choose_node_from_list(color); crm_devel("assigned %s to color %d", safe_val5(NULL, color, details, chosen_node, details, uname), color->id); slist_iter( rsc, resource_t, color->details->allocated_resources, lpc2, slist_iter( constraint, rsc_colocation_t, rsc->rsc_cons, lpc, rsc->fns->rsc_colocation_lh(constraint); ); ); ); crm_verbose("done"); return TRUE; } /* * Attach nodes to the actions that need to be taken * * Mark actions XML_LRM_ATTR_OPTIONAL if possible (Ie. if the start and stop are * for the same node) * * Mark unrunnable actions */ gboolean stage5(GListPtr resources, GListPtr *ordering_constraints) { slist_iter( rsc, resource_t, resources, lpc, rsc->fns->create_actions(rsc, ordering_constraints); rsc->fns->internal_constraints(rsc, ordering_constraints); ); return TRUE; } /* * Create dependacies for stonith and shutdown operations */ gboolean stage6(GListPtr *actions, GListPtr *ordering_constraints, GListPtr nodes, GListPtr resources) { action_t *down_op = NULL; action_t *stonith_op = NULL; crm_devel("Processing stage 6"); slist_iter( node, node_t, nodes, lpc, if(node->details->online && node->details->shutdown) { crm_info("Scheduling Node %s for shutdown", node->details->uname); down_op = action_new(NULL, shutdown_crm, NULL, node); down_op->runnable = TRUE; *actions = g_list_append(*actions, down_op); shutdown_constraints( node, down_op, ordering_constraints); } if(node->details->unclean && stonith_enabled) { crm_warn("Scheduling Node %s for STONITH", node->details->uname); stonith_op = action_new(NULL, stonith_node,NULL,NULL); stonith_op->runnable = TRUE; add_hash_param( stonith_op->extra, XML_LRM_ATTR_TARGET, node->details->uname); add_hash_param( stonith_op->extra, XML_LRM_ATTR_TARGET_UUID, node->details->id); if(down_op != NULL) { down_op->failure_is_fatal = FALSE; } *actions = g_list_append(*actions, stonith_op); stonith_constraints(node, stonith_op, down_op, ordering_constraints); } if(node->details->online == FALSE && node->details->expected_up) { crm_warn("Node %s was expected to be up!", node->details->uname); crm_warn("YOUR RESOURCES ARE NOW LIKELY COMPROMISED"); crm_warn("ENABLE STONITH TO KEEP YOUR RESOURCES SAFE"); } ); return TRUE; } /* * Determin the sets of independant actions and the correct order for the * actions in each set. * * Mark dependancies of un-runnable actions un-runnable * */ gboolean stage7(GListPtr resources, GListPtr actions, GListPtr ordering_constraints) { crm_devel("Processing stage 7"); slist_iter( order, order_constraint_t, ordering_constraints, lpc, /* try rsc_action-to-rsc_action */ resource_t *rsc = order->lh_rsc; if(rsc == NULL && order->lh_action) { rsc = order->lh_action->rsc; } if(rsc != NULL) { rsc->fns->rsc_order_lh(rsc, order); continue; } /* try action-to-rsc_action */ /* que off the rh resource */ rsc = order->rh_rsc; if(rsc == NULL && order->rh_action) { rsc = order->rh_action->rsc; } if(rsc != NULL) { rsc->fns->rsc_order_rh(order->lh_action, rsc, order); } else { /* fall back to action-to-action */ order_actions( order->lh_action, order->rh_action, order); } ); update_action_states(actions); return TRUE; } /* * Create a dependancy graph to send to the transitioner (via the CRMd) */ gboolean stage8(GListPtr resources, GListPtr actions, crm_data_t * *graph) { crm_devel("Processing stage 8"); *graph = create_xml_node(NULL, XML_TAG_GRAPH); set_xml_property_copy( *graph, "global_timeout", transition_timeout); /* errors... slist_iter(action, action_t, action_list, lpc, if(action->optional == FALSE && action->runnable == FALSE) { print_action("Ignoring", action, TRUE); } ); */ + crm_verbose("========= Action List ========="); + slist_iter(action, action_t, actions, lpc, + print_action(NULL, action, FALSE)); + slist_iter( rsc, resource_t, resources, lpc, crm_devel("processing actions for rsc=%s", rsc->id); rsc->fns->expand(rsc, graph); ); crm_xml_devel(*graph, "created resource-driven action list"); /* catch any non-resource specific actions */ crm_devel("processing non-resource actions"); slist_iter( action, action_t, actions, lpc, graph_element_from_action(action, graph); ); crm_xml_devel(*graph, "created generic action list"); return TRUE; } gboolean choose_node_from_list(color_t *color) { /* 1. Sort by weight 2. color.chosen_node = the node (of those with the highest wieght) with the fewest resources 3. remove color.chosen_node from all other colors */ GListPtr nodes = color->details->candidate_nodes; node_t *chosen = NULL; crm_devel("Choosing node for color %d", color->id); nodes = g_list_sort(nodes, sort_node_weight); chosen = g_list_nth_data(nodes, 0); color->details->chosen_node = NULL; color->details->pending = FALSE; if(chosen == NULL) { crm_debug("Could not allocate a node for color %d", color->id); return FALSE; + + } else if(chosen->details->unclean || chosen->details->shutdown) { + crm_debug("Even highest ranked node for color %d" + " is unclean or shutting down", + color->id); + return FALSE; + + } else if(chosen->weight < 0) { + crm_debug("Even highest ranked node for color %d, had weight %f", + color->id, chosen->weight); + return FALSE; } /* todo: update the old node for each resource to reflect its * new resource count */ chosen->details->num_resources += color->details->num_resources; color->details->chosen_node = node_copy(chosen); return TRUE; } diff --git a/crm/pengine/unpack.c b/crm/pengine/unpack.c index 22a8a6bd7b..6bb21d6c13 100644 --- a/crm/pengine/unpack.c +++ b/crm/pengine/unpack.c @@ -1,1143 +1,1143 @@ -/* $Id: unpack.c,v 1.70 2005/04/08 17:34:33 andrew Exp $ */ +/* $Id: unpack.c,v 1.71 2005/04/08 19:10:14 andrew Exp $ */ /* * Copyright (C) 2004 Andrew Beekhof * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include /* for ONLINESTATUS */ #include #include #include gboolean unpack_rsc_to_attr(crm_data_t * xml_obj, GListPtr rsc_list, GListPtr node_list, GListPtr *placement_constraints); gboolean unpack_rsc_to_node(crm_data_t * xml_obj, GListPtr rsc_list, GListPtr node_list, GListPtr *placement_constraints); gboolean unpack_rsc_order( crm_data_t * xml_obj, GListPtr rsc_list, GListPtr *ordering_constraints); gboolean unpack_rsc_colocation( crm_data_t * xml_obj, GListPtr rsc_list, GListPtr *ordering_constraints); gboolean unpack_rsc_location( crm_data_t * xml_obj, GListPtr rsc_list, GListPtr node_list, GListPtr *ordering_constraints); gboolean unpack_lrm_rsc_state( node_t *node, crm_data_t * lrm_state, GListPtr rsc_list, GListPtr nodes, GListPtr *actions, GListPtr *placement_constraints); gboolean add_node_attrs(crm_data_t * attrs, node_t *node); gboolean unpack_healthy_resource(GListPtr *placement_constraints, GListPtr *actions, crm_data_t * rsc_entry, resource_t *rsc_lh, node_t *node); gboolean unpack_failed_resource(GListPtr *placement_constraints, crm_data_t * rsc_entry, resource_t *rsc_lh, node_t *node); gboolean determine_online_status(crm_data_t * node_state, node_t *this_node); gboolean unpack_lrm_agents(node_t *node, crm_data_t * agent_list); gboolean rsc_colocation_new( const char *id, enum con_strength strength, resource_t *rsc_lh, resource_t *rsc_rh); gboolean create_ordering( const char *id, enum con_strength strength, resource_t *rsc_lh, resource_t *rsc_rh, GListPtr *ordering_constraints); rsc_to_node_t *rsc2node_new( const char *id, resource_t *rsc, double weight, gboolean can_run, node_t *node, GListPtr *placement_constraints); const char *get_agent_param(resource_t *rsc, const char *param); const char *get_agent_param_rsc(resource_t *rsc, const char *param); const void *get_agent_param_metadata(resource_t *rsc, const char *param); const char *get_agent_param_global(resource_t *rsc, const char *param); const char *param_value(crm_data_t * parent, const char *name); gboolean unpack_config(crm_data_t * config) { const char *value = NULL; value = param_value(config, "transition_timeout"); if(value != NULL) { int tmp = atoi(value); if(tmp > 0) { transition_timeout = value; } else { crm_warn("Invalid value for %s: %s", "transition_timeout", value); } } crm_devel("%s set to: %s", "transition_timeout", transition_timeout); value = param_value(config, "stonith_enabled"); if(value != NULL) { crm_str_to_boolean(value, &stonith_enabled); } crm_info("STONITH of failed nodes is %s", stonith_enabled?"enabled":"disabled"); value = param_value(config, "symmetric_cluster"); if(value != NULL) { crm_str_to_boolean(value, &symmetric_cluster); } if(symmetric_cluster) { crm_info("Cluster is symmetric" " - resources can run anywhere by default"); } value = param_value(config, "require_quorum"); if(value != NULL) { crm_str_to_boolean(value, &require_quorum); } if(require_quorum) { crm_info("Quorum required for fencing and resource management"); } return TRUE; } const char * param_value(crm_data_t * parent, const char *name) { crm_data_t * a_default = NULL; if(parent != NULL) { a_default = find_entity( parent, XML_CIB_TAG_NVPAIR, name, FALSE); } if(a_default == NULL) { crm_warn("Option %s not set", name); return NULL; } return crm_element_value(a_default, XML_NVPAIR_ATTR_VALUE); } const char * get_agent_param(resource_t *rsc, const char *param) { const char *value = NULL; if(param == NULL) { return NULL; } value = get_agent_param_rsc(rsc, param); if(value == NULL) { value = get_agent_param_metadata(rsc, param); } if(value == NULL) { value = get_agent_param_global(rsc, param); } return value; } const char * get_agent_param_rsc(resource_t *rsc, const char *param) { crm_data_t * xml_rsc = rsc->xml; return crm_element_value(xml_rsc, param); } const void * get_agent_param_metadata(resource_t *rsc, const char *param) { return NULL; } const char * get_agent_param_global(resource_t *rsc, const char *param) { const char * value = NULL;/*g_hashtable_lookup(agent_global_defaults, param); */ if(value == NULL) { crm_err("No global value default for %s", param); } return value; } gboolean unpack_global_defaults(crm_data_t * defaults) { return TRUE; } gboolean unpack_nodes(crm_data_t * xml_nodes, GListPtr *nodes) { node_t *new_node = NULL; crm_data_t * attrs = NULL; const char *id = NULL; const char *uname = NULL; const char *type = NULL; crm_verbose("Begining unpack..."); xml_child_iter( xml_nodes, xml_obj, XML_CIB_TAG_NODE, id = crm_element_value(xml_obj, XML_ATTR_ID); uname = crm_element_value(xml_obj, XML_ATTR_UNAME); type = crm_element_value(xml_obj, XML_ATTR_TYPE); crm_verbose("Processing node %s/%s", uname, id); attrs = find_xml_node(xml_obj, "attributes", FALSE); if(id == NULL) { crm_err("Must specify id tag in "); continue; } if(type == NULL) { crm_err("Must specify type tag in "); continue; } crm_malloc(new_node, sizeof(node_t)); if(new_node == NULL) { return FALSE; } - new_node->weight = 1.0; + new_node->weight = 0; new_node->fixed = FALSE; crm_malloc(new_node->details, sizeof(struct node_shared_s)); if(new_node->details == NULL) { crm_free(new_node); return FALSE; } crm_verbose("Creaing node for entry %s/%s", uname, id); new_node->details->id = id; new_node->details->uname = uname; new_node->details->type = node_ping; new_node->details->online = FALSE; new_node->details->unclean = TRUE; /* all nodes are unclean until we've seen their status entry */ new_node->details->shutdown = FALSE; new_node->details->running_rsc = NULL; new_node->details->agents = NULL; new_node->details->attrs = g_hash_table_new( g_str_hash, g_str_equal); if(safe_str_eq(type, "member")) { new_node->details->type = node_member; } add_node_attrs(xml_obj, new_node); if(crm_is_true(g_hash_table_lookup( new_node->details->attrs, "standby"))) { crm_info("Node %s is in standby-mode", new_node->details->uname); new_node->weight = -1.0; } *nodes = g_list_append(*nodes, new_node); crm_verbose("Done with node %s", crm_element_value(xml_obj, XML_ATTR_UNAME)); crm_devel_action(print_node("Added", new_node, FALSE)); ); *nodes = g_list_sort(*nodes, sort_node_weight); return TRUE; } gboolean unpack_resources(crm_data_t * xml_resources, GListPtr *resources, GListPtr *actions, GListPtr *ordering_constraints, GListPtr *placement_constraints, GListPtr all_nodes) { crm_verbose("Begining unpack..."); xml_child_iter( xml_resources, xml_obj, NULL, resource_t *new_rsc = NULL; if(common_unpack(xml_obj, &new_rsc)) { *resources = g_list_append(*resources, new_rsc); crm_devel_action( print_resource("Added", new_rsc, FALSE)); if(symmetric_cluster) { rsc_to_node_t *new_con = rsc2node_new( "symmetric_default", new_rsc, 0, TRUE, NULL, placement_constraints); new_con->node_list_rh = node_list_dup(all_nodes, FALSE); } } else { crm_err("Failed unpacking resource %s", crm_element_value(xml_obj, XML_ATTR_ID)); } ); *resources = g_list_sort(*resources, sort_rsc_priority); return TRUE; } gboolean unpack_constraints(crm_data_t * xml_constraints, GListPtr nodes, GListPtr resources, GListPtr *placement_constraints, GListPtr *ordering_constraints) { crm_verbose("Begining unpack..."); xml_child_iter( xml_constraints, xml_obj, NULL, const char *id = crm_element_value(xml_obj, XML_ATTR_ID); if(id == NULL) { crm_err("Constraint <%s...> must have an id", crm_element_name(xml_obj)); continue; } crm_verbose("Processing constraint %s %s", crm_element_name(xml_obj),id); if(safe_str_eq(XML_CONS_TAG_RSC_ORDER, crm_element_name(xml_obj))) { unpack_rsc_order( xml_obj, resources, ordering_constraints); } else if(safe_str_eq(XML_CONS_TAG_RSC_DEPEND, crm_element_name(xml_obj))) { unpack_rsc_colocation( xml_obj, resources, ordering_constraints); } else if(safe_str_eq(XML_CONS_TAG_RSC_LOCATION, crm_element_name(xml_obj))) { unpack_rsc_location( xml_obj, resources, nodes, placement_constraints); } else { crm_err("Unsupported constraint type: %s", crm_element_name(xml_obj)); } ); return TRUE; } rsc_to_node_t * rsc2node_new(const char *id, resource_t *rsc, double weight, gboolean can, node_t *node, GListPtr *placement_constraints) { rsc_to_node_t *new_con = NULL; if(rsc == NULL || id == NULL) { crm_err("Invalid constraint %s for rsc=%p", crm_str(id), rsc); return NULL; } crm_malloc(new_con, sizeof(rsc_to_node_t)); if(new_con != NULL) { new_con->id = id; new_con->rsc_lh = rsc; new_con->node_list_rh = NULL; new_con->weight = weight; if(node != NULL) { new_con->node_list_rh = g_list_append(NULL, node); } *placement_constraints = g_list_append(*placement_constraints, new_con); } return new_con; } /* remove nodes that are down, stopping */ /* create +ve rsc_to_node constraints between resources and the nodes they are running on */ /* anything else? */ gboolean unpack_status(crm_data_t * status, GListPtr nodes, GListPtr rsc_list, GListPtr *actions, GListPtr *placement_constraints) { const char *uname = NULL; crm_data_t * lrm_rsc = NULL; crm_data_t * lrm_agents = NULL; crm_data_t * attrs = NULL; node_t *this_node = NULL; crm_verbose("Begining unpack"); xml_child_iter( status, node_state, XML_CIB_TAG_STATE, /* id = crm_element_value(node_state, XML_ATTR_ID); */ uname = crm_element_value(node_state, XML_ATTR_UNAME); attrs = find_xml_node(node_state, XML_LRM_TAG_ATTRIBUTES,FALSE); lrm_rsc = find_xml_node(node_state, XML_CIB_TAG_LRM, FALSE); lrm_agents = find_xml_node(lrm_rsc, XML_LRM_TAG_AGENTS, FALSE); lrm_rsc = find_xml_node(lrm_rsc, XML_LRM_TAG_RESOURCES, FALSE); crm_verbose("Processing node %s", uname); this_node = pe_find_node(nodes, uname); if(uname == NULL) { /* error */ continue; } else if(this_node == NULL) { crm_warn("Node %s in status section no longer exists", uname); continue; } /* Mark the node as provisionally clean * - at least we have seen it in the current cluster's lifetime */ this_node->details->unclean = FALSE; crm_verbose("Adding runtime node attrs"); add_node_attrs(node_state, this_node); crm_verbose("determining node state"); determine_online_status(node_state, this_node); if(this_node->details->online || stonith_enabled) { /* offline nodes run no resources * unless stonith is enabled in which case we need to * know */ crm_verbose("Processing lrm resource entries"); unpack_lrm_rsc_state( this_node, lrm_rsc, rsc_list, nodes, actions, placement_constraints); } ); return TRUE; } gboolean determine_online_status(crm_data_t * node_state, node_t *this_node) { gboolean online = FALSE; const char *uname = crm_element_value(node_state,XML_ATTR_UNAME); /* const char *state = crm_element_value(node_state,XML_NODE_ATTR_STATE); */ const char *exp_state = crm_element_value(node_state,XML_CIB_ATTR_EXPSTATE); const char *join_state = crm_element_value(node_state,XML_CIB_ATTR_JOINSTATE); const char *crm_state = crm_element_value(node_state,XML_CIB_ATTR_CRMDSTATE); const char *ccm_state = crm_element_value(node_state,XML_CIB_ATTR_INCCM); const char *ha_state = crm_element_value(node_state,XML_CIB_ATTR_HASTATE); const char *shutdown = crm_element_value(node_state,XML_CIB_ATTR_SHUTDOWN); if(this_node == NULL) { return online; } if(shutdown != NULL) { - /* dont run resources here */ - this_node->weight = -1; - this_node->fixed = TRUE; this_node->details->shutdown = TRUE; } if(safe_str_eq(join_state, CRMD_JOINSTATE_MEMBER)) { this_node->details->expected_up = TRUE; } if(crm_is_true(ccm_state) == FALSE || ha_state == NULL || safe_str_eq(ha_state, DEADSTATUS)) { crm_debug("Node is down: ha_state=%s, ccm_state=%s", crm_str(ha_state), crm_str(ccm_state)); } else if(safe_str_eq(join_state, CRMD_JOINSTATE_MEMBER) && safe_str_eq(crm_state, ONLINESTATUS)) { online = TRUE; } else if(this_node->details->expected_up == FALSE) { crm_debug("CRMd is down: ha_state=%s, ccm_state=%s", crm_str(ha_state), crm_str(ccm_state)); crm_debug("\tcrm_state=%s, join_state=%s, expected=%s", crm_str(crm_state), crm_str(join_state), crm_str(exp_state)); } else { /* mark it unclean */ this_node->details->unclean = TRUE; crm_warn("Node %s is un-expectedly down", uname); crm_debug("\tcrm_state=%s, join_state=%s, expected=%s", crm_str(crm_state), crm_str(join_state), crm_str(exp_state)); } if(online) { crm_debug("Node %s is online", uname); this_node->details->online = TRUE; } else { /* remove node from contention */ crm_debug("Node %s is down", uname); this_node->weight = -1; this_node->fixed = TRUE; } if(this_node->details->unclean) { crm_warn("Node %s is unclean", uname); } if(this_node->details->shutdown) { + /* dont run resources here */ + this_node->weight = -1; + this_node->fixed = TRUE; crm_debug("Node %s is due for shutdown", uname); } return online; } gboolean unpack_lrm_agents(node_t *node, crm_data_t * agent_list) { /* if the agent is not listed, remove the node from * the resource's list of allowed_nodes */ lrm_agent_t *agent = NULL; const char *version = NULL; if(agent_list == NULL) { return FALSE; } xml_child_iter( agent_list, xml_agent, XML_LRM_TAG_AGENT, crm_malloc(agent, sizeof(lrm_agent_t)); if(agent == NULL) { continue; } agent->class = crm_element_value(xml_agent, XML_AGENT_ATTR_CLASS); agent->type = crm_element_value(xml_agent, XML_ATTR_TYPE); version = crm_element_value(xml_agent, XML_ATTR_VERSION); agent->version = version?version:"0.0"; crm_trace("Adding agent %s/%s v%s to node %s", agent->class, agent->type, agent->version, node->details->uname); node->details->agents = g_list_append( node->details->agents, agent); ); return TRUE; } gboolean unpack_lrm_rsc_state(node_t *node, crm_data_t * lrm_rsc, GListPtr rsc_list, GListPtr nodes, GListPtr *actions, GListPtr *placement_constraints) { const char *rsc_id = NULL; const char *node_id = NULL; const char *rsc_state = NULL; const char *op_status = NULL; const char *last_rc = NULL; const char *last_op = NULL; resource_t *rsc_lh = NULL; op_status_t action_status_i = LRM_OP_ERROR; xml_child_iter( lrm_rsc, rsc_entry, XML_LRM_TAG_RESOURCE, rsc_id = crm_element_value(rsc_entry, XML_ATTR_ID); node_id = crm_element_value(rsc_entry, XML_LRM_ATTR_TARGET); rsc_state = crm_element_value(rsc_entry, XML_LRM_ATTR_RSCSTATE); op_status = crm_element_value(rsc_entry, XML_LRM_ATTR_OPSTATUS); last_rc = crm_element_value(rsc_entry, XML_LRM_ATTR_RC); last_op = crm_element_value(rsc_entry, XML_LRM_ATTR_LASTOP); rsc_lh = pe_find_resource(rsc_list, rsc_id); crm_verbose("[%s] Processing %s on %s (%s)", crm_element_name(rsc_entry), rsc_id, node_id, rsc_state); if(rsc_lh == NULL) { crm_err("Could not find a match for resource" " %s in %s's status section", rsc_id, node_id); continue; } else if(op_status == NULL) { crm_err("Invalid resource status entry for %s in %s", rsc_id, node_id); continue; } action_status_i = atoi(op_status); if(node->details->unclean) { crm_debug("Node %s (where %s is running) is unclean." " Further action depends on the value of %s", node->details->uname, rsc_lh->id, XML_RSC_ATTR_STOPFAIL); /* map the status to an error and then handle as a * failed resource. */ action_status_i = LRM_OP_ERROR; } else if(action_status_i == (op_status_t)-1) { /* * TODO: this may need some more thought * Some cases: * - PE reinvoked with pending action that will succeed * - PE reinvoked with pending action that will fail * - After DC election * - After startup * * pending start - required start * pending stop - required stop * pending on unavailable node - stonith * * For now this should do */ if(safe_str_eq(last_op, "stop")) { /* map this to a timeout so it is re-issued */ action_status_i = LRM_OP_TIMEOUT; } else { /* map this to a "done" so it is not marked * as failed, then make sure it is re-issued */ action_new(rsc_lh, start_rsc, NULL, node); action_status_i = LRM_OP_DONE; } } switch(action_status_i) { case LRM_OP_DONE: unpack_healthy_resource( placement_constraints, actions, rsc_entry, rsc_lh,node); break; case LRM_OP_ERROR: case LRM_OP_TIMEOUT: case LRM_OP_NOTSUPPORTED: unpack_failed_resource(placement_constraints, rsc_entry, rsc_lh,node); break; case LRM_OP_CANCELLED: /* do nothing?? */ crm_warn("Dont know what to do for cancelled ops yet"); break; } ); return TRUE; } gboolean unpack_failed_resource(GListPtr *placement_constraints, crm_data_t * rsc_entry, resource_t *rsc_lh, node_t *node) { const char *last_op = crm_element_value(rsc_entry, XML_LRM_ATTR_LASTOP); crm_devel("Unpacking failed action %s on %s", last_op, rsc_lh->id); /* make sure we dont allocate the resource here again*/ rsc2node_new("dont_run__generated", rsc_lh, -INFINITY, FALSE, node, placement_constraints); if(safe_str_eq(last_op, "start")) { /* the resource is not actually running... nothing more to do*/ return TRUE; } switch(rsc_lh->stopfail_type) { case pesf_stonith: /* treat it as if it is still running * but also mark the node as unclean */ native_add_running(rsc_lh, node); node->details->running_rsc = g_list_append( node->details->running_rsc, rsc_lh); if(node->details->online) { node->details->shutdown = TRUE; } node->details->unclean = TRUE; break; case pesf_block: /* let this depend on the stop action which will fail * but make sure the transition continues... */ native_add_running(rsc_lh, node); (node->details->num_resources)++; node->details->running_rsc = g_list_append( node->details->running_rsc, rsc_lh); /* rsc_lh->stop->timeout = NULL; /\* wait forever *\/ */ break; case pesf_ignore: /* pretend nothing happened */ break; } return TRUE; } gboolean unpack_healthy_resource(GListPtr *placement_constraints, GListPtr *actions, crm_data_t * rsc_entry, resource_t *rsc_lh, node_t *node) { const char *last_op = crm_element_value(rsc_entry, XML_LRM_ATTR_LASTOP); crm_devel("Unpacking healthy action %s on %s", last_op, rsc_lh->id); if(safe_str_neq(last_op, "stop")) { /* create the link between this node and the rsc */ crm_verbose("Setting cur_node = %s for rsc = %s", node->details->uname, rsc_lh->id); native_add_running(rsc_lh, node); (node->details->num_resources)++; node->details->running_rsc = g_list_append( node->details->running_rsc, rsc_lh); } return TRUE; } gboolean rsc_colocation_new(const char *id, enum con_strength strength, resource_t *rsc_lh, resource_t *rsc_rh) { rsc_colocation_t *new_con = NULL; rsc_colocation_t *inverted_con = NULL; if(rsc_lh == NULL || rsc_rh == NULL){ /* error */ return FALSE; } crm_malloc(new_con, sizeof(rsc_colocation_t)); if(new_con == NULL) { return FALSE; } new_con->id = id; new_con->rsc_lh = rsc_lh; new_con->rsc_rh = rsc_rh; new_con->strength = strength; inverted_con = invert_constraint(new_con); crm_devel("Adding constraint %s (%p) to %s", new_con->id, new_con, rsc_lh->id); rsc_lh->rsc_cons = g_list_insert_sorted( rsc_lh->rsc_cons, new_con, sort_cons_strength); crm_devel("Adding constraint %s (%p) to %s", inverted_con->id, inverted_con, rsc_rh->id); rsc_rh->rsc_cons = g_list_insert_sorted( rsc_rh->rsc_cons, inverted_con, sort_cons_strength); return TRUE; } gboolean order_new(resource_t *lh_rsc, enum action_tasks lh_action_task, action_t *lh_action, resource_t *rh_rsc, enum action_tasks rh_action_task, action_t *rh_action, enum con_strength strength, GListPtr *ordering_constraints) { order_constraint_t *order = NULL; if((lh_action == NULL && lh_rsc == NULL) || (rh_action == NULL && rh_rsc == NULL) || ordering_constraints == NULL){ crm_err("Invalid inputs lh_rsc=%p, lh_a=%p," " rh_rsc=%p, rh_a=%p, l=%p", lh_rsc, lh_action, rh_rsc, rh_action, ordering_constraints); return FALSE; } crm_malloc(order, sizeof(order_constraint_t)); if(order == NULL) { return FALSE; } order->id = order_id++; order->strength = strength; order->lh_rsc = lh_rsc; order->rh_rsc = rh_rsc; order->lh_action = lh_action; order->rh_action = rh_action; order->lh_action_task = lh_action_task; order->rh_action_task = rh_action_task; *ordering_constraints = g_list_append( *ordering_constraints, order); if(lh_rsc != NULL && rh_rsc != NULL) { crm_devel("Created ordering constraint %d (%s):" " %s/%s before %s/%s", order->id, strength2text(order->strength), lh_rsc->id, task2text(lh_action_task), rh_rsc->id, task2text(rh_action_task)); } else if(lh_rsc != NULL) { crm_devel("Created ordering constraint %d (%s):" " %s/%s before action %d (%s)", order->id, strength2text(order->strength), lh_rsc->id, task2text(lh_action_task), rh_action->id, task2text(rh_action_task)); } else if(rh_rsc != NULL) { crm_devel("Created ordering constraint %d (%s):" " action %d (%s) before %s/%s", order->id, strength2text(order->strength), lh_action->id, task2text(lh_action_task), rh_rsc->id, task2text(rh_action_task)); } else { crm_devel("Created ordering constraint %d (%s):" " action %d (%s) before action %d (%s)", order->id, strength2text(order->strength), lh_action->id, task2text(lh_action_task), rh_action->id, task2text(rh_action_task)); } return TRUE; } gboolean unpack_rsc_colocation(crm_data_t * xml_obj, GListPtr rsc_list, GListPtr *ordering_constraints) { enum con_strength strength_e = pecs_ignore; const char *id_lh = crm_element_value(xml_obj, XML_CONS_ATTR_FROM); const char *id = crm_element_value(xml_obj, XML_ATTR_ID); const char *id_rh = crm_element_value(xml_obj, XML_CONS_ATTR_TO); const char *type = crm_element_value(xml_obj, XML_ATTR_TYPE); resource_t *rsc_lh = pe_find_resource(rsc_list, id_lh); resource_t *rsc_rh = pe_find_resource(rsc_list, id_rh); if(rsc_lh == NULL) { crm_err("No resource (con=%s, rsc=%s)", id, id_lh); return FALSE; } else if(rsc_rh == NULL) { crm_err("No resource (con=%s, rsc=%s)", id, id_rh); return FALSE; } if(safe_str_eq(type, XML_STRENGTH_VAL_MUST)) { strength_e = pecs_must; } else if(safe_str_eq(type, XML_STRENGTH_VAL_SHOULD)) { crm_err("Type %s is no longer supported", type); strength_e = pecs_must; } else if(safe_str_eq(type, XML_STRENGTH_VAL_SHOULDNOT)) { crm_err("Type %s is no longer supported", type); strength_e = pecs_must_not; } else if(safe_str_eq(type, XML_STRENGTH_VAL_MUSTNOT)) { strength_e = pecs_must_not; } else { crm_err("Unknown value for %s: %s", XML_ATTR_TYPE, type); return FALSE; } return rsc_colocation_new(id, strength_e, rsc_lh, rsc_rh); } gboolean unpack_rsc_order( crm_data_t * xml_obj, GListPtr rsc_list, GListPtr *ordering_constraints) { gboolean symmetrical_bool = TRUE; gboolean action_is_start = TRUE; gboolean type_is_after = TRUE; const char *id = crm_element_value(xml_obj, XML_ATTR_ID); const char *id_lh = crm_element_value(xml_obj, XML_CONS_ATTR_FROM); const char *id_rh = crm_element_value(xml_obj, XML_CONS_ATTR_TO); const char *action = crm_element_value(xml_obj, XML_CONS_ATTR_ACTION); const char *symmetrical = crm_element_value(xml_obj, XML_CONS_ATTR_SYMMETRICAL); const char *type = crm_element_value(xml_obj, XML_ATTR_TYPE); resource_t *rsc_lh = pe_find_resource(rsc_list, id_lh); resource_t *rsc_rh = pe_find_resource(rsc_list, id_rh); if(xml_obj == NULL) { crm_err("No constraint object to process."); return FALSE; } else if(id == NULL) { crm_err("%s constraint must have an id", crm_element_name(xml_obj)); return FALSE; } else if(rsc_lh == NULL || rsc_rh == NULL) { crm_err("Constraint %s needs two sides lh: %p rh: %p" " (NULL indicates missing side)", id, rsc_lh, rsc_rh); return FALSE; } crm_str_to_boolean(symmetrical, &symmetrical_bool); if(safe_str_eq(type, "before")) { type_is_after = FALSE; } if(safe_str_eq(action, CRMD_RSCSTATE_STOP)) { action_is_start = FALSE; } #if 1 if((type_is_after && action_is_start) || (type_is_after == FALSE && action_is_start == FALSE)){ if(symmetrical_bool || action_is_start == FALSE) { order_new(rsc_lh, stop_rsc, NULL, rsc_rh, stop_rsc, NULL, pecs_startstop, ordering_constraints); } if(symmetrical_bool || action_is_start) { order_new(rsc_rh, start_rsc, NULL, rsc_lh, start_rsc, NULL, pecs_startstop, ordering_constraints); } } else { if(symmetrical_bool || action_is_start == FALSE) { order_new(rsc_rh, stop_rsc, NULL, rsc_lh, stop_rsc, NULL, pecs_startstop, ordering_constraints); } if(symmetrical_bool || action_is_start) { order_new(rsc_lh, start_rsc, NULL, rsc_rh, start_rsc, NULL, pecs_startstop, ordering_constraints); } } #else if(type_is_after) { order_new(rsc_lh, stop_rsc, NULL, rsc_rh, stop_rsc, NULL, pecs_startstop, ordering_constraints); order_new(rsc_rh, start_rsc, NULL, rsc_lh, start_rsc, NULL, pecs_startstop, ordering_constraints); } else { order_new(rsc_rh, stop_rsc, NULL, rsc_lh, stop_rsc, NULL, pecs_startstop, ordering_constraints); order_new(rsc_lh, start_rsc, NULL, rsc_rh, start_rsc, NULL, pecs_startstop, ordering_constraints); } #endif return TRUE; } gboolean add_node_attrs(crm_data_t *xml_obj, node_t *node) { g_hash_table_insert(node->details->attrs, crm_strdup(XML_ATTR_UNAME), crm_strdup(node->details->uname)); g_hash_table_insert(node->details->attrs, crm_strdup(XML_ATTR_ID), crm_strdup(node->details->id)); if(safe_str_eq(node->details->id, dc_uuid)) { node->details->is_dc = TRUE; g_hash_table_insert(node->details->attrs, crm_strdup(XML_ATTR_DC), crm_strdup(XML_BOOLEAN_TRUE)); } else { g_hash_table_insert(node->details->attrs, crm_strdup(XML_ATTR_DC), crm_strdup(XML_BOOLEAN_FALSE)); } unpack_instance_attributes(xml_obj, node->details->attrs); return TRUE; } gboolean unpack_rsc_location( crm_data_t * xml_obj, GListPtr rsc_list, GListPtr node_list, GListPtr *placement_constraints) { gboolean were_rules = FALSE; const char *id_lh = crm_element_value(xml_obj, "rsc"); const char *id = crm_element_value(xml_obj, XML_ATTR_ID); resource_t *rsc_lh = pe_find_resource(rsc_list, id_lh); if(rsc_lh == NULL) { crm_warn("No resource (con=%s, rsc=%s)", id, id_lh); return FALSE; } xml_child_iter( xml_obj, rule, XML_TAG_RULE, gboolean first_expr = TRUE; gboolean can_run = FALSE; gboolean do_and = TRUE; gboolean rule_has_expressions; const char *rule_id = crm_element_value(rule, XML_ATTR_ID); const char *score = crm_element_value(rule, XML_RULE_ATTR_SCORE); const char *boolean = crm_element_value(rule, XML_RULE_ATTR_BOOLEAN_OP); GListPtr match_L = NULL; GListPtr old_list = NULL; float score_f = 0.0; rsc_to_node_t *new_con = NULL; were_rules = TRUE; if(score == NULL) { score_f = 0.0; } else if(safe_str_eq(score, MINUS_INFINITY_S)) { score_f = -INFINITY; } else if(safe_str_eq(score, INFINITY_S)) { score_f = INFINITY; } else { score_f = atof(score); } if(safe_str_eq(boolean, "or")) { do_and = FALSE; } if(score_f >= 0.0) { can_run = TRUE; } new_con = rsc2node_new(rule_id, rsc_lh, score_f, can_run, NULL, placement_constraints); if(new_con == NULL) { continue; } crm_trace("processing rule: %s", crm_element_value(rule, XML_ATTR_ID)); rule_has_expressions = FALSE; xml_child_iter( rule, expr, XML_TAG_EXPRESSION, const char *attr = crm_element_value( expr, XML_EXPR_ATTR_ATTRIBUTE); const char *op = crm_element_value( expr, XML_EXPR_ATTR_OPERATION); const char *value = crm_element_value( expr, XML_EXPR_ATTR_VALUE); const char *type = crm_element_value( expr, XML_EXPR_ATTR_TYPE); rule_has_expressions = TRUE; crm_trace("processing expression: %s", crm_element_value(expr, XML_ATTR_ID)); match_L = apply_node_expression( attr, op, value, type, node_list); if(first_expr) { new_con->node_list_rh = node_list_dup( match_L, FALSE); first_expr = FALSE; continue; } old_list = new_con->node_list_rh; if(do_and) { crm_trace("do_and"); new_con->node_list_rh = node_list_and( old_list, match_L, FALSE); } else { crm_trace("do_or"); new_con->node_list_rh = node_list_or( old_list, match_L, FALSE); } pe_free_shallow_adv(match_L, FALSE); pe_free_shallow_adv(old_list, TRUE); ); if(rule_has_expressions == FALSE && symmetric_cluster == FALSE) { /* feels like a hack */ crm_devel("Rule %s had no expressions," " adding all nodes", crm_element_value(rule, XML_ATTR_ID)); new_con->node_list_rh = node_list_dup(node_list,FALSE); } if(new_con->node_list_rh == NULL) { crm_debug("No matching nodes for constraint/rule %s/%s", id, crm_element_value(rule, XML_ATTR_ID)); } crm_devel_action(print_rsc_to_node("Added", new_con, FALSE)); ); if(were_rules == FALSE) { crm_debug("no rules for constraint %s", id); } return TRUE; } diff --git a/crm/pengine/utils.c b/crm/pengine/utils.c index b040f70089..0473a4f061 100644 --- a/crm/pengine/utils.c +++ b/crm/pengine/utils.c @@ -1,1203 +1,1216 @@ -/* $Id: utils.c,v 1.63 2005/04/07 13:56:47 andrew Exp $ */ +/* $Id: utils.c,v 1.64 2005/04/08 19:10:14 andrew Exp $ */ /* * Copyright (C) 2004 Andrew Beekhof * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include int action_id = 1; int color_id = 0; void print_str_str(gpointer key, gpointer value, gpointer user_data); gboolean ghash_free_str_str(gpointer key, gpointer value, gpointer user_data); gboolean node_merge_weights(node_t *node, node_t *with); const char *find_action_timeout(resource_t *rsc, enum action_tasks task); /* only for rsc_colocation constraints */ rsc_colocation_t * invert_constraint(rsc_colocation_t *constraint) { rsc_colocation_t *inverted_con = NULL; crm_verbose("Inverting constraint"); if(constraint == NULL) { crm_err("Cannot invert NULL constraint"); return NULL; } crm_malloc(inverted_con, sizeof(rsc_colocation_t)); if(inverted_con == NULL) { return NULL; } inverted_con->id = crm_strdup(constraint->id); inverted_con->strength = constraint->strength; /* swap the direction */ inverted_con->rsc_lh = constraint->rsc_rh; inverted_con->rsc_rh = constraint->rsc_lh; crm_devel_action( print_rsc_colocation("Inverted constraint", inverted_con, FALSE)); return inverted_con; } /* are the contents of list1 and list2 equal * nodes with weight < 0 are ignored if filter == TRUE * * slow but linear * */ gboolean node_list_eq(GListPtr list1, GListPtr list2, gboolean filter) { node_t *other_node; GListPtr lhs = list1; GListPtr rhs = list2; slist_iter( node, node_t, lhs, lpc, if(node == NULL || (filter && node->weight < 0)) { continue; } other_node = (node_t*) pe_find_node(rhs, node->details->uname); if(other_node == NULL || other_node->weight < 0) { return FALSE; } ); lhs = list2; rhs = list1; slist_iter( node, node_t, lhs, lpc, if(node == NULL || (filter && node->weight < 0)) { continue; } other_node = (node_t*) pe_find_node(rhs, node->details->uname); if(other_node == NULL || other_node->weight < 0) { return FALSE; } ); return TRUE; } /* the intersection of list1 and list2 */ GListPtr node_list_and(GListPtr list1, GListPtr list2, gboolean filter) { GListPtr result = NULL; unsigned lpc = 0; for(lpc = 0; lpc < g_list_length(list1); lpc++) { node_t *node = (node_t*)g_list_nth_data(list1, lpc); node_t *new_node = node_copy(node); node_t *other_node = pe_find_node(list2, node->details->uname); if(node_merge_weights(new_node, other_node) == FALSE) { crm_free(new_node); } else if(filter && new_node->weight < 0) { crm_free(new_node); } else { result = g_list_append(result, new_node); } } return result; } gboolean node_merge_weights(node_t *node, node_t *with) { if(node == NULL || with == NULL) { return FALSE; } else if(node->weight < 0 || with->weight < 0) { node->weight = -1; } else if(node->weight < with->weight) { node->weight = with->weight; } return TRUE; } /* list1 - list2 */ GListPtr node_list_minus(GListPtr list1, GListPtr list2, gboolean filter) { GListPtr result = NULL; slist_iter( node, node_t, list1, lpc, node_t *other_node = pe_find_node(list2, node->details->uname); node_t *new_node = NULL; if(node == NULL || other_node != NULL || (filter && node->weight < 0)) { continue; } new_node = node_copy(node); result = g_list_append(result, new_node); ); crm_verbose("Minus result len: %d", g_list_length(result)); return result; } /* list1 + list2 - (intersection of list1 and list2) */ GListPtr node_list_xor(GListPtr list1, GListPtr list2, gboolean filter) { GListPtr result = NULL; slist_iter( node, node_t, list1, lpc, node_t *new_node = NULL; node_t *other_node = (node_t*) pe_find_node(list2, node->details->uname); if(node == NULL || other_node != NULL || (filter && node->weight < 0)) { continue; } new_node = node_copy(node); result = g_list_append(result, new_node); ); slist_iter( node, node_t, list2, lpc, node_t *new_node = NULL; node_t *other_node = (node_t*) pe_find_node(list1, node->details->uname); if(node == NULL || other_node != NULL || (filter && node->weight < 0)) { continue; } new_node = node_copy(node); result = g_list_append(result, new_node); ); crm_verbose("Xor result len: %d", g_list_length(result)); return result; } GListPtr node_list_or(GListPtr list1, GListPtr list2, gboolean filter) { node_t *other_node = NULL; GListPtr result = NULL; result = node_list_dup(list1, filter); slist_iter( node, node_t, list2, lpc, if(node == NULL) { continue; } other_node = (node_t*)pe_find_node( result, node->details->uname); if(other_node != NULL) { node_merge_weights(other_node, node); if(filter && node->weight < 0) { /* TODO: remove and free other_node */ } } else if(filter && node->weight < 0) { } else { node_t *new_node = node_copy(node); result = g_list_append(result, new_node); } ); return result; } GListPtr node_list_dup(GListPtr list1, gboolean filter) { GListPtr result = NULL; slist_iter( this_node, node_t, list1, lpc, node_t *new_node = NULL; if(filter && this_node->weight < 0) { continue; } new_node = node_copy(this_node); if(new_node != NULL) { result = g_list_append(result, new_node); } ); return result; } node_t * node_copy(node_t *this_node) { node_t *new_node = NULL; CRM_DEV_ASSERT(this_node != NULL); if(this_node == NULL) { crm_err("Failed copy of node."); return NULL; } crm_malloc(new_node, sizeof(node_t)); CRM_DEV_ASSERT(new_node != NULL); if(new_node == NULL) { return NULL; } crm_trace("Copying %p (%s) to %p", this_node, this_node->details->uname, new_node); new_node->weight = this_node->weight; new_node->fixed = this_node->fixed; - new_node->details = this_node->details; + new_node->details = this_node->details; return new_node; } /* * Create a new color with the contents of "nodes" as the list of * possible nodes that resources with this color can be run on. * * Typically, when creating a color you will provide the node list from * the resource you will first assign the color to. * * If "colors" != NULL, it will be added to that list * If "resources" != NULL, it will be added to every provisional resource * in that list */ color_t * create_color(GListPtr *colors, resource_t *resource, GListPtr node_list) { color_t *new_color = NULL; crm_trace("Creating color"); crm_malloc(new_color, sizeof(color_t)); if(new_color == NULL) { return NULL; } new_color->id = color_id++; new_color->local_weight = 1.0; crm_trace("Creating color details"); crm_malloc(new_color->details, sizeof(struct color_shared_s)); if(new_color->details == NULL) { crm_free(new_color); return NULL; } new_color->details->id = new_color->id; new_color->details->highest_priority = -1; new_color->details->chosen_node = NULL; new_color->details->candidate_nodes = NULL; new_color->details->allocated_resources = NULL; new_color->details->pending = TRUE; if(resource != NULL) { crm_trace("populating node list"); new_color->details->highest_priority = resource->priority; new_color->details->candidate_nodes = node_list_dup(node_list, TRUE); } crm_devel_action(print_color("Created color", new_color, TRUE)); if(colors != NULL) { *colors = g_list_append(*colors, new_color); } return new_color; } color_t * copy_color(color_t *a_color) { color_t *color_copy = NULL; if(a_color == NULL) { crm_err("Cannot copy NULL"); return NULL; } crm_malloc(color_copy, sizeof(color_t)); if(color_copy != NULL) { color_copy->id = a_color->id; color_copy->details = a_color->details; color_copy->local_weight = 1.0; } return color_copy; } resource_t * pe_find_resource(GListPtr rsc_list, const char *id) { unsigned lpc = 0; resource_t *rsc = NULL; resource_t *child_rsc = NULL; crm_devel("Looking for %s in %d objects", id, g_list_length(rsc_list)); for(lpc = 0; lpc < g_list_length(rsc_list); lpc++) { rsc = g_list_nth_data(rsc_list, lpc); if(rsc != NULL && safe_str_eq(rsc->id, id)){ crm_devel("Found a match for %s", id); return rsc; } } for(lpc = 0; lpc < g_list_length(rsc_list); lpc++) { rsc = g_list_nth_data(rsc_list, lpc); child_rsc = rsc->fns->find_child(rsc, id); if(child_rsc != NULL) { crm_devel("Found a match for %s in %s", id, rsc->id); return child_rsc; } } /* error */ return NULL; } node_t * pe_find_node(GListPtr nodes, const char *uname) { unsigned lpc = 0; node_t *node = NULL; for(lpc = 0; lpc < g_list_length(nodes); lpc++) { node = g_list_nth_data(nodes, lpc); if(node != NULL && safe_str_eq(node->details->uname, uname)) { return node; } } /* error */ return NULL; } node_t * pe_find_node_id(GListPtr nodes, const char *id) { unsigned lpc = 0; node_t *node = NULL; for(lpc = 0; lpc < g_list_length(nodes); lpc++) { node = g_list_nth_data(nodes, lpc); if(safe_str_eq(node->details->id, id)) { return node; } } /* error */ return NULL; } gint gslist_color_compare(gconstpointer a, gconstpointer b); color_t * find_color(GListPtr candidate_colors, color_t *other_color) { GListPtr tmp = g_list_find_custom(candidate_colors, other_color, gslist_color_compare); if(tmp != NULL) { return (color_t *)tmp->data; } return NULL; } gint gslist_color_compare(gconstpointer a, gconstpointer b) { const color_t *color_a = (const color_t*)a; const color_t *color_b = (const color_t*)b; /* crm_trace("%d vs. %d", a?color_a->id:-2, b?color_b->id:-2); */ if(a == b) { return 0; } else if(a == NULL || b == NULL) { return 1; } else if(color_a->id == color_b->id) { return 0; } return 1; } gint sort_rsc_priority(gconstpointer a, gconstpointer b) { const resource_t *resource1 = (const resource_t*)a; const resource_t *resource2 = (const resource_t*)b; if(a == NULL) { return 1; } if(b == NULL) { return -1; } if(resource1->priority > resource2->priority) { return -1; } if(resource1->priority < resource2->priority) { return 1; } return 0; } gint sort_cons_strength(gconstpointer a, gconstpointer b) { const rsc_colocation_t *rsc_constraint1 = (const rsc_colocation_t*)a; const rsc_colocation_t *rsc_constraint2 = (const rsc_colocation_t*)b; if(a == NULL) { return 1; } if(b == NULL) { return -1; } if(rsc_constraint1->strength > rsc_constraint2->strength) { return 1; } if(rsc_constraint1->strength < rsc_constraint2->strength) { return -1; } return 0; } gint sort_color_weight(gconstpointer a, gconstpointer b) { const color_t *color1 = (const color_t*)a; const color_t *color2 = (const color_t*)b; if(a == NULL) { return 1; } if(b == NULL) { return -1; } if(color1->local_weight > color2->local_weight) { return -1; } if(color1->local_weight < color2->local_weight) { return 1; } return 0; } /* return -1 if 'a' is more preferred * return 1 if 'b' is more preferred */ gint sort_node_weight(gconstpointer a, gconstpointer b) { const node_t *node1 = (const node_t*)a; const node_t *node2 = (const node_t*)b; + float node1_weight = 0; + float node2_weight = 0; + if(a == NULL) { return 1; } if(b == NULL) { return -1; } + + node1_weight = node1->weight; + node2_weight = node2->weight; - if(node1->weight > node2->weight) { + if(node1->details->unclean || node1->details->shutdown) { + node1_weight = -INFINITY; + } + if(node2->details->unclean || node2->details->shutdown) { + node2_weight = -INFINITY; + } + + if(node1_weight > node2_weight) { crm_devel("%s (%f) > %s (%f) : weight", - node1->details->id, node1->weight, - node2->details->id, node2->weight); + node1->details->id, node1_weight, + node2->details->id, node2_weight); return -1; } - if(node1->weight < node2->weight) { + if(node1_weight < node2_weight) { crm_devel("%s (%f) < %s (%f) : weight", - node1->details->id, node1->weight, - node2->details->id, node2->weight); + node1->details->id, node1_weight, + node2->details->id, node2_weight); return 1; } /* now try to balance resources across the cluster */ if(node1->details->num_resources < node2->details->num_resources) { crm_devel("%s (%d) < %s (%d) : resources", node1->details->id, node1->details->num_resources, node2->details->id, node2->details->num_resources); return -1; } else if(node1->details->num_resources > node2->details->num_resources) { crm_devel("%s (%d) > %s (%d) : resources", node1->details->id, node1->details->num_resources, node2->details->id, node2->details->num_resources); return 1; } crm_devel("%s = %s", node1->details->id, node2->details->id); return 0; } action_t * action_new(resource_t *rsc, enum action_tasks task, const char *timeout, node_t *on_node) { action_t *action = NULL; GListPtr possible_matches = NULL; if(rsc != NULL && task != monitor_rsc) { possible_matches = find_actions(rsc->actions, task, on_node); } if(on_node != NULL && on_node->details->unclean) { crm_warn("Not creating action %s for %s because %s is unclean", task2text(task), rsc?rsc->id:"", on_node->details->id); } if(on_node != NULL && on_node->details->unclean) { crm_warn("Not creating action %s for %s because %s is unclean", task2text(task), rsc?rsc->id:"", on_node->details->id); } if(possible_matches != NULL) { if(g_list_length(possible_matches) > 1) { crm_warn("Action %s for %s on %s exists %d times", task2text(task), rsc?rsc->id:"", on_node?on_node->details->id:"", g_list_length(possible_matches)); } action = g_list_nth_data(possible_matches, 0); crm_devel("Returning existing action (%d) %s for %s on %s", action->id, task2text(task), rsc?rsc->id:"", on_node?on_node->details->id:""); /* todo: free possible_matches */ return action; } if(timeout == NULL && rsc != NULL) { timeout = find_action_timeout(rsc, task); } crm_devel("Creating action %s for %s on %s", task2text(task), rsc?rsc->id:"", on_node?on_node->details->id:""); crm_malloc(action, sizeof(action_t)); if(action != NULL) { action->id = action_id++; action->rsc = rsc; action->task = task; action->node = on_node; action->actions_before = NULL; action->actions_after = NULL; action->failure_is_fatal = TRUE; action->pseudo = FALSE; action->dumped = FALSE; action->discard = FALSE; action->runnable = TRUE; action->processed = FALSE; action->optional = FALSE; action->seen_count = 0; action->extra = g_hash_table_new_full( g_str_hash, g_str_equal, g_hash_destroy_str, g_hash_destroy_str); if(timeout != NULL) { add_hash_param(action->extra, "timeout", timeout); } if(rsc != NULL) { crm_devel("Adding created action to its resource"); rsc->actions = g_list_append(rsc->actions, action); if(task == start_rsc) { rsc->starting = TRUE; } else if(task == stop_rsc) { rsc->stopping = TRUE; } } } crm_devel("Action %d created", action->id); return action; } const char * find_action_timeout(resource_t *rsc, enum action_tasks task) { crm_data_t *op = NULL; if(rsc != NULL && rsc->ops_xml != NULL) { xml_child_iter( rsc->ops_xml, a_child, "op", if(safe_str_eq(task2text(task), crm_element_value(a_child,"name"))) { op = a_child; break; } ); } if(op == NULL) { return NULL; } return crm_element_value(op, "timeout"); } const char * strength2text(enum con_strength strength) { const char *result = ""; switch(strength) { case pecs_ignore: result = "ignore"; break; case pecs_must: result = XML_STRENGTH_VAL_MUST; break; case pecs_must_not: result = XML_STRENGTH_VAL_MUSTNOT; break; case pecs_startstop: result = "start/stop"; break; } return result; } const char * task2text(enum action_tasks task) { const char *result = ""; switch(task) { case no_action: result = "no_action"; break; case stop_rsc: result = CRMD_RSCSTATE_STOP; break; case stopped_rsc: result = CRMD_RSCSTATE_STOP_OK; break; case start_rsc: result = CRMD_RSCSTATE_START; break; case started_rsc: result = CRMD_RSCSTATE_START_OK; break; case shutdown_crm: result = CRM_OP_SHUTDOWN; break; case stonith_node: result = XML_CIB_ATTR_STONITH; break; case monitor_rsc: result = CRMD_RSCSTATE_MON; break; } return result; } void print_node(const char *pre_text, node_t *node, gboolean details) { if(node == NULL) { crm_devel("%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": "); return; } crm_devel("%s%s%sNode %s: (weight=%f, fixed=%s)", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", node->details==NULL?"error ":node->details->online?"":"Unavailable/Unclean ", node->details->uname, node->weight, node->fixed?"True":"False"); if(details && node->details != NULL) { char *pe_mutable = crm_strdup("\t\t"); crm_devel("\t\t===Node Attributes"); g_hash_table_foreach(node->details->attrs, print_str_str, pe_mutable); crm_free(pe_mutable); } if(details) { crm_devel("\t\t===Node Attributes"); slist_iter( rsc, resource_t, node->details->running_rsc, lpc, print_resource("\t\t", rsc, FALSE); ); } } /* * Used by the HashTable for-loop */ void print_str_str(gpointer key, gpointer value, gpointer user_data) { crm_devel("%s%s %s ==> %s", user_data==NULL?"":(char*)user_data, user_data==NULL?"":": ", (char*)key, (char*)value); } void print_color_details(const char *pre_text, struct color_shared_s *color, gboolean details) { if(color == NULL) { crm_devel("%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": "); return; } crm_devel("%s%sColor %d: node=%s (from %d candidates)", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", color->id, color->chosen_node==NULL?"":color->chosen_node->details->uname, g_list_length(color->candidate_nodes)); if(details) { slist_iter(node, node_t, color->candidate_nodes, lpc, print_node("\t", node, FALSE)); } } void print_color(const char *pre_text, color_t *color, gboolean details) { if(color == NULL) { crm_devel("%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": "); return; } crm_devel("%s%sColor %d: (weight=%f, node=%s, possible=%d)", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", color->id, color->local_weight, safe_val5("",color,details,chosen_node,details,uname), g_list_length(color->details->candidate_nodes)); if(details) { print_color_details("\t", color->details, details); } } void print_rsc_to_node(const char *pre_text, rsc_to_node_t *cons, gboolean details) { if(cons == NULL) { crm_devel("%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": "); return; } crm_devel("%s%s%s Constraint %s (%p) - %d nodes:", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", "rsc_to_node", cons->id, cons, g_list_length(cons->node_list_rh)); if(details == FALSE) { crm_devel("\t%s (score=%f : node placement rule)", safe_val3(NULL, cons, rsc_lh, id), cons->weight); slist_iter( node, node_t, cons->node_list_rh, lpc, print_node("\t\t-->", node, FALSE) ); } } void print_rsc_colocation(const char *pre_text, rsc_colocation_t *cons, gboolean details) { if(cons == NULL) { crm_devel("%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": "); return; } crm_devel("%s%s%s Constraint %s (%p):", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", XML_CONS_TAG_RSC_DEPEND, cons->id, cons); if(details == FALSE) { crm_devel("\t%s --> %s, %s", safe_val3(NULL, cons, rsc_lh, id), safe_val3(NULL, cons, rsc_rh, id), strength2text(cons->strength)); } } void print_resource(const char *pre_text, resource_t *rsc, gboolean details) { if(rsc == NULL) { crm_devel("%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": "); return; } rsc->fns->dump(rsc, pre_text, details); } void print_action(const char *pre_text, action_t *action, gboolean details) { if(action == NULL) { crm_devel("%s%s: ", pre_text==NULL?"":pre_text, pre_text==NULL?"":": "); return; } switch(action->task) { case stonith_node: case shutdown_crm: crm_devel("%s%s%sAction %d: %s @ %s", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", action->discard?"Discarded ":action->optional?"Optional ":action->runnable?action->processed?"":"(Provisional) ":"!!Non-Startable!! ", action->id, task2text(action->task), safe_val4(NULL, action, node, details, uname)); break; default: crm_devel("%s%s%sAction %d: %s %s @ %s", pre_text==NULL?"":pre_text, pre_text==NULL?"":": ", action->optional?"Optional ":action->runnable?action->processed?"":"(Provisional) ":"!!Non-Startable!! ", action->id, task2text(action->task), safe_val3(NULL, action, rsc, id), safe_val4(NULL, action, node, details, uname)); break; } if(details) { #if 1 crm_devel("\t\t====== Preceeding Actions"); slist_iter( other, action_wrapper_t, action->actions_before, lpc, print_action("\t\t", other->action, FALSE); ); crm_devel("\t\t====== Subsequent Actions"); slist_iter( other, action_wrapper_t, action->actions_after, lpc, print_action("\t\t", other->action, FALSE); ); #else crm_devel("\t\t====== Subsequent Actions"); slist_iter( other, action_wrapper_t, action->actions_after, lpc, print_action("\t\t", other->action, FALSE); ); #endif crm_devel("\t\t====== End"); } else { crm_devel("\t\t(seen=%d, before=%d, after=%d)", action->seen_count, g_list_length(action->actions_before), g_list_length(action->actions_after)); } } void pe_free_nodes(GListPtr nodes) { while(nodes != NULL) { GListPtr list_item = nodes; node_t *node = (node_t*)list_item->data; struct node_shared_s *details = node->details; nodes = nodes->next; crm_trace("deleting node"); crm_trace("%s is being deleted", details->uname); print_node("delete", node, FALSE); if(details != NULL) { if(details->attrs != NULL) { g_hash_table_foreach_remove(details->attrs, ghash_free_str_str, NULL); g_hash_table_destroy(details->attrs); } } } if(nodes != NULL) { g_list_free(nodes); } } gboolean ghash_free_str_str(gpointer key, gpointer value, gpointer user_data) { crm_free(key); crm_free(value); return TRUE; } void pe_free_colors(GListPtr colors) { while(colors != NULL) { GListPtr list_item = colors; color_t *color = (color_t *)list_item->data; struct color_shared_s *details = color->details; colors = colors->next; if(details != NULL) { pe_free_shallow(details->candidate_nodes); pe_free_shallow_adv(details->allocated_resources, FALSE); crm_free(details->chosen_node); crm_free(details); } crm_free(color); } if(colors != NULL) { g_list_free(colors); } } void pe_free_shallow(GListPtr alist) { pe_free_shallow_adv(alist, TRUE); } void pe_free_shallow_adv(GListPtr alist, gboolean with_data) { GListPtr item; GListPtr item_next = alist; while(item_next != NULL) { item = item_next; item_next = item_next->next; if(with_data) { /* crm_trace("freeing %p", item->data); */ crm_free(item->data); } item->data = NULL; item->next = NULL; g_list_free(item); } } void pe_free_resources(GListPtr resources) { volatile GListPtr list_item = NULL; resource_t *rsc = NULL; while(resources != NULL) { list_item = resources; rsc = (resource_t *)list_item->data; resources = resources->next; pe_free_shallow_adv(rsc->candidate_colors, TRUE); g_hash_table_destroy(rsc->parameters); rsc->fns->free(rsc); } if(resources != NULL) { g_list_free(resources); } } void pe_free_actions(GListPtr actions) { while(actions != NULL) { GListPtr list_item = actions; action_t *action = (action_t *)list_item->data; actions = actions->next; pe_free_shallow(action->actions_before);/* action_warpper_t* */ pe_free_shallow(action->actions_after); /* action_warpper_t* */ action->actions_before = NULL; action->actions_after = NULL; g_hash_table_destroy(action->extra); crm_free(action); } if(actions != NULL) { g_list_free(actions); } } void pe_free_rsc_colocation(rsc_colocation_t *cons) { if(cons != NULL) { crm_devel("Freeing constraint %s (%p)", cons->id, cons); crm_free(cons); } } void pe_free_rsc_to_node(rsc_to_node_t *cons) { if(cons != NULL) { /* right now we dont make copies so this isnt required */ /* pe_free_shallow(cons->node_list_rh); */ /* node_t* */ crm_free(cons); } } GListPtr find_actions(GListPtr input, enum action_tasks task, node_t *on_node) { GListPtr result = NULL; slist_iter( action, action_t, input, lpc, if(action->task == task) { if(on_node == NULL) { result = g_list_append(result, action); } else if(action->node == NULL) { /* skip */ crm_warn("While looking for %s action on %s, " "found an unallocated one. Assigning" " it to the requested node...", task2text(task), on_node->details->uname); action->node = on_node; result = g_list_append(result, action); } else if(safe_str_eq(on_node->details->id, action->node->details->id)) { result = g_list_append(result, action); } } ); return result; } void set_id(crm_data_t * xml_obj, const char *prefix, int child) { int id_len = 0; gboolean use_prefix = TRUE; gboolean use_child = TRUE; char *new_id = NULL; const char *id = crm_element_value(xml_obj, XML_ATTR_ID); id_len = 1 + strlen(id); if(child > 999) { crm_err("Are you insane?!?" " The CRM does not support > 1000 children per resource"); return; } else if(child < 0) { use_child = FALSE; } else { id_len += 4; /* child */ } if(prefix == NULL || safe_str_eq(id, prefix)) { use_prefix = FALSE; } else { id_len += (1 + strlen(prefix)); } crm_malloc(new_id, id_len); if(use_child) { snprintf(new_id, id_len, "%s%s%s:%d", use_prefix?prefix:"", use_prefix?":":"", id, child); } else { snprintf(new_id, id_len, "%s%s%s", use_prefix?prefix:"", use_prefix?":":"", id); } set_xml_property_copy(xml_obj, XML_ATTR_ID, new_id); crm_free(new_id); }