testvotequorum1.c 5.6 KB

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