Преглед изворни кода

qnetd: sort by node_id when add new client

Signed-off-by: liangxin1300 <XLiang@suse.com>
Reviewed-by: Jan Friesse <jfriesse@redhat.com>

(cherry picked from corosync-qdevice project commit
 9ef67787640b75977b6cbeb174a12fc97d9461e0)
liangxin1300 пре 4 година
родитељ
комит
3eb94907fa
1 измењених фајлова са 29 додато и 1 уклоњено
  1. 29 1
      qdevices/qnetd-cluster-list.c

+ 29 - 1
qdevices/qnetd-cluster-list.c

@@ -61,10 +61,23 @@ qnetd_cluster_list_find_by_name(struct qnetd_cluster_list *list,
 	return (NULL);
 	return (NULL);
 }
 }
 
 
+static int
+client_node_id_cmp(const struct qnetd_client *entry1, const struct qnetd_client *entry2)
+{
+	int res;
+
+	res = 0;
+	if (entry1->node_id < entry2->node_id) res = -1;
+	if (entry1->node_id > entry2->node_id) res = 1;
+
+	return (res);
+}
+
 struct qnetd_cluster *
 struct qnetd_cluster *
 qnetd_cluster_list_add_client(struct qnetd_cluster_list *list, struct qnetd_client *client)
 qnetd_cluster_list_add_client(struct qnetd_cluster_list *list, struct qnetd_client *client)
 {
 {
 	struct qnetd_cluster *cluster;
 	struct qnetd_cluster *cluster;
+	struct qnetd_client *entry;
 
 
 	cluster = qnetd_cluster_list_find_by_name(list, client->cluster_name,
 	cluster = qnetd_cluster_list_find_by_name(list, client->cluster_name,
 	    client->cluster_name_len);
 	    client->cluster_name_len);
@@ -84,7 +97,22 @@ qnetd_cluster_list_add_client(struct qnetd_cluster_list *list, struct qnetd_clie
 		TAILQ_INSERT_TAIL(list, cluster, entries);
 		TAILQ_INSERT_TAIL(list, cluster, entries);
 	}
 	}
 
 
-	TAILQ_INSERT_TAIL(&cluster->client_list, client, cluster_entries);
+	/*
+	 * Sort by nodeid to keep consistent results when listing clients
+	 */
+	entry = TAILQ_FIRST(&cluster->client_list);
+	while (entry != NULL) {
+		if (client_node_id_cmp(entry, client) > 0) {
+			TAILQ_INSERT_BEFORE(entry, client, cluster_entries);
+			break;
+		}
+
+		entry = TAILQ_NEXT(entry, cluster_entries);
+	}
+
+	if (entry == NULL) {
+		TAILQ_INSERT_TAIL(&cluster->client_list, client, cluster_entries);
+	}
 
 
 	return (cluster);
 	return (cluster);
 }
 }