Repository navigation
Description
I'm currently working on a cluster manager for AWS ECS called ECSCluster which inherits from SpecCluster. It takes Scheduler and Worker classes to create those resources. In order to create a scheduler or worker I need to query the AWS API ahead of time to do some bootstrapping. Currently I'm doing this in the ECSCluster init function and then passing the AWS IDs through to the SpecCluster as arguments.
Once example bootstrapping job would be registering a template for the docker container with AWS. This only needs doing once and AWS returns the ID of the template for later use by the individual scheduler and worker tasks. Therefore doing it from within the cluster object makes sense.
However I'm now trying to make everything async and I'm running into circular issues. I want to create the template in the init and then pass the ID to the super init. However the creation task is async and the super init does the setup self.sync.
I can think of a few API changes that would resolve this, but would appreciate some guidance:
- Instead of doing the bootstrapping in the init, pass a reference to the cluster object to the
SchedulerandWorkerclass instead. Then call the varioustemplate_id = cluster.register_template()from within theSchedulerandWorkerinit methods. This method would cache the ID for subsequent calls so it would only make an API call one.
class Scheduler(Task):
def __init__(self, cluster, **kwargs):
self.cluster = cluster
self.template_id = None
super().__init__(**kwargs)
def __await__(self):
async def _():
async with self.lock:
await self.start()
return self
return _().__await__()
def start(self):
self.template_id = await self.cluster.get_template()
class ECSCluster(SpecCluster):
def __init__(self, **kwargs):
self.template = None
scheduler = {"cls": Scheduler, "options": {"cluster": self}}
worker = {"cls": Worker, "options": {"cluster": self}}
super().__init__({}, scheduler, worker , **kwargs)
async def get_template(self):
if not self.template:
await self.register_template()
async def register_template(self):
self.template = ...- Instead of passing the scheduler and worker classes to the
SpecClusterinit we could pass/set them within our own init after calling super init. However we would need to tell theSpecClusterinit that we will call_startinstead.
class ECSCluster(SpecCluster):
def __init__(self, **kwargs):
super().__init__(start=False, **kwargs)
self.template = self.sync(self.get_template)
self.scheduler_spec = {"cls": Scheduler, "options": {"template": self.template}}
self.new_spec = {"cls": Worker, "options": {"template": self.template}}
# Copied from `SpecCluster`. We would change this to be `if start and ...` in `SpecCluster`
if not self.asynchronous:
self._loop_runner.start()
self.sync(self._start)
self.sync(self._correct_state)
self.sync(self._wait_for_workers)
async def get_template(self):
if not self.template:
await self.register_template()
async def register_template(self):
self.template = ...- Another option would be to write our own
_startwhich does the bootstrapping and callsuper(self)._start(). We would need to explicitly setself.scheduler_specandself.new_specin our start as we will have had to passNoneto the super init for those things. We would also need to changeSpecClusterto construct the scheduler object in_startrather than__init__.
class ECSCluster(SpecCluster):
def __init__(self, **kwargs):
super().__init__(**kwargs)
async def _start(self):
self.template = await self.get_template()
self.scheduler_spec = {"cls": Scheduler, "options": {"template": self.template}}
self.new_spec = {"cls": Worker, "options": {"template": self.template}}
super()._start()
async def get_template(self):
if not self.template:
await self.register_template()
async def register_template(self):
self.template = ...Reading these back I think the third option (or some variant) makes the most sense. I'm not sure the second suggestion would even work.
I appreciate this is a bit of a sprawling question but I would appreciate some feedback.
cc @mrocklin
Activity
mrocklin commentedon Jul 16, 2019
I also agree that moving more (almost all?) of the setup logic to the async
_startmethod seems to make sense. My guess is that similar issues will arrive in the future and that it will be nice to have access to async functionality more broadly.mrocklin commentedon Jul 19, 2019
Is this now resolved with #2850 ?
jacobtomlinson commentedon Jul 22, 2019
Yup thanks!