qnetd-algo-lms.c 14 KB

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