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