-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_manager.py
97 lines (74 loc) · 3.15 KB
/
resource_manager.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
from abc import ABCMeta, abstractmethod
from yarn_api_client import ResourceManager as YarnResourceManager
from typing import Dict
from threading import Lock
class ResourceManager(metaclass=ABCMeta):
@abstractmethod
# [(node_name, n_containers)]
def nodes(self) -> Dict[str, int]:
pass
@abstractmethod
def next_application_id(self) -> str:
pass
@abstractmethod
def is_application_running(self, application_id: str) -> bool:
pass
@abstractmethod
def is_application_finished(self, application_id: str) -> bool:
pass
class DummyRM(ResourceManager):
def __init__(self, n_nodes=4, n_containers=8, node_pattern="N{}", app_pattern="A{}", apps_running=None,
apps_submitted=0, apps_finished=None):
self.n_nodes = n_nodes
self.n_containers = n_containers
self.node_pattern = node_pattern
self.app_pattern = app_pattern
self.apps_running = {} if apps_running is None else apps_running
self.apps_finished = {} if apps_finished is None else apps_finished
self.apps_submitted = apps_submitted
def nodes(self):
nodes = {}
for i in range(self.n_nodes):
nodes[self.node_pattern.format(i)] = self.n_containers
return nodes
def next_application_id(self):
self.apps_submitted += 1
return self.app_pattern.format(self.apps_submitted)
def is_application_running(self, application_id: str):
return self.apps_running.get(application_id, False)
def is_application_finished(self, application_id: str):
return self.apps_finished.get(application_id, False)
class Yarn(YarnResourceManager, ResourceManager):
def __init__(self, address, port=8088, timeout=30):
super().__init__(address=address, port=port, timeout=timeout)
self.cluster_started_on = self.cluster_information().data['clusterInfo']['startedOn']
self.__next_app_id = self.cluster_metrics().data['clusterMetrics']['appsSubmitted']
self.lock = Lock()
def nodes(self):
nodes = {}
for node in self.cluster_nodes().data['nodes']['node']:
nodes[node['nodeHostName']] = node['availableVirtualCores']
return nodes
def next_application_id(self):
self.__next_app_id += 1
return "application_{}_{:04}".format(self.cluster_started_on, self.__next_app_id)
def is_application_running(self, application_id):
self.lock.acquire()
try:
output = self.cluster_application(application_id).data['app']['state'] == "RUNNING"
except BaseException as e:
print(e)
output = False
self.lock.release()
return output
def is_application_finished(self, application_id):
self.lock.acquire()
try:
app_data = self.cluster_application(application_id).data
output = app_data['app']['state'] == "FINISHED"
#print("Check status of application with ID = {} : {}".format(application_id, app_data['app']['state']))
except BaseException as e:
print(e)
output = False
self.lock.release()
return output