Difference between convergence and idempotence in Chef

前端 未结 3 1250
北荒
北荒 2020-12-05 05:37

What is the basic difference between convergence and idempotence in Chef?

3条回答
  •  有刺的猬
    2020-12-05 06:05

    @Mark Amery asked for a more satisfying example of the difference between the two so I will endeavor to provide that.

    A step is convergent if, at the successful conclusion of the step, the system has been brought into a known state.

    A step is idempotent if, after multiple executions of the step on a system (whose underlying state has not changed), the result is the same as if the step had been executed once.

    Convergence without idempotence

    A step that is convergent but not idempotent is:

    rm -rf /var/log/myapp
    mkdir -p /var/log/myapp
    

    At the successful conclusion of the step we know for a fact that /var/log/myapp is an empty directory that exists.

    It is not idempotent because it blows away the /var/log/myapp directory each time. Idempotence is desirable because it reduces unnecessary churn on the system. Obviously any applications writing to the /var/log/myapp directory would not be happy with the above step.

    Idempotence without convergence

    A step that is idempotent but not convergent is:

    test "$(ls -A /home/foo 2>/dev/null)" || tempfile -d /home/foo
    

    That script will create a file with a random name in /home/foo only if there are no files in /home/foo. This is idempotent, after the first run the directory will not be empty so future runs will do nothing.

    However, it is not convergent. You could not say this step is putting the system into any kind of known state because the file that gets created will have a random name.

    Convergence is desired because it helps to produce systems that are in identical states and thus be more likely to behave predictably.

    A word of caution

    These terms are like abstractions, they are not exact and they can leak. For example, you could state that an operation isn't idempotent because it uses up CPU cycles. You could state that one idempotent test-and-repair operation that does an expensive test is "less idempotent" than another operation that does a cheap test even though "less idempotent" is not a factual thing.

    You could try and state that a step that installs MySQL version X is not convergent because, when run on different machines, it leaves a different timestamp on the files. Alternatively you could say the step I posted above IS convergent because it leaves the system in the state "/home/foo exists and contains exactly one file".

    This is what happens when math escapes the chalkboard.

提交回复
热议问题