/* * return 1 if quorate * 0 if not quorate * -1 on error */ static int show_status(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type) { int is_quorate; int err; err=quorum_getquorate(q_handle, &is_quorate); if (err != CS_OK) { fprintf(stderr, "Unable to get cluster quorate status: %s\n", cs_strerror(err)); goto quorum_err; } err=quorum_trackstart(q_handle, CS_TRACK_CURRENT); if (err != CS_OK) { fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err)); goto quorum_err; } g_called = 0; while (g_called == 0 && err == CS_OK) { err = quorum_dispatch(q_handle, CS_DISPATCH_ONE); if (err != CS_OK) { fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err)); } } if (quorum_trackstop(q_handle) != CS_OK) { fprintf(stderr, "Unable to stop quorum status tracking: %s\n", cs_strerror(err)); } if (using_votequorum()) { if ( (err=votequorum_trackstart(v_handle, 0LL, CS_TRACK_CURRENT)) != CS_OK) { fprintf(stderr, "Unable to start votequorum status tracking: %s\n", cs_strerror(err)); goto quorum_err; } g_vq_called = 0; while (g_vq_called == 0 && err == CS_OK) { err = votequorum_dispatch(v_handle, CS_DISPATCH_ONE); if (err != CS_OK) { fprintf(stderr, "Unable to dispatch votequorum status: %s\n", cs_strerror(err)); } } } quorum_err: if (err != CS_OK) { return -1; } err = display_quorum_data(is_quorate, nodeid_format, name_format, sort_type, 0); if (err != CS_OK) { return -1; } return is_quorate; }
static int q_lib_init(void) { votequorum_callbacks_t vq_callbacks; quorum_callbacks_t q_callbacks; int ret = 0; int retry = 3; int fd; if (vq_handle == 0) { qb_log (LOG_INFO, "votequorum_initialize"); vq_callbacks.votequorum_notify_fn = votequorum_notification_fn; vq_callbacks.votequorum_expectedvotes_notify_fn = NULL; ret = CS_ERR_NOT_EXIST; while (ret == CS_ERR_NOT_EXIST && retry > 0) { ret = votequorum_initialize (&vq_handle, &vq_callbacks); if (ret == CS_ERR_NOT_EXIST) { sleep (1); retry--; } } if (ret != CS_OK) { qb_log (LOG_ERR, "votequorum_initialize FAILED: %d", ret); vq_handle = 0; } else { ret = votequorum_trackstart (vq_handle, vq_handle, CS_TRACK_CHANGES); if (ret != CS_OK) { qb_log (LOG_ERR, "votequorum_trackstart FAILED: %d", ret); } votequorum_fd_get (vq_handle, &fd); qb_loop_poll_add (ta_poll_handle_get(), QB_LOOP_MED, fd, POLLIN|POLLNVAL, NULL, vq_dispatch_wrapper_fn); } } if (q_handle == 0) { uint32_t q_type; qb_log (LOG_INFO, "quorum_initialize"); q_callbacks.quorum_notify_fn = quorum_notification_fn; ret = quorum_initialize (&q_handle, &q_callbacks, &q_type); if (ret != CS_OK) { qb_log (LOG_ERR, "quorum_initialize FAILED: %d", ret); q_handle = 0; } else { ret = quorum_trackstart (q_handle, CS_TRACK_CHANGES); if (ret != CS_OK) { qb_log (LOG_ERR, "quorum_trackstart FAILED: %d", ret); } quorum_fd_get (q_handle, &fd); qb_loop_poll_add (ta_poll_handle_get(), QB_LOOP_MED, fd, POLLIN|POLLNVAL, NULL, q_dispatch_wrapper_fn); } } return ret; }
static int monitor_status(nodeid_format_t nodeid_format, name_format_t name_format, sorttype_t sort_type) { int err; int loop = 0; if (q_type == QUORUM_FREE) { printf("\nQuorum is not configured - cannot monitor\n"); return show_status(nodeid_format, name_format, sort_type); } err=quorum_trackstart(q_handle, CS_TRACK_CHANGES); if (err != CS_OK) { fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err)); goto quorum_err; } if (using_votequorum()) { if ( (err=votequorum_trackstart(v_handle, 0LL, CS_TRACK_CHANGES)) != CS_OK) { fprintf(stderr, "Unable to start votequorum status tracking: %s\n", cs_strerror(err)); goto quorum_err; } } while (1) { err = quorum_dispatch(q_handle, CS_DISPATCH_ONE); if (err != CS_OK) { fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err)); goto quorum_err; } if (using_votequorum()) { g_vq_called = 0; while (!g_vq_called) { err = votequorum_dispatch(v_handle, CS_DISPATCH_ONE); if (err != CS_OK) { fprintf(stderr, "Unable to dispatch votequorum status: %s\n", cs_strerror(err)); goto quorum_err; } } } err = display_quorum_data(g_quorate, nodeid_format, name_format, sort_type, loop); printf("\n"); loop = 1; if (err != CS_OK) { fprintf(stderr, "Unable to display quorum data: %s\n", cs_strerror(err)); goto quorum_err; } } quorum_err: return -1; }
int main(int argc, char *argv[]) { int ret = 0; int cast_vote = 1, master_wins = 0; int pollcount=0, polltime=1, quiet=0, once=0; int send_old_ringid = 0; int err; int opt; votequorum_callbacks_t callbacks; const char *devicename = "QDEVICE"; const char *options = "F:n:p:t:cmq1h"; memset(&callbacks, 0, sizeof(callbacks)); callbacks.votequorum_notify_fn = votequorum_notification_fn; while ((opt = getopt(argc, argv, options)) != -1) { switch (opt) { case 'm': master_wins = 1; break; case 'c': cast_vote = 1; break; case '1': once = 1; break; case 'q': quiet = 1; break; case 'F': send_old_ringid = atoi(optarg)+1; break; case 'p': pollcount = atoi(optarg)+1; break; case 'n': devicename = strdup(optarg); break; case 't': polltime = atoi(optarg); break; case 'h': usage(argv[0]); exit(0); } } if ( (err=votequorum_initialize(&handle, &callbacks)) != CS_OK) { fprintf(stderr, "votequorum_initialize FAILED: %d\n", err); return -1; } if (quiet && once) { fprintf(stderr, "setting both -q (quet) and -1 (once) makes no sense\n"); usage(argv[0]); exit(1); } if (!quiet) { print_info(1); } if (once) { exit(0); } if (argc >= 2) { if ( (err = votequorum_trackstart(handle, handle, CS_TRACK_CHANGES)) != CS_OK) { fprintf(stderr, "votequorum_trackstart FAILED: %d\n", err); ret = -1; goto out; } if ( (err=votequorum_qdevice_register(handle, devicename)) != CS_OK) { fprintf(stderr, "qdevice_register FAILED: %d\n", err); ret = -1; goto out; } if ( (err=votequorum_qdevice_master_wins(handle, devicename, master_wins)) != CS_OK) { fprintf(stderr, "qdevice_master_wins FAILED: %d\n", err); ret = -1; goto out; } while (--pollcount) { if (votequorum_dispatch(handle, CS_DISPATCH_ALL) != CS_OK) { fprintf(stderr, "votequorum_dispatch error\n"); ret = -1; goto out; } if (!quiet) print_info(0); if (no_sent_old_ringid + 1 >= send_old_ringid) { /* * Finally send correct ringid */ memcpy(&ring_id_to_send, &last_received_ring_id, sizeof(ring_id_to_send)); } else { no_sent_old_ringid++; } if ((err=votequorum_qdevice_poll(handle, devicename, cast_vote, ring_id_to_send)) != CS_OK && err != CS_ERR_MESSAGE_ERROR) { fprintf(stderr, "qdevice poll FAILED: %d\n", err); ret = -1; goto out; } if (err == CS_ERR_MESSAGE_ERROR) { fprintf(stderr, "qdevice poll passed OLD ring_id\n"); } if (!quiet) print_info(0); sleep(polltime); } if ((err= votequorum_qdevice_unregister(handle, devicename)) != CS_OK) { fprintf(stderr, "qdevice unregister FAILED: %d\n", err); ret = -1; goto out; } } if (!quiet) print_info(1); out: votequorum_finalize(handle); return ret; }
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_notify_fn = votequorum_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_FLAG_TWONODE) printf("2Node "); if (info.flags & VOTEQUORUM_INFO_FLAG_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_LEAVE_REMOVE) printf("LeaveRemove "); 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_FLAG_TWONODE) printf("2Node "); if (info.flags & VOTEQUORUM_INFO_FLAG_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_LEAVE_REMOVE) printf("LeaveRemove "); printf("\n"); } } printf("Waiting for votequorum events, press ^C to finish\n"); printf("-------------------\n"); while (1) { if (votequorum_dispatch(g_handle, CS_DISPATCH_ALL) != CS_OK) { fprintf(stderr, "votequorum_dispatch error\n"); return -1; } } return 0; }