a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
%%%-------------------------------------------------------------------
%%% @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).