qnetd-algo-utils.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /*
  40. * Returns -1 if any node that is supposedly in the same cluster partition
  41. * as us has a different ring_id.
  42. * If this happens it simply means that qnetd does not yet have the full current view
  43. * of the cluster and should wait until all of the ring_ids in this membership list match up
  44. */
  45. int
  46. qnetd_algo_all_ring_ids_match(struct qnetd_client *client, const struct tlv_ring_id *ring_id)
  47. {
  48. struct node_list_entry *node_info;
  49. struct qnetd_client *other_client;
  50. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  51. int in_our_partition = 0;
  52. if (other_client == client) {
  53. continue; /* We've seen our membership list */
  54. }
  55. qnetd_log(LOG_DEBUG, "algo-util: all_ring_ids_match: seen nodeid %d (client %p) ring_id (%d/%ld)", other_client->node_id, other_client, other_client->last_ring_id.node_id, other_client->last_ring_id.seq);
  56. /* Look down our node list and see if this client is known to us */
  57. TAILQ_FOREACH(node_info, &client->last_membership_node_list, entries) {
  58. if (node_info->node_id == other_client->node_id) {
  59. in_our_partition = 1;
  60. }
  61. }
  62. if (in_our_partition == 0) {
  63. /*
  64. * Also try to look from the other side to see if we are
  65. * not in the other node's membership list.
  66. * Because if so it may mean the membership lists are not equal
  67. */
  68. TAILQ_FOREACH(node_info, &other_client->last_membership_node_list, entries) {
  69. if (node_info->node_id == client->node_id) {
  70. in_our_partition = 1;
  71. }
  72. }
  73. }
  74. /*
  75. * If the other nodes on our side of a partition have a different ring ID then
  76. * we need to wait until they have all caught up before making a decision
  77. */
  78. if (in_our_partition && !tlv_ring_id_eq(ring_id, &other_client->last_ring_id)) {
  79. qnetd_log(LOG_DEBUG, "algo-util: nodeid %d in our partition has different ring_id (%d/%ld) to us (%d/%ld)", other_client->node_id, other_client->last_ring_id.node_id, other_client->last_ring_id.seq, ring_id->node_id, ring_id->seq);
  80. return (-1); /* ring IDs don't match */
  81. }
  82. }
  83. return (0);
  84. }
  85. struct qnetd_algo_partition *
  86. qnetd_algo_find_partition(partitions_list_t *partitions_list, const struct tlv_ring_id *ring_id)
  87. {
  88. struct qnetd_algo_partition *cur_partition;
  89. TAILQ_FOREACH(cur_partition, partitions_list, entries) {
  90. if (tlv_ring_id_eq(&cur_partition->ring_id, ring_id)) {
  91. return (cur_partition);
  92. }
  93. }
  94. return (NULL);
  95. }
  96. int
  97. qnetd_algo_create_partitions(struct qnetd_client *client, partitions_list_t *partitions_list, const struct tlv_ring_id *ring_id)
  98. {
  99. struct qnetd_client *other_client;
  100. int num_partitions = 0;
  101. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  102. struct qnetd_algo_partition *partition;
  103. if (other_client->last_ring_id.seq == 0){
  104. continue; /* not initialised yet */
  105. }
  106. partition = qnetd_algo_find_partition(partitions_list, &other_client->last_ring_id);
  107. if (!partition) {
  108. partition = malloc(sizeof(struct qnetd_algo_partition));
  109. if (!partition) {
  110. return (-1);
  111. }
  112. partition->num_nodes = 0;
  113. memcpy(&partition->ring_id, &other_client->last_ring_id, sizeof(*ring_id));
  114. num_partitions++;
  115. TAILQ_INSERT_TAIL(partitions_list, partition, entries);
  116. }
  117. partition->num_nodes++;
  118. }
  119. return (num_partitions);
  120. }
  121. void
  122. qnetd_algo_free_partitions(partitions_list_t *partitions_list)
  123. {
  124. struct qnetd_algo_partition *cur_partition;
  125. struct qnetd_algo_partition *partition_next;
  126. cur_partition = TAILQ_FIRST(partitions_list);
  127. while (cur_partition != NULL) {
  128. partition_next = TAILQ_NEXT(cur_partition, entries);
  129. free(cur_partition);
  130. cur_partition = partition_next;
  131. }
  132. TAILQ_INIT(partitions_list);
  133. }
  134. void
  135. qnetd_algo_dump_partitions(partitions_list_t *partitions_list)
  136. {
  137. struct qnetd_algo_partition *partition;
  138. TAILQ_FOREACH(partition, partitions_list, entries) {
  139. qnetd_log(LOG_DEBUG, "algo-util: partition %d/%ld (%p) has %d nodes",
  140. partition->ring_id.node_id, partition->ring_id.seq, partition, partition->num_nodes);
  141. }
  142. }