pydistsim.demo_algorithms.santoro2007.mega_merger.algorithm.MegaMergerAlgorithm
- class MegaMergerAlgorithm(*args, **kwargs)[source]
Bases:
NodeAlgorithmMega-Merger
This algorithm is a distributed algorithm that elects a single node in a network. The way it works is by creating cities (a grouping of nodes) and merging them together, while maintaining a tree structure with a root (the downtown node) that makes the decisions.
Ultimately, when the cities are merged, the downtown node is elected as the root of the tree.
The algorithm is based on the descriptions in the book Design and Analysis of Distributed Algorithms by Nicola Santoro.
Parameters
- percentage_of_initiatorsfloat
The percentage of initiators in the network.
- INF_WEIGHTT
Value representing infinity.
- WEIGHT_LISTlist[T] | Callable[[int], T]
Mapping of weights for the links.
- CITY_LISTlist[T] | Callable[[int], T]
Mapping of cities in the network.
Methods
__init__add_childadd_internal_linkadd_observersalarmapply_restrictionsApply all applicable restrictions.
block_inboxBlock the inbox of a node, so that only messages that satisfy the filter can be processed.
block_msgcheck_algorithm_initializationCheck if the algorithm's current state matches the expected initial state.
check_algorithm_terminationCheck if the algorithm's current state matches the expected termination state.
check_restrictionsCheck if the restrictions are satisfied.
clear_observerscloseClose the inbox of the node for a specific neighbor.
defaultdefault_INITIATORdisable_alarmDisable an alarm.
disable_all_node_alarmsDisable all alarms set for the node.
Assumes there are external links
get_external_linksget_internal_linksMethod used to initialize the algorithm.
is_done_asking_outsideis_friendly_mergeis_haltedCheck if the distributed algorithm has come to an end or deadlock, i.e. there are no messages to pass and no alarms set.
is_initializedis_terminationlog_ignorelog_receivednotify_observersHandles the 'are you outside' message received by a node.
on_awakeningHandles the reception of an external message by a node.
Handles the internal message received by a node.
Handles the LET_US_MERGE message received by a node.
Handles the MERGE_ME message received by a node.
Handles the event when a minimum link weight message is received by a node.
Handles the notification message received by a node.
on_terminationopenOpen the inbox of the node for a specific neighbor.
Handles the partial termination of a node in the Mega Merger algorithm.
receivingreceiving_ASKING_OUTSIDEreceiving_COMPUTING_MIN_WEIGHTSreceiving_NOT_ELECTEDreceiving_SLEEPINGreceiving_WAITING_PARENTresetReset the algorithm to its initial state.
reset_metasendSend content to destination, with header.
send_INF_WEIGHTSends a 'LET_US_MERGE' message from the given node to its link with the minimum weight.
send_msgSend a message to nodes listed in message's destination field.
set_alarmSet an alarm for the node.
set_cityset_parentspontaneouslystepunblock_by_headerunblock_inboxUnblock the inbox of a node, so that all messages can be processed.
update_alarm_timeUpdate the time of an alarm by adding a time difference.
update_link_with_min_weightHandles the event when a node's city changes.
when_friendly_mergewhen_terminationAttributes
INITuple of statuses that nodes should have at the beginning of the algorithm.
Tuple of statuses that nodes should have at the end of the algorithm.
Tuple of restrictions that must be satisfied for the algorithm to run.
default_paramsnetworkrequired_paramsnwmNode wrapper manager.
- S_init = (MMStatus.INITIATOR, MMStatus.SLEEPING)
Tuple of statuses that nodes should have at the beginning of the algorithm.
- S_term = (MMStatus.ELECTED, MMStatus.NOT_ELECTED)
Tuple of statuses that nodes should have at the end of the algorithm.
- algorithm_restrictions = (<class 'pydistsim.restrictions.communication.BidirectionalLinks'>, <class 'pydistsim.restrictions.reliability.TotalReliability'>, <class 'pydistsim.restrictions.topological.Connectivity'>)
Tuple of restrictions that must be satisfied for the algorithm to run.
- initializer()[source]
Method used to initialize the algorithm. Is always called first.
NodeAlgorithmsubclasses may want to reimplement it.Base implementation sends an INI message to the node with the lowest id and applies all restrictions.
- on_are_you_outside(node: MMNode, message: Message)[source]
Handles the ‘are you outside’ message received by a node.
This method processes the message to determine if the node is part of the same city as the sender or if it is outside. Depending on the city levels and the source of the message, it will either handle the message internally, send an internal or external response, or block the message.
- on_external(node: MMNode, message: Message)[source]
Handles the reception of an external message by a node.
This method is called when a node receives a message confirming the link leads to an external node. It updates the node’s state and processes the message according to the Mega Merger algorithm.
- Behavior:
Sets the node’s external_received flag to True.
If the message source is not the expected link, logs a debug message and ignores the message.
Updates the node’s status to COMPUTING_MIN_WEIGHTS (prev. was ASKING_OUTSIDE).
Updates the link with the minimum weight.
- If the node is done asking outside:
If the node is the downtown, sends a “let us merge” message.
If the node is not the root, sends the minimum link weight to the parent node and updates the
node’s status.
If the minimum weight is infinite, triggers partial termination.
- on_internal(node: MMNode, message: Message)[source]
Handles the internal message received by a node.
This method processes an internal message received by a node and updates the node’s state accordingly. It performs the following actions: - Sets as internal the link between the node and the message source. - Checks if the message source is the expected link asking outside.
If not, logs a debug message and ignores the message.
Updates the node’s status to COMPUTING_MIN_WEIGHTS if the message source is the expected.
- If the node is done asking outside:
If the node is the root, triggers a new merge or the global termination.
If the node is not the root, sends the minimum link weight to the parent node.
- on_let_us_merge(node: MMNode, message: Message)[source]
Handles the LET_US_MERGE message received by a node.
This method processes the LET_US_MERGE message and determines the appropriate action based on the relationship between the sender and the receiver nodes, as well as their respective cities and levels.
Actions: - If the sender’s city is the same as the receiver’s city, sends the LET_US_MERGE message down the tree. - If the merge is considered friendly, it triggers a friendly merge. - If the receiver’s city level is higher than the sender’s, it triggers a forced merge. - If the receiver’s city level is the same as the sender’s, it blocks the message. - Otherwise, it ignores the message.
- on_merge_me(node: MMNode, message: Message)[source]
Handles the MERGE_ME message received by a node.
This method processes the MERGE_ME message, which indicates a forced merge between the sender and the receiver. It triggers the merge and updates the node’s state accordingly.
- on_min_link_weight(node: MMNode, message: Message)[source]
Handles the event when a minimum link weight message is received by a node.
This method processes the received message, updates the node’s state, and determines the next steps based on the node’s role in the tree (root or non-root).
- on_notification(node: MMNode, message: Message)[source]
Handles the notification message received by a node.
This method processes a notification message, updates the node’s city, and manages the parent-child relationships within the node’s tree structure. It also sends the notification to the node’s children and triggers any necessary actions when the city changes.
- partial_termination(node)[source]
Handles the partial termination of a node in the Mega Merger algorithm.
This method sets the status of the given node to NOT_ELECTED and sends a termination message to all its children nodes.
Partial termination means that the node is not elected as the downtown node and neither it nor its children will make any further progress in the algorithm.
- send_let_us_merge(node, new_city, road_weight)[source]
Sends a ‘LET_US_MERGE’ message from the given node to its link with the minimum weight.
This method handles both border and internal nodes. For border nodes, it checks if the minimum weight of the node matches the provided road weight before sending the message. If the weights do not match, an error is logged and the method returns without sending the message. For internal nodes, it sends the message directly.
- when_city_change(node, new_city, to_ask_min_weight)[source]
Handles the event when a node’s city changes.
This method resets the node’s metadata and processes blocked messages related to merging and external status checks. Depending on the new city’s level and the message’s city level, it either merges the node with the source of the message or marks the source as internal or external.
Internal Processors: - let_us_merge_processor: Processes messages with the header LET_US_MERGE. - are_you_outside_processor: Processes messages with the header ARE_YOU_OUTSIDE.
If to_ask_min_weight is True, the node’s status is set to COMPUTING_MIN_WEIGHTS, and it initiates the process to ask for the minimum weight from external links.