1
|
|
|
# -*- coding: utf-8 -*- |
|
|
|
|
2
|
|
|
|
3
|
|
|
import numpy |
4
|
|
|
|
5
|
|
|
class ServerVoteMixin: |
|
|
|
|
6
|
|
|
def __init__(self): |
7
|
|
|
self.g2master = None |
8
|
|
|
self.master_servers = [ |
9
|
|
|
"g2master-us-east.tclclouds.com", |
10
|
|
|
"g2master-us-west.tclclouds.com", |
11
|
|
|
"g2master-eu-west.tclclouds.com", |
12
|
|
|
"g2master-ap-south.tclclouds.com", |
13
|
|
|
"g2master-ap-north.tclclouds.com", |
14
|
|
|
"g2master-sa-east.tclclouds.com", |
15
|
|
|
] |
16
|
|
|
self.master_servers_weights = [3] * len(self.master_servers) |
17
|
|
|
self.check_time_sum = 3 |
18
|
|
|
self.check_time_count = 1 |
19
|
|
|
|
20
|
|
|
def get_master_server(self): |
|
|
|
|
21
|
|
|
weight_sum = 0 |
22
|
|
|
for i in self.master_servers_weights: |
23
|
|
|
weight_sum += i |
24
|
|
|
numpy_weights = [] |
25
|
|
|
for i in self.master_servers_weights: |
26
|
|
|
numpy_weights.append(i/weight_sum) |
27
|
|
|
return numpy.random.choice(self.master_servers, p=numpy_weights) |
28
|
|
|
|
29
|
|
|
def master_server_downvote(self): |
|
|
|
|
30
|
|
|
idx = self.master_servers.index(self.g2master) |
31
|
|
|
if self.master_servers_weights[idx] > 1: |
32
|
|
|
self.master_servers_weights[idx] -= 1 |
33
|
|
|
|
34
|
|
|
def master_server_upvote(self): |
|
|
|
|
35
|
|
|
idx = self.master_servers.index(self.g2master) |
36
|
|
|
if self.master_servers_weights[idx] < 10: |
37
|
|
|
self.master_servers_weights[idx] += 1 |
38
|
|
|
|
39
|
|
|
def check_time_add(self, duration): |
|
|
|
|
40
|
|
|
self.check_time_sum += duration |
41
|
|
|
self.check_time_count += 1 |
42
|
|
|
|
43
|
|
|
def check_time_avg(self): |
|
|
|
|
44
|
|
|
return (self.check_time_sum / self.check_time_count) |
|
|
|
|
45
|
|
|
|
46
|
|
|
def master_server_vote_on_time(self, last_duration, avg_duration): |
|
|
|
|
47
|
|
|
if last_duration < avg_duration - 0.5: |
48
|
|
|
self.master_server_upvote() |
49
|
|
|
elif last_duration > avg_duration + 0.5: |
50
|
|
|
self.master_server_downvote() |
51
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.