Skip to content

Prepare for coroutines #6116

Closed
Closed

Description

@overlookmotel

Now that we're all ES6 for Sequelize v4, the door is open to be using coroutines. Personally, I think this is a great thing - it'll simplify Sequelize's codebase a lot.

However, I don't think we're quite ready to do that - I'd like to conclude my research on bluebird and CLS, to make absolutely sure that introducing coroutines won't break CLS support, before we take the plunge.

But when we do start using coroutines, it will be a semver major change, due to a change in the way errors are handled.

Error handling

At present some Sequelize async methods throw synchronously if the arguments they are passed are illegal in some way. Once those methods become coroutines, this wouldn't be the case any more - they'll return rejected promises instead.

How it is now:

Model.findAll = function(options) {
    if (!options) throw new Error('You must provide options!');
}

// Model.findAll() throws synchronously
try {
   Model.findAll();
} catch (err) {
   console.log('It throws!');
}

How it will be with coroutines:

Model.findAll = co.wrap( function*(options) {
    if (!options) throw new Error('You must provide options!');
} );

// Model.findAll() returns a rejected promise
Model.findAll().catch( function(err) {
    console.log('It returns a rejected promise');
} );

A proposal to "prepare the way"

I would like to see coroutines in Sequelize as soon as possible, but I don't want to hold up the release of v4 until the CLS issues are resolved.

I propose that we "prepare the way" now by converting all Sequelize methods from throwing synchronously to returning rejected promises.

This isn't hard to do using Promise.method():

Model.findAll = Promise.method( function(options) {
    if (!options) throw new Error('You must provide options!');
} );

// Model.findAll() returns a rejected promise
Model.findAll().catch( function(err) {
    console.log('It returns a rejected promise');
} );

This way:

  1. The whole API switches away from sync throwing in one go.
  2. It can all be done as a single change in Sequelize v4.0.0.
  3. Methods can then be refactored with coroutines as and when we feel like it, without creating any further breaking changes.
  4. We won't have to wait until Sequelize v5 to start using coroutines.

What do you think?

Activity

  1. mickhansen commented on Jun 15, 2016

    @mickhansen
    Contributor

    We're in 4.0.0-pre so it's the time for breaking changes!
    4.0.0 is about cleaning up all the code we want so we can be more productive.

  2. overlookmotel commented on Jun 15, 2016

    @overlookmotel
    ContributorAuthor

    @mickhansen So that's a yes? :)

  3. overlookmotel commented on Jun 15, 2016

    @overlookmotel
    ContributorAuthor

    @felixfbecker Are there any major ES6 changes still in the works which might interfere with this if I start putting together a PR?

  4. mickhansen commented on Jun 15, 2016

    @mickhansen
    Contributor

    That's a we're introducing co routines before 4.0.0 launches.
    See other issues, we'll likely use function *() { in classes and then just a post processor to rebind and wrap.

  5. overlookmotel commented on Jun 15, 2016

    @overlookmotel
    ContributorAuthor

    My opinion is that we're not ready for coroutines quite yet.

    Please see my comment here #5898 (comment) about coroutines being responsible for loss of CLS context in some cases.

    My feeling is that ensuring CLS support is 100% robust is really important because it's used for running queries in the right transactions - so a bug threatens to undermine consistency, which I think is a pretty big deal.

    I'm going for it fast as I can in trying to iron out the CLS/bluebird issues. But it's really complex (see test suite I'm building and issue on CLS), so it may take a while and might not be ready for Sequelize v4 if that's imminent.

    Of course, I don't have the final say in these things! But in my opinion, keeping CLS support bug-free is critical.

  6. overlookmotel commented on Jun 15, 2016

    @overlookmotel
    ContributorAuthor

    By the way, @mickhansen if the post-processor you mention is https://github.com/danielstjules/async-class, that looks like a nice clean way to co-wrap the methods of a class.

  7. felixfbecker commented on Jun 15, 2016

    @felixfbecker
    Contributor

    @overlookmotel No, not really. Only the tests and template strings.

    Personally I'm -1 on coroutines because

    • they can't be defined as methods currently without calling a post processor, which sucks for static analysis, V8 optimization (it's not an optimized static class anymore because you modify the whole class afterwards, so V8 creates hidden classes), etc...
    • generators cannot be optimized at all currently by V8. If you are using generators, the compiler instantly switches to non-optimized mode and there's nothing you can do.
    • I don't think the code is that bad now that we can use arrow functions in promises.

    BUT I'm totally 100% for always either throwing sync or async, but never both. It releases Zalgo. So go ahead with this "always throw async" thing, and maybe we can do the important stuff like cleaner API for ES6 users, connection-independent models etc. first and later use coroutines after 4.0 is released.

  8. mickhansen commented on Jun 16, 2016

    @mickhansen
    Contributor

    @felixfbecker We will need to evaluate the performance of coroutines via our benchmark suite in any case, but having used async/await a lot in professional projects it's something me and @janmeier think would increase contributor productivity greatly.

  9. janmeier commented on Jun 16, 2016

    @janmeier
    Member

    Definitely agree that we should wrap everything in promise.method to be consistent with regards to sync / async errors.

    It does however have the same problems as wrapping in a coroutine does - The class is no longer static so V8 might de-opt it. Of course we could wrap the body of each method instead, but pretty it is not.

  10. felixfbecker commented on Jun 16, 2016

    @felixfbecker
    Contributor

    @janmeier Promise.method() is not needed, simply replace all occurences of throw new Whatever() with return Promise.reject(new Whatever()) (of course only outside of promise handlers). Or start every method with return Promise.try(), as it is already used in the codebase in quite a few places.

  11. janmeier commented on Jun 16, 2016

    @janmeier
    Member

    Regarding CLS, we might also consider if the extra 🐒 patching work is worth it, compared to a switch to domains. Yes I know domains are deprecated, but at least they are officially supported by bluebird.

    I'm not saying its a good solution, but I don't think there are any good solutions

  12. felixfbecker commented on Jun 16, 2016

    @felixfbecker
    Contributor

    We could also pass the contexts explicitely via options. I always prefer explicit over implicit and really dislike CLS in general because it is so magic.

  13. overlookmotel commented on Jun 16, 2016

    @overlookmotel
    ContributorAuthor

    @felixfbecker Contexts are already passed explicitly with options inside Sequelize. The CLS support is for 2 things: (1) so that if the user wants to use CLS in their own code, calling a Sequelize method doesn't foul it up, and (2) transaction can be passed via CLS.

    @janmeier Personally I'm quite invested in CLS so I'm going to do the work of working out the monkey-patching anyway. It's just a question of how long it'll take.

    The replacement for both domains and CLS is under development in node core. Async Wrap is actually already in node, but undocumented, and it probably won't be too long until it's officially released.

    So I don't think now is a particularly good time to switch to domains.

  14. felixfbecker commented on Jun 18, 2016

    @felixfbecker
    Contributor

    @overlookmotel If we need to patch global Bluebird, the recently added Promise.getNewLibraryCopy() might be helpful

  15. overlookmotel commented on Jun 18, 2016

    @overlookmotel
    ContributorAuthor

    @felixfbecker Yes, that's why I made the PR on bluebird to introduce it! 😄

  16. 30 remaining items

  17. overlookmotel commented on Jun 30, 2017

    @overlookmotel
    ContributorAuthor

    At present that would screw up CLS support - native promises don't support CLS. But once async_hooks stabilises, CLS will use that underlying mechanism instead and that particular problem will go away. Give it a couple of months I'd say.

  18. janmeier commented on Jun 30, 2017

    @janmeier
    Member

    Really looking forward to async_hooks - Alone for the fact that we will then have something that's stable, and officially supported by core (as opposed to CLS and domains)

  19. felixfbecker commented on Jun 30, 2017

    @felixfbecker
    Contributor

    We will have to wait until Node 4 is out of LTS anyway.
    Personally I would never use CLS (or domains), the perf impact is just way to high.

  20. overlookmotel commented on Jun 30, 2017

    @overlookmotel
    ContributorAuthor

    @janmeier Yes me too!
    @felixfbecker I imagine CLS will be adapted so it uses the old methods on old versions of Node and async_hooks on recent node versions. The performance of async_hooks is meant to be very good, but I haven't benchmarked it yet.

  21. stale commented on Aug 29, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  22. overlookmotel commented on Aug 29, 2017

    @overlookmotel
    ContributorAuthor

    I think this issue should remain open.

  23. papb commented on Jul 26, 2019

    @papb
    Member

    @overlookmotel What is the status/ideas on this? After two years, anything has changed? New ideas?

  24. added
    type: feature
    type: refactor
    status: awaiting response
    and removed
    status: in discussion
    on Jul 26, 2019
  25. sushantdhiman commented on May 2, 2020

    @sushantdhiman
    Contributor

    Superseded by #11859, Closing this issue

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

    status: awaiting responsetype: featuretype: refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions