You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Detect cyclic dependencies in Serverless Variable system #3711
Given the fact that we're recursively resolving the variables it might be possible that developers create cyclic dependencies when using the variable system.
Serverless should detect those dependencies and throw an error if those are found.
Since it appears the cyclic dependencies are a requirement in some configuration cases, what if instead we broke the variable loading into several phases:
First load the file dependencies
Then load other dependencies...
Finally, load the self references.
This would allow us to use self references inside included configuration files, as the files will have already been loaded and cached by the time the self reference is evaluated, meaning we don't get into an infinite loop between the self reference and the file import.
And since I can see this functionality being incredibly useful in other packages, have you considered breaking out the configuration loading into another package and then just referencing it here?
Thanks @pmuens! And yep, I meant a separate npm package. I'll leave the internal discussions to you all, just thought that specific functionality would be very useful for the community at large 😎
Unfortunately the order is not so simple. Our variable system can have nested variables and variables that depend on other variables which prevents any logical ordering based on type. They really need to be evaluated in the order they are encountered. I think we'll need to find a better way of dealing with cyclical dependencies.
Usually when evaluating a dependency graph each node should stored in a map and then the map is checked to see if the node has already been evaluated. It seems we need to do the same here by storing each promise in a map and reusing the existing promise. This should solve any issues with cyclical dependencies.
Unfortunately the order is not so simple. Our variable system can have nested variables and variables that depend on other variables which prevents any logical ordering based on type.
Right, haven't thought about that case. 🤔
But I'm also in favor to untangle the Variables.js code a little bit and try to write it in a more "functional" way so that it's easier to reason about and extend later on.
Usually when evaluating a dependency graph each node should stored in a map and then the map is checked to see if the node has already been evaluated.
Really like this approach since it feels natural for such a use case.
I've done something similar in the past and found that a topological sort of the graph works nicely for figuring out which dependencies should be resolved first. I'm not sure if the topological sort with the circular dependencies will cause the sorting algorithm to go into a loop as I'm not familiar with the math behind it. If we can get it to spit out the dependencies in the correct order still, we can just cache the results for each key. If we come across a cached key, we can just return the cached value.
Activity
sammarks commentedon May 31, 2017
Since it appears the cyclic dependencies are a requirement in some configuration cases, what if instead we broke the variable loading into several phases:
selfreferences.This would allow us to use
selfreferences inside included configuration files, as the files will have already been loaded and cached by the time theselfreference is evaluated, meaning we don't get into an infinite loop between theselfreference and thefileimport.And since I can see this functionality being incredibly useful in other packages, have you considered breaking out the configuration loading into another package and then just referencing it here?
pmuens commentedon Jun 1, 2017
Thank you very much for the feedback @sammarks 👍
That sounds like a really good plan to me. Love the caching. This would also improve the readability of the code a lot. 👍
Do you mean an
npmpackage? I'm more in favor of keeping this into core. Any thoughts @eahefnawy @brianneisler ?sammarks commentedon Jun 1, 2017
Thanks @pmuens! And yep, I meant a separate
npmpackage. I'll leave the internal discussions to you all, just thought that specific functionality would be very useful for the community at large 😎pmuens commentedon Jun 2, 2017
Thanks for getting back @sammarks 👍
That's a valid argument! We'll do our best to make it as accessible as possible. 👍
brianneisler commentedon Jun 2, 2017
Unfortunately the order is not so simple. Our variable system can have nested variables and variables that depend on other variables which prevents any logical ordering based on type. They really need to be evaluated in the order they are encountered. I think we'll need to find a better way of dealing with cyclical dependencies.
Usually when evaluating a dependency graph each node should stored in a map and then the map is checked to see if the node has already been evaluated. It seems we need to do the same here by storing each promise in a map and reusing the existing promise. This should solve any issues with cyclical dependencies.
pmuens commentedon Jun 3, 2017
Right, haven't thought about that case. 🤔
But I'm also in favor to untangle the
Variables.jscode a little bit and try to write it in a more "functional" way so that it's easier to reason about and extend later on.Really like this approach since it feels natural for such a use case.
sammarks commentedon Jun 6, 2017
I've done something similar in the past and found that a topological sort of the graph works nicely for figuring out which dependencies should be resolved first. I'm not sure if the topological sort with the circular dependencies will cause the sorting algorithm to go into a loop as I'm not familiar with the math behind it. If we can get it to spit out the dependencies in the correct order still, we can just cache the results for each key. If we come across a cached key, we can just return the cached value.