| 5448 | } |
| 5449 | |
| 5450 | static list *clusterManagerComputeReshardTable(list *sources, int numslots) { |
| 5451 | list *moved = listCreate(); |
| 5452 | int src_count = listLength(sources), i = 0, tot_slots = 0, j; |
| 5453 | clusterManagerNode **sorted = zmalloc(src_count * sizeof(*sorted)); |
| 5454 | listIter li; |
| 5455 | listNode *ln; |
| 5456 | listRewind(sources, &li); |
| 5457 | while ((ln = listNext(&li)) != NULL) { |
| 5458 | clusterManagerNode *node = ln->value; |
| 5459 | tot_slots += node->slots_count; |
| 5460 | sorted[i++] = node; |
| 5461 | } |
| 5462 | qsort(sorted, src_count, sizeof(clusterManagerNode *), |
| 5463 | clusterManagerSlotCountCompareDesc); |
| 5464 | for (i = 0; i < src_count; i++) { |
| 5465 | clusterManagerNode *node = sorted[i]; |
| 5466 | float n = ((float) numslots / tot_slots * node->slots_count); |
| 5467 | if (i == 0) n = ceil(n); |
| 5468 | else n = floor(n); |
| 5469 | int max = (int) n, count = 0; |
| 5470 | for (j = 0; j < CLUSTER_MANAGER_SLOTS; j++) { |
| 5471 | int slot = node->slots[j]; |
| 5472 | if (!slot) continue; |
| 5473 | if (count >= max || (int)listLength(moved) >= numslots) break; |
| 5474 | clusterManagerReshardTableItem *item = zmalloc(sizeof(*item)); |
| 5475 | item->source = node; |
| 5476 | item->slot = j; |
| 5477 | listAddNodeTail(moved, item); |
| 5478 | count++; |
| 5479 | } |
| 5480 | } |
| 5481 | zfree(sorted); |
| 5482 | return moved; |
| 5483 | } |
| 5484 | |
| 5485 | static void clusterManagerShowReshardTable(list *table) { |
| 5486 | listIter li; |
no test coverage detected