qnetd-algo-lms.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Copyright (c) 2015 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@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 simple '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 the nodes can't
  41. * see each other then we return a vote to the lowest nodeID of the two
  42. *
  43. * If there are more than two nodes, then we don't return a vote.
  44. * this is not our job (any other ideas??)
  45. */
  46. #include <sys/types.h>
  47. #include <string.h>
  48. #include <limits.h>
  49. #include "qnetd-algo-lms.h"
  50. #include "qnetd-log.h"
  51. #include "qnetd-cluster-list.h"
  52. struct qnetd_algo_lms_partition {
  53. struct tlv_ring_id ring_id;
  54. int num_nodes;
  55. TAILQ_ENTRY(qnetd_algo_lms_partition) entries;
  56. };
  57. struct qnetd_algo_lms_info {
  58. int num_config_nodes;
  59. enum tlv_vote last_result;
  60. struct tlv_ring_id ring_id;
  61. TAILQ_HEAD( ,qnetd_algo_lms_partition) partition_list;
  62. };
  63. static int rings_eq(const struct tlv_ring_id *ring_id1, const struct tlv_ring_id *ring_id2)
  64. {
  65. if (ring_id1->node_id == ring_id2->node_id &&
  66. ring_id1->seq == ring_id2->seq) {
  67. return 1;
  68. }
  69. else {
  70. return 0;
  71. }
  72. }
  73. static struct qnetd_algo_lms_partition *find_partition(struct qnetd_algo_lms_info *info, const struct tlv_ring_id *ring_id)
  74. {
  75. struct qnetd_algo_lms_partition *cur_partition;
  76. TAILQ_FOREACH(cur_partition, &info->partition_list, entries) {
  77. if (rings_eq(&cur_partition->ring_id, ring_id)) {
  78. return cur_partition;
  79. }
  80. }
  81. return NULL;
  82. }
  83. static int create_partitions(struct qnetd_client *client,
  84. const struct tlv_ring_id *ring_id)
  85. {
  86. struct qnetd_client *other_client;
  87. struct qnetd_algo_lms_info *info = client->algorithm_data;
  88. int num_partitions = 0;
  89. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  90. struct qnetd_algo_lms_info *other_info = other_client->algorithm_data;
  91. struct qnetd_algo_lms_partition *partition;
  92. if (other_info->ring_id.seq == 0){
  93. continue; /* not initialised yet */
  94. }
  95. partition = find_partition(info, &other_info->ring_id);
  96. if (!partition) {
  97. partition = malloc(sizeof(struct qnetd_algo_lms_partition));
  98. if (!partition) {
  99. return -1;
  100. }
  101. partition->num_nodes = 0;
  102. memcpy(&partition->ring_id, &other_info->ring_id, sizeof(*ring_id));
  103. num_partitions++;
  104. TAILQ_INSERT_TAIL(&info->partition_list, partition, entries);
  105. }
  106. partition->num_nodes++;
  107. }
  108. return num_partitions;
  109. }
  110. static void free_partitions(struct qnetd_algo_lms_info *info)
  111. {
  112. struct qnetd_algo_lms_partition *cur_partition;
  113. TAILQ_FOREACH(cur_partition, &info->partition_list, entries) {
  114. TAILQ_REMOVE(&info->partition_list, cur_partition, entries);
  115. free(cur_partition);
  116. }
  117. }
  118. static void dump_partitions(struct qnetd_algo_lms_info *info)
  119. {
  120. struct qnetd_algo_lms_partition *partition;
  121. TAILQ_FOREACH(partition, &info->partition_list, entries) {
  122. qnetd_log(LOG_DEBUG, "algo-lms: partition %d/%ld (%p) has %d nodes", partition->ring_id.node_id, partition->ring_id.seq, partition, partition->num_nodes);
  123. }
  124. }
  125. /*
  126. * Returns -1 if any node that is supposedly in the same cluster partition
  127. * as us has a different ring_id.
  128. * If this happens it simply means that qnetd does not yet have the full current view
  129. * of the cluster and should wait until all of the ring_ids in this membership list match up
  130. */
  131. static int ring_ids_match(struct qnetd_client *client, const struct tlv_ring_id *ring_id)
  132. {
  133. struct node_list_entry *node_info;
  134. struct qnetd_client *other_client;
  135. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  136. struct qnetd_algo_lms_info *other_info = other_client->algorithm_data;
  137. int in_our_partition = 0;
  138. if (other_client == client) {
  139. continue; /* We've seen our membership list */
  140. }
  141. /* Look down our node list and see if this client is known to us */
  142. TAILQ_FOREACH(node_info, &client->last_membership_node_list, entries) {
  143. if (node_info->node_id == other_client->node_id) {
  144. in_our_partition = 1;
  145. }
  146. }
  147. /*
  148. * If the other nodes on our side of a partition have a different ring ID then
  149. * we need to wait until they have all caught up before making a decision
  150. */
  151. if (in_our_partition && !rings_eq(ring_id, &other_info->ring_id)) {
  152. qnetd_log(LOG_DEBUG, "algo-lms: nodeid %d in our partition has different ring_id (%d/%ld) to us (%d/%ld)", other_client->node_id, other_info->ring_id.node_id, other_info->ring_id.seq, ring_id->node_id, ring_id->seq);
  153. return -1; /* ring IDs don't match */
  154. }
  155. }
  156. return 0;
  157. }
  158. static enum tlv_reply_error_code do_lms_algorithm(struct qnetd_client *client, enum tlv_vote *result_vote)
  159. {
  160. struct qnetd_client *other_client;
  161. struct qnetd_algo_lms_info *info = client->algorithm_data;
  162. struct qnetd_algo_lms_partition *cur_partition;
  163. struct qnetd_algo_lms_partition *largest_partition;
  164. int num_partitions;
  165. int joint_leader;
  166. if (ring_ids_match(client, &info->ring_id) == -1) {
  167. qnetd_log(LOG_DEBUG, "algo-lms: nodeid %d: ring ID %d/%ld not unique in this membership, waiting", client->node_id, info->ring_id.node_id, info->ring_id.seq);
  168. *result_vote = info->last_result = TLV_VOTE_ASK_LATER;
  169. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  170. }
  171. /* Create and count the number of separate partitions */
  172. if ( (num_partitions = create_partitions(client, &info->ring_id)) == -1) {
  173. qnetd_log(LOG_DEBUG, "algo-lms: Error creating partition list");
  174. return (TLV_REPLY_ERROR_CODE_INTERNAL_ERROR);
  175. }
  176. dump_partitions(info);
  177. /* Only 1 partition - let votequorum sort it out */
  178. if (num_partitions == 1) {
  179. qnetd_log(LOG_DEBUG, "algo-lms: Only 1 partition. This is votequorum's problem, not ours");
  180. free_partitions(info);
  181. *result_vote = info->last_result = TLV_VOTE_ACK;
  182. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  183. }
  184. /* If we're a newcomer and there is another active partition, then we must NACK
  185. * to avoid quorum moving to us from already active nodes.
  186. */
  187. if (info->last_result == 0) {
  188. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  189. struct qnetd_algo_lms_info *other_info = other_client->algorithm_data;
  190. if (!rings_eq(&info->ring_id, &other_info->ring_id) &&
  191. other_info->last_result == TLV_VOTE_ACK) {
  192. free_partitions(info);
  193. /* Don't save NACK, we need to know subsequently if we haven't been voting */
  194. *result_vote = TLV_VOTE_NACK;
  195. qnetd_log(LOG_DEBUG, "algo-lms: we are a new partition and another active partition exists. NACK");
  196. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  197. }
  198. }
  199. }
  200. /* Find the largest partition */
  201. largest_partition = NULL;
  202. TAILQ_FOREACH(cur_partition, &info->partition_list, entries) {
  203. if (!largest_partition ||
  204. largest_partition->num_nodes < cur_partition->num_nodes) {
  205. largest_partition = cur_partition;
  206. }
  207. }
  208. qnetd_log(LOG_DEBUG, "algo-lms: largest partition is %d/%ld with %d nodes", largest_partition->ring_id.node_id, largest_partition->ring_id.seq, largest_partition->num_nodes);
  209. /* Now check if it's really the largest, and not just the joint-largest */
  210. joint_leader = 0;
  211. TAILQ_FOREACH(cur_partition, &info->partition_list, entries) {
  212. if (largest_partition != cur_partition &&
  213. largest_partition->num_nodes == cur_partition->num_nodes) {
  214. joint_leader = 1;
  215. }
  216. }
  217. if (!joint_leader) {
  218. /* Largest partition is unique, allow us to run if we're in that partition. */
  219. if (rings_eq(&largest_partition->ring_id, &info->ring_id)) {
  220. qnetd_log(LOG_DEBUG, "algo-lms: We are in the largest partition. ACK\n");
  221. *result_vote = info->last_result = TLV_VOTE_ACK;
  222. }
  223. else {
  224. qnetd_log(LOG_DEBUG, "algo-lms: We are NOT in the largest partition. NACK\n");
  225. *result_vote = info->last_result = TLV_VOTE_NACK;
  226. }
  227. }
  228. else {
  229. int tb_node_id;
  230. struct tlv_ring_id tb_node_ring_id = {0LL, 0};
  231. /* Look for the tie-breaker node */
  232. if (client->tie_breaker.mode == TLV_TIE_BREAKER_MODE_LOWEST) {
  233. tb_node_id = INT_MAX;
  234. }
  235. else if (client->tie_breaker.mode == TLV_TIE_BREAKER_MODE_HIGHEST) {
  236. tb_node_id = 0;
  237. }
  238. else if (client->tie_breaker.mode == TLV_TIE_BREAKER_MODE_NODE_ID) {
  239. tb_node_id = client->tie_breaker.node_id;
  240. }
  241. else {
  242. qnetd_log(LOG_DEBUG, "algo-lms: denied vote because tie-breaker option is invalid: %d", client->tie_breaker.mode);
  243. tb_node_id = -1;
  244. }
  245. /* Find the tie_breaker node */
  246. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  247. struct qnetd_algo_lms_info *other_info = other_client->algorithm_data;
  248. switch (client->tie_breaker.mode) {
  249. case TLV_TIE_BREAKER_MODE_LOWEST:
  250. if (other_client->node_id < tb_node_id) {
  251. tb_node_id = other_client->node_id;
  252. memcpy(&tb_node_ring_id, &other_info->ring_id, sizeof(struct tlv_ring_id));
  253. qnetd_log(LOG_DEBUG, "algo-lms: Looking for low node ID. found %d (%d/%ld)", tb_node_id, tb_node_ring_id.node_id, tb_node_ring_id.seq);
  254. }
  255. break;
  256. case TLV_TIE_BREAKER_MODE_HIGHEST:
  257. if (other_client->node_id > tb_node_id) {
  258. tb_node_id = other_client->node_id;
  259. memcpy(&tb_node_ring_id, &other_info->ring_id, sizeof(struct tlv_ring_id));
  260. qnetd_log(LOG_DEBUG, "algo-lms: Looking for high node ID. found %d (%d/%ld)", tb_node_id, tb_node_ring_id.node_id, tb_node_ring_id.seq);
  261. }
  262. break;
  263. case TLV_TIE_BREAKER_MODE_NODE_ID:
  264. if (client->tie_breaker.node_id == client->node_id) {
  265. memcpy(&tb_node_ring_id, &other_info->ring_id, sizeof(struct tlv_ring_id));
  266. qnetd_log(LOG_DEBUG, "algo-lms: Looking for nominated node ID. found %d (%d/%ld)", tb_node_id, tb_node_ring_id.node_id, tb_node_ring_id.seq);
  267. }
  268. break;
  269. default:
  270. qnetd_log(LOG_DEBUG, "algo-lms: denied vote because tie-breaker option is invalid: %d", client->tie_breaker.mode);
  271. memset(&tb_node_ring_id, 0, sizeof(struct tlv_ring_id));
  272. }
  273. }
  274. if (client->node_id == tb_node_id || rings_eq(&tb_node_ring_id, &info->ring_id)) {
  275. qnetd_log(LOG_DEBUG, "algo-lms: We are in the same partition (%d/%ld) as tie-breaker node id %d. ACK", tb_node_ring_id.node_id, tb_node_ring_id.seq, tb_node_id);
  276. *result_vote = info->last_result = TLV_VOTE_ACK;
  277. }
  278. else {
  279. qnetd_log(LOG_DEBUG, "algo-lms: We are NOT in the same partition (%d/%ld) as tie-breaker node id %d. NACK", tb_node_ring_id.node_id, tb_node_ring_id.seq, tb_node_id);
  280. *result_vote = info->last_result = TLV_VOTE_NACK;
  281. }
  282. }
  283. free_partitions(info);
  284. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  285. }
  286. enum tlv_reply_error_code
  287. qnetd_algo_lms_client_init(struct qnetd_client *client)
  288. {
  289. struct qnetd_algo_lms_info *info;
  290. info = malloc(sizeof(struct qnetd_algo_lms_info));
  291. if (!info) {
  292. return (TLV_REPLY_ERROR_CODE_INTERNAL_ERROR);
  293. }
  294. memset(info, 0, sizeof(*info));
  295. client->algorithm_data = info;
  296. info->last_result = 0; /* status unknown, or NEW */
  297. TAILQ_INIT(&info->partition_list);
  298. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  299. }
  300. /*
  301. * Called after client sent configuration node list
  302. * All client fields are already set. Nodes is actual node list, initial is used
  303. * to distinquish between initial node list and changed node list.
  304. * msg_seq_num is 32-bit number set by client. If client sent config file version,
  305. * config_version_set is set to 1 and config_version contains valid config file version.
  306. *
  307. * Function has to return result_vote. This can be one of ack/nack, ask_later (client
  308. * should ask later for a vote) or wait_for_reply (client should wait for reply).
  309. *
  310. * Return TLV_REPLY_ERROR_CODE_NO_ERROR on success, different TLV_REPLY_ERROR_CODE_*
  311. * on failure (error is send back to client)
  312. */
  313. enum tlv_reply_error_code
  314. qnetd_algo_lms_config_node_list_received(struct qnetd_client *client,
  315. uint32_t msg_seq_num, int config_version_set, uint64_t config_version,
  316. const struct node_list *nodes, int initial, enum tlv_vote *result_vote)
  317. {
  318. struct node_list_entry *node_info;
  319. struct qnetd_algo_lms_info *info = client->algorithm_data;
  320. int node_count = 0;
  321. TAILQ_FOREACH(node_info, nodes, entries) {
  322. node_count++;
  323. }
  324. info->num_config_nodes = node_count;
  325. qnetd_log(LOG_DEBUG, "algo-lms: cluster %s config_list has %d nodes", client->cluster_name, node_count);
  326. *result_vote = TLV_VOTE_NO_CHANGE;
  327. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  328. }
  329. /*
  330. * Called after client sent membership node list.
  331. * All client fields are already set. Nodes is actual node list.
  332. * msg_seq_num is 32-bit number set by client. If client sent config file version,
  333. * config_version_set is set to 1 and config_version contains valid config file version.
  334. * ring_id and quorate are copied from client votequorum callback.
  335. *
  336. * Function has to return result_vote. This can be one of ack/nack, ask_later (client
  337. * should ask later for a vote) or wait_for_reply (client should wait for reply).
  338. *
  339. * Return TLV_REPLY_ERROR_CODE_NO_ERROR on success, different TLV_REPLY_ERROR_CODE_*
  340. * on failure (error is send back to client)
  341. */
  342. enum tlv_reply_error_code
  343. qnetd_algo_lms_membership_node_list_received(struct qnetd_client *client,
  344. uint32_t msg_seq_num, const struct tlv_ring_id *ring_id,
  345. const struct node_list *nodes, enum tlv_vote *result_vote)
  346. {
  347. struct qnetd_algo_lms_info *info = client->algorithm_data;
  348. /* Save this now */
  349. memcpy(&info->ring_id, ring_id, sizeof(*ring_id));
  350. qnetd_log(LOG_DEBUG, "\nalgo-lms: membership list from node %d partition %d/%ld", client->node_id, ring_id->node_id, ring_id->seq);
  351. return do_lms_algorithm(client, result_vote);
  352. }
  353. enum tlv_reply_error_code
  354. qnetd_algo_lms_quorum_node_list_received(struct qnetd_client *client,
  355. uint32_t msg_seq_num, enum tlv_quorate quorate, const struct node_list *nodes, enum tlv_vote *result_vote)
  356. {
  357. struct qnetd_algo_lms_info *info = client->algorithm_data;
  358. qnetd_log(LOG_DEBUG, "\nalgo-lms: quorum node list from node %d partition %d/%ld", client->node_id, info->ring_id.node_id, info->ring_id.seq);
  359. return do_lms_algorithm(client, result_vote);
  360. }
  361. /*
  362. * Called after client disconnect. Client structure is still existing (and it's part
  363. * of a client->cluster), but it is destroyed (and removed from cluster) right after
  364. * this callback finishes. Callback is used mainly for destroing client->algorithm_data.
  365. */
  366. void
  367. qnetd_algo_lms_client_disconnect(struct qnetd_client *client, int server_going_down)
  368. {
  369. qnetd_log(LOG_DEBUG, "\nalgo-lms: Client %p (cluster %s, node_id %"PRIx32") "
  370. "disconnect", client, client->cluster_name, client->node_id);
  371. qnetd_log(LOG_INFO, "algo-lms: server going down %u", server_going_down);
  372. free(client->algorithm_data);
  373. }
  374. /*
  375. * Called after client sent ask for vote message. This is usually happening after server
  376. * replied TLV_VOTE_ASK_LATER.
  377. */
  378. enum tlv_reply_error_code
  379. qnetd_algo_lms_ask_for_vote_received(struct qnetd_client *client, uint32_t msg_seq_num,
  380. enum tlv_vote *result_vote)
  381. {
  382. qnetd_log(LOG_DEBUG, "\nalgo-lms: Client %p (cluster %s, node_id %"PRIx32") "
  383. "asked for a vote", client, client->cluster_name, client->node_id);
  384. return do_lms_algorithm(client, result_vote);
  385. }
  386. enum tlv_reply_error_code
  387. qnetd_algo_lms_vote_info_reply_received(struct qnetd_client *client, uint32_t msg_seq_num)
  388. {
  389. qnetd_log(LOG_DEBUG, "\nalgo-lms: Client %p (cluster %s, node_id %"PRIx32") "
  390. "replied back to vote info message", client, client->cluster_name, client->node_id);
  391. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  392. }
  393. static struct qnetd_algorithm qnetd_algo_lms = {
  394. .init = qnetd_algo_lms_client_init,
  395. .config_node_list_received = qnetd_algo_lms_config_node_list_received,
  396. .membership_node_list_received = qnetd_algo_lms_membership_node_list_received,
  397. .quorum_node_list_received = qnetd_algo_lms_quorum_node_list_received,
  398. .client_disconnect = qnetd_algo_lms_client_disconnect,
  399. .ask_for_vote_received = qnetd_algo_lms_ask_for_vote_received,
  400. .vote_info_reply_received = qnetd_algo_lms_vote_info_reply_received,
  401. };
  402. enum tlv_reply_error_code qnetd_algo_lms_register()
  403. {
  404. return qnetd_algorithm_register(TLV_DECISION_ALGORITHM_TYPE_LMS, &qnetd_algo_lms);
  405. }