]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-daemon: Fix sorting of hot keys
authorMartin Schwenke <martin@meltin.net>
Thu, 23 Apr 2020 08:59:47 +0000 (18:59 +1000)
committerAmitay Isaacs <amitay@samba.org>
Fri, 22 May 2020 06:41:45 +0000 (06:41 +0000)
The current code only ever swaps with slot 0.  This will only ever
happen with slots 0 and 1, so probably never sorts.

Replace with qsort().

Signed-off-by: Martin Schwenke <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/server/ctdb_call.c

index f833cac04c8b21084343ba9f91b0bb0c9d7df648..14baa797bd637cfc595ebcfeff2e2b0b5e8fad27 100644 (file)
@@ -820,6 +820,21 @@ ctdb_defer_pinned_down_request(struct ctdb_context *ctdb, struct ctdb_db_context
        return 0;
 }
 
+static int hot_key_cmp(const void *a, const void *b)
+{
+       const struct ctdb_db_hot_key *ka = (const struct ctdb_db_hot_key *)a;
+       const struct ctdb_db_hot_key *kb = (const struct ctdb_db_hot_key *)b;
+
+       if (ka->count < kb->count) {
+               return -1;
+       }
+       if (ka->count > kb->count) {
+               return 1;
+       }
+
+       return 0;
+}
+
 static void
 ctdb_update_db_stat_hot_keys(struct ctdb_db_context *ctdb_db, TDB_DATA key,
                             unsigned int count)
@@ -890,20 +905,10 @@ ctdb_update_db_stat_hot_keys(struct ctdb_db_context *ctdb_db, TDB_DATA key,
        ctdb_db->hot_keys[id].last_logged_count = count;
 
 sort_keys:
-       for (i = 1; i < MAX_HOT_KEYS; i++) {
-               if (ctdb_db->hot_keys[i].count == 0) {
-                       continue;
-               }
-               if (ctdb_db->hot_keys[i].count < ctdb_db->hot_keys[0].count) {
-                       count = ctdb_db->hot_keys[i].count;
-                       ctdb_db->hot_keys[i].count = ctdb_db->hot_keys[0].count;
-                       ctdb_db->hot_keys[0].count = count;
-
-                       key = ctdb_db->hot_keys[i].key;
-                       ctdb_db->hot_keys[i].key = ctdb_db->hot_keys[0].key;
-                       ctdb_db->hot_keys[0].key = key;
-               }
-       }
+       qsort(&ctdb_db->hot_keys[0],
+             ctdb_db->statistics.num_hot_keys,
+             sizeof(struct ctdb_db_hot_key),
+             hot_key_cmp);
 }
 
 /*