Skip to content

Commit

Permalink
mgt: simplify sync
Browse files Browse the repository at this point in the history
  • Loading branch information
verbit committed Nov 10, 2024
1 parent 0bbdf94 commit 084dd6e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 67 deletions.
81 changes: 32 additions & 49 deletions apps/virtuerl/src/virtuerl_host.erl
Original file line number Diff line number Diff line change
Expand Up @@ -55,76 +55,59 @@ generate_unique_tap_name(Prefix, TapNames) ->

handle_continue(sync_domains, #state{server_id = ServerId, vm_proc_mod = VmProcMod, idmap = IdMap, dom_tap_map = DomTapMap, prefix = IfPrefix, controller = ControllerPid} = State) ->
Domains0 = virtuerl_mgt:domains_list(ControllerPid, ServerId),
io:format("DOMAINS0: ~p~n", [Domains0]),
% TODO: Tap name generation should be part of the virtuerl_net module since
% this is a detail we shouldn't care about here
{_, Domains1} = lists:foldl(fun(Dom, {IfNames, Doms}) ->
#{id := DomId} = Dom,
case DomTapMap of
#{DomId := TapName} -> {[TapName | IfNames], [maps:put(tap_name, TapName, Dom) | Doms]};
_ ->
TapName = generate_unique_tap_name(IfPrefix, IfNames),
{[TapName | IfNames], [maps:put(tap_name, TapName, Dom) | Doms]}
end
TapName = case DomTapMap of
#{DomId := TapName0} -> TapName0;
_ -> generate_unique_tap_name(IfPrefix, IfNames)
end,
{[TapName | IfNames], [maps:put(tap_name, TapName, Dom) | Doms]}
end,
{[], []},
Domains0),

io:format("DOMAINS1: ~p~n", [Domains1]),
Domains = [ {Id, Dom} || #{id := Id} = Dom <- Domains1 ],
TargetDomains = maps:from_list(
[ {Id, case maps:get(host, Domain, undefined) of ServerId -> node(); Else -> Else end}
|| {Id, Domain} <- Domains,
case Domain of
#{state := stopped} -> false;
_ -> true
end ]),

AllNodes = nodes([this]),
RunningDomains = maps:from_list(lists:flatten(
[ [ {Id, Node} || {Id, _, _, _} <- virtuerl_sup:which_children(ServerId, Node), is_binary(Id) ]
|| Node <- AllNodes ])),
ToDelete = maps:without(maps:keys(TargetDomains), RunningDomains),
ToAdd = maps:without(maps:keys(RunningDomains), TargetDomains),
[ virtuerl_sup:terminate_child(ServerId, Node, Id) || {Id, Node} <- maps:to_list(ToDelete) ],
[ virtuerl_sup:delete_child(ServerId, Node, Id) || {Id, Node} <- maps:to_list(ToDelete) ],
Domains = maps:from_list([ {Id, Dom} || #{id := Id} = Dom <- Domains1 ]),

TargetDomains = [ Id || {Id, Domain} <- maps:to_list(Domains),
case Domain of
#{state := stopped} -> false;
_ -> true
end ],
RunningDomains = [ Id || {Id, _, _, _} <- virtuerl_sup:which_children(ServerId), is_binary(Id) ],
ToDelete = RunningDomains -- TargetDomains,
ToAdd = TargetDomains -- RunningDomains,

[ virtuerl_sup:terminate_child(ServerId, Id) || Id <- ToDelete ],
[ virtuerl_sup:delete_child(ServerId, Id) || Id <- ToDelete ],

% cleanup deleted domains
case file:list_dir(filename:join([virtuerl_host:home_path(), "domains"])) of
{ok, Filenames} ->
DirsToDel = sets:subtract(sets:from_list([ iolist_to_binary(FName) || FName <- Filenames ]),
sets:from_list(maps:keys(maps:from_list(Domains)))),
[ file:del_dir_r(filename:join([virtuerl_host:home_path(), "domains", Dir])) || Dir <- sets:to_list(DirsToDel) ];
DirsToDel = [ iolist_to_binary(FName) || FName <- Filenames ] -- maps:keys(Domains),
[ file:del_dir_r(filename:join([virtuerl_host:home_path(), "domains", Dir])) || Dir <- DirsToDel ];
_ -> ok
end,

% lists:foreach(fun () -> ok end, List)
DomsByNode0 = maps:groups_from_list(
fun({_Id, Dom}) -> case maps:get(host, Dom, undefined) of ServerId -> node(); Else -> Else end end,
fun({_Id, Dom}) -> Dom end,
Domains),
DomsByNode = maps:merge(#{node() => []}, DomsByNode0),
[ virtuerl_net:update_net(Node, ServerId, Doms) || {Node, Doms} <- maps:to_list(DomsByNode) ],
virtuerl_net:update_net(ServerId, Domains1),

io:format("DomsByNode: ~p~n", [DomsByNode]),
io:format("RUNNING: ~p~n", [RunningDomains]),
io:format("TOADD: ~p~n", [ToAdd]),
io:format("TODELETE: ~p~n", [ToDelete]),
[ virtuerl_mgt:notify(ControllerPid, {domain_stopped, DomId}) || DomId <- maps:keys(ToDelete) ],
[ virtuerl_mgt:notify(ControllerPid, {domain_stopped, DomId}) || DomId <- ToDelete ],

VmPids = [ {Id,
virtuerl_sup:start_child(ServerId,
Node,
{Id,
{VmProcMod, start_link, [maps:get(Id, maps:from_list(Domains))]},
transient,
infinity,
worker,
[]})} || {Id, Node} <- maps:to_list(ToAdd) ],
io:format("VMPIDS: ~p~n", [VmPids]),
#{
id => Id,
start => {VmProcMod, start_link, [maps:get(Id, Domains)]},
restart => transient,
shutdown => infinity
})} || Id <- ToAdd ],

[ virtuerl_mgt:notify(ControllerPid, {domain_started, DomId}) || {DomId, _} <- VmPids ],
VmPidToDomId = maps:from_list([ {VmPid, DomId} || {DomId, {ok, VmPid}} <- VmPids ]),
[ monitor(process, VmPid) || {_, {ok, VmPid}} <- VmPids ],

NewDomTapMap = maps:from_list([ {Id, TapName} || {Id, #{tap_name := TapName}} <- Domains ]),
NewDomTapMap = maps:map(fun(_Id, #{tap_name := TapName}) -> TapName end, Domains),
{noreply, State#state{idmap = maps:merge(IdMap, VmPidToDomId), dom_tap_map = NewDomTapMap}}.


Expand Down
5 changes: 3 additions & 2 deletions apps/virtuerl/src/virtuerl_mgt.erl
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ domains_list(Ref) ->
gen_server:call(?MODULE, domains_list).


domains_list(Ref, WorkerName) ->
gen_server:call(?MODULE, {domains_list, WorkerName}).
-spec domains_list(Pid :: pid(), WorkerName :: term()) -> [Domain :: #{}].
domains_list(Pid, WorkerName) ->
gen_server:call(Pid, {domains_list, WorkerName}).


domain_update(Conf) -> domain_update({default, ?MODULE}, Conf).
Expand Down
10 changes: 3 additions & 7 deletions apps/virtuerl/src/virtuerl_net.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

-behaviour(gen_server).

-export([start_link/2, update_net/2, update_net/3]).
-export([start_link/2, update_net/2]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([parse_cidr/1, format_cidr/1, parse_ip/1, format_ip/1, format_ip_bitstring/1, bridge_addr/1, bridge_addr/2, normalize_net/1]).

Expand All @@ -28,12 +28,8 @@ start_link(ServerId, Conf) ->
gen_server:start_link({via, virtuerl_reg, {ServerId, ?MODULE}}, ?MODULE, [Conf], []).


update_net(Node, Domains) ->
erpc:call(Node, gen_server, call, [{via, virtuerl_reg, {default, ?MODULE}}, {net_update, Domains}, infinity]).


update_net(Node, Ref, Domains) ->
erpc:call(Node, gen_server, call, [{via, virtuerl_reg, {Ref, ?MODULE}}, {net_update, Domains}, infinity]).
update_net(Ref, Domains) ->
gen_server:call({via, virtuerl_reg, {Ref, ?MODULE}}, {net_update, Domains}, infinity).


-define(NET_PROVIDER, get(net_provider_mod)).
Expand Down
22 changes: 13 additions & 9 deletions apps/virtuerl/src/virtuerl_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@

-behaviour(supervisor).

-export([start_link/2, start_child/3, which_children/2, delete_child/3, terminate_child/3]).
-export([start_link/2,
start_child/2,
which_children/1,
delete_child/2,
terminate_child/2]).
-export([init/1]).


start_child(ServerId, Node, ChildSpec) ->
erpc:call(Node, supervisor, start_child, [{via, virtuerl_reg, {ServerId, ?MODULE}}, ChildSpec]).
start_child(ServerId, ChildSpec) ->
supervisor:start_child({via, virtuerl_reg, {ServerId, ?MODULE}}, ChildSpec).


which_children(ServerId, Node) ->
erpc:call(Node, supervisor, which_children, [{via, virtuerl_reg, {ServerId, ?MODULE}}]).
which_children(ServerId) ->
supervisor:which_children({via, virtuerl_reg, {ServerId, ?MODULE}}).


terminate_child(ServerId, Node, DomId) ->
erpc:call(Node, supervisor, terminate_child, [{via, virtuerl_reg, {ServerId, ?MODULE}}, DomId]).
terminate_child(ServerId, DomId) ->
supervisor:terminate_child({via, virtuerl_reg, {ServerId, ?MODULE}}, DomId).


delete_child(ServerId, Node, DomId) ->
erpc:call(Node, supervisor, delete_child, [{via, virtuerl_reg, {ServerId, ?MODULE}}, DomId]).
delete_child(ServerId, DomId) ->
supervisor:delete_child({via, virtuerl_reg, {ServerId, ?MODULE}}, DomId).


start_link(ServerId, Conf) ->
Expand Down

0 comments on commit 084dd6e

Please sign in to comment.