2018-02-03 20:40:17 +00:00
|
|
|
#!/usr/bin/env python3
|
2018-02-03 00:36:08 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-02-03 20:40:17 +00:00
|
|
|
# pylint: disable=C0111,C0326,C0103
|
|
|
|
|
2018-02-03 21:25:26 +00:00
|
|
|
"""Tools to sort API servers to find the least awful one."""
|
|
|
|
|
2018-02-03 00:36:08 +00:00
|
|
|
import numpy
|
|
|
|
|
2018-02-03 20:24:36 +00:00
|
|
|
|
2018-02-03 12:46:17 +00:00
|
|
|
class ServerVoteMixin:
|
2018-02-03 21:25:26 +00:00
|
|
|
"""A mixin component for server sorting."""
|
2018-02-03 01:37:55 +00:00
|
|
|
def __init__(self):
|
2018-02-03 21:25:26 +00:00
|
|
|
"""Populate server list and weighting variables."""
|
2018-02-03 01:37:55 +00:00
|
|
|
self.g2master = None
|
|
|
|
self.master_servers = [
|
|
|
|
"g2master-us-east.tclclouds.com",
|
|
|
|
"g2master-us-west.tclclouds.com",
|
|
|
|
"g2master-eu-west.tclclouds.com",
|
|
|
|
"g2master-ap-south.tclclouds.com",
|
|
|
|
"g2master-ap-north.tclclouds.com",
|
|
|
|
"g2master-sa-east.tclclouds.com",
|
|
|
|
]
|
|
|
|
self.master_servers_weights = [3] * len(self.master_servers)
|
|
|
|
self.check_time_sum = 3
|
|
|
|
self.check_time_count = 1
|
|
|
|
|
2018-02-03 00:36:08 +00:00
|
|
|
def get_master_server(self):
|
2018-02-03 21:25:26 +00:00
|
|
|
"""Return weighted choice from server list."""
|
2018-02-03 00:36:08 +00:00
|
|
|
weight_sum = 0
|
|
|
|
for i in self.master_servers_weights:
|
|
|
|
weight_sum += i
|
|
|
|
numpy_weights = []
|
|
|
|
for i in self.master_servers_weights:
|
|
|
|
numpy_weights.append(i/weight_sum)
|
|
|
|
return numpy.random.choice(self.master_servers, p=numpy_weights)
|
|
|
|
|
|
|
|
def master_server_downvote(self):
|
2018-02-03 21:25:26 +00:00
|
|
|
"""Decrease weight of a server."""
|
2018-02-03 00:36:08 +00:00
|
|
|
idx = self.master_servers.index(self.g2master)
|
|
|
|
if self.master_servers_weights[idx] > 1:
|
|
|
|
self.master_servers_weights[idx] -= 1
|
|
|
|
|
|
|
|
def master_server_upvote(self):
|
2018-02-03 21:25:26 +00:00
|
|
|
"""Increase weight of a server."""
|
2018-02-03 00:36:08 +00:00
|
|
|
idx = self.master_servers.index(self.g2master)
|
|
|
|
if self.master_servers_weights[idx] < 10:
|
|
|
|
self.master_servers_weights[idx] += 1
|
|
|
|
|
|
|
|
def check_time_add(self, duration):
|
2018-02-03 21:25:26 +00:00
|
|
|
"""Record connection time."""
|
2018-02-03 00:36:08 +00:00
|
|
|
self.check_time_sum += duration
|
|
|
|
self.check_time_count += 1
|
|
|
|
|
|
|
|
def check_time_avg(self):
|
2018-02-03 21:25:26 +00:00
|
|
|
"""Return average connection time."""
|
2018-02-03 20:24:36 +00:00
|
|
|
return self.check_time_sum / self.check_time_count
|
2018-02-03 00:36:08 +00:00
|
|
|
|
|
|
|
def master_server_vote_on_time(self, last_duration, avg_duration):
|
2018-02-03 21:25:26 +00:00
|
|
|
"""Change weight of a server based on average connection time."""
|
2018-02-03 00:36:08 +00:00
|
|
|
if last_duration < avg_duration - 0.5:
|
|
|
|
self.master_server_upvote()
|
|
|
|
elif last_duration > avg_duration + 0.5:
|
|
|
|
self.master_server_downvote()
|