Skip to content

Commit

Permalink
Merge pull request #228 from esl/add_node_to_cluster_command
Browse files Browse the repository at this point in the history
Add node to cluster command
  • Loading branch information
michalwski committed Nov 18, 2014
2 parents 7fc1fe3 + 3aead7c commit 54030ad
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 18 deletions.
3 changes: 2 additions & 1 deletion apps/ejabberd/src/ejabberd_auth_anonymous.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
remove_user/3,
store_type/1,
plain_password_required/0,
get_vh_registered_users/2,
get_vh_registered_users/2,
get_vh_registered_users_number/1,
get_vh_registered_users_number/2,
get_password_s/2 % not impl
Expand All @@ -73,6 +73,7 @@ start(Host) ->
mnesia:create_table(anonymous, [{ram_copies, [node()]},
{type, bag},
{attributes, record_info(fields, anonymous)}]),
mnesia:add_table_copy(anonymous, node(), ram_copies),
%% The hooks are needed to add / remove users from the anonymous tables
ejabberd_hooks:add(sm_register_connection_hook, Host,
?MODULE, register_connection, 100),
Expand Down
2 changes: 2 additions & 0 deletions apps/ejabberd/src/ejabberd_auth_internal.erl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ start(Host) ->
mnesia:create_table(reg_users_counter,
[{ram_copies, [node()]},
{attributes, record_info(fields, reg_users_counter)}]),
mnesia:add_table_copy(passwd, node(), disc_copies),
mnesia:add_table_copy(reg_users_counter, node(), ram_copies),
update_reg_users_counter_table(Host),
ok.

Expand Down
1 change: 1 addition & 0 deletions apps/ejabberd/src/mod_mam_mnesia_dirty_prefs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ start(Host, Opts) ->
mnesia:create_table(mam_prefs,
[{disc_copies, [node()]},
{attributes, record_info(fields, mam_prefs)}]),
mnesia:add_table_copy(mam_prefs, node(), disc_copies),
case gen_mod:get_module_opt(Host, ?MODULE, pm, false) of
true ->
start_pm(Host, Opts);
Expand Down
1 change: 1 addition & 0 deletions apps/ejabberd/src/mod_mam_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ start_host_mapping(Host, ServerHost) ->
[{ram_copies, [node()]},
{type, set},
{attributes, record_info(fields, mam_host)}]),
mnesia:add_table_copy(mam_host, node(), ram_copies),
mnesia:dirty_write(#mam_host{host = Host, server_host = ServerHost}).


Expand Down
2 changes: 2 additions & 0 deletions apps/ejabberd/src/mod_muc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ init([Host, Opts]) ->
[{ram_copies, [node()]},
{attributes, record_info(fields, muc_online_room)}]),
mnesia:add_table_copy(muc_online_room, node(), ram_copies),
mnesia:add_table_copy(muc_room, node(), disc_copies),
mnesia:add_table_copy(muc_registered, node(), disc_copies),
catch ets:new(muc_online_users, [bag, named_table, public, {keypos, 2}]),
MyHost = gen_mod:get_opt_host(Host, Opts, <<"conference.@HOST@">>),
update_tables(MyHost),
Expand Down
1 change: 1 addition & 0 deletions apps/ejabberd/src/mod_offline_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ init(_Host, _Opts) ->
[{disc_only_copies, [node()]},
{type, bag},
{attributes, record_info(fields, offline_msg)}]),
mnesia:add_table_copy(offline_msg, node(), disc_only_copies),
ok.

pop_messages(LUser, LServer) ->
Expand Down
1 change: 1 addition & 0 deletions apps/ejabberd/src/mod_privacy_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
init(_Host, _Opts) ->
mnesia:create_table(privacy, [{disc_copies, [node()]},
{attributes, record_info(fields, privacy)}]),
mnesia:add_table_copy(privacy, node(), disc_copies),
ok.

get_default_list(LUser, LServer) ->
Expand Down
4 changes: 3 additions & 1 deletion apps/ejabberd/src/mod_private_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ init(_Host, _Opts) ->
mnesia:create_table(private_storage,
[{disc_only_copies, [node()]},
{attributes, record_info(fields, private_storage)}]),

mnesia:add_table_copy(private_storage, node(), disc_only_copies),
ok.

multi_set_data(LUser, LServer, NS2XML) ->
Expand All @@ -57,7 +59,7 @@ multi_set_data_t(LUser, LServer, NS2XML) ->

set_data_t(LUser, LServer, NS, XML) ->
mnesia:write(#private_storage{usns = {LUser, LServer, NS}, xml = XML}).

multi_get_data(LUser, LServer, NS2Def) ->
[get_data(LUser, LServer, NS, Default) || {NS, Default} <- NS2Def].

Expand Down
4 changes: 4 additions & 0 deletions apps/ejabberd/src/mod_roster.erl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ start(Host, Opts) ->
update_table(),
mnesia:add_table_index(roster, us),
mnesia:add_table_index(roster_version, us),

mnesia:add_table_copy(roster, node(), disc_copies),
mnesia:add_table_copy(roster_version, node(), disc_copies),

ejabberd_hooks:add(roster_get, Host,
?MODULE, get_user_roster, 50),
ejabberd_hooks:add(roster_in_subscription, Host,
Expand Down
31 changes: 18 additions & 13 deletions apps/ejabberd/src/mod_vcard_mnesia.erl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ search_fields(_VHost) ->
prepare_db() ->
create_tables(),
update_tables(),
set_indexes().
set_indexes(),
add_table_copies().

create_tables() ->
mnesia:create_table(vcard, [{disc_only_copies, [node()]},
Expand All @@ -115,6 +116,10 @@ update_tables() ->
update_vcard_table(),
update_vcard_search_table().

add_table_copies() ->
mnesia:add_table_copy(vcard, node(), disc_only_copies),
mnesia:add_table_copy(vcard_search, node(), disc_copies).

set_indexes() ->
mnesia:add_table_index(vcard_search, luser),
mnesia:add_table_index(vcard_search, lfn),
Expand Down Expand Up @@ -203,18 +208,18 @@ update_vcard_search_table() ->
mnesia:write_lock_table(mod_vcard_tmp_table),
mnesia:foldl(
fun({vcard_search,
User, LUser,
FN, LFN,
Family, LFamily,
Given, LGiven,
Middle, LMiddle,
Nickname, LNickname,
BDay, LBDay,
CTRY, LCTRY,
Locality, LLocality,
EMail, LEMail,
OrgName, LOrgName,
OrgUnit, LOrgUnit
User, LUser,
FN, LFN,
Family, LFamily,
Given, LGiven,
Middle, LMiddle,
Nickname, LNickname,
BDay, LBDay,
CTRY, LCTRY,
Locality, LLocality,
EMail, LEMail,
OrgName, LOrgName,
OrgUnit, LOrgUnit
}, _) ->
mnesia:dirty_write(
mod_vcard_tmp_table,
Expand Down
38 changes: 37 additions & 1 deletion rel/files/mongooseim
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,43 @@ case "$1" in
sleep 1
done
;;
add_to_cluster)
NAME=$(echo $NAME_ARG | awk '{print $1}')
NODE=$(echo $NAME_ARG | awk '{print $2}')
NAME_TYPE=$(echo $NAME | sed 's/^\-//')

TARGETNODETOOL="$ERTS_PATH/escript $ERTS_PATH/nodetool $NAME $2 $COOKIE_ARG"
ADD_TO_CLUSTER_CMD="$ERTS_PATH/escript $ERTS_PATH/nodetool -exact_$NAME_TYPE $NODE $COOKIE_ARG add_to_cluster $2"

RES=`$NODETOOL ping`
if [ "$RES" = "pong" ]; then
echo "Error: Node is already running! Stop the node and delete Mnesia database directory ($MNESIA_DIR)."
exit 1
fi

MNESIA_DIR="$RUNNER_BASE_DIR/Mnesia.$NODE"
if [ -d "$MNESIA_DIR" ]; then
echo "Error: $MNESIA_DIR already exists. Please remove whole directory before continuing."
exit 1
fi
RES2=`$TARGETNODETOOL ping`
if [ "$RES2" = "pong" ]; then
RES3=`$ADD_TO_CLUSTER_CMD`
if [ "$RES3" = "ok" ]; then
echo "Node added to cluster. Run mongooseimctl live or mongooseimclt start."
else
echo "Adding Node to cluster failed: $RES3"
exit 1
fi
else
echo "Error: Node $2 is not reachable."
exit 1
fi
;;
remove_from_cluster)
## See if the VM is alive
$NODETOOL remove_from_cluster $2
;;
restart)
## Restart the VM without exiting the process
$NODETOOL restart
Expand Down Expand Up @@ -197,7 +233,7 @@ case "$1" in
fi
;;
*)
echo "Usage: $SCRIPT {start|stop|restart|reboot|ping|live|console|console_clean|attach|debug|version}"
echo "Usage: $SCRIPT {start|stop|restart|reboot|ping|live|console|console_clean|attach|debug|version|add_to_cluster|remove_from_cluster}"
exit 1
;;
esac
Expand Down
17 changes: 16 additions & 1 deletion rel/files/mongooseimctl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ fi
NAME_TYPE="${NODENAME_ARG% *}"
NODENAME="${NODENAME_ARG#* }"

add_to_cluster()
{
$RUNNER_SCRIPT_DIR/mongooseim $ARGS
}

remove_from_cluster()
{
$RUNNER_SCRIPT_DIR/mongooseim $ARGS
}

start ()
{
$RUNNER_SCRIPT_DIR/mongooseim start
Expand All @@ -60,6 +70,9 @@ help ()
echo " start Start a MongooseIM node in server mode"
echo " debug Attach an interactive Erlang shell to a running MongooseIM node"
echo " live Start MongooseIM node in live (interactive) mode"
echo "MongooseIM cluster management commads:"
echo " add_to_cluster other_node_name Add current node to cluster"
echo " remove_from_cluster dead_node_name Remove other node from cluster"
echo ""
}

Expand Down Expand Up @@ -191,8 +204,10 @@ wait_for_status()
return $status
}

case $ARGS in
case "$ARGS" in
' start') start;;
\ add_to_cluster*) add_to_cluster;;
\ remove_from_cluster*) remove_from_cluster;;
' debug') debug;;
' live') live;;
' started') wait_for_status 0 30 2;; # wait 30x2s before timeout
Expand Down
21 changes: 21 additions & 0 deletions rel/files/nodetool
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ main(Args) ->
{vsn, Version} = lists:keyfind(vsn, 1, Env),
io:format("~s~n", [Version]),
halt(0);
["add_to_cluster", ExtraNode] ->
ok = mnesia:start(),
NodeName = list_to_atom(ExtraNode),
{ok, _} = mnesia:change_config(extra_db_nodes, [NodeName]),
{atomic, ok} = mnesia:change_table_copy_type(schema, node(), disc_copies),
WhereToCommitTables = [{T, mnesia:table_info(T, where_to_commit)} || T <- mnesia:system_info(tables)],
[mnesia:add_table_copy(T, node(), Type) || {T, [{_, Type}]} <- WhereToCommitTables],
mnesia:stop(),
io:format("ok~n",[]),
halt(0);
_ ->
ok
end,
Expand All @@ -42,6 +52,8 @@ main(Args) ->
io:format("~p\n", [rpc:call(TargetNode, init, restart, [], 60000)]);
["reboot"] ->
io:format("~p\n", [rpc:call(TargetNode, init, reboot, [], 60000)]);
["remove_from_cluster", DeadNode] ->
io:format("~p\n", [rpc:call(TargetNode, mnesia, del_table_copy, [schema, list_to_atom(DeadNode)] )]);
["rpc", Module, Function | RpcArgs] ->
case rpc:call(TargetNode, list_to_atom(Module), list_to_atom(Function),
[RpcArgs], 60000) of
Expand Down Expand Up @@ -81,6 +93,15 @@ process_args(["-sname", TargetName | Rest], Acc, _) ->
ThisNode = append_node_suffix(TargetName, "_maint_"),
{ok, _} = net_kernel:start([ThisNode, shortnames]),
process_args(Rest, Acc, nodename(TargetName));
process_args(["-exact_name", TargetName | Rest], Acc, _) ->
ThisNode = list_to_atom(TargetName),
{ok, _} = net_kernel:start([ThisNode, longnames]),
process_args(Rest, Acc, nodename(TargetName));
process_args(["-exact_sname", TargetName | Rest], Acc, _) ->
ThisNode = list_to_atom(TargetName),
{ok, _} = net_kernel:start([ThisNode, shortnames]),
process_args(Rest, Acc, nodename(TargetName));

process_args([Arg | Rest], Acc, Opts) ->
process_args(Rest, [Arg | Acc], Opts).

Expand Down
2 changes: 1 addition & 1 deletion rel/reltool_vars/node2_vars.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{hosts, "[ \"localhost2\", \"michał\"]"}.
{hosts, "[ \"localhost2\", \"michał\", \"localhost\"]"}.
{outgoing_s2s_port, 5269}.
{odbc_server, ""}.
{s2s_addr, "{ {s2s_addr, \"localhost2\"}, {127,0,0,1} }."}.
Expand Down

0 comments on commit 54030ad

Please sign in to comment.