qnetd-algo-utils.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2016 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <sys/types.h>
  35. #include <string.h>
  36. #include "qnetd-log.h"
  37. #include "qnetd-cluster-list.h"
  38. #include "qnetd-algo-utils.h"
  39. #include "utils.h"
  40. /*
  41. * Returns -1 if any node that is supposedly in the same cluster partition
  42. * as us has a different ring_id.
  43. * If this happens it simply means that qnetd does not yet have the full current view
  44. * of the cluster and should wait until all of the ring_ids in this membership list match up
  45. */
  46. int
  47. qnetd_algo_all_ring_ids_match(struct qnetd_client *client, const struct tlv_ring_id *ring_id)
  48. {
  49. struct node_list_entry *node_info;
  50. struct qnetd_client *other_client;
  51. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  52. int in_our_partition = 0;
  53. if (other_client == client) {
  54. continue; /* We've seen our membership list */
  55. }
  56. qnetd_log(LOG_DEBUG, "algo-util: all_ring_ids_match: seen nodeid %d (client %p) ring_id (" UTILS_PRI_RING_ID ")", other_client->node_id, other_client, other_client->last_ring_id.node_id, other_client->last_ring_id.seq);
  57. /* Look down our node list and see if this client is known to us */
  58. TAILQ_FOREACH(node_info, &client->last_membership_node_list, entries) {
  59. if (node_info->node_id == other_client->node_id) {
  60. in_our_partition = 1;
  61. }
  62. }
  63. if (in_our_partition == 0) {
  64. /*
  65. * Also try to look from the other side to see if we are
  66. * not in the other node's membership list.
  67. * Because if so it may mean the membership lists are not equal
  68. */
  69. TAILQ_FOREACH(node_info, &other_client->last_membership_node_list, entries) {
  70. if (node_info->node_id == client->node_id) {
  71. in_our_partition = 1;
  72. }
  73. }
  74. }
  75. /*
  76. * If the other nodes on our side of a partition have a different ring ID then
  77. * we need to wait until they have all caught up before making a decision
  78. */
  79. if (in_our_partition && !tlv_ring_id_eq(ring_id, &other_client->last_ring_id)) {
  80. qnetd_log(LOG_DEBUG, "algo-util: nodeid %d in our partition has different ring_id (" UTILS_PRI_RING_ID ") to us (" UTILS_PRI_RING_ID ")", other_client->node_id, other_client->last_ring_id.node_id, other_client->last_ring_id.seq, ring_id->node_id, ring_id->seq);
  81. return (-1); /* ring IDs don't match */
  82. }
  83. }
  84. return (0);
  85. }
  86. struct qnetd_algo_partition *
  87. qnetd_algo_find_partition(partitions_list_t *partitions_list, const struct tlv_ring_id *ring_id)
  88. {
  89. struct qnetd_algo_partition *cur_partition;
  90. TAILQ_FOREACH(cur_partition, partitions_list, entries) {
  91. if (tlv_ring_id_eq(&cur_partition->ring_id, ring_id)) {
  92. return (cur_partition);
  93. }
  94. }
  95. return (NULL);
  96. }
  97. int
  98. qnetd_algo_create_partitions(struct qnetd_client *client, partitions_list_t *partitions_list, const struct tlv_ring_id *ring_id)
  99. {
  100. struct qnetd_client *other_client;
  101. int num_partitions = 0;
  102. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  103. struct qnetd_algo_partition *partition;
  104. if (other_client->last_ring_id.seq == 0){
  105. continue; /* not initialised yet */
  106. }
  107. partition = qnetd_algo_find_partition(partitions_list, &other_client->last_ring_id);
  108. if (!partition) {
  109. partition = malloc(sizeof(struct qnetd_algo_partition));
  110. if (!partition) {
  111. return (-1);
  112. }
  113. partition->num_nodes = 0;
  114. partition->score = 0;
  115. memcpy(&partition->ring_id, &other_client->last_ring_id, sizeof(*ring_id));
  116. num_partitions++;
  117. TAILQ_INSERT_TAIL(partitions_list, partition, entries);
  118. }
  119. partition->num_nodes++;
  120. /*
  121. * Score is computer similar way as in the ffsplit algorithm
  122. */
  123. partition->score++;
  124. if (other_client->last_heuristics == TLV_HEURISTICS_PASS) {
  125. partition->score++;
  126. } else if (other_client->last_heuristics == TLV_HEURISTICS_FAIL) {
  127. partition->score--;
  128. }
  129. }
  130. return (num_partitions);
  131. }
  132. void
  133. qnetd_algo_free_partitions(partitions_list_t *partitions_list)
  134. {
  135. struct qnetd_algo_partition *cur_partition;
  136. struct qnetd_algo_partition *partition_next;
  137. cur_partition = TAILQ_FIRST(partitions_list);
  138. while (cur_partition != NULL) {
  139. partition_next = TAILQ_NEXT(cur_partition, entries);
  140. free(cur_partition);
  141. cur_partition = partition_next;
  142. }
  143. TAILQ_INIT(partitions_list);
  144. }
  145. void
  146. qnetd_algo_dump_partitions(partitions_list_t *partitions_list)
  147. {
  148. struct qnetd_algo_partition *partition;
  149. TAILQ_FOREACH(partition, partitions_list, entries) {
  150. qnetd_log(LOG_DEBUG, "algo-util: partition (" UTILS_PRI_RING_ID ") (%p) has %d nodes",
  151. partition->ring_id.node_id, partition->ring_id.seq, partition, partition->num_nodes);
  152. }
  153. }