erlang的热更新

erlang作为一个为电信级别而出现的语言,热更新是其最重要的特性之一

 热代码升级-Erlang允许程序代码在运行系统中被修改。旧代码能被逐步淘汰而后被新代码替换。在此过渡期间,新旧代码是共存的。

下面我们以最典型的gen_server为例子,讲解一下这个BT的功能

-module(tt13).
-behaviour(gen_server).

-export([test/0]).
-export([start_link/0, stop/0, init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).

-record(state, {cnt}).

-define(SERVER, ?MODULE).

%%--------------------------------------------------------------------
%% Function: start_link() -> {ok,Pid} | ignore | {error,Error}
%% Description: Starts the server
%%--------------------------------------------------------------------
start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], [{debug, [trace]}]).

test() ->
    gen_server:call(?SERVER, test). 

stop() -> 
    gen_server:cast(?SERVER, stop). 

%%--------------------------------------------------------------------
%% Function: init(Args) -> {ok, State} |
%%                         {ok, State, Timeout} |
%%                         ignore               |
%%                         {stop, Reason}
%% Description: Initiates the server
%%--------------------------------------------------------------------
init(_) -> {ok, #state{cnt=1}}.
%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
%%                                      {reply, Reply, State, Timeout} |
%%                                      {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, Reply, State} |
%%                                      {stop, Reason, State}
%% Description: Handling call messages
handle_call(test, _From, #state{cnt=Cnt} = State) ->
    {reply, {ok, Cnt}, State#state{cnt=Cnt+1}};

handle_call(stop, _From, State) ->
    {stop, normal, ok, State};

handle_call(_Unrec, _From, State) ->
    {reply, {error, invalid_call}, State}.

%%--------------------------------------------------------------------
%% Function: handle_cast(Msg, State) -> {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, State}
%% Description: Handling cast messages
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
    {noreply, State}.

%%--------------------------------------------------------------------
%% Function: handle_info(Info, State) -> {noreply, State} |
%%                                       {noreply, State, Timeout} |
%%                                       {stop, Reason, State}
%% Description: Handling all non call/cast messages
%%--------------------------------------------------------------------

handle_info(_Info, State) ->
    {noreply, State}.

%%--------------------------------------------------------------------
%% Function: terminate(Reason, State) -> void()
%% Description: This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any necessary
%% cleaning up. When it returns, the gen_server terminates with Reason.
%% The return value is ignored.
%%--------------------------------------------------------------------

terminate(_Reason, _State) ->
    io:format("hello gen server: terminating~n").

%%--------------------------------------------------------------------
%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
%% Description: Convert process state when code is changed
%%--------------------------------------------------------------------

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

%%====================================================================
%%other fun
%%====================================================================

 编译运行结果

1> c(tt13).
{ok,tt13}
2> tt13:start_link().
{ok,<0.39.0>}
3> tt13:test().
*DBG* tt13 got call test from <0.32.0>
*DBG* tt13 sent {ok,1} to <0.32.0>, new state {state,2}
{ok,1}
4> tt13:test().
*DBG* tt13 got call test from <0.32.0>
*DBG* tt13 sent {ok,2} to <0.32.0>, new state {state,3}
{ok,2}
5> tt13:test().
*DBG* tt13 got call test from <0.32.0>
*DBG* tt13 sent {ok,3} to <0.32.0>, new state {state,4}
{ok,3}

 如果修改了函数,可以直接运行

-module(tt13).
-version("1.1").
-behaviour(gen_server).

%...........
%...........省略若干行
%................

handle_call(test, _From, #state{cnt=Cnt} = State) ->
    {reply, {ok, Cnt}, State#state{cnt=Cnt*2}};

%...........
%...........省略若干行
%................

 可以看到我们修改了计数的方法,而且修改了版本号,然后我们继续运行

6> c(tt13).                                    %编译新的代码
{ok,tt13}
7> tt13:test().      
*DBG* tt13 got call test from <0.32.0>
*DBG* tt13 sent {ok,4} to <0.32.0>, new state {state,8}
{ok,4}
8> tt13:test().
*DBG* tt13 got call test from <0.32.0>
*DBG* tt13 sent {ok,8} to <0.32.0>, new state {state,16}
{ok,8}
9> tt13:test().
*DBG* tt13 got call test from <0.32.0>
*DBG* tt13 sent {ok,16} to <0.32.0>, new state {state,32}
{ok,16}

 可以看到代码就直接替换了,注意编译的时候会用新的代码替换下一次运行的结果,正在运行还是old code,所以不要编译多次(一般在测试环境先进行热更新测试)。

   如果要替换init/1里面的代码?这个方法肯定是不行的,因为init/1代码只运行一次,比如我要修改state结构体,那要怎么弄呢

 

-module(tt13).
-version("2.0").
-behaviour(gen_server).

-record(state, {testcheck, cnt}).
%...........
%...........省略若干行
%................

init(_) -> {ok, #state{testcheck='chk', cnt=1}}.
%%--------------------------------------------------------------------
%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} |
%%                                      {reply, Reply, State, Timeout} |
%%                                      {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, Reply, State} |
%%                                      {stop, Reason, State}
%% Description: Handling call messages
handle_call(test, _From, #state{cnt=Cnt} = State) ->
    {reply, {ok, Cnt}, State#state{cnt=Cnt+1}};
%...........
%...........省略若干行
%................


猜你喜欢

转载自www.cnblogs.com/tudou008/p/9473639.html