erlang的热更新

时间:2023-03-08 20:09:02

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}}; %...........
%...........省略若干行
%................ 26 code_change("1.1", {state, Cnt}, _Extra) ->
27 {ok, {state, chk, Cnt}}; code_change(_OldVsn, State, _Extra) ->
{ok, State}. %...........
%...........省略若干行
%................

  我们修改了state结构体,修改了init/1函数,而且重写了code_change/3,下面我们运行如下

 1 10> compile:file(tt13).          /*编译代码
2 {ok,tt13}
3 11> sys:suspend(tt13).           /*暂停服务
4 ok
5 12> code:purge(tt13).           /*清除旧的代码,如果有运行的化
6 false
7 13> code:load_file(tt13).        /*加载新的代码
8 {module,tt13}
9 14> sys:change_code(tt13,tt13,"1.1",[]).    /*修改state状态
10 ok
11 15> sys:resume(tt13).                /*恢复服务
12 ok
16> tt13:test().
*DBG* tt13 got call test from <0.32.0>
*DBG* tt13 sent {ok,32} to <0.32.0>, new state {state,chk,64}
{ok,32}
17> tt13:test().
*DBG* tt13 got call test from <0.32.0>
*DBG* tt13 sent {ok,64} to <0.32.0>, new state {state,chk,128}
{ok,64}
18> tt13:test().
*DBG* tt13 got call test from <0.32.0>
*DBG* tt13 sent {ok,128} to <0.32.0>, new state {state,chk,256}
{ok,128}

  整个替换过程是10-15步,注意这个过程中的tt13服务是hang住的,如果这时候使用服务,会出现timeout,所以一般这5步都是同时执行。

       后面可以看到state状态已经改变