經典–淺析Erlang分布的核心技術

來源:互聯網
上載者:User

轉載:http://developer.51cto.com/art/200812/100298.htm

 

    本文簡單介紹了Erlang系統的核心技術和幾個重要特性:分布、多核心支援、fp;並對erlang最強大的特性——分布特性做了重點介紹。

     

    Erlang系統在我看來有3個特性: 1. 分布 2. 多核心支援 3. fp。 這3個特性中分布我認為是erlang最強大的,從第一個版本就支援到現在,相對非常成熟,而多核心支援是這幾年才加進去的。
    erlang的分布系統做到了2點:

    1.節點的位置無關性;

    2. 對使用者分布式透明的。

    具體體現就是node是靠名字識別的, 進程也是靠pid來識別。
    分布系統就要實現節點間通訊,erlang也不列外。 erlang的節點通訊介質是可以替換的 目前官方版本支援tcp, ssl通訊。

    可以用 -proto_dist xxxx來選擇通道。 目前支援inet_ssl inet_tcp 使用者很容易模仿這這2個通訊協議,寫個自己的傳輸通道,就是要求這個通道是可靠的,不能丟失資訊。

    這幾個實現防火牆友好:

    {inet_dist_use_interface, ip_address()}     //If the host of an Erlang node has several network interfaces, 
    //this parameter specifies which one to listen on.
    See inet(3) for the type definition of ip_address(). {inet_dist_listen_min, First}     See below. {inet_dist_listen_max, Last}     Define the First..Last port range for the listener socket of a distributed Erlang node. 

    erlang的核心裡面和分布相關的erl模組主要有net_kernel inet_tcp_dist inet_ssl_dist inet_tcp dist_util erlang(trap send/link等語義)。
    當使用者運行erl -sname xxxxx啟動erlang系統的時候 kernel模組就會啟動net_kernel和epmd模組。

    epmd的作用是提供一個node名稱到監聽地址連接埠的映射。epmd值守在知名連接埠4369上.
    net_kernel會啟動proto_dist比如說inet_tcp_dist監聽連接埠,同時把連接埠報到epmd.

    這時候erts就準備好了節點通訊。
    這時候另外一個節點要和我們通訊的時候,首先要串連,流程大概是這樣的:
    1. 根據節點名找到節點地址。
    2. 查詢節點的4369連接埠,也就是epmd,向它要節點對應的inet_tcp_dist監聽的連接埠。
    3. 發起串連, 握手,cookie認證,如果未授權就失敗。
    4. 記錄節點名稱和響應的資訊。

    所以節點要正常通訊要考慮到firewall和nat的影響,以及cookie正常。

    要給節點發訊息首先要保證我們和節點聯絡過,也就是說我們的節點表裡可以查到這個節點。
    net_kernel要節點的可用性, 會定期發tick訊息堅持節點的可達。對端節點也會主動發送nodeup,nodeup等訊息,協助維護。

    net_ticktime = TickTime     Specifies the net_kernel tick time. TickTime is given in seconds. Once every TickTime/4 
    second,all connected nodes are ticked (if anything else has been written to a node) and if 
    nothing has been received from another node within the last four (4) tick times that node is 
    considered to be down. This ensures that nodes which arenot responding, for reasons such as 
    hardware errors, are considered to be down. 

    節點有2種類型可見的和不可見的。erlang節點是可見的, c_interface寫的節點不可見,因為c模組提供的節點能力有限。

    erlang進程見通訊可以通過 1. pid  2.進程名稱 來進行。 erlang系統很大的威力就在用elang實現了名稱和pid的全域性維護。也就是說erlang做了個相當複雜的模組來解決名稱失效 重複 查詢功能。正常情況下名稱是全域性的,也就是erlang的節點是全聯通的,可以通過來調整。

    auto_connect = Value     Specifies when no will be automatically connected. If this parameter is not specified, 
    a node is always automatically connected, e.g when a message is to be sent to that node.
    Value is one of: never Connections are never automatically connected, they must be explicitly connected.
    See net_kernel(3).     once         Connections will be established automatically, but only once per node. If a node goes 
    down,
    it must thereafter be explicitly connected. See net_kernel(3).

    erlang實現透明進程通訊的關鍵點在於pid的變換:
    pid {X,Y,Z} 在發到網路的時候發出去的格式是{sysname, Y, Z}
    因為節點之前互相聯絡過 所以互相知道對方的sysname, 而且sysname在dist_entry裡儲存,當對端收到dec_pid的時候,用peer sysname 的查到在自己dist_entry裡面的索引,然後用這個index 來構造新的pid,即 {index, Y, Z}。
    erlang給一個pid發訊息的時候, 首先檢查Pid是本地的還是外部的, 如果是外部的,則進行上面的變換,然後通過inet_tcp_dist模組,沿著inet_tcp, inet_drv這條線發送出去。
    這條訊息到達對端的時候 inet_drv首先受到這條訊息, 照理說應該提交給inet_tcp, 然後再到inet_tcp_dist, net_kernel來處理.但是erlang為了效率的考慮做了個折中。在inet_drv裡面driver_output*中檢查訊息的類型 如果是dist來的訊息,就給erts_net_message來處理。
    這個erts_net_message處理以下幾個重要訊息:

    #define DOP_SEND 2 #define DOP_EXIT 3 #define DOP_UNLINK 4 #define DOP_NODE_LINK 5 #define DOP_REG_SEND 6 #define DOP_GROUP_LEADER 7 #define DOP_EXIT2 8 #define DOP_SEND_TT 12 #define DOP_EXIT_TT 13 #define DOP_REG_SEND_TT 16 #define DOP_EXIT2_TT 18 #define DOP_MONITOR_P 19 #define DOP_DEMONITOR_P 20 #define DOP_MONITOR_P_EXIT 21

    如果是DOP_SEND的話,就把message放到變換好的進程的隊列中去。
    這個核心的功能,由beam的c模組(dist.c, erl_node_tables.c, io.c )和net_kernel模組一起實現。然後在這底層的原語上進一步實現了如rpc 這樣的上層模組,進一步方便了使用者.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.