Skip to content

Spec Cluster initialisation questions #2848

Closed

Description

@jacobtomlinson

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 Scheduler and Worker class instead. Then call the various template_id = cluster.register_template() from within the Scheduler and Worker init 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 SpecCluster init we could pass/set them within our own init after calling super init. However we would need to tell the SpecCluster init that we will call _start instead.
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 _start which does the bootstrapping and call super(self)._start(). We would need to explicitly set self.scheduler_spec and self.new_spec in our start as we will have had to pass None to the super init for those things. We would also need to change SpecCluster to construct the scheduler object in _start rather 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

  1. mrocklin commented on Jul 16, 2019

    @mrocklin
    Member

    I also agree that moving more (almost all?) of the setup logic to the async _start method 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.

  2. mrocklin commented on Jul 19, 2019

    @mrocklin
    Member

    Is this now resolved with #2850 ?

  3. jacobtomlinson commented on Jul 22, 2019

    @jacobtomlinson
    MemberAuthor

    Yup thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions