qnetd-algo-2nodelms.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 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 nominated tie_breaker node
  42. *
  43. * If there are more than two nodes, then we don't return a vote.
  44. * this is not our job.
  45. */
  46. #include <sys/types.h>
  47. #include <string.h>
  48. #include <limits.h>
  49. #include "qnetd-algo-2nodelms.h"
  50. #include "qnetd-log.h"
  51. #include "qnetd-cluster-list.h"
  52. #include "qnetd-algo-utils.h"
  53. #include "utils.h"
  54. struct qnetd_algo_2nodelms_info {
  55. int num_config_nodes;
  56. enum tlv_vote last_result;
  57. };
  58. enum tlv_reply_error_code
  59. qnetd_algo_2nodelms_client_init(struct qnetd_client *client)
  60. {
  61. struct qnetd_algo_2nodelms_info *info;
  62. info = malloc(sizeof(struct qnetd_algo_2nodelms_info));
  63. if (!info) {
  64. return (TLV_REPLY_ERROR_CODE_INTERNAL_ERROR);
  65. }
  66. client->algorithm_data = info;
  67. info->last_result = 0;
  68. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  69. }
  70. /*
  71. * Called after client sent configuration node list
  72. * All client fields are already set. Nodes is actual node list, initial is used
  73. * to distinquish between initial node list and changed node list.
  74. * msg_seq_num is 32-bit number set by client. If client sent config file version,
  75. * config_version_set is set to 1 and config_version contains valid config file version.
  76. *
  77. * Function has to return result_vote. This can be one of ack/nack, ask_later (client
  78. * should ask later for a vote) or wait_for_reply (client should wait for reply).
  79. *
  80. * Return TLV_REPLY_ERROR_CODE_NO_ERROR on success, different TLV_REPLY_ERROR_CODE_*
  81. * on failure (error is sent back to client)
  82. */
  83. enum tlv_reply_error_code
  84. qnetd_algo_2nodelms_config_node_list_received(struct qnetd_client *client,
  85. uint32_t msg_seq_num, int config_version_set, uint64_t config_version,
  86. const struct node_list *nodes, int initial, enum tlv_vote *result_vote)
  87. {
  88. struct node_list_entry *node_info;
  89. struct qnetd_algo_2nodelms_info *info = client->algorithm_data;
  90. int node_count = 0;
  91. /* Check this is a 2 node cluster */
  92. TAILQ_FOREACH(node_info, nodes, entries) {
  93. node_count++;
  94. }
  95. info->num_config_nodes = node_count;
  96. qnetd_log(LOG_DEBUG, "algo-2nodelms: cluster %s config_list has %d nodes", client->cluster_name, node_count);
  97. if (node_count != 2) {
  98. qnetd_log(LOG_INFO, "algo-2nodelms: cluster %s does not have 2 configured nodes, it has %d", client->cluster_name, node_count);
  99. *result_vote = TLV_VOTE_NACK;
  100. return (TLV_REPLY_ERROR_CODE_UNSUPPORTED_DECISION_ALGORITHM);
  101. }
  102. *result_vote = TLV_VOTE_NO_CHANGE;
  103. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  104. }
  105. /*
  106. * Called after client sent membership node list.
  107. * All client fields are already set. Nodes is actual node list.
  108. * msg_seq_num is 32-bit number set by client. If client sent config file version,
  109. * config_version_set is set to 1 and config_version contains valid config file version.
  110. * ring_id and quorate are copied from client votequorum callback.
  111. *
  112. * Function has to return result_vote. This can be one of ack/nack, ask_later (client
  113. * should ask later for a vote) or wait_for_reply (client should wait for reply).
  114. *
  115. * Return TLV_REPLY_ERROR_CODE_NO_ERROR on success, different TLV_REPLY_ERROR_CODE_*
  116. * on failure (error is sent back to client)
  117. */
  118. enum tlv_reply_error_code
  119. qnetd_algo_2nodelms_membership_node_list_received(struct qnetd_client *client,
  120. uint32_t msg_seq_num, const struct tlv_ring_id *ring_id,
  121. const struct node_list *nodes, enum tlv_heuristics heuristics,
  122. enum tlv_vote *result_vote)
  123. {
  124. struct node_list_entry *node_info;
  125. struct qnetd_client *other_client;
  126. struct qnetd_algo_2nodelms_info *info = client->algorithm_data;
  127. int node_count = 0;
  128. uint32_t low_node_id = UINT32_MAX;
  129. uint32_t high_node_id = 0;
  130. enum tlv_heuristics other_node_heuristics;
  131. /* If we're a newcomer and there is another active partition, then we must NACK
  132. * to avoid quorum moving to us from already active nodes.
  133. */
  134. if (info->last_result == 0) {
  135. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  136. struct qnetd_algo_2nodelms_info *other_info = other_client->algorithm_data;
  137. if (!tlv_ring_id_eq(ring_id, &other_client->last_ring_id) &&
  138. other_info->last_result == TLV_VOTE_ACK) {
  139. /* Don't save NACK, we need to know subsequently if we haven't been voting */
  140. *result_vote = TLV_VOTE_NACK;
  141. qnetd_log(LOG_DEBUG, "algo-2nodelms: we are a new partition and another active partition exists. NACK");
  142. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  143. }
  144. }
  145. }
  146. /* If both nodes are present, then we're OK. return a vote */
  147. TAILQ_FOREACH(node_info, nodes, entries) {
  148. node_count++;
  149. }
  150. qnetd_log(LOG_DEBUG, "algo-2nodelms: cluster %s (client %p nodeid "UTILS_PRI_NODE_ID") membership list has %d member nodes (ring ID "UTILS_PRI_RING_ID")", client->cluster_name, client, client->node_id, node_count, ring_id->node_id, ring_id->seq);
  151. if (node_count == 2) {
  152. qnetd_log(LOG_DEBUG, "algo-2nodelms: cluster %s running normally. Both nodes active", client->cluster_name);
  153. *result_vote = info->last_result = TLV_VOTE_ACK;
  154. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  155. }
  156. /* Now look for other clients connected from this cluster that can't see us any more */
  157. node_count = 0;
  158. other_node_heuristics = TLV_HEURISTICS_UNDEFINED;
  159. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  160. node_count++;
  161. qnetd_log(LOG_DEBUG, "algo-2nodelms: seen nodeid "UTILS_PRI_NODE_ID" on client %p (ring ID "UTILS_PRI_RING_ID")", other_client->node_id, other_client, other_client->last_ring_id.node_id, other_client->last_ring_id.seq);
  162. if (other_client->node_id < low_node_id) {
  163. low_node_id = other_client->node_id;
  164. }
  165. if (other_client->node_id > high_node_id) {
  166. high_node_id = other_client->node_id;
  167. }
  168. if (other_client != client) {
  169. other_node_heuristics = other_client->last_heuristics;
  170. }
  171. }
  172. qnetd_log(LOG_DEBUG, "algo-2nodelms: cluster %s %d nodes running independently", client->cluster_name, node_count);
  173. /* Only 1 node alive .. allow it to continue */
  174. if (node_count == 1) {
  175. qnetd_log(LOG_DEBUG, "algo-2nodelms: cluster %s running on 'last-man'", client->cluster_name);
  176. *result_vote = info->last_result = TLV_VOTE_ACK;
  177. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  178. }
  179. /*
  180. * Both nodes are alive.
  181. * Check their heuristics.
  182. */
  183. if (tlv_heuristics_cmp(heuristics, other_node_heuristics) > 0) {
  184. *result_vote = info->last_result = TLV_VOTE_ACK;
  185. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  186. } else if (tlv_heuristics_cmp(heuristics, other_node_heuristics) < 0) {
  187. *result_vote = info->last_result = TLV_VOTE_NACK;
  188. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  189. }
  190. /* Heuristics are equal -> Only give a vote to the nominated tie-breaker node */
  191. switch (client->tie_breaker.mode) {
  192. case TLV_TIE_BREAKER_MODE_LOWEST:
  193. if (client->node_id == low_node_id) {
  194. qnetd_log(LOG_DEBUG, "algo-2nodelms: cluster %s running on low node-id %d", client->cluster_name, low_node_id);
  195. *result_vote = info->last_result = TLV_VOTE_ACK;
  196. }
  197. else {
  198. qnetd_log(LOG_DEBUG, "algo-2nodelms: cluster %s node-id %d denied vote because low nodeid %d is active", client->cluster_name, client->node_id, low_node_id);
  199. *result_vote = info->last_result = TLV_VOTE_NACK;
  200. }
  201. break;
  202. case TLV_TIE_BREAKER_MODE_HIGHEST:
  203. if (client->node_id == high_node_id) {
  204. qnetd_log(LOG_DEBUG, "algo-2nodelms: cluster %s running on high node-id %d", client->cluster_name, high_node_id);
  205. *result_vote = info->last_result = TLV_VOTE_ACK;
  206. }
  207. else {
  208. qnetd_log(LOG_DEBUG, "algo-2nodelms: cluster %s node-id %d denied vote because high nodeid %d is active", client->cluster_name, client->node_id, high_node_id);
  209. *result_vote = info->last_result = TLV_VOTE_NACK;
  210. }
  211. break;
  212. case TLV_TIE_BREAKER_MODE_NODE_ID:
  213. if (client->node_id == client->tie_breaker.node_id) {
  214. qnetd_log(LOG_DEBUG, "algo-2nodelms: cluster %s running on nominated tie-breaker node %d", client->cluster_name, client->tie_breaker.node_id);
  215. *result_vote = info->last_result = TLV_VOTE_ACK;
  216. }
  217. else {
  218. qnetd_log(LOG_DEBUG, "algo-2nodelms: cluster %s node-id %d denied vote because nominated tie-breaker nodeid %d is active", client->cluster_name, client->node_id, client->tie_breaker.node_id);
  219. *result_vote = info->last_result = TLV_VOTE_NACK;
  220. }
  221. break;
  222. default:
  223. qnetd_log(LOG_DEBUG, "algo-2nodelms: cluster %s node-id %d denied vote because tie-breaker option is invalid: %d", client->cluster_name, client->node_id, client->tie_breaker.mode);
  224. *result_vote = info->last_result = TLV_VOTE_NACK;
  225. }
  226. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  227. }
  228. enum tlv_reply_error_code
  229. qnetd_algo_2nodelms_quorum_node_list_received(struct qnetd_client *client,
  230. uint32_t msg_seq_num, enum tlv_quorate quorate, const struct node_list *nodes,
  231. enum tlv_vote *result_vote)
  232. {
  233. *result_vote = TLV_VOTE_NO_CHANGE;
  234. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  235. }
  236. /*
  237. * Called after client disconnect. Client structure is still existing (and it's part
  238. * of a client->cluster), but it is destroyed (and removed from cluster) right after
  239. * this callback finishes. Callback is used mainly for destroing client->algorithm_data.
  240. */
  241. void
  242. qnetd_algo_2nodelms_client_disconnect(struct qnetd_client *client, int server_going_down)
  243. {
  244. qnetd_log(LOG_INFO, "algo-2nodelms: Client %p (cluster %s, node_id "UTILS_PRI_NODE_ID") "
  245. "disconnect", client, client->cluster_name, client->node_id);
  246. qnetd_log(LOG_INFO, "algo-2nodelms: server going down %u", server_going_down);
  247. free(client->algorithm_data);
  248. }
  249. /*
  250. * Called after client sent ask for vote message. This is usually happening after server
  251. * replied TLV_VOTE_ASK_LATER.
  252. */
  253. enum tlv_reply_error_code
  254. qnetd_algo_2nodelms_ask_for_vote_received(struct qnetd_client *client, uint32_t msg_seq_num,
  255. enum tlv_vote *result_vote)
  256. {
  257. struct qnetd_algo_2nodelms_info *info = client->algorithm_data;
  258. qnetd_log(LOG_INFO, "algo-2nodelms: Client %p (cluster %s, node_id "UTILS_PRI_NODE_ID") "
  259. "asked for a vote", client, client->cluster_name, client->node_id);
  260. if (info->last_result == 0) {
  261. *result_vote = TLV_VOTE_ASK_LATER;
  262. }
  263. else {
  264. *result_vote = info->last_result;
  265. }
  266. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  267. }
  268. enum tlv_reply_error_code
  269. qnetd_algo_2nodelms_vote_info_reply_received(struct qnetd_client *client, uint32_t msg_seq_num)
  270. {
  271. qnetd_log(LOG_INFO, "algo-2nodelms: Client %p (cluster %s, node_id "UTILS_PRI_NODE_ID") "
  272. "replied back to vote info message", client, client->cluster_name, client->node_id);
  273. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  274. }
  275. enum tlv_reply_error_code
  276. qnetd_algo_2nodelms_heuristics_change_received(struct qnetd_client *client, uint32_t msg_seq_num,
  277. enum tlv_heuristics heuristics, enum tlv_vote *result_vote)
  278. {
  279. qnetd_log(LOG_INFO, "algo-2nodelms: heuristics change is not supported.");
  280. *result_vote = TLV_VOTE_NO_CHANGE;
  281. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  282. }
  283. enum tlv_reply_error_code
  284. qnetd_algo_2nodelms_timer_callback(struct qnetd_client *client, int *reschedule_timer,
  285. int *send_vote, enum tlv_vote *result_vote)
  286. {
  287. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  288. }
  289. static struct qnetd_algorithm qnetd_algo_2nodelms = {
  290. .init = qnetd_algo_2nodelms_client_init,
  291. .config_node_list_received = qnetd_algo_2nodelms_config_node_list_received,
  292. .membership_node_list_received = qnetd_algo_2nodelms_membership_node_list_received,
  293. .quorum_node_list_received = qnetd_algo_2nodelms_quorum_node_list_received,
  294. .client_disconnect = qnetd_algo_2nodelms_client_disconnect,
  295. .ask_for_vote_received = qnetd_algo_2nodelms_ask_for_vote_received,
  296. .vote_info_reply_received = qnetd_algo_2nodelms_vote_info_reply_received,
  297. .heuristics_change_received = qnetd_algo_2nodelms_heuristics_change_received,
  298. .timer_callback = qnetd_algo_2nodelms_timer_callback,
  299. };
  300. enum tlv_reply_error_code qnetd_algo_2nodelms_register()
  301. {
  302. return qnetd_algorithm_register(TLV_DECISION_ALGORITHM_TYPE_2NODELMS, &qnetd_algo_2nodelms);
  303. }