Erlang (programming Language) - Hot Code Loading and Modules

Hot Code Loading and Modules

Erlang supports language-level Dynamic Software Updating. To implement this, code is loaded and managed as "module" units; the module is a compilation unit. The system can keep two versions of a module in memory at the same time, and processes can concurrently run code from each. The versions are referred to as the "new" and the "old" version. A process will not move into the new version until it makes an external call to its module.

An example of the mechanism of hot code loading:

%% A process whose only job is to keep a counter. %% First version -module(counter). -export. start -> loop(0). loop(Sum) -> receive {increment, Count} -> loop(Sum+Count); {counter, Pid} -> Pid ! {counter, Sum}, loop(Sum); code_switch -> ?MODULE:codeswitch(Sum) % Force the use of 'codeswitch/1' from the latest MODULE version end. codeswitch(Sum) -> loop(Sum).

For the second version, we add the possibility to reset the count to zero.

%% Second version -module(counter). -export. start -> loop(0). loop(Sum) -> receive {increment, Count} -> loop(Sum+Count); reset -> loop(0); {counter, Pid} -> Pid ! {counter, Sum}, loop(Sum); code_switch -> ?MODULE:codeswitch(Sum) end. codeswitch(Sum) -> loop(Sum).

Only when receiving a message consisting of the atom 'code_switch' will the loop execute an external call to codeswitch/1 (?MODULE is a preprocessor macro for the current module). If there is a new version of the "counter" module in memory, then its codeswitch/1 function will be called. The practice of having a specific entry-point into a new version allows the programmer to transform state to what is required in the newer version. In our example we keep the state as an integer.

In practice, systems are built up using design principles from the Open Telecom Platform which leads to more code upgradable designs. Successful hot code loading is a tricky subject; code needs to be written to make use of Erlang's facilities.

Read more about this topic:  Erlang (programming Language)

Famous quotes containing the words hot, code and/or loading:

    One that loves a cup of hot wine with not a drop of allaying
    Tiber in’t.
    William Shakespeare (1564–1616)

    Many people will say to working mothers, in effect, “I don’t think you can have it all.” The phrase for “have it all” is code for “have your cake and eat it too.” What these people really mean is that achievement in the workplace has always come at a price—usually a significant personal price; conversely, women who stayed home with their children were seen as having sacrificed a great deal of their own ambition for their families.
    Anne C. Weisberg (20th century)

    Nitrates and phosphates for ammunition. The seeds of war. They’re loading a full cargo of death. And when that ship takes it home, the world will die a little more.
    Earl Felton, and Richard Fleischer. Captain Nemo (James Mason)