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}}; %...........
%...........省略若干行
%................ 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状态已经改变
erlang的热更新的更多相关文章
- [Erlang10]为什么热更新时,Shell执行2次l(Module)后会把原来用到Module的进程 kill?
0. 问题引入: -module(hot_code_server). -compile(export_all). start() –> erlang:register(?MODULE, erla ...
- 【原】Android热更新开源项目Tinker源码解析系列之三:so热更新
本系列将从以下三个方面对Tinker进行源码解析: Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Android热更新开源项目Tinker源码解析系列之二:资源文件热更新 A ...
- 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新
[原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...
- 【原】Android热更新开源项目Tinker源码解析系列之二:资源文件热更新
上一篇文章介绍了Dex文件的热更新流程,本文将会分析Tinker中对资源文件的热更新流程. 同Dex,资源文件的热更新同样包括三个部分:资源补丁生成,资源补丁合成及资源补丁加载. 本系列将从以下三个方 ...
- iOS热更新-8种实现方式
一.JSPatch 热更新时,从服务器拉去js脚本.理论上可以修改和新建所有的模块,但是不建议这样做. 建议 用来做紧急的小需求和 修复严重的线上bug. 二.lua脚本 比如: wax.热更新时,从 ...
- 【.net 深呼吸】程序集的热更新
当一个程序集被加载使用的时候,出于数据的完整性和安全性考虑,程序集文件(在99.9998%的情况下是.dll文件)会被锁定,如果此时你想更新程序集(实际上是替换dll文件),是不可以操作的,这时你得把 ...
- 谁偷了我的热更新?Mono,JIT,iOS
前言 由于匹夫本人是做游戏开发工作的,所以平时也会加一些玩家的群.而一些困扰玩家的问题,同样也困扰着我们这些手机游戏开发者.这不最近匹夫看自己加的一些群,常常会有人问为啥这个游戏一更新就要重新下载,而 ...
- ReactNative 告别CodePush,自建热更新版本升级环境
微软的CodePush热更新非常难用大家都知道,速度跟被墙了没什么区别. 另外一方面,我们不希望把代码放到别人的服务器.自己写接口更新总归感觉安全一点. so,就来自己搞个React-Native A ...
- ReactNative 使用微软的CodePush进行热更新,继续填坑
1.别被开发环境骗了 在我们开发react native的时候,一键运行工程,js改了,只要cmd+R就可以刷新了.然后会轻易以为真正app上线的时候也是一样,只要app一打开就是最新的. 其实!这是 ...
随机推荐
- 关于iframe的一些操作
用于自己学习,本身对于js的一些东西没有深入了解,也是用到再去查 1.如果现在在一个页面,想要获取这个页面中的iframe中嵌入的页面中的一个指定id的div var iframeObj = docu ...
- es6 遍历总结
1.for in / for of for in --> index是key值 var array = [1,2,3,4,5]; for(let index in array) { consol ...
- centos7 安装oracle 11g数据库
1.新建oracle用户 groupadd oracle useradd -g oracle oracle .修改操作系统核心参数 在Root用户下执行以下步骤: 1)修改用户的SHELL的限制,修改 ...
- SV-assertion
断言(assert)是一种描述性语言,通过描述的期望结果来进行仿真验证. 断言有一个更加基础的信息,我们称为属性(property),属性可以作为断言结果,功能覆盖点,形式检查和约束随机激励生成. 断 ...
- Grid move
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Web应用启动时,后台自动启动一个线程(转)
原文:http://blog.sina.com.cn/s/blog_6810dfc20101ipzq.html Web应用启动时,后台自动启动一个线程 (1)前言 前几天,manager问道一个问题: ...
- shell的输入参数
$# 参数格式 $0 $1 $2 ...第一个,第二个参数...
- Windows环境下IOS APP打包上传AppStore详细流程
我们知道在上架苹果应用过程中,申请发布证书需要用到钥匙串,上传ipa需要用到xcode或Application loader提交构建版本,这都需要Mac苹果机. 本文介绍如何在Windows环境下申请 ...
- go 语言
go语言(或 Golang)是Google在 2007 年开发的一种开源编程语言,于2009年11月开源,2012年发布go稳定版 go是非常年轻的一门语言,它的主要目标是“兼具Python 等动态语 ...
- 2.1JAVA基础复习——JAVA语言的基础组成注释和常量变量
JAVA语言的基础组成有: 1.关键字:被赋予特殊含义的单词. 2.标识符:用来标识的符号. 3.注释:用来注释说明程序的文字. 4.常量和变量:内存存储区域的表示. 5.运算符:程序中用来运算的符号 ...