GNOME Bugzilla – Bug 583398
SRV weight sorting is incorrect
Last modified: 2009-06-01 18:32:46 UTC
Please describe the problem: RFC 2782 says that targets with weight 0 should have a small chance of being selected. They should be put at the beginning of the list that will be used to run the selection algorithm, not so that they will be picked up first in the end of the process. Besides that, the random number should include the computed sum, and g_random_int_range has an open end. So we must add one to the range. And we must pick the target that has a greater OR EQUAL running sum. Steps to reproduce: GList *list; list = g_list_append (NULL, g_srv_target_new ("a", 1, 1, 0)); list = g_list_append (list, g_srv_target_new ("b", 1, 1, 0)); list = g_list_append (list, g_srv_target_new ("c", 1, 1, 0)); list = g_list_append (list, g_srv_target_new ("d", 1, 1, 10)); list = g_list_append (list, g_srv_target_new ("e", 1, 0, 10)); list = g_list_append (list, g_srv_target_new ("f", 1, 0, 20)); list = g_srv_target_list_sort (list); while (list != NULL) { printf ("%s\n", g_srv_target_get_hostname (list->data)); list = list->next; } Actual results: I see a b c before d all the time. Expected results: See d before a b c most of the time, but a before d some times. Does this happen every time? Other information:
Created attachment 135062 [details] [review] git patch that fixes the bug based onto master
Created attachment 135063 [details] a brief test case I've built this including only gsrvtarget.[ch] stripped off the type/object system
danw: This is yours
Created attachment 135512 [details] [review] Fix multiple bugs in g_srv_target_list_sort() OK. In addition to the problems the reporter mentioned, there were a few others, which meant that some allowable sortings couldn't actually be generated. I'm adding a test case too, however, the probabilities in the test case come from just running the existing code and seeing how many copies of each ordering were generated. It *looks* right though...
committed