qnetd-algo-ffsplit.c 31 KB

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