-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfarmer.py
165 lines (135 loc) · 4.77 KB
/
farmer.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# farmer.py
# connector to the farms
from pyro_helper import pyro_connect
import threading as th
import time
# farmport = 20099
class farmlist:
def __init__(self):
self.list = []
def generate(self):
farmport = 20099
def addressify(farmaddr,port):
return farmaddr+':'+str(port)
addresses = [addressify(farm[0],farmport) for farm in self.list]
capacities = [farm[1] for farm in self.list]
failures = [0 for i in range(len(capacities))]
return addresses,capacities,failures
def push(self, addr, capa):
self.list.append((addr,capa))
fl = farmlist()
def reload_addr():
global addresses,capacities,failures
g = {'nothing':0}
with open('farmlist.py','r') as f:
farmlist_py = f.read()
exec(farmlist_py,g)
farmlist_base = g['farmlist_base']
fl.list = []
for item in farmlist_base:
fl.push(item[0],item[1])
addresses,capacities,failures = fl.generate()
reload_addr()
class remoteEnv:
def pretty(self,s):
print(('(remoteEnv) {} ').format(self.id)+str(s))
def __init__(self,fp,id): # fp = farm proxy
self.fp = fp
self.id = id
def reset(self):
return self.fp.reset(self.id)
def step(self,actions):
ret = self.fp.step(self.id, actions)
if ret == False:
self.pretty('env not found on farm side, might been released.')
raise Exception('env not found on farm side, might been released.')
return ret
def rel(self):
count = 0
while True: # releasing is important, so
try:
count+=1
self.fp.rel(self.id)
break
except Exception as e:
self.pretty('exception caught on rel()')
self.pretty(e)
time.sleep(3)
if count>5:
self.pretty('failed to rel().')
break
pass
self.fp._pyroRelease()
def __del__(self):
self.rel()
class farmer:
def reload_addr(self):
self.pretty('reloading farm list...')
reload_addr()
def pretty(self,s):
print('(farmer) '+str(s))
def __init__(self):
for idx,address in enumerate(addresses):
fp = pyro_connect(address,'farm')
self.pretty('forced renewing... '+address)
try:
fp.forcerenew(capacities[idx])
self.pretty('fp.forcerenew() success on '+address)
except Exception as e:
self.pretty('fp.forcerenew() failed on '+address)
self.pretty(e)
fp._pyroRelease()
continue
fp._pyroRelease()
# find non-occupied instances from all available farms
def acq_env(self):
ret = False
import random # randomly sample to achieve load averaging
# l = list(enumerate(addresses))
l = list(range(len(addresses)))
random.shuffle(l)
for idx in l:
time.sleep(0.1)
address = addresses[idx]
capacity = capacities[idx]
if failures[idx]>0:
# wait for a few more rounds upon failure,
# to minimize overhead on querying busy instances
failures[idx] -= 1
continue
else:
fp = pyro_connect(address,'farm')
try:
result = fp.acq(capacity)
except Exception as e:
self.pretty('fp.acq() failed on '+address)
self.pretty(e)
fp._pyroRelease()
failures[idx] += 4
continue
else:
if result == False: # no free ei
fp._pyroRelease() # destroy proxy
failures[idx] += 4
continue
else: # result is an id
eid = result
renv = remoteEnv(fp,eid) # build remoteEnv around the proxy
self.pretty('got one on {} id:{}'.format(address,eid))
ret = renv
break
# ret is False if none of the farms has free ei
return ret
# the following is commented out. should not use.
# def renew(self):
# for idx,address in enumerate(addresses):
# fp = pyro_connect(address,'farm')
# try:
# fp.renew(capacities[idx])
# except Exception as e:
# print('(farmer) fp.renew() failed on '+address)
# print(e)
# fp._pyroRelease()
# continue
# print('(farmer) '+address+' renewed.')
# fp._pyroRelease()