qnetd-algo-lms.c 15 KB

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