summaryrefslogtreecommitdiffstats
path: root/test/testvotequorum1.c
blob: 70fea5045543cf1409ec1c9e4d0aae8159cb1e94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Copyright (c) 2009 Red Hat, Inc.
 *
 * All rights reserved.
 *
 * Author: Christine Caulfield (ccaulfie@redhat.com)
 *
 * This software licensed under BSD license, the text of which follows:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name of the MontaVista Software, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <config.h>

#include <sys/types.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <corosync/corotypes.h>
#include <corosync/votequorum.h>

static votequorum_handle_t g_handle;

static const char *node_state(int state)
{
	switch (state) {
	case VOTEQUORUM_NODESTATE_MEMBER:
		return "Member";
		break;
	case VOTEQUORUM_NODESTATE_DEAD:
		return "Dead";
		break;
	case VOTEQUORUM_NODESTATE_LEAVING:
		return "Leaving";
		break;
	default:
		return "UNKNOWN";
		break;
	}
}


static void votequorum_expectedvotes_notification_fn(
	votequorum_handle_t handle,
	uint64_t context,
	uint32_t expected_votes
	)
{
	printf("votequorum expectedvotes notification called \n");
	printf("  expected_votes = %d\n", expected_votes);
}


static void votequorum_quorum_notification_fn(
	votequorum_handle_t handle,
	uint64_t context,
	uint32_t quorate,
	uint32_t node_list_entries,
	votequorum_node_t node_list[]
	)
{
	int i;

	printf("votequorum quorum notification called \n");
	printf("  number of nodes = %d\n", node_list_entries);
	printf("  quorate         = %d\n", quorate);

	for (i = 0; i< node_list_entries; i++) {
		printf("      " CS_PRI_NODE_ID ": %s\n", node_list[i].nodeid, node_state(node_list[i].state));
	}

}

static void votequorum_nodelist_notification_fn(
	votequorum_handle_t handle,
	uint64_t context,
	votequorum_ring_id_t ring_id,
	uint32_t node_list_entries,
	uint32_t node_list[]
	)
{
	int i;

	printf("votequorum nodelist notification called \n");
	printf("  number of nodes = %d\n", node_list_entries);
	printf("  current ringid  = (" CS_PRI_RING_ID ")\n", ring_id.nodeid, ring_id.seq);
	printf("  nodes: ");

	for (i = 0; i< node_list_entries; i++) {
		printf(CS_PRI_NODE_ID " ", node_list[i]);
	}
	printf("\n\n");
}


int main(int argc, char *argv[])
{
	struct votequorum_info info;
	votequorum_callbacks_t callbacks;
	int err;

	if (argc > 1 && strcmp(argv[1], "-h")==0) {
		fprintf(stderr, "usage: %s [new-expected] [new-votes]\n", argv[0]);
		return 0;
	}

	callbacks.votequorum_quorum_notify_fn = votequorum_quorum_notification_fn;
	callbacks.votequorum_nodelist_notify_fn = votequorum_nodelist_notification_fn;
	callbacks.votequorum_expectedvotes_notify_fn = votequorum_expectedvotes_notification_fn;

	if ( (err=votequorum_initialize(&g_handle, &callbacks)) != CS_OK)
		fprintf(stderr, "votequorum_initialize FAILED: %d\n", err);

	if ( (err = votequorum_trackstart(g_handle, g_handle, CS_TRACK_CHANGES)) != CS_OK)
		fprintf(stderr, "votequorum_trackstart FAILED: %d\n", err);

	if ( (err=votequorum_getinfo(g_handle, 0, &info)) != CS_OK)
		fprintf(stderr, "votequorum_getinfo FAILED: %d\n", err);
	else {
		printf("node votes       %d\n", info.node_votes);
		printf("expected votes   %d\n", info.node_expected_votes);
		printf("highest expected %d\n", info.highest_expected);
		printf("total votes      %d\n", info.total_votes);
		printf("quorum           %d\n", info.quorum);
		printf("flags            ");
		if (info.flags & VOTEQUORUM_INFO_TWONODE) printf("2Node ");
		if (info.flags & VOTEQUORUM_INFO_QUORATE) printf("Quorate ");
		if (info.flags & VOTEQUORUM_INFO_WAIT_FOR_ALL) printf("WaitForAll ");
		if (info.flags & VOTEQUORUM_INFO_LAST_MAN_STANDING) printf("LastManStanding ");
		if (info.flags & VOTEQUORUM_INFO_AUTO_TIE_BREAKER) printf("AutoTieBreaker ");
		if (info.flags & VOTEQUORUM_INFO_ALLOW_DOWNSCALE) printf("AllowDownscale ");

		printf("\n");
	}

	if (argc >= 2 && atoi(argv[1])) {
		if ( (err=votequorum_setexpected(g_handle, atoi(argv[1]))) != CS_OK)
			fprintf(stderr, "set expected votes FAILED: %d\n", err);
	}
	if (argc >= 3 && atoi(argv[2])) {
		if ( (err=votequorum_setvotes(g_handle, 0, atoi(argv[2]))) != CS_OK)
			fprintf(stderr, "set votes FAILED: %d\n", err);
	}

	if (argc >= 2) {
		if ( (err=votequorum_getinfo(g_handle, 0, &info)) != CS_OK)
			fprintf(stderr, "votequorum_getinfo2 FAILED: %d\n", err);
		else {
			printf("-------------------\n");
			printf("node votes       %d\n", info.node_votes);
			printf("expected votes   %d\n", info.node_expected_votes);
			printf("highest expected %d\n", info.highest_expected);
			printf("total votes      %d\n", info.total_votes);
			printf("votequorum           %d\n", info.quorum);
			printf("flags            ");
			if (info.flags & VOTEQUORUM_INFO_TWONODE) printf("2Node ");
			if (info.flags & VOTEQUORUM_INFO_QUORATE) printf("Quorate ");
			if (info.flags & VOTEQUORUM_INFO_WAIT_FOR_ALL) printf("WaitForAll ");
			if (info.flags & VOTEQUORUM_INFO_LAST_MAN_STANDING) printf("LastManStanding ");
			if (info.flags & VOTEQUORUM_INFO_AUTO_TIE_BREAKER) printf("AutoTieBreaker ");
			if (info.flags & VOTEQUORUM_INFO_ALLOW_DOWNSCALE) printf("AllowDownscale ");
			printf("\n");
		}
	}

	printf("Waiting for votequorum events, press ^C to finish\n");
	printf("-------------------\n");

	if (votequorum_dispatch(g_handle, CS_DISPATCH_BLOCKING) != CS_OK) {
		fprintf(stderr, "votequorum_dispatch error\n");
		return -1;
	}

	return 0;
}