Skip to content

Eliminate/Report Hung Promises (#4687), Prepopulate Stage and Region (#4311), Handle Quoted Strings (#4734) - #4713

Merged
HyperBrain merged 6 commits into
serverless:masterfrom
erikerikson:fix-4687-cyclic-variable-dependencies
Feb 20, 2018
Merged

HyperBrain merged 6 commits into
serverless:masterfrom
erikerikson:fix-4687-cyclic-variable-dependencies

Conversation

@erikerikson

@erikerikson erikerikson commented Feb 3, 2018 •

Copy link
Copy Markdown
Contributor

What did you implement:

Closes #4311, #4734, and #4687

For #4311:

  1. Add pre-population of the service and region values on the provider due to their requirement in the use of request. Reject any dependencies on AWS for resolution of these values.
  2. In support, added a deep variable type for internal use only. This allows variable resolution to be paused and resumed. More specifically it allows population to return to the level of the original request for validation (i.e. during pre-population check whether AWS dependencies have been found during recursive variable resolution). This actually has an additional benefit of solving for an obscure class of bugs where the recursive population previously in getDeepValue caused potential deadlocks resulting from circular dependencies.

For #4687:

  1. Add a more sophisticated splitByComma capability (rather than using split(',')). Note that creating a regex to do the splitting while ignoring quoted commas was getting very deep and involved so I chose to use simpler statements and be procedural. Trims white space as well.
  2. Adds sophistication to the overwrite syntax, allowing for whitespace and repetitions (for robustness)
  3. Adds sophistication to the string ref syntax, allowing use for identifying multiple quoted strings in a variable (i.e. full overwrite variables)

For #4687:

  1. Add a "PromiseTracker" class. It provides tracking and reporting of pending variable resolutions during service populations. Use it in the Variables class.
  2. Decompose populateObject and populateProperty into smaller pieces that are easier to understand.
  3. Additionally, remove the immediate recursive resolution of variables, recursing only at the Object or Property entry point (after changes have been applied). This avoids a problem where the resolved value, prior to being replaced in the containing object or value refers to itself and thereby is waiting on its own resolution before it will resolve itself, causing a deadlock.
  4. Return the given value in warnIfNotFound so that it can chain.
  5. Adjust and add tests.

How did you implement it:

How can we verify it:

Tests have been added to cover the new error cases. A good deal of cleanup has been involved as previously many of the tests were checking that the code was as written rather than identifying function behaviors. Not perfectly but have transitioned them to something more sustainable.

Todos:

  • Write tests
  • [N/A] Write documentation
  • Fix linting errors
  • Make sure code coverage hasn't dropped
  • Provide verification config / commands / resources
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES
Is it a breaking change?: NO?

Add a "PromiseTracker" to the Variables class.  It provides tracking and reporting of pending variable resolutions during service populations.
Decompose populateObject and populateProperty into smaller pieces that are easier to understand.
Additionally, remove the immediate recursive resolution of variables, recursing only at the Object or Property entry point (when changes have been made).  This avoids a problem where the resolved value, prior to being replaced in the containing object or value refers to itself and thereby is waiting on its own resolution before it will resolve itself.
Return the given value in warnIfNotFound so that it can chain.
Comment thread lib/classes/Variables.test.js Outdated
describe('#constructor()', () => {
const serverless = new Serverless();

describe.only('Variables', () => {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Embarrassing. I left the only here. I'll clear this up.

expect(serverless.service.resources.bar).to.equal(barValue);
it('should remove problematic attributes bofore calling populateObject with the service',
() => {
const populateObjectStub = sinon.stub(serverless.variables, 'populateObject', (val) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you delegate the unit test into the stub here?

It is cleaner, if you just stub and then check how the stub was called afterwards like this:

const populateObjectStub = sinon.stub(serverless.variables, 'populateObject').resolves();
return expect(serverless.variables.populateService()).to.be.fulfilled
.then(() => {
  expect(populateObjectStub).to.have.been.calledWithExactly(serverless.service);
  const callVar = _.first(populateObjectStub.firstCall.args); 
  expect(callVar.provider.variableSyntax).to.be.undefined;
  ...
});

Otherwise you melt stub execution and result checking into one blob.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code under test, for reference: https://github.com/erikerikson/serverless/blob/c18eb70a28d61317007091ae765585adc7cd37f6/lib/classes/Variables.js#L98
This particular test is validating temporary state that only exists from just prior to the invocation of "populateObject" and only persists until immediately after its resolution. After populateService is settled, the state cannot be validated.

return expect(serverless.variables.populateProperty(property)
.then(newProperty => {
return serverless.variables.populateProperty(property)
.should.become(undefined)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use chai-as-promised, so you can model this with expect instead of introducing the should semantics.

return expect(serverless.variables.populateProperty(property)).to.eventually.be.undefined
.then(() => ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chai-as-promised provides these should semantics to promises as well. this seemed much cleaner.

Is there a reason not to use should here?
Is there a standard to avoid it?

@HyperBrain HyperBrain Feb 4, 2018 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erikerikson I do not have a specific opinion here. I just was curious why you switched to should here :) - noone had that idea before

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Thanks for explaining. I'm happy to go either way. I stumbled into should elsewhere and it has sometimes made tests more legible.

I've actually avoided it on the sister branch to this one where I'm trying to debug the build/tests (#4716). I'd be happy to import that work here but mildly prefer should. Just say the word and I'll do that but otherwise I'll assume it's fine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go for should then :)

add expected rejection message
Calling .then here creates a new promise.  One that is not returned and generating a rejection within that promise can lead to unhandledRejection events being inappropriately raised.
As such, don't declare unneeded variables.
@erikerikson
erikerikson force-pushed the fix-4687-cyclic-variable-dependencies branch from fee8699 to 3606589 Compare February 7, 2018 00:35
serverless#4311
  see prepopulateService - attempts to pre-populate the region and stage settings necessary for making a request to AWS, rejecting and dependencies thereon it runs into during that process
  see the `deep` variable work.  this was a knock-on effect of providing pre-population.  it actually represents an obscure class of bugs where the recursive population previously in getDeepValue caused the caller not to be in charge of population choices (thus fixing for pre-population) but also caused potential deadlocks resulting from getDeepValue creating circular dependencies.

serverless#4734
  see splitByComma - a regex to do the splitting but ignore quoted commas was getting very deep and involved.  Instead, identify quoted string boundaries, then identify commas (including white space around them) and take those commas not contained by quotes as the locations for splitting the given string.  Trim the string as well (removing leading and trailing white space).
  add sophistication to the overwrite syntax, allowing for whitespace and repetitions (for robustness)
  add sophistication to the string ref syntax, allowing for use in identifying multiple quoted strings in a variable (i.e. for overwrites)

#NotCreated
  fix a bug I created earlier in the branch that caused reporting to be less informative (see renderMatches)

separate PromiseTracker
  move this class into its own file for the purpose of increasing testability and offering reuse

minutiae
  filter the properties given to populateVariables so as to avoid attempting resolution on non-variables.  Efficiency and noise reduction change.  Also cleans up the code (e.g. see populateObject and its use)
  cleaning of overwrite as a side effect
  offer variable cleaning as a idiom
  reorder the Variables.js `require`s to be in alphabetical order
@erikerikson erikerikson changed the title Eliminate Deadlocks, Report Hung Promises (#4687) Eliminate/Report Hung Promises (#4687), Prepopulate Stage and Region (#4311), Handle Quoted Strings (#4734) Feb 17, 2018
@erikerikson

Copy link
Copy Markdown
Contributor Author

This should actually be ready for review now. It's not clear to me why this single test that succeeds locally is failing on 4.x:

  1) PromiseTracker reports one settled promise when one has been added:
      AssertionError: expected 0 to equal 1
      + expected - actual
      -0
      +1
      
      at lib\classes\PromiseTracker.test.js:54:34
  From previous event:
      at Context.<anonymous> (lib\classes\PromiseTracker.test.js:51:31)

@erikerikson
erikerikson force-pushed the fix-4687-cyclic-variable-dependencies branch from 42c17f4 to fa58c29 Compare February 17, 2018 02:07
@erikerikson
erikerikson force-pushed the fix-4687-cyclic-variable-dependencies branch from 9a522df to ffc7686 Compare February 17, 2018 02:46
@erikerikson

Copy link
Copy Markdown
Contributor Author

I was able to reproduce the build break by switching to 4.x.
Creating a new promise and explicitly resolving it seems to have solved the problem.
This should be entirely ready for review.

@HyperBrain

Copy link
Copy Markdown
Contributor

@erikerikson Great work. I'll put it into in-review now and have a look soon :)

@HyperBrain HyperBrain left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

@HyperBrain
HyperBrain merged commit 39caddc into serverless:master Feb 20, 2018
@HyperBrain HyperBrain added this to the 1.27 milestone Feb 20, 2018
@laardee

laardee commented Feb 20, 2018

Copy link
Copy Markdown
Contributor

@HyperBrain @erikerikson I just rebased my development version of SLS to master and deployed my project and got an error:

Serverless Error ---------------------------------------

  Invalid variable reference syntax for variable ${file(secrets.self:custom.stage.yml)}. You can only reference env vars, options, & files. You can check our docs for more info.

The original variable is secrets: ${file(secrets.${self:custom.stage}.yml)} which should be valid syntax (https://serverless.com/framework/docs/providers/aws/guide/variables/#recursively-reference-properties).

@HyperBrain

HyperBrain commented Feb 20, 2018 •

Copy link
Copy Markdown
Contributor

@laardee You're right. The original variable should be correct and self:custom.stage should be replaced within the filename. @erikerikson Is there something broken for the file variable resolution in this case?

I think the issue is, that the file name itself must additionally go through a deep resolution in case it partially matches the variable regex. The new solution seems only to respect deep resolutions for the referenced properties.
I did not test this case specifically :(. Sorry.

@erikerikson

erikerikson commented Feb 20, 2018 •

Copy link
Copy Markdown
Contributor Author

Thank you for reporting @lardee. Thank you @HyperBrain for following up and linking another issue.

Looks like this needs to be reverted. I'll add test cases and fix this.

Apologies that cases slipped through.

@erikerikson erikerikson mentioned this pull request Feb 20, 2018
6 tasks done
HyperBrain added a commit that referenced this pull request Feb 24, 2018
HyperBrain added a commit that referenced this pull request Feb 25, 2018
@jmparsons jmparsons mentioned this pull request Jul 13, 2018
@erikerikson
erikerikson deleted the fix-4687-cyclic-variable-dependencies branch August 20, 2019 05:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ssm inclusions fail

4 participants