Created
October 24, 2019 14:45
-
-
Save byyam/6fe49cc9861b9134025c8234517f778a to your computer and use it in GitHub Desktop.
WRRS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define server map: {name : weight} | |
server_index = {0:'A', 1:'B', 2:'C'} | |
server_weight = {'A':4, 'B':3, 'C':2} | |
# global varibles | |
# server index is initialized with -1 | |
# current weight is the current weight in scheduling, and current weight is initialized with 0 | |
g_server_index = -1 | |
g_current_weight = 0 | |
# function of getting the max weight of servers | |
def get_weight_max(): | |
max_weight = 0 | |
for k in server_weight: | |
if max_weight < server_weight[k]: | |
max_weight = server_weight[k] | |
print 'max_weight = %s' % (max_weight) | |
return max_weight | |
# function of getting the greatest common divisor of all server weights | |
def get_weight_gcd(): | |
max_weight = get_weight_max() | |
gcd = 1 | |
for i in range(2, max_weight+1): | |
count = 0 | |
for k in server_weight: | |
if server_weight[k] % i != 0: | |
continue | |
count += 1 | |
if count == len(server_weight): | |
gcd = i | |
print 'gcd = %s' % (gcd) | |
return gcd | |
# function of one round | |
def get_server(): | |
global g_server_index | |
global g_current_weight | |
while True: | |
g_server_index = (g_server_index + 1) % len(server_weight) | |
if g_server_index == 0: | |
g_current_weight = g_current_weight - get_weight_gcd() | |
if g_current_weight <= 0: | |
g_current_weight = get_weight_max() | |
if g_current_weight == 0: | |
return 'None' | |
server_name = server_index[g_server_index] | |
weight = server_weight[server_name] | |
if weight >= g_current_weight: | |
return server_name | |
# print 20 rounds | |
for i in range(0, 20): | |
wrrs = get_server() | |
print wrrs | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment