syncv2.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (c) 2010 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Angus Salkeld <asalkeld@redhat.com>
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <sys/types.h>
  36. #include <sys/socket.h>
  37. #include <sys/un.h>
  38. #include <sys/time.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 <netinet/in.h>
  49. #include <arpa/inet.h>
  50. #include <string.h>
  51. #include <corosync/swab.h>
  52. #include <corosync/corotypes.h>
  53. #include <qb/qbipc_common.h>
  54. #include <corosync/corodefs.h>
  55. #include <corosync/lcr/lcr_comp.h>
  56. #include <corosync/mar_gen.h>
  57. #include <corosync/engine/coroapi.h>
  58. #include <corosync/list.h>
  59. #include <corosync/engine/logsys.h>
  60. LOGSYS_DECLARE_SUBSYS ("TST2");
  61. /*
  62. * Service Interfaces required by service_message_handler struct
  63. */
  64. static int tst_sv2_exec_init_fn (
  65. struct corosync_api_v1 *corosync_api);
  66. static void tst_sv2_confchg_fn (
  67. enum totem_configuration_type configuration_type,
  68. const unsigned int *member_list, size_t member_list_entries,
  69. const unsigned int *left_list, size_t left_list_entries,
  70. const unsigned int *joined_list, size_t joined_list_entries,
  71. const struct memb_ring_id *ring_id);
  72. static int tst_sv2_lib_init_fn (void *conn);
  73. static int tst_sv2_lib_exit_fn (void *conn);
  74. static struct corosync_api_v1 *api;
  75. static void tst_sv2_sync_init_v2 (
  76. const unsigned int *member_list,
  77. size_t member_list_entries,
  78. const struct memb_ring_id *ring_id);
  79. static int tst_sv2_sync_process (void);
  80. static void tst_sv2_sync_activate (void);
  81. static void tst_sv2_sync_abort (void);
  82. static int tst_sv2_exec_exit_fn (void);
  83. struct corosync_service_engine tst_sv2_service_engine = {
  84. .name = "corosync test synv2 service",
  85. .id = TST_SV2_SERVICE,
  86. .priority = 1,
  87. .private_data_size = 0,
  88. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  89. .lib_init_fn = tst_sv2_lib_init_fn,
  90. .lib_exit_fn = tst_sv2_lib_exit_fn,
  91. .lib_engine = NULL,
  92. .lib_engine_count = 0,
  93. .exec_engine = NULL,
  94. .exec_engine_count = 0,
  95. .confchg_fn = tst_sv2_confchg_fn,
  96. .exec_init_fn = tst_sv2_exec_init_fn,
  97. .exec_exit_fn = tst_sv2_exec_exit_fn,
  98. .exec_dump_fn = NULL,
  99. .sync_mode = CS_SYNC_V2,
  100. .sync_init = tst_sv2_sync_init_v2,
  101. .sync_process = tst_sv2_sync_process,
  102. .sync_activate = tst_sv2_sync_activate,
  103. .sync_abort = tst_sv2_sync_abort
  104. };
  105. static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
  106. static unsigned int my_member_list_entries;
  107. static unsigned int my_old_member_list[PROCESSOR_COUNT_MAX];
  108. static unsigned int my_old_member_list_entries = 0;
  109. static int num_sync_processes = 0;
  110. static DECLARE_LIST_INIT (confchg_notify);
  111. /*
  112. * Dynamic loading descriptor
  113. */
  114. static struct corosync_service_engine *tst_sv2_get_service_engine_ver0 (void);
  115. static struct corosync_service_engine_iface_ver0 tst_sv2_service_engine_iface = {
  116. .corosync_get_service_engine_ver0 = tst_sv2_get_service_engine_ver0
  117. };
  118. static struct lcr_iface corosync_tst_sv2_ver0[1] = {
  119. {
  120. .name = "corosync_tst_sv2",
  121. .version = 0,
  122. .versions_replace = 0,
  123. .versions_replace_count = 0,
  124. .dependencies = 0,
  125. .dependency_count = 0,
  126. .constructor = NULL,
  127. .destructor = NULL,
  128. .interfaces = NULL,
  129. }
  130. };
  131. static struct lcr_comp tst_sv2_comp_ver0 = {
  132. .iface_count = 1,
  133. .ifaces = corosync_tst_sv2_ver0
  134. };
  135. static struct corosync_service_engine *tst_sv2_get_service_engine_ver0 (void)
  136. {
  137. return (&tst_sv2_service_engine);
  138. }
  139. #ifdef COROSYNC_SOLARIS
  140. void corosync_lcr_component_register (void);
  141. void corosync_lcr_component_register (void) {
  142. #else
  143. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  144. #endif
  145. lcr_interfaces_set (&corosync_tst_sv2_ver0[0], &tst_sv2_service_engine_iface);
  146. lcr_component_register (&tst_sv2_comp_ver0);
  147. }
  148. static int tst_sv2_exec_init_fn (
  149. struct corosync_api_v1 *corosync_api)
  150. {
  151. #ifdef COROSYNC_SOLARIS
  152. logsys_subsys_init();
  153. #endif
  154. api = corosync_api;
  155. return 0;
  156. }
  157. static int32_t exit_count = 0;
  158. static int tst_sv2_exec_exit_fn (void)
  159. {
  160. exit_count++;
  161. log_printf (LOGSYS_LEVEL_INFO, "exit_count:%d", exit_count);
  162. if (exit_count < 4) {
  163. return -1;
  164. }
  165. }
  166. static void tst_sv2_confchg_fn (
  167. enum totem_configuration_type configuration_type,
  168. const unsigned int *member_list, size_t member_list_entries,
  169. const unsigned int *left_list, size_t left_list_entries,
  170. const unsigned int *joined_list, size_t joined_list_entries,
  171. const struct memb_ring_id *ring_id)
  172. {
  173. int j;
  174. for (j = 0; j < left_list_entries; j++) {
  175. log_printf (LOGSYS_LEVEL_INFO,
  176. "Member left: %s", api->totem_ifaces_print (left_list[j]));
  177. }
  178. for (j = 0; j < joined_list_entries; j++) {
  179. log_printf (LOGSYS_LEVEL_INFO,
  180. "Member joined: %s", api->totem_ifaces_print (joined_list[j]));
  181. }
  182. }
  183. static int tst_sv2_lib_init_fn (void *conn)
  184. {
  185. return (0);
  186. }
  187. static int tst_sv2_lib_exit_fn (void *conn)
  188. {
  189. return (0);
  190. }
  191. static void tst_sv2_sync_init_v2 (
  192. const unsigned int *member_list,
  193. size_t member_list_entries,
  194. const struct memb_ring_id *ring_id)
  195. {
  196. unsigned int lowest_nodeid = 0xffffffff;
  197. int i, j;
  198. int found;
  199. num_sync_processes = 0;
  200. memcpy (my_member_list, member_list, member_list_entries *
  201. sizeof (unsigned int));
  202. my_member_list_entries = member_list_entries;
  203. for (i = 0; i < my_member_list_entries; i++) {
  204. if (my_member_list[i] < lowest_nodeid) {
  205. lowest_nodeid = my_member_list[i];
  206. }
  207. }
  208. log_printf (LOGSYS_LEVEL_INFO,
  209. "tst_sv2_sync_init_v2 %s",
  210. api->totem_ifaces_print (lowest_nodeid));
  211. /* look for new (joined) nodes */
  212. for (j = 0; j < member_list_entries; j++) {
  213. found = 0;
  214. for (i = 0; i < my_old_member_list_entries; i++) {
  215. if (my_old_member_list[i] == member_list[j]) {
  216. found = 1;
  217. break;
  218. }
  219. }
  220. if (found == 0) {
  221. log_printf (LOGSYS_LEVEL_INFO,
  222. "sync: node joined %s",
  223. api->totem_ifaces_print (member_list[j]));
  224. }
  225. }
  226. /* look for old (left) nodes */
  227. for (i = 0; i < my_old_member_list_entries; i++) {
  228. found = 0;
  229. for (j = 0; j < member_list_entries; j++) {
  230. if (my_old_member_list[i] == member_list[j]) {
  231. found = 1;
  232. break;
  233. }
  234. }
  235. if (found == 0) {
  236. log_printf (LOGSYS_LEVEL_INFO,
  237. "sync: node left %s",
  238. api->totem_ifaces_print (my_old_member_list[i]));
  239. }
  240. }
  241. }
  242. static int tst_sv2_sync_process (void)
  243. {
  244. num_sync_processes++;
  245. if (num_sync_processes > 3) {
  246. return 0;
  247. } else {
  248. return 1;
  249. }
  250. }
  251. static void tst_sv2_sync_activate (void)
  252. {
  253. memcpy (my_old_member_list, my_member_list,
  254. my_member_list_entries * sizeof (unsigned int));
  255. my_old_member_list_entries = my_member_list_entries;
  256. if (num_sync_processes <= 3) {
  257. log_printf (LOGSYS_LEVEL_ERROR,
  258. "sync: activate called before process is done %d",
  259. num_sync_processes);
  260. } else {
  261. log_printf (LOGSYS_LEVEL_INFO,
  262. "sync: activate correctly %d",
  263. num_sync_processes);
  264. }
  265. num_sync_processes = 0;
  266. }
  267. static void tst_sv2_sync_abort (void)
  268. {
  269. log_printf (LOGSYS_LEVEL_INFO, "sync: abort");
  270. }