ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Erlang Hot Code Swapping for GenServer Behavior

Erlang

Goal -- WPM

Ready
Exercise Algorithm Area
1-module(hot_config_v1).
2
3-export([start_link/1, get_config/1, update_config/2]).
4
5-behavior(gen_server).
6
7%% API
8start_link(InitialConfig) ->
9gen_server:start_link({local, ?MODULE}, ?MODULE, [InitialConfig], []).
10
11get_config(Pid) ->
12gen_server:call(Pid, get_config).
13
14update_config(Pid, NewConfig) ->
15gen_server:cast(Pid, {update_config, NewConfig}).
16
17%% GenServer Callbacks
18init(InitialConfig) ->
19io:format("~p: Initializing with config: ~p~n", [self(), InitialConfig]),
20{ok, InitialConfig}.
21
22handle_call(get_config, _From, Config) ->
23{reply, Config, Config}.
24
25handle_cast({update_config, NewConfig}, _CurrentConfig) ->
26io:format("~p: Updating config from ~p to ~p~n", [self(), _CurrentConfig, NewConfig]),
27{noreply, NewConfig}.
28
29handle_info(_Info, State) ->
30{noreply, State}.
31
32terminate(_Reason, _State) ->
33ok.
34
35code_change(OldVsn, State, Extra) ->
36io:format("~p: Code changed from ~p to ~p~n", [self(), OldVsn, ?MODULE]),
37%% In v1, we don't have specific old versions to handle.
38%% If we had multiple versions, we'd check OldVsn and transform State.
39{ok, State}.
Algorithm description viewbox

Erlang Hot Code Swapping for GenServer Behavior

Algorithm description:

This Erlang code demonstrates hot code swapping for a GenServer. It defines an initial version (`hot_config_v1`) of a configuration manager. This GenServer stores a configuration value and allows it to be retrieved and updated. The `code_change/3` callback is crucial for handling state transitions during code updates. By updating the code file and then using Erlang's `code:purge` and `code:load_file` functions, the running GenServer can be updated without interrupting its service, a key feature for high-availability systems.

Algorithm explanation:

The `hot_config_v1` module implements a GenServer that holds a configuration value. The `init/1` callback initializes the state with the provided configuration. `handle_call/3` for `get_config` returns the current configuration. `handle_cast/2` for `update_config` updates the internal state. The `code_change/3` callback is designed to handle transitions between different versions of the module. In this v1, it simply returns `{ok, State}`, indicating no transformation is needed for the current state when upgrading from a hypothetical older version or when the module is loaded for the first time. To perform a hot swap, one would save this code as `hot_config_v1.erl`, start an instance, then save a new version (e.g., `hot_config_v2.erl`) with potentially different logic or an updated `code_change` function. After saving the new version, `code:purge(hot_config_v1)` and `code:load_file(hot_config_v2)` would be executed in the Erlang shell. The running GenServer would then use the new code. Time complexity for `get_config` and `update_config` is O(1). Space complexity is O(C) where C is the size of the configuration data.

Pseudocode:

GenServer (Version 1):
  State: Configuration value.
  init(InitialConfig): Set state to InitialConfig, log initialization.
  handle_call('get_config'): Reply with current state.
  handle_cast('update_config', NewConfig): Update state to NewConfig, log update.
  code_change(OldVsn, State, Extra): Return {ok, State} (no transformation needed for v1).

Hot Swap Procedure:
  1. Start an instance of the GenServer (v1).
  2. Save the updated code as a new version (e.g., v2).
  3. In the Erlang shell:
     a. Purge the old module: `code:purge(module_v1)`.
     b. Load the new module: `code:load_file(module_v2)`.
  4. The running GenServer instance will now use the new code.