paxos_signal_handlers.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  1. #include "include/paxos_signal_handlers.h"
  2. #include "include/my_rtrm.h"
  3. #include "include/libfunctions.h"
  4. #include "include/sig_aux.h"
  5. #include "include/scc_signals.h"
  6. #include "include/controller_core.h"
  7. #include "include/common_core.h"
  8. #include "include/idag_defs.h"
  9. #include "include/signal_handlers.h"
  10. #include "include/variables.h"
  11. #include "include/macros.h"
  12. #include "include/structs.h"
  13. int faulty_core = -1;
  14. int first_time = 0;
  15. int pending_workload[2] = {-1,-1};
  16. int proposal_number_personal;
  17. core_states paxos_state;
  18. acceptor_var acceptor_vars = {-1,-1,-1};
  19. proposer_var proposer_vars = {-1,-1,0,0,NULL};
  20. int fail_flag = 0;
  21. struct timeval fail_time_val;
  22. static char local_scen_directory[SCEN_DIR_SIZE], local_scen_num[SCEN_NUM_SIZE];
  23. #ifdef PLAT_SCC
  24. char error_str[64];
  25. int error, str_len, sig_array_local[LINE_SIZE];
  26. #endif
  27. int leader_preference(){
  28. coworkers_list *tmp_cowork_list;
  29. int num_of_coworkers = 0;
  30. if (state == IDLE_CORE){
  31. return 60;
  32. }
  33. else if (state == INIT_MANAGER ||
  34. state == INIT_MANAGER_SEND_OFFERS ||
  35. state == IDLE_INIT_MAN ||
  36. state == INIT_MAN_CHK_OFFERS){
  37. return 50;
  38. }
  39. else if (state == WORKING_NODE && pending_state == NO_PENDING_STATE){
  40. if (coworkers != NULL)
  41. FOR_MY_COWORKERS_LIST num_of_coworkers++;
  42. if (num_of_coworkers == 1)
  43. return 1;
  44. else
  45. return (40 + num_of_coworkers);
  46. }
  47. else if (state == WORKING_NODE && (
  48. pending_state == INIT_MANAGER ||
  49. pending_state == INIT_MANAGER_SEND_OFFERS ||
  50. pending_state == IDLE_INIT_MAN ||
  51. pending_state == INIT_MAN_CHK_OFFERS)){
  52. if (coworkers != NULL)
  53. FOR_MY_COWORKERS_LIST num_of_coworkers++;
  54. if (num_of_coworkers == 1)
  55. return 1;
  56. else{
  57. return (30+num_of_coworkers);
  58. }
  59. }
  60. else if (state == WORKING_NODE && (
  61. pending_state == IDLE_AGENT ||
  62. pending_state == IDLE_AGENT_WAITING_OFF ||
  63. pending_state == AGENT_INIT_STATE ||
  64. pending_state == AGENT_SELF_OPT ||
  65. pending_state == AGENT_SELF_CHK_OFFERS ||
  66. pending_state == AGENT_ENDING ||
  67. pending_state == IDAG_ENDING ||
  68. pending_state == AGENT_ZOMBIE ||
  69. pending_state == AGENT_INIT_APP_INIT ||
  70. pending_state == AGENT_INIT_CHK_OFFERS ||
  71. pending_state == AGENT_INIT_IDLE_INIT ||
  72. pending_state == IDLE_INIT_IDLE_AGENT ||
  73. pending_state == IDLE_INIT_AGENT_SELFOPT ||
  74. pending_state == INIT_CHK_OFFERS_IDLE_AGENT ||
  75. pending_state == INIT_CHK_OFFERS_SELFOPT)){
  76. if (coworkers != NULL)
  77. FOR_MY_COWORKERS_LIST num_of_coworkers++;
  78. if (num_of_coworkers == 1)
  79. return 1;
  80. else{
  81. return (20+num_of_coworkers);
  82. }
  83. }
  84. else if (state == IDLE_AGENT ||
  85. state == IDLE_AGENT_WAITING_OFF ||
  86. state == AGENT_INIT_STATE ||
  87. state == AGENT_SELF_OPT ||
  88. state == AGENT_SELF_CHK_OFFERS ||
  89. state == AGENT_ENDING ||
  90. state == IDAG_ENDING ||
  91. state == AGENT_ZOMBIE){
  92. return 10;
  93. }
  94. return 0;
  95. }
  96. void initialize_PAXOS_data (char scen_directory[SCEN_DIR_SIZE], char scen_num[SCEN_NUM_SIZE]) {
  97. //printf("Initializing Paxos data...\n");
  98. strcpy(local_scen_directory, scen_directory);
  99. strcpy(local_scen_num, scen_num);
  100. }
  101. void sig_PAXOS_INIT_handler(){
  102. int i;
  103. int num_of_coworkers = 0;
  104. inter_list tmp_inter_list;
  105. coworkers_list *tmp_cowork_list;
  106. handler_Enter(-1, "sig_PAXOS_INIT_handler");
  107. paxos_state = PAXOS_ACTIVE;
  108. #ifdef PLAT_SCC
  109. RCCE_wait_until(proposal_number_lock,RCCE_FLAG_UNSET);
  110. RCCE_flag_write(&proposal_number_lock,RCCE_FLAG_SET,node_id);
  111. proposal_number_personal = *proposal_number_global;
  112. *proposal_number_global += 1;
  113. RCCE_flag_write(&proposal_number_lock,RCCE_FLAG_UNSET,node_id);
  114. #else
  115. /* Pick up my personal proposal number */
  116. sem_wait(proposal_number_lock);
  117. proposal_number_personal = *proposal_number_global;
  118. *proposal_number_global += 1;
  119. sem_post(proposal_number_lock);
  120. #endif
  121. fprintf(log_file, "\t\tI am %d, and my proposal number is %d\n",node_id,proposal_number_personal);
  122. tmp_inter_list.next = NULL;
  123. proposer_vars.core_states = (int *)malloc((X_max*Y_max)*sizeof(int));
  124. for (i = 0; i < X_max*Y_max; i++){
  125. proposer_vars.core_states[i] = -1;
  126. }
  127. /* Case where controller fails */
  128. if (idag_mask[faulty_core] == faulty_core){
  129. for (i = 0; i < X_max*Y_max; i++){
  130. if ((i != my_idag) && (idag_mask[i] == my_idag)){
  131. tmp_inter_list.data.proposal_number = proposal_number_personal;
  132. tmp_inter_list.type = PREPARE_REQUEST;
  133. scc_kill(i,SIG_PREPARE_REQUEST,&tmp_inter_list);
  134. }
  135. }
  136. }
  137. /* Case where manager fails */
  138. else{
  139. fprintf(log_file,"\t\tcoworkers List: ");
  140. FOR_MY_COWORKERS_LIST{
  141. fprintf(log_file,"%d --> ", tmp_cowork_list->core_id);
  142. }
  143. printf("\n");
  144. FOR_MY_COWORKERS_LIST{
  145. num_of_coworkers++;
  146. tmp_inter_list.data.proposal_number = proposal_number_personal;
  147. tmp_inter_list.type = PREPARE_REQUEST;
  148. scc_kill(tmp_cowork_list->core_id, SIG_PREPARE_REQUEST, &tmp_inter_list);
  149. }
  150. fprintf(log_file,"num_of_coworkers=%d\n",num_of_coworkers);
  151. printf("num_of_coworkers=%d\n",num_of_coworkers);
  152. }
  153. handler_Exit(-1, "sig_PAXOS_INIT_handler");
  154. return;
  155. }
  156. /************* If a core doesn't receive a signal for a period of time it assumes the
  157. ************** controller has died and sends PREPARE_REQUEST signal
  158. *************/
  159. void sig_PREPARE_REQUEST_handler(int sender_id){
  160. int received_proposal_number = sig_read_ar[2];
  161. inter_list tmp_inter_list;
  162. int score;
  163. handler_Enter(sender_id,"sig_PREPARE_REQUEST_handler");
  164. fprintf(log_file, "\t\tReceived SIG_PREPARE_REQUEST from %d with Proposal Number %d\n", sender_id, received_proposal_number);
  165. /* The acceptor has seen a higher proposal number */
  166. /* Prepare Request Rejected */
  167. if (acceptor_vars.highest_proposed_n > received_proposal_number){
  168. fprintf(log_file, "\t\tI have seen a higher proposal number request --> REJECTED!\n");
  169. return;
  170. }
  171. /* The acceptor has not seen another prepare request before so it promises never to accept a prepare request with proposal number
  172. lower than this one. Replies with PREPARE_ACCEPT_NO_PREVIOUS */
  173. /* UPDATE 13.10.2016 -- Also send my state in order to elect core with minimum utilization */
  174. else if (acceptor_vars.highest_proposed_n == -1){
  175. score = leader_preference();
  176. printf("My score is %d and my state %s\n",score, id2string(state));
  177. fprintf(log_file, "\t\tI haven't accepted any values yet!\n");
  178. acceptor_vars.highest_proposed_n = received_proposal_number;
  179. fprintf(log_file, "\t\tUpdated: acceptor_vars.highest_proposed_n = %d\n", acceptor_vars.highest_proposed_n);
  180. tmp_inter_list.next = NULL;
  181. tmp_inter_list.type = PREPARE_ACCEPT_NO_PREVIOUS;
  182. tmp_inter_list.data.accepted_values[0] = -1;
  183. tmp_inter_list.data.accepted_values[1] = -1;
  184. tmp_inter_list.data.accepted_values[2] = score;
  185. scc_kill(sender_id,SIG_PREPARE_ACCEPT_NO_PREVIOUS,&tmp_inter_list);
  186. }
  187. /* Proposal number received > Highest proposal number seen */
  188. /* Acceptor replies with highest proposal number seen and its value if any and updates the values*/
  189. else if (acceptor_vars.highest_proposed_n < received_proposal_number){
  190. fprintf(log_file, "\t\treceived_proposal_number higher than highest_proposed_n\n");
  191. acceptor_vars.highest_proposed_n = received_proposal_number;
  192. fprintf(log_file, "\t\tUpdated: acceptor_vars.highest_proposed_n = %d\n", acceptor_vars.highest_proposed_n);
  193. /*If the core has accepted any value it sends that back along with the proposal number of this value*/
  194. if (acceptor_vars.highest_acc_value == -1){
  195. fprintf(log_file, "\t\tI haven't accepted any values yet!\n");
  196. tmp_inter_list.next = NULL;
  197. tmp_inter_list.type = PREPARE_ACCEPT_NO_PREVIOUS;
  198. tmp_inter_list.data.accepted_values[0] = -1;
  199. tmp_inter_list.data.accepted_values[1] = -1;
  200. tmp_inter_list.data.accepted_values[2] = score;
  201. scc_kill(sender_id,SIG_PREPARE_ACCEPT_NO_PREVIOUS,&tmp_inter_list);
  202. }else{
  203. fprintf(log_file, "\t\tI have already accepted the value %d!\n", acceptor_vars.highest_acc_value);
  204. tmp_inter_list.next = NULL;
  205. tmp_inter_list.type = PREPARE_ACCEPT;
  206. tmp_inter_list.data.accepted_values[0] = acceptor_vars.highest_acc_n;
  207. tmp_inter_list.data.accepted_values[1] = acceptor_vars.highest_acc_value;
  208. tmp_inter_list.data.accepted_values[2] = score; /* FIXME This seems to be uninitialised */
  209. scc_kill(sender_id, SIG_PREPARE_ACCEPT, &tmp_inter_list);
  210. }
  211. }
  212. handler_Exit(sender_id,"sig_PREPARE_REQUEST_handler");
  213. return;
  214. }
  215. void sig_PREPARE_ACCEPT_NO_PREVIOUS_handler(int sender_id){
  216. int k,i;
  217. int num_of_coworkers;
  218. int replied_score = sig_read_ar[4];
  219. int max_score,index;
  220. inter_list tmp_inter_list;
  221. coworkers_list *tmp_cowork_list;
  222. handler_Enter(sender_id,"sig_PREPARE_ACCEPT_NO_PREVIOUS_handler");
  223. proposer_vars.core_states[sender_id] = replied_score;
  224. proposer_vars.cores_promised++;
  225. fprintf(log_file, "\t\t Updated state of %d to %s\n", sender_id, id2string(proposer_vars.core_states[sender_id]));
  226. tmp_inter_list.next = NULL;
  227. /* Case where controller fails */
  228. if (idag_mask[faulty_core] == faulty_core){
  229. /*Received reply from majority */
  230. if (proposer_vars.cores_promised >= majority(CLUSTER_SIZE)){
  231. if (PREPARE_ACCEPT_SENT == 0){
  232. for (i=0; i < X_max*Y_max;i++){
  233. if (proposer_vars.core_states[i] > max_score){
  234. max_score = proposer_vars.core_states[i];
  235. index = i;
  236. }
  237. }
  238. PREPARE_ACCEPT_SENT = 1;
  239. fprintf(log_file,"\t\t-------- LAST KNOWN CORE STATES --------\n");
  240. FOR_NUES{
  241. fprintf(log_file,"\t\t%d --> %s\n",k,id2string(proposer_vars.core_states[k]));
  242. }
  243. fprintf(log_file,"\t\t----------------------------------------\n");
  244. fprintf(log_file,"\n");
  245. //TODO Decide proposing core based on core states
  246. fprintf(log_file, "\t\tCONTROLLER CASE ; RECEIVED ACCEPT FROM MAJORITY!\n");
  247. for (k = 0; k < X_max*Y_max; k++){
  248. /* I send SIG_ACCEPT_REQUEST to cores inside my cluster */
  249. if ((k != my_idag) && (idag_mask[k] == my_idag)){
  250. tmp_inter_list.type = ACCEPT_REQUEST;
  251. tmp_inter_list.data.accepted_values[PROP_NW] = proposal_number_personal;
  252. /*The acceptor hasn't received any reply with accepted value so it will propose itself for leader*/
  253. if (proposer_vars.highest_replied_value == -1){
  254. /*if (im_manager() != 1){
  255. fprintf(log_file,"\t\tI am not a manager. I propose myself as the new controller\n");
  256. proposer_vars.highest_replied_value = node_id;
  257. tmp_inter_list.data.accepted_values[VALUE_W] = proposer_vars.highest_replied_value;
  258. }else{
  259. fprintf(log_file,"\t\tI am a manager. I propose a worker of mine as the new controller.That is %d\n",my_cores->next->core_id);
  260. tmp_inter_list.data.accepted_values[VALUE_W] = my_cores->next->core_id;
  261. }*/
  262. /* RANDOM PAXOS */
  263. //proposer_vars.highest_replied_value = node_id;
  264. //tmp_inter_list.data.accepted_values[VALUE_W] = node_id;
  265. /****************/
  266. /* MODIFIED PAXOS */
  267. proposer_vars.highest_replied_value = index;
  268. tmp_inter_list.data.accepted_values[VALUE_W] = index;
  269. /****************/
  270. }else{
  271. tmp_inter_list.data.accepted_values[VALUE_W] = proposer_vars.highest_replied_value;
  272. }
  273. scc_kill(k,SIG_ACCEPT_REQUEST,&tmp_inter_list);
  274. }
  275. }
  276. }
  277. }
  278. /* Case where manager fails */
  279. }else{
  280. num_of_coworkers = 0;
  281. FOR_MY_COWORKERS_LIST num_of_coworkers++;
  282. fprintf(log_file,"num_of_coworkers=%d\n",num_of_coworkers);
  283. if (proposer_vars.cores_promised > majority(num_of_coworkers)){
  284. if (PREPARE_ACCEPT_SENT == 0){
  285. PREPARE_ACCEPT_SENT = 1;
  286. fprintf(log_file, "\t\tMANAGER CASE ; RECEIVED ACCEPT FROM MAJORITY!\n");
  287. FOR_MY_COWORKERS_LIST{
  288. tmp_inter_list.type = ACCEPT_REQUEST;
  289. tmp_inter_list.data.accepted_values[PROP_NW] = proposal_number_personal;
  290. if (proposer_vars.highest_replied_value == -1)
  291. proposer_vars.highest_replied_value = node_id;
  292. tmp_inter_list.data.accepted_values[VALUE_W] = proposer_vars.highest_replied_value;
  293. scc_kill(tmp_cowork_list->core_id,SIG_ACCEPT_REQUEST,&tmp_inter_list);
  294. }
  295. }
  296. }
  297. }
  298. handler_Exit(sender_id,"sig_PREPARE_ACCEPT_NO_PREVIOUS_handler");
  299. return;
  300. }
  301. /************* The other cores would either accept its proposal if they haven't seen
  302. ************** a higher proposal number
  303. *************/
  304. void sig_PREPARE_ACCEPT_handler(int sender_id){
  305. int replied_proposal_number = sig_read_ar[PROP_NR];
  306. int replied_value = sig_read_ar[VALUE_R];
  307. int k;
  308. int num_of_coworkers;
  309. inter_list tmp_inter_list;
  310. coworkers_list *tmp_cowork_list;
  311. handler_Enter(sender_id, "sig_PREPARE_ACCEPT_handler");
  312. proposer_vars.core_states[sender_id] = sig_read_ar[4];
  313. proposer_vars.cores_promised++;
  314. fprintf(log_file, "\t\t Updated state of %d to %s\n", sender_id, id2string(proposer_vars.core_states[sender_id]));
  315. tmp_inter_list.next = NULL;
  316. /*Save the values if proposal number is higher than the highest replied so far*/
  317. if (replied_proposal_number > proposer_vars.highest_replied_n){
  318. proposer_vars.highest_replied_n = replied_proposal_number;
  319. proposer_vars.highest_replied_value = replied_value;
  320. fprintf(log_file, "\t\t Updated proposer_vars.highest_replied_n = %d\n", proposer_vars.highest_replied_n);
  321. fprintf(log_file, "\t\t Updated proposer_vars.highest_replied_value = %d\n", proposer_vars.highest_replied_value);
  322. }
  323. /* Case where controller fails */
  324. if (idag_mask[faulty_core] == faulty_core){
  325. if (proposer_vars.cores_promised >= majority(CLUSTER_SIZE)){
  326. if (PREPARE_ACCEPT_SENT == 0){
  327. PREPARE_ACCEPT_SENT = 1;
  328. fprintf(log_file,"\t\t-------- LAST KNOWN CORE STATES --------\n");
  329. FOR_NUES{
  330. fprintf(log_file,"\t\t%d --> %s\n",k,id2string(proposer_vars.core_states[k]));
  331. }
  332. fprintf(log_file,"\n");
  333. fprintf(log_file,"\t\t----------------------------------------\n");
  334. fprintf(log_file, "\t\tRECEIVED ACCEPT FROM MAJORITY!\n");
  335. for (k = 0; k < X_max*Y_max; k++){
  336. if ((k != my_idag) && (idag_mask[k] == my_idag)){
  337. tmp_inter_list.type = ACCEPT_REQUEST;
  338. tmp_inter_list.data.accepted_values[PROP_NW] = proposal_number_personal;
  339. tmp_inter_list.data.accepted_values[VALUE_W] = proposer_vars.highest_replied_value;
  340. scc_kill(k,SIG_ACCEPT_REQUEST,&tmp_inter_list);
  341. }
  342. }
  343. }
  344. }
  345. /* Case where manager fails */
  346. }else{
  347. num_of_coworkers = 0;
  348. FOR_MY_COWORKERS_LIST num_of_coworkers++;
  349. fprintf(log_file,"num_of_coworkers=%d\n",num_of_coworkers);
  350. if (proposer_vars.cores_promised >= majority(num_of_coworkers)){
  351. if (PREPARE_ACCEPT_SENT == 0){
  352. PREPARE_ACCEPT_SENT = 1;
  353. fprintf(log_file, "\t\tRECEIVED ACCEPT FROM MAJORITY!\n");
  354. FOR_MY_COWORKERS_LIST{
  355. tmp_inter_list.type = ACCEPT_REQUEST;
  356. tmp_inter_list.data.accepted_values[PROP_NW] = proposal_number_personal;
  357. tmp_inter_list.data.accepted_values[VALUE_W] = proposer_vars.highest_replied_value;
  358. scc_kill(tmp_cowork_list->core_id,SIG_ACCEPT_REQUEST,&tmp_inter_list);
  359. }
  360. }
  361. }
  362. }
  363. handler_Exit(sender_id, "sig_PREPARE_ACCEPT_handler");
  364. return;
  365. }
  366. void sig_ACCEPT_REQUEST_handler(int sender_id){
  367. int proposer_proposal_number = sig_read_ar[PROP_NR];
  368. inter_list tmp_inter_list;
  369. handler_Enter(sender_id,"sig_ACCEPT_REQUEST_handler");
  370. tmp_inter_list.next = NULL;
  371. if (proposer_proposal_number < acceptor_vars.highest_proposed_n){
  372. fprintf(log_file, "\t\t ACCEPT_REQUEST proposal number lower than highest_proposed_n %d -> REJECTED\n", acceptor_vars.highest_proposed_n);
  373. }else{
  374. acceptor_vars.highest_acc_n = proposer_proposal_number;
  375. fprintf(log_file, "\t\t Updated acceptor_vars.highest_acc_n = %d\n", acceptor_vars.highest_acc_n);
  376. acceptor_vars.highest_acc_value = sig_read_ar[VALUE_R];
  377. fprintf(log_file, "\t\t Updated acceptor_vars.highest_acc_value = %d\n", acceptor_vars.highest_acc_value);
  378. acceptor_vars.highest_proposed_n = proposer_proposal_number;
  379. fprintf(log_file, "\t\t Updated acceptor_vars.highest_proposed_n = %d\n", acceptor_vars.highest_proposed_n);
  380. tmp_inter_list.type = ACCEPTED;
  381. tmp_inter_list.data.accepted_values[VALUE_W] = acceptor_vars.highest_acc_value;
  382. scc_kill(sender_id, SIG_ACCEPTED, &tmp_inter_list);
  383. }
  384. handler_Exit(sender_id,"sig_ACCEPT_REQUEST_handler");
  385. return;
  386. }
  387. void sig_ACCEPTED_handler(int sender_id){
  388. int k;
  389. int received_value = sig_read_ar[VALUE_R];
  390. int num_of_coworkers;
  391. inter_list tmp_inter_list;
  392. coworkers_list *tmp_cowork_list;
  393. handler_Enter(sender_id,"sig_ACCEPTED_handler");
  394. proposer_vars.cores_accepted++;
  395. tmp_inter_list.next = NULL;
  396. /****************************************************************/
  397. /***************** Case where controller failed *****************/
  398. /****************************************************************/
  399. if (idag_mask[faulty_core] == faulty_core && idag_mask[faulty_core] != -1){
  400. if ((proposer_vars.cores_accepted >= majority(CLUSTER_SIZE)) && (SIG_LEARN_SENT == 0)){
  401. fprintf(log_file, "\t\tRECEIVED ACCEPTED FROM MAJORITY!\n");
  402. SIG_LEARN_SENT = 1;
  403. tmp_inter_list.type = LEARN;
  404. tmp_inter_list.data.learn_ack_info[VALUE_W] = received_value;
  405. tmp_inter_list.data.learn_ack_info[PREV_CW] = faulty_core;
  406. scc_kill(node_id,SIG_LEARN,&tmp_inter_list);
  407. for (k = 0; k < X_max*Y_max; k++){
  408. if ((k != my_idag) && (k != node_id)){
  409. tmp_inter_list.type = LEARN;
  410. tmp_inter_list.data.learn_ack_info[VALUE_W] = received_value;
  411. tmp_inter_list.data.learn_ack_info[PREV_CW] = faulty_core;
  412. scc_kill(k,SIG_LEARN,&tmp_inter_list);
  413. }
  414. }
  415. }
  416. /****************************************************************/
  417. /******************* Case where manager failed ******************/
  418. /****************************************************************/
  419. }else{
  420. num_of_coworkers=0;
  421. FOR_MY_COWORKERS_LIST num_of_coworkers++;
  422. printf("num_of_coworkers=%d\n",num_of_coworkers);
  423. if ((proposer_vars.cores_accepted >= majority(num_of_coworkers)) && (SIG_LEARN_SENT == 0)){
  424. fprintf(log_file, "\t\t MANAGER CASE ; RECEIVED ACCEPTED FROM MAJORITY!\n");
  425. SIG_LEARN_SENT = 1;
  426. tmp_inter_list.type = LEARN;
  427. tmp_inter_list.data.learn_ack_info[VALUE_W] = received_value;
  428. tmp_inter_list.data.learn_ack_info[PREV_CW] = faulty_core;
  429. scc_kill(node_id,SIG_LEARN,&tmp_inter_list);
  430. for (k = 0; k < X_max*Y_max; k++){
  431. if (k != node_id && k != faulty_core){
  432. tmp_inter_list.type = LEARN;
  433. tmp_inter_list.data.learn_ack_info[VALUE_W] = received_value;
  434. tmp_inter_list.data.learn_ack_info[PREV_CW] = faulty_core;
  435. scc_kill(k,SIG_LEARN,&tmp_inter_list);
  436. }
  437. }
  438. }
  439. }
  440. handler_Exit(sender_id,"sig_ACCEPTED_handler");
  441. return;
  442. }
  443. void sig_LEARN_handler(int sender_id){
  444. int received_value = sig_read_ar[VALUE_R];
  445. int failed_core = sig_read_ar[PREV_CR];
  446. int k;
  447. int i;
  448. int cluster_idag;
  449. int counter;
  450. int selfopt_r;
  451. int failed_interaction = 0; /* 0 nothing, 1 init_search, 2 manager_search */
  452. int one_core;
  453. core_list *tmp_core_list;
  454. core_list *tmp_core_list_prev;
  455. DDS_list *tmp_dds;
  456. DDS_list *tmp_prev_dds;
  457. inter_list tmp_inter_list;
  458. coworkers_list *tmp_cowork_list;
  459. inter_list *tmp_inter;
  460. core_states new_state = NO_PENDING_STATE;
  461. handler_Enter(sender_id,"sig_LEARN_handler");
  462. fprintf(log_file,"\t\t Received_value = %d and failed_core=%d\n",received_value,failed_core);
  463. suspected[received_value] = -1;
  464. //faulty_core = failed_core;
  465. /****************************************************************/
  466. /***************** Case where controller failed *****************/
  467. /****************************************************************/
  468. i = 0;
  469. /* FIXED IDs */
  470. for (i=0; i < X_max*Y_max;i++)
  471. if (idag_mask[i] == failed_core && i != failed_core){
  472. if (i == node_id)
  473. printf("I am the new controller and my current state is: %s\n\n",id2string(state));
  474. break;
  475. }
  476. if (idag_mask[node_id] == idag_mask[failed_core])
  477. printf("%d : %s\n",node_id,id2string(state));
  478. exit(0);
  479. /* I am checking the interactions i had with the new controller */
  480. if (failed_core != node_id && idag_mask[failed_core] != -1){
  481. if (core_inter_head[failed_core] == NULL){
  482. fprintf(log_file,"\t\tI had no interactions with failed core %d\n", failed_core);
  483. }else{
  484. fprintf(log_file, "\t\tMy interactions with failed core %d were:\n",failed_core);
  485. for (tmp_inter = core_inter_head[failed_core]; tmp_inter != NULL; tmp_inter = tmp_inter->next){
  486. fprintf(log_file,"\t\t\t%d. %s\n",i,inter2string(tmp_inter->type));
  487. if (tmp_inter->type == IDAG_FIND_IDAGS ||
  488. tmp_inter->type == IDAG_FIND_IDAGS_PENDING){
  489. failed_interaction = 1;
  490. }
  491. if (tmp_inter->type == SELFOPT_IDAG_FIND_IDAGS ||
  492. tmp_inter->type == SELFOPT_IDAG_FIND_IDAGS_PENDING){
  493. failed_interaction = 2;
  494. }
  495. i++;
  496. }
  497. }
  498. }
  499. if (core_inter_head[failed_core] != NULL &&
  500. (core_inter_head[failed_core]->type == IDAG_REQ_DDS ||
  501. core_inter_head[failed_core]->type == IDAG_REQ_DDS_PENDING ||
  502. core_inter_head[failed_core]->type == SELFOPT_IDAG_REQ_DDS ||
  503. core_inter_head[failed_core]->type == SELFOPT_IDAG_REQ_DDS_PENDING)){
  504. trigger_shit(failed_core);
  505. }
  506. /* Controller Failure and First Time i receive SIG_LEARN */
  507. if (idag_mask[failed_core] == failed_core && idag_mask[failed_core] != -1){
  508. /* Update idag_mask and idag_id_arr in any subcase */
  509. fprintf(log_file,"\t\tUpdating idag_mask and idag_id_arr with new controller %d... ",received_value);
  510. for (k = 0; k < X_max*Y_max; k++){
  511. if (idag_mask[k] == failed_core)
  512. idag_mask[k] = received_value;
  513. }
  514. for (k = 0; k < num_idags; k++){
  515. if (idag_id_arr[k] == failed_core)
  516. idag_id_arr[k] = received_value;
  517. }
  518. idag_mask[failed_core] = -1;
  519. fprintf(log_file,"DONE\n\n");
  520. fprintf(log_file,"\t\tMy interactions with the new controller are:\n");
  521. i = 0;
  522. tmp_inter = core_inter_head[received_value];
  523. while (tmp_inter != NULL && node_id != received_value)
  524. {
  525. fprintf(log_file, "\t\t\t%d. %s...\n",i,inter2string(tmp_inter->type));
  526. tmp_inter = tmp_inter->next;
  527. i++;
  528. }
  529. /***** I am a newly elected controller *****/
  530. coworkers_list *tmp_cowork_list;
  531. int num_of_coworkers;
  532. if (node_id == received_value){
  533. if (tmp_cowork_list != NULL)
  534. FOR_MY_COWORKERS_LIST num_of_coworkers++;
  535. printf("I am the new controller : %d -- Current state : %s - %d!\n", received_value,id2string(state),num_of_coworkers);
  536. fprintf(log_file, "\t\tI am the new controller : %d -- Current state : %s!\n", received_value,id2string(state));
  537. my_idag = -1;
  538. /***** Create my cores list *****/
  539. if (my_cores != NULL){
  540. printf("my_cores list is not NULL...\n");
  541. fprintf(log_file,"\t\tmy_cores list is not NULL...\n");
  542. for (tmp_core_list = my_cores->next; tmp_core_list != NULL; tmp_core_list=tmp_core_list->next){
  543. printf("\t\tCore_id : %d | Offered_to : %d ... %sREMOVED%s\n",my_cores->core_id,my_cores->offered_to,KRED,KNRM);
  544. fprintf(log_file,"\t\t\tCore_id : %d | Offered_to : %d ... %sREMOVED%s\n",my_cores->core_id,my_cores->offered_to,KRED,KNRM);
  545. free(my_cores);
  546. my_cores = tmp_core_list;
  547. }
  548. printf("\t\tCore_id : %d | Offered_to : %d ... %sREMOVED%s\n",my_cores->core_id,my_cores->offered_to,KRED,KNRM);
  549. fprintf(log_file,"\t\t\tCore_id : %d | Offered_to : %d ... %sREMOVED%s\n",my_cores->core_id,my_cores->offered_to,KRED,KNRM);
  550. free(my_cores);
  551. my_cores = NULL;
  552. }
  553. if (my_cores == NULL){
  554. printf("\t\tCreating my_cores list... ");
  555. fprintf(log_file,"\t\tCreating my_cores list... ");
  556. my_cores = (core_list *) malloc(sizeof(core_list));
  557. my_cores_count = 0;
  558. if (my_cores != NULL){
  559. printf("%sSuccess!%s\n",KGRN,KNRM);
  560. my_cores_tail = my_cores;
  561. my_cores_count++;
  562. my_cores_tail->core_id = node_id;
  563. my_cores_tail->offered_to = -1;
  564. my_cores_tail->next = NULL;
  565. my_cores_tail->workload[0] = -1;
  566. my_cores_tail->workload[1] = -1;
  567. printf("\t\t\tAdded Core_id : %d | Offered_to : %d\n",my_cores_tail->core_id,my_cores_tail->offered_to);
  568. fprintf(log_file,"\t\t\tAdded Core_id : %d | Offered_to : %d\n",my_cores_tail->core_id,my_cores_tail->offered_to);
  569. for (i = 0; i < X_max*Y_max; i++){
  570. if (idag_mask[i] == node_id && i != node_id){
  571. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  572. if (my_cores_tail->next != NULL){
  573. my_cores_tail = my_cores_tail->next;
  574. my_cores_tail->next = NULL;
  575. my_cores_tail->core_id = i;
  576. my_cores_tail->offered_to = -1;
  577. printf("\t\t\tAdded Core_id : %d | Offered_to : %d\n",my_cores_tail->core_id,my_cores_tail->offered_to);
  578. fprintf(log_file,"\t\t\tAdded Core_id : %d | Offered_to : %d\n",my_cores_tail->core_id,my_cores_tail->offered_to);
  579. my_cores_count++;
  580. }else printf("--%d-- error allocating memory for my_cores\n",node_id);
  581. }
  582. }
  583. }else{
  584. printf("--%d-- error allocating memory for my_cores\n",node_id);
  585. fprintf(log_file, "--%d-- error allocating memory for my_cores\n",node_id);
  586. }
  587. }
  588. /***** Create my DDS List *****/
  589. if (DDS == DDS_tail && DDS != NULL){
  590. printf("\t\tDDS = DDS_tail with value: \n\t\t\tAgent_id : %d | Cores in cluster : %d\n",DDS->agent_id,DDS->num_of_cores);
  591. printf("\t\tReconfiguring DDS...\n");
  592. fprintf(log_file,"\t\tDDS = DDS_tail with value: \n\t\t\tAgent_id : %d | Cores in cluster : %d\n",DDS->agent_id,DDS->num_of_cores);
  593. fprintf(log_file,"\t\tReconfiguring DDS...\n");
  594. free(DDS);
  595. DDS = NULL;
  596. DDS_count = 0;
  597. }
  598. if (DDS == NULL){
  599. printf("\t\tCreating DDS list... ");
  600. fflush(stdout);
  601. DDS = (DDS_list *) malloc(sizeof(DDS_list));
  602. DDS_count = 0;
  603. if (DDS != NULL){
  604. printf("%sSuccess!%s\n",KGRN,KNRM);
  605. fflush(stdout);
  606. DDS->agent_id = node_id;
  607. DDS->next = NULL;
  608. DDS_tail = DDS;
  609. DDS_count++;
  610. DDS->num_of_cores = my_cores_count;
  611. printf("\t\t\tAdded Agent_id : %d | Cores in cluster : %d\n\n",DDS->agent_id, DDS->num_of_cores);
  612. fprintf(log_file,"\t\t\tAdded Agent_id : %d | Cores in cluster : %d\n\n",DDS->agent_id, DDS->num_of_cores);
  613. }else{
  614. printf("--%d-- error allocating memory for my_cores\n",node_id);
  615. fprintf(log_file,"--%d-- error allocating memory for my_cores\n",node_id);
  616. }
  617. }else{
  618. DDS_list *tmp_dds;
  619. printf("\t\tDDS list of %d:\n",node_id);
  620. fprintf(log_file,"\t\tDDS list of %d:\n",node_id);
  621. tmp_dds = DDS;
  622. while (tmp_dds != NULL){
  623. printf("\t\t\tAgent_id : %d | Cores in cluster : %d\n",tmp_dds->agent_id, tmp_dds->num_of_cores);
  624. fprintf(log_file,"\t\t\tAgent_id : %d | Cores in cluster : %d\n",tmp_dds->agent_id, tmp_dds->num_of_cores);
  625. tmp_dds = tmp_dds->next;
  626. }
  627. printf("\n\n");
  628. }
  629. paxos_state = NEW_IDAG;
  630. fprintf(log_file,"Changed Paxos State to %s\n", id2string(paxos_state));
  631. }
  632. /***** I am in the same cluster as the new controller *****/
  633. else if ((my_idag != -1) && (my_idag == failed_core)){
  634. fprintf(log_file, "\t\t I am in the same cluster as %d. My new controller is %d\n", sender_id, received_value);
  635. my_idag = received_value;
  636. if (failed_interaction == 1) {
  637. fprintf(log_file,"I have to resend signal SIG_IDAG_FIND_IDAGS to new controller\n");
  638. if (core_inter_head[my_idag] == NULL){
  639. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  640. core_inter_tail[my_idag] = core_inter_head[my_idag];
  641. } else {
  642. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  643. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  644. }
  645. selfopt_r = (int) (1.5 * (X_max / num_idags_x));
  646. core_inter_tail[my_idag]->type = IDAG_FIND_IDAGS_PENDING;
  647. core_inter_tail[my_idag]->data.reg.C = node_id-1;
  648. core_inter_tail[my_idag]->data.reg.r = selfopt_r;
  649. core_inter_tail[my_idag]->next = NULL;
  650. if (core_inter_head[my_idag]->next == NULL) {
  651. paxos_node_stats.msg_count++;
  652. scc_kill(my_idag, SIG_IDAG_FIND_IDAGS, core_inter_head[my_idag]);
  653. } else {
  654. fprintf(log_file, "paxos_signal_handlers.c: Did not send idag_find_idags with interaction %s no2 %s\n",inter2string(core_inter_head[my_idag]->type),inter2string(core_inter_head[my_idag]->next->type));
  655. }
  656. if (selfopt_time_rem != -1) {
  657. selfopt_time_rem = my_gettimer();
  658. if (selfopt_time_rem > 0)
  659. my_settimer(0);
  660. }
  661. if (pending_state == WORKING_NODE) {
  662. fprintf(log_file, "I change to working idle init\n");
  663. state = WORKING_NODE;
  664. pending_state = IDLE_INIT_MAN;
  665. }else{
  666. state = IDLE_INIT_MAN;
  667. }
  668. }
  669. if (failed_interaction == 2){
  670. if (core_inter_head[my_idag] == NULL){
  671. core_inter_head[my_idag] = (inter_list *) malloc(sizeof(inter_list));
  672. core_inter_tail[my_idag] = core_inter_head[my_idag];
  673. } else {
  674. core_inter_tail[my_idag]->next = (inter_list *) malloc(sizeof(inter_list));
  675. core_inter_tail[my_idag] = core_inter_tail[my_idag]->next;
  676. }
  677. selfopt_r = (int) (1.5 * (X_max / num_idags_x));
  678. core_inter_tail[my_idag]->type = SELFOPT_IDAG_FIND_IDAGS_PENDING;
  679. core_inter_tail[my_idag]->data.reg.C = node_id;
  680. core_inter_tail[my_idag]->data.reg.r = selfopt_r;
  681. core_inter_tail[my_idag]->next = NULL;
  682. if (core_inter_head[my_idag]->next == NULL){
  683. paxos_node_stats.msg_count++;
  684. scc_kill(my_idag, SIG_IDAG_FIND_IDAGS, core_inter_head[my_idag]);
  685. } else {
  686. fprintf(log_file,"paxos_signal_handlers.c: Did not send sig_find_idags with inter1 = %s, inter2 = %s\n",inter2string(core_inter_head[my_idag]->type),inter2string(core_inter_head[my_idag]->next->type));
  687. }
  688. if (selfopt_interval != MAX_SELF_OPT_INTERVAL_MS){
  689. selfopt_interval = 2 * selfopt_interval;
  690. }else{
  691. selfopt_interval = -1;
  692. }
  693. new_state = IDLE_AGENT_WAITING_OFF;
  694. }
  695. }
  696. /***** I am a controller so i have to update idag_id_arr and reply with LEARN_ACK_CONTR *****/
  697. /*else if (im_controller() == 1){
  698. for (k = 0; k < num_idags; k++)
  699. if (idag_id_arr[k] == failed_core)
  700. idag_id_arr[k] = received_value;
  701. else if (idag_id_arr[k] == node_id)
  702. tmp_inter_list.data.controller_index = k;
  703. fprintf(log_file, "\t\tI am a Controller. Changed controller %d with %d\n",failed_core,sender_id);
  704. tmp_inter_list.next = NULL;
  705. tmp_inter_list.type = LEARN_ACK_CONTR;
  706. scc_kill(received_value,SIG_LEARN_ACK_CONTR,&tmp_inter_list);
  707. fprintf(log_file, "\t\tUpdated idag_id_arr\n");
  708. }*/
  709. /* I am a manager so i have to reply with ADD_AGENT_TO_DDS. In addition if the new controller was a worker of mine i remove him from my_cores list */
  710. if (im_manager() == 1){
  711. if (new_state == IDLE_AGENT_WAITING_OFF) state = IDLE_AGENT_WAITING_OFF;
  712. counter = 0;
  713. FOR_MY_CORES_LIST{
  714. cluster_idag = idag_mask[tmp_core_list->core_id];
  715. if (cluster_idag == received_value){
  716. fprintf(log_file,"\t\tI am manager %d and my core %d utilizes in cluster with idag %d\n", node_id, tmp_core_list->core_id,cluster_idag);
  717. /* if the new controller was a worker of mine i do not send him */
  718. if (tmp_core_list->core_id != received_value)
  719. tmp_inter_list.data.workers_info[++counter] = tmp_core_list->core_id;
  720. }
  721. }
  722. if (counter > 0){
  723. tmp_inter_list.next = NULL;
  724. tmp_inter_list.type = ADD_TO_DDS;
  725. tmp_inter_list.data.workers_info[0] = counter;
  726. fprintf(log_file,"\t\tNUMBER OF WORKERS: %d\n", counter);
  727. scc_kill(received_value,SIG_ADD_TO_DDS,&tmp_inter_list);
  728. }
  729. /***** If the new controller was a worker of mine i remove him from my_cores list and reappoing workload *****/
  730. tmp_core_list = my_cores->next;
  731. tmp_core_list_prev = my_cores;
  732. while (tmp_core_list != NULL){
  733. if (tmp_core_list->core_id == received_value){
  734. fprintf(log_file,"\t\tNew controller was a worker of mine! I remove him from my cores_list!\n");
  735. my_cores_count--;
  736. tmp_core_list_prev->next = tmp_core_list->next;
  737. pending_workload[0] = tmp_core_list->workload[0];
  738. pending_workload[1] = tmp_core_list->workload[1];
  739. fprintf(log_file,"Pending workload of new controller was: %d %d\n", pending_workload[0], pending_workload[1]);
  740. free(tmp_core_list);
  741. if (pending_workload[0] != -1 || pending_workload[1] != -1){
  742. //active_working_cores--;
  743. tmp_core_list = my_cores->next;
  744. fprintf(log_file,"\t\tI am reassigning the workload!\n");
  745. fprintf(log_file,"\t\t-------- CURRENT WORKLOADS --------\n");
  746. while (tmp_core_list != NULL){
  747. fprintf(log_file,"\t\t%d\t|\t%d\t|\t%d\n", tmp_core_list->core_id, tmp_core_list->workload[0], tmp_core_list->workload[1]);
  748. printf("\t\t%d\t|\t%d\t|\t%d\n", tmp_core_list->core_id, tmp_core_list->workload[0], tmp_core_list->workload[1]);
  749. tmp_core_list = tmp_core_list->next;
  750. }
  751. fprintf(log_file,"\t\t-----------------------------------\n");
  752. tmp_core_list = my_cores->next;
  753. while (tmp_core_list != NULL){
  754. if ((tmp_core_list->workload[0] == -1) && (tmp_core_list->workload[1] == -1)){
  755. one_core = tmp_core_list->core_id;
  756. fprintf(log_file,"\t\tpaxos_signal_handlers.c : I have pending workload %d | %d\n",pending_workload[0],pending_workload[1]);
  757. fprintf(log_file,"\t\tpaxos_signal_handlers.c : I am assigning workload to %d\n",one_core);
  758. if (core_inter_head[one_core] == NULL){
  759. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  760. core_inter_tail[one_core] = core_inter_head[one_core];
  761. } else {
  762. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  763. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  764. }
  765. core_inter_tail[one_core]->type = APPOINT_WORK_NODE;
  766. core_inter_tail[one_core]->data.work_bounds[0] = pending_workload[0];
  767. core_inter_tail[one_core]->data.work_bounds[1] = pending_workload[1];
  768. fprintf(app_log_file,"%d (%d, %d), ",one_core,core_inter_tail[one_core]->data.work_bounds[0],core_inter_tail[one_core]->data.work_bounds[1]);
  769. core_inter_tail[one_core]->next = NULL;
  770. if (core_inter_head[one_core]->next == NULL) {
  771. paxos_node_stats.msg_count++;
  772. scc_kill(one_core, SIG_APPOINT_WORK, core_inter_head[one_core]);
  773. } else {
  774. fprintf(log_file,"I am doing smth else with my working node %d in init inter1 = %d inter2 = %d\n",one_core,core_inter_head[one_core]->type,core_inter_head[one_core]->next->type);
  775. printf("ASDASDASDASDASDAS\n");
  776. }
  777. pending_workload[0] = -1;
  778. pending_workload[1] = -1;
  779. break;
  780. }
  781. tmp_core_list = tmp_core_list->next;
  782. }
  783. break;
  784. }
  785. }else{
  786. tmp_core_list = tmp_core_list->next;
  787. tmp_core_list_prev = tmp_core_list_prev->next;
  788. }
  789. }
  790. }
  791. /****************************************************************/
  792. /******************* Case where manager failed ******************/
  793. /****************************************************************/
  794. }
  795. else{
  796. /***** I am the newly elected manager *****/
  797. if (node_id == received_value && idag_mask[failed_core] != -1){
  798. idag_mask[failed_core] = -1;
  799. printf("I am the new manager : %d -- Current state : %s!\n", received_value,id2string(state));
  800. if (my_cores != NULL){
  801. printf("my_cores list is not NULL...\n");
  802. for (tmp_core_list = my_cores->next; tmp_core_list != NULL; tmp_core_list=tmp_core_list->next){
  803. printf("\t\tCore_id : %d | Offered_to : %d ... %sREMOVED%s\n",my_cores->core_id,my_cores->offered_to,KRED,KNRM);
  804. free(my_cores);
  805. my_cores = tmp_core_list;
  806. }
  807. printf("\t\tCore_id : %d | Offered_to : %d ... %sREMOVED%s\n",my_cores->core_id,my_cores->offered_to,KRED,KNRM);
  808. free(my_cores);
  809. my_cores = NULL;
  810. }
  811. if (my_cores == NULL){
  812. printf("\t\tCreating my_cores list... ");
  813. fflush(stdout);
  814. my_cores = (core_list *) malloc(sizeof(core_list));
  815. my_cores_count = 0;
  816. if (my_cores != NULL){
  817. printf("%sSuccess!%s\n",KGRN,KNRM);
  818. my_cores_tail = my_cores;
  819. my_cores_tail->core_id = node_id;
  820. my_cores_tail->offered_to = -1;
  821. my_cores_tail->workload[0] = -1;
  822. my_cores_tail->workload[1] = -1;
  823. my_cores_tail->next = NULL;
  824. my_cores_count++;
  825. printf("\t\t\tAdded Core_id : %d | Offered_to : %d\n",my_cores_tail->core_id,my_cores_tail->offered_to);
  826. FOR_MY_COWORKERS_LIST{
  827. my_cores_tail->next = (core_list *) malloc(sizeof(core_list));
  828. if (my_cores_tail->next != NULL){
  829. my_cores_tail = my_cores_tail->next;
  830. my_cores_tail->next = NULL;
  831. my_cores_tail->core_id = tmp_cowork_list->core_id;
  832. my_cores_tail->offered_to = -1;
  833. my_cores_tail->workload[0] = -1;
  834. my_cores_tail->workload[1] = -1;
  835. printf("\t\t\tAdded Core_id : %d | Offered_to : %d\n",my_cores_tail->core_id,my_cores_tail->offered_to);
  836. my_cores_count++;
  837. }else printf("--%d-- error allocating memory for my_cores\n",node_id);
  838. }
  839. }else printf("--%d-- error allocating memory for my_cores\n",node_id);
  840. }
  841. /**** I have to send SIG_ADD_TO_DDS to the controllers of my workers ****/
  842. for (i = 0; i < X_max*Y_max; i++){
  843. if (idag_mask[i] == i){
  844. counter = 0;
  845. cluster_idag = idag_mask[i];
  846. FOR_MY_CORES_LIST{
  847. if (cluster_idag == idag_mask[tmp_core_list->core_id]){
  848. fprintf(log_file,"\t\tI am manager %d and my core %d utilizes in cluster with idag %d\n", node_id, tmp_core_list->core_id,cluster_idag);
  849. tmp_inter_list.data.workers_info[++counter] = tmp_core_list->core_id;
  850. }
  851. }
  852. if (counter > 0){
  853. tmp_inter_list.next = NULL;
  854. tmp_inter_list.type = ADD_TO_DDS;
  855. tmp_inter_list.data.workers_info[0] = counter;
  856. fprintf(log_file,"\t\tNUMBER OF WORKERS: %d\n", counter);
  857. scc_kill(i,SIG_ADD_TO_DDS,&tmp_inter_list);
  858. }
  859. }
  860. }
  861. state = AGENT_INIT_STATE;
  862. paxos_state = NEW_AGENT;
  863. printf("I was working for app: %d\n", worker_app_id);
  864. my_app.id = worker_app_id;
  865. my_app.num_of_cores = my_cores_count-1;
  866. find_app_info();
  867. #ifndef ARTIFICIAL_APPS_SIM
  868. printf("Found array size = %d\n", my_app.array_size);
  869. fprintf(log_file,"Found array size = %d\n", my_app.array_size);
  870. #endif
  871. printf("Found remaining workload = %d\n", my_app.workld);
  872. fprintf(log_file,"Found remaining workload = %d\n", my_app.workld);
  873. printf("App number of cores = %d\n", my_app.num_of_cores);
  874. fprintf(log_file,"App number of cores = %d\n", my_app.num_of_cores);
  875. FOR_MY_CORES_LIST{
  876. fprintf(log_file,"\t\t\tWorker_id : %d | Workload : %d %d\n", tmp_core_list->core_id, tmp_core_list->workload[0], tmp_core_list->workload[1]);
  877. printf("\t\t\tWorker_id : %d | Workload : %d %d\n", tmp_core_list->core_id, tmp_core_list->workload[0], tmp_core_list->workload[1]);
  878. }
  879. /***** I am controller i have to remove the failed_core from my DDS and cores list *****/
  880. }else if (my_idag == -1){
  881. if (idag_mask[failed_core] == node_id){
  882. printf("--%d-- I received SIG_LEARN from %d\n",node_id,sender_id);
  883. tmp_core_list = my_cores->next;
  884. tmp_core_list_prev = my_cores;
  885. while (tmp_core_list != NULL){
  886. if (tmp_core_list->core_id == failed_core){
  887. my_cores_count--;
  888. tmp_core_list_prev->next = tmp_core_list->next;
  889. free(tmp_core_list);
  890. break;
  891. }
  892. tmp_core_list = tmp_core_list->next;
  893. tmp_core_list_prev = tmp_core_list_prev->next;
  894. }
  895. printf("\t\tUpdated my_cores list:\n");
  896. fprintf(log_file,"\t\tUpdated my_cores list:\n");
  897. counter = 0;
  898. FOR_MY_CORES_LIST{
  899. if (tmp_core_list->offered_to == failed_core){
  900. tmp_core_list->offered_to = -1;
  901. counter++;
  902. }
  903. printf("\t\t\tCore_id : %d | Offered_to : %d\n",tmp_core_list->core_id,tmp_core_list->offered_to);
  904. fprintf(log_file,"\t\t\tCore_id : %d | Offered_to : %d\n",tmp_core_list->core_id,tmp_core_list->offered_to);
  905. }
  906. printf("\t\t\tmy_cores_count = %d\n",my_cores_count);
  907. tmp_dds = DDS->next;
  908. tmp_prev_dds = DDS;
  909. while (tmp_dds != NULL){
  910. if (tmp_dds->agent_id == failed_core){
  911. fprintf(log_file,"\t\t Removed failed core %d from DDS\n",tmp_dds->agent_id);
  912. tmp_prev_dds->next = tmp_dds->next;
  913. if (tmp_dds->next == NULL){
  914. DDS_tail = tmp_prev_dds;
  915. }
  916. DDS_count--;
  917. free(tmp_dds);
  918. DDS->num_of_cores = DDS->num_of_cores + counter;
  919. break;
  920. }else{
  921. tmp_prev_dds = tmp_dds;
  922. tmp_dds = tmp_dds->next;
  923. }
  924. }
  925. printf("\t\tUpdated DDS list:\n");
  926. fprintf(log_file,"\t\tUpdated DDS list:\n");
  927. FOR_MY_DDS_LIST{
  928. printf("\t\t\tAgent_id : %d | Cores in cluster : %d\n",tmp_dds->agent_id,tmp_dds->num_of_cores);
  929. fprintf(log_file,"\t\t\tAgent_id : %d | Cores in cluster : %d\n",tmp_dds->agent_id,tmp_dds->num_of_cores);
  930. }
  931. printf("\t\t\tDDS_count = %d\n",DDS_count);
  932. }
  933. /***** I was working for the failed manager *****/
  934. }else if (cur_agent.my_agent == failed_core){
  935. cur_agent.my_agent = -1;
  936. }
  937. }
  938. cur_time = time(NULL);
  939. cur_t = localtime(&cur_time);
  940. fprintf(log_file, "[%d:%d:%d]: I ended sig_LEARN_handler with sender = %d state = %s\n\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  941. //exit(0);
  942. return;
  943. }
  944. void sig_REINIT_APP_handler(int sender_id){
  945. inter_list *tmp_inter_list;
  946. cur_time = time(NULL);
  947. cur_t = localtime(&cur_time);
  948. fprintf(log_file, "\n\n[%d:%d:%d]: I entered sig_REINIT_APP_handler with sender = %d state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  949. printf("Received SIG_REINIT_APP from %d\n", sender_id);
  950. //int i, data_array_local[LINE_SIZE];
  951. for (tmp_inter_list = core_inter_head[0]; tmp_inter_list != NULL; tmp_inter_list = tmp_inter_list->next)
  952. if (tmp_inter_list->type == INIT_APP) {
  953. printf("i not null\n");
  954. break;
  955. }
  956. if (core_inter_head[12] == NULL){
  957. core_inter_head[12] = (inter_list *)malloc(sizeof(inter_list));
  958. core_inter_tail[12] = core_inter_head[12];
  959. }else{
  960. core_inter_tail[12]->next = (inter_list *)malloc(sizeof(inter_list));
  961. core_inter_tail[12] = core_inter_tail[12]->next;
  962. }
  963. core_inter_tail[12]->next = NULL;
  964. core_inter_tail[12]->type = INIT_APP;
  965. core_inter_tail[12]->data.new_app.id = sig_read_ar[2];
  966. core_inter_tail[12]->data.new_app.num_of_cores = sig_read_ar[3];
  967. core_inter_tail[12]->data.new_app.workld = sig_read_ar[4];
  968. #ifndef ARTIFICIAL_APPS_SIM
  969. core_inter_tail[12]->data.new_app.array_size = sig_read_ar[5];
  970. #endif
  971. if (core_inter_head[12]->next == NULL){
  972. scc_kill(12, SIG_INIT_APP, core_inter_head[12]);
  973. }
  974. cur_time = time(NULL);
  975. cur_t = localtime(&cur_time);
  976. fprintf(log_file, "[%d:%d:%d]: I ended sig_REINIT_APP_handler with sender = %d state=%s\n\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  977. return;
  978. }
  979. void find_app_info(){
  980. //int sz;
  981. char app_log_file_name[64];
  982. char buffer[64];
  983. int temp;
  984. strcpy(app_log_file_name, "../");
  985. strcat(app_log_file_name,local_scen_directory);
  986. strcat(app_log_file_name, "/");
  987. strcat(app_log_file_name,local_scen_num);
  988. strcat(app_log_file_name,"/app_logs/");
  989. strcat(app_log_file_name, itoa(worker_app_id));
  990. strcat(app_log_file_name, ".txt");
  991. printf("Trying to open file %s... ", app_log_file_name);
  992. if ((app_log_file = fopen(app_log_file_name, "r")) == NULL){
  993. printf("%sError%s\n",KRED,KNRM);
  994. printf("paxos_signal_handlers.c : Cannot open input file with file path = %s ",app_log_file_name);
  995. perror("open app_log_file");
  996. }else{
  997. printf("%sSuccess%s\n",KGRN,KNRM);
  998. }
  999. while (fscanf(app_log_file,"%s", buffer) != EOF){
  1000. #ifndef ARTIFICIAL_APPS_SIM
  1001. if (strcmp(buffer,"array_size") == 0){
  1002. fscanf(app_log_file,"%s",buffer);
  1003. fscanf(app_log_file,"%d", &temp);
  1004. my_app.array_size = temp;
  1005. }
  1006. #else
  1007. /* FIXME must locate var and A */
  1008. #endif
  1009. if (strcmp(buffer,"workload") == 0){
  1010. fscanf(app_log_file,"%s",buffer);
  1011. fscanf(app_log_file,"%d",&temp);
  1012. my_app.workld = temp;
  1013. }
  1014. }
  1015. fclose(app_log_file);
  1016. }
  1017. void rollback(){
  1018. offer_list *tmp_offer_list;
  1019. inter_list tmp_inter_list;
  1020. core_list *tmp_core_list;
  1021. int core_idag;
  1022. cur_time = time(NULL);
  1023. cur_t = localtime(&cur_time);
  1024. tmp_inter_list.next = NULL;
  1025. fprintf(log_file, "Rolling back... %s\n", id2string(state));
  1026. //If i am the new controller and i have an app to initialize i send SIG_REINIT_APP to 0.
  1027. if ((paxos_state == NEW_IDAG) && (state == INIT_MANAGER || state == INIT_MANAGER_SEND_OFFERS || state == IDLE_INIT_MAN || state == INIT_MAN_CHK_OFFERS || pending_state == INIT_MANAGER
  1028. || pending_state == INIT_MANAGER_SEND_OFFERS || pending_state == INIT_MAN_CHK_OFFERS || pending_state == AGENT_INIT_CHK_OFFERS || pending_state == IDLE_INIT_MAN
  1029. || pending_state == IDLE_INIT_IDLE_AGENT || pending_state == IDLE_INIT_AGENT_SELFOPT || pending_state == INIT_CHK_OFFERS_IDLE_AGENT || pending_state == INIT_CHK_OFFERS_SELFOPT)){
  1030. printf("i am the new controller and i have an app to initialize i send SIG_REINIT_APP to 0\n");
  1031. if (init_man_offers != NULL){
  1032. tmp_offer_list = init_man_offers;
  1033. while (tmp_offer_list != NULL){
  1034. *tmp_offer_list->answer = 0;
  1035. tmp_offer_list = tmp_offer_list->next;
  1036. }
  1037. }
  1038. while (init_man_offers != NULL){
  1039. if (core_inter_head[init_man_offers->sender] != NULL) {
  1040. if (core_inter_head[init_man_offers->sender]->type == REP_AGENT_OFFER_PENDING){
  1041. core_inter_head[init_man_offers->sender]->type = REP_AGENT_OFFER_SENT;
  1042. //kill(pid_num[init_man_offers->sender], SIG_REP_OFFERS);
  1043. paxos_node_stats.msg_count++;
  1044. scc_kill(init_man_offers->sender, SIG_REP_OFFERS, core_inter_head[init_man_offers->sender]);
  1045. //my_stats.msg_count++;
  1046. //my_stats.distance += distance(node_id,init_man_offers->sender);
  1047. } else {
  1048. printf("gamietai b = %d",init_man_offers->sender);
  1049. fprintf(log_file,"gamietai b = %d",init_man_offers->sender);
  1050. }
  1051. tmp_offer_list = init_man_offers;
  1052. init_man_offers = init_man_offers->next;
  1053. free(tmp_offer_list);
  1054. }
  1055. }
  1056. fprintf(log_file, "Replied to all my offers negatively\n");
  1057. tmp_inter_list.type = REINIT_APP;
  1058. tmp_inter_list.data.reappointed_app = init_app;
  1059. scc_kill(0,SIG_REINIT_APP,&tmp_inter_list);
  1060. }
  1061. /* If i were a manager i have to inform that i am no longer manager and also add other managers to my DDS */
  1062. else if (im_manager() == 1){
  1063. printf("New controller was a manager before paxos! Remove him from dds lists and create his dds list...\n");
  1064. //TODO remove from dds and add managers to dds
  1065. tmp_core_list = my_cores;
  1066. while (tmp_core_list != NULL){
  1067. //idag_mask[tmp_core_list->core_id] -> idag id
  1068. core_idag = idag_mask[tmp_core_list->core_id];
  1069. tmp_inter_list.next = NULL;
  1070. tmp_inter_list.type = REMOVE_FROM_DDS;
  1071. scc_kill(core_idag, SIG_REMOVE_FROM_DDS, &tmp_inter_list);
  1072. tmp_core_list = tmp_core_list->next;
  1073. }
  1074. }else
  1075. //printf("New controller was an idle core before paxos! Just create his dds list...\n");
  1076. //TODO add managers to dds
  1077. //my_cores = NULL;
  1078. return;
  1079. }
  1080. void sig_ADD_TO_DDS_handler(int sender_id, int *inc_cnt, int cur_index_top){
  1081. DDS_list *tmp_dds = NULL;
  1082. core_list *tmp_core_list;
  1083. int num_of_workers = 0, flag = 0, current = 0;
  1084. handler_Enter(sender_id,"sig_ADD_TO_DDS_handler");
  1085. num_of_workers = sig_read_ar[2];
  1086. fprintf(log_file,"--%d-- [%d:%d:%d]:I received SIG_ADD_TO_DDS from %d with num_of_workers = %d\n",node_id,cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,num_of_workers);
  1087. if (num_of_workers > 5){
  1088. *inc_cnt = *inc_cnt + 1;
  1089. #ifdef PLAT_SCC
  1090. error = RCCE_get((t_vcharp)(&sig_read_ar[LINE_SIZE]), (t_vcharp)(&sig_array[(cur_index_top+1)*LINE_SIZE]), LINE_SIZE * sizeof(int), node_id);
  1091. if (error != RCCE_SUCCESS) {
  1092. RCCE_error_string(error, error_str, &str_len);
  1093. fprintf(log_file,"I got an error in get data in sig_ADD_CORES_DDS_handler from %d with descr %s\n",sender_id,error_str);
  1094. fflush(log_file);
  1095. }
  1096. #else
  1097. new_RCCE_get(sig_read_ar, sig_array, cur_index_top, LINE_SIZE, node_id);
  1098. #endif
  1099. }
  1100. if (DDS == NULL){
  1101. DDS_count=0;
  1102. DDS = (DDS_list *) malloc(sizeof(DDS_list));
  1103. DDS->agent_id = node_id;
  1104. DDS->next = NULL;
  1105. DDS_tail = DDS;
  1106. DDS_count++;
  1107. flag = 0;
  1108. }else{
  1109. FOR_MY_DDS_LIST{
  1110. if (tmp_dds->agent_id == sender_id){
  1111. fprintf(log_file,"\t\t%d is already in my DDS. %d->num_of_cores++ && DDS->num_of_cores--\n",sender_id,sender_id);
  1112. DDS->num_of_cores--;
  1113. tmp_dds->num_of_cores++;
  1114. break;
  1115. flag = 1;
  1116. }
  1117. }
  1118. }
  1119. if (flag == 0){ /* Sender was not in my DDS */
  1120. fprintf(log_file,"\t\t%d is not in my DDS. DDS_count++\n",sender_id);
  1121. DDS_tail->next = (DDS_list *)malloc(sizeof(DDS_list));
  1122. DDS_tail = DDS_tail->next;
  1123. DDS_tail->next = NULL;
  1124. DDS_tail->agent_id = sender_id;
  1125. DDS_tail->num_of_cores = num_of_workers;
  1126. DDS->num_of_cores = DDS->num_of_cores - num_of_workers;
  1127. DDS_count++;
  1128. }
  1129. fprintf(log_file, "\t\t%d utilizes %d cores in my cluster\n",sender_id, num_of_workers);
  1130. while (num_of_workers > 0){
  1131. current = sig_read_ar[2+num_of_workers];
  1132. FOR_MY_CORES_LIST{
  1133. if (tmp_core_list->core_id == current){
  1134. fprintf(log_file,"\t\tChanged %d->offered_to = %d\n",tmp_core_list->core_id,sender_id);
  1135. tmp_core_list->offered_to = sender_id;
  1136. }
  1137. }
  1138. num_of_workers--;
  1139. }
  1140. printf("\t\tUpdated my_cores list:\n");
  1141. fprintf(log_file,"\t\tUpdated my_cores list:\n");
  1142. FOR_MY_CORES_LIST{
  1143. printf("\t\t\tCore_id : %d | Offered_to : %d\n",tmp_core_list->core_id,tmp_core_list->offered_to);
  1144. fprintf(log_file,"\t\t\tCore_id : %d | Offered_to : %d\n",tmp_core_list->core_id,tmp_core_list->offered_to);
  1145. }
  1146. printf("\t\tUpdated DDS list:\n");
  1147. fprintf(log_file,"\t\tUpdated DDS list:\n");
  1148. FOR_MY_DDS_LIST{
  1149. printf("\t\t\tAgent_id : %d | Cores in cluster : %d\n",tmp_dds->agent_id,tmp_dds->num_of_cores);
  1150. fprintf(log_file,"\t\t\tAgent_id : %d | Cores in cluster : %d\n",tmp_dds->agent_id,tmp_dds->num_of_cores);
  1151. }
  1152. cur_time = time(NULL);
  1153. cur_t = localtime(&cur_time);
  1154. fprintf(log_file, "\n[%d:%d:%d]: I ended sig_ADD_TO_DDS_handler with sender = %d state = %s\n\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  1155. return;
  1156. }
  1157. void sig_REMOVE_FROM_DDS_handler(int sender_id){
  1158. core_list *tmp_core_list;
  1159. DDS_list *tmp_dds, *tmp_prev_dds;
  1160. cur_time = time(NULL);
  1161. cur_t = localtime(&cur_time);
  1162. fprintf(log_file, "\n\n[%d:%d:%d]: I entered sig_REMOVE_FROM_DDS_handler with sender = %d state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  1163. tmp_core_list = my_cores;
  1164. while (tmp_core_list != NULL){
  1165. if (tmp_core_list->offered_to == sender_id){
  1166. fprintf(log_file,"\t\tChanged %d->offered_to to -1\n",tmp_core_list->core_id);
  1167. tmp_core_list->offered_to = -1;
  1168. }
  1169. tmp_core_list = tmp_core_list->next;
  1170. }
  1171. tmp_dds = DDS->next;
  1172. tmp_prev_dds = DDS;
  1173. while (tmp_dds != NULL){
  1174. if (tmp_dds->agent_id == sender_id){
  1175. fprintf(log_file,"\t\t Removed %d from DDS\n",tmp_dds->agent_id);
  1176. tmp_prev_dds->next = tmp_dds->next;
  1177. if (tmp_dds->next == NULL){
  1178. DDS_tail = tmp_prev_dds;
  1179. }
  1180. DDS_count--;
  1181. free(tmp_dds);
  1182. break;
  1183. }else{
  1184. tmp_prev_dds = tmp_dds;
  1185. tmp_dds = tmp_dds->next;
  1186. }
  1187. }
  1188. cur_time = time(NULL);
  1189. cur_t = localtime(&cur_time);
  1190. fprintf(log_file, "[%d:%d:%d]: I ended sig_REMOVE_FROM_DDS_handler with sender = %d state = %s\n\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  1191. return;
  1192. }
  1193. void sig_CONTR_TO_handler(int sender_id){
  1194. cur_time = time(NULL);
  1195. cur_t = localtime(&cur_time);
  1196. fprintf(log_file, "\n\n[%d:%d:%d]: I entered sig_CONTR_TO_handler with sender=%d state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  1197. faulty_core = my_idag;
  1198. if (paxos_state != PAXOS_ACTIVE && paxos_state != NEW_AGENT && paxos_state != NEW_IDAG)
  1199. sig_PAXOS_INIT_handler();
  1200. cur_time = time(NULL);
  1201. cur_t = localtime(&cur_time);
  1202. fprintf(log_file, "[%d:%d:%d]: I ended sig_CONTR_TO_handler with sender = %d state = %s\n\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  1203. return;
  1204. }
  1205. void sig_HEARTBEAT_REQ_handler(int sender_id){
  1206. inter_list tmp_inter_list;
  1207. cur_time = time(NULL);
  1208. cur_t = localtime(&cur_time);
  1209. //fprintf(log_file, "[%d:%d:%d]: I entered sig_HEARTBEAT_REQ_handler with sender=%d state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  1210. tmp_inter_list.next = NULL;
  1211. tmp_inter_list.type = HEARTBEAT_REP;
  1212. scc_kill(sender_id,SIG_HEARTBEAT_REP,&tmp_inter_list);
  1213. cur_time = time(NULL);
  1214. cur_t = localtime(&cur_time);
  1215. return;
  1216. }
  1217. void sig_HEARTBEAT_REP_handler(int sender_id){
  1218. cur_time = time(NULL);
  1219. cur_t = localtime(&cur_time);
  1220. //fprintf(log_file, "[%d:%d:%d]: I entered sig_HEARTBEAT_REP_handler with sender = %d state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  1221. alive[sender_id] = 1;
  1222. suspected[sender_id] = 0;
  1223. cur_time = time(NULL);
  1224. cur_t = localtime(&cur_time);
  1225. //fprintf(log_file, "[%d:%d:%d]: I ended sig_TERMINATE_handler with sender = %d state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  1226. return;
  1227. }
  1228. void sig_PFD_TIMER_handler(int signo, siginfo_t *info, void *context){
  1229. int i, j, failed_core, pending_workload[2], one_core;
  1230. DDS_list *tmp_dds, *tmp_dds_prev;
  1231. inter_list tmp_inter_list;
  1232. core_list *tmp_core_list, *tmp_core_list_prev;
  1233. if (first_time == 0){
  1234. printf("%d oh yeah\n",node_id);
  1235. first_time = 1;
  1236. its.it_interval.tv_sec = 0;
  1237. its.it_interval.tv_nsec = 0;
  1238. its.it_value.tv_sec = 2;
  1239. its.it_value.tv_nsec = 0;//100000000;
  1240. if (timer_settime(pfd_timer, 0, &its, NULL) == -1){
  1241. printf("-- %d --", node_id);
  1242. fflush(stdout);
  1243. perror("paxos_signal_handlers.c : timer_settime error9");
  1244. }else {
  1245. fprintf(log_file,"Updated timer!\n");
  1246. }
  1247. return;
  1248. }
  1249. signals_disable();
  1250. cur_time = time(NULL);
  1251. cur_t = localtime(&cur_time);
  1252. fprintf(log_file, "\n\n[%d:%d:%d]: I entered sig_PFD_TIMER_handler state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,id2string(state));
  1253. //printf("[%d:%d:%d]: -%d- I entered sig_PFD_TIMER_handler state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,node_id, id2string(state));
  1254. for (i = 0; i < X_max*Y_max; i++){
  1255. if (alive[i] == 0){
  1256. suspected[i]++;
  1257. }
  1258. #if defined(PFD) && defined(BASIC_PAXOS)
  1259. /* The Perfect Failure Detectors sends a SIG_HEARTBEAT_REQ each time the timer explodes and waits for a SIG_HEARTBEAT_REP*/
  1260. /* If he doesn't receive a reply until the timer reexplodes then the node is detected as faulty */
  1261. if (alive[i] != 1 && alive[i] != -1 && faulty_core == -1 && i != node_id && idag_mask[i] == idag_mask[node_id])
  1262. #elif defined(tPFD) && defined(BASIC_PAXOS) //tPFD
  1263. /* The tweaked Perfect Failure Detectors only suspects a core if he sends him a signal and doesn't receive a reply in some period of time*/
  1264. if (alive[i] == 0 && suspected[i] == 2 && i != node_id && idag_mask[i] == idag_mask[node_id])
  1265. #else
  1266. if (alive[i] == -5)
  1267. #endif
  1268. {
  1269. cur_time = time(NULL);
  1270. cur_t = localtime(&cur_time);
  1271. printf("-- %d -- I detected %d as faulty at [%d:%d:%d]!!\n", node_id, i,cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  1272. fprintf(log_file, "-- %d -- I detected %d as faulty at [%d:%d:%d]!!\n", node_id, i,cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  1273. fprintf(log_file, "-- %d -- I detected %d as faulty!!\n", node_id, i);
  1274. failed_core = i;
  1275. faulty_core = i;
  1276. suspected[failed_core] = -1;
  1277. alive[failed_core] = -1;
  1278. #if defined(CONTROLLER) && defined(PLAT_LINUX)
  1279. int semvalue = 0;
  1280. sem_getvalue(&flag_data_written[failed_core],&semvalue);
  1281. if (semvalue == 0){
  1282. /*I am locked*/
  1283. printf("--%d-- I unlocked semaphore for node %d\n",node_id,failed_core);
  1284. sem_post(&flag_data_written[failed_core]);
  1285. sem_getvalue(&flag_data_written[failed_core],&semvalue);
  1286. }
  1287. #endif
  1288. /*My controller timed out */
  1289. if (failed_core == my_idag){
  1290. sig_PAXOS_INIT_handler();
  1291. /* Controller in an other cluster timed out */
  1292. }else if (idag_mask[failed_core] == failed_core){
  1293. printf("--%d-- Other controller TIMED OUT\n", node_id);
  1294. for (j = 0; j < X_max*Y_max; j++){
  1295. if (idag_mask[j] == failed_core && j != failed_core){
  1296. if (state == IDLE_AGENT_WAITING_OFF) state = IDLE_AGENT;
  1297. tmp_inter_list.next = NULL;
  1298. tmp_inter_list.type = CONTR_TO;
  1299. scc_kill(j,SIG_CONTR_TO,&tmp_inter_list);
  1300. }
  1301. }
  1302. /* Worker or manager timed out */
  1303. }else{
  1304. /*I am controller.
  1305. *If failed node is inside my cluster
  1306. *i have to remove the failed node from my cores and DDS list */
  1307. if ((idag_mask[node_id] == node_id) && (idag_mask[failed_core] == node_id)){
  1308. tmp_core_list = my_cores->next;
  1309. tmp_core_list_prev = my_cores;
  1310. while (tmp_core_list != NULL){
  1311. if (tmp_core_list->core_id == failed_core){
  1312. my_cores_count--;
  1313. tmp_core_list_prev->next = tmp_core_list->next;
  1314. free(tmp_core_list);
  1315. break;
  1316. }else{
  1317. tmp_core_list_prev = tmp_core_list;
  1318. tmp_core_list = tmp_core_list->next;
  1319. }
  1320. }
  1321. tmp_dds = DDS->next;
  1322. tmp_dds_prev = DDS;
  1323. while (tmp_dds != NULL){
  1324. if (tmp_dds->agent_id == failed_core){
  1325. tmp_dds_prev->next = tmp_dds->next;
  1326. free(tmp_dds);
  1327. break;
  1328. }else{
  1329. tmp_dds_prev = tmp_dds;
  1330. tmp_dds = tmp_dds->next;
  1331. }
  1332. }
  1333. }
  1334. /*I am manager.
  1335. *I have to check if the failed node is my worker
  1336. *If yes i have to appoint work to a new node.*/
  1337. if (im_manager()){
  1338. printf("-- %d -- I am manager of an application.\n",node_id);
  1339. tmp_core_list = my_cores->next;
  1340. tmp_core_list_prev = my_cores;
  1341. while (tmp_core_list != NULL){
  1342. /* I am the manager of the failed worker. I reappoint the work to another core. */
  1343. if (tmp_core_list->core_id == failed_core){
  1344. my_cores_count--;
  1345. /* I am the manager of the failed worker so i remove him from my core list */
  1346. tmp_core_list_prev->next = tmp_core_list->next;
  1347. fprintf(log_file,"I am the manager of the failed worker %d! I removed him from my cores list\n",tmp_core_list->core_id);
  1348. printf("-- %d --I am the manager of the failed worker %d! I removed him from my cores list\n",node_id, tmp_core_list->core_id);
  1349. //one_core = tmp_core_list->core_id;
  1350. pending_workload[0] = tmp_core_list->workload[0];
  1351. pending_workload[1] = tmp_core_list->workload[1];
  1352. printf("Pending workload of faulty core: %d %d\n", pending_workload[0], pending_workload[1]);
  1353. /*else{
  1354. reappoint = FALSE;
  1355. }*/
  1356. free(tmp_core_list);
  1357. tmp_core_list = my_cores->next;
  1358. fprintf(log_file,"I am reassigning the workload!\n");
  1359. fprintf(log_file,"-------- CURRENT WORKLOADS --------\n");
  1360. printf("-------- CURRENT WORKLOADS --------\n");
  1361. while (tmp_core_list != NULL){
  1362. fprintf(log_file,"%d\t|\t%d\t|\t%d\n", tmp_core_list->core_id, tmp_core_list->workload[0], tmp_core_list->workload[1]);
  1363. printf("%d\t|\t%d\t|\t%d\n", tmp_core_list->core_id, tmp_core_list->workload[0], tmp_core_list->workload[1]);
  1364. tmp_core_list = tmp_core_list->next;
  1365. }
  1366. printf("-----------------------------------\n");
  1367. fprintf(log_file,"-----------------------------------\n");
  1368. tmp_core_list = my_cores->next;
  1369. while (tmp_core_list != NULL){
  1370. if ((tmp_core_list->workload[0] == -1) && (tmp_core_list->workload[1] == -1)){
  1371. one_core = tmp_core_list->core_id;
  1372. if (core_inter_head[one_core] == NULL){
  1373. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  1374. core_inter_tail[one_core] = core_inter_head[one_core];
  1375. } else {
  1376. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  1377. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  1378. }
  1379. core_inter_tail[one_core]->type = APPOINT_WORK_NODE;
  1380. core_inter_tail[one_core]->data.work_bounds[0] = pending_workload[0];
  1381. core_inter_tail[one_core]->data.work_bounds[1] = pending_workload[1];
  1382. fprintf(app_log_file,"%d (%d, %d), ",one_core,core_inter_tail[one_core]->data.work_bounds[0],core_inter_tail[one_core]->data.work_bounds[1]);
  1383. core_inter_tail[one_core]->next = NULL;
  1384. if (core_inter_head[one_core]->next == NULL) {
  1385. paxos_node_stats.msg_count++;
  1386. scc_kill(one_core, SIG_APPOINT_WORK, core_inter_head[one_core]);
  1387. } else {
  1388. fprintf(log_file,"I am doing smth else with my working node %d in init inter1 = %d inter2 = %d\n",one_core,core_inter_head[one_core]->type,core_inter_head[one_core]->next->type);
  1389. }
  1390. break;
  1391. }
  1392. tmp_core_list = tmp_core_list->next;
  1393. }
  1394. break;
  1395. }else {
  1396. tmp_core_list_prev = tmp_core_list;
  1397. tmp_core_list = tmp_core_list->next;
  1398. }
  1399. }
  1400. }else{
  1401. /* My manager timed out */
  1402. if (cur_agent.my_agent == failed_core){
  1403. printf("--%d-- %d is my manager! I initiate a Paxos instance\n",node_id, failed_core);
  1404. paxos_state = PAXOS_ACTIVE;
  1405. sig_PAXOS_INIT_handler();
  1406. }
  1407. }
  1408. }
  1409. }
  1410. #ifdef PFD
  1411. else{
  1412. if (alive[i] != -1 && i != node_id && idag_mask[i] == idag_mask[node_id]){
  1413. alive[i] = 0;
  1414. tmp_inter_list.type = HEARTBEAT_REQ;
  1415. tmp_inter_list.next = NULL;
  1416. scc_kill(i,SIG_HEARTBEAT_REQ,&tmp_inter_list);
  1417. }
  1418. }
  1419. #else
  1420. else{
  1421. if (alive[i] == 0 && i != node_id && idag_mask[i] == idag_mask[node_id]){
  1422. tmp_inter_list.type = HEARTBEAT_REQ;
  1423. tmp_inter_list.next = NULL;
  1424. scc_kill(i,SIG_HEARTBEAT_REQ,&tmp_inter_list);
  1425. }
  1426. }
  1427. #endif
  1428. }
  1429. fprintf(log_file,"\t\tNodes in my cluster: ");
  1430. for (i = 0; i < X_max*Y_max; i++){
  1431. if (idag_mask[i] == idag_mask[node_id] && i != node_id) fprintf(log_file,"%d, ",i);
  1432. }
  1433. fprintf(log_file,"\n");
  1434. fprintf(log_file,"\t\tI have received a signal from: ");
  1435. for (i = 0; i < X_max*Y_max; i++){
  1436. if (alive[i] == 1) fprintf(log_file,"%d, ",i);
  1437. }
  1438. fprintf(log_file,"\n");
  1439. fprintf(log_file,"\t\tSent HEARTBEAT_REQ to:");
  1440. for (i = 0; i < X_max*Y_max; i++){
  1441. if (alive[i] == 0 && i != node_id && idag_mask[i] == idag_mask[node_id]){
  1442. fprintf(log_file,"%d, ",i);
  1443. }
  1444. alive[i] = 0;
  1445. }
  1446. its.it_interval.tv_sec = 0;
  1447. its.it_interval.tv_nsec = 0;
  1448. its.it_value.tv_sec = 2;
  1449. its.it_value.tv_nsec = 0;//100000000;
  1450. if (timer_settime(pfd_timer, 0, &its, NULL) == -1){
  1451. printf("-- %d --", node_id);
  1452. fflush(stdout);
  1453. perror("paxos_signal_handlers.c : timer_settime error9");
  1454. }else {
  1455. fprintf(log_file,"Updated timer!\n");
  1456. }
  1457. cur_time = time(NULL);
  1458. cur_t = localtime(&cur_time);
  1459. fprintf(log_file, "[%d:%d:%d]: I ended sig_PFD_TIMER_handler state = %s\n\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,id2string(state));
  1460. signals_enable();
  1461. }
  1462. void sig_EPFD_TIMER_handler(int signo, siginfo_t *info, void *context)
  1463. {
  1464. core_list *tmp_core_list, *tmp_core_list_prev;
  1465. DDS_list *tmp_dds, *tmp_dds_prev;
  1466. int one_core,failed_core, pending_workload[2]/*,reappoint = TRUE*/;
  1467. signals_disable();
  1468. cur_time = time(NULL);
  1469. cur_t = localtime(&cur_time);
  1470. fprintf(log_file, "\n\n[%d:%d:%d]: I entered sig_EPFD_TIMER_handler state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,id2string(state));
  1471. int i, j;//, disjoint = 1;
  1472. inter_list tmp_inter_list;
  1473. for (i = 0; i < X_max*Y_max; i++){
  1474. if (alive[i] == suspected[i]){
  1475. fprintf(log_file,"\t\tNew Delay: %d\n",delay);
  1476. delay *= 2;
  1477. break;
  1478. //disjoint = 0;
  1479. }
  1480. }
  1481. fprintf(log_file,"\t\t------ FAILURE DETECTION ------\n");
  1482. for (i = 0; i < X_max*Y_max; i++){
  1483. if (alive[i] == 0 && suspected[i] != -1){
  1484. suspected[i]++;
  1485. fprintf(log_file,"\t\t%d -> SUSPECTED with suspected[%d] = %d\n",i,i,suspected[i]);
  1486. if ((suspected[i] > 2) && (faulty_core != i)){
  1487. cur_time = time(NULL);
  1488. cur_t = localtime(&cur_time);
  1489. failed_core = i;
  1490. faulty_core = i;
  1491. suspected[failed_core] = -1;
  1492. alive[failed_core] = -1;
  1493. #if defined(CONTROLLER) && defined(PLAT_LINUX)
  1494. int semvalue = 0;
  1495. sem_getvalue(&flag_data_written[failed_core],&semvalue);
  1496. if (semvalue == 0){
  1497. /*I am locked*/
  1498. printf("--%d-- I unlocked semaphore for node %d\n",node_id,failed_core);
  1499. sem_post(&flag_data_written[failed_core]);
  1500. sem_getvalue(&flag_data_written[failed_core],&semvalue);
  1501. }
  1502. #endif
  1503. /*My controller timed out */
  1504. if (failed_core == my_idag){
  1505. sig_PAXOS_INIT_handler();
  1506. /* Controller in an other cluster timed out */
  1507. }else if (idag_mask[failed_core] == failed_core){
  1508. printf("--%d-- Other controller TIMED OUT\n", node_id);
  1509. for (j = 0; j < X_max*Y_max; j++){
  1510. if (idag_mask[j] == failed_core && j != failed_core){
  1511. if (state == IDLE_AGENT_WAITING_OFF) state = IDLE_AGENT;
  1512. tmp_inter_list.next = NULL;
  1513. tmp_inter_list.type = CONTR_TO;
  1514. scc_kill(j,SIG_CONTR_TO,&tmp_inter_list);
  1515. }
  1516. }
  1517. /* Worker or manager timed out */
  1518. }else{
  1519. /*I am controller.
  1520. *If failed node is inside my cluster
  1521. *i have to remove the failed node from my cores and DDS list */
  1522. if ((idag_mask[node_id] == node_id) && (idag_mask[failed_core] == node_id)){
  1523. tmp_core_list = my_cores->next;
  1524. tmp_core_list_prev = my_cores;
  1525. while (tmp_core_list != NULL){
  1526. if (tmp_core_list->core_id == failed_core){
  1527. my_cores_count--;
  1528. tmp_core_list_prev->next = tmp_core_list->next;
  1529. free(tmp_core_list);
  1530. break;
  1531. }else{
  1532. tmp_core_list_prev = tmp_core_list;
  1533. tmp_core_list = tmp_core_list->next;
  1534. }
  1535. }
  1536. tmp_dds = DDS->next;
  1537. tmp_dds_prev = DDS;
  1538. while (tmp_dds != NULL){
  1539. if (tmp_dds->agent_id == failed_core){
  1540. tmp_dds_prev->next = tmp_dds->next;
  1541. free(tmp_dds);
  1542. break;
  1543. }else{
  1544. tmp_dds_prev = tmp_dds;
  1545. tmp_dds = tmp_dds->next;
  1546. }
  1547. }
  1548. }
  1549. /*I am manager.
  1550. *I have to check if the failed node is my worker
  1551. *If yes i have to appoint work to a new node.*/
  1552. if (im_manager()){
  1553. printf("-- %d -- I am manager of an application.\n",node_id);
  1554. tmp_core_list = my_cores->next;
  1555. tmp_core_list_prev = my_cores;
  1556. while (tmp_core_list != NULL){
  1557. /* I am the manager of the failed worker. I reappoint the work to another core. */
  1558. if (tmp_core_list->core_id == failed_core){
  1559. my_cores_count--;
  1560. /* I am the manager of the failed worker so i remove him from my core list */
  1561. tmp_core_list_prev->next = tmp_core_list->next;
  1562. fprintf(log_file,"I am the manager of the failed worker %d! I removed him from my cores list\n",tmp_core_list->core_id);
  1563. printf("-- %d --I am the manager of the failed worker %d! I removed him from my cores list\n",node_id, tmp_core_list->core_id);
  1564. //one_core = tmp_core_list->core_id;
  1565. pending_workload[0] = tmp_core_list->workload[0];
  1566. pending_workload[1] = tmp_core_list->workload[1];
  1567. printf("Pending workload of faulty core: %d %d\n", pending_workload[0], pending_workload[1]);
  1568. /*else{
  1569. reappoint = FALSE;
  1570. }*/
  1571. free(tmp_core_list);
  1572. tmp_core_list = my_cores->next;
  1573. fprintf(log_file,"I am reassigning the workload!\n");
  1574. fprintf(log_file,"-------- CURRENT WORKLOADS --------\n");
  1575. printf("-------- CURRENT WORKLOADS --------\n");
  1576. while (tmp_core_list != NULL){
  1577. fprintf(log_file,"%d\t|\t%d\t|\t%d\n", tmp_core_list->core_id, tmp_core_list->workload[0], tmp_core_list->workload[1]);
  1578. printf("%d\t|\t%d\t|\t%d\n", tmp_core_list->core_id, tmp_core_list->workload[0], tmp_core_list->workload[1]);
  1579. tmp_core_list = tmp_core_list->next;
  1580. }
  1581. printf("-----------------------------------\n");
  1582. fprintf(log_file,"-----------------------------------\n");
  1583. tmp_core_list = my_cores->next;
  1584. while (tmp_core_list != NULL){
  1585. if ((tmp_core_list->workload[0] == -1) && (tmp_core_list->workload[1] == -1)){
  1586. one_core = tmp_core_list->core_id;
  1587. if (core_inter_head[one_core] == NULL){
  1588. core_inter_head[one_core] = (inter_list *) malloc(sizeof(inter_list));
  1589. core_inter_tail[one_core] = core_inter_head[one_core];
  1590. } else {
  1591. core_inter_tail[one_core]->next = (inter_list *) malloc(sizeof(inter_list));
  1592. core_inter_tail[one_core] = core_inter_tail[one_core]->next;
  1593. }
  1594. core_inter_tail[one_core]->type = APPOINT_WORK_NODE;
  1595. core_inter_tail[one_core]->data.work_bounds[0] = pending_workload[0];
  1596. core_inter_tail[one_core]->data.work_bounds[1] = pending_workload[1];
  1597. fprintf(app_log_file,"%d (%d, %d), ",one_core,core_inter_tail[one_core]->data.work_bounds[0],core_inter_tail[one_core]->data.work_bounds[1]);
  1598. core_inter_tail[one_core]->next = NULL;
  1599. if (core_inter_head[one_core]->next == NULL) {
  1600. paxos_node_stats.msg_count++;
  1601. scc_kill(one_core, SIG_APPOINT_WORK, core_inter_head[one_core]);
  1602. } else {
  1603. fprintf(log_file,"I am doing smth else with my working node %d in init inter1 = %d inter2 = %d\n",one_core,core_inter_head[one_core]->type,core_inter_head[one_core]->next->type);
  1604. }
  1605. break;
  1606. }
  1607. tmp_core_list = tmp_core_list->next;
  1608. }
  1609. break;
  1610. }else {
  1611. tmp_core_list_prev = tmp_core_list;
  1612. tmp_core_list = tmp_core_list->next;
  1613. }
  1614. }
  1615. }else{
  1616. /* My manager timed out */
  1617. if (cur_agent.my_agent == failed_core){
  1618. printf("--%d-- %d is my manager! I initiate a Paxos instance\n",node_id, failed_core);
  1619. paxos_state = PAXOS_ACTIVE;
  1620. sig_PAXOS_INIT_handler();
  1621. }
  1622. }
  1623. }
  1624. }
  1625. #ifdef tEPFD
  1626. else if (suspected[i] == 2){
  1627. tmp_inter_list.next = NULL;
  1628. tmp_inter_list.type = HEARTBEAT_REQ;
  1629. scc_kill(i,SIG_HEARTBEAT_REQ,&tmp_inter_list);
  1630. }
  1631. #else
  1632. else {
  1633. if (i == 10)
  1634. printf("suspected[%d]=%d and alive[%d]=%d\n",i,suspected[i],i,alive[i]);
  1635. tmp_inter_list.next = NULL;
  1636. tmp_inter_list.type = HEARTBEAT_REQ;
  1637. scc_kill(i,SIG_HEARTBEAT_REQ,&tmp_inter_list);
  1638. }
  1639. #endif
  1640. }else if (alive[i] == 1){
  1641. suspected[i] = 0;
  1642. //fprintf(log_file,"\t\t%d -> ALIVE\n", i);
  1643. }
  1644. alive[i] = 0;
  1645. }
  1646. its.it_interval.tv_sec = 0;
  1647. its.it_interval.tv_nsec = 0;
  1648. its.it_value.tv_sec = delay;
  1649. its.it_value.tv_nsec = 0;
  1650. if (timer_settime(epfd_timer, 0, &its, NULL) == -1){
  1651. printf("-- %d --", node_id);
  1652. fflush(stdout);
  1653. perror("paxos_signal_handlers.c : timer_settime error9");
  1654. }else {
  1655. fprintf(log_file,"Updated timer!\n");
  1656. }
  1657. cur_time = time(NULL);
  1658. cur_t = localtime(&cur_time);
  1659. fprintf(log_file, "[%d:%d:%d]: I ended sig_EPFD_TIMER_handler state = %s\n\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,id2string(state));
  1660. signals_enable();
  1661. return;
  1662. }
  1663. void sig_CTIMER_handler(int signo, siginfo_t *info, void *context)
  1664. {
  1665. struct tm cur_t_1;
  1666. signals_disable();
  1667. struct timeval recover_time_val;
  1668. #ifdef CONTROLLER
  1669. DDS_list *tmp_dds;
  1670. printf("--%d-- CTIMER_handler : Controller %sTimed out!%s\n",node_id,KRED,KNRM);
  1671. printf("DDS list before time out:\n");
  1672. for (tmp_dds = DDS; tmp_dds != NULL; tmp_dds = tmp_dds->next)
  1673. printf("\t\t\tAgent_id : %d | Cores in cluster : %d\n",tmp_dds->agent_id, tmp_dds->num_of_cores);
  1674. #elif WORKER
  1675. printf("--%d-- CTIMER_handler : Worker %sTimed out!%s\n",node_id,KRED,KNRM);
  1676. printf("Worker state before timeout: %s\n",id2string(state));
  1677. #elif MANAGER
  1678. core_list *tmp_core_list;
  1679. printf("--%d-- CTIMER_handler : Manager %sTimed out!%s\n",node_id,KRED,KNRM);
  1680. printf("Manager state before timeout: %s\n",id2string(state));
  1681. for (tmp_core_list = my_cores; tmp_core_list != NULL; tmp_core_list = tmp_core_list->next)
  1682. printf("\t\t\tWorker_id : %d | Workload : %d %d\n", tmp_core_list->core_id, tmp_core_list->workload[0], tmp_core_list->workload[1]);
  1683. #endif
  1684. cur_time = time(NULL);
  1685. cur_t = localtime(&cur_time);
  1686. printf("\n\nI timed out at [%d:%d:%d]\n\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec);
  1687. /* FIXED IDs */
  1688. int k;
  1689. inter_list tmp_inter_list;
  1690. for (k = 0; k < X_max*Y_max; k++){
  1691. if (k != node_id && k != faulty_core){
  1692. tmp_inter_list.type = LEARN;
  1693. tmp_inter_list.data.learn_ack_info[VALUE_W] = node_id+1;
  1694. tmp_inter_list.data.learn_ack_info[PREV_CW] = node_id;
  1695. scc_kill(k,SIG_LEARN,&tmp_inter_list);
  1696. }
  1697. }
  1698. exit(0);
  1699. paxos_state = FAILED_CORE;
  1700. gettimeofday(&time_val,NULL);
  1701. cur_t = localtime(&time_val.tv_sec);
  1702. fail_time_val = time_val;
  1703. cur_t_1 = *cur_t;
  1704. while (fail_flag == 0){
  1705. scc_pause();
  1706. scc_signals_check();
  1707. }
  1708. gettimeofday(&time_val, NULL);
  1709. cur_t = localtime(&time_val.tv_sec);
  1710. printf("\n\n\n\n\n\n\n[%d:%d:%d:%ld]: gettimeofday_1\n",cur_t_1.tm_hour,cur_t_1.tm_min,cur_t_1.tm_sec,fail_time_val.tv_usec);
  1711. printf("[%d:%d:%d:%ld]: gettimeofday_2\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,time_val.tv_usec);
  1712. long int dif = ((cur_t->tm_sec * 1000000) + time_val.tv_usec) - ((cur_t_1.tm_sec * 1000000) + fail_time_val.tv_usec);
  1713. printf("Difference in us is: %ld\n\n\n\n\n\n\n",dif);
  1714. fflush(stdout);
  1715. exit(0);
  1716. signals_enable();
  1717. }
  1718. void sig_ITIMER_handler(int signo, siginfo_t *info, void *context)
  1719. {
  1720. int i;
  1721. inter_list tmp_inter_list;
  1722. signals_disable();
  1723. printf("--%d-- i have to decide what to do here!!\n", node_id);
  1724. fflush(stdout);
  1725. for (i = 0; i < X_max*Y_max; i++){
  1726. if (idag_mask[i] == 10 && i != 10){
  1727. if (state == IDLE_AGENT_WAITING_OFF) state = IDLE_AGENT;
  1728. tmp_inter_list.next = NULL;
  1729. tmp_inter_list.type = CONTR_TO;
  1730. scc_kill(i,SIG_CONTR_TO,&tmp_inter_list);
  1731. }
  1732. }
  1733. signals_enable();
  1734. }
  1735. /* END */
  1736. void sig_FAIL_handler(){
  1737. #ifdef WORKER
  1738. sev.sigev_notify = SIGEV_SIGNAL;
  1739. sev.sigev_signo = SIG_CTIMER;
  1740. sev.sigev_value.sival_ptr = &controller_timer;
  1741. if (timer_create(CLOCK_REALTIME, &sev, &controller_timer) == -1)
  1742. printf("timer_create error\n");
  1743. else
  1744. printf("Worker Timer created succesfully!\n");
  1745. its.it_interval.tv_sec = 0;
  1746. its.it_interval.tv_nsec = 0;
  1747. its.it_value.tv_sec = 2;
  1748. its.it_value.tv_nsec = 0;
  1749. if (timer_settime(controller_timer, 0, &its, NULL) == -1)
  1750. perror("controller_core.c : timer_settime error9");
  1751. else
  1752. printf("%d : My timer will explode in %d seconds.\n", node_id, 10);
  1753. return;
  1754. #endif
  1755. #ifdef MANAGER
  1756. sev.sigev_notify = SIGEV_SIGNAL;
  1757. sev.sigev_signo = SIG_CTIMER;
  1758. sev.sigev_value.sival_ptr = &controller_timer;
  1759. if (timer_create(CLOCK_REALTIME, &sev, &controller_timer) == -1)
  1760. printf("timer_create error\n");
  1761. else
  1762. printf("Manager Timer created succesfully!\n");
  1763. its.it_interval.tv_sec = 0;
  1764. its.it_interval.tv_nsec = 0;
  1765. its.it_value.tv_sec = 10;
  1766. its.it_value.tv_nsec = 0;
  1767. if (timer_settime(controller_timer, 0, &its, NULL) == -1)
  1768. perror("controller_core.c : timer_settime error9");
  1769. else
  1770. printf("%d : My timer will explode in %d seconds.\n", node_id, 10);
  1771. return;
  1772. #endif
  1773. }
  1774. void sig_PAXOS_STATS_REQ_handler(int sender_id){
  1775. inter_list tmp_inter_list;
  1776. cur_time = time(NULL);
  1777. cur_t = localtime(&cur_time);
  1778. fprintf(log_file, "\n\n[%d:%d:%d]: I entered sig_PAXOS_STATS_REQ_handler with sender = %d state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  1779. tmp_inter_list.next = NULL;
  1780. tmp_inter_list.type = PAXOS_STATS_REP;
  1781. tmp_inter_list.data.paxos_stats[0] = paxos_node_stats.msg_count;
  1782. tmp_inter_list.data.paxos_stats[1] = paxos_node_stats.fd_msg_count;
  1783. fprintf(log_file,"\t\tI send %d my paxos stats %lld , %d\n",sender_id,paxos_node_stats.msg_count,paxos_node_stats.fd_msg_count);
  1784. scc_kill(sender_id,SIG_PAXOS_STATS_REP,&tmp_inter_list);
  1785. cur_time = time(NULL);
  1786. cur_t = localtime(&cur_time);
  1787. fprintf(log_file, "\n\n[%d:%d:%d]: I ended sig_PAXOS_STATS_REQ_handler with sender = %d state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  1788. }
  1789. void sig_PAXOS_STATS_REP_handler(int sender_id){
  1790. long long int paxos_replied_stats = sig_read_ar[2];
  1791. long long int fd_replied_stats = sig_read_ar[3];
  1792. cur_time = time(NULL);
  1793. cur_t = localtime(&cur_time);
  1794. fprintf(log_file, "\n\n[%d:%d:%d]: I entered sig_PAXOS_STATS_REP_handler with sender = %d state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  1795. fprintf(log_file,"\t\t%d has replied with msg_count = %lld and fd_msg_count = %lld\n",sender_id,paxos_replied_stats,fd_replied_stats);
  1796. paxos_total_stats.msg_count += paxos_replied_stats;
  1797. paxos_total_stats.fd_msg_count += fd_replied_stats;
  1798. paxos_stats_replied++;
  1799. fprintf(log_file,"\t\tI have updated my stats. New message count = %lld\n",paxos_total_stats.msg_count);
  1800. fprintf(log_file,"\t\tCores replied: %d | My cores count: %d\n",paxos_stats_replied,my_cores_count);
  1801. cur_time = time(NULL);
  1802. cur_t = localtime(&cur_time);
  1803. fprintf(log_file, "\n\n[%d:%d:%d]: I ended sig_PAXOS_STATS_REP_handler with sender = %d state = %s\n",cur_t->tm_hour,cur_t->tm_min,cur_t->tm_sec,sender_id,id2string(state));
  1804. }