Code reloading in Erlang is limited to only two versions?

Erlang's ability to easily reload code without discarding existing connections is a very attractive proposition, largely recognized as a feature of the language.

What is not explicitly advertised is that, apparently, you can do such a seamless reload in one go, before discarding existing connections that have not yet been able to switch the execution code path from old to new.

Is this really true, or does Erlang / OTP have an additional advanced code reloading feature that allows you to refuse to execute executable code from the previous generation when the second code reload is performed? ( I mean, for example, nginx can be updated binary at any time. )

+3


source to share


2 answers


Only 2 versions. To illustrate the behavior, I used a small module:

-module (tver).

-compile([export_all]).

start(Name) ->
    Pid = spawn(?MODULE,loop,[]),
    register(Name,Pid).

loop() ->
    receive
        % an internal call won't reload the code. The process keeps
        % executing the current version 
        version -> io:format("version 1~n"), loop();
        % an external call will reload the code. The process will use
        % the new version if any available in the VM. Notes that it
        % will not search in the path if a new version exists!
        update -> io:format("update code~n"), ?MODULE:loop();
        stop -> io:format("bye...~n")
    end.

      

when i change the print line when i change the version. and now version control is in action:

Erlang/OTP 18 [erts-7.0] [64-bit] [smp:4:4] [async-threads:10]

Eshell V7.0  (abort with ^G)
1> c(tver).
{ok,tver}
2> tver:start(v1).
true
3> v1 ! version.
version 1
version
4> whereis(v1).
<0.39.0>

      

change version to version 2. The c / 1 command compiles the code and loads it into the VM

5> c(tver).        
{ok,tver}

      

Checks that process v1 is still running version 1

6> v1 ! version.   
version 1
version
7> whereis(v1).    
<0.39.0>

      

But the newly created process is running version 2.

8> tver:start(v2). 
true
9> v1 ! version.  
version 1
version
10> v2 ! version.
version 2
version

      

an external call forces the use of the new version. In real life, you have to do some checks, for example, what is the old version, what is new, is it possible to update, is there any data to adapt ...



11> v1 ! update. 
update code
update
12> v1 ! version.
version 2
version
13> v2 ! version.
version 2
version
14> whereis(v1).   
<0.39.0>
15> whereis(v2). 
<0.50.0>

      

make version 3, compile and reload

16> % modif version.
16> c(tver).        
{ok,tver}
17> whereis(v1).    
<0.39.0>
18> whereis(v2).    
<0.50.0>
19> v1 ! version.   
version 2
version
20> v1 ! version.   
version 2
version
21> v2 ! version.   
version 2
version
22> tver:start(v3). 
true
23> v2 ! version.  
version
version 2
24> v3 ! version.
version 3
version

      

in the thin version, pocesses 1 and 2 are running version 2 and process 3 is running version 3.

Now change version 4 without updating v1 and v2 processes, compile and download the code.

25> c(tver).                                                  
{ok,tver}
26> whereis(v1).                                              
undefined
27> whereis(v2).                                              
undefined

      

Version n-2 no longer exists! Processes V1 and V2 died even though they were just waiting in the receive block. In a real OTP system, their supervisor restarted them in accordance with his restart strategy.

28> tver:start(v4).                                           
true
29> whereis(v3).                            
<0.69.0>
30> whereis(v4).
<0.80.0>

      

V3 and V4 processes run versions 3 and 4 respectively

31> v3 ! version.                                             
version
version 3
32> v4 ! version.
version 4
version
33>

      

+5


source


Yes, you can only use two versions of one module at a time. But you can write your code in a way that switches the code to a new one while servicing one connection.



+1


source







All Articles