sync.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * Copyright (c) 2005-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2007, 2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <config.h>
  36. #include <sys/types.h>
  37. #include <sys/socket.h>
  38. #include <sys/un.h>
  39. #include <sys/ioctl.h>
  40. #include <netinet/in.h>
  41. #include <sys/uio.h>
  42. #include <unistd.h>
  43. #include <fcntl.h>
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <errno.h>
  47. #include <time.h>
  48. #include <unistd.h>
  49. #include <netinet/in.h>
  50. #include <arpa/inet.h>
  51. #include <corosync/corotypes.h>
  52. #include <corosync/swab.h>
  53. #include <corosync/totem/totempg.h>
  54. #include <corosync/totem/totem.h>
  55. #include <corosync/lcr/lcr_ifact.h>
  56. #include <corosync/engine/logsys.h>
  57. #include <corosync/coroipc_types.h>
  58. #include "quorum.h"
  59. #include "sync.h"
  60. LOGSYS_DECLARE_SUBSYS ("SYNC");
  61. #define MESSAGE_REQ_SYNC_BARRIER 0
  62. struct barrier_data {
  63. unsigned int nodeid;
  64. int completed;
  65. };
  66. static const struct memb_ring_id *sync_ring_id;
  67. static int (*sync_callbacks_retrieve) (int sync_id, struct sync_callbacks *callack);
  68. static void (*sync_started) (
  69. const struct memb_ring_id *ring_id);
  70. static void (*sync_aborted) (void);
  71. static struct sync_callbacks sync_callbacks;
  72. static int sync_processing = 0;
  73. static void (*sync_next_start) (
  74. const unsigned int *member_list,
  75. size_t member_list_entries,
  76. const struct memb_ring_id *ring_id);
  77. static int sync_recovery_index = 0;
  78. static int my_processing_idx = 0;
  79. static void *sync_callback_token_handle = 0;
  80. static struct barrier_data barrier_data_confchg[PROCESSOR_COUNT_MAX];
  81. static size_t barrier_data_confchg_entries;
  82. static struct barrier_data barrier_data_process[PROCESSOR_COUNT_MAX];
  83. static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
  84. static unsigned int my_trans_list[PROCESSOR_COUNT_MAX];
  85. static unsigned int my_member_list_entries;
  86. static unsigned int my_trans_list_entries;
  87. static int sync_barrier_send (const struct memb_ring_id *ring_id);
  88. static int sync_start_process (enum totem_callback_token_type type,
  89. const void *data);
  90. static void sync_service_init (struct memb_ring_id *ring_id);
  91. static int sync_service_process (enum totem_callback_token_type type,
  92. const void *data);
  93. static void sync_deliver_fn (
  94. unsigned int nodeid,
  95. const void *msg,
  96. unsigned int msg_len,
  97. int endian_conversion_required);
  98. static void sync_confchg_fn (
  99. enum totem_configuration_type configuration_type,
  100. const unsigned int *member_list,
  101. size_t member_list_entries,
  102. const unsigned int *left_list,
  103. size_t left_list_entries,
  104. const unsigned int *joined_list,
  105. size_t joined_list_entries,
  106. const struct memb_ring_id *ring_id);
  107. static void sync_primary_callback_fn (
  108. const unsigned int *view_list,
  109. size_t view_list_entries,
  110. int primary_designated,
  111. const struct memb_ring_id *ring_id);
  112. static struct totempg_group sync_group = {
  113. .group = "sync",
  114. .group_len = 4
  115. };
  116. static hdb_handle_t sync_group_handle;
  117. struct req_exec_sync_barrier_start {
  118. coroipc_request_header_t header;
  119. struct memb_ring_id ring_id;
  120. };
  121. /*
  122. * Send a barrier data structure
  123. */
  124. static int sync_barrier_send (const struct memb_ring_id *ring_id)
  125. {
  126. struct req_exec_sync_barrier_start req_exec_sync_barrier_start;
  127. struct iovec iovec;
  128. int res;
  129. req_exec_sync_barrier_start.header.size = sizeof (struct req_exec_sync_barrier_start);
  130. req_exec_sync_barrier_start.header.id = MESSAGE_REQ_SYNC_BARRIER;
  131. memcpy (&req_exec_sync_barrier_start.ring_id, ring_id,
  132. sizeof (struct memb_ring_id));
  133. iovec.iov_base = (char *)&req_exec_sync_barrier_start;
  134. iovec.iov_len = sizeof (req_exec_sync_barrier_start);
  135. res = totempg_groups_mcast_joined (sync_group_handle, &iovec, 1, TOTEMPG_AGREED);
  136. return (res);
  137. }
  138. static void sync_start_init (const struct memb_ring_id *ring_id)
  139. {
  140. totempg_callback_token_create (
  141. &sync_callback_token_handle,
  142. TOTEM_CALLBACK_TOKEN_SENT,
  143. 0, /* don't delete after callback */
  144. sync_start_process,
  145. ring_id);
  146. }
  147. static void sync_service_init (struct memb_ring_id *ring_id)
  148. {
  149. if (sync_callbacks.api_version == 1) {
  150. if (sync_callbacks_retrieve(my_processing_idx, NULL) != -1) {
  151. sync_callbacks.sync_init_api.sync_init_v1 (my_member_list,
  152. my_member_list_entries, ring_id);
  153. }
  154. } else {
  155. if (sync_callbacks_retrieve(my_processing_idx, NULL) != -1) {
  156. sync_callbacks.sync_init_api.sync_init_v2 (my_trans_list,
  157. my_trans_list_entries,
  158. my_member_list, my_member_list_entries, ring_id);
  159. }
  160. }
  161. totempg_callback_token_destroy (&sync_callback_token_handle);
  162. /*
  163. * Create the token callback for the processing
  164. */
  165. totempg_callback_token_create (
  166. &sync_callback_token_handle,
  167. TOTEM_CALLBACK_TOKEN_SENT,
  168. 0, /* don't delete after callback */
  169. sync_service_process,
  170. ring_id);
  171. }
  172. static int sync_start_process (enum totem_callback_token_type type,
  173. const void *data)
  174. {
  175. int res;
  176. const struct memb_ring_id *ring_id = data;
  177. res = sync_barrier_send (ring_id);
  178. if (res == 0) {
  179. /*
  180. * Delete the token callback for the barrier
  181. */
  182. totempg_callback_token_destroy (&sync_callback_token_handle);
  183. }
  184. return (0);
  185. }
  186. static void sync_callbacks_load (void)
  187. {
  188. int res;
  189. for (;;) {
  190. res = sync_callbacks_retrieve (sync_recovery_index,
  191. &sync_callbacks);
  192. /*
  193. * No more service handlers have sync callbacks at this time
  194. ` */
  195. if (res == -1) {
  196. sync_processing = 0;
  197. break;
  198. }
  199. my_processing_idx = sync_recovery_index;
  200. sync_recovery_index += 1;
  201. if (sync_callbacks.sync_init_api.sync_init_v1) {
  202. break;
  203. }
  204. }
  205. }
  206. static int sync_service_process (enum totem_callback_token_type type,
  207. const void *data)
  208. {
  209. int res;
  210. const struct memb_ring_id *ring_id = data;
  211. /*
  212. * If process operation not from this ring id, then ignore it and stop
  213. * processing
  214. */
  215. if (memcmp (ring_id, sync_ring_id, sizeof (struct memb_ring_id)) != 0) {
  216. return (0);
  217. }
  218. /*
  219. * If process returns 0, then its time to activate
  220. * and start the next service's synchronization
  221. */
  222. if (sync_callbacks_retrieve(my_processing_idx, NULL) != -1) {
  223. res = sync_callbacks.sync_process ();
  224. } else {
  225. res = 0;
  226. }
  227. if (res != 0) {
  228. return (0);
  229. }
  230. totempg_callback_token_destroy (&sync_callback_token_handle);
  231. sync_start_init (ring_id);
  232. return (0);
  233. }
  234. int sync_register (
  235. int (*callbacks_retrieve) (
  236. int sync_id,
  237. struct sync_callbacks *callbacks),
  238. void (*started) (
  239. const struct memb_ring_id *ring_id),
  240. void (*aborted) (void),
  241. void (*next_start) (
  242. const unsigned int *member_list,
  243. size_t member_list_entries,
  244. const struct memb_ring_id *ring_id))
  245. {
  246. unsigned int res;
  247. res = totempg_groups_initialize (
  248. &sync_group_handle,
  249. sync_deliver_fn,
  250. sync_confchg_fn);
  251. if (res == -1) {
  252. log_printf (LOGSYS_LEVEL_ERROR,
  253. "Couldn't initialize groups interface.\n");
  254. return (-1);
  255. }
  256. res = totempg_groups_join (
  257. sync_group_handle,
  258. &sync_group,
  259. 1);
  260. if (res == -1) {
  261. log_printf (LOGSYS_LEVEL_ERROR, "Couldn't join group.\n");
  262. return (-1);
  263. }
  264. sync_callbacks_retrieve = callbacks_retrieve;
  265. sync_next_start = next_start;
  266. sync_started = started;
  267. sync_aborted = aborted;
  268. return (0);
  269. }
  270. static void sync_primary_callback_fn (
  271. const unsigned int *view_list,
  272. size_t view_list_entries,
  273. int primary_designated,
  274. const struct memb_ring_id *ring_id)
  275. {
  276. int i;
  277. if (primary_designated) {
  278. log_printf (LOGSYS_LEVEL_DEBUG, "This node is within the primary component and will provide service.\n");
  279. } else {
  280. log_printf (LOGSYS_LEVEL_DEBUG, "This node is within the non-primary component and will NOT provide any services.\n");
  281. return;
  282. }
  283. /*
  284. * Execute configuration change for synchronization service
  285. */
  286. sync_processing = 1;
  287. totempg_callback_token_destroy (&sync_callback_token_handle);
  288. sync_recovery_index = 0;
  289. my_processing_idx = 0;
  290. memset (&barrier_data_confchg, 0, sizeof (barrier_data_confchg));
  291. for (i = 0; i < view_list_entries; i++) {
  292. barrier_data_confchg[i].nodeid = view_list[i];
  293. barrier_data_confchg[i].completed = 0;
  294. }
  295. memcpy (barrier_data_process, barrier_data_confchg,
  296. sizeof (barrier_data_confchg));
  297. barrier_data_confchg_entries = view_list_entries;
  298. sync_start_init (sync_ring_id);
  299. }
  300. static struct memb_ring_id deliver_ring_id;
  301. static void sync_endian_convert (struct req_exec_sync_barrier_start
  302. *req_exec_sync_barrier_start)
  303. {
  304. totemip_copy_endian_convert(&req_exec_sync_barrier_start->ring_id.rep,
  305. &req_exec_sync_barrier_start->ring_id.rep);
  306. req_exec_sync_barrier_start->ring_id.seq = swab64 (req_exec_sync_barrier_start->ring_id.seq);
  307. }
  308. static void sync_deliver_fn (
  309. unsigned int nodeid,
  310. const void *msg,
  311. unsigned int msg_len,
  312. int endian_conversion_required)
  313. {
  314. struct req_exec_sync_barrier_start *req_exec_sync_barrier_start =
  315. (struct req_exec_sync_barrier_start *)msg;
  316. unsigned int barrier_completed;
  317. int i;
  318. log_printf (LOGSYS_LEVEL_DEBUG, "confchg entries %lu\n",
  319. (unsigned long int) barrier_data_confchg_entries);
  320. if (endian_conversion_required) {
  321. sync_endian_convert (req_exec_sync_barrier_start);
  322. }
  323. barrier_completed = 1;
  324. memcpy (&deliver_ring_id, &req_exec_sync_barrier_start->ring_id,
  325. sizeof (struct memb_ring_id));
  326. /*
  327. * Is this barrier from this configuration, if not, ignore it
  328. */
  329. if (memcmp (&req_exec_sync_barrier_start->ring_id, sync_ring_id,
  330. sizeof (struct memb_ring_id)) != 0) {
  331. return;
  332. }
  333. /*
  334. * Set completion for source_addr's address
  335. */
  336. for (i = 0; i < barrier_data_confchg_entries; i++) {
  337. if (nodeid == barrier_data_process[i].nodeid) {
  338. barrier_data_process[i].completed = 1;
  339. log_printf (LOGSYS_LEVEL_DEBUG,
  340. "Barrier Start Received From %d\n",
  341. barrier_data_process[i].nodeid);
  342. break;
  343. }
  344. }
  345. /*
  346. * Test if barrier is complete
  347. */
  348. for (i = 0; i < barrier_data_confchg_entries; i++) {
  349. log_printf (LOGSYS_LEVEL_DEBUG,
  350. "Barrier completion status for nodeid %d = %d. \n",
  351. barrier_data_process[i].nodeid,
  352. barrier_data_process[i].completed);
  353. if (barrier_data_process[i].completed == 0) {
  354. barrier_completed = 0;
  355. }
  356. }
  357. if (barrier_completed) {
  358. log_printf (LOGSYS_LEVEL_DEBUG,
  359. "Synchronization barrier completed\n");
  360. }
  361. /*
  362. * This sync is complete so activate and start next service sync
  363. */
  364. if (barrier_completed && sync_callbacks.sync_activate) {
  365. if (sync_callbacks_retrieve(my_processing_idx, NULL) != -1) {
  366. sync_callbacks.sync_activate ();
  367. }
  368. log_printf (LOGSYS_LEVEL_DEBUG,
  369. "Committing synchronization for (%s)\n",
  370. sync_callbacks.name);
  371. }
  372. /*
  373. * Start synchronization if the barrier has completed
  374. */
  375. if (barrier_completed) {
  376. memcpy (barrier_data_process, barrier_data_confchg,
  377. sizeof (barrier_data_confchg));
  378. sync_callbacks_load();
  379. /*
  380. * if sync service found, execute it
  381. */
  382. if (sync_processing && sync_callbacks.sync_init_api.sync_init_v1) {
  383. log_printf (LOGSYS_LEVEL_DEBUG,
  384. "Synchronization actions starting for (%s)\n",
  385. sync_callbacks.name);
  386. sync_service_init (&deliver_ring_id);
  387. }
  388. if (sync_processing == 0) {
  389. sync_next_start (my_member_list, my_member_list_entries, sync_ring_id);
  390. }
  391. }
  392. return;
  393. }
  394. static void sync_confchg_fn (
  395. enum totem_configuration_type configuration_type,
  396. const unsigned int *member_list,
  397. size_t member_list_entries,
  398. const unsigned int *left_list,
  399. size_t left_list_entries,
  400. const unsigned int *joined_list,
  401. size_t joined_list_entries,
  402. const struct memb_ring_id *ring_id)
  403. {
  404. sync_ring_id = ring_id;
  405. if (configuration_type != TOTEM_CONFIGURATION_REGULAR) {
  406. memcpy (my_trans_list, member_list, member_list_entries *
  407. sizeof (unsigned int));
  408. my_trans_list_entries = member_list_entries;
  409. return;
  410. }
  411. memcpy (my_member_list, member_list, member_list_entries * sizeof (unsigned int));
  412. my_member_list_entries = member_list_entries;
  413. sync_aborted ();
  414. if (sync_processing && sync_callbacks.sync_abort != NULL) {
  415. if (sync_callbacks_retrieve(my_processing_idx, NULL) != -1) {
  416. sync_callbacks.sync_abort ();
  417. }
  418. sync_callbacks.sync_activate = NULL;
  419. }
  420. sync_started (
  421. ring_id);
  422. sync_primary_callback_fn (
  423. member_list,
  424. member_list_entries,
  425. 1,
  426. ring_id);
  427. }