syn_registry_SUITE.erl 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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_remote/1,
  47. two_nodes_registration_race_condition_conflict_resolution_keep_remote_with_custom_handler/1,
  48. two_nodes_registration_race_condition_conflict_resolution_keep_local_with_custom_handler/1,
  49. two_nodes_registration_race_condition_conflict_resolution_when_process_died/1,
  50. two_nodes_registry_full_cluster_sync_on_boot_node_added_later/1,
  51. two_nodes_registry_full_cluster_sync_on_boot_syn_started_later/1,
  52. two_nodes_reregister/1
  53. ]).
  54. -export([
  55. three_nodes_partial_netsplit_consistency/1,
  56. three_nodes_full_netsplit_consistency/1,
  57. three_nodes_start_syn_before_connecting_cluster_with_conflict/1,
  58. three_nodes_start_syn_before_connecting_cluster_with_custom_conflict_resolution_keep_remote/1,
  59. three_nodes_registration_race_condition_custom_conflict_resolution/1,
  60. three_nodes_anti_entropy/1,
  61. three_nodes_anti_entropy_manual/1,
  62. three_nodes_concurrent_registration_unregistration/1,
  63. three_nodes_resolve_conflict_on_all_nodes/1
  64. ]).
  65. %% support
  66. -export([
  67. start_syn_delayed_and_register_local_process/3,
  68. start_syn_delayed_with_custom_handler_register_local_process/4,
  69. seq_unregister_register/3
  70. ]).
  71. %% include
  72. -include_lib("common_test/include/ct.hrl").
  73. -include_lib("../src/syn.hrl").
  74. %% ===================================================================
  75. %% Callbacks
  76. %% ===================================================================
  77. %% -------------------------------------------------------------------
  78. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  79. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  80. %% GroupName = atom()
  81. %% TestCase = atom()
  82. %% Reason = any()
  83. %% -------------------------------------------------------------------
  84. all() ->
  85. [
  86. {group, single_node_process_registration},
  87. {group, two_nodes_process_registration},
  88. {group, three_nodes_process_registration}
  89. ].
  90. %% -------------------------------------------------------------------
  91. %% Function: groups() -> [Group]
  92. %% Group = {GroupName,Properties,GroupsAndTestCases}
  93. %% GroupName = atom()
  94. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  95. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  96. %% TestCase = atom()
  97. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  98. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  99. %% repeat_until_any_ok | repeat_until_any_fail
  100. %% N = integer() | forever
  101. %% -------------------------------------------------------------------
  102. groups() ->
  103. [
  104. {single_node_process_registration, [shuffle], [
  105. single_node_register_and_monitor,
  106. single_node_register_and_unregister,
  107. single_node_registration_errors,
  108. single_node_registry_count,
  109. single_node_register_gen_server,
  110. single_node_callback_on_process_exit,
  111. single_node_ensure_callback_process_exit_is_called_if_process_killed,
  112. single_node_monitor_after_registry_crash
  113. ]},
  114. {two_nodes_process_registration, [shuffle], [
  115. two_nodes_register_monitor_and_unregister,
  116. two_nodes_registry_count,
  117. two_nodes_registration_race_condition_conflict_resolution_keep_remote,
  118. two_nodes_registration_race_condition_conflict_resolution_keep_remote_with_custom_handler,
  119. two_nodes_registration_race_condition_conflict_resolution_keep_local_with_custom_handler,
  120. two_nodes_registration_race_condition_conflict_resolution_when_process_died,
  121. two_nodes_registry_full_cluster_sync_on_boot_node_added_later,
  122. two_nodes_registry_full_cluster_sync_on_boot_syn_started_later,
  123. two_nodes_reregister
  124. ]},
  125. {three_nodes_process_registration, [shuffle], [
  126. three_nodes_partial_netsplit_consistency,
  127. three_nodes_full_netsplit_consistency,
  128. three_nodes_start_syn_before_connecting_cluster_with_conflict,
  129. three_nodes_start_syn_before_connecting_cluster_with_custom_conflict_resolution_keep_remote,
  130. three_nodes_registration_race_condition_custom_conflict_resolution,
  131. three_nodes_anti_entropy,
  132. three_nodes_anti_entropy_manual,
  133. three_nodes_concurrent_registration_unregistration,
  134. three_nodes_resolve_conflict_on_all_nodes
  135. ]}
  136. ].
  137. %% -------------------------------------------------------------------
  138. %% Function: init_per_suite(Config0) ->
  139. %% Config1 | {skip,Reason} |
  140. %% {skip_and_save,Reason,Config1}
  141. %% Config0 = Config1 = [tuple()]
  142. %% Reason = any()
  143. %% -------------------------------------------------------------------
  144. init_per_suite(Config) ->
  145. Config.
  146. %% -------------------------------------------------------------------
  147. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  148. %% Config0 = Config1 = [tuple()]
  149. %% -------------------------------------------------------------------
  150. end_per_suite(_Config) ->
  151. ok.
  152. %% -------------------------------------------------------------------
  153. %% Function: init_per_group(GroupName, Config0) ->
  154. %% Config1 | {skip,Reason} |
  155. %% {skip_and_save,Reason,Config1}
  156. %% GroupName = atom()
  157. %% Config0 = Config1 = [tuple()]
  158. %% Reason = any()
  159. %% -------------------------------------------------------------------
  160. init_per_group(two_nodes_process_registration, Config) ->
  161. %% start slave
  162. {ok, SlaveNode} = syn_test_suite_helper:start_slave(syn_slave),
  163. %% config
  164. [{slave_node, SlaveNode} | Config];
  165. init_per_group(three_nodes_process_registration, Config) ->
  166. %% start slave
  167. {ok, SlaveNode1} = syn_test_suite_helper:start_slave(syn_slave_1),
  168. {ok, SlaveNode2} = syn_test_suite_helper:start_slave(syn_slave_2),
  169. %% config
  170. [{slave_node_1, SlaveNode1}, {slave_node_2, SlaveNode2} | Config];
  171. init_per_group(_GroupName, Config) ->
  172. Config.
  173. %% -------------------------------------------------------------------
  174. %% Function: end_per_group(GroupName, Config0) ->
  175. %% void() | {save_config,Config1}
  176. %% GroupName = atom()
  177. %% Config0 = Config1 = [tuple()]
  178. %% -------------------------------------------------------------------
  179. end_per_group(two_nodes_process_registration, Config) ->
  180. SlaveNode = proplists:get_value(slave_node, Config),
  181. syn_test_suite_helper:connect_node(SlaveNode),
  182. syn_test_suite_helper:clean_after_test(),
  183. syn_test_suite_helper:stop_slave(syn_slave),
  184. timer:sleep(1000);
  185. end_per_group(three_nodes_process_registration, Config) ->
  186. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  187. syn_test_suite_helper:connect_node(SlaveNode1),
  188. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  189. syn_test_suite_helper:connect_node(SlaveNode2),
  190. syn_test_suite_helper:clean_after_test(),
  191. syn_test_suite_helper:stop_slave(syn_slave_1),
  192. syn_test_suite_helper:stop_slave(syn_slave_2),
  193. timer:sleep(1000);
  194. end_per_group(_GroupName, _Config) ->
  195. syn_test_suite_helper:clean_after_test().
  196. %% -------------------------------------------------------------------
  197. %% Function: init_per_testcase(TestCase, Config0) ->
  198. %% Config1 | {skip,Reason} | {skip_and_save,Reason,Config1}
  199. %% TestCase = atom()
  200. %% Config0 = Config1 = [tuple()]
  201. %% Reason = any()
  202. %% -------------------------------------------------------------------
  203. init_per_testcase(TestCase, Config) ->
  204. ct:pal("Starting test: ~p", [TestCase]),
  205. Config.
  206. %% -------------------------------------------------------------------
  207. %% Function: end_per_testcase(TestCase, Config0) ->
  208. %% void() | {save_config,Config1} | {fail,Reason}
  209. %% TestCase = atom()
  210. %% Config0 = Config1 = [tuple()]
  211. %% Reason = any()
  212. %% -------------------------------------------------------------------
  213. end_per_testcase(_, _Config) ->
  214. syn_test_suite_helper:clean_after_test().
  215. %% ===================================================================
  216. %% Tests
  217. %% ===================================================================
  218. single_node_register_and_monitor(_Config) ->
  219. %% start
  220. ok = syn:start(),
  221. %% start processes
  222. Pid = syn_test_suite_helper:start_process(),
  223. PidWithMeta = syn_test_suite_helper:start_process(),
  224. %% retrieve
  225. undefined = syn:whereis(<<"my proc">>),
  226. %% register
  227. ok = syn:register(<<"my proc">>, Pid),
  228. ok = syn:register({"my proc 2"}, Pid),
  229. ok = syn:register(<<"my proc with meta">>, PidWithMeta, {meta, <<"meta">>}),
  230. %% retrieve
  231. Pid = syn:whereis(<<"my proc">>),
  232. Pid = syn:whereis({"my proc 2"}),
  233. {PidWithMeta, {meta, <<"meta">>}} = syn:whereis(<<"my proc with meta">>, with_meta),
  234. %% re-register
  235. ok = syn:register(<<"my proc with meta">>, PidWithMeta, {meta2, <<"meta2">>}),
  236. {PidWithMeta, {meta2, <<"meta2">>}} = syn:whereis(<<"my proc with meta">>, with_meta),
  237. %% kill process
  238. syn_test_suite_helper:kill_process(Pid),
  239. syn_test_suite_helper:kill_process(PidWithMeta),
  240. timer:sleep(100),
  241. %% retrieve
  242. undefined = syn:whereis(<<"my proc">>),
  243. undefined = syn:whereis({"my proc 2"}),
  244. undefined = syn:whereis(<<"my proc with meta">>).
  245. single_node_register_and_unregister(_Config) ->
  246. %% start
  247. ok = syn:start(),
  248. %% start process
  249. Pid = syn_test_suite_helper:start_process(),
  250. %% retrieve
  251. undefined = syn:whereis(<<"my proc">>),
  252. %% register
  253. ok = syn:register(<<"my proc">>, Pid),
  254. ok = syn:register(<<"my proc 2">>, Pid),
  255. %% retrieve
  256. Pid = syn:whereis(<<"my proc">>),
  257. Pid = syn:whereis(<<"my proc 2">>),
  258. %% unregister 1
  259. ok = syn:unregister(<<"my proc">>),
  260. %% retrieve
  261. undefined = syn:whereis(<<"my proc">>),
  262. Pid = syn:whereis(<<"my proc 2">>),
  263. %% unregister 2
  264. ok = syn:unregister(<<"my proc 2">>),
  265. {error, undefined} = syn:unregister(<<"my proc 2">>),
  266. %% retrieve
  267. undefined = syn:whereis(<<"my proc">>),
  268. undefined = syn:whereis(<<"my proc 2">>).
  269. single_node_registration_errors(_Config) ->
  270. %% start
  271. ok = syn:start(),
  272. %% start process
  273. Pid = syn_test_suite_helper:start_process(),
  274. Pid2 = syn_test_suite_helper:start_process(),
  275. %% register
  276. ok = syn:register(<<"my proc">>, Pid),
  277. {error, taken} = syn:register(<<"my proc">>, Pid2),
  278. %% kill processes
  279. syn_test_suite_helper:kill_process(Pid),
  280. syn_test_suite_helper:kill_process(Pid2),
  281. timer:sleep(100),
  282. %% retrieve
  283. undefined = syn:whereis(<<"my proc">>),
  284. %% try registering a dead pid
  285. {error, not_alive} = syn:register(<<"my proc">>, Pid).
  286. single_node_registry_count(_Config) ->
  287. %% start
  288. ok = syn:start(),
  289. %% start process
  290. Pid = syn_test_suite_helper:start_process(),
  291. Pid2 = syn_test_suite_helper:start_process(),
  292. PidUnregistered = syn_test_suite_helper:start_process(),
  293. %% register
  294. ok = syn:register(<<"my proc">>, Pid),
  295. ok = syn:register(<<"my proc 2">>, Pid2),
  296. %% count
  297. 2 = syn:registry_count(),
  298. 2 = syn:registry_count(node()),
  299. %% kill & unregister
  300. syn_test_suite_helper:kill_process(Pid),
  301. ok = syn:unregister(<<"my proc 2">>),
  302. syn_test_suite_helper:kill_process(PidUnregistered),
  303. timer:sleep(100),
  304. %% count
  305. 0 = syn:registry_count(),
  306. 0 = syn:registry_count(node()).
  307. single_node_register_gen_server(_Config) ->
  308. %% start
  309. ok = syn:start(),
  310. %% start gen server via syn
  311. {ok, Pid} = syn_test_gen_server:start_link(),
  312. %% retrieve
  313. Pid = syn:whereis(syn_test_gen_server),
  314. %% call
  315. pong = syn_test_gen_server:ping(),
  316. %% send via syn
  317. syn:send(syn_test_gen_server, {self(), send_ping}),
  318. receive
  319. send_pong -> ok
  320. after 1000 ->
  321. ok = did_not_receive_gen_server_pong
  322. end,
  323. %% stop server
  324. syn_test_gen_server:stop(),
  325. timer:sleep(200),
  326. %% retrieve
  327. undefined = syn:whereis(syn_test_gen_server),
  328. %% send via syn
  329. {badarg, {syn_test_gen_server, anything}} = (catch syn:send(syn_test_gen_server, anything)).
  330. single_node_callback_on_process_exit(_Config) ->
  331. %% use custom handler
  332. syn_test_suite_helper:use_custom_handler(),
  333. %% start
  334. ok = syn:start(),
  335. %% start process
  336. Pid = syn_test_suite_helper:start_process(),
  337. Pid2 = syn_test_suite_helper:start_process(),
  338. %% register
  339. TestPid = self(),
  340. ok = syn:register(<<"my proc">>, Pid, {pid, TestPid}),
  341. ok = syn:register(<<"my proc - alternate">>, Pid, {pid_alternate, TestPid}),
  342. ok = syn:register(<<"my proc 2">>, Pid2, {pid2, TestPid}),
  343. %% kill 1
  344. syn_test_suite_helper:kill_process(Pid),
  345. receive
  346. {received_event_on, pid} ->
  347. ok;
  348. {received_event_on, pid2} ->
  349. ok = callback_on_process_exit_was_received_by_pid2
  350. after 1000 ->
  351. ok = callback_on_process_exit_was_not_received_by_pid
  352. end,
  353. receive
  354. {received_event_on, pid_alternate} ->
  355. ok;
  356. {received_event_on, pid2} ->
  357. ok = callback_on_process_exit_was_received_by_pid2
  358. after 1000 ->
  359. ok = callback_on_process_exit_was_not_received_by_pid
  360. end,
  361. %% unregister & kill 2
  362. ok = syn:unregister(<<"my proc 2">>),
  363. syn_test_suite_helper:kill_process(Pid2),
  364. receive
  365. {received_event_on, pid2} ->
  366. ok = callback_on_process_exit_was_received_by_pid2
  367. after 1000 ->
  368. ok
  369. end.
  370. single_node_ensure_callback_process_exit_is_called_if_process_killed(_Config) ->
  371. Name = <<"my proc">>,
  372. %% use custom handler
  373. syn_test_suite_helper:use_custom_handler(),
  374. %% start
  375. ok = syn:start(),
  376. %% start process
  377. Pid = syn_test_suite_helper:start_process(),
  378. %% register
  379. TestPid = self(),
  380. ok = syn:register(Name, Pid, {some_meta, TestPid}),
  381. %% remove from table to simulate conflict resolution
  382. syn_registry:remove_from_local_table(Name, TestPid),
  383. %% kill
  384. exit(Pid, {syn_resolve_kill, Name, {some_meta, TestPid}}),
  385. receive
  386. {received_event_on, some_meta} ->
  387. ok
  388. after 1000 ->
  389. ok = callback_on_process_exit_was_not_received_by_pid
  390. end.
  391. single_node_monitor_after_registry_crash(_Config) ->
  392. %% start
  393. ok = syn:start(),
  394. %% start processes
  395. Pid = syn_test_suite_helper:start_process(),
  396. %% register
  397. ok = syn:register(<<"my proc">>, Pid),
  398. %% kill registry
  399. exit(whereis(syn_registry), kill),
  400. timer:sleep(200),
  401. %% retrieve
  402. Pid = syn:whereis(<<"my proc">>),
  403. %% kill process
  404. syn_test_suite_helper:kill_process(Pid),
  405. timer:sleep(200),
  406. %% retrieve
  407. undefined = syn:whereis(<<"my proc 2">>).
  408. two_nodes_register_monitor_and_unregister(Config) ->
  409. %% get slave
  410. SlaveNode = proplists:get_value(slave_node, Config),
  411. %% start
  412. ok = syn:start(),
  413. ok = rpc:call(SlaveNode, syn, start, []),
  414. timer:sleep(100),
  415. %% start processes
  416. LocalPid = syn_test_suite_helper:start_process(),
  417. RemotePid = syn_test_suite_helper:start_process(SlaveNode),
  418. RemotePidRegRemote = syn_test_suite_helper:start_process(SlaveNode),
  419. %% retrieve
  420. undefined = syn:whereis(<<"local proc">>),
  421. undefined = syn:whereis(<<"remote proc">>),
  422. undefined = syn:whereis(<<"remote proc reg_remote">>),
  423. undefined = rpc:call(SlaveNode, syn, whereis, [<<"local proc">>]),
  424. undefined = rpc:call(SlaveNode, syn, whereis, [<<"remote proc">>]),
  425. undefined = rpc:call(SlaveNode, syn, whereis, [<<"remote proc reg_remote">>]),
  426. %% register
  427. ok = syn:register(<<"local proc">>, LocalPid),
  428. ok = syn:register(<<"remote proc">>, RemotePid),
  429. ok = rpc:call(SlaveNode, syn, register, [<<"remote proc reg_remote">>, RemotePidRegRemote]),
  430. timer:sleep(500),
  431. %% retrieve
  432. LocalPid = syn:whereis(<<"local proc">>),
  433. RemotePid = syn:whereis(<<"remote proc">>),
  434. RemotePidRegRemote = syn:whereis(<<"remote proc reg_remote">>),
  435. LocalPid = rpc:call(SlaveNode, syn, whereis, [<<"local proc">>]),
  436. RemotePid = rpc:call(SlaveNode, syn, whereis, [<<"remote proc">>]),
  437. RemotePidRegRemote = rpc:call(SlaveNode, syn, whereis, [<<"remote proc reg_remote">>]),
  438. %% kill & unregister processes
  439. syn_test_suite_helper:kill_process(LocalPid),
  440. ok = syn:unregister(<<"remote proc">>),
  441. syn_test_suite_helper:kill_process(RemotePidRegRemote),
  442. timer:sleep(100),
  443. %% retrieve
  444. undefined = syn:whereis(<<"local proc">>),
  445. undefined = syn:whereis(<<"remote proc">>),
  446. undefined = syn:whereis(<<"remote proc reg_remote">>),
  447. undefined = rpc:call(SlaveNode, syn, whereis, [<<"local proc">>]),
  448. undefined = rpc:call(SlaveNode, syn, whereis, [<<"remote proc">>]),
  449. undefined = rpc:call(SlaveNode, syn, whereis, [<<"remote proc reg_remote">>]).
  450. two_nodes_registry_count(Config) ->
  451. %% get slave
  452. SlaveNode = proplists:get_value(slave_node, Config),
  453. %% start
  454. ok = syn:start(),
  455. ok = rpc:call(SlaveNode, syn, start, []),
  456. timer:sleep(100),
  457. %% start processes
  458. LocalPid = syn_test_suite_helper:start_process(),
  459. RemotePid = syn_test_suite_helper:start_process(SlaveNode),
  460. RemotePidRegRemote = syn_test_suite_helper:start_process(SlaveNode),
  461. _PidUnregistered = syn_test_suite_helper:start_process(),
  462. %% register
  463. ok = syn:register(<<"local proc">>, LocalPid),
  464. ok = syn:register(<<"remote proc">>, RemotePid),
  465. ok = rpc:call(SlaveNode, syn, register, [<<"remote proc reg_remote">>, RemotePidRegRemote]),
  466. timer:sleep(500),
  467. %% count
  468. 3 = syn:registry_count(),
  469. 1 = syn:registry_count(node()),
  470. 2 = syn:registry_count(SlaveNode),
  471. %% kill & unregister processes
  472. syn_test_suite_helper:kill_process(LocalPid),
  473. ok = syn:unregister(<<"remote proc">>),
  474. syn_test_suite_helper:kill_process(RemotePidRegRemote),
  475. timer:sleep(100),
  476. %% count
  477. 0 = syn:registry_count(),
  478. 0 = syn:registry_count(node()),
  479. 0 = syn:registry_count(SlaveNode).
  480. two_nodes_registration_race_condition_conflict_resolution_keep_remote(Config) ->
  481. ConflictingName = "COMMON",
  482. %% get slaves
  483. SlaveNode = proplists:get_value(slave_node, Config),
  484. %% start syn on nodes
  485. ok = syn:start(),
  486. ok = rpc:call(SlaveNode, syn, start, []),
  487. timer:sleep(1000),
  488. %% start processes
  489. Pid0 = syn_test_suite_helper:start_process(),
  490. Pid1 = syn_test_suite_helper:start_process(SlaveNode),
  491. %% inject into syn to simulate concurrent registration
  492. ok = syn_registry:add_to_local_table(ConflictingName, Pid0, node(), undefined),
  493. %% register on slave node to trigger conflict resolution on master node
  494. ok = rpc:call(SlaveNode, syn, register, [ConflictingName, Pid1, SlaveNode]),
  495. timer:sleep(1000),
  496. %% check metadata, resolution happens on master node
  497. {Pid1, SlaveNode} = syn:whereis(ConflictingName, with_meta),
  498. {Pid1, SlaveNode} = rpc:call(SlaveNode, syn, whereis, [ConflictingName, with_meta]),
  499. %% check that other processes are not alive because syn killed them
  500. false = is_process_alive(Pid0),
  501. true = rpc:call(SlaveNode, erlang, is_process_alive, [Pid1]).
  502. two_nodes_registration_race_condition_conflict_resolution_keep_remote_with_custom_handler(Config) ->
  503. ConflictingName = "COMMON",
  504. %% get slaves
  505. SlaveNode = proplists:get_value(slave_node, Config),
  506. %% use customer handler
  507. syn_test_suite_helper:use_custom_handler(),
  508. rpc:call(SlaveNode, syn_test_suite_helper, use_custom_handler, []),
  509. %% start syn on nodes
  510. ok = syn:start(),
  511. ok = rpc:call(SlaveNode, syn, start, []),
  512. timer:sleep(1000),
  513. %% start processes
  514. Pid0 = syn_test_suite_helper:start_process(),
  515. Pid1 = syn_test_suite_helper:start_process(SlaveNode),
  516. %% inject into syn to simulate concurrent registration
  517. ok = syn:register(ConflictingName, Pid0, node()),
  518. %% trigger conflict resolution on master node
  519. ok = syn_registry:sync_register(node(), ConflictingName, Pid1, keep_this_one),
  520. timer:sleep(1000),
  521. %% check metadata, resolution happens on master node
  522. {Pid1, keep_this_one} = syn:whereis(ConflictingName, with_meta),
  523. %% check that other processes are not alive because syn killed them
  524. true = is_process_alive(Pid0),
  525. true = rpc:call(SlaveNode, erlang, is_process_alive, [Pid1]),
  526. %% check that discarded process is not monitored
  527. {monitored_by, Monitors} = erlang:process_info(Pid0, monitored_by),
  528. 0 = length(Monitors).
  529. two_nodes_registration_race_condition_conflict_resolution_keep_local_with_custom_handler(Config) ->
  530. ConflictingName = "COMMON",
  531. %% get slaves
  532. SlaveNode = proplists:get_value(slave_node, Config),
  533. %% use customer handler
  534. syn_test_suite_helper:use_custom_handler(),
  535. rpc:call(SlaveNode, syn_test_suite_helper, use_custom_handler, []),
  536. %% start syn on nodes
  537. ok = syn:start(),
  538. ok = rpc:call(SlaveNode, syn, start, []),
  539. timer:sleep(1000),
  540. %% start processes
  541. Pid0 = syn_test_suite_helper:start_process(),
  542. Pid1 = syn_test_suite_helper:start_process(SlaveNode),
  543. %% inject into syn to simulate concurrent registration
  544. ok = syn_registry:add_to_local_table(ConflictingName, Pid0, keep_this_one, undefined),
  545. %% register on slave node to trigger conflict resolution on master node
  546. ok = rpc:call(SlaveNode, syn, register, [ConflictingName, Pid1, SlaveNode]),
  547. timer:sleep(1000),
  548. %% check metadata, resolution happens on master node
  549. {Pid0, keep_this_one} = syn:whereis(ConflictingName, with_meta),
  550. {Pid0, keep_this_one} = rpc:call(SlaveNode, syn, whereis, [ConflictingName, with_meta]),
  551. %% check that other processes are not alive because syn killed them
  552. true = is_process_alive(Pid0),
  553. true = rpc:call(SlaveNode, erlang, is_process_alive, [Pid1]),
  554. %% check that discarded process is not monitored
  555. {monitored_by, Monitors} = rpc:call(SlaveNode, erlang, process_info, [Pid1, monitored_by]),
  556. 0 = length(Monitors).
  557. two_nodes_registration_race_condition_conflict_resolution_when_process_died(Config) ->
  558. ConflictingName = "COMMON",
  559. %% get slaves
  560. SlaveNode = proplists:get_value(slave_node, Config),
  561. %% use customer handler
  562. syn_test_suite_helper:use_custom_handler(),
  563. rpc:call(SlaveNode, syn_test_suite_helper, use_custom_handler, []),
  564. %% start syn on nodes
  565. ok = syn:start(),
  566. ok = rpc:call(SlaveNode, syn, start, []),
  567. timer:sleep(1000),
  568. %% start processes
  569. Pid0 = syn_test_suite_helper:start_process(),
  570. Pid1 = syn_test_suite_helper:start_process(SlaveNode),
  571. %% inject into syn to simulate concurrent registration
  572. syn_registry:add_to_local_table(ConflictingName, Pid0, keep_this_one, undefined),
  573. timer:sleep(250),
  574. %% kill process
  575. syn_test_suite_helper:kill_process(Pid0),
  576. %% register to trigger conflict resolution
  577. timer:sleep(250),
  578. ok = rpc:call(SlaveNode, syn, register, [ConflictingName, Pid1, SlaveNode]),
  579. timer:sleep(250),
  580. %% check
  581. {Pid1, SlaveNode} = syn:whereis(ConflictingName, with_meta),
  582. {Pid1, SlaveNode} = rpc:call(SlaveNode, syn, whereis, [ConflictingName, with_meta]),
  583. %% check that process is alive
  584. true = rpc:call(SlaveNode, erlang, is_process_alive, [Pid1]).
  585. two_nodes_registry_full_cluster_sync_on_boot_node_added_later(_Config) ->
  586. %% stop slave
  587. syn_test_suite_helper:stop_slave(syn_slave),
  588. %% start syn on local node
  589. ok = syn:start(),
  590. %% start process
  591. Pid = syn_test_suite_helper:start_process(),
  592. %% register
  593. ok = syn:register(<<"proc">>, Pid),
  594. %% start remote node and syn
  595. {ok, SlaveNode} = syn_test_suite_helper:start_slave(syn_slave),
  596. ok = rpc:call(SlaveNode, syn, start, []),
  597. timer:sleep(1000),
  598. %% check
  599. Pid = syn:whereis(<<"proc">>),
  600. Pid = rpc:call(SlaveNode, syn, whereis, [<<"proc">>]).
  601. two_nodes_registry_full_cluster_sync_on_boot_syn_started_later(Config) ->
  602. %% get slaves
  603. SlaveNode = proplists:get_value(slave_node, Config),
  604. %% start syn on local node
  605. ok = syn:start(),
  606. %% start process
  607. Pid = syn_test_suite_helper:start_process(),
  608. %% register
  609. ok = syn:register(<<"proc">>, Pid),
  610. %% start ib remote syn
  611. ok = rpc:call(SlaveNode, syn, start, []),
  612. timer:sleep(500),
  613. %% check
  614. Pid = syn:whereis(<<"proc">>),
  615. Pid = rpc:call(SlaveNode, syn, whereis, [<<"proc">>]).
  616. two_nodes_reregister(Config) ->
  617. Name = "common name",
  618. %% get slave
  619. SlaveNode = proplists:get_value(slave_node, Config),
  620. %% start
  621. ok = syn:start(),
  622. ok = rpc:call(SlaveNode, syn, start, []),
  623. timer:sleep(100),
  624. %% start processes
  625. PidLocal = syn_test_suite_helper:start_process(),
  626. PidRemote = syn_test_suite_helper:start_process(SlaveNode),
  627. ok = rpc:call(SlaveNode, syn, register, [Name, PidRemote]),
  628. %% fast unreg-reg
  629. ok = syn:reregister(Name, PidLocal),
  630. timer:sleep(1000),
  631. PidLocal = syn:whereis(Name),
  632. ok = rpc:call(SlaveNode, syn, reregister, [Name, PidRemote, some_meta]),
  633. timer:sleep(1000),
  634. {PidRemote, some_meta} = syn:whereis(Name, with_meta).
  635. three_nodes_partial_netsplit_consistency(Config) ->
  636. %% get slaves
  637. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  638. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  639. %% start syn on nodes
  640. ok = syn:start(),
  641. ok = rpc:call(SlaveNode1, syn, start, []),
  642. ok = rpc:call(SlaveNode2, syn, start, []),
  643. timer:sleep(100),
  644. %% start processes
  645. Pid0 = syn_test_suite_helper:start_process(),
  646. Pid0Changed = syn_test_suite_helper:start_process(),
  647. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  648. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  649. timer:sleep(100),
  650. %% retrieve
  651. undefined = syn:whereis(<<"proc0">>),
  652. undefined = syn:whereis(<<"proc0-changed">>),
  653. undefined = syn:whereis(<<"proc1">>),
  654. undefined = syn:whereis(<<"proc2">>),
  655. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  656. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  657. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  658. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  659. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  660. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  661. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  662. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]),
  663. %% register (mix nodes)
  664. ok = rpc:call(SlaveNode2, syn, register, [<<"proc0">>, Pid0]),
  665. ok = syn:register(<<"proc1">>, Pid1),
  666. ok = rpc:call(SlaveNode1, syn, register, [<<"proc2">>, Pid2]),
  667. ok = rpc:call(SlaveNode1, syn, register, [<<"proc0-changed">>, Pid0Changed]),
  668. timer:sleep(200),
  669. %% retrieve
  670. Pid0 = syn:whereis(<<"proc0">>),
  671. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  672. Pid1 = syn:whereis(<<"proc1">>),
  673. Pid2 = syn:whereis(<<"proc2">>),
  674. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  675. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  676. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  677. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  678. Pid0 = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  679. Pid0Changed = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  680. Pid1 = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  681. Pid2 = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]),
  682. %% disconnect slave 2 from main (slave 1 can still see slave 2)
  683. syn_test_suite_helper:disconnect_node(SlaveNode2),
  684. timer:sleep(500),
  685. %% retrieve
  686. Pid0 = syn:whereis(<<"proc0">>),
  687. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  688. Pid1 = syn:whereis(<<"proc1">>),
  689. undefined = syn:whereis(<<"proc2">>), %% main has lost slave 2 so 'proc2' is removed
  690. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  691. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  692. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  693. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]), %% slave 1 still has slave 2 so 'proc2' is still there
  694. %% disconnect slave 1
  695. syn_test_suite_helper:disconnect_node(SlaveNode1),
  696. timer:sleep(500),
  697. %% unregister proc0-changed
  698. ok = syn:unregister(<<"proc0-changed">>),
  699. %% retrieve
  700. Pid0 = syn:whereis(<<"proc0">>),
  701. undefined = syn:whereis(<<"proc0-changed">>),
  702. undefined = syn:whereis(<<"proc1">>),
  703. undefined = syn:whereis(<<"proc2">>),
  704. %% reconnect all
  705. syn_test_suite_helper:connect_node(SlaveNode1),
  706. syn_test_suite_helper:connect_node(SlaveNode2),
  707. timer:sleep(5000),
  708. %% retrieve
  709. Pid0 = syn:whereis(<<"proc0">>),
  710. undefined = syn:whereis(<<"proc0-changed">>),
  711. Pid1 = syn:whereis(<<"proc1">>),
  712. Pid2 = syn:whereis(<<"proc2">>),
  713. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  714. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  715. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  716. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  717. Pid0 = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  718. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  719. Pid1 = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  720. Pid2 = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]).
  721. three_nodes_full_netsplit_consistency(Config) ->
  722. %% get slaves
  723. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  724. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  725. %% start syn on nodes
  726. ok = syn:start(),
  727. ok = rpc:call(SlaveNode1, syn, start, []),
  728. ok = rpc:call(SlaveNode2, syn, start, []),
  729. timer:sleep(100),
  730. %% start processes
  731. Pid0 = syn_test_suite_helper:start_process(),
  732. Pid0Changed = syn_test_suite_helper:start_process(),
  733. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  734. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  735. timer:sleep(100),
  736. %% retrieve
  737. undefined = syn:whereis(<<"proc0">>),
  738. undefined = syn:whereis(<<"proc0-changed">>),
  739. undefined = syn:whereis(<<"proc1">>),
  740. undefined = syn:whereis(<<"proc2">>),
  741. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  742. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  743. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  744. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  745. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  746. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  747. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  748. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]),
  749. %% register (mix nodes)
  750. ok = rpc:call(SlaveNode2, syn, register, [<<"proc0">>, Pid0]),
  751. ok = rpc:call(SlaveNode2, syn, register, [<<"proc0-changed">>, Pid0Changed]),
  752. ok = syn:register(<<"proc1">>, Pid1),
  753. ok = rpc:call(SlaveNode1, syn, register, [<<"proc2">>, Pid2]),
  754. timer:sleep(200),
  755. %% retrieve
  756. Pid0 = syn:whereis(<<"proc0">>),
  757. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  758. Pid1 = syn:whereis(<<"proc1">>),
  759. Pid2 = syn:whereis(<<"proc2">>),
  760. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  761. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  762. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  763. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  764. Pid0 = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  765. Pid0Changed = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  766. Pid1 = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  767. Pid2 = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]),
  768. %% disconnect slave 2 from main (slave 1 can still see slave 2)
  769. syn_test_suite_helper:disconnect_node(SlaveNode2),
  770. timer:sleep(500),
  771. %% retrieve
  772. Pid0 = syn:whereis(<<"proc0">>),
  773. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  774. Pid1 = syn:whereis(<<"proc1">>),
  775. undefined = syn:whereis(<<"proc2">>), %% main has lost slave 2 so 'proc2' is removed
  776. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  777. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  778. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  779. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]), %% slave 1 still has slave 2 so 'proc2' is still there
  780. %% disconnect slave 2 from slave 1
  781. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  782. timer:sleep(500),
  783. %% retrieve
  784. Pid0 = syn:whereis(<<"proc0">>),
  785. Pid0Changed = syn:whereis(<<"proc0-changed">>),
  786. Pid1 = syn:whereis(<<"proc1">>),
  787. undefined = syn:whereis(<<"proc2">>), %% main has lost slave 2 so 'proc2' is removed
  788. undefined = syn:whereis(<<"proc2">>, with_meta),
  789. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  790. Pid0Changed = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  791. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  792. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  793. %% disconnect slave 1
  794. syn_test_suite_helper:disconnect_node(SlaveNode1),
  795. timer:sleep(500),
  796. %% unregister
  797. ok = syn:unregister(<<"proc0-changed">>),
  798. %% retrieve
  799. Pid0 = syn:whereis(<<"proc0">>),
  800. undefined = syn:whereis(<<"proc0-changed">>),
  801. undefined = syn:whereis(<<"proc1">>),
  802. undefined = syn:whereis(<<"proc2">>),
  803. %% reconnect all
  804. syn_test_suite_helper:connect_node(SlaveNode1),
  805. syn_test_suite_helper:connect_node(SlaveNode2),
  806. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  807. timer:sleep(1500),
  808. %% retrieve
  809. Pid0 = syn:whereis(<<"proc0">>),
  810. undefined = syn:whereis(<<"proc0-changed">>),
  811. Pid1 = syn:whereis(<<"proc1">>),
  812. Pid2 = syn:whereis(<<"proc2">>),
  813. Pid0 = rpc:call(SlaveNode1, syn, whereis, [<<"proc0">>]),
  814. undefined = rpc:call(SlaveNode1, syn, whereis, [<<"proc0-changed">>]),
  815. Pid1 = rpc:call(SlaveNode1, syn, whereis, [<<"proc1">>]),
  816. Pid2 = rpc:call(SlaveNode1, syn, whereis, [<<"proc2">>]),
  817. Pid0 = rpc:call(SlaveNode2, syn, whereis, [<<"proc0">>]),
  818. undefined = rpc:call(SlaveNode2, syn, whereis, [<<"proc0-changed">>]),
  819. Pid1 = rpc:call(SlaveNode2, syn, whereis, [<<"proc1">>]),
  820. Pid2 = rpc:call(SlaveNode2, syn, whereis, [<<"proc2">>]).
  821. three_nodes_start_syn_before_connecting_cluster_with_conflict(Config) ->
  822. ConflictingName = "COMMON",
  823. %% get slaves
  824. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  825. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  826. %% start processes
  827. Pid0 = syn_test_suite_helper:start_process(),
  828. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  829. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  830. %% start delayed
  831. start_syn_delayed_and_register_local_process(ConflictingName, Pid0, 1500),
  832. rpc:cast(SlaveNode1, ?MODULE, start_syn_delayed_and_register_local_process, [ConflictingName, Pid1, 1500]),
  833. rpc:cast(SlaveNode2, ?MODULE, start_syn_delayed_and_register_local_process, [ConflictingName, Pid2, 1500]),
  834. timer:sleep(500),
  835. %% disconnect all
  836. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  837. syn_test_suite_helper:disconnect_node(SlaveNode1),
  838. syn_test_suite_helper:disconnect_node(SlaveNode2),
  839. timer:sleep(2000),
  840. [] = nodes(),
  841. %% reconnect all
  842. syn_test_suite_helper:connect_node(SlaveNode1),
  843. syn_test_suite_helper:connect_node(SlaveNode2),
  844. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  845. timer:sleep(2500),
  846. %% count
  847. 1 = syn:registry_count(),
  848. 1 = rpc:call(SlaveNode1, syn, registry_count, []),
  849. 1 = rpc:call(SlaveNode2, syn, registry_count, []),
  850. %% retrieve
  851. true = lists:member(syn:whereis(ConflictingName), [Pid0, Pid1, Pid2]),
  852. true = lists:member(rpc:call(SlaveNode1, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  853. true = lists:member(rpc:call(SlaveNode2, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  854. %% check metadata
  855. case syn:whereis(ConflictingName, with_meta) of
  856. {Pid0, Meta} ->
  857. CurrentNode = node(),
  858. %% check that other nodes' data corresponds
  859. {Pid0, CurrentNode} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  860. {Pid0, CurrentNode} = rpc:call(SlaveNode2, syn, whereis, [ConflictingName, with_meta]),
  861. %% check that other processes are not alive because syn killed them
  862. true = is_process_alive(Pid0),
  863. false = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  864. false = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]);
  865. {Pid1, Meta} ->
  866. SlaveNode1 = Meta,
  867. %% check that other nodes' data corresponds
  868. {Pid1, Meta} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  869. {Pid1, Meta} = rpc:call(SlaveNode2, syn, whereis, [ConflictingName, with_meta]),
  870. %% check that other processes are not alive because syn killed them
  871. false = is_process_alive(Pid0),
  872. true = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  873. false = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]);
  874. {Pid2, Meta} ->
  875. SlaveNode2 = Meta,
  876. %% check that other nodes' data corresponds
  877. {Pid2, Meta} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  878. {Pid2, Meta} = rpc:call(SlaveNode2, syn, whereis, [ConflictingName, with_meta]),
  879. %% check that other processes are not alive because syn killed them
  880. false = is_process_alive(Pid0),
  881. false = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  882. true = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]);
  883. _ ->
  884. ok = no_process_is_registered_with_conflicting_name
  885. end.
  886. three_nodes_start_syn_before_connecting_cluster_with_custom_conflict_resolution_keep_remote(Config) ->
  887. ConflictingName = "COMMON",
  888. %% get slaves
  889. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  890. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  891. %% start processes
  892. Pid0 = syn_test_suite_helper:start_process(),
  893. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  894. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  895. %% start delayed
  896. start_syn_delayed_with_custom_handler_register_local_process(ConflictingName, Pid0, {node, node()}, 1500),
  897. rpc:cast(
  898. SlaveNode1,
  899. ?MODULE,
  900. start_syn_delayed_with_custom_handler_register_local_process,
  901. [ConflictingName, Pid1, keep_this_one, 1500])
  902. ,
  903. rpc:cast(
  904. SlaveNode2,
  905. ?MODULE,
  906. start_syn_delayed_with_custom_handler_register_local_process,
  907. [ConflictingName, Pid2, {node, SlaveNode2}, 1500]
  908. ),
  909. timer:sleep(500),
  910. %% disconnect all
  911. rpc:call(SlaveNode1, syn_test_suite_helper, disconnect_node, [SlaveNode2]),
  912. syn_test_suite_helper:disconnect_node(SlaveNode1),
  913. syn_test_suite_helper:disconnect_node(SlaveNode2),
  914. timer:sleep(1500),
  915. [] = nodes(),
  916. %% reconnect all
  917. syn_test_suite_helper:connect_node(SlaveNode1),
  918. syn_test_suite_helper:connect_node(SlaveNode2),
  919. rpc:call(SlaveNode1, syn_test_suite_helper, connect_node, [SlaveNode2]),
  920. timer:sleep(5000),
  921. %% count
  922. 1 = syn:registry_count(),
  923. 1 = rpc:call(SlaveNode1, syn, registry_count, []),
  924. 1 = rpc:call(SlaveNode2, syn, registry_count, []),
  925. %% retrieve
  926. true = lists:member(syn:whereis(ConflictingName), [Pid0, Pid1, Pid2]),
  927. true = lists:member(rpc:call(SlaveNode1, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  928. true = lists:member(rpc:call(SlaveNode2, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  929. %% check metadata that we kept the correct process on all nodes
  930. {Pid1, keep_this_one} = syn:whereis(ConflictingName, with_meta),
  931. {Pid1, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  932. {Pid1, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  933. %% check that other processes are still alive because we didn't kill them
  934. true = is_process_alive(Pid0),
  935. true = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  936. true = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]),
  937. %% check that discarded processes are not monitored
  938. {monitored_by, Monitors0} = erlang:process_info(Pid0, monitored_by),
  939. 0 = length(Monitors0),
  940. {monitored_by, Monitors2} = rpc:call(SlaveNode2, erlang, process_info, [Pid2, monitored_by]),
  941. 0 = length(Monitors2).
  942. three_nodes_registration_race_condition_custom_conflict_resolution(Config) ->
  943. ConflictingName = "COMMON",
  944. %% get slaves
  945. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  946. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  947. %% use customer handler
  948. syn_test_suite_helper:use_custom_handler(),
  949. rpc:call(SlaveNode1, syn_test_suite_helper, use_custom_handler, []),
  950. rpc:call(SlaveNode2, syn_test_suite_helper, use_custom_handler, []),
  951. %% start syn on nodes
  952. ok = syn:start(),
  953. ok = rpc:call(SlaveNode1, syn, start, []),
  954. ok = rpc:call(SlaveNode2, syn, start, []),
  955. timer:sleep(500),
  956. %% start processes
  957. Pid0 = syn_test_suite_helper:start_process(),
  958. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  959. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  960. %% inject into syn to simulate concurrent registration
  961. ok = rpc:call(SlaveNode1, syn_registry, add_to_local_table, [ConflictingName, Pid1, keep_this_one, undefined]),
  962. %% register on master node to trigger conflict resolution
  963. ok = syn:register(ConflictingName, Pid0, node()),
  964. timer:sleep(1000),
  965. %% retrieve
  966. true = lists:member(syn:whereis(ConflictingName), [Pid0, Pid1, Pid2]),
  967. true = lists:member(rpc:call(SlaveNode1, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  968. true = lists:member(rpc:call(SlaveNode2, syn, whereis, [ConflictingName]), [Pid0, Pid1, Pid2]),
  969. %% check metadata that we kept the correct process on all nodes
  970. {Pid1, keep_this_one} = syn:whereis(ConflictingName, with_meta),
  971. {Pid1, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  972. {Pid1, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, [ConflictingName, with_meta]),
  973. %% check that other processes are still alive because we didn't kill them
  974. true = is_process_alive(Pid0),
  975. true = rpc:call(SlaveNode1, erlang, is_process_alive, [Pid1]),
  976. true = rpc:call(SlaveNode2, erlang, is_process_alive, [Pid2]).
  977. three_nodes_anti_entropy(Config) ->
  978. %% get slaves
  979. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  980. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  981. %% use customer handler
  982. syn_test_suite_helper:use_custom_handler(),
  983. rpc:call(SlaveNode1, syn_test_suite_helper, use_custom_handler, []),
  984. rpc:call(SlaveNode2, syn_test_suite_helper, use_custom_handler, []),
  985. %% set anti-entropy with a very low interval (0.25 second)
  986. syn_test_suite_helper:use_anti_entropy(registry, 0.25),
  987. rpc:call(SlaveNode1, syn_test_suite_helper, use_anti_entropy, [registry, 0.25]),
  988. rpc:call(SlaveNode2, syn_test_suite_helper, use_anti_entropy, [registry, 0.25]),
  989. %% start syn on nodes
  990. ok = syn:start(),
  991. ok = rpc:call(SlaveNode1, syn, start, []),
  992. ok = rpc:call(SlaveNode2, syn, start, []),
  993. timer:sleep(100),
  994. %% start processes
  995. Pid0 = syn_test_suite_helper:start_process(),
  996. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  997. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  998. Pid0Conflict = syn_test_suite_helper:start_process(),
  999. Pid1Conflict = syn_test_suite_helper:start_process(SlaveNode1),
  1000. Pid2Conflict = syn_test_suite_helper:start_process(SlaveNode2),
  1001. timer:sleep(100),
  1002. %% inject data to simulate latent conflicts
  1003. ok = syn_registry:add_to_local_table("pid0", Pid0, node(), undefined),
  1004. ok = rpc:call(SlaveNode1, syn_registry, add_to_local_table, ["pid1", Pid1, SlaveNode1, undefined]),
  1005. ok = rpc:call(SlaveNode2, syn_registry, add_to_local_table, ["pid2", Pid2, SlaveNode2, undefined]),
  1006. ok = syn_registry:add_to_local_table("conflict", Pid0Conflict, node(), undefined),
  1007. ok = rpc:call(SlaveNode1, syn_registry, add_to_local_table, ["conflict", Pid1Conflict, keep_this_one, undefined]),
  1008. ok = rpc:call(SlaveNode2, syn_registry, add_to_local_table, ["conflict", Pid2Conflict, SlaveNode2, undefined]),
  1009. %% wait to let anti-entropy settle
  1010. timer:sleep(5000),
  1011. %% check
  1012. Node = node(),
  1013. {Pid0, Node} = syn:whereis("pid0", with_meta),
  1014. {Pid1, SlaveNode1} = syn:whereis("pid1", with_meta),
  1015. {Pid2, SlaveNode2} = syn:whereis("pid2", with_meta),
  1016. {Pid1Conflict, keep_this_one} = syn:whereis("conflict", with_meta),
  1017. {Pid0, Node} = rpc:call(SlaveNode1, syn, whereis, ["pid0", with_meta]),
  1018. {Pid1, SlaveNode1} = rpc:call(SlaveNode1, syn, whereis, ["pid1", with_meta]),
  1019. {Pid2, SlaveNode2} = rpc:call(SlaveNode1, syn, whereis, ["pid2", with_meta]),
  1020. {Pid1Conflict, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, ["conflict", with_meta]),
  1021. {Pid0, Node} = rpc:call(SlaveNode2, syn, whereis, ["pid0", with_meta]),
  1022. {Pid1, SlaveNode1} = rpc:call(SlaveNode2, syn, whereis, ["pid1", with_meta]),
  1023. {Pid2, SlaveNode2} = rpc:call(SlaveNode2, syn, whereis, ["pid2", with_meta]),
  1024. {Pid1Conflict, keep_this_one} = rpc:call(SlaveNode2, syn, whereis, ["conflict", with_meta]).
  1025. three_nodes_anti_entropy_manual(Config) ->
  1026. %% get slaves
  1027. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  1028. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  1029. %% use customer handler
  1030. syn_test_suite_helper:use_custom_handler(),
  1031. rpc:call(SlaveNode1, syn_test_suite_helper, use_custom_handler, []),
  1032. rpc:call(SlaveNode2, syn_test_suite_helper, use_custom_handler, []),
  1033. %% start syn on nodes
  1034. ok = syn:start(),
  1035. ok = rpc:call(SlaveNode1, syn, start, []),
  1036. ok = rpc:call(SlaveNode2, syn, start, []),
  1037. timer:sleep(100),
  1038. %% start processes
  1039. Pid0 = syn_test_suite_helper:start_process(),
  1040. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  1041. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  1042. Pid0Conflict = syn_test_suite_helper:start_process(),
  1043. Pid1Conflict = syn_test_suite_helper:start_process(SlaveNode1),
  1044. Pid2Conflict = syn_test_suite_helper:start_process(SlaveNode2),
  1045. timer:sleep(100),
  1046. %% inject data to simulate latent conflicts
  1047. ok = syn_registry:add_to_local_table("pid0", Pid0, node(), undefined),
  1048. ok = rpc:call(SlaveNode1, syn_registry, add_to_local_table, ["pid1", Pid1, SlaveNode1, undefined]),
  1049. ok = rpc:call(SlaveNode2, syn_registry, add_to_local_table, ["pid2", Pid2, SlaveNode2, undefined]),
  1050. ok = syn_registry:add_to_local_table("conflict", Pid0Conflict, node(), undefined),
  1051. ok = rpc:call(SlaveNode1, syn_registry, add_to_local_table, ["conflict", Pid1Conflict, keep_this_one, undefined]),
  1052. ok = rpc:call(SlaveNode2, syn_registry, add_to_local_table, ["conflict", Pid2Conflict, SlaveNode2, undefined]),
  1053. %% call anti entropy
  1054. ok = syn:force_cluster_sync(registry),
  1055. timer:sleep(1000),
  1056. %% check
  1057. Node = node(),
  1058. {Pid0, Node} = syn:whereis("pid0", with_meta),
  1059. {Pid1, SlaveNode1} = syn:whereis("pid1", with_meta),
  1060. {Pid2, SlaveNode2} = syn:whereis("pid2", with_meta),
  1061. {Pid1Conflict, keep_this_one} = syn:whereis("conflict", with_meta),
  1062. {Pid0, Node} = rpc:call(SlaveNode1, syn, whereis, ["pid0", with_meta]),
  1063. {Pid1, SlaveNode1} = rpc:call(SlaveNode1, syn, whereis, ["pid1", with_meta]),
  1064. {Pid2, SlaveNode2} = rpc:call(SlaveNode1, syn, whereis, ["pid2", with_meta]),
  1065. {Pid1Conflict, keep_this_one} = rpc:call(SlaveNode1, syn, whereis, ["conflict", with_meta]),
  1066. {Pid0, Node} = rpc:call(SlaveNode2, syn, whereis, ["pid0", with_meta]),
  1067. {Pid1, SlaveNode1} = rpc:call(SlaveNode2, syn, whereis, ["pid1", with_meta]),
  1068. {Pid2, SlaveNode2} = rpc:call(SlaveNode2, syn, whereis, ["pid2", with_meta]),
  1069. {Pid1Conflict, keep_this_one} = rpc:call(SlaveNode2, syn, whereis, ["conflict", with_meta]).
  1070. three_nodes_concurrent_registration_unregistration(Config) ->
  1071. CommonName = "common-name",
  1072. %% get slaves
  1073. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  1074. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  1075. %% start syn on nodes
  1076. ok = syn:start(),
  1077. ok = rpc:call(SlaveNode1, syn, start, []),
  1078. ok = rpc:call(SlaveNode2, syn, start, []),
  1079. timer:sleep(100),
  1080. %% start processes
  1081. Pid0 = syn_test_suite_helper:start_process(),
  1082. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  1083. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  1084. timer:sleep(100),
  1085. %% register on 0
  1086. ok = syn:register(CommonName, Pid0, node()),
  1087. timer:sleep(250),
  1088. %% check
  1089. Node = node(),
  1090. {Pid0, Node} = syn:whereis(CommonName, with_meta),
  1091. {Pid0, Node} = rpc:call(SlaveNode1, syn, whereis, [CommonName, with_meta]),
  1092. {Pid0, Node} = rpc:call(SlaveNode2, syn, whereis, [CommonName, with_meta]),
  1093. %% simulate unregistration with inconsistent data
  1094. syn_registry:sync_unregister(SlaveNode1, Pid1, CommonName),
  1095. timer:sleep(250),
  1096. %% check
  1097. Node = node(),
  1098. {Pid0, Node} = syn:whereis(CommonName, with_meta),
  1099. {Pid0, Node} = rpc:call(SlaveNode1, syn, whereis, [CommonName, with_meta]),
  1100. {Pid0, Node} = rpc:call(SlaveNode2, syn, whereis, [CommonName, with_meta]).
  1101. three_nodes_resolve_conflict_on_all_nodes(Config) ->
  1102. CommonName = "common-name",
  1103. %% get slaves
  1104. SlaveNode1 = proplists:get_value(slave_node_1, Config),
  1105. SlaveNode2 = proplists:get_value(slave_node_2, Config),
  1106. %% start syn on nodes
  1107. ok = syn:start(),
  1108. ok = rpc:call(SlaveNode1, syn, start, []),
  1109. ok = rpc:call(SlaveNode2, syn, start, []),
  1110. timer:sleep(100),
  1111. %% start processes
  1112. Pid0 = syn_test_suite_helper:start_process(),
  1113. Pid1 = syn_test_suite_helper:start_process(SlaveNode1),
  1114. Pid2 = syn_test_suite_helper:start_process(SlaveNode2),
  1115. timer:sleep(100),
  1116. %% register on slave 1 to begin conflict resolution
  1117. ok = rpc:call(SlaveNode1, syn, register, [CommonName, Pid1, SlaveNode1]),
  1118. timer:sleep(500),
  1119. %% check
  1120. {Pid1, SlaveNode1} = syn:whereis(CommonName, with_meta),
  1121. {Pid1, SlaveNode1} = rpc:call(SlaveNode1, syn, whereis, [CommonName, with_meta]),
  1122. {Pid1, SlaveNode1} = rpc:call(SlaveNode2, syn, whereis, [CommonName, with_meta]),
  1123. %% force a sync registration conflict on master node from slave 2
  1124. syn_registry:sync_register(node(), CommonName, Pid2, SlaveNode2),
  1125. timer:sleep(1000),
  1126. %% check
  1127. {Pid2, SlaveNode2} = syn:whereis(CommonName, with_meta),
  1128. {Pid2, SlaveNode2} = rpc:call(SlaveNode1, syn, whereis, [CommonName, with_meta]).
  1129. %% ===================================================================
  1130. %% Internal
  1131. %% ===================================================================
  1132. start_syn_delayed_and_register_local_process(Name, Pid, Ms) ->
  1133. spawn(fun() ->
  1134. lists:foreach(fun(Node) ->
  1135. syn_test_suite_helper:disconnect_node(Node)
  1136. end, nodes()),
  1137. timer:sleep(Ms),
  1138. [] = nodes(),
  1139. %%
  1140. syn:start(),
  1141. ok = syn:register(Name, Pid, node())
  1142. end).
  1143. start_syn_delayed_with_custom_handler_register_local_process(Name, Pid, Meta, Ms) ->
  1144. spawn(fun() ->
  1145. lists:foreach(fun(Node) ->
  1146. syn_test_suite_helper:disconnect_node(Node)
  1147. end, nodes()),
  1148. timer:sleep(Ms),
  1149. [] = nodes(),
  1150. %% use customer handler
  1151. syn_test_suite_helper:use_custom_handler(),
  1152. %%
  1153. syn:start(),
  1154. ok = syn:register(Name, Pid, Meta)
  1155. end).
  1156. seq_unregister_register(Name, Pid, Meta) ->
  1157. syn:unregister(Name),
  1158. syn:register(Name, Pid, Meta).