qnetd-algo-lms.c 13 KB

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