testcpg.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Copyright (c) 2006-2009 Red Hat Inc
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield <ccaulfie@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 <inttypes.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <errno.h>
  39. #include <unistd.h>
  40. #include <string.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <sys/select.h>
  44. #include <sys/uio.h>
  45. #include <sys/un.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <time.h>
  49. #include <sys/time.h>
  50. #include <assert.h>
  51. #include <limits.h>
  52. #include <corosync/corotypes.h>
  53. #include <corosync/cpg.h>
  54. #include <corosync/swab.h>
  55. #ifdef QBLOG
  56. #include <qb/qblog.h>
  57. #endif
  58. #ifndef HOST_NAME_MAX
  59. #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
  60. #endif
  61. static int quit = 0;
  62. static int show_ip = 0;
  63. static int restart = 0;
  64. static uint32_t nodeidStart = 0;
  65. static void print_localnodeid(cpg_handle_t handle);
  66. static void print_cpgname (const struct cpg_name *name)
  67. {
  68. unsigned int i;
  69. for (i = 0; i < name->length; i++) {
  70. printf ("%c", name->value[i]);
  71. }
  72. }
  73. static char * node_pid_format(unsigned int nodeid, unsigned int pid) {
  74. static char buffer[100];
  75. if (show_ip) {
  76. struct in_addr saddr;
  77. #if __BYTE_ORDER == __BIG_ENDIAN
  78. saddr.s_addr = swab32(nodeid);
  79. #else
  80. saddr.s_addr = nodeid;
  81. #endif
  82. sprintf(buffer, "node/pid %s/%d", inet_ntoa(saddr),pid);
  83. }
  84. else {
  85. sprintf(buffer, "node/pid %d/%d", nodeid, pid);
  86. }
  87. return buffer;
  88. }
  89. static void
  90. print_time(void)
  91. {
  92. #define MAXLEN (256)
  93. char buf[MAXLEN];
  94. char hostname[HOST_NAME_MAX];
  95. struct timeval tnow;
  96. time_t t;
  97. size_t len;
  98. char *s = buf;
  99. len = sizeof(hostname);
  100. if(gethostname(hostname, len) == 0) {
  101. char *longName;
  102. hostname[len-1] = '\0';
  103. longName = hostname;
  104. if( (longName = strstr( hostname, "." )) != NULL )
  105. *longName = '\0';
  106. }
  107. strcpy(s, hostname);
  108. s += strlen(hostname);
  109. s += snprintf(s, sizeof(buf)-(s-buf), ":%d", getpid());
  110. t = time(0);
  111. gettimeofday( &tnow, 0 );
  112. s += strftime(s, sizeof(buf)-(s-buf) , " %Y-%m-%d %T", localtime(&t));
  113. s += snprintf(s, sizeof(buf)-(s-buf), ".%03ld", tnow.tv_usec/1000);
  114. assert(s-buf < (int)sizeof(buf));
  115. printf("%s\n", buf);
  116. }
  117. static void DeliverCallback (
  118. cpg_handle_t handle,
  119. const struct cpg_name *groupName,
  120. uint32_t nodeid,
  121. uint32_t pid,
  122. void *msg,
  123. size_t msg_len)
  124. {
  125. print_time();
  126. printf("DeliverCallback: message (len=%lu)from %s: '%s'\n",
  127. (unsigned long int) msg_len, node_pid_format(nodeid, pid),
  128. (const char *)msg);
  129. }
  130. static void ConfchgCallback (
  131. cpg_handle_t handle,
  132. const struct cpg_name *groupName,
  133. const struct cpg_address *member_list, size_t member_list_entries,
  134. const struct cpg_address *left_list, size_t left_list_entries,
  135. const struct cpg_address *joined_list, size_t joined_list_entries)
  136. {
  137. unsigned int i;
  138. int result;
  139. uint32_t nodeid;
  140. print_time();
  141. printf("ConfchgCallback: group '");
  142. print_cpgname(groupName);
  143. printf("'\n");
  144. print_localnodeid(handle);
  145. for (i=0; i<joined_list_entries; i++) {
  146. printf("joined %s reason: %d\n",
  147. node_pid_format(joined_list[i].nodeid, joined_list[i].pid),
  148. joined_list[i].reason);
  149. }
  150. for (i=0; i<left_list_entries; i++) {
  151. printf("left %s reason: %d\n",
  152. node_pid_format(left_list[i].nodeid, left_list[i].pid),
  153. left_list[i].reason);
  154. }
  155. printf("nodes in group now %lu\n",
  156. (unsigned long int) member_list_entries);
  157. for (i=0; i<member_list_entries; i++) {
  158. printf("%s\n",
  159. node_pid_format(member_list[i].nodeid, member_list[i].pid));
  160. }
  161. result = cpg_local_get(handle, &nodeid);
  162. if(result != CS_OK) {
  163. printf("failed to get local nodeid %d\n", result);
  164. nodeid = 0;
  165. }
  166. /* Is it us??
  167. NOTE: in reality we should also check the nodeid */
  168. if (left_list_entries && (pid_t)left_list[0].pid == getpid()) {
  169. printf("We might have left the building pid %d\n", left_list[0].pid);
  170. /* can only use nodeidStart as a reliable check (version <= 1.4.2) */
  171. if(nodeidStart) {
  172. /* report dynamic nature of nodeid returned from local_get */
  173. /* local get of nodeid might change identity from original! */
  174. if(htonl((uint32_t)nodeid) == INADDR_LOOPBACK) {
  175. printf("We probably left the building switched identity? start nodeid %d nodeid %d current nodeid %d pid %d\n", nodeidStart, left_list[0].nodeid, nodeid, left_list[0].pid);
  176. } else if(htonl((uint32_t)left_list[0].nodeid) == INADDR_LOOPBACK) {
  177. printf("We probably left the building started alone? start nodeid %d nodeid %d current nodeid %d pid %d\n", nodeidStart, left_list[0].nodeid, nodeid, left_list[0].pid);
  178. }
  179. /* a possibly reliable way to check is based on original address */
  180. if(left_list[0].nodeid == nodeidStart) {
  181. printf("We have left the building direct match start nodeid %d nodeid %d local get current nodeid %d pid %d\n", nodeidStart, left_list[0].nodeid, nodeid, left_list[0].pid);
  182. // quit = 1;
  183. restart = 1;
  184. } else {
  185. printf("Probably another node with matching pid start nodeid %d nodeid %d current nodeid %d pid %d\n", nodeidStart, left_list[0].nodeid, nodeid, left_list[0].pid);
  186. }
  187. }
  188. }
  189. }
  190. static void TotemConfchgCallback (
  191. cpg_handle_t handle,
  192. struct cpg_ring_id ring_id,
  193. uint32_t member_list_entries,
  194. const uint32_t *member_list)
  195. {
  196. unsigned int i;
  197. printf("\n");
  198. print_time();
  199. printf ("TotemConfchgCallback: ringid (%u.%"PRIu64")\n",
  200. ring_id.nodeid, ring_id.seq);
  201. printf("active processors %lu: ",
  202. (unsigned long int) member_list_entries);
  203. for (i=0; i<member_list_entries; i++) {
  204. printf("%d ", member_list[i]);
  205. }
  206. printf ("\n");
  207. }
  208. static cpg_model_v1_data_t model_data = {
  209. .cpg_deliver_fn = DeliverCallback,
  210. .cpg_confchg_fn = ConfchgCallback,
  211. .cpg_totem_confchg_fn = TotemConfchgCallback,
  212. .flags = CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF,
  213. };
  214. static void sigintr_handler (int signum) __attribute__((noreturn));
  215. static void sigintr_handler (int signum) {
  216. exit (0);
  217. }
  218. static struct cpg_name group_name;
  219. #define retrybackoff(counter) { \
  220. counter++; \
  221. printf("Restart operation after %ds\n", counter); \
  222. sleep((unsigned int)counter); \
  223. restart = 1; \
  224. continue; \
  225. }
  226. #define cs_repeat_init(counter, max, code) do { \
  227. code; \
  228. if (result == CS_ERR_TRY_AGAIN || result == CS_ERR_QUEUE_FULL || result == CS_ERR_LIBRARY) { \
  229. counter++; \
  230. printf("Retrying operation after %ds\n", counter); \
  231. sleep((unsigned int)counter); \
  232. } else { \
  233. break; \
  234. } \
  235. } while (counter < max)
  236. #define cs_repeat(counter, max, code) do { \
  237. code; \
  238. if (result == CS_ERR_TRY_AGAIN || result == CS_ERR_QUEUE_FULL) { \
  239. counter++; \
  240. printf("Retrying operation after %ds\n", counter); \
  241. sleep((unsigned int)counter); \
  242. } else { \
  243. break; \
  244. } \
  245. } while (counter < max)
  246. static void print_localnodeid(cpg_handle_t handle)
  247. {
  248. char addrStr[128];
  249. unsigned int retries;
  250. unsigned int nodeid;
  251. struct sockaddr_storage addr;
  252. struct sockaddr_in *v4addr = (struct sockaddr_in *)&addr;
  253. int result;
  254. retries = 0;
  255. cs_repeat(retries, 30, result = cpg_local_get(handle, &nodeid));
  256. if (result != CS_OK) {
  257. printf ("Could not get local node id\n");
  258. } else {
  259. v4addr->sin_addr.s_addr = nodeid;
  260. if(inet_ntop(AF_INET, (const void *)&v4addr->sin_addr.s_addr,
  261. addrStr, (socklen_t)sizeof(addrStr)) == NULL) {
  262. addrStr[0] = 0;
  263. }
  264. printf ("Local node id is %s/%x result %d\n", addrStr, nodeid, result);
  265. }
  266. }
  267. int main (int argc, char *argv[]) {
  268. cpg_handle_t handle;
  269. fd_set read_fds;
  270. int select_fd;
  271. int result;
  272. int retries;
  273. const char *options = "i";
  274. int opt;
  275. unsigned int nodeid;
  276. char *fgets_res;
  277. struct cpg_address member_list[64];
  278. int member_list_entries;
  279. int i;
  280. int recnt;
  281. int doexit;
  282. const char *exitStr = "EXIT";
  283. doexit = 0;
  284. #ifdef QBLOG
  285. qb_log_init("testcpg", LOG_USER, LOG_ERR);
  286. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
  287. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
  288. QB_LOG_FILTER_FILE, "*", LOG_TRACE);
  289. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
  290. qb_log_format_set(QB_LOG_STDERR, "[%p] %f %b");
  291. #endif
  292. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  293. switch (opt) {
  294. case 'i':
  295. show_ip = 1;
  296. break;
  297. }
  298. }
  299. if (argc > optind) {
  300. strcpy(group_name.value, argv[optind]);
  301. group_name.length = strlen(argv[optind]);
  302. }
  303. else {
  304. strcpy(group_name.value, "GROUP");
  305. group_name.length = 6;
  306. }
  307. recnt = 0;
  308. printf ("Type %s to finish\n", exitStr);
  309. restart = 1;
  310. do {
  311. if(restart) {
  312. restart = 0;
  313. retries = 0;
  314. cs_repeat_init(retries, 30, result = cpg_model_initialize (&handle, CPG_MODEL_V1, (cpg_model_data_t *)&model_data, NULL));
  315. if (result != CS_OK) {
  316. printf ("Could not initialize Cluster Process Group API instance error %d\n", result);
  317. retrybackoff(recnt);
  318. }
  319. retries = 0;
  320. cs_repeat(retries, 30, result = cpg_local_get(handle, &nodeid));
  321. if (result != CS_OK) {
  322. printf ("Could not get local node id\n");
  323. retrybackoff(recnt);
  324. }
  325. printf ("Local node id is %x\n", nodeid);
  326. nodeidStart = nodeid;
  327. retries = 0;
  328. cs_repeat(retries, 30, result = cpg_join(handle, &group_name));
  329. if (result != CS_OK) {
  330. printf ("Could not join process group, error %d\n", result);
  331. retrybackoff(recnt);
  332. }
  333. retries = 0;
  334. cs_repeat(retries, 30, result = cpg_membership_get (handle, &group_name,
  335. (struct cpg_address *)&member_list, &member_list_entries));
  336. if (result != CS_OK) {
  337. printf ("Could not get current membership list %d\n", result);
  338. retrybackoff(recnt);
  339. }
  340. recnt = 0;
  341. printf ("membership list\n");
  342. for (i = 0; i < member_list_entries; i++) {
  343. printf ("node id %d pid %d\n", member_list[i].nodeid,
  344. member_list[i].pid);
  345. }
  346. FD_ZERO (&read_fds);
  347. cpg_fd_get(handle, &select_fd);
  348. }
  349. FD_SET (select_fd, &read_fds);
  350. FD_SET (STDIN_FILENO, &read_fds);
  351. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  352. if (result == -1) {
  353. perror ("select\n");
  354. }
  355. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  356. char inbuf[132];
  357. struct iovec iov;
  358. fgets_res = fgets(inbuf, (int)sizeof(inbuf), stdin);
  359. if (fgets_res == NULL) {
  360. doexit = 1;
  361. cpg_leave(handle, &group_name);
  362. }
  363. if (strncmp(inbuf, exitStr, strlen(exitStr)) == 0) {
  364. doexit = 1;
  365. cpg_leave(handle, &group_name);
  366. }
  367. else {
  368. iov.iov_base = inbuf;
  369. iov.iov_len = strlen(inbuf)+1;
  370. cpg_mcast_joined(handle, CPG_TYPE_AGREED, &iov, 1);
  371. }
  372. }
  373. if (FD_ISSET (select_fd, &read_fds)) {
  374. if (cpg_dispatch (handle, CS_DISPATCH_ALL) != CS_OK) {
  375. if(doexit) {
  376. exit(1);
  377. }
  378. restart = 1;
  379. }
  380. }
  381. if(restart) {
  382. if(!doexit) {
  383. result = cpg_finalize (handle);
  384. printf ("Finalize+restart result is %d (should be 1)\n", result);
  385. continue;
  386. }
  387. }
  388. } while (result && !quit && !doexit);
  389. result = cpg_finalize (handle);
  390. printf ("Finalize result is %d (should be 1)\n", result);
  391. return (0);
  392. }