Skip to content

Commit

Permalink
CLUSTER SLOT-STATS ORDERBY when stats are the same, compare by slot i…
Browse files Browse the repository at this point in the history
…n ascending order

Test failed in my local:
```
*** [err]: CLUSTER SLOT-STATS ORDERBY LIMIT correct response pagination, where limit is less than number of assigned slots in tests/unit/cluster/slot-stats.tcl
Expected [dict exists 0 0 1 0 2 0 3 0 4 0 16383] (context: type source line 64 file /xxx/tests/unit/cluster/slot-stats.tcl cmd {assert {[dict exists $expected_slots $slot]}} proc ::assert_slot_visibility level 1)
```

It seems that when the stat is equal, that is, when the key-count is equal,
the qsort performance will be different. When the stat is equal, we compare
by slot (in ascending order).

Signed-off-by: Binbin <[email protected]>
  • Loading branch information
enjoy-binbin committed Jun 28, 2024
1 parent 1269532 commit e757c39
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/cluster_slot_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,23 @@ static uint64_t getSlotStat(int slot, int stat_type) {
return slot_stat;
}

/* Compare by stat in ascending order. If stat is the same, compare by slot in ascending order. */
static int slotStatForSortAscCmp(const void *a, const void *b) {
slotStatForSort entry_a = *((slotStatForSort *)a);
slotStatForSort entry_b = *((slotStatForSort *)b);
if (entry_a.stat == entry_b.stat) {
return entry_a.slot - entry_b.slot;
}
return entry_a.stat - entry_b.stat;
}

/* Compare by stat in descending order. If stat is the same, compare by slot in ascending order. */
static int slotStatForSortDescCmp(const void *a, const void *b) {
slotStatForSort entry_a = *((slotStatForSort *)a);
slotStatForSort entry_b = *((slotStatForSort *)b);
if (entry_b.stat == entry_a.stat) {
return entry_a.slot - entry_b.slot;
}
return entry_b.stat - entry_a.stat;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/unit/cluster/slot-stats.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ start_cluster 1 0 {tags {external:skip cluster}} {
set slot_stats_desc_length [llength $slot_stats_desc]
set slot_stats_asc_length [llength $slot_stats_asc]
assert {$limit == $slot_stats_desc_length && $limit == $slot_stats_asc_length}


# The key count of all slots is 0, so we will order by slot in ascending order.
set expected_slots [dict create 0 0 1 0 2 0 3 0 4 0]
assert_slot_visibility $slot_stats_desc $expected_slots
assert_slot_visibility $slot_stats_asc $expected_slots
Expand Down

0 comments on commit e757c39

Please sign in to comment.