-
Notifications
You must be signed in to change notification settings - Fork 200
CFE-4681: timer_policy support for classes: promises #6167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nickanderson
wants to merge
2
commits into
cfengine:master
Choose a base branch
from
nickanderson:CFE-4681/timer-policy-classes-promises
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
tests/acceptance/02_classes/01_basic/persistent_timer_policy.cf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| ####################################################### | ||
| # | ||
| # CFE-4681: classes: promises with timer_policy => "absolute" (default) | ||
| # | ||
| # Verify that timer_policy => "absolute" (the default) on a classes: | ||
| # promise preserves the persistence timer across agent runs. | ||
| # | ||
| # This is the counterpart to persistent_timer_policy_reset.cf and | ||
| # directly demonstrates the "promise is not evaluated" behaviour: | ||
| # once the persistent class is loaded from the DB on a subsequent | ||
| # run, the classes: promise is skipped in ExpandDeRefPromise (it is | ||
| # never handed to VerifyClassPromise), so the timer is left untouched. | ||
| # | ||
| # First run: expect "Creating persistent class ... policy preserve" | ||
| # Second run: expect "Skipping evaluation of classes promise ... is | ||
| # already set" and NO "Resetting persistent class". | ||
| # | ||
| # Note: the skip message is logged at LOG_LEVEL_DEBUG, so the second | ||
| # run uses -d. Both sub-agent runs happen in the test bundle so that | ||
| # the check bundle only evaluates assertions -- this avoids a spurious | ||
| # FAIL report during the agent's convergence passes. | ||
| # | ||
| ####################################################### | ||
|
|
||
| body common control | ||
| { | ||
| inputs => { "../../default.sub.cf" }; | ||
| bundlesequence => { default("$(this.promise_filename)") }; | ||
| version => "1.0"; | ||
| } | ||
|
|
||
| bundle agent init | ||
| { | ||
| # Remove the persistent class DB to ensure a clean state. | ||
| files: | ||
| "$(sys.workdir)/state/cf_state.lmdb" | ||
| delete => tidy; | ||
| "$(sys.workdir)/state/cf_state.lmdb-lock" | ||
| delete => tidy; | ||
| "$(sys.workdir)/state/cf_state.lmdb.lock" | ||
| delete => tidy; | ||
| } | ||
|
|
||
| bundle agent test | ||
| { | ||
| meta: | ||
| "description" -> { "CFE-4681" } | ||
| string => "timer_policy => absolute (default) on classes: promises preserves the timer; the promise is skipped (not evaluated) when the class is already defined"; | ||
|
|
||
| commands: | ||
| # First run: define the persistent class with timer_policy => "absolute". | ||
| !first_done:: | ||
| "$(sys.cf_agent) -Kv -f $(this.promise_filename).sub > $(G.testdir)/timer_policy_run1.log 2>&1" | ||
| contain => in_shell, | ||
| classes => always("first_done"); | ||
|
|
||
| # Second run: the class is already loaded from the persistent DB, so with | ||
| # timer_policy => absolute the classes: promise must be skipped (not | ||
| # evaluated) and the timer must not be reset. Run with -d so the | ||
| # DEBUG-level skip message is captured. | ||
| first_done.!second_done:: | ||
| "$(sys.cf_agent) -Kd -f $(this.promise_filename).sub > $(G.testdir)/timer_policy_run2.log 2>&1" | ||
| contain => in_shell, | ||
| classes => always("second_done"); | ||
| } | ||
|
|
||
| bundle agent check | ||
| { | ||
| classes: | ||
| # Run 1 creates the persistent class with the preserve (absolute) policy. | ||
| "create_ok" expression => regline(".*Creating persistent class.*timer_policy_test_class.*policy preserve.*", | ||
| "$(G.testdir)/timer_policy_run1.log"); | ||
| # Run 2 must skip the promise because the class is already defined. | ||
| "skip_ok" expression => regline(".*Skipping evaluation of classes promise as class.*timer_policy_test_class.*is already set.*", | ||
| "$(G.testdir)/timer_policy_run2.log"); | ||
| # Run 2 must NOT reset the timer (no EvalContextHeapPersistentSave call). | ||
| "reset_seen" expression => regline(".*Resetting persistent class.*timer_policy_test_class.*", | ||
| "$(G.testdir)/timer_policy_run2.log"); | ||
| "ok" expression => "create_ok.skip_ok.!reset_seen"; | ||
|
|
||
| reports: | ||
| DEBUG.!create_ok:: | ||
| "FAIL: first run did not log 'Creating persistent class ... policy preserve'"; | ||
| DEBUG.!skip_ok:: | ||
| "FAIL: second run did not skip the classes promise (expected 'Skipping evaluation of classes promise ... is already set')"; | ||
| DEBUG.reset_seen:: | ||
| "FAIL: second run reset the timer despite timer_policy => absolute"; | ||
| ok:: | ||
| "$(this.promise_filename) Pass"; | ||
| !ok:: | ||
| "$(this.promise_filename) FAIL"; | ||
| } |
15 changes: 15 additions & 0 deletions
15
tests/acceptance/02_classes/01_basic/persistent_timer_policy.cf.sub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| body common control | ||
| { | ||
| bundlesequence => { run }; | ||
| } | ||
|
|
||
| bundle agent run | ||
| { | ||
| classes: | ||
| # Define persistent class with timer_policy => "absolute" | ||
| # This stores CONTEXT_STATE_POLICY_PRESERVE in the DB | ||
| "timer_policy_test_class" | ||
| expression => "any", | ||
| persistence => "120", | ||
| timer_policy => "absolute"; | ||
| } |
83 changes: 83 additions & 0 deletions
83
tests/acceptance/02_classes/01_basic/persistent_timer_policy_reset.cf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| ####################################################### | ||
| # | ||
| # CFE-4681: classes: promises with timer_policy => "reset" | ||
| # | ||
| # Verify that timer_policy => "reset" on a classes: promise | ||
| # causes the persistence timer to be reset on subsequent | ||
| # agent runs, even though the class is already defined | ||
| # (loaded from the persistent DB). | ||
| # | ||
| # First run: expect "Creating persistent class" | ||
| # Second run: expect "Resetting persistent class" (not skipped) | ||
| # | ||
| # Both sub-agent runs happen in the test bundle so that the check | ||
| # bundle only evaluates assertions -- this avoids a spurious FAIL | ||
| # report during the agent's convergence passes. | ||
| # | ||
| ####################################################### | ||
|
|
||
| body common control | ||
| { | ||
| inputs => { "../../default.sub.cf" }; | ||
| bundlesequence => { default("$(this.promise_filename)") }; | ||
| version => "1.0"; | ||
| } | ||
|
|
||
| bundle agent init | ||
| { | ||
| # Remove the persistent class DB to ensure a clean state. | ||
| files: | ||
| "$(sys.workdir)/state/cf_state.lmdb" | ||
| delete => tidy; | ||
| "$(sys.workdir)/state/cf_state.lmdb-lock" | ||
| delete => tidy; | ||
| "$(sys.workdir)/state/cf_state.lmdb.lock" | ||
| delete => tidy; | ||
| } | ||
|
|
||
| bundle agent test | ||
| { | ||
| meta: | ||
| "description" -> { "CFE-4681" } | ||
| string => "timer_policy => reset on classes: promises resets the persistence timer on subsequent runs"; | ||
|
|
||
| commands: | ||
| # First run: define the persistent class. | ||
| !first_done:: | ||
| "$(sys.cf_agent) -Kv -f $(this.promise_filename).sub > $(G.testdir)/timer_reset_run1.log 2>&1" | ||
| contain => in_shell, | ||
| classes => always("first_done"); | ||
|
|
||
| # Second run: the class already exists in the DB; timer_policy => reset | ||
| # must cause the promise to be evaluated and the timer to be reset. | ||
| first_done.!second_done:: | ||
| "$(sys.cf_agent) -Kv -f $(this.promise_filename).sub > $(G.testdir)/timer_reset_run2.log 2>&1" | ||
| contain => in_shell, | ||
| classes => always("second_done"); | ||
| } | ||
|
|
||
| bundle agent check | ||
| { | ||
| classes: | ||
| "first_ok" expression => regline(".*Creating persistent class.*timer_reset_test_class.*", | ||
| "$(G.testdir)/timer_reset_run1.log"); | ||
| # Match the EvalContextHeapPersistentSave message specifically (it | ||
| # reports "... timer to N minutes (was M remaining)"). This only | ||
| # appears when the existing DB record is actually found, so it would | ||
| # NOT match the "C: + Resetting persistent class timer: ..." progress | ||
| # line that VerifyClassPromise logs regardless. This distinction is | ||
| # what makes the test catch a broken existing-record lookup. | ||
| "second_ok" expression => regline(".*Resetting persistent class 'timer_reset_test_class' timer to.*", | ||
| "$(G.testdir)/timer_reset_run2.log"); | ||
| "ok" expression => "first_ok.second_ok"; | ||
|
|
||
| reports: | ||
| DEBUG.!first_ok:: | ||
| "FAIL: first run did not log 'Creating persistent class'"; | ||
| DEBUG.!second_ok:: | ||
| "FAIL: second run did not log 'Resetting persistent class' (short-circuit not bypassed)"; | ||
| ok:: | ||
| "$(this.promise_filename) Pass"; | ||
| !ok:: | ||
| "$(this.promise_filename) FAIL"; | ||
| } |
16 changes: 16 additions & 0 deletions
16
tests/acceptance/02_classes/01_basic/persistent_timer_policy_reset.cf.sub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| body common control | ||
| { | ||
| bundlesequence => { run }; | ||
| } | ||
|
|
||
| bundle agent run | ||
| { | ||
| classes: | ||
| # Define persistent class with timer_policy => "reset" | ||
| # On second run, the timer should be reset even though | ||
| # the class is already defined from the persistent DB. | ||
| "timer_reset_test_class" | ||
| expression => "any", | ||
| persistence => "120", | ||
| timer_policy => "reset"; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an unrelated change that should go into a separate commit, isn't it? And it doesn't look correct at the first glance. AFAICT, we don't include the terminating NUL-bytes in LMDB data (but I'd have to double-check).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pulled this into its own commit (627d5e2). Digging in, it turns out to be correct and a prerequisite rather than unrelated.
WriteDB/ReadDB/DeleteDBall store and look up keys withstrlen(key) + 1(the terminating NUL is included — seedbm_api.c), and LMDB matches keys by exact byte length. SoValueSizeDB(dbp, key, strlen(key))searched for a key one byte shorter than the stored one and never matched — the whole existing-record branch inEvalContextHeapPersistentSavewas dead code.Verified by building both ways and running the reset policy twice:
+1: run 2 logsCreating persistent class … policy reset(existing record never found)+1: run 2 logsResetting persistent class … (was 120 minutes remaining)The timer still physically resets either way (the
WriteDBis unconditional), but without this fix theabsolute/preserve early-return (don't overwrite an already-preserved, unexpired timer) is unreachable, and the reset acceptance test can't pass. Happy to move it to a standalone PR instead if you'd prefer.