qnetd-algo-ffsplit.c 28 KB

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