This is the 18th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Yuan Xiaobai happily shared his understanding of LLB. State and Definition at the regular meeting. Got the consistent praise of Long Fei and Jia Dazhi. To be able to master key data structure usage in a short period of time is really a remarkable thing.

Because most of the time, the reason people write their own code rather than look at it is because of the complexity of the code. I think I’m pretty good, but when I look at someone else’s code, I kind of doubt my confidence. In addition, when I do not understand, I will doubt it again and again. It’s certainly a bad experience.

As a practical matter, reading other people’s code does present challenges. If the code is written to a standard that is pure and easy to read, it’s good code. But it is also a high requirement, requiring a good understanding of design patterns, algorithms, data structures, frameworks and programming paradigms, and so on. The things written in this way are common language among some programmers, and of course it is easier to communicate. But often this ideal state is not easy to achieve, and many times, we have complicated business logic into it, it is even more difficult.

Jia Dazhi to Yuan Xiaobai appreciation, also make some suggestions. That is, can we use the acquired knowledge to sort out the complete examples from the previous first impression of LLB. State, which can not only verify the correctness of our understanding, but also deepen our understanding.

Words are so said, Yuan Xiaobai can not help but heart or a whisper: why don’t you comb ah.

Want to return to think, Yuan Xiaobai or looking for opportunities, seriously look up the source code.

Recursive Marshal idea

Recursive instance understanding

Based on theexamples/buildkit0/buildkit.goYuan Xiaobai sorted out the use cases based on the understanding of examples and the recurrence thought:The figure shows a clear recursive call flow:

  1. Based on the llB. State list built, marshals start recursively from the last node, but all operations in this example are of SourceOp and ExecOp type, so they all have their own Marshal methods.
  2. If there are multiple dependencies, the corresponding recursion is completed in sequence, and steps 3, 9, and 16 in the figure are all SourceOp
  3. Because it is a depth-first algorithm, each dependency will be exhausted and then the next dependency will be processed. Only after all dependencies are processed will the real last step 18 be processed.

Structurally, it looks like an inverted tree.

Llb. State Instance understanding

If all the prepared states were to be converted to Definition, what would the corresponding llb.State call order look like:By combing:

  1. It can be seen that at first, there are independent states, such as S1, S2 and S3, which are the results created through llb.Image. At this time, they do not know the existence of each other and are not related
  2. Step by step, e5 ties everyone together. Since the copy operation copies SRC to DEST, and ExecOp gets the location information from Mount, several execStates are associated
  3. Finally, we perform our final operationls -l /bin

After some combing, Yuan Xiaobai is indeed more confident than before. This also lays a good foundation for the subsequent PoC.

Dig deeper into the Moby Buildkit series # 19-Edge