Skip to content

Commit

Permalink
Use used_resources instead of unreserved_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
dtaniwaki committed Feb 12, 2018
1 parent 7df7d5c commit 6e34047
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
11 changes: 11 additions & 0 deletions tests/test_mesosslave.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ def test_match(monkeypatch):
with pytest.raises(UCRSpawnerException) as excinfo:
slave.match(MarathonConstraint('foo', 'UNKNOWN', ''))
assert str(excinfo.value) == 'Unsupported constraint operator: UNKNOWN'

def test_is_available(monkeypatch):
mesos_master_response = json.loads('{"id":"40237088-f509-46a4-a5f1-c8f88ca8e03f-S0","hostname":"mesos-slave","port":5051,"attributes":{"foo":123.0,"bar":"wow"},"pid":"slave(1)@172.24.0.5:5051","registered_time":1518339392.10703,"resources":{"disk":69716.0,"mem":999.0,"gpus":0.0,"cpus":2.0,"ports":"[31000-32000]"},"used_resources":{"disk":0.0,"mem":0.0,"gpus":0.0,"cpus":0.0},"offered_resources":{"disk":0.0,"mem":0.0,"gpus":0.0,"cpus":0.0},"reserved_resources":{},"unreserved_resources":{"disk":69716.0,"mem":999.0,"gpus":0.0,"cpus":2.0,"ports":"[31000-32000]"},"active":true,"version":"1.4.0","capabilities":["MULTI_ROLE","HIERARCHICAL_ROLE","RESERVATION_REFINEMENT"],"reserved_resources_full":{},"unreserved_resources_full":[{"name":"cpus","type":"SCALAR","scalar":{"value":2.0},"role":"*"},{"name":"mem","type":"SCALAR","scalar":{"value":999.0},"role":"*"},{"name":"disk","type":"SCALAR","scalar":{"value":69716.0},"role":"*"},{"name":"ports","type":"RANGES","ranges":{"range":[{"begin":31000,"end":32000}]},"role":"*"}],"used_resources_full":[],"offered_resources_full":[]}')

slave = mesosslave.MesosSlave(mesos_master_response)
assert slave.is_available()

mesos_master_response = json.loads('{"id":"40237088-f509-46a4-a5f1-c8f88ca8e03f-S0","hostname":"mesos-slave","port":5051,"attributes":{"foo":123.0,"bar":"wow"},"pid":"slave(1)@172.24.0.5:5051","registered_time":1518339392.10703,"resources":{"disk":69716.0,"mem":999.0,"gpus":0.0,"cpus":2.0,"ports":"[31000-32000]"},"used_resources":{"disk":0.0,"mem":0.0,"gpus":0.0,"cpus":2.0},"offered_resources":{"disk":0.0,"mem":0.0,"gpus":0.0,"cpus":0.0},"reserved_resources":{},"unreserved_resources":{"disk":69716.0,"mem":999.0,"gpus":0.0,"cpus":2.0,"ports":"[31000-32000]"},"active":true,"version":"1.4.0","capabilities":["MULTI_ROLE","HIERARCHICAL_ROLE","RESERVATION_REFINEMENT"],"reserved_resources_full":{},"unreserved_resources_full":[{"name":"cpus","type":"SCALAR","scalar":{"value":2.0},"role":"*"},{"name":"mem","type":"SCALAR","scalar":{"value":999.0},"role":"*"},{"name":"disk","type":"SCALAR","scalar":{"value":69716.0},"role":"*"},{"name":"ports","type":"RANGES","ranges":{"range":[{"begin":31000,"end":32000}]},"role":"*"}],"used_resources_full":[],"offered_resources_full":[]}')

slave = mesosslave.MesosSlave(mesos_master_response)
assert not slave.is_available()
16 changes: 8 additions & 8 deletions ucrspawner/mesosslave.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ def __init__(self, params):
self.disk = params['resources']['disk']
self.gpus = int(params['resources']['gpus'])

self.available_cpus = params['unreserved_resources']['cpus']
self.available_mem = params['unreserved_resources']['mem']
self.available_disk = params['unreserved_resources']['disk']
self.available_gpus = int(params['unreserved_resources']['gpus'])
self.used_cpus = params['used_resources']['cpus']
self.used_mem = params['used_resources']['mem']
self.used_disk = params['used_resources']['disk']
self.used_gpus = int(params['used_resources']['gpus'])

def is_available(self):
return self.available_cpus > 0 and \
self.available_mem > 0 and \
self.available_disk > 0 and \
(self.gpus == 0 or self.available_gpus > 0)
return self.used_cpus < self.cpus and \
self.used_mem < self.mem and \
self.used_disk < self.disk and \
(self.gpus == 0 or self.used_gpus < self.gpus)

def match(self, constraint):
if constraint.operator not in ('LIKE', 'UNLIKE'):
Expand Down

0 comments on commit 6e34047

Please sign in to comment.