4
0

testvotequorum1.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 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 CONTIBUTORS "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 <inttypes.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdint.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <corosync/corotypes.h>
  43. #include <corosync/votequorum.h>
  44. static votequorum_handle_t g_handle;
  45. static const char *node_state(int state)
  46. {
  47. switch (state) {
  48. case VOTEQUORUM_NODESTATE_MEMBER:
  49. return "Member";
  50. break;
  51. case VOTEQUORUM_NODESTATE_DEAD:
  52. return "Dead";
  53. break;
  54. case VOTEQUORUM_NODESTATE_LEAVING:
  55. return "Leaving";
  56. break;
  57. default:
  58. return "UNKNOWN";
  59. break;
  60. }
  61. }
  62. static void votequorum_expectedvotes_notification_fn(
  63. votequorum_handle_t handle,
  64. uint64_t context,
  65. uint32_t expected_votes
  66. )
  67. {
  68. printf("votequorum expectedvotes notification called \n");
  69. printf(" expected_votes = %d\n", expected_votes);
  70. }
  71. static void votequorum_quorum_notification_fn(
  72. votequorum_handle_t handle,
  73. uint64_t context,
  74. uint32_t quorate,
  75. uint32_t node_list_entries,
  76. votequorum_node_t node_list[]
  77. )
  78. {
  79. int i;
  80. printf("votequorum quorum notification called \n");
  81. printf(" number of nodes = %d\n", node_list_entries);
  82. printf(" quorate = %d\n", quorate);
  83. for (i = 0; i< node_list_entries; i++) {
  84. printf(" " CS_PRI_NODE_ID ": %s\n", node_list[i].nodeid, node_state(node_list[i].state));
  85. }
  86. }
  87. static void votequorum_nodelist_notification_fn(
  88. votequorum_handle_t handle,
  89. uint64_t context,
  90. votequorum_ring_id_t ring_id,
  91. uint32_t node_list_entries,
  92. uint32_t node_list[]
  93. )
  94. {
  95. int i;
  96. printf("votequorum nodelist notification called \n");
  97. printf(" number of nodes = %d\n", node_list_entries);
  98. printf(" current ringid = (" CS_PRI_RING_ID ")\n", ring_id.nodeid, ring_id.seq);
  99. printf(" nodes: ");
  100. for (i = 0; i< node_list_entries; i++) {
  101. printf(CS_PRI_NODE_ID " ", node_list[i]);
  102. }
  103. printf("\n\n");
  104. }
  105. int main(int argc, char *argv[])
  106. {
  107. struct votequorum_info info;
  108. votequorum_callbacks_t callbacks;
  109. int err;
  110. if (argc > 1 && strcmp(argv[1], "-h")==0) {
  111. fprintf(stderr, "usage: %s [new-expected] [new-votes]\n", argv[0]);
  112. return 0;
  113. }
  114. callbacks.votequorum_quorum_notify_fn = votequorum_quorum_notification_fn;
  115. callbacks.votequorum_nodelist_notify_fn = votequorum_nodelist_notification_fn;
  116. callbacks.votequorum_expectedvotes_notify_fn = votequorum_expectedvotes_notification_fn;
  117. if ( (err=votequorum_initialize(&g_handle, &callbacks)) != CS_OK)
  118. fprintf(stderr, "votequorum_initialize FAILED: %d\n", err);
  119. if ( (err = votequorum_trackstart(g_handle, g_handle, CS_TRACK_CHANGES)) != CS_OK)
  120. fprintf(stderr, "votequorum_trackstart FAILED: %d\n", err);
  121. if ( (err=votequorum_getinfo(g_handle, 0, &info)) != CS_OK)
  122. fprintf(stderr, "votequorum_getinfo FAILED: %d\n", err);
  123. else {
  124. printf("node votes %d\n", info.node_votes);
  125. printf("expected votes %d\n", info.node_expected_votes);
  126. printf("highest expected %d\n", info.highest_expected);
  127. printf("total votes %d\n", info.total_votes);
  128. printf("quorum %d\n", info.quorum);
  129. printf("flags ");
  130. if (info.flags & VOTEQUORUM_INFO_TWONODE) printf("2Node ");
  131. if (info.flags & VOTEQUORUM_INFO_QUORATE) printf("Quorate ");
  132. if (info.flags & VOTEQUORUM_INFO_WAIT_FOR_ALL) printf("WaitForAll ");
  133. if (info.flags & VOTEQUORUM_INFO_LAST_MAN_STANDING) printf("LastManStanding ");
  134. if (info.flags & VOTEQUORUM_INFO_AUTO_TIE_BREAKER) printf("AutoTieBreaker ");
  135. if (info.flags & VOTEQUORUM_INFO_ALLOW_DOWNSCALE) printf("AllowDownscale ");
  136. printf("\n");
  137. }
  138. if (argc >= 2 && atoi(argv[1])) {
  139. if ( (err=votequorum_setexpected(g_handle, atoi(argv[1]))) != CS_OK)
  140. fprintf(stderr, "set expected votes FAILED: %d\n", err);
  141. }
  142. if (argc >= 3 && atoi(argv[2])) {
  143. if ( (err=votequorum_setvotes(g_handle, 0, atoi(argv[2]))) != CS_OK)
  144. fprintf(stderr, "set votes FAILED: %d\n", err);
  145. }
  146. if (argc >= 2) {
  147. if ( (err=votequorum_getinfo(g_handle, 0, &info)) != CS_OK)
  148. fprintf(stderr, "votequorum_getinfo2 FAILED: %d\n", err);
  149. else {
  150. printf("-------------------\n");
  151. printf("node votes %d\n", info.node_votes);
  152. printf("expected votes %d\n", info.node_expected_votes);
  153. printf("highest expected %d\n", info.highest_expected);
  154. printf("total votes %d\n", info.total_votes);
  155. printf("votequorum %d\n", info.quorum);
  156. printf("flags ");
  157. if (info.flags & VOTEQUORUM_INFO_TWONODE) printf("2Node ");
  158. if (info.flags & VOTEQUORUM_INFO_QUORATE) printf("Quorate ");
  159. if (info.flags & VOTEQUORUM_INFO_WAIT_FOR_ALL) printf("WaitForAll ");
  160. if (info.flags & VOTEQUORUM_INFO_LAST_MAN_STANDING) printf("LastManStanding ");
  161. if (info.flags & VOTEQUORUM_INFO_AUTO_TIE_BREAKER) printf("AutoTieBreaker ");
  162. if (info.flags & VOTEQUORUM_INFO_ALLOW_DOWNSCALE) printf("AllowDownscale ");
  163. printf("\n");
  164. }
  165. }
  166. printf("Waiting for votequorum events, press ^C to finish\n");
  167. printf("-------------------\n");
  168. if (votequorum_dispatch(g_handle, CS_DISPATCH_BLOCKING) != CS_OK) {
  169. fprintf(stderr, "votequorum_dispatch error\n");
  170. return -1;
  171. }
  172. return 0;
  173. }