qnetd-algo-utils.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /*
  63. * If the other nodes on our side of a partition have a different ring ID then
  64. * we need to wait until they have all caught up before making a decision
  65. */
  66. if (in_our_partition && !tlv_ring_id_eq(ring_id, &other_client->last_ring_id)) {
  67. 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);
  68. return (-1); /* ring IDs don't match */
  69. }
  70. }
  71. return (0);
  72. }
  73. struct qnetd_algo_partition *
  74. qnetd_algo_find_partition(partitions_list_t *partitions_list, const struct tlv_ring_id *ring_id)
  75. {
  76. struct qnetd_algo_partition *cur_partition;
  77. TAILQ_FOREACH(cur_partition, partitions_list, entries) {
  78. if (tlv_ring_id_eq(&cur_partition->ring_id, ring_id)) {
  79. return (cur_partition);
  80. }
  81. }
  82. return (NULL);
  83. }
  84. int
  85. qnetd_algo_create_partitions(struct qnetd_client *client, partitions_list_t *partitions_list, const struct tlv_ring_id *ring_id)
  86. {
  87. struct qnetd_client *other_client;
  88. int num_partitions = 0;
  89. TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
  90. struct qnetd_algo_partition *partition;
  91. if (other_client->last_ring_id.seq == 0){
  92. continue; /* not initialised yet */
  93. }
  94. partition = qnetd_algo_find_partition(partitions_list, &other_client->last_ring_id);
  95. if (!partition) {
  96. partition = malloc(sizeof(struct qnetd_algo_partition));
  97. if (!partition) {
  98. return (-1);
  99. }
  100. partition->num_nodes = 0;
  101. memcpy(&partition->ring_id, &other_client->last_ring_id, sizeof(*ring_id));
  102. num_partitions++;
  103. TAILQ_INSERT_TAIL(partitions_list, partition, entries);
  104. }
  105. partition->num_nodes++;
  106. }
  107. return (num_partitions);
  108. }
  109. void
  110. qnetd_algo_free_partitions(partitions_list_t *partitions_list)
  111. {
  112. struct qnetd_algo_partition *cur_partition;
  113. struct qnetd_algo_partition *partition_next;
  114. cur_partition = TAILQ_FIRST(partitions_list);
  115. while (cur_partition != NULL) {
  116. partition_next = TAILQ_NEXT(cur_partition, entries);
  117. free(cur_partition);
  118. cur_partition = partition_next;
  119. }
  120. TAILQ_INIT(partitions_list);
  121. }
  122. void
  123. qnetd_algo_dump_partitions(partitions_list_t *partitions_list)
  124. {
  125. struct qnetd_algo_partition *partition;
  126. TAILQ_FOREACH(partition, partitions_list, entries) {
  127. qnetd_log(LOG_DEBUG, "algo-util: partition %d/%ld (%p) has %d nodes",
  128. partition->ring_id.node_id, partition->ring_id.seq, partition, partition->num_nodes);
  129. }
  130. }