Skip to content

async loading should happen once (lest inconsistencies abound) #4527

Closed

Description

@erikerikson

This is a Bug Report

Description

Example...

Input to populateObject:

    custom: {
        item9: '${self:custom.item7}',
        item7: '${self:custom.item5}',
        item5: '${self:custom.item3}',
        item3: '${self:custom.item1}',
        item1: '${self:custom.val}',
        item2: '${self:custom.item1}',
        item4: '${self:custom.item3}',
        item6: '${self:custom.item5}',
        item8: '${self:custom.item7}',
        val: '${file(async.load.js):}',
    },
}

code for async.load.js:

let i = 0
module.exports = () => new Promise((resolve) => {
    setTimeout(() => {
        i += 1 // side effect
        resolve(`my-async-value-${i}`)
    }, 200)
})

result:

{
  "custom": {
    "item9": "my-async-value-9",
    "item7": "my-async-value-7",
    "item5": "my-async-value-5",
    "item3": "my-async-value-3",
    "item1": "my-async-value-2",
    "item2": "my-async-value-4",
    "item4": "my-async-value-6",
    "item6": "my-async-value-8",
    "item8": "my-async-value-10",
    "val": "my-async-value-1"
  }
}

expected:

{
  "custom": {
    "item9": "my-async-value-1",
    "item7": "my-async-value-1",
    "item5": "my-async-value-1",
    "item3": "my-async-value-1",
    "item1": "my-async-value-1",
    "item2": "my-async-value-1",
    "item4": "my-async-value-1",
    "item6": "my-async-value-1",
    "item8": "my-async-value-1",
    "val": "my-async-value-1"
  }
}

While this is a trivial and maybe adversarial case, consider the more common and pedestrian case of retrieving values from a database. What happens if the database contents are retrieved multiple times during the resolution of SLS variables and between each retrieval (or even just one of them) the values are changed?

Similar or dependent issues:

Additional Data

  • Serverless Framework Version you're using: master
  • Operating System: osx
  • Stack Trace: n/a
  • Provider Error messages: n/a

Activity

  1. HyperBrain commented on Dec 5, 2017

    @HyperBrain
    Contributor

    This might already be resolved with the variable cache and optimization (fully available in the cache-AWS-requests branch until merged)

  2. erikerikson commented on Dec 7, 2017

    @erikerikson
    ContributorAuthor

    Although #4534 fixes these test cases, it seems not to cover cases similar to:
    ${file('async.load.js'):foo}
    ${file('async.load.js'):bar}

    Since the variable strings will be different.

    To cover such a case, the variable system would have to differentiate between the loaded thing and the reference into that thing. For the above case, foo and bar could be strings but they could also be a string and a function returning a promise in which case, refetching could be appropriate. Worms in a can, there be.

    For each variable type, the meaning of a different variable string could have different implications. For SSM, for example, indexing into parameter should not influence caching but as discussed, doing so with a javascript file should contingently influence caching.

  3. HyperBrain commented on Dec 7, 2017

    @HyperBrain
    Contributor

    As the interpretation of cache keys is dependent on the target technology used (file, SSM, etc.) it could be more appropriate to let the getValueFromXXXX methods return an additional flag, if the pointers should be included into the cache key or not. This would make the system extensible for new, yet unknown, technologies where the behavior is not yet known.

    From an architectural perspective, it could even make sense to abstract each technology including its resolver into classes that offer (1) an getValueFrom(), (2) the matching regex and (3) the pointer interpretation flag, so that there is e.g. an SSMResolver, that provides the flag that it wants to include the pointer, and an FileResolver, that tells you that it does not want to include them.

  4. erikerikson commented on Dec 12, 2017

    @erikerikson
    ContributorAuthor

    Resolved by #4144 and #4534.

    Should we open up a more deeply/extensively ranging issue under which to provide per source caching as we've discussed?

  5. HyperBrain commented on Dec 12, 2017

    @HyperBrain
    Contributor

    @erikerikson That would be helpful and allow us to track the cache key improvements separately. Please open a new task for that.

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions