-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcontroller.py
124 lines (104 loc) · 2.91 KB
/
controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'''
Author: Guanyu Gao
Email: guanyugao@gmail.com
Description:
Resource management for controlling the transcoding workers
'''
import sys
import time
import config
import MySQLdb
from sys_info import *
sched_feq = 20
list_name = 'vm.list'
ip = config.mysql_ip
passwd = config.mysql_password
user_name = config.mysql_user_name
db_name = config.mysql_db_name
'''
get the current number of pending transcoding tasks
'''
def get_pending_task():
try:
con = MySQLdb.connect(ip, user_name, passwd, db_name)
cur = con.cursor()
sql_cmd = 'SELECT * FROM task_info WHERE task_ongoing = 1'
cur.execute(sql_cmd)
rows = cur.fetchall()
con.close()
return rows
except Exception, e:
print str(e)
return -1
def db_update_worker_state(host_name, state):
try:
con = MySQLdb.connect(ip, user_name, passwd, db_name)
cur = con.cursor()
sql_cmd = "UPDATE server_info SET state = %d WHERE id = '%s'" \
% (state, host_name)
#print sql_cmd
cur.execute(sql_cmd)
con.commit()
con.close()
return 0
except:
return -1
'''
to estimate the workload of the pending tasks using whatever method
'''
def feature_extraction(task_list):
return len(task_list)
'''
determine the optimal number of instances using whatever method
'''
def policy(workload):
MIN_NUM = 1
MAX_NUM = 100
if workload <= 0:
return MIN_NUM
else:
return MAX_NUM
if __name__ == '__main__':
vm_list = []
f = open(list_name, 'r')
lines = f.readlines()
for line in lines:
line = line.strip('\n')
line = line.replace(" ", "").lower()
vm_list.append(line)
capacity = len(vm_list)
while True:
#get the pending tasks
task_list = get_pending_task()
value = feature_extraction(task_list)
print 'current workload:', value
opt_num = policy(value)
if opt_num > capacity:
opt_num = capacity
print 'number of instances that should be provisioned:', opt_num
up_set = []
up_num = 0
down_set = []
down_num = 0
for vm in vm_list:
ret = db_get_worker_state(vm)
if ret == -1:
print 'vm does not exist'
continue
elif int(ret) == 0:
down_num += 1
down_set.append(vm)
elif int(ret) == 1:
up_num += 1
up_set.append(vm)
if opt_num == up_num:
pass
elif opt_num > up_num:
for i in range(0, opt_num - up_num):
vm = down_set.pop(0)
db_update_worker_state(vm, 1)
elif opt_num < up_num:
for i in range(0, up_num - opt_num):
vm = up_set.pop(0)
db_update_worker_state(vm, 0)
time.sleep(sched_feq)