qnetd-algo-ffsplit.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * Copyright (c) 2015-2020 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. #include <sys/types.h>
  35. #include <string.h>
  36. #include "log.h"
  37. #include "log-common.h"
  38. #include "qnetd-algo-ffsplit.h"
  39. #include "qnetd-log-debug.h"
  40. #include "qnetd-cluster-list.h"
  41. #include "qnetd-cluster.h"
  42. #include "qnetd-client-send.h"
  43. enum qnetd_algo_ffsplit_cluster_state {
  44. QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_CHANGE,
  45. QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_STABLE_MEMBERSHIP,
  46. QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_NACKS,
  47. QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_ACKS,
  48. };
  49. struct qnetd_algo_ffsplit_cluster_data {
  50. enum qnetd_algo_ffsplit_cluster_state cluster_state;
  51. struct node_list quorate_partition_node_list;
  52. };
  53. enum qnetd_algo_ffsplit_client_state {
  54. QNETD_ALGO_FFSPLIT_CLIENT_STATE_WAITING_FOR_CHANGE,
  55. QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_NACK,
  56. QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_ACK,
  57. };
  58. struct qnetd_algo_ffsplit_client_data {
  59. enum qnetd_algo_ffsplit_client_state client_state;
  60. uint32_t vote_info_expected_seq_num;
  61. };
  62. enum tlv_reply_error_code
  63. qnetd_algo_ffsplit_client_init(struct qnetd_client *client)
  64. {
  65. struct qnetd_algo_ffsplit_cluster_data *cluster_data;
  66. struct qnetd_algo_ffsplit_client_data *client_data;
  67. if (qnetd_cluster_size(client->cluster) == 1) {
  68. cluster_data = malloc(sizeof(*cluster_data));
  69. if (cluster_data == NULL) {
  70. log(LOG_ERR, "ffsplit: Can't initialize cluster data for client %s",
  71. client->addr_str);
  72. return (TLV_REPLY_ERROR_CODE_INTERNAL_ERROR);
  73. }
  74. memset(cluster_data, 0, sizeof(*cluster_data));
  75. cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_CHANGE;
  76. node_list_init(&cluster_data->quorate_partition_node_list);
  77. client->cluster->algorithm_data = cluster_data;
  78. }
  79. client_data = malloc(sizeof(*client_data));
  80. if (client_data == NULL) {
  81. log(LOG_ERR, "ffsplit: Can't initialize node data for client %s",
  82. client->addr_str);
  83. return (TLV_REPLY_ERROR_CODE_INTERNAL_ERROR);
  84. }
  85. memset(client_data, 0, sizeof(*client_data));
  86. client_data->client_state = QNETD_ALGO_FFSPLIT_CLIENT_STATE_WAITING_FOR_CHANGE;
  87. client->algorithm_data = client_data;
  88. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  89. }
  90. static int
  91. qnetd_algo_ffsplit_is_preferred_partition(const struct qnetd_client *client,
  92. const struct node_list *config_node_list, const struct node_list *membership_node_list)
  93. {
  94. uint32_t preferred_node_id;
  95. struct node_list_entry *node_entry;
  96. int case_processed;
  97. preferred_node_id = 0;
  98. case_processed = 0;
  99. switch (client->tie_breaker.mode) {
  100. case TLV_TIE_BREAKER_MODE_LOWEST:
  101. node_entry = TAILQ_FIRST(config_node_list);
  102. preferred_node_id = node_entry->node_id;
  103. TAILQ_FOREACH(node_entry, config_node_list, entries) {
  104. if (node_entry->node_id < preferred_node_id) {
  105. preferred_node_id = node_entry->node_id;
  106. }
  107. }
  108. case_processed = 1;
  109. break;
  110. case TLV_TIE_BREAKER_MODE_HIGHEST:
  111. node_entry = TAILQ_FIRST(config_node_list);
  112. preferred_node_id = node_entry->node_id;
  113. TAILQ_FOREACH(node_entry, config_node_list, entries) {
  114. if (node_entry->node_id > preferred_node_id) {
  115. preferred_node_id = node_entry->node_id;
  116. }
  117. }
  118. case_processed = 1;
  119. break;
  120. case TLV_TIE_BREAKER_MODE_NODE_ID:
  121. preferred_node_id = client->tie_breaker.node_id;
  122. case_processed = 1;
  123. break;
  124. }
  125. if (!case_processed) {
  126. log(LOG_CRIT, "qnetd_algo_ffsplit_is_preferred_partition unprocessed "
  127. "tie_breaker.mode");
  128. exit(EXIT_FAILURE);
  129. }
  130. return (node_list_find_node_id(membership_node_list, preferred_node_id) != NULL);
  131. }
  132. static int
  133. qnetd_algo_ffsplit_is_membership_stable(const struct qnetd_client *client, int client_leaving,
  134. const struct tlv_ring_id *ring_id, const struct node_list *config_node_list,
  135. const struct node_list *membership_node_list)
  136. {
  137. const struct qnetd_client *iter_client1, *iter_client2;
  138. const struct node_list *config_node_list1, *config_node_list2;
  139. const struct node_list *membership_node_list1, *membership_node_list2;
  140. const struct node_list_entry *iter_node1, *iter_node2;
  141. const struct node_list_entry *iter_node3, *iter_node4;
  142. const struct tlv_ring_id *ring_id1, *ring_id2;
  143. /*
  144. * Test if all active clients share same config list.
  145. */
  146. TAILQ_FOREACH(iter_client1, &client->cluster->client_list, cluster_entries) {
  147. TAILQ_FOREACH(iter_client2, &client->cluster->client_list, cluster_entries) {
  148. if (iter_client1 == iter_client2) {
  149. continue;
  150. }
  151. if (iter_client1->node_id == client->node_id) {
  152. if (client_leaving) {
  153. continue;
  154. }
  155. config_node_list1 = config_node_list;
  156. } else {
  157. config_node_list1 = &iter_client1->configuration_node_list;
  158. }
  159. if (iter_client2->node_id == client->node_id) {
  160. if (client_leaving) {
  161. continue;
  162. }
  163. config_node_list2 = config_node_list;
  164. } else {
  165. config_node_list2 = &iter_client2->configuration_node_list;
  166. }
  167. /*
  168. * Walk thru all node ids in given config node list...
  169. */
  170. TAILQ_FOREACH(iter_node1, config_node_list1, entries) {
  171. /*
  172. * ... and try to find given node id in other list
  173. */
  174. iter_node2 = node_list_find_node_id(config_node_list2, iter_node1->node_id);
  175. if (iter_node2 == NULL) {
  176. /*
  177. * Node with iter_node1->node_id was not found in
  178. * config_node_list2 -> lists doesn't match
  179. */
  180. return (0);
  181. }
  182. }
  183. }
  184. }
  185. /*
  186. * Test if same partitions share same ring ids and membership node list
  187. */
  188. TAILQ_FOREACH(iter_client1, &client->cluster->client_list, cluster_entries) {
  189. if (iter_client1->node_id == client->node_id) {
  190. if (client_leaving) {
  191. continue;
  192. }
  193. membership_node_list1 = membership_node_list;
  194. ring_id1 = ring_id;
  195. } else {
  196. membership_node_list1 = &iter_client1->last_membership_node_list;
  197. ring_id1 = &iter_client1->last_ring_id;
  198. }
  199. /*
  200. * Walk thru all memberships nodes
  201. */
  202. TAILQ_FOREACH(iter_node1, membership_node_list1, entries) {
  203. /*
  204. * try to find client with given node id
  205. */
  206. iter_client2 = qnetd_cluster_find_client_by_node_id(client->cluster,
  207. iter_node1->node_id);
  208. if (iter_client2 == NULL) {
  209. /*
  210. * Client with given id is not connected
  211. */
  212. continue;
  213. }
  214. if (iter_client2->node_id == client->node_id) {
  215. if (client_leaving) {
  216. continue;
  217. }
  218. membership_node_list2 = membership_node_list;
  219. ring_id2 = ring_id;
  220. } else {
  221. membership_node_list2 = &iter_client2->last_membership_node_list;
  222. ring_id2 = &iter_client2->last_ring_id;
  223. }
  224. /*
  225. * Compare ring ids
  226. */
  227. if (!tlv_ring_id_eq(ring_id1, ring_id2)) {
  228. return (0);
  229. }
  230. /*
  231. * Now compare that membership node list equals, so walk thru all
  232. * members ...
  233. */
  234. TAILQ_FOREACH(iter_node3, membership_node_list1, entries) {
  235. /*
  236. * ... and try to find given node id in other membership node list
  237. */
  238. iter_node4 = node_list_find_node_id(membership_node_list2, iter_node3->node_id);
  239. if (iter_node4 == NULL) {
  240. /*
  241. * Node with iter_node3->node_id was not found in
  242. * membership_node_list2 -> lists doesn't match
  243. */
  244. return (0);
  245. }
  246. }
  247. }
  248. }
  249. return (1);
  250. }
  251. static void
  252. qnetd_algo_ffsplit_get_active_clients_in_partition_stats(const struct qnetd_client *client,
  253. const struct node_list *client_membership_node_list, enum tlv_heuristics client_heuristics,
  254. size_t *no_clients, size_t *no_heuristics_pass, size_t *no_heuristics_fail)
  255. {
  256. const struct node_list_entry *iter_node;
  257. const struct qnetd_client *iter_client;
  258. enum tlv_heuristics iter_heuristics;
  259. *no_clients = 0;
  260. *no_heuristics_pass = 0;
  261. *no_heuristics_fail = 0;
  262. if (client == NULL || client_membership_node_list == NULL) {
  263. return ;
  264. }
  265. TAILQ_FOREACH(iter_node, client_membership_node_list, entries) {
  266. iter_client = qnetd_cluster_find_client_by_node_id(client->cluster,
  267. iter_node->node_id);
  268. if (iter_client != NULL) {
  269. (*no_clients)++;
  270. if (iter_client == client) {
  271. iter_heuristics = client_heuristics;
  272. } else {
  273. iter_heuristics = iter_client->last_heuristics;
  274. }
  275. if (iter_heuristics == TLV_HEURISTICS_PASS) {
  276. (*no_heuristics_pass)++;
  277. } else if (iter_heuristics == TLV_HEURISTICS_FAIL) {
  278. (*no_heuristics_fail)++;
  279. }
  280. }
  281. }
  282. }
  283. /*
  284. * Compares two partitions. Return 1 if client1, config_node_list1, membership_node_list1 is
  285. * "better" than client2, config_node_list2, membership_node_list2
  286. */
  287. static int
  288. qnetd_algo_ffsplit_partition_cmp(const struct qnetd_client *client1,
  289. const struct node_list *config_node_list1, const struct node_list *membership_node_list1,
  290. enum tlv_heuristics heuristics_1,
  291. const struct qnetd_client *client2,
  292. const struct node_list *config_node_list2, const struct node_list *membership_node_list2,
  293. enum tlv_heuristics heuristics_2,
  294. const struct node_list *quorate_partition_node_list)
  295. {
  296. size_t part1_active_clients, part2_active_clients;
  297. size_t part1_no_heuristics_pass, part2_no_heuristics_pass;
  298. size_t part1_no_heuristics_fail, part2_no_heuristics_fail;
  299. size_t part1_score, part2_score;
  300. /* Result of node_list_find_node_id of client 1 node id in quorate_partition_node_list */
  301. struct node_list_entry *qpnl_client1;
  302. /* Result of node_list_find_node_id of client 2 node id in quorate_partition_node_list */
  303. struct node_list_entry *qpnl_client2;
  304. int res;
  305. res = -1;
  306. if (node_list_size(config_node_list1) % 2 != 0) {
  307. /*
  308. * Odd clusters never split into 50:50.
  309. */
  310. if (node_list_size(membership_node_list1) > node_list_size(config_node_list1) / 2) {
  311. res = 1; goto exit_res;
  312. } else {
  313. res = 0; goto exit_res;
  314. }
  315. } else {
  316. if (node_list_size(membership_node_list1) > node_list_size(config_node_list1) / 2) {
  317. res = 1; goto exit_res;
  318. } else if (node_list_size(membership_node_list1) < node_list_size(config_node_list1) / 2) {
  319. res = 0; goto exit_res;
  320. }
  321. /*
  322. * 50:50 split
  323. */
  324. /*
  325. * Check how many active clients are in partitions and heuristics results
  326. */
  327. qnetd_algo_ffsplit_get_active_clients_in_partition_stats(client1,
  328. membership_node_list1, heuristics_1, &part1_active_clients,
  329. &part1_no_heuristics_pass, &part1_no_heuristics_fail);
  330. qnetd_algo_ffsplit_get_active_clients_in_partition_stats(client2,
  331. membership_node_list2, heuristics_2, &part2_active_clients,
  332. &part2_no_heuristics_pass, &part2_no_heuristics_fail);
  333. /*
  334. * Partition can contain clients with one of 4 states:
  335. * 1. Not-connected to qnetd (D)
  336. * 2. Disabled heuristics (U)
  337. * 3. Enabled heuristics with pass result (P)
  338. * 4. Enabled heuristics with fail result (F)
  339. *
  340. * The question is, what partition should get vote is kind of hard with
  341. * so much states. Following simple "score" seems to be good enough, but may
  342. * be suboptimal in some cases. As and example let's say there are
  343. * 2 partitions with 4 nodes each. Partition 1 looks like PDDD and partition 2 looks
  344. * like FUUU. Partition 1 score is 1 + (1 - 0), partition 2 score is 4 + (0 - 1).
  345. * Partition 2 wins eventho there is one processor with failed heuristics.
  346. */
  347. part1_score = part1_active_clients + (part1_no_heuristics_pass - part1_no_heuristics_fail);
  348. part2_score = part2_active_clients + (part2_no_heuristics_pass - part2_no_heuristics_fail);
  349. if (part1_score > part2_score) {
  350. res = 1; goto exit_res;
  351. } else if (part1_score < part2_score) {
  352. res = 0; goto exit_res;
  353. }
  354. /*
  355. * This also handles NULL client (best_client)
  356. */
  357. if (part1_active_clients > part2_active_clients) {
  358. res = 1; goto exit_res;
  359. } else if (part1_active_clients < part2_active_clients) {
  360. res = 0; goto exit_res;
  361. }
  362. /*
  363. * Use keep active partition tie-breaker if enabled for both clients
  364. */
  365. if (client1->keep_active_partition_tie_breaker &&
  366. client2 != NULL && client2->keep_active_partition_tie_breaker) {
  367. qpnl_client1 = node_list_find_node_id(quorate_partition_node_list, client1->node_id);
  368. qpnl_client2 = node_list_find_node_id(quorate_partition_node_list, client2->node_id);
  369. /*
  370. * Client 1 in quorate partition, client 2 isn't and vice-versa.
  371. * If both either doesn't exist in quorate partion or both exists use
  372. * next tie-breaker
  373. */
  374. if (qpnl_client1 != NULL && qpnl_client2 == NULL) {
  375. res = 1; goto exit_res;
  376. } else if (qpnl_client1 == NULL && qpnl_client2 != NULL) {
  377. res = 0; goto exit_res;
  378. }
  379. }
  380. /*
  381. * All previous metrics failed (client1 and client2 are equal).
  382. * Scores (number of clients + heuristics) are equal,
  383. * number of active clients in both partitions equals
  384. * and keep_active_partition_tie_breaker is either disabled or both clients
  385. * either were or weren't members of previous quorate partition.
  386. * Last step is to use tie-breaker.
  387. */
  388. if (qnetd_algo_ffsplit_is_preferred_partition(client1, config_node_list1,
  389. membership_node_list1)) {
  390. res = 1; goto exit_res;
  391. } else {
  392. res = 0; goto exit_res;
  393. }
  394. }
  395. exit_res:
  396. if (res == -1) {
  397. log(LOG_CRIT, "qnetd_algo_ffsplit_partition_cmp unhandled case");
  398. exit(EXIT_FAILURE);
  399. /* NOTREACHED */
  400. }
  401. return (res);
  402. }
  403. /*
  404. * Select best partition for given client->cluster.
  405. * If there is no partition which could become quorate, NULL is returned
  406. */
  407. static const struct node_list *
  408. qnetd_algo_ffsplit_select_partition(const struct qnetd_client *client, int client_leaving,
  409. const struct node_list *config_node_list, const struct node_list *membership_node_list,
  410. const struct node_list *quorate_partition_node_list, enum tlv_heuristics client_heuristics)
  411. {
  412. const struct qnetd_client *iter_client;
  413. const struct qnetd_client *best_client;
  414. const struct node_list *best_config_node_list, *best_membership_node_list;
  415. const struct node_list *iter_config_node_list, *iter_membership_node_list;
  416. enum tlv_heuristics iter_heuristics, best_heuristics;
  417. best_client = NULL;
  418. best_config_node_list = best_membership_node_list = NULL;
  419. best_heuristics = TLV_HEURISTICS_UNDEFINED;
  420. /*
  421. * Get highest score
  422. */
  423. TAILQ_FOREACH(iter_client, &client->cluster->client_list, cluster_entries) {
  424. if (iter_client->node_id == client->node_id) {
  425. if (client_leaving) {
  426. continue;
  427. }
  428. iter_config_node_list = config_node_list;
  429. iter_membership_node_list = membership_node_list;
  430. iter_heuristics = client_heuristics;
  431. } else {
  432. iter_config_node_list = &iter_client->configuration_node_list;
  433. iter_membership_node_list = &iter_client->last_membership_node_list;
  434. iter_heuristics = iter_client->last_heuristics;
  435. }
  436. if (qnetd_algo_ffsplit_partition_cmp(iter_client, iter_config_node_list,
  437. iter_membership_node_list, iter_heuristics, best_client, best_config_node_list,
  438. best_membership_node_list, best_heuristics,
  439. quorate_partition_node_list) > 0) {
  440. best_client = iter_client;
  441. best_config_node_list = iter_config_node_list;
  442. best_membership_node_list = iter_membership_node_list;
  443. best_heuristics = iter_heuristics;
  444. }
  445. }
  446. return (best_membership_node_list);
  447. }
  448. /*
  449. * Update state of all nodes to match quorate_partition_node_list
  450. */
  451. static void
  452. qnetd_algo_ffsplit_update_nodes_state(struct qnetd_client *client, int client_leaving,
  453. const struct node_list *quorate_partition_node_list)
  454. {
  455. const struct qnetd_client *iter_client;
  456. struct qnetd_algo_ffsplit_client_data *iter_client_data;
  457. TAILQ_FOREACH(iter_client, &client->cluster->client_list, cluster_entries) {
  458. iter_client_data = (struct qnetd_algo_ffsplit_client_data *)iter_client->algorithm_data;
  459. if (iter_client->node_id == client->node_id && client_leaving) {
  460. iter_client_data->client_state = QNETD_ALGO_FFSPLIT_CLIENT_STATE_WAITING_FOR_CHANGE;
  461. continue;
  462. }
  463. if (quorate_partition_node_list == NULL ||
  464. node_list_find_node_id(quorate_partition_node_list, iter_client->node_id) == NULL) {
  465. iter_client_data->client_state = QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_NACK;
  466. } else {
  467. iter_client_data->client_state = QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_ACK;
  468. }
  469. }
  470. }
  471. /*
  472. * Send vote info. If client_leaving is set, client is ignored. if send_acks
  473. * is set, only ACK votes are sent (nodes in QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_ACK state),
  474. * otherwise only NACK votes are sent (nodes in QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_NACK state)
  475. *
  476. * Returns number of send votes
  477. */
  478. static size_t
  479. qnetd_algo_ffsplit_send_votes(struct qnetd_client *client, int client_leaving,
  480. const struct tlv_ring_id *ring_id, int send_acks)
  481. {
  482. size_t sent_votes;
  483. struct qnetd_client *iter_client;
  484. struct qnetd_algo_ffsplit_client_data *iter_client_data;
  485. const struct tlv_ring_id *ring_id_to_send;
  486. enum tlv_vote vote_to_send;
  487. sent_votes = 0;
  488. TAILQ_FOREACH(iter_client, &client->cluster->client_list, cluster_entries) {
  489. if (iter_client->node_id == client->node_id) {
  490. if (client_leaving) {
  491. continue;
  492. }
  493. ring_id_to_send = ring_id;
  494. } else {
  495. ring_id_to_send = &iter_client->last_ring_id;
  496. }
  497. iter_client_data = (struct qnetd_algo_ffsplit_client_data *)iter_client->algorithm_data;
  498. vote_to_send = TLV_VOTE_UNDEFINED;
  499. if (send_acks) {
  500. if (iter_client_data->client_state == QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_ACK) {
  501. vote_to_send = TLV_VOTE_ACK;
  502. }
  503. } else {
  504. if (iter_client_data->client_state == QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_NACK) {
  505. vote_to_send = TLV_VOTE_NACK;
  506. }
  507. }
  508. if (vote_to_send != TLV_VOTE_UNDEFINED) {
  509. iter_client_data->vote_info_expected_seq_num++;
  510. sent_votes++;
  511. if (qnetd_client_send_vote_info(iter_client,
  512. iter_client_data->vote_info_expected_seq_num, ring_id_to_send,
  513. vote_to_send) == -1) {
  514. client->schedule_disconnect = 1;
  515. }
  516. }
  517. }
  518. return (sent_votes);
  519. }
  520. /*
  521. * Return number of clients in QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_ACK state if sending_acks is
  522. * set or number of nodes in QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_NACK state if sending_acks is
  523. * not set
  524. */
  525. static size_t
  526. qnetd_algo_ffsplit_no_clients_in_sending_state(struct qnetd_client *client, int sending_acks)
  527. {
  528. size_t no_clients;
  529. struct qnetd_client *iter_client;
  530. struct qnetd_algo_ffsplit_client_data *iter_client_data;
  531. no_clients = 0;
  532. TAILQ_FOREACH(iter_client, &client->cluster->client_list, cluster_entries) {
  533. iter_client_data = (struct qnetd_algo_ffsplit_client_data *)iter_client->algorithm_data;
  534. if (sending_acks &&
  535. iter_client_data->client_state == QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_ACK) {
  536. no_clients++;
  537. }
  538. if (!sending_acks &&
  539. iter_client_data->client_state == QNETD_ALGO_FFSPLIT_CLIENT_STATE_SENDING_NACK) {
  540. no_clients++;
  541. }
  542. }
  543. return (no_clients);
  544. }
  545. static enum tlv_reply_error_code
  546. qnetd_algo_ffsplit_do(struct qnetd_client *client, int client_leaving,
  547. const struct tlv_ring_id *ring_id, const struct node_list *config_node_list,
  548. const struct node_list *membership_node_list, enum tlv_heuristics client_heuristics,
  549. enum tlv_vote *result_vote)
  550. {
  551. struct qnetd_algo_ffsplit_cluster_data *cluster_data;
  552. const struct node_list *quorate_partition_node_list;
  553. cluster_data = (struct qnetd_algo_ffsplit_cluster_data *)client->cluster->algorithm_data;
  554. cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_STABLE_MEMBERSHIP;
  555. if (!qnetd_algo_ffsplit_is_membership_stable(client, client_leaving,
  556. ring_id, config_node_list, membership_node_list)) {
  557. /*
  558. * Wait until membership is stable
  559. */
  560. log(LOG_DEBUG, "ffsplit: Membership for cluster %s is not yet stable", client->cluster_name);
  561. *result_vote = TLV_VOTE_WAIT_FOR_REPLY;
  562. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  563. }
  564. log(LOG_DEBUG, "ffsplit: Membership for cluster %s is now stable", client->cluster_name);
  565. quorate_partition_node_list = qnetd_algo_ffsplit_select_partition(client, client_leaving,
  566. config_node_list, membership_node_list, &cluster_data->quorate_partition_node_list,
  567. client_heuristics);
  568. node_list_free(&cluster_data->quorate_partition_node_list);
  569. if (quorate_partition_node_list == NULL) {
  570. log(LOG_DEBUG, "ffsplit: No quorate partition was selected");
  571. } else {
  572. log(LOG_DEBUG, "ffsplit: Quorate partition selected");
  573. log_common_debug_dump_node_list(quorate_partition_node_list);
  574. if (node_list_clone(&cluster_data->quorate_partition_node_list,
  575. quorate_partition_node_list) != 0) {
  576. log(LOG_ERR, "ffsplit: Can't clone quourate partition node list");
  577. return (TLV_REPLY_ERROR_CODE_INTERNAL_ERROR);
  578. }
  579. }
  580. qnetd_algo_ffsplit_update_nodes_state(client, client_leaving, quorate_partition_node_list);
  581. cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_NACKS;
  582. if (qnetd_algo_ffsplit_send_votes(client, client_leaving, ring_id, 0) == 0) {
  583. log(LOG_DEBUG, "ffsplit: No client gets NACK");
  584. /*
  585. * No one gets nack -> send acks
  586. */
  587. cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_ACKS;
  588. if (qnetd_algo_ffsplit_send_votes(client, client_leaving, ring_id, 1) == 0) {
  589. log(LOG_DEBUG, "ffsplit: No client gets ACK");
  590. /*
  591. * No one gets acks -> finished
  592. */
  593. cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_CHANGE;
  594. }
  595. }
  596. *result_vote = TLV_VOTE_NO_CHANGE;
  597. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  598. }
  599. enum tlv_reply_error_code
  600. qnetd_algo_ffsplit_config_node_list_received(struct qnetd_client *client,
  601. uint32_t msg_seq_num, int config_version_set, uint64_t config_version,
  602. const struct node_list *nodes, int initial, enum tlv_vote *result_vote)
  603. {
  604. enum tlv_reply_error_code reply_error_code;
  605. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  606. if (node_list_size(nodes) == 0) {
  607. /*
  608. * Empty node list shouldn't happen
  609. */
  610. log(LOG_ERR, "ffsplit: Received empty config node list for client %s",
  611. client->addr_str);
  612. return (TLV_REPLY_ERROR_CODE_INVALID_CONFIG_NODE_LIST);
  613. }
  614. if (node_list_find_node_id(nodes, client->node_id) == NULL) {
  615. /*
  616. * Current node is not in node list
  617. */
  618. log(LOG_ERR, "ffsplit: Received config node list without client %s",
  619. client->addr_str);
  620. return (TLV_REPLY_ERROR_CODE_INVALID_CONFIG_NODE_LIST);
  621. }
  622. if (initial || node_list_size(&client->last_membership_node_list) == 0) {
  623. /*
  624. * Initial node list -> membership is going to be send by client
  625. */
  626. *result_vote = TLV_VOTE_ASK_LATER;
  627. } else {
  628. reply_error_code = qnetd_algo_ffsplit_do(client, 0, &client->last_ring_id,
  629. nodes, &client->last_membership_node_list, client->last_heuristics,
  630. result_vote);
  631. }
  632. return (reply_error_code);
  633. }
  634. /*
  635. * Called after client sent membership node list.
  636. * All client fields are already set. Nodes is actual node list.
  637. * msg_seq_num is 32-bit number set by client. If client sent config file version,
  638. * config_version_set is set to 1 and config_version contains valid config file version.
  639. * ring_id and quorate are copied from client votequorum callback.
  640. *
  641. * Function has to return result_vote. This can be one of ack/nack, ask_later (client
  642. * should ask later for a vote) or wait_for_reply (client should wait for reply).
  643. *
  644. * Return TLV_REPLY_ERROR_CODE_NO_ERROR on success, different TLV_REPLY_ERROR_CODE_*
  645. * on failure (error is send back to client)
  646. */
  647. enum tlv_reply_error_code
  648. qnetd_algo_ffsplit_membership_node_list_received(struct qnetd_client *client,
  649. uint32_t msg_seq_num, const struct tlv_ring_id *ring_id,
  650. const struct node_list *nodes, enum tlv_heuristics heuristics, enum tlv_vote *result_vote)
  651. {
  652. enum tlv_reply_error_code reply_error_code;
  653. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  654. if (node_list_size(nodes) == 0) {
  655. /*
  656. * Empty node list shouldn't happen
  657. */
  658. log(LOG_ERR, "ffsplit: Received empty membership node list for client %s",
  659. client->addr_str);
  660. return (TLV_REPLY_ERROR_CODE_INVALID_MEMBERSHIP_NODE_LIST);
  661. }
  662. if (node_list_find_node_id(nodes, client->node_id) == NULL) {
  663. /*
  664. * Current node is not in node list
  665. */
  666. log(LOG_ERR, "ffsplit: Received membership node list without client %s",
  667. client->addr_str);
  668. return (TLV_REPLY_ERROR_CODE_INVALID_MEMBERSHIP_NODE_LIST);
  669. }
  670. if (node_list_size(&client->configuration_node_list) == 0) {
  671. /*
  672. * Config node list not received -> it's going to be sent later
  673. */
  674. *result_vote = TLV_VOTE_ASK_LATER;
  675. } else {
  676. reply_error_code = qnetd_algo_ffsplit_do(client, 0, ring_id,
  677. &client->configuration_node_list, nodes, heuristics,
  678. result_vote);
  679. }
  680. return (reply_error_code);
  681. }
  682. enum tlv_reply_error_code
  683. qnetd_algo_ffsplit_quorum_node_list_received(struct qnetd_client *client,
  684. uint32_t msg_seq_num, enum tlv_quorate quorate, const struct node_list *nodes,
  685. enum tlv_vote *result_vote)
  686. {
  687. /*
  688. * Quorum node list is informative -> no change
  689. */
  690. *result_vote = TLV_VOTE_NO_CHANGE;
  691. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  692. }
  693. void
  694. qnetd_algo_ffsplit_client_disconnect(struct qnetd_client *client, int server_going_down)
  695. {
  696. enum tlv_vote result_vote;
  697. struct qnetd_algo_ffsplit_cluster_data *cluster_data;
  698. cluster_data = (struct qnetd_algo_ffsplit_cluster_data *)client->cluster->algorithm_data;
  699. if (!server_going_down) {
  700. (void)qnetd_algo_ffsplit_do(client, 1, &client->last_ring_id,
  701. &client->configuration_node_list, &client->last_membership_node_list,
  702. client->last_heuristics, &result_vote);
  703. }
  704. free(client->algorithm_data);
  705. if (qnetd_cluster_size(client->cluster) == 1) {
  706. /*
  707. * Last client in the cluster
  708. */
  709. node_list_free(&cluster_data->quorate_partition_node_list);
  710. free(client->cluster->algorithm_data);
  711. }
  712. }
  713. enum tlv_reply_error_code
  714. qnetd_algo_ffsplit_ask_for_vote_received(struct qnetd_client *client, uint32_t msg_seq_num,
  715. enum tlv_vote *result_vote)
  716. {
  717. /*
  718. * Ask for vote is not supported in current algorithm
  719. */
  720. return (TLV_REPLY_ERROR_CODE_UNSUPPORTED_DECISION_ALGORITHM_MESSAGE);
  721. }
  722. enum tlv_reply_error_code
  723. qnetd_algo_ffsplit_vote_info_reply_received(struct qnetd_client *client, uint32_t msg_seq_num)
  724. {
  725. struct qnetd_algo_ffsplit_cluster_data *cluster_data;
  726. struct qnetd_algo_ffsplit_client_data *client_data;
  727. cluster_data = (struct qnetd_algo_ffsplit_cluster_data *)client->cluster->algorithm_data;
  728. client_data = (struct qnetd_algo_ffsplit_client_data *)client->algorithm_data;
  729. if (client_data->vote_info_expected_seq_num != msg_seq_num) {
  730. log(LOG_DEBUG, "ffsplit: Received old vote info reply from client %s",
  731. client->addr_str);
  732. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  733. }
  734. client_data->client_state = QNETD_ALGO_FFSPLIT_CLIENT_STATE_WAITING_FOR_CHANGE;
  735. if (cluster_data->cluster_state != QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_NACKS &&
  736. cluster_data->cluster_state != QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_ACKS) {
  737. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  738. }
  739. if (cluster_data->cluster_state == QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_NACKS) {
  740. if (qnetd_algo_ffsplit_no_clients_in_sending_state(client, 0) == 0) {
  741. log(LOG_DEBUG, "ffsplit: All NACK votes sent for cluster %s",
  742. client->cluster_name);
  743. cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_SENDING_ACKS;
  744. if (qnetd_algo_ffsplit_send_votes(client, 0, &client->last_ring_id, 1) == 0) {
  745. log(LOG_DEBUG, "ffsplit: No client gets ACK");
  746. /*
  747. * No one gets acks -> finished
  748. */
  749. cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_CHANGE;
  750. }
  751. }
  752. } else {
  753. if (qnetd_algo_ffsplit_no_clients_in_sending_state(client, 1) == 0) {
  754. log(LOG_DEBUG, "ffsplit: All ACK votes sent for cluster %s",
  755. client->cluster_name);
  756. cluster_data->cluster_state = QNETD_ALGO_FFSPLIT_CLUSTER_STATE_WAITING_FOR_CHANGE;
  757. }
  758. }
  759. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  760. }
  761. enum tlv_reply_error_code
  762. qnetd_algo_ffsplit_heuristics_change_received(struct qnetd_client *client, uint32_t msg_seq_num,
  763. enum tlv_heuristics heuristics, enum tlv_vote *result_vote)
  764. {
  765. enum tlv_reply_error_code reply_error_code;
  766. reply_error_code = TLV_REPLY_ERROR_CODE_NO_ERROR;
  767. if (node_list_size(&client->configuration_node_list) == 0 ||
  768. node_list_size(&client->last_membership_node_list) == 0) {
  769. /*
  770. * Config or membership node list not received -> it's going to be sent later
  771. */
  772. *result_vote = TLV_VOTE_ASK_LATER;
  773. } else {
  774. reply_error_code = qnetd_algo_ffsplit_do(client, 0, &client->last_ring_id,
  775. &client->configuration_node_list, &client->last_membership_node_list,
  776. heuristics, result_vote);
  777. }
  778. return (reply_error_code);
  779. }
  780. enum tlv_reply_error_code
  781. qnetd_algo_ffsplit_timer_callback(struct qnetd_client *client, int *reschedule_timer,
  782. int *send_vote, enum tlv_vote *result_vote)
  783. {
  784. return (TLV_REPLY_ERROR_CODE_NO_ERROR);
  785. }
  786. static struct qnetd_algorithm qnetd_algo_ffsplit = {
  787. .init = qnetd_algo_ffsplit_client_init,
  788. .config_node_list_received = qnetd_algo_ffsplit_config_node_list_received,
  789. .membership_node_list_received = qnetd_algo_ffsplit_membership_node_list_received,
  790. .quorum_node_list_received = qnetd_algo_ffsplit_quorum_node_list_received,
  791. .client_disconnect = qnetd_algo_ffsplit_client_disconnect,
  792. .ask_for_vote_received = qnetd_algo_ffsplit_ask_for_vote_received,
  793. .vote_info_reply_received = qnetd_algo_ffsplit_vote_info_reply_received,
  794. .heuristics_change_received = qnetd_algo_ffsplit_heuristics_change_received,
  795. .timer_callback = qnetd_algo_ffsplit_timer_callback,
  796. };
  797. enum tlv_reply_error_code qnetd_algo_ffsplit_register()
  798. {
  799. return (qnetd_algorithm_register(TLV_DECISION_ALGORITHM_TYPE_FFSPLIT, &qnetd_algo_ffsplit));
  800. }