testvotequorum1.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <stdio.h>
  37. #include <string.h>
  38. #include <stdint.h>
  39. #include <stdlib.h>
  40. #include <unistd.h>
  41. #include <corosync/corotypes.h>
  42. #include <corosync/votequorum.h>
  43. static votequorum_handle_t g_handle;
  44. static const char *node_state(int state)
  45. {
  46. switch (state) {
  47. case NODESTATE_JOINING:
  48. return "Joining";
  49. break;
  50. case NODESTATE_MEMBER:
  51. return "Member";
  52. break;
  53. case NODESTATE_DEAD:
  54. return "Dead";
  55. break;
  56. case NODESTATE_LEAVING:
  57. return "Leaving";
  58. break;
  59. case NODESTATE_DISALLOWED:
  60. return "Disallowed";
  61. break;
  62. default:
  63. return "UNKNOWN";
  64. break;
  65. }
  66. }
  67. static void votequorum_expectedvotes_notification_fn(
  68. votequorum_handle_t handle,
  69. uint64_t context,
  70. uint32_t expected_votes
  71. )
  72. {
  73. printf("votequorum expectedvotes notification called \n");
  74. printf(" expected_votes = %d\n", expected_votes);
  75. }
  76. static void votequorum_notification_fn(
  77. votequorum_handle_t handle,
  78. uint64_t context,
  79. uint32_t quorate,
  80. uint32_t node_list_entries,
  81. votequorum_node_t node_list[]
  82. )
  83. {
  84. int i;
  85. printf("votequorum notification called \n");
  86. printf(" quorate = %d\n", quorate);
  87. printf(" number of nodes = %d\n", node_list_entries);
  88. for (i = 0; i< node_list_entries; i++) {
  89. printf(" %d: %s\n", node_list[i].nodeid, node_state(node_list[i].state));
  90. }
  91. printf("\n");
  92. }
  93. int main(int argc, char *argv[])
  94. {
  95. struct votequorum_info info;
  96. votequorum_callbacks_t callbacks;
  97. int err;
  98. if (argc > 1 && strcmp(argv[1], "-h")==0) {
  99. fprintf(stderr, "usage: %s [new-expected] [new-votes]\n", argv[0]);
  100. return 0;
  101. }
  102. callbacks.votequorum_notify_fn = votequorum_notification_fn;
  103. callbacks.votequorum_expectedvotes_notify_fn = votequorum_expectedvotes_notification_fn;
  104. if ( (err=votequorum_initialize(&g_handle, &callbacks)) != CS_OK)
  105. fprintf(stderr, "votequorum_initialize FAILED: %d\n", err);
  106. if ( (err = votequorum_trackstart(g_handle, g_handle, CS_TRACK_CHANGES)) != CS_OK)
  107. fprintf(stderr, "votequorum_trackstart FAILED: %d\n", err);
  108. if ( (err=votequorum_getinfo(g_handle, 0, &info)) != CS_OK)
  109. fprintf(stderr, "votequorum_getinfo FAILED: %d\n", err);
  110. else {
  111. printf("node votes %d\n", info.node_votes);
  112. printf("expected votes %d\n", info.node_expected_votes);
  113. printf("highest expected %d\n", info.highest_expected);
  114. printf("total votes %d\n", info.total_votes);
  115. printf("quorum %d\n", info.quorum);
  116. printf("flags ");
  117. if (info.flags & VOTEQUORUM_INFO_FLAG_HASSTATE) printf("HasState ");
  118. if (info.flags & VOTEQUORUM_INFO_FLAG_DISALLOWED) printf("Disallowed ");
  119. if (info.flags & VOTEQUORUM_INFO_FLAG_TWONODE) printf("2Node ");
  120. if (info.flags & VOTEQUORUM_INFO_FLAG_QUORATE) printf("Quorate ");
  121. printf("\n");
  122. }
  123. if (argc >= 2 && atoi(argv[1])) {
  124. if ( (err=votequorum_setexpected(g_handle, atoi(argv[1]))) != CS_OK)
  125. fprintf(stderr, "set expected votes FAILED: %d\n", err);
  126. }
  127. if (argc >= 3 && atoi(argv[2])) {
  128. if ( (err=votequorum_setvotes(g_handle, 0, atoi(argv[2]))) != CS_OK)
  129. fprintf(stderr, "set votes FAILED: %d\n", err);
  130. }
  131. if (argc >= 2) {
  132. if ( (err=votequorum_getinfo(g_handle, 0, &info)) != CS_OK)
  133. fprintf(stderr, "votequorum_getinfo2 FAILED: %d\n", err);
  134. else {
  135. printf("-------------------\n");
  136. printf("node votes %d\n", info.node_votes);
  137. printf("expected votes %d\n", info.node_expected_votes);
  138. printf("highest expected %d\n", info.highest_expected);
  139. printf("total votes %d\n", info.total_votes);
  140. printf("votequorum %d\n", info.quorum);
  141. printf("flags ");
  142. if (info.flags & VOTEQUORUM_INFO_FLAG_HASSTATE) printf("HasState ");
  143. if (info.flags & VOTEQUORUM_INFO_FLAG_DISALLOWED) printf("Disallowed ");
  144. if (info.flags & VOTEQUORUM_INFO_FLAG_TWONODE) printf("2Node ");
  145. if (info.flags & VOTEQUORUM_INFO_FLAG_QUORATE) printf("Quorate ");
  146. printf("\n");
  147. }
  148. }
  149. printf("Waiting for votequorum events, press ^C to finish\n");
  150. printf("-------------------\n");
  151. while (1)
  152. votequorum_dispatch(g_handle, CS_DISPATCH_ALL);
  153. return 0;
  154. }