syn_registry_SUITE.erl 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015-2019 Roberto Ostinelli <roberto@ostinelli.net> and Neato Robotics, Inc.
  7. %%
  8. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  9. %% of this software and associated documentation files (the "Software"), to deal
  10. %% in the Software without restriction, including without limitation the rights
  11. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. %% copies of the Software, and to permit persons to whom the Software is
  13. %% furnished to do so, subject to the following conditions:
  14. %%
  15. %% The above copyright notice and this permission notice shall be included in
  16. %% all copies or substantial portions of the Software.
  17. %%
  18. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. %% THE SOFTWARE.
  25. %% ==========================================================================================================
  26. -module(syn_registry_SUITE).
  27. %% callbacks
  28. -export([all/0]).
  29. -export([init_per_suite/1, end_per_suite/1]).
  30. -export([groups/0, init_per_group/2, end_per_group/2]).
  31. -export([init_per_testcase/2, end_per_testcase/2]).
  32. %% tests
  33. -export([
  34. single_node_register_and_monitor/1,
  35. single_node_register_and_unregister/1,
  36. single_node_registration_errors/1,
  37. single_node_registry_count/1,
  38. single_node_register_gen_server/1,
  39. single_node_callback_on_process_exit/1,
  40. single_node_ensure_callback_process_exit_is_called_if_process_killed/1,
  41. single_node_monitor_after_registry_crash/1
  42. ]).
  43. -export([
  44. two_nodes_register_monitor_and_unregister/1,
  45. two_nodes_registry_count/1,
  46. two_nodes_registration_race_condition_conflict_resolution_keep_local/1,
  47. two_nodes_registration_race_condition_conflict_resolution_keep_remote/1,
  48. two_nodes_registration_race_condition_conflict_resolution_when_process_died/1,
  49. two_nodes_registry_full_cluster_sync_on_boot_node_added_later/1,
  50. two_nodes_registry_full_cluster_sync_on_boot_syn_started_later/1
  51. ]).
  52. -export([
  53. three_nodes_partial_netsplit_consistency/1,
  54. three_nodes_full_netsplit_consistency/1,
  55. three_nodes_start_syn_before_connecting_cluster_with_conflict/1,
  56. three_nodes_start_syn_before_connecting_cluster_with_custom_conflict_resolution/1,
  57. three_nodes_registration_race_condition_custom_conflict_resolution/1,
  58. three_nodes_anti_entropy/1
  59. ]).
  60. %% support
  61. -export([
  62. start_syn_delayed_and_register_local_process/3,
  63. start_syn_delayed_with_custom_handler_register_local_process/4
  64. ]).
  65. %% include
  66. -include_lib("common_test/include/ct.hrl").
  67. -include_lib("../src/syn.hrl").
  68. %% ===================================================================
  69. %% Callbacks
  70. %% ===================================================================
  71. %% -------------------------------------------------------------------
  72. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  73. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  74. %% GroupName = atom()
  75. %% TestCase = atom()
  76. %% Reason = any()
  77. %% -------------------------------------------------------------------
  78. all() ->
  79. [
  80. {group, single_node_process_registration},
  81. {group, two_nodes_process_registration},
  82. {group, three_nodes_process_registration}
  83. ].
  84. %% -------------------------------------------------------------------
  85. %% Function: groups() -> [Group]
  86. %% Group = {GroupName,Properties,GroupsAndTestCases}
  87. %% GroupName = atom()
  88. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  89. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  90. %% TestCase = atom()
  91. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  92. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  93. %% repeat_until_any_ok | repeat_until_any_fail
  94. %% N = integer() | forever
  95. %% -------------------------------------------------------------------
  96. groups() ->
  97. [
  98. {single_node_process_registration, [shuffle], [
  99. single_node_register_and_monitor,
  100. single_node_register_and_unregister,
  101. single_node_registration_errors,
  102. single_node_registry_count,
  103. single_node_register_gen_server,
  104. single_node_callback_on_process_exit,
  105. single_node_ensure_callback_process_exit_is_called_if_process_killed,
  106. single_node_monitor_after_registry_crash
  107. ]},
  108. {two_nodes_process_registration, [shuffle], [
  109. two_nodes_register_monitor_and_unregister,
  110. two_nodes_registry_count,
  111. two_nodes_registration_race_condition_conflict_resolution_keep_local,
  112. two_nodes_registration_race_condition_conflict_resolution_keep_remote,
  113. two_nodes_registration_race_condition_conflict_resolution_when_process_died,
  114. two_nodes_registry_full_cluster_sync_on_boot_node_added_later,
  115. two_nodes_registry_full_cluster_sync_on_boot_syn_started_later
  116. ]},
  117. {three_nodes_process_registration, [shuffle], [
  118. three_nodes_partial_netsplit_consistency,
  119. three_nodes_full_netsplit_consistency,
  120. three_nodes_start_syn_before_connecting_cluster_with_conflict,
  121. three_nodes_start_syn_before_connecting_cluster_with_custom_conflict_resolution,
  122. three_nodes_registration_race_condition_custom_conflict_resolution,
  123. three_nodes_anti_entropy
  124. ]}
  125. ].
  126. %% -------------------------------------------------------------------
  127. %% Function: init_per_suite(Config0) ->
  128. %% Config1 | {skip,Reason} |
  129. %% {skip_and_save,Reason,Config1}
  130. %% Config0 = Config1 = [tuple()]
  131. %% Reason = any()
  132. %% -------------------------------------------------------------------
  133. init_per_suite(Config) ->
  134. Config.
  135. %% -------------------------------------------------------------------
  136. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  137. %% Config0 = Config1 = [tuple()]
  138. %% -------------------------------------------------------------------
  139. end_per_suite(_Config) ->
  140. ok.
  141. %% -------------------------------------------------------------------
  142. %% Function: init_per_group(GroupName, Config0) ->
  143. %% Config1 | {skip,Reason} |
  144. %% {skip_and_save,Reason,Config1}
  145. %% GroupName = atom()
  146. %% Config0 = Config1 = [tuple()]
  147. %% Reason = any()
  148. %% -------------------------------------------------------------------
  149. init_per_group(two_nodes_process_registration, Config) ->
  150. %% start slave
  151. {ok, SlaveNode} = syn_test_suite_helper:start_slave(syn_slave),
  152. %% config
  153. [{slave_node, SlaveNode} | Config];
  154. init_per_group(three_nodes_process_registration, Config) ->
  155. %% start slave
  156. {ok, SlaveNode1} = syn_test_suite_helper:start_slave(syn_slave_1),
  157. {ok, SlaveNode2} = syn_test_suite_helper:start_slave(syn_slave_2),
  158. %% config
  159. [{slave_node_1, SlaveNode1}, {slave_node_2, SlaveNode2} | Config];
  160. init_per_group(_GroupName, Config) ->
  161. Config.
  162. %% -------------------------------------------------------------------
  163. %% Function: end_per_group(GroupName, Config0) ->
  164. %% void() | {save_config,Config1}
  165. %% GroupName = atom()
  166. %% Config0 = Config1 = [tuple()]
  167. %% -------------------------------------------------------------------
  168. end_per_group(two_nodes_process_registration, Config) ->
  169. SlaveNode = proplists:get_value(slave_node, Config),
  170. syn_test_suite_helper:connect_node(SlaveNode),
  171. syn_test_suite_helper:clean_after_test(),
  172. syn_test_suite_helper:stop_slave(syn_slave),
  173. timer:sleep(1000);
  174. end_per_group(three_nodes_process_registration, Config) ->
  175. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  176. syn_test_suite_helper:connect_node(SlaveNode1),
  177. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  178. syn_test_suite_helper:connect_node(SlaveNode2),
  179. syn_test_suite_helper:clean_after_test(),
  180. syn_test_suite_helper:stop_slave(syn_slave_1),
  181. syn_test_suite_helper:stop_slave(syn_slave_2),
  182. timer:sleep(1000);
  183. end_per_group(_GroupName, _Config) ->
  184. syn_test_suite_helper:clean_after_test().
  185. %% -------------------------------------------------------------------
  186. %% Function: init_per_testcase(TestCase, Config0) ->
  187. %% Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  188. %% TestCase = atom()
  189. %% Config0 = Config1 = [tuple()]
  190. %% Reason = any()
  191. %% -------------------------------------------------------------------
  192. init_per_testcase(_TestCase, Config) ->
  193. Config.
  194. %% -------------------------------------------------------------------
  195. %% Function: end_per_testcase(TestCase, Config0) ->
  196. %% void() | {save_config,Config1} | {fail,Reason}
  197. %% TestCase = atom()
  198. %% Config0 = Config1 = [tuple()]
  199. %% Reason = any()
  200. %% -------------------------------------------------------------------
  201. end_per_testcase(_, _Config) ->
  202. syn_test_suite_helper:clean_after_test().
  203. %% ===================================================================
  204. %% Tests
  205. %% ===================================================================
  206. single_node_register_and_monitor(_Config) ->
  207. %% start
  208. ok = syn:start(),
  209. %% start processes
  210. Pid = syn_test_suite_helper:start_process(),
  211. PidWithMeta = syn_test_suite_helper:start_process(),
  212. %% retrieve
  213. undefined = syn:whereis(<<"my proc">>),
  214. %% register
  215. ok = syn:register(<<"my proc">>, Pid),
  216. ok = syn:register({"my proc 2"}, Pid),
  217. ok = syn:register(<<"my proc with meta">>, PidWithMeta, {meta, <<"meta">>}),
  218. %% retrieve
  219. Pid = syn:whereis(<<"my proc">>),
  220. Pid = syn:whereis({"my proc 2"}),
  221. {PidWithMeta, {meta, <<"meta">>}} = syn:whereis(<<"my proc with meta">>, with_meta),
  222. %% re-register
  223. ok = syn:register(<<"my proc with meta">>, PidWithMeta, {meta2, <<"meta2">>}),
  224. {PidWithMeta, {meta2, <<"meta2">>}} = syn:whereis(<<"my proc with meta">>, with_meta),
  225. %% kill process
  226. syn_test_suite_helper:kill_process(Pid),
  227. syn_test_suite_helper:kill_process(PidWithMeta),
  228. timer:sleep(100),
  229. %% retrieve
  230. undefined = syn:whereis(<<"my proc">>),
  231. undefined = syn:whereis({"my proc 2"}),
  232. undefined = syn:whereis(<<"my proc with meta">>).
  233. single_node_register_and_unregister(_Config) ->
  234. %% start
  235. ok = syn:start(),
  236. %% start process
  237. Pid = syn_test_suite_helper:start_process(),
  238. %% retrieve
  239. undefined = syn:whereis(<<"my proc">>),
  240. %% register
  241. ok = syn:register(<<"my proc">>, Pid),
  242. ok = syn:register(<<"my proc 2">>, Pid),
  243. %% retrieve
  244. Pid = syn:whereis(<<"my proc">>),
  245. Pid = syn:whereis(<<"my proc 2">>),
  246. %% unregister 1
  247. ok = syn:unregister(<<"my proc">>),
  248. %% retrieve
  249. undefined = syn:whereis(<<"my proc">>),
  250. Pid = syn:whereis(<<"my proc 2">>),
  251. %% unregister 2
  252. ok = syn:unregister(<<"my proc 2">>),
  253. {error, undefined} = syn:unregister(<<"my proc 2">>),
  254. %% retrieve
  255. undefined = syn:whereis(<<"my proc">>),
  256. undefined = syn:whereis(<<"my proc 2">>).
  257. single_node_registration_errors(_Config) ->
  258. %% start
  259. ok = syn:start(),
  260. %% start process
  261. Pid = syn_test_suite_helper:start_process(),
  262. Pid2 = syn_test_suite_helper:start_process(),
  263. %% register
  264. ok = syn:register(<<"my proc">>, Pid),
  265. {error, taken} = syn:register(<<"my proc">>, Pid2),
  266. %% kill processes
  267. syn_test_suite_helper:kill_process(Pid),
  268. syn_test_suite_helper:kill_process(Pid2),
  269. timer:sleep(100),
  270. %% retrieve
  271. undefined = syn:whereis(<<"my proc">>),
  272. %% try registering a dead pid
  273. {error, not_alive} = syn:register(<<"my proc">>, Pid).
  274. single_node_registry_count(_Config) ->
  275. %% start
  276. ok = syn:start(),
  277. %% start process
  278. Pid = syn_test_suite_helper:start_process(),
  279. Pid2 = syn_test_suite_helper:start_process(),
  280. PidUnregistered = syn_test_suite_helper:start_process(),
  281. %% register
  282. ok = syn:register(<<"my proc">>, Pid),
  283. ok = syn:register(<<"my proc 2">>, Pid2),
  284. %% count
  285. 2 = syn:registry_count(),
  286. 2 = syn:registry_count(node()),
  287. %% kill & unregister
  288. syn_test_suite_helper:kill_process(Pid),
  289. ok = syn:unregister(<<"my proc 2">>),
  290. syn_test_suite_helper:kill_process(PidUnregistered),
  291. timer:sleep(100),
  292. %% count
  293. 0 = syn:registry_count(),
  294. 0 = syn:registry_count(node()).
  295. single_node_register_gen_server(_Config) ->
  296. %% start
  297. ok = syn:start(),
  298. %% start gen server via syn
  299. {ok, Pid} = syn_test_gen_server:start_link(),
  300. %% retrieve
  301. Pid = syn:whereis(syn_test_gen_server),
  302. %% call
  303. pong = syn_test_gen_server:ping(),
  304. %% send via syn
  305. syn:send(syn_test_gen_server, {self(), send_ping}),
  306. receive
  307. send_pong -> ok
  308. after 1000 ->
  309. ok = did_not_receive_gen_server_pong
  310. end,
  311. %% stop server
  312. syn_test_gen_server:stop(),
  313. timer:sleep(200),
  314. %% retrieve
  315. undefined = syn:whereis(syn_test_gen_server),
  316. %% send via syn
  317. {badarg, {syn_test_gen_server, anything}} = (catch syn:send(syn_test_gen_server, anything)).
  318. single_node_callback_on_process_exit(_Config) ->
  319. %% use custom handler
  320. syn_test_suite_helper:use_custom_handler(),
  321. %% start
  322. ok = syn:start(),
  323. %% start process
  324. Pid = syn_test_suite_helper:start_process(),
  325. Pid2 = syn_test_suite_helper:start_process(),
  326. %% register
  327. TestPid = self(),
  328. ok = syn:register(<<"my proc">>, Pid, {pid, TestPid}),
  329. ok = syn:register(<<"my proc - alternate">>, Pid, {pid_alternate, TestPid}),
  330. ok = syn:register(<<"my proc 2">>, Pid2, {pid2, TestPid}),
  331. %% kill 1
  332. syn_test_suite_helper:kill_process(Pid),
  333. receive
  334. {received_event_on, pid} ->
  335. ok;
  336. {received_event_on, pid2} ->
  337. ok = callback_on_process_exit_was_received_by_pid2
  338. after 1000 ->
  339. ok = callback_on_process_exit_was_not_received_by_pid
  340. end,
  341. receive
  342. {received_event_on, pid_alternate} ->
  343. ok;
  344. {received_event_on, pid2} ->
  345. ok = callback_on_process_exit_was_received_by_pid2
  346. after 1000 ->
  347. ok = callback_on_process_exit_was_not_received_by_pid
  348. end,
  349. %% unregister & kill 2
  350. ok = syn:unregister(<<"my proc 2">>),
  351. syn_test_suite_helper:kill_process(Pid2),
  352. receive
  353. {received_event_on, pid2} ->
  354. ok = callback_on_process_exit_was_received_by_pid2
  355. after 1000 ->
  356. ok
  357. end.
  358. single_node_ensure_callback_process_exit_is_called_if_process_killed(_Config) ->
  359. Name = <<"my proc">>,
  360. %% use custom handler
  361. syn_test_suite_helper:use_custom_handler(),
  362. %% start
  363. ok = syn:start(),
  364. %% start process
  365. Pid = syn_test_suite_helper:start_process(),
  366. %% register
  367. TestPid = self(),
  368. ok = syn:register(Name, Pid, {some_meta, TestPid}),
  369. %% remove from table to simulate conflict resolution
  370. syn_registry:remove_from_local_table(Name),
  371. %% kill
  372. exit(Pid, {syn_resolve_kill, Name, {some_meta, TestPid}}),
  373. receive
  374. {received_event_on, some_meta} ->
  375. ok
  376. after 1000 ->
  377. ok = callback_on_process_exit_was_not_received_by_pid
  378. end.
  379. single_node_monitor_after_registry_crash(_Config) ->
  380. %% start
  381. ok = syn:start(),
  382. %% start processes
  383. Pid = syn_test_suite_helper:start_process(),
  384. %% register
  385. ok = syn:register(<<"my proc">>, Pid),
  386. %% kill registry
  387. exit(whereis(syn_registry), kill),
  388. timer:sleep(200),
  389. %% retrieve
  390. Pid = syn:whereis(<<"my proc">>),
  391. %% kill process
  392. syn_test_suite_helper:kill_process(Pid),
  393. timer:sleep(200),
  394. %% retrieve
  395. undefined = syn:whereis(<<"my proc 2">>).
  396. two_nodes_register_monitor_and_unregister(Config) ->
  397. %% get slave
  398. SlaveNode = proplists:get_value(slave_node, Config),
  399. %% start
  400. ok = syn:start(),
  401. ok = rpc:call(SlaveNode, syn, start, []),
  402. timer:sleep(100),
  403. %% start processes
  404. LocalPid = syn_test_suite_helper:start_process(),
  405. RemotePid = syn_test_suite_helper:start_process(SlaveNode),
  406. RemotePidRegRemote = syn_test_suite_helper:start_process(SlaveNode),
  407. %% retrieve
  408. undefined = syn:whereis(<<"local proc">>),
  409. undefined = syn:whereis(<<"remote proc">>),
  410. undefined = syn:whereis(<<"remote proc reg_remote">>),
  411. undefined = rpc:call(SlaveNode, syn, whereis, [<<"local proc">>]),
  412. undefined = rpc:call(SlaveNode, syn, whereis, [<<"remote proc">>]),
  413. undefined = rpc:call(SlaveNode, syn, whereis, [<<"remote proc reg_remote">>]),
  414. %% register
  415. ok = syn:register(<<"local proc">>, LocalPid),
  416. ok = syn:register(<<"remote proc">>, RemotePid),
  417. ok = rpc:call(SlaveNode, syn, register, [<<"remote proc reg_remote">>, RemotePidRegRemote]),
  418. timer:sleep(500),
  419. %% retrieve
  420. LocalPid = syn:whereis(<<"local proc">>),
  421. RemotePid = syn:whereis(<<"remote proc">>),
  422. RemotePidRegRemote = syn:whereis(<<"remote proc reg_remote">>),
  423. LocalPid = rpc:call(SlaveNode, syn, whereis, [<<"local proc">>]),
  424. RemotePid = rpc:call(SlaveNode, syn, whereis, [<<"remote proc">>]),
  425. RemotePidRegRemote = rpc:call(SlaveNode, syn, whereis, [<<"remote proc reg_remote">>]),
  426. %% kill & unregister processes
  427. syn_test_suite_helper:kill_process(LocalPid),
  428. ok = syn:unregister(<<"remote proc">>),
  429. syn_test_suite_helper:kill_process(RemotePidRegRemote),
  430. timer:sleep(100),
  431. %% retrieve
  432. undefined = syn:whereis(<<"local proc">>),
  433. undefined = syn:whereis(<<"remote proc">>),
  434. undefined = syn:whereis(<<"remote proc reg_remote">>),
  435. undefined = rpc:call(SlaveNode, syn, whereis, [<<"local proc">>]),
  436. undefined = rpc:call(SlaveNode, syn, whereis, [<<"remote proc">>]),
  437. undefined = rpc:call(SlaveNode, syn, whereis, [<<"remote proc reg_remote">>]).
  438. two_nodes_registry_count(Config) ->
  439. %% get slave
  440. SlaveNode = proplists:get_value(slave_node, Config),
  441. %% start
  442. ok = syn:start(),
  443. ok = rpc:call(SlaveNode, syn, start, []),
  444. timer:sleep(100),
  445. %% start processes
  446. LocalPid = syn_test_suite_helper:start_process(),
  447. RemotePid = syn_test_suite_helper:start_process(SlaveNode),
  448. RemotePidRegRemote = syn_test_suite_helper:start_process(SlaveNode),
  449. _PidUnregistered = syn_test_suite_helper:start_process(),
  450. %% register
  451. ok = syn:register(<<"local proc">>, LocalPid),
  452. ok = syn:register(<<"remote proc">>, RemotePid),
  453. ok = rpc:call(SlaveNode, syn, register, [<<"remote proc reg_remote">>, RemotePidRegRemote]),
  454. timer:sleep(500),
  455. %% count
  456. 3 = syn:registry_count(),
  457. 1 = syn:registry_count(node()),
  458. 2 = syn:registry_count(SlaveNode),
  459. %% kill & unregister processes
  460. syn_test_suite_helper:kill_process(LocalPid),
  461. ok = syn:unregister(<<"remote proc">>),
  462. syn_test_suite_helper:kill_process(RemotePidRegRemote),
  463. timer:sleep(100),
  464. %% count
  465. 0 = syn:registry_count(),
  466. 0 = syn:registry_count(node()),
  467. 0 = syn:registry_count(SlaveNode).
  468. two_nodes_registration_race_condition_conflict_resolution_keep_local(Config) ->
  469. ConflictingName = "COMMON",
  470. %% get slaves
  471. SlaveNode = proplists:get_value(slave_node, Config),
  472. %% start syn on nodes
  473. ok = syn:start(),
  474. ok = rpc:call(SlaveNode, syn, start, []),
  475. timer:sleep(1000),
  476. %% start processes
  477. Pid0 = syn_test_suite_helper:start_process(),
  478. Pid1 = syn_test_suite_helper:start_process(SlaveNode),
  479. %% inject into syn to simulate concurrent registration
  480. ok = syn_registry:add_to_local_table(ConflictingName, Pid0, node(), undefined),
  481. %% register on slave node to trigger conflict resolution on master node
  482. ok = rpc:call(SlaveNode, syn, register, [ConflictingName, Pid1, SlaveNode]),
  483. timer:sleep(1000),
  484. %% check metadata, resolution happens on master node
  485. Node = node(),
  486. {Pid0, Node} = syn:whereis(ConflictingName, with_meta),
  487. {Pid0, Node} = rpc:call(SlaveNode, syn, whereis, [ConflictingName, with_meta]),
  488. %% check that other processes are not alive because syn killed them
  489. true = is_process_alive(Pid0),
  490. false = rpc:call(SlaveNode, erlang, is_process_alive, [Pid1]).
  491. two_nodes_registration_race_condition_conflict_resolution_keep_remote(Config) ->
  492. ConflictingName = "COMMON",
  493. %% get slaves
  494. SlaveNode = proplists:get_value(slave_node, Config),
  495. %% use customer handler
  496. syn_test_suite_helper:use_custom_handler(),
  497. rpc:call(SlaveNode, syn_test_suite_helper, use_custom_handler, []),
  498. %% start syn on nodes
  499. ok = syn:start(),
  500. ok = rpc:call(SlaveNode, syn, start, []),
  501. timer:sleep(1000),
  502. %% start processes
  503. Pid0 = syn_test_suite_helper:start_process(),
  504. Pid1 = syn_test_suite_helper:start_process(SlaveNode),
  505. %% inject into syn to simulate concurrent registration
  506. ok = syn_registry:add_to_local_table(ConflictingName, Pid0, node(), undefined),
  507. %% register on slave node to trigger conflict resolution on master node
  508. ok = rpc:call(SlaveNode, syn, register, [ConflictingName, Pid1, keep_this_one]),
  509. timer:sleep(1000),
  510. %% check metadata, resolution happens on master node
  511. {Pid1, keep_this_one} = syn:whereis(ConflictingName, with_meta),
  512. {Pid1, keep_this_one} = rpc:call(SlaveNode, syn, whereis, [ConflictingName, with_meta]),
  513. %% check that other processes are not alive because syn killed them
  514. true = is_process_alive(Pid0),
  515. true = rpc:call(SlaveNode, erlang, is_process_alive, [Pid1]).
  516. two_nodes_registration_race_condition_conflict_resolution_when_process_died(Config) ->
  517. ConflictingName = "COMMON",
  518. %% get slaves
  519. SlaveNode = proplists:get_value(slave_node, Config),
  520. %% use customer handler
  521. syn_test_suite_helper:use_custom_handler(),
  522. rpc:call(SlaveNode, syn_test_suite_helper, use_custom_handler, []),
  523. %% start syn on nodes
  524. ok = syn:start(),
  525. ok = rpc:call(SlaveNode, syn, start, []),
  526. timer:sleep(100),
  527. %% start processes
  528. Pid0 = syn_test_suite_helper:start_process(),
  529. Pid1 = syn_test_suite_helper:start_process(SlaveNode),
  530. %% inject into syn to simulate concurrent registration
  531. syn_registry:add_to_local_table(ConflictingName, Pid0, keep_this_one, undefined),
  532. %% kill process
  533. syn_test_suite_helper:kill_process(Pid0),
  534. %% register to trigger conflict resolution
  535. ok = rpc:call(SlaveNode, syn, register, [ConflictingName, Pid1, SlaveNode]),
  536. timer:sleep(1000),
  537. %% check
  538. {Pid1, SlaveNode} = syn:whereis(ConflictingName, with_meta),
  539. {Pid1, SlaveNode} = rpc:call(SlaveNode, syn, whereis, [ConflictingName, with_meta]),
  540. %% check that process is alive
  541. true = rpc:call(SlaveNode, erlang, is_process_alive, [Pid1]).
  542. two_nodes_registry_full_cluster_sync_on_boot_node_added_later(_Config) ->
  543. %% stop slave
  544. syn_test_suite_helper:stop_slave(syn_slave),
  545. %% start syn on local node
  546. ok = syn:start(),
  547. %% start process
  548. Pid = syn_test_suite_helper:start_process(),
  549. %% register
  550. ok = syn:register(<<"proc">>, Pid),
  551. %% start remote node and syn
  552. {ok, SlaveNode} = syn_test_suite_helper:start_slave(syn_slave),
  553. ok = rpc:call(SlaveNode, syn, start, []),
  554. timer:sleep(1000),
  555. %% check
  556. Pid = syn:whereis(<<"proc">>),
  557. Pid = rpc:call(SlaveNode, syn, whereis, [<<"proc">>]).
  558. two_nodes_registry_full_cluster_sync_on_boot_syn_started_later(Config) ->
  559. %% get slaves
  560. SlaveNode = proplists:get_value(slave_node, Config),
  561. %% start syn on local node
  562. ok = syn:start(),
  563. %% start process
  564. Pid = syn_test_suite_helper:start_process(),
  565. %% register
  566. ok = syn:register(<<"proc">>, Pid),
  567. %% start ib remote syn
  568. ok = rpc:call(SlaveNode, syn, start, []),
  569. timer:sleep(500),
  570. %% check
  571. Pid = syn:whereis(<<"proc">>),
  572. Pid = rpc:call(SlaveNode, syn, whereis, [<<"proc">>]).
  573. three_nodes_partial_netsplit_consistency(Config) ->
  574. %% get slaves
  575. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  576. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  577. %% start syn on nodes
  578. ok = syn:start(),
  579. ok = rpc:call(SlaveNode1, syn, start, []),
  580. ok = rpc:call(SlaveNode2, syn, start, []),
  581. timer:sleep(100),
  582. %% start processes
  583. Pid0 = syn_test_suite_helper:start_process(),
  584. Pid0Changed = syn_test_suite_helper:start_process(),
  585. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  586. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  587. timer:sleep(100),
  588. %% retrieve
  589. undefined = syn:whereis(<<"proc0">>),
  590. undefined = syn:whereis(<<"proc0-changed">>),
  591. undefined = syn:whereis(<<"proc1">>),
  592. undefined = syn:whereis(<<"proc2">>),
  593. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  594. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  595. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  596. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  597. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  598. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  599. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  600. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]),
  601. %% register (mix nodes)
  602. ok = rpc:call(SlaveNode2, syn, register, [<<"proc0">>, Pid0]),
  603. ok = syn:register(<<"proc1">>, Pid1),
  604. ok = rpc:call(SlaveNode1, syn, register, [<<"proc2">>, Pid2]),
  605. ok = rpc:call(SlaveNode1, syn, register, [<<"proc0-changed">>, Pid0Changed]),
  606. timer:sleep(200),
  607. %% retrieve
  608. Pid0 = syn:whereis(<<"proc0">>),
  609. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  610. Pid1 = syn:whereis(<<"proc1">>),
  611. Pid2 = syn:whereis(<<"proc2">>),
  612. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  613. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  614. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  615. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  616. Pid0 = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  617. Pid0Changed = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  618. Pid1 = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  619. Pid2 = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]),
  620. %% disconnect slave 2 from main (slave 1 can still see slave 2)
  621. syn_test_suite_helper:disconnect_node(SlaveNode2),
  622. timer:sleep(500),
  623. %% retrieve
  624. Pid0 = syn:whereis(<<"proc0">>),
  625. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  626. Pid1 = syn:whereis(<<"proc1">>),
  627. undefined = syn:whereis(<<"proc2">>), %% main has lost slave 2 so 'proc2' is removed
  628. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  629. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  630. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  631. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]), %% slave 1 still has slave 2 so 'proc2' is still there
  632. %% disconnect slave 1
  633. syn_test_suite_helper:disconnect_node(SlaveNode1),
  634. timer:sleep(500),
  635. %% unregister proc0-changed
  636. ok = syn:unregister(<<"proc0-changed">>),
  637. %% retrieve
  638. Pid0 = syn:whereis(<<"proc0">>),
  639. undefined = syn:whereis(<<"proc0-changed">>),
  640. undefined = syn:whereis(<<"proc1">>),
  641. undefined = syn:whereis(<<"proc2">>),
  642. %% reconnect all
  643. syn_test_suite_helper:connect_node(SlaveNode1),
  644. syn_test_suite_helper:connect_node(SlaveNode2),
  645. timer:sleep(5000),
  646. %% retrieve
  647. Pid0 = syn:whereis(<<"proc0">>),
  648. undefined = syn:whereis(<<"proc0-changed">>),
  649. Pid1 = syn:whereis(<<"proc1">>),
  650. Pid2 = syn:whereis(<<"proc2">>),
  651. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  652. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  653. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  654. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  655. Pid0 = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  656. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  657. Pid1 = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  658. Pid2 = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]).
  659. three_nodes_full_netsplit_consistency(Config) ->
  660. %% get slaves
  661. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  662. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  663. %% start syn on nodes
  664. ok = syn:start(),
  665. ok = rpc:call(SlaveNode1, syn, start, []),
  666. ok = rpc:call(SlaveNode2, syn, start, []),
  667. timer:sleep(100),
  668. %% start processes
  669. Pid0 = syn_test_suite_helper:start_process(),
  670. Pid0Changed = syn_test_suite_helper:start_process(),
  671. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  672. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  673. timer:sleep(100),
  674. %% retrieve
  675. undefined = syn:whereis(<<"proc0">>),
  676. undefined = syn:whereis(<<"proc0-changed">>),
  677. undefined = syn:whereis(<<"proc1">>),
  678. undefined = syn:whereis(<<"proc2">>),
  679. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  680. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  681. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  682. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  683. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  684. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  685. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  686. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]),
  687. %% register (mix nodes)
  688. ok = rpc:call(SlaveNode2, syn, register, [<<"proc0">>, Pid0]),
  689. ok = rpc:call(SlaveNode2, syn, register, [<<"proc0-changed">>, Pid0Changed]),
  690. ok = syn:register(<<"proc1">>, Pid1),
  691. ok = rpc:call(SlaveNode1, syn, register, [<<"proc2">>, Pid2]),
  692. timer:sleep(200),
  693. %% retrieve
  694. Pid0 = syn:whereis(<<"proc0">>),
  695. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  696. Pid1 = syn:whereis(<<"proc1">>),
  697. Pid2 = syn:whereis(<<"proc2">>),
  698. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  699. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  700. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  701. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  702. Pid0 = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  703. Pid0Changed = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  704. Pid1 = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  705. Pid2 = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]),
  706. %% disconnect slave 2 from main (slave 1 can still see slave 2)
  707. syn_test_suite_helper:disconnect_node(SlaveNode2),
  708. timer:sleep(500),
  709. %% retrieve
  710. Pid0 = syn:whereis(<<"proc0">>),
  711. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  712. Pid1 = syn:whereis(<<"proc1">>),
  713. undefined = syn:whereis(<<"proc2">>), %% main has lost slave 2 so 'proc2' is removed
  714. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  715. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  716. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  717. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]), %% slave 1 still has slave 2 so 'proc2' is still there
  718. %% disconnect slave 2 from slave 1
  719. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  720. timer:sleep(500),
  721. %% retrieve
  722. Pid0 = syn:whereis(<<"proc0">>),
  723. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  724. Pid1 = syn:whereis(<<"proc1">>),
  725. undefined = syn:whereis(<<"proc2">>), %% main has lost slave 2 so 'proc2' is removed
  726. undefined = syn:whereis(<<"proc2">>, with_meta),
  727. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  728. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  729. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  730. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  731. %% disconnect slave 1
  732. syn_test_suite_helper:disconnect_node(SlaveNode1),
  733. timer:sleep(500),
  734. %% unregister
  735. ok = syn:unregister(<<"proc0-changed">>),
  736. %% retrieve
  737. Pid0 = syn:whereis(<<"proc0">>),
  738. undefined = syn:whereis(<<"proc0-changed">>),
  739. undefined = syn:whereis(<<"proc1">>),
  740. undefined = syn:whereis(<<"proc2">>),
  741. %% reconnect all
  742. syn_test_suite_helper:connect_node(SlaveNode1),
  743. syn_test_suite_helper:connect_node(SlaveNode2),
  744. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  745. timer:sleep(1500),
  746. %% retrieve
  747. Pid0 = syn:whereis(<<"proc0">>),
  748. undefined = syn:whereis(<<"proc0-changed">>),
  749. Pid1 = syn:whereis(<<"proc1">>),
  750. Pid2 = syn:whereis(<<"proc2">>),
  751. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  752. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  753. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  754. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  755. Pid0 = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  756. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  757. Pid1 = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  758. Pid2 = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]).
  759. three_nodes_start_syn_before_connecting_cluster_with_conflict(Config) ->
  760. ConflictingName = "COMMON",
  761. %% get slaves
  762. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  763. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  764. %% start processes
  765. Pid0 = syn_test_suite_helper:start_process(),
  766. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  767. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  768. %% start delayed
  769. start_syn_delayed_and_register_local_process(ConflictingName, Pid0, 1500),
  770. rpc:cast(SlaveNode1, ?MODULE, start_syn_delayed_and_register_local_process, [ConflictingName, Pid1, 1500]),
  771. rpc:cast(SlaveNode2, ?MODULE, start_syn_delayed_and_register_local_process, [ConflictingName, Pid2, 1500]),
  772. timer:sleep(500),
  773. %% disconnect all
  774. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  775. syn_test_suite_helper:disconnect_node(SlaveNode1),
  776. syn_test_suite_helper:disconnect_node(SlaveNode2),
  777. timer:sleep(1500),
  778. [] = nodes(),
  779. %% reconnect all
  780. syn_test_suite_helper:connect_node(SlaveNode1),
  781. syn_test_suite_helper:connect_node(SlaveNode2),
  782. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  783. timer:sleep(1500),
  784. %% count
  785. 1 = syn:registry_count(),
  786. 1 = rpc:call(SlaveNode1, syn, registry_count, []),
  787. 1 = rpc:call(SlaveNode2, syn, registry_count, []),
  788. %% retrieve
  789. true = lists:member(syn:whereis(ConflictingName), [Pid0, Pid1, Pid2]),
  790. true = lists:member(rpc:call(SlaveNode1, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  791. true = lists:member(rpc:call(SlaveNode2, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  792. %% check metadata
  793. case syn:whereis(ConflictingName, with_meta) of
  794. {Pid0, Meta} ->
  795. CurrentNode = node(),
  796. %% check that other nodes' data corresponds
  797. {Pid0, CurrentNode} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  798. {Pid0, CurrentNode} = rpc:call(SlaveNode2, syn, whereis, [ConflictingName, with_meta]),
  799. %% check that other processes are not alive because syn killed them
  800. true = is_process_alive(Pid0),
  801. false = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  802. false = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]);
  803. {Pid1, Meta} ->
  804. SlaveNode1 = Meta,
  805. %% check that other nodes' data corresponds
  806. {Pid1, Meta} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  807. {Pid1, Meta} = rpc:call(SlaveNode2, syn, whereis, [ConflictingName, with_meta]),
  808. %% check that other processes are not alive because syn killed them
  809. false = is_process_alive(Pid0),
  810. true = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  811. false = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]);
  812. {Pid2, Meta} ->
  813. SlaveNode2 = Meta,
  814. %% check that other nodes' data corresponds
  815. {Pid2, Meta} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  816. {Pid2, Meta} = rpc:call(SlaveNode2, syn, whereis, [ConflictingName, with_meta]),
  817. %% check that other processes are not alive because syn killed them
  818. false = is_process_alive(Pid0),
  819. false = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  820. true = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]);
  821. _ ->
  822. ok = no_process_is_registered_with_conflicting_name
  823. end.
  824. three_nodes_start_syn_before_connecting_cluster_with_custom_conflict_resolution(Config) ->
  825. ConflictingName = "COMMON",
  826. %% get slaves
  827. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  828. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  829. %% start processes
  830. Pid0 = syn_test_suite_helper:start_process(),
  831. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  832. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  833. %% start delayed
  834. start_syn_delayed_with_custom_handler_register_local_process(ConflictingName, Pid0, {node, node()}, 1500),
  835. rpc:cast(
  836. SlaveNode1,
  837. ?MODULE,
  838. start_syn_delayed_with_custom_handler_register_local_process,
  839. [ConflictingName, Pid1, keep_this_one, 1500])
  840. ,
  841. rpc:cast(
  842. SlaveNode2,
  843. ?MODULE,
  844. start_syn_delayed_with_custom_handler_register_local_process,
  845. [ConflictingName, Pid2, {node, SlaveNode2}, 1500]
  846. ),
  847. timer:sleep(500),
  848. %% disconnect all
  849. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  850. syn_test_suite_helper:disconnect_node(SlaveNode1),
  851. syn_test_suite_helper:disconnect_node(SlaveNode2),
  852. timer:sleep(1500),
  853. [] = nodes(),
  854. %% reconnect all
  855. syn_test_suite_helper:connect_node(SlaveNode1),
  856. syn_test_suite_helper:connect_node(SlaveNode2),
  857. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  858. timer:sleep(5000),
  859. %% count
  860. 1 = syn:registry_count(),
  861. 1 = rpc:call(SlaveNode1, syn, registry_count, []),
  862. 1 = rpc:call(SlaveNode2, syn, registry_count, []),
  863. %% retrieve
  864. true = lists:member(syn:whereis(ConflictingName), [Pid0, Pid1, Pid2]),
  865. true = lists:member(rpc:call(SlaveNode1, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  866. true = lists:member(rpc:call(SlaveNode2, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  867. %% check metadata that we kept the correct process on all nodes
  868. {Pid1, keep_this_one} = syn:whereis(ConflictingName, with_meta),
  869. {Pid1, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  870. {Pid1, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  871. %% check that other processes are still alive because we didn't kill them
  872. true = is_process_alive(Pid0),
  873. true = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  874. true = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]).
  875. three_nodes_registration_race_condition_custom_conflict_resolution(Config) ->
  876. ConflictingName = "COMMON",
  877. %% get slaves
  878. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  879. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  880. %% use customer handler
  881. syn_test_suite_helper:use_custom_handler(),
  882. rpc:call(SlaveNode1, syn_test_suite_helper, use_custom_handler, []),
  883. rpc:call(SlaveNode2, syn_test_suite_helper, use_custom_handler, []),
  884. %% start syn on nodes
  885. ok = syn:start(),
  886. ok = rpc:call(SlaveNode1, syn, start, []),
  887. ok = rpc:call(SlaveNode2, syn, start, []),
  888. timer:sleep(500),
  889. %% start processes
  890. Pid0 = syn_test_suite_helper:start_process(),
  891. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  892. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  893. %% inject into syn to simulate concurrent registration
  894. ok = rpc:call(SlaveNode1, syn_registry, add_to_local_table, [ConflictingName, Pid1, keep_this_one, undefined]),
  895. %% register on master node to trigger conflict resolution
  896. ok = syn:register(ConflictingName, Pid0, node()),
  897. timer:sleep(1000),
  898. %% retrieve
  899. true = lists:member(syn:whereis(ConflictingName), [Pid0, Pid1, Pid2]),
  900. true = lists:member(rpc:call(SlaveNode1, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  901. true = lists:member(rpc:call(SlaveNode2, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  902. %% check metadata that we kept the correct process on all nodes
  903. {Pid1, keep_this_one} = syn:whereis(ConflictingName, with_meta),
  904. {Pid1, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  905. {Pid1, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  906. %% check that other processes are still alive because we didn't kill them
  907. true = is_process_alive(Pid0),
  908. true = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  909. true = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]).
  910. three_nodes_anti_entropy(Config) ->
  911. %% get slaves
  912. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  913. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  914. %% use customer handler
  915. syn_test_suite_helper:use_custom_handler(),
  916. rpc:call(SlaveNode1, syn_test_suite_helper, use_custom_handler, []),
  917. rpc:call(SlaveNode2, syn_test_suite_helper, use_custom_handler, []),
  918. %% set anti-entropy with a very low interval (0.25 second)
  919. syn_test_suite_helper:use_anti_entropy(registry, 0.25),
  920. rpc:call(SlaveNode1, syn_test_suite_helper, use_anti_entropy, [registry, 0.25]),
  921. rpc:call(SlaveNode2, syn_test_suite_helper, use_anti_entropy, [registry, 0.25]),
  922. %% start syn on nodes
  923. ok = syn:start(),
  924. ok = rpc:call(SlaveNode1, syn, start, []),
  925. ok = rpc:call(SlaveNode2, syn, start, []),
  926. timer:sleep(100),
  927. %% start processes
  928. Pid0 = syn_test_suite_helper:start_process(),
  929. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  930. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  931. Pid0Conflict = syn_test_suite_helper:start_process(),
  932. Pid1Conflict = syn_test_suite_helper:start_process(SlaveNode1),
  933. Pid2Conflict = syn_test_suite_helper:start_process(SlaveNode2),
  934. timer:sleep(100),
  935. %% inject data to simulate latent conflicts
  936. ok = syn_registry:add_to_local_table("pid0", Pid0, node(), undefined),
  937. ok = rpc:call(SlaveNode1, syn_registry, add_to_local_table, ["pid1", Pid1, SlaveNode1, undefined]),
  938. ok = rpc:call(SlaveNode2, syn_registry, add_to_local_table, ["pid2", Pid2, SlaveNode2, undefined]),
  939. ok = syn_registry:add_to_local_table("conflict", Pid0Conflict, node(), undefined),
  940. ok = rpc:call(SlaveNode1, syn_registry, add_to_local_table, ["conflict", Pid1Conflict, keep_this_one, undefined]),
  941. ok = rpc:call(SlaveNode2, syn_registry, add_to_local_table, ["conflict", Pid2Conflict, SlaveNode2, undefined]),
  942. %% wait to let anti-entropy settle
  943. timer:sleep(5000),
  944. %% check
  945. Node = node(),
  946. {Pid0, Node} = syn:whereis("pid0", with_meta),
  947. {Pid1, SlaveNode1} = syn:whereis("pid1", with_meta),
  948. {Pid2, SlaveNode2} = syn:whereis("pid2", with_meta),
  949. {Pid1Conflict, keep_this_one} = syn:whereis("conflict", with_meta),
  950. {Pid0, Node} = rpc:call(SlaveNode1, syn, whereis, ["pid0", with_meta]),
  951. {Pid1, SlaveNode1} = rpc:call(SlaveNode1, syn, whereis, ["pid1", with_meta]),
  952. {Pid2, SlaveNode2} = rpc:call(SlaveNode1, syn, whereis, ["pid2", with_meta]),
  953. {Pid1Conflict, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, ["conflict", with_meta]),
  954. {Pid0, Node} = rpc:call(SlaveNode2, syn, whereis, ["pid0", with_meta]),
  955. {Pid1, SlaveNode1} = rpc:call(SlaveNode2, syn, whereis, ["pid1", with_meta]),
  956. {Pid2, SlaveNode2} = rpc:call(SlaveNode2, syn, whereis, ["pid2", with_meta]),
  957. {Pid1Conflict, keep_this_one} = rpc:call(SlaveNode2, syn, whereis, ["conflict", with_meta]).
  958. %% ===================================================================
  959. %% Internal
  960. %% ===================================================================
  961. start_syn_delayed_and_register_local_process(Name, Pid, Ms) ->
  962. spawn(fun() ->
  963. lists:foreach(fun(Node) ->
  964. syn_test_suite_helper:disconnect_node(Node)
  965. end, nodes()),
  966. timer:sleep(Ms),
  967. [] = nodes(),
  968. %%
  969. syn:start(),
  970. ok = syn:register(Name, Pid, node())
  971. end).
  972. start_syn_delayed_with_custom_handler_register_local_process(Name, Pid, Meta, Ms) ->
  973. spawn(fun() ->
  974. lists:foreach(fun(Node) ->
  975. syn_test_suite_helper:disconnect_node(Node)
  976. end, nodes()),
  977. timer:sleep(Ms),
  978. [] = nodes(),
  979. %% use customer handler
  980. syn_test_suite_helper:use_custom_handler(),
  981. %%
  982. syn:start(),
  983. ok = syn:register(Name, Pid, Meta)
  984. end).