static PyObject * py_cpg_leave(PyObject *self, PyObject *args) { char *name; int length, ret; cpg_handle_t handle; struct cpg_name group_name; if (!PyArg_ParseTuple(args, "ls#", &handle, &name, &length)) return NULL; if (length > CPG_MAX_NAME_LENGTH) { PyErr_Format(py_cpg_error, "Group name too long (%d, max length = %d)", length, CPG_MAX_NAME_LENGTH); return NULL; } group_name.length = length; memcpy(group_name.value, name, length); ret = cpg_leave(handle, &group_name); RETURN_ON_ERROR(ret, "cpg_leave"); Py_INCREF(Py_None); return Py_None; }
void cluster_disconnect_cpg(crm_cluster_t *cluster) { pcmk_cpg_handle = 0; if (cluster->cpg_handle) { crm_trace("Disconnecting CPG"); cpg_leave(cluster->cpg_handle, &cluster->group); cpg_finalize(cluster->cpg_handle); cluster->cpg_handle = 0; } else { crm_info("No CPG connection"); } }
cs_error_t CorosyncCpg::leave() { cs_error_t result; unsigned int snooze = 10; for (unsigned int nth_try = 0; nth_try < cpgRetries; ++nth_try) { if (CS_OK == (result = cpg_leave(handle, &group))) { break; } else if (result == CS_ERR_TRY_AGAIN) { LOG(WARNING)<< "Retrying leave"; std::chrono::microseconds dura(snooze); std::this_thread::sleep_for(dura); snooze *= 10; snooze = (snooze <= maxCpgRetrySleep) ? snooze : maxCpgRetrySleep; } else break; // Don't retry unless CPG tells us to. } return result; }
static void test_cpg(void) { cpg_handle_t cpg_handle; struct cpg_name group_name; cs_error_t cs_res; unsigned int local_nodeid; int cpg_fd; struct pollfd pfd; int poll_res; int state; int cont; ENTER(); state = 0; strcpy(group_name.value, TEST_GROUP_NAME); group_name.length = strlen(TEST_GROUP_NAME); cs_repeat(cs_res = cpg_model_initialize(&cpg_handle, CPG_MODEL_V1, (cpg_model_data_t *)&model_data, NULL)); assert(cs_res == CS_OK); cs_repeat(cs_res = cpg_join(cpg_handle, &group_name)); assert(cs_res == CS_OK); cs_repeat(cs_res = cpg_local_get(cpg_handle, &local_nodeid)); assert(cs_res == CS_OK); assert(local_nodeid == TEST_NODEID); cs_repeat(cs_res = cpg_fd_get(cpg_handle, &cpg_fd)); assert(cs_res == CS_OK); cont = 1; do { pfd.fd = cpg_fd; pfd.events = POLLIN; pfd.revents = 0; poll_res = poll(&pfd, 1, INFTIM); if (poll_res == -1) { perror("poll_res == -1"); } assert(poll_res != 0); assert(pfd.revents & POLLIN); cs_repeat(cs_res = cpg_dispatch(cpg_handle, CS_DISPATCH_ALL)); assert(cs_res == CS_OK); switch (state) { case 0: /* * Waiting for cpg_confchg_received and cpg_totem_confchg_received */ if (cpg_confchg_received && cpg_totem_confchg_received) { /* * Send first message and wait for it in next state */ send_msg(cpg_handle); state = 1; } break; case 1: if (received_msg_seq_no >= MESSAGES_TO_SENT) { cont = 0; } else if (received_msg_seq_no == sent_msg_seq_no) { /* * Message delivered so sent new one and wait for it */ send_msg(cpg_handle); } break; } } while (cont); cs_repeat(cs_res = cpg_leave(cpg_handle, &group_name)); assert(cs_res == CS_OK); cs_repeat(cs_res = cpg_finalize(cpg_handle)); assert(cs_res == CS_OK); LEAVE(); }
static void do_command (int sock, char* func, char*args[], int num_args) { int result; char response[100]; struct cpg_name group_name; ssize_t rc; size_t send_len; qb_log (LOG_TRACE, "RPC:%s() called.", func); if (strcmp ("cpg_mcast_joined",func) == 0) { struct iovec iov[5]; int a; for (a = 0; a < num_args; a++) { iov[a].iov_base = args[a]; iov[a].iov_len = strlen(args[a])+1; } cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, num_args); } else if (strcmp ("cpg_join",func) == 0) { if (strlen(args[0]) >= CPG_MAX_NAME_LENGTH) { qb_log (LOG_ERR, "Invalid group name"); exit (1); } strcpy (group_name.value, args[0]); group_name.length = strlen(args[0]); result = cpg_join (cpg_handle, &group_name); if (result != CS_OK) { qb_log (LOG_ERR, "Could not join process group, error %d", result); exit (1); } qb_log (LOG_INFO, "called cpg_join(%s)!", group_name.value); } else if (strcmp ("cpg_leave",func) == 0) { strcpy (group_name.value, args[0]); group_name.length = strlen(args[0]); result = cpg_leave (cpg_handle, &group_name); if (result != CS_OK) { qb_log (LOG_ERR, "Could not leave process group, error %d", result); exit (1); } qb_log (LOG_INFO, "called cpg_leave(%s)!", group_name.value); } else if (strcmp ("cpg_initialize",func) == 0) { int retry_count = 0; result = cpg_initialize (&cpg_handle, &callbacks); while (result != CS_OK) { qb_log (LOG_ERR, "cpg_initialize error %d (attempt %d)", result, retry_count); if (retry_count >= 3) { exit (1); } sleep(1); retry_count++; result = cpg_initialize (&cpg_handle, &callbacks); } cpg_fd_get (cpg_handle, &cpg_fd); qb_loop_poll_add (ta_poll_handle_get(), QB_LOOP_MED, cpg_fd, POLLIN|POLLNVAL, NULL, cpg_dispatch_wrapper_fn); } else if (strcmp ("cpg_local_get", func) == 0) { unsigned int local_nodeid; cpg_local_get (cpg_handle, &local_nodeid); snprintf (response, 100, "%u",local_nodeid); send_len = strlen (response); rc = send (sock, response, send_len, 0); assert(rc == send_len); } else if (strcmp ("cpg_finalize", func) == 0) { if (cpg_handle > 0) { cpg_finalize (cpg_handle); cpg_handle = 0; } } else if (strcmp ("record_config_events", func) == 0) { record_config_events (sock); } else if (strcmp ("record_messages", func) == 0) { record_messages (); } else if (strcmp ("read_config_event", func) == 0) { read_config_event (sock); } else if (strcmp ("read_messages", func) == 0) { read_messages (sock, args[0]); } else if (strcmp ("msg_blaster_zcb", func) == 0) { msg_blaster_zcb (sock, args[0]); } else if (strcmp ("pcmk_test", func) == 0) { pcmk_test = 1; } else if (strcmp ("msg_blaster", func) == 0) { msg_blaster (sock, args[0]); } else if (strcmp ("context_test", func) == 0) { context_test (sock); } else if (strcmp ("are_you_ok_dude", func) == 0) { snprintf (response, 100, "%s", OK_STR); send_len = strlen (response); rc = send (sock, response, strlen (response), 0); assert(rc == send_len); } else if (strcmp ("cfg_shutdown", func) == 0) { qb_log (LOG_INFO, "calling %s() called!", func); result = corosync_cfg_try_shutdown (cfg_handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST); qb_log (LOG_INFO,"%s() returned %d!", func, result); } else if (strcmp ("cfg_initialize",func) == 0) { int retry_count = 0; qb_log (LOG_INFO,"%s() called!", func); result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks); while (result != CS_OK) { qb_log (LOG_ERR, "cfg_initialize error %d (attempt %d)", result, retry_count); if (retry_count >= 3) { exit (1); } sleep(1); retry_count++; result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks); } qb_log (LOG_INFO,"corosync_cfg_initialize() == %d", result); result = corosync_cfg_fd_get (cfg_handle, &cfg_fd); qb_log (LOG_INFO,"corosync_cfg_fd_get() == %d", result); qb_loop_poll_add (ta_poll_handle_get(), QB_LOOP_MED, cfg_fd, POLLIN|POLLNVAL, NULL, cfg_dispatch_wrapper_fn); } else { qb_log(LOG_ERR, "RPC:%s not supported!", func); } }
int main (int argc, char *argv[]) { cpg_handle_t handle; fd_set read_fds; int select_fd; int result; const char *options = "i"; int opt; unsigned int nodeid; char *fgets_res; while ( (opt = getopt(argc, argv, options)) != -1 ) { switch (opt) { case 'i': show_ip = 1; break; } } if (argc > optind) { strcpy(group_name.value, argv[optind]); group_name.length = strlen(argv[optind]); } else { strcpy(group_name.value, "GROUP"); group_name.length = 6; } result = cpg_initialize (&handle, &callbacks); if (result != CS_OK) { printf ("Could not initialize Cluster Process Group API instance error %d\n", result); exit (1); } result = cpg_local_get (handle, &nodeid); if (result != CS_OK) { printf ("Could not get local node id\n"); exit (1); } printf ("Local node id is %x\n", nodeid); result = cpg_join(handle, &group_name); if (result != CS_OK) { printf ("Could not join process group, error %d\n", result); exit (1); } FD_ZERO (&read_fds); cpg_fd_get(handle, &select_fd); printf ("Type EXIT to finish\n"); do { FD_SET (select_fd, &read_fds); FD_SET (STDIN_FILENO, &read_fds); result = select (select_fd + 1, &read_fds, 0, 0, 0); if (result == -1) { perror ("select\n"); } if (FD_ISSET (STDIN_FILENO, &read_fds)) { char inbuf[132]; struct iovec iov; fgets_res = fgets(inbuf, sizeof(inbuf), stdin); if (fgets_res == NULL) { cpg_leave(handle, &group_name); } if (strncmp(inbuf, "EXIT", 4) == 0) { cpg_leave(handle, &group_name); } else { iov.iov_base = inbuf; iov.iov_len = strlen(inbuf)+1; cpg_mcast_joined(handle, CPG_TYPE_AGREED, &iov, 1); } } if (FD_ISSET (select_fd, &read_fds)) { if (cpg_dispatch (handle, CS_DISPATCH_ALL) != CS_OK) exit(1); } } while (result && !quit); result = cpg_finalize (handle); printf ("Finalize result is %d (should be 1)\n", result); return (0); }
int main (int argc, char *argv[]) { cpg_handle_t handle; fd_set read_fds; int select_fd; int result; int retries; const char *options = "i"; int opt; unsigned int nodeid; char *fgets_res; struct cpg_address member_list[64]; int member_list_entries; int i; int recnt; int doexit; const char *exitStr = "EXIT"; doexit = 0; #ifdef QBLOG qb_log_init("testcpg", LOG_USER, LOG_ERR); qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE); qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*", LOG_TRACE); qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE); qb_log_format_set(QB_LOG_STDERR, "[%p] %f %b"); #endif while ( (opt = getopt(argc, argv, options)) != -1 ) { switch (opt) { case 'i': show_ip = 1; break; } } if (argc > optind) { strcpy(group_name.value, argv[optind]); group_name.length = strlen(argv[optind]); } else { strcpy(group_name.value, "GROUP"); group_name.length = 6; } recnt = 0; printf ("Type %s to finish\n", exitStr); restart = 1; do { if(restart) { restart = 0; retries = 0; cs_repeat_init(retries, 30, result = cpg_model_initialize (&handle, CPG_MODEL_V1, (cpg_model_data_t *)&model_data, NULL)); if (result != CS_OK) { printf ("Could not initialize Cluster Process Group API instance error %d\n", result); retrybackoff(recnt); } retries = 0; cs_repeat(retries, 30, result = cpg_local_get(handle, &nodeid)); if (result != CS_OK) { printf ("Could not get local node id\n"); retrybackoff(recnt); } printf ("Local node id is %x\n", nodeid); nodeidStart = nodeid; retries = 0; cs_repeat(retries, 30, result = cpg_join(handle, &group_name)); if (result != CS_OK) { printf ("Could not join process group, error %d\n", result); retrybackoff(recnt); } retries = 0; cs_repeat(retries, 30, result = cpg_membership_get (handle, &group_name, (struct cpg_address *)&member_list, &member_list_entries)); if (result != CS_OK) { printf ("Could not get current membership list %d\n", result); retrybackoff(recnt); } recnt = 0; printf ("membership list\n"); for (i = 0; i < member_list_entries; i++) { printf ("node id %d pid %d\n", member_list[i].nodeid, member_list[i].pid); } FD_ZERO (&read_fds); cpg_fd_get(handle, &select_fd); } FD_SET (select_fd, &read_fds); FD_SET (STDIN_FILENO, &read_fds); result = select (select_fd + 1, &read_fds, 0, 0, 0); if (result == -1) { perror ("select\n"); } if (FD_ISSET (STDIN_FILENO, &read_fds)) { char inbuf[132]; struct iovec iov; fgets_res = fgets(inbuf, (int)sizeof(inbuf), stdin); if (fgets_res == NULL) { doexit = 1; cpg_leave(handle, &group_name); } if (strncmp(inbuf, exitStr, strlen(exitStr)) == 0) { doexit = 1; cpg_leave(handle, &group_name); } else { iov.iov_base = inbuf; iov.iov_len = strlen(inbuf)+1; cpg_mcast_joined(handle, CPG_TYPE_AGREED, &iov, 1); } } if (FD_ISSET (select_fd, &read_fds)) { if (cpg_dispatch (handle, CS_DISPATCH_ALL) != CS_OK) { if(doexit) { exit(1); } restart = 1; } } if(restart) { if(!doexit) { result = cpg_finalize (handle); printf ("Finalize+restart result is %d (should be 1)\n", result); continue; } } } while (result && !quit && !doexit); result = cpg_finalize (handle); printf ("Finalize result is %d (should be 1)\n", result); return (0); }
static void do_command (int sock, char* func, char*args[], int num_args) { int result; char response[100]; struct cpg_name group_name; if (parse_debug) syslog (LOG_DEBUG,"RPC:%s() called.", func); if (strcmp ("cpg_mcast_joined",func) == 0) { struct iovec iov[5]; int a; for (a = 0; a < num_args; a++) { iov[a].iov_base = args[a]; iov[a].iov_len = strlen(args[a])+1; } cpg_mcast_joined (cpg_handle, CPG_TYPE_AGREED, iov, num_args); } else if (strcmp ("cpg_join",func) == 0) { strcpy (group_name.value, args[0]); group_name.length = strlen(args[0]); result = cpg_join (cpg_handle, &group_name); if (result != CS_OK) { syslog (LOG_ERR, "Could not join process group, error %d\n", result); exit (1); } syslog (LOG_INFO, "called cpg_join()!"); } else if (strcmp ("cpg_leave",func) == 0) { strcpy (group_name.value, args[0]); group_name.length = strlen(args[0]); result = cpg_leave (cpg_handle, &group_name); if (result != CS_OK) { syslog (LOG_ERR, "Could not leave process group, error %d\n", result); exit (1); } syslog (LOG_INFO, "called cpg_leave()!"); } else if (strcmp ("cpg_initialize",func) == 0) { int retry_count = 0; result = cpg_initialize (&cpg_handle, &callbacks); while (result != CS_OK) { syslog (LOG_ERR, "cpg_initialize error %d (attempt %d)\n", result, retry_count); if (retry_count >= 3) { exit (1); } sleep(1); retry_count++; result = cpg_initialize (&cpg_handle, &callbacks); } cpg_fd_get (cpg_handle, &cpg_fd); poll_dispatch_add (ta_poll_handle_get(), cpg_fd, POLLIN|POLLNVAL, NULL, cpg_dispatch_wrapper_fn); } else if (strcmp ("cpg_local_get", func) == 0) { unsigned int local_nodeid; cpg_local_get (cpg_handle, &local_nodeid); snprintf (response, 100, "%u",local_nodeid); send (sock, response, strlen (response), 0); } else if (strcmp ("cpg_finalize", func) == 0) { cpg_finalize (cpg_handle); poll_dispatch_delete (ta_poll_handle_get(), cpg_fd); cpg_fd = -1; } else if (strcmp ("record_config_events", func) == 0) { record_config_events (sock); } else if (strcmp ("record_messages", func) == 0) { record_messages (); } else if (strcmp ("read_config_event", func) == 0) { read_config_event (sock); } else if (strcmp ("read_messages", func) == 0) { read_messages (sock, args[0]); } else if (strcmp ("msg_blaster_zcb", func) == 0) { msg_blaster_zcb (sock, args[0]); } else if (strcmp ("pcmk_test", func) == 0) { pcmk_test = 1; } else if (strcmp ("msg_blaster", func) == 0) { msg_blaster (sock, args[0]); } else if (strcmp ("context_test", func) == 0) { context_test (sock); } else if (strcmp ("are_you_ok_dude", func) == 0) { snprintf (response, 100, "%s", OK_STR); send (sock, response, strlen (response), 0); } else if (strcmp ("cfg_shutdown", func) == 0) { corosync_cfg_try_shutdown (cfg_handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST); } else if (strcmp ("cfg_initialize",func) == 0) { int retry_count = 0; syslog (LOG_INFO,"%s %s() called!", __func__, func); result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks); while (result != CS_OK) { syslog (LOG_ERR, "cfg_initialize error %d (attempt %d)\n", result, retry_count); if (retry_count >= 3) { exit (1); } sleep(1); retry_count++; result = corosync_cfg_initialize (&cfg_handle, &cfg_callbacks); } corosync_cfg_fd_get (cfg_handle, &cfg_fd); corosync_cfg_state_track (cfg_handle, 0, ¬ification_buffer); poll_dispatch_add (ta_poll_handle_get(), cfg_fd, POLLIN|POLLNVAL, NULL, cfg_dispatch_wrapper_fn); } else { syslog (LOG_ERR,"%s RPC:%s not supported!", __func__, func); } }