qnetd-algo-lms.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright (c) 2015-2016 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 Red Hat, 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 CONTRIBUTORS "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. * This is a 'last man standing' algorithm for 2+ node clusters
  36. *
  37. * If the node is the only one left in the cluster that can see the
  38. * qdevice server then we return a vote.
  39. *
  40. * If more than one node can see the qdevice server but some nodes can't
  41. * see each other then we divide the cluster up into 'partitions' based on
  42. * their ring_id and return a vote to nodes in the partition that contains
  43. * a nominated nodeid. (lowest, highest, etc)
  44. *
  45. */
  46. #include <sys/types.h>
  47. #include <sys/queue.h>
  48. #include <string.h>
  49. #include <limits.h>
  50. #include "qnetd-algo-lms.h"
  51. #include "qnetd-log.h"
  52. #include "qnetd-cluster-list.h"
  53. #include "qnetd-algo-utils.h"
  54. #include "qnetd-client-algo-timer.h"
  55. #include "utils.h"
  56. struct qnetd_algo_lms_info {
  57. int num_config_nodes;
  58. enum tlv_vote last_result;
  59. partitions_list_t partition_list;
  60. };
  61. static enum tlv_reply_error_code do_lms_algorithm(struct qnetd_client *client, const struct tlv_ring_id *cur_ring_id, enum tlv_vote *result_vote)
  62. {
  63. struct qnetd_client *other_client;
  64. struct qnetd_algo_lms_info *info = client->algorithm_data;
  65. struct qnetd_algo_partition *cur_partition;
  66. struct qnetd_algo_partition *largest_partition;
  67. const struct tlv_ring_id *ring_id = cur_ring_id;
  68. int num_partitions;
  69. int joint_leader;
  70. /* We are running the algorithm, don't do it again unless we say so */
  71. qnetd_client_algo_timer_abort(client);
  72. if (qnetd_algo_all_ring_ids_match(client, ring_id) == -1) {
  73. qnetd_log(LOG_DEBUG, "algo-lms: nodeid %d: ring ID (" UTILS_PRI_RING_ID ") not unique in this membership, waiting",
  74. client->node_id, ring_id->node_id, ring_id->seq);
  75. qnetd_client_algo_timer_schedule(client);
  76. *result_vote = info->last_result = TLV_VOTE_WAIT_FOR_REPLY;
  77. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  78. }
  79. /* Create and count the number of separate partitions */
  80. if ( (num_partitions = qnetd_algo_create_partitions(client, &info->partition_list, ring_id)) == -1) {
  81. qnetd_log(LOG_DEBUG, "algo-lms: Error creating partition list");
  82. return (TLV_REPLY_ERROR_CODE_INTERNAL_ERROR);
  83. }
  84. /* This can happen if we are first on the block */
  85. if (num_partitions == 0) {
  86. qnetd_log(LOG_DEBUG, "algo-lms: No partitions found");
  87. qnetd_client_algo_timer_schedule(client);
  88. *result_vote = info->last_result = TLV_VOTE_WAIT_FOR_REPLY;
  89. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  90. }
  91. qnetd_algo_dump_partitions(&info->partition_list);
  92. /* Only 1 partition - let votequorum sort it out */
  93. if (num_partitions == 1) {
  94. qnetd_log(LOG_DEBUG, "algo-lms: Only 1 partition. This is votequorum's problem, not ours");
  95. qnetd_algo_free_partitions(&info->partition_list);
  96. *result_vote = info->last_result = TLV_VOTE_ACK;
  97. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  98. }
  99. /* If we're a newcomer and there is another active partition, then we must NACK
  100. * to avoid quorum moving to us from already active nodes.
  101. */
  102. if (info->last_result == 0) {
  103. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  104. struct qnetd_algo_lms_info *other_info = other_client->algorithm_data;
  105. if (!tlv_ring_id_eq(ring_id, &other_client->last_ring_id) &&
  106. other_info->last_result == TLV_VOTE_ACK) {
  107. qnetd_algo_free_partitions(&info->partition_list);
  108. /* Don't save NACK, we need to know subsequently if we haven't been voting */
  109. *result_vote = TLV_VOTE_NACK;
  110. qnetd_log(LOG_DEBUG, "algo-lms: we are a new partition and another active partition exists. NACK");
  111. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  112. }
  113. }
  114. }
  115. /* Find the largest partition */
  116. largest_partition = NULL;
  117. TAILQ_FOREACH(cur_partition, &info->partition_list, entries) {
  118. if (!largest_partition ||
  119. largest_partition->num_nodes < cur_partition->num_nodes) {
  120. largest_partition = cur_partition;
  121. }
  122. }
  123. qnetd_log(LOG_DEBUG, "algo-lms: largest partition is (" UTILS_PRI_RING_ID ") with %d nodes",
  124. largest_partition->ring_id.node_id, largest_partition->ring_id.seq, largest_partition->num_nodes);
  125. /* Now check if it's really the largest, and not just the joint-largest */
  126. joint_leader = 0;
  127. TAILQ_FOREACH(cur_partition, &info->partition_list, entries) {
  128. if (largest_partition != cur_partition &&
  129. largest_partition->num_nodes == cur_partition->num_nodes) {
  130. joint_leader = 1;
  131. }
  132. }
  133. if (!joint_leader) {
  134. /* Largest partition is unique, allow us to run if we're in that partition. */
  135. if (tlv_ring_id_eq(&largest_partition->ring_id, ring_id)) {
  136. qnetd_log(LOG_DEBUG, "algo-lms: We are in the largest partition. ACK");
  137. *result_vote = info->last_result = TLV_VOTE_ACK;
  138. }
  139. else {
  140. qnetd_log(LOG_DEBUG, "algo-lms: We are NOT in the largest partition. NACK");
  141. *result_vote = info->last_result = TLV_VOTE_NACK;
  142. }
  143. }
  144. else {
  145. int tb_node_id;
  146. struct tlv_ring_id tb_node_ring_id = {0LL, 0};
  147. /* Look for the tie-breaker node */
  148. if (client->tie_breaker.mode == TLV_TIE_BREAKER_MODE_LOWEST) {
  149. tb_node_id = INT_MAX;
  150. }
  151. else if (client->tie_breaker.mode == TLV_TIE_BREAKER_MODE_HIGHEST) {
  152. tb_node_id = 0;
  153. }
  154. else if (client->tie_breaker.mode == TLV_TIE_BREAKER_MODE_NODE_ID) {
  155. tb_node_id = client->tie_breaker.node_id;
  156. }
  157. else {
  158. qnetd_log(LOG_DEBUG, "algo-lms: denied vote because tie-breaker option is invalid: %d",
  159. client->tie_breaker.mode);
  160. tb_node_id = -1;
  161. }
  162. /* Find the tie_breaker node */
  163. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  164. switch (client->tie_breaker.mode) {
  165. case TLV_TIE_BREAKER_MODE_LOWEST:
  166. if (other_client->node_id < tb_node_id) {
  167. tb_node_id = other_client->node_id;
  168. memcpy(&tb_node_ring_id, &other_client->last_ring_id, sizeof(struct tlv_ring_id));
  169. qnetd_log(LOG_DEBUG, "algo-lms: Looking for low node ID. found %d (" UTILS_PRI_RING_ID ")",
  170. tb_node_id, tb_node_ring_id.node_id, tb_node_ring_id.seq);
  171. }
  172. break;
  173. case TLV_TIE_BREAKER_MODE_HIGHEST:
  174. if (other_client->node_id > tb_node_id) {
  175. tb_node_id = other_client->node_id;
  176. memcpy(&tb_node_ring_id, &other_client->last_ring_id, sizeof(struct tlv_ring_id));
  177. qnetd_log(LOG_DEBUG, "algo-lms: Looking for high node ID. found %d (" UTILS_PRI_RING_ID ")",
  178. tb_node_id, tb_node_ring_id.node_id, tb_node_ring_id.seq);
  179. }
  180. break;
  181. case TLV_TIE_BREAKER_MODE_NODE_ID:
  182. if (client->tie_breaker.node_id == client->node_id) {
  183. memcpy(&tb_node_ring_id, &other_client->last_ring_id, sizeof(struct tlv_ring_id));
  184. qnetd_log(LOG_DEBUG, "algo-lms: Looking for nominated node ID. found %d (" UTILS_PRI_RING_ID ")",
  185. tb_node_id, tb_node_ring_id.node_id, tb_node_ring_id.seq);
  186. }
  187. break;
  188. default:
  189. qnetd_log(LOG_DEBUG, "algo-lms: denied vote because tie-breaker option is invalid: %d",
  190. client->tie_breaker.mode);
  191. memset(&tb_node_ring_id, 0, sizeof(struct tlv_ring_id));
  192. }
  193. }
  194. if (client->node_id == tb_node_id || tlv_ring_id_eq(&tb_node_ring_id, ring_id)) {
  195. qnetd_log(LOG_DEBUG, "algo-lms: We are in the same partition (" UTILS_PRI_RING_ID ") as tie-breaker node id %d. ACK",
  196. tb_node_ring_id.node_id, tb_node_ring_id.seq, tb_node_id);
  197. *result_vote = info->last_result = TLV_VOTE_ACK;
  198. }
  199. else {
  200. qnetd_log(LOG_DEBUG, "algo-lms: We are NOT in the same partition (" UTILS_PRI_RING_ID ") as tie-breaker node id %d. NACK",
  201. tb_node_ring_id.node_id, tb_node_ring_id.seq, tb_node_id);
  202. *result_vote = info->last_result = TLV_VOTE_NACK;
  203. }
  204. }
  205. qnetd_algo_free_partitions(&info->partition_list);
  206. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  207. }
  208. enum tlv_reply_error_code
  209. qnetd_algo_lms_client_init(struct qnetd_client *client)
  210. {
  211. struct qnetd_algo_lms_info *info;
  212. info = malloc(sizeof(struct qnetd_algo_lms_info));
  213. if (!info) {
  214. return (TLV_REPLY_ERROR_CODE_INTERNAL_ERROR);
  215. }
  216. memset(info, 0, sizeof(*info));
  217. client->algorithm_data = info;
  218. info->last_result = 0; /* status unknown, or NEW */
  219. TAILQ_INIT(&info->partition_list);
  220. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  221. }
  222. /*
  223. * We got the config node list. Simply count the number of available nodes
  224. * and wait for the quorum list.
  225. */
  226. enum tlv_reply_error_code
  227. qnetd_algo_lms_config_node_list_received(struct qnetd_client *client,
  228. uint32_t msg_seq_num, int config_version_set, uint64_t config_version,
  229. const struct node_list *nodes, int initial, enum tlv_vote *result_vote)
  230. {
  231. struct node_list_entry *node_info;
  232. struct qnetd_algo_lms_info *info = client->algorithm_data;
  233. int node_count = 0;
  234. TAILQ_FOREACH(node_info, nodes, entries) {
  235. node_count++;
  236. }
  237. info->num_config_nodes = node_count;
  238. qnetd_log(LOG_DEBUG, "algo-lms: cluster %s config_list has %d nodes", client->cluster_name, node_count);
  239. *result_vote = TLV_VOTE_NO_CHANGE;
  240. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  241. }
  242. /*
  243. * membership node list. This is where we get to work.
  244. */
  245. enum tlv_reply_error_code
  246. qnetd_algo_lms_membership_node_list_received(struct qnetd_client *client,
  247. uint32_t msg_seq_num, const struct tlv_ring_id *ring_id,
  248. const struct node_list *nodes, enum tlv_vote *result_vote)
  249. {
  250. qnetd_log(LOG_DEBUG, " ");
  251. qnetd_log(LOG_DEBUG, "algo-lms: membership list from node %d partition (" UTILS_PRI_RING_ID ")", client->node_id, ring_id->node_id, ring_id->seq);
  252. return do_lms_algorithm(client, ring_id, result_vote);
  253. }
  254. /*
  255. * The quorum node list is received after corosync has decided which nodes are in the cluster.
  256. * We run our algorithm again to be sure that things still match. By this time we will (or should)
  257. * all know the current ring_id (not guaranteed when the membership list is received). So this
  258. * might be the most reliable return.
  259. */
  260. enum tlv_reply_error_code
  261. qnetd_algo_lms_quorum_node_list_received(struct qnetd_client *client,
  262. uint32_t msg_seq_num, enum tlv_quorate quorate, const struct node_list *nodes, enum tlv_vote *result_vote)
  263. {
  264. qnetd_log(LOG_DEBUG, " ");
  265. qnetd_log(LOG_DEBUG, "algo-lms: quorum node list from node %d partition (" UTILS_PRI_RING_ID ")", client->node_id, client->last_ring_id.node_id, client->last_ring_id.seq);
  266. return do_lms_algorithm(client, &client->last_ring_id, result_vote);
  267. }
  268. /*
  269. * Called after client disconnect. Client structure is still existing (and it's part
  270. * of a client->cluster), but it is destroyed (and removed from cluster) right after
  271. * this callback finishes. Callback is used mainly for destroing client->algorithm_data.
  272. */
  273. void
  274. qnetd_algo_lms_client_disconnect(struct qnetd_client *client, int server_going_down)
  275. {
  276. qnetd_log(LOG_DEBUG, "algo-lms: Client %p (cluster %s, node_id "UTILS_PRI_NODE_ID") "
  277. "disconnect", client, client->cluster_name, client->node_id);
  278. qnetd_log(LOG_INFO, "algo-lms: server going down %u", server_going_down);
  279. free(client->algorithm_data);
  280. }
  281. /*
  282. * Called after client sent ask for vote message. This is usually happening after server
  283. * replied TLV_VOTE_WAIT_FOR_REPLY.
  284. */
  285. enum tlv_reply_error_code
  286. qnetd_algo_lms_ask_for_vote_received(struct qnetd_client *client, uint32_t msg_seq_num,
  287. enum tlv_vote *result_vote)
  288. {
  289. qnetd_log(LOG_DEBUG, " ");
  290. qnetd_log(LOG_DEBUG, "algo-lms: Client %p (cluster %s, node_id "UTILS_PRI_NODE_ID") "
  291. "asked for a vote", client, client->cluster_name, client->node_id);
  292. return do_lms_algorithm(client, &client->last_ring_id, result_vote);
  293. }
  294. enum tlv_reply_error_code
  295. qnetd_algo_lms_vote_info_reply_received(struct qnetd_client *client, uint32_t msg_seq_num)
  296. {
  297. qnetd_log(LOG_DEBUG, "algo-lms: Client %p (cluster %s, node_id "UTILS_PRI_NODE_ID") "
  298. "replied back to vote info message", client, client->cluster_name, client->node_id);
  299. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  300. }
  301. enum tlv_reply_error_code
  302. qnetd_algo_lms_timer_callback(struct qnetd_client *client, int *reschedule_timer,
  303. int *send_vote, enum tlv_vote *result_vote)
  304. {
  305. enum tlv_reply_error_code ret;
  306. qnetd_log(LOG_DEBUG, "algo-lms: Client %p (cluster %s, node_id "UTILS_PRI_NODE_ID") "
  307. "Timer callback", client, client->cluster_name, client->node_id);
  308. ret = do_lms_algorithm(client, &client->last_ring_id, result_vote);
  309. if (ret == TLV_REPLY_ERROR_CODE_NO_ERROR &&
  310. (*result_vote == TLV_VOTE_ACK || *result_vote == TLV_VOTE_NACK)) {
  311. *send_vote = 1;
  312. }
  313. if (ret == TLV_REPLY_ERROR_CODE_NO_ERROR &&
  314. *result_vote == TLV_VOTE_WAIT_FOR_REPLY) {
  315. /*
  316. * Reschedule was called in the do_lms_algorithm but algo_timer is
  317. * not stack based so there can only be one. So if do_lms aborted
  318. * the active timer, and scheduled it again the timer would be aborted
  319. * if reschedule_timer was not set.
  320. */
  321. *reschedule_timer = 1;
  322. }
  323. return ret;
  324. }
  325. static struct qnetd_algorithm qnetd_algo_lms = {
  326. .init = qnetd_algo_lms_client_init,
  327. .config_node_list_received = qnetd_algo_lms_config_node_list_received,
  328. .membership_node_list_received = qnetd_algo_lms_membership_node_list_received,
  329. .quorum_node_list_received = qnetd_algo_lms_quorum_node_list_received,
  330. .client_disconnect = qnetd_algo_lms_client_disconnect,
  331. .ask_for_vote_received = qnetd_algo_lms_ask_for_vote_received,
  332. .vote_info_reply_received = qnetd_algo_lms_vote_info_reply_received,
  333. .timer_callback = qnetd_algo_lms_timer_callback,
  334. };
  335. enum tlv_reply_error_code qnetd_algo_lms_register()
  336. {
  337. return qnetd_algorithm_register(TLV_DECISION_ALGORITHM_TYPE_LMS, &qnetd_algo_lms);
  338. }