%%%-------------------------------------------------------------------
|
%%% @author pansen
|
%%% @copyright (C) 2018, <COMPANY>
|
%%% @doc
|
%%%
|
%%% @end
|
%%% Created : 02. 八月 2018 11:10
|
%%%-------------------------------------------------------------------
|
-author("pansen").
|
|
-include("DbTool.hrl").
|
|
%%%停止节点
|
%%%不安全的,可能关闭其他节点
|
stopNode(NodeName) ->
|
case syncTool:pingOnce(NodeName) of
|
true ->
|
Rtn = rpc:call(NodeName, net_kernel, stop, []);
|
false ->
|
Rtn = error
|
end,
|
io:format("Rtn ~p ,~n", [Rtn]),
|
case Rtn of
|
{badrpc, nodedown} ->
|
Ret = true;
|
error ->
|
Ret = false
|
end,
|
io:format("Ret ~p ,~n", [Ret]),
|
Ret.
|
|
|
initLocalFathNodeDB(Node) ->
|
createDb([node() | nodes()]),
|
TableList = mnesia:system_info(tables),
|
io:format("TableList ~p ,~n", [TableList]),
|
ok.
|
|
%%%初始化本地数据库节点
|
%%% 新节点id 集群节点
|
initLocalSubNodeDB(NewNode, FatherNode) ->
|
io:format("New Node = ~p~n", [NewNode]),
|
RunningNodeList = rpc:call(FatherNode, mnesia, system_info, [running_db_nodes]),
|
io:format("-----------RunningNodeList ---------~p~n", [RunningNodeList]),
|
ListOfTables = rpc:call(FatherNode, mnesia, system_info, [tables]),
|
io:format("-----------ListOfTables ---------~p~n", [ListOfTables]),
|
io:format("-----------Adding Extra Node---------~n"),
|
addExtraNode(RunningNodeList, NewNode),
|
io:format("-----------Chang schema -> disc_copies---------~n"),
|
Rtn = mnesia:change_table_copy_type(schema, NewNode, disc_copies),
|
io:format("Rtn=~p~n", [Rtn]),
|
io:format("-----------Reboot Remote Node Mnesia---------~n"),
|
mnesia:stop(),
|
timer:sleep(1000),
|
mnesiaDbStart('50000'),
|
io:format("-----------Adding Table List---------~n"),
|
addTableList(ListOfTables, NewNode),
|
io:format("-----------Over All---------~n").
|
|
%%%远程调用添加节点
|
%%%
|
addNode(NewNode) ->
|
io:format("New Node = ~p~n", [NewNode]),
|
RunningNodeList = mnesia:system_info(running_db_nodes),
|
io:format("-----------RunningNodeList ---------~p~n", [RunningNodeList]),
|
ListOfTables = mnesia:system_info(tables),
|
io:format("-----------ListOfTables ---------~p~n", [ListOfTables]),
|
io:format("-----------Adding Extra Node---------~n"),
|
addExtraNode(RunningNodeList, NewNode),
|
io:format("-----------Chang schema -> disc_copies---------~n"),
|
Rtn = mnesia:change_table_copy_type(schema, NewNode, disc_copies),
|
io:format("Rtn=~p~n", [Rtn]),
|
io:format("-----------Reboot Remote Node Mnesia---------~n"),
|
rpc:call(NewNode, mnesia, stop, []),
|
timer:sleep(1000),
|
rpc:call(NewNode, mnesia, start, []),
|
timer:sleep(1000),
|
%io:format("-----------Adding Table List---------~n"),
|
addTableList(ListOfTables, NewNode),
|
io:format("-----------Over All---------~n").
|
|
%%%远程调用已知节点change_config
|
%%%使新节点点加入集群
|
addExtraNode([], _NewNode) ->
|
null;
|
addExtraNode(_RunningNodeList = [Node | T], NewNode) ->
|
io:format("addExtraNode Node = ~p, connection=~p~n", [Node, net_adm:ping(Node)]),
|
Rtn = rpc:call(Node, mnesia, change_config, [extra_db_nodes, [NewNode]]),
|
if
|
Rtn == ok ->
|
io:format("call ~p change_config ret is ~p~n", [Node, Rtn]);
|
true ->
|
addExtraNode(T, NewNode)
|
end.
|
|
%%% 检查表的存储类型是否是同步表
|
%%% 同步表返回sync
|
checkTableType(Table) ->
|
StrTableName = atom_to_list(Table),
|
Pos = string:str(StrTableName, "lt_"),
|
if
|
Pos == 1 ->
|
Ret = lt;
|
true ->
|
Ret = sync
|
end,
|
Ret.
|
|
%%%复制集群中的数据表保存到硬盘
|
addTableList([], _NewNode) ->
|
null;
|
addTableList(_TableList = [Table | T], NewNode) ->
|
LTRet = checkTableType(Table),
|
if
|
LTRet == lt ->
|
Rtn = "this is local table not sync";
|
true ->
|
Rtn = mnesia:add_table_copy(Table, NewNode, disc_copies)
|
end,
|
io:format("Table = ~p, Rtn = ~p~n", [Table, Rtn]),
|
addTableList(T, NewNode).
|
|
%%% c写函数删除文件目录
|
%%deleteNode(NodeName) ->
|
%% disconnectNode(mnesia:system_info(running_db_nodes), NodeName),
|
%% %%delete data??
|
%% net_kernel:stop(),
|
%% ok.
|
|
disconnectNode([], _NodeName) ->
|
null;
|
disconnectNode(_RunningNodeList = [Node | T], NodeName) ->
|
mnesia:stop(),
|
Rtn = rpc:call(Node, net_kernel, disconnect, [NodeName]),
|
Rtn2 = rpc:call(Node, mnesia, del_table_copy, [schema, NodeName]),
|
io:format("call ~p change_config ret is ~p ret2 ~p ~n", [Node, Rtn, Rtn2]),
|
disconnectNode(T, NodeName),
|
ok.
|
|
rpcDeleteNode(NodeName) ->
|
%% mnesia:stop(),
|
device_info:del_DevInfo(NodeName),
|
rpc:call(NodeName, mnesia, stop, []),
|
Rtn2 = mnesia:del_table_copy(schema, NodeName),
|
io:format("call ~p deleteNode ret2 ~p ~n", [NodeName, Rtn2]),
|
Rtn2.
|
|
|
addFea2TableName(TableName) ->
|
PerTable = string:concat(atom_to_list(TableName), "_fea"),
|
list_to_atom(PerTable).
|