Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion libpromises/attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,21 @@ ContextConstraint GetContextConstraints(const EvalContext *ctx, const Promise *p
a.expression = NULL;
a.persistent = PromiseGetConstraintAsInt(ctx, "persistence", pp);

{
const char *tp = PromiseGetConstraintAsRval(pp, "timer_policy", RVAL_TYPE_SCALAR);
if (StringEqual(tp, "reset"))
{
a.timer = CONTEXT_STATE_POLICY_RESET;
}
else
{
/* Default to PRESERVE (absolute) for backward compatibility:
* classes: promises historically skip re-evaluation when the
* class is already defined, so the timer was never reset. */
a.timer = CONTEXT_STATE_POLICY_PRESERVE;
}
}

{
const char *context_scope = PromiseGetConstraintAsRval(pp, "scope", RVAL_TYPE_SCALAR);
a.scope = ContextScopeFromString(context_scope);
Expand All @@ -1143,7 +1158,9 @@ ContextConstraint GetContextConstraints(const EvalContext *ctx, const Promise *p

for (int k = 0; CF_CLASSBODY[k].lval != NULL; k++)
{
if (strcmp(cp->lval, "persistence") == 0 || strcmp(cp->lval, "scope") == 0)
if (StringEqual(cp->lval, "persistence") ||
StringEqual(cp->lval, "scope") ||
StringEqual(cp->lval, "timer_policy"))
{
continue;
}
Expand Down
1 change: 1 addition & 0 deletions libpromises/cf3.defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,7 @@ typedef struct
ContextScope scope;
int nconstraints;
int persistent;
PersistentClassPolicy timer;
} ContextConstraint;

/*************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion libpromises/eval_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ void EvalContextHeapPersistentSave(EvalContext *ctx, const char *name, unsigned

// first see if we have an existing record, and if we should bother to update
{
int existing_info_size = ValueSizeDB(dbp, key, strlen(key));
int existing_info_size = ValueSizeDB(dbp, key, strlen(key) + 1);

Copy link
Copy Markdown
Contributor

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).

Copy link
Copy Markdown
Member Author

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/DeleteDB all store and look up keys with strlen(key) + 1 (the terminating NUL is included — see dbm_api.c), and LMDB matches keys by exact byte length. So ValueSizeDB(dbp, key, strlen(key)) searched for a key one byte shorter than the stored one and never matched — the whole existing-record branch in EvalContextHeapPersistentSave was dead code.

Verified by building both ways and running the reset policy twice:

  • without +1: run 2 logs Creating persistent class … policy reset (existing record never found)
  • with +1: run 2 logs Resetting persistent class … (was 120 minutes remaining)

The timer still physically resets either way (the WriteDB is unconditional), but without this fix the absolute/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.

if (existing_info_size > 0)
{
PersistentClassInfo *existing_info = xcalloc(existing_info_size, 1);
Expand Down
1 change: 1 addition & 0 deletions libpromises/mod_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ const ConstraintSyntax CF_CLASSBODY[] =
ConstraintSyntaxNewContext("not", "Evaluate the negation of string expression in normal form", SYNTAX_STATUS_NORMAL),
ConstraintSyntaxNewContextList("select_class", "Select one of the named list of classes to define based on host identity. Default value: random_selection", SYNTAX_STATUS_NORMAL),
ConstraintSyntaxNewContextList("xor", "Combine class sources with XOR", SYNTAX_STATUS_NORMAL),
ConstraintSyntaxNewOption("timer_policy", "absolute,reset", "Whether a persistent class restarts its counter when rediscovered. Default value: absolute", SYNTAX_STATUS_NORMAL),
ConstraintSyntaxNewNull()
};

Expand Down
25 changes: 20 additions & 5 deletions libpromises/promises.c
Original file line number Diff line number Diff line change
Expand Up @@ -705,16 +705,31 @@ Promise *ExpandDeRefPromise(EvalContext *ctx, const Promise *pp, bool *excluded)
pcopy->org_pp = pp->org_pp;

// if this is a class promise, check if it is already set, if so, skip
// Exception: persistent classes with timer_policy => "reset" must not
// be skipped — the promise needs to fire so the timer gets reset.
if (strcmp("classes", PromiseGetPromiseType(pp)) == 0)
{
if (IsDefinedClass(ctx, CanonifyName(pcopy->promiser)))
{
Log(LOG_LEVEL_DEBUG,
"Skipping evaluation of classes promise as class '%s' is already set",
CanonifyName(pcopy->promiser));
const char *tp = PromiseGetConstraintAsRval(pp, "timer_policy", RVAL_TYPE_SCALAR);
int persistence = PromiseGetConstraintAsInt(ctx, "persistence", pp);

*excluded = true;
return pcopy;
if (StringEqual(tp, "reset") && persistence > 0)
{
Log(LOG_LEVEL_DEBUG,
"Class '%s' is already set but timer_policy is reset"
" — allowing promise evaluation to reset persistence timer",
CanonifyName(pcopy->promiser));
}
else
{
Log(LOG_LEVEL_DEBUG,
"Skipping evaluation of classes promise as class '%s' is already set",
CanonifyName(pcopy->promiser));

*excluded = true;
return pcopy;
}
}
}

Expand Down
35 changes: 32 additions & 3 deletions libpromises/verify_classes.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,37 @@ PromiseResult VerifyClassPromise(EvalContext *ctx, const Promise *pp, ARG_UNUSED
return PROMISE_RESULT_FAIL;
}

if (a.context.expression == NULL ||
EvalClassExpression(ctx, a.context.expression, pp))
bool class_expression_true =
(a.context.expression == NULL ||
EvalClassExpression(ctx, a.context.expression, pp));

/* When a persistent class is already defined (loaded from DB),
* EvalClassExpression short-circuits and returns false. If the
* timer_policy is "reset", we still need to reset the persistence
* timer in the DB even though the class is already in the context. */
if (!class_expression_true &&
a.context.persistent > 0 &&
a.context.timer == CONTEXT_STATE_POLICY_RESET &&
IsDefinedClass(ctx, pp->promiser))
{
StringSet *tags = StringSetNew();
StringSetAdd(tags, xstrdup("source=promise"));
for (const Rlist *rp = PromiseGetConstraintAsList(ctx, "meta", pp); rp; rp = rp->next)
{
StringSetAdd(tags, xstrdup(RlistScalarValue(rp)));
}
Log(LOG_LEVEL_VERBOSE,
"C: + Resetting persistent class timer: '%s' (%d minutes)",
pp->promiser, a.context.persistent);
Buffer *buf = StringSetToBuffer(tags, ',');
EvalContextHeapPersistentSave(ctx, pp->promiser, a.context.persistent,
CONTEXT_STATE_POLICY_RESET, BufferData(buf));
BufferDestroy(buf);
StringSetDestroy(tags);
return PROMISE_RESULT_NOOP;
}

if (class_expression_true)
{
if (a.context.expression == NULL)
{
Expand Down Expand Up @@ -131,7 +160,7 @@ PromiseResult VerifyClassPromise(EvalContext *ctx, const Promise *pp, ARG_UNUSED
pp->promiser, a.context.persistent);
Buffer *buf = StringSetToBuffer(tags, ',');
EvalContextHeapPersistentSave(ctx, pp->promiser, a.context.persistent,
CONTEXT_STATE_POLICY_RESET, BufferData(buf));
a.context.timer, BufferData(buf));
BufferDestroy(buf);
}
if (inserted && (comment != NULL))
Expand Down
92 changes: 92 additions & 0 deletions tests/acceptance/02_classes/01_basic/persistent_timer_policy.cf
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";
}
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";
}
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";
}
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";
}
Loading
Loading