[Erlang 0005] net_kernel: monitor_nodes subscribe to node connection \ disconnect message

Source: Internet
Author: User

To establish a connection between nodes in Erlang, we can use net_adm: Ping (). If the connection succeeds, Pong is returned. If the connection fails, Pang is returned. In practice, we not only need to establish a connection, for example, we need to do something when we establish a connection with other nodes or when other nodes are disconnected, such as logging when the node is down, this requires the corresponding information at the corresponding time; Erlang provides the following method in the net_kernel Library: net_kernel: monitor_nodes (FLAG ); call this method to subscribe to messages with node status changes. when a node is added, a nodeup message is sent to all subscription processes, and a nodedown message is sent when a node is disconnected.

In the Specification Description, We can customize the subscribed messages:

Doc address: http://www.erlang.org/doc/man/net_kernel.html#monitor_nodes-1

Monitor_nodes (FLAG)-> OK | Error
Monitor_nodes (flag, options)-> OK | Error

Types:

Flag = Boolean ()
Options = [Option]
Option = {node_type, nodetype} | nodedown_reason
Nodetype = visible | hidden | all
Error = Error | {error, term ()}

  

A typical application is the code of rabbit_node_monitor in the rabbitmq project. For more information about the rabbitmq project, click here:

In the following code, rabbit_node_monitor is a gen_server. In the callback function it starts, the net_kernel: monitor_nodes (true) method is called, at the same time, the processing of nodeup and nodedown messages is added to handle_info. This Code records the logs when establishing a link with other nodes (nodeup, when the node is down (nodedown), the database and network are cleaned up.

Location of the relevant code file: rabbit_networking rabbit_amqqueue

-Module (rabbit_node_monitor ).

-Behaviour (gen_server ).

-Export ([start_link/0]).

-Export ([init/1, handle_call/3, handle_cast/2, handle_info/2,
Terminate/2, code_change/3]).

-Define (server ,? Module ).

% --------------------------------------------------------------------

Start_link ()->
Gen_server: start_link ({local ,? Server },? Module, [], []).

% --------------------------------------------------------------------

Init ([])->
OK = net_kernel: monitor_nodes (true ),
{OK, no_state }.

Handle_call (_ Request, _ from, state)->
{Noreply, State }.

Handle_cast (_ MSG, state)->
{Noreply, State }.

Handle_info ({nodeup, node}, state)->
Rabbit_log: Info ("Node ~ P up ", [node]),
{Noreply, State };
Handle_info ({nodedown, node}, state)->
Rabbit_log: Info ("Node ~ P down ", [node]),
% Todo: This may turn out to be a performance hog when there are
% Lots of nodes. We really only need to execute this code on
% * One * node, rather than all of them.
OK = rabbit_networking: on_node_down (node ),
OK = rabbit_amqqueue: on_node_down (node ),
{Noreply, State };
Handle_info (_ INFO, state)->
{Noreply, State }.

Terminate (_ reason, _ state)->
OK.

Code_change (_ oldvsn, state, _ extra)->
{OK, State }.

% --------------------------------------------------------------------

There are so many things that can be mined in a successful open-source project. When learning a new thing, you need to explore a wide range of open-source projects to broaden your horizons, accumulate knowledge, and continue!

17:57:57 update active disconnection Node

 

Erlang: disconnect unwanted nodes when you use same cookie for all nodes in a distributed Erlang system, you might have probably seen that all the nodes are interconnected with each other.

(Mathu @ mathu)>
(Mathu @ mathu)> nodes ().
[Mathu9 @ mathu, mathu1 @ mathu, mathu2 @ mathu, mathu3 @ mathu,
Mathu4 @ mathu, mathu5 @ mathu, mathu6 @ mathu, mathu7 @ mathu,
Mathu15 @ mathu, mathu12 @ mathu, mathu14 @ mathu, mathu11 @ mathu,
Mathu10 @ mathu, mathu13 @ mathu, mathu8 @ mathu, mathu9 @ mathu]
(Mathu @ mathu)>

Having unwanted nodes connected is overhead for the system. Therefore the bellow one-liner will help to make it clean.

 [Net_kernel: disconnect (x) | x <-nodes () -- [list_of_wanted_nodes].

(Mathu @ mathu) 3> [net_kernel: disconnect (x) | x <-nodes () -- [mathu3 @ mathu, mathu4 @ mathu].
[True, true,
True, true]
(Mathu @ mathu) 4> nodes ().
[Mathu3 @ mathu, mathu4 @ mathu]
(Mathu @ mathu) 5>

-Mathuvathanan mounasmay

 

It is better to kill the following nodes that can be linked:

allow(Nodes) -> ok | errorTypes:Nodes = [node()]Limits access to the specified set of nodes. Any access attempts made from (or to) nodes not in Nodes will be rejected.Returns error if any element in Nodes is not an atom.

Yu Feng has an article devoted to: How to restrict the access to the cluster of Erlang net_kernel: Allow http://blog.yufeng.info/archives/1752

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.