mirror_corosync/qdevices/qnetd-algo-utils.c
Christine Caulfield 2f5ff28c4d qnetd-algo: Refactor common routines
Move several commonly used routines into their own
qnetd-algo-utils.[ch] files and change over to using
the ring_id held in the client structure rather than
managint it ourself.

Signed-off-by: Christine Caulfield <ccaulfie@redhat.com>
2016-06-28 13:58:39 +02:00

157 lines
5.4 KiB
C

/*
* Copyright (c) 2016 Red Hat, Inc.
*
* All rights reserved.
*
* Author: Christine Caulfield (ccaulfie@redhat.com)
*
* This software licensed under BSD license, the text of which follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of the Red Hat, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/types.h>
#include <string.h>
#include <err.h>
#include "qnetd-log.h"
#include "qnetd-cluster-list.h"
#include "qnetd-algo-utils.h"
/*
* Returns -1 if any node that is supposedly in the same cluster partition
* as us has a different ring_id.
* If this happens it simply means that qnetd does not yet have the full current view
* of the cluster and should wait until all of the ring_ids in this membership list match up
*/
int qnetd_algo_all_ring_ids_match(struct qnetd_client *client, const struct tlv_ring_id *ring_id)
{
struct node_list_entry *node_info;
struct qnetd_client *other_client;
TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
int in_our_partition = 0;
if (other_client == client) {
continue; /* We've seen our membership list */
}
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);
/* Look down our node list and see if this client is known to us */
TAILQ_FOREACH(node_info, &client->last_membership_node_list, entries) {
if (node_info->node_id == other_client->node_id) {
in_our_partition = 1;
}
}
/*
* If the other nodes on our side of a partition have a different ring ID then
* we need to wait until they have all caught up before making a decision
*/
if (in_our_partition && !qnetd_algo_rings_eq(ring_id, &other_client->last_ring_id)) {
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);
return -1; /* ring IDs don't match */
}
}
return 0;
}
int qnetd_algo_rings_eq(const struct tlv_ring_id *ring_id1, const struct tlv_ring_id *ring_id2)
{
if (ring_id1->node_id == ring_id2->node_id &&
ring_id1->seq == ring_id2->seq) {
return 1;
}
else {
return 0;
}
}
struct qnetd_algo_partition *qnetd_algo_find_partition(partitions_list_t *partitions_list, const struct tlv_ring_id *ring_id)
{
struct qnetd_algo_partition *cur_partition;
TAILQ_FOREACH(cur_partition, partitions_list, entries) {
if (qnetd_algo_rings_eq(&cur_partition->ring_id, ring_id)) {
return cur_partition;
}
}
return NULL;
}
int qnetd_algo_create_partitions(struct qnetd_client *client, partitions_list_t *partitions_list, const struct tlv_ring_id *ring_id)
{
struct qnetd_client *other_client;
int num_partitions = 0;
TAILQ_FOREACH(other_client, &client->cluster->client_list, cluster_entries) {
struct qnetd_algo_partition *partition;
if (other_client->last_ring_id.seq == 0){
continue; /* not initialised yet */
}
partition = qnetd_algo_find_partition(partitions_list, &other_client->last_ring_id);
if (!partition) {
partition = malloc(sizeof(struct qnetd_algo_partition));
if (!partition) {
return -1;
}
partition->num_nodes = 0;
memcpy(&partition->ring_id, &other_client->last_ring_id, sizeof(*ring_id));
num_partitions++;
TAILQ_INSERT_TAIL(partitions_list, partition, entries);
}
partition->num_nodes++;
}
return num_partitions;
}
void qnetd_algo_free_partitions(partitions_list_t *partitions_list)
{
struct qnetd_algo_partition *cur_partition;
TAILQ_FOREACH(cur_partition, partitions_list, entries) {
TAILQ_REMOVE(partitions_list, cur_partition, entries);
free(cur_partition);
}
}
void qnetd_algo_dump_partitions(partitions_list_t *partitions_list)
{
struct qnetd_algo_partition *partition;
TAILQ_FOREACH(partition, partitions_list, entries) {
qnetd_log(LOG_DEBUG, "algo-util: partition %d/%ld (%p) has %d nodes",
partition->ring_id.node_id, partition->ring_id.seq, partition, partition->num_nodes);
}
}