syncv2.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 struct corosync_api_v1 *api;
  73. static void tst_sv2_sync_init_v2 (
  74. const unsigned int *member_list,
  75. size_t member_list_entries,
  76. const struct memb_ring_id *ring_id);
  77. static int tst_sv2_sync_process (void);
  78. static void tst_sv2_sync_activate (void);
  79. static void tst_sv2_sync_abort (void);
  80. static int tst_sv2_exec_exit_fn (void);
  81. struct corosync_service_engine tst_sv2_service_engine = {
  82. .name = "corosync test synv2 service",
  83. .id = TST_SV2_SERVICE,
  84. .priority = 1,
  85. .private_data_size = 0,
  86. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  87. .lib_init_fn = NULL,
  88. .lib_exit_fn = NULL,
  89. .lib_engine = NULL,
  90. .lib_engine_count = 0,
  91. .exec_engine = NULL,
  92. .exec_engine_count = 0,
  93. .confchg_fn = tst_sv2_confchg_fn,
  94. .exec_init_fn = tst_sv2_exec_init_fn,
  95. .exec_exit_fn = tst_sv2_exec_exit_fn,
  96. .exec_dump_fn = NULL,
  97. .sync_mode = CS_SYNC_V2,
  98. .sync_init = tst_sv2_sync_init_v2,
  99. .sync_process = tst_sv2_sync_process,
  100. .sync_activate = tst_sv2_sync_activate,
  101. .sync_abort = tst_sv2_sync_abort
  102. };
  103. static unsigned int my_member_list[PROCESSOR_COUNT_MAX];
  104. static unsigned int my_member_list_entries;
  105. static unsigned int my_old_member_list[PROCESSOR_COUNT_MAX];
  106. static unsigned int my_old_member_list_entries = 0;
  107. static int num_sync_processes = 0;
  108. static DECLARE_LIST_INIT (confchg_notify);
  109. /*
  110. * Dynamic loading descriptor
  111. */
  112. static struct corosync_service_engine *tst_sv2_get_service_engine_ver0 (void);
  113. static struct corosync_service_engine_iface_ver0 tst_sv2_service_engine_iface = {
  114. .corosync_get_service_engine_ver0 = tst_sv2_get_service_engine_ver0
  115. };
  116. static struct lcr_iface corosync_tst_sv2_ver0[1] = {
  117. {
  118. .name = "corosync_tst_sv2",
  119. .version = 0,
  120. .versions_replace = 0,
  121. .versions_replace_count = 0,
  122. .dependencies = 0,
  123. .dependency_count = 0,
  124. .constructor = NULL,
  125. .destructor = NULL,
  126. .interfaces = NULL,
  127. }
  128. };
  129. static struct lcr_comp tst_sv2_comp_ver0 = {
  130. .iface_count = 1,
  131. .ifaces = corosync_tst_sv2_ver0
  132. };
  133. static struct corosync_service_engine *tst_sv2_get_service_engine_ver0 (void)
  134. {
  135. return (&tst_sv2_service_engine);
  136. }
  137. #ifdef COROSYNC_SOLARIS
  138. void corosync_lcr_component_register (void);
  139. void corosync_lcr_component_register (void) {
  140. #else
  141. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  142. #endif
  143. lcr_interfaces_set (&corosync_tst_sv2_ver0[0], &tst_sv2_service_engine_iface);
  144. lcr_component_register (&tst_sv2_comp_ver0);
  145. }
  146. static int tst_sv2_exec_init_fn (
  147. struct corosync_api_v1 *corosync_api)
  148. {
  149. #ifdef COROSYNC_SOLARIS
  150. logsys_subsys_init();
  151. #endif
  152. api = corosync_api;
  153. return 0;
  154. }
  155. static int32_t exit_count = 0;
  156. static int tst_sv2_exec_exit_fn (void)
  157. {
  158. exit_count++;
  159. log_printf (LOGSYS_LEVEL_INFO, "exit_count:%d", exit_count);
  160. if (exit_count < 4) {
  161. return -1;
  162. }
  163. return 0;
  164. }
  165. static void tst_sv2_confchg_fn (
  166. enum totem_configuration_type configuration_type,
  167. const unsigned int *member_list, size_t member_list_entries,
  168. const unsigned int *left_list, size_t left_list_entries,
  169. const unsigned int *joined_list, size_t joined_list_entries,
  170. const struct memb_ring_id *ring_id)
  171. {
  172. int j;
  173. for (j = 0; j < left_list_entries; j++) {
  174. log_printf (LOGSYS_LEVEL_INFO,
  175. "Member left: %s", api->totem_ifaces_print (left_list[j]));
  176. }
  177. for (j = 0; j < joined_list_entries; j++) {
  178. log_printf (LOGSYS_LEVEL_INFO,
  179. "Member joined: %s", api->totem_ifaces_print (joined_list[j]));
  180. }
  181. }
  182. static void tst_sv2_sync_init_v2 (
  183. const unsigned int *member_list,
  184. size_t member_list_entries,
  185. const struct memb_ring_id *ring_id)
  186. {
  187. unsigned int lowest_nodeid = 0xffffffff;
  188. int i, j;
  189. int found;
  190. num_sync_processes = 0;
  191. memcpy (my_member_list, member_list, member_list_entries *
  192. sizeof (unsigned int));
  193. my_member_list_entries = member_list_entries;
  194. for (i = 0; i < my_member_list_entries; i++) {
  195. if (my_member_list[i] < lowest_nodeid) {
  196. lowest_nodeid = my_member_list[i];
  197. }
  198. }
  199. log_printf (LOGSYS_LEVEL_INFO,
  200. "tst_sv2_sync_init_v2 %s",
  201. api->totem_ifaces_print (lowest_nodeid));
  202. /* look for new (joined) nodes */
  203. for (j = 0; j < member_list_entries; j++) {
  204. found = 0;
  205. for (i = 0; i < my_old_member_list_entries; i++) {
  206. if (my_old_member_list[i] == member_list[j]) {
  207. found = 1;
  208. break;
  209. }
  210. }
  211. if (found == 0) {
  212. log_printf (LOGSYS_LEVEL_INFO,
  213. "sync: node joined %s",
  214. api->totem_ifaces_print (member_list[j]));
  215. }
  216. }
  217. /* look for old (left) nodes */
  218. for (i = 0; i < my_old_member_list_entries; i++) {
  219. found = 0;
  220. for (j = 0; j < member_list_entries; j++) {
  221. if (my_old_member_list[i] == member_list[j]) {
  222. found = 1;
  223. break;
  224. }
  225. }
  226. if (found == 0) {
  227. log_printf (LOGSYS_LEVEL_INFO,
  228. "sync: node left %s",
  229. api->totem_ifaces_print (my_old_member_list[i]));
  230. }
  231. }
  232. }
  233. static int tst_sv2_sync_process (void)
  234. {
  235. num_sync_processes++;
  236. if (num_sync_processes > 3) {
  237. return 0;
  238. } else {
  239. return 1;
  240. }
  241. }
  242. static void tst_sv2_sync_activate (void)
  243. {
  244. memcpy (my_old_member_list, my_member_list,
  245. my_member_list_entries * sizeof (unsigned int));
  246. my_old_member_list_entries = my_member_list_entries;
  247. if (num_sync_processes <= 3) {
  248. log_printf (LOGSYS_LEVEL_ERROR,
  249. "sync: activate called before process is done %d",
  250. num_sync_processes);
  251. } else {
  252. log_printf (LOGSYS_LEVEL_INFO,
  253. "sync: activate correctly %d",
  254. num_sync_processes);
  255. }
  256. num_sync_processes = 0;
  257. }
  258. static void tst_sv2_sync_abort (void)
  259. {
  260. log_printf (LOGSYS_LEVEL_INFO, "sync: abort");
  261. }