qnetd-algo-lms.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. qnetd_log(LOG_DEBUG, "algo-lms: nodeid %d in our partition", node_info->node_id);
  144. if (node_info->node_id == other_client->node_id) {
  145. in_our_partition = 1;
  146. }
  147. }
  148. /*
  149. * If the other nodes on our side of a partition have a different ring ID then
  150. * we need to wait until they have all caught up before making a decision
  151. */
  152. if (in_our_partition && !rings_eq(ring_id, &other_info->ring_id)) {
  153. 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);
  154. return -1; /* ring IDs don't match */
  155. }
  156. }
  157. return 0;
  158. }
  159. static enum tlv_reply_error_code do_lms_algorithm(struct qnetd_client *client, enum tlv_vote *result_vote)
  160. {
  161. struct qnetd_client *other_client;
  162. struct qnetd_algo_lms_info *info = client->algorithm_data;
  163. struct qnetd_algo_lms_partition *cur_partition;
  164. struct qnetd_algo_lms_partition *largest_partition;
  165. int num_partitions;
  166. int joint_leader;
  167. if (ring_ids_match(client, &info->ring_id) == -1) {
  168. 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);
  169. *result_vote = info->last_result = TLV_VOTE_ASK_LATER;
  170. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  171. }
  172. /* Create and count the number of separate partitions */
  173. if ( (num_partitions = create_partitions(client, &info->ring_id)) == -1) {
  174. qnetd_log(LOG_DEBUG, "algo-lms: Error creating partition list");
  175. return (TLV_REPLY_ERROR_CODE_INTERNAL_ERROR);
  176. }
  177. dump_partitions(info);
  178. /* Only 1 partition - let votequorum sort it out */
  179. if (num_partitions == 1) {
  180. qnetd_log(LOG_DEBUG, "algo-lms: Only 1 partition. This is votequorum's problem, not ours");
  181. free_partitions(info);
  182. *result_vote = info->last_result = TLV_VOTE_ACK;
  183. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  184. }
  185. /* If we're a newcomer and there is another active partition, then we must NACK
  186. * to avoid quorum moving to us from already active nodes.
  187. */
  188. if (info->last_result == 0) {
  189. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  190. struct qnetd_algo_lms_info *other_info = other_client->algorithm_data;
  191. if (!rings_eq(&info->ring_id, &other_info->ring_id) &&
  192. other_info->last_result == TLV_VOTE_ACK) {
  193. free_partitions(info);
  194. /* Don't save NACK, we need to know subsequently if we haven't been voting */
  195. *result_vote = TLV_VOTE_NACK;
  196. qnetd_log(LOG_DEBUG, "algo-lms: we are a new partition and another active partition exists. NACK");
  197. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  198. }
  199. }
  200. }
  201. /* Find the largest partition */
  202. largest_partition = NULL;
  203. TAILQ_FOREACH(cur_partition, &info->partition_list, entries) {
  204. if (!largest_partition ||
  205. largest_partition->num_nodes < cur_partition->num_nodes) {
  206. largest_partition = cur_partition;
  207. }
  208. }
  209. 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);
  210. /* Now check if it's really the largest, and not just the joint-largest */
  211. joint_leader = 0;
  212. TAILQ_FOREACH(cur_partition, &info->partition_list, entries) {
  213. if (largest_partition != cur_partition &&
  214. largest_partition->num_nodes == cur_partition->num_nodes) {
  215. joint_leader = 1;
  216. }
  217. }
  218. if (!joint_leader) {
  219. /* Largest partition is unique, allow us to run if we're in that partition. */
  220. if (rings_eq(&largest_partition->ring_id, &info->ring_id)) {
  221. qnetd_log(LOG_DEBUG, "algo-lms: We are in the largest partition. ACK\n");
  222. *result_vote = info->last_result = TLV_VOTE_ACK;
  223. }
  224. else {
  225. qnetd_log(LOG_DEBUG, "algo-lms: We are NOT in the largest partition. NACK\n");
  226. *result_vote = info->last_result = TLV_VOTE_NACK;
  227. }
  228. }
  229. else {
  230. int tb_node_id;
  231. struct tlv_ring_id tb_node_ring_id = {0LL, 0};
  232. if (client->tie_breaker.mode == TLV_TIE_BREAKER_MODE_LOWEST) {
  233. tb_node_id = INT_MAX;
  234. }
  235. else {
  236. tb_node_id = 0;
  237. }
  238. /* Find the tie_breaker node */
  239. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  240. struct qnetd_algo_lms_info *other_info = other_client->algorithm_data;
  241. switch (client->tie_breaker.mode) {
  242. case TLV_TIE_BREAKER_MODE_LOWEST:
  243. if (other_client->node_id < tb_node_id) {
  244. tb_node_id = other_client->node_id;
  245. memcpy(&tb_node_ring_id, &other_info->ring_id, sizeof(struct tlv_ring_id));
  246. qnetd_log(LOG_DEBUG, "algo-lms: Looking for low node ID. found (%d/%ld) %d", tb_node_ring_id.node_id, tb_node_ring_id.seq, tb_node_id);
  247. }
  248. break;
  249. case TLV_TIE_BREAKER_MODE_HIGHEST:
  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 high node ID. found (%d/%ld) %d", tb_node_ring_id.node_id, tb_node_ring_id.seq, tb_node_id);
  254. }
  255. break;
  256. case TLV_TIE_BREAKER_MODE_NODE_ID:
  257. if (client->tie_breaker.node_id == client->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 nominated node ID. found (%d/%ld) %d", tb_node_ring_id.node_id, tb_node_ring_id.seq, tb_node_id);
  261. }
  262. break;
  263. default:
  264. qnetd_log(LOG_DEBUG, "algo-lms: denied vote because tie-breaker option is invalid: %d", client->tie_breaker.mode);
  265. memset(&tb_node_ring_id, 0, sizeof(struct tlv_ring_id));
  266. }
  267. }
  268. if (rings_eq(&tb_node_ring_id, &info->ring_id)) {
  269. 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);
  270. *result_vote = info->last_result = TLV_VOTE_ACK;
  271. }
  272. else {
  273. 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);
  274. *result_vote = info->last_result = TLV_VOTE_NACK;
  275. }
  276. }
  277. free_partitions(info);
  278. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  279. }
  280. enum tlv_reply_error_code
  281. qnetd_algo_lms_client_init(struct qnetd_client *client)
  282. {
  283. struct qnetd_algo_lms_info *info;
  284. info = malloc(sizeof(struct qnetd_algo_lms_info));
  285. if (!info) {
  286. return (TLV_REPLY_ERROR_CODE_INTERNAL_ERROR);
  287. }
  288. memset(info, 0, sizeof(*info));
  289. client->algorithm_data = info;
  290. info->last_result = 0; /* status unknown, or NEW */
  291. TAILQ_INIT(&info->partition_list);
  292. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  293. }
  294. /*
  295. * Called after client sent configuration node list
  296. * All client fields are already set. Nodes is actual node list, initial is used
  297. * to distinquish between initial node list and changed node list.
  298. * msg_seq_num is 32-bit number set by client. If client sent config file version,
  299. * config_version_set is set to 1 and config_version contains valid config file version.
  300. *
  301. * Function has to return result_vote. This can be one of ack/nack, ask_later (client
  302. * should ask later for a vote) or wait_for_reply (client should wait for reply).
  303. *
  304. * Return TLV_REPLY_ERROR_CODE_NO_ERROR on success, different TLV_REPLY_ERROR_CODE_*
  305. * on failure (error is send back to client)
  306. */
  307. enum tlv_reply_error_code
  308. qnetd_algo_lms_config_node_list_received(struct qnetd_client *client,
  309. uint32_t msg_seq_num, int config_version_set, uint64_t config_version,
  310. const struct node_list *nodes, int initial, enum tlv_vote *result_vote)
  311. {
  312. struct node_list_entry *node_info;
  313. struct qnetd_algo_lms_info *info = client->algorithm_data;
  314. int node_count = 0;
  315. TAILQ_FOREACH(node_info, nodes, entries) {
  316. node_count++;
  317. }
  318. info->num_config_nodes = node_count;
  319. qnetd_log(LOG_DEBUG, "algo-lms: cluster %s config_list has %d nodes", client->cluster_name, node_count);
  320. *result_vote = TLV_VOTE_NO_CHANGE;
  321. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  322. }
  323. /*
  324. * Called after client sent membership node list.
  325. * All client fields are already set. Nodes is actual node list.
  326. * msg_seq_num is 32-bit number set by client. If client sent config file version,
  327. * config_version_set is set to 1 and config_version contains valid config file version.
  328. * ring_id and quorate are copied from client votequorum callback.
  329. *
  330. * Function has to return result_vote. This can be one of ack/nack, ask_later (client
  331. * should ask later for a vote) or wait_for_reply (client should wait for reply).
  332. *
  333. * Return TLV_REPLY_ERROR_CODE_NO_ERROR on success, different TLV_REPLY_ERROR_CODE_*
  334. * on failure (error is send back to client)
  335. */
  336. enum tlv_reply_error_code
  337. qnetd_algo_lms_membership_node_list_received(struct qnetd_client *client,
  338. uint32_t msg_seq_num, const struct tlv_ring_id *ring_id,
  339. const struct node_list *nodes, enum tlv_vote *result_vote)
  340. {
  341. struct qnetd_algo_lms_info *info = client->algorithm_data;
  342. /* Save this now */
  343. memcpy(&info->ring_id, ring_id, sizeof(*ring_id));
  344. qnetd_log(LOG_DEBUG, "\nalgo-lms: membership list from node %d partition %d/%ld", client->node_id, ring_id->node_id, ring_id->seq);
  345. return do_lms_algorithm(client, result_vote);
  346. }
  347. enum tlv_reply_error_code
  348. qnetd_algo_lms_quorum_node_list_received(struct qnetd_client *client,
  349. uint32_t msg_seq_num, enum tlv_quorate quorate, const struct node_list *nodes, enum tlv_vote *result_vote)
  350. {
  351. return do_lms_algorithm(client, result_vote);
  352. }
  353. /*
  354. * Called after client disconnect. Client structure is still existing (and it's part
  355. * of a client->cluster), but it is destroyed (and removed from cluster) right after
  356. * this callback finishes. Callback is used mainly for destroing client->algorithm_data.
  357. */
  358. void
  359. qnetd_algo_lms_client_disconnect(struct qnetd_client *client, int server_going_down)
  360. {
  361. qnetd_log(LOG_DEBUG, "\nalgo-lms: Client %p (cluster %s, node_id %"PRIx32") "
  362. "disconnect", client, client->cluster_name, client->node_id);
  363. qnetd_log(LOG_INFO, "algo-lms: server going down %u", server_going_down);
  364. free(client->algorithm_data);
  365. }
  366. /*
  367. * Called after client sent ask for vote message. This is usually happening after server
  368. * replied TLV_VOTE_ASK_LATER.
  369. */
  370. enum tlv_reply_error_code
  371. qnetd_algo_lms_ask_for_vote_received(struct qnetd_client *client, uint32_t msg_seq_num,
  372. enum tlv_vote *result_vote)
  373. {
  374. qnetd_log(LOG_DEBUG, "\nalgo-lms: Client %p (cluster %s, node_id %"PRIx32") "
  375. "asked for a vote", client, client->cluster_name, client->node_id);
  376. return do_lms_algorithm(client, result_vote);
  377. }
  378. enum tlv_reply_error_code
  379. qnetd_algo_lms_vote_info_reply_received(struct qnetd_client *client, uint32_t msg_seq_num)
  380. {
  381. qnetd_log(LOG_DEBUG, "\nalgo-lms: Client %p (cluster %s, node_id %"PRIx32") "
  382. "replied back to vote info message", client, client->cluster_name, client->node_id);
  383. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  384. }
  385. static struct qnetd_algorithm qnetd_algo_lms = {
  386. .init = qnetd_algo_lms_client_init,
  387. .config_node_list_received = qnetd_algo_lms_config_node_list_received,
  388. .membership_node_list_received = qnetd_algo_lms_membership_node_list_received,
  389. .quorum_node_list_received = qnetd_algo_lms_quorum_node_list_received,
  390. .client_disconnect = qnetd_algo_lms_client_disconnect,
  391. .ask_for_vote_received = qnetd_algo_lms_ask_for_vote_received,
  392. .vote_info_reply_received = qnetd_algo_lms_vote_info_reply_received,
  393. };
  394. enum tlv_reply_error_code qnetd_algo_lms_register()
  395. {
  396. return qnetd_algorithm_register(TLV_DECISION_ALGORITHM_TYPE_LMS, &qnetd_algo_lms);
  397. }