qnetd-algo-lms.c 16 KB

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