From e0209d83e7b30153f43b1a633c955f66eb2c2e4a Mon Sep 17 00:00:00 2001 From: Michal Schmidt Date: Wed, 25 Apr 2012 11:58:27 +0200 Subject: core: add NOP jobs, job type collapsing Two of our current job types are special: JOB_TRY_RESTART, JOB_RELOAD_OR_START. They differ from other job types by being sensitive to the unit active state. They perform some action when the unit is active and some other action otherwise. This raises a question: when exactly should the unit state be checked to make the decision? Currently the unit state is checked when the job becomes runnable. It's more sensible to check the state immediately when the job is added by the user. When the user types "systemctl try-restart foo.service", he really intends to restart the service if it's running right now. If it isn't running right now, the restart is pointless. Consider the example (from Bugzilla[1]): sleep.service takes some time to start. hello.service has After=sleep.service. Both services get started. Two jobs will appear: hello.service/start waiting sleep.service/start running Then someone runs "systemctl try-restart hello.service". Currently the try-restart operation will block and wait for sleep.service/start to complete. The correct result is to complete the try-restart operation immediately with success, because hello.service is not running. The two original jobs must not be disturbed by this. To fix this we introduce two new concepts: - a new job type: JOB_NOP A JOB_NOP job does not do anything to the unit. It does not pull in any dependencies. It is always immediately runnable. When installed to a unit, it sits in a special slot (u->nop_job) where it never conflicts with the installed job (u->job) of a different type. It never merges with jobs of other types, but it can merge into an already installed JOB_NOP job. - "collapsing" of job types When a job of one of the two special types is added, the state of the unit is checked immediately and the job type changes: JOB_TRY_RESTART -> JOB_RESTART or JOB_NOP JOB_RELOAD_OR_START -> JOB_RELOAD or JOB_START Should a job type JOB_RELOAD_OR_START appear later during job merging, it collapses immediately afterwards. Collapsing actually makes some things simpler, because there are now fewer job types that are allowed in the transaction. [1] Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=753586 --- src/test/test-job-type.c | 91 +++++++++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 35 deletions(-) (limited to 'src/test') diff --git a/src/test/test-job-type.c b/src/test/test-job-type.c index ba8b307dd3..a60be24090 100644 --- a/src/test/test-job-type.c +++ b/src/test/test-job-type.c @@ -25,59 +25,80 @@ #include #include "job.h" +#include "unit.h" +#include "service.h" int main(int argc, char*argv[]) { - JobType a, b, c, d, e, f, g; - - for (a = 0; a < _JOB_TYPE_MAX; a++) - for (b = 0; b < _JOB_TYPE_MAX; b++) { - - if (!job_type_is_mergeable(a, b)) - printf("Not mergeable: %s + %s\n", job_type_to_string(a), job_type_to_string(b)); - - for (c = 0; c < _JOB_TYPE_MAX; c++) { + JobType a, b, c, ab, bc, ab_c, bc_a, a_bc; + const ServiceState test_states[] = { SERVICE_DEAD, SERVICE_RUNNING }; + unsigned i; + bool merged_ab; + + /* fake a unit */ + static Service s = { + .meta.load_state = UNIT_LOADED, + .type = UNIT_SERVICE + }; + Unit *u = UNIT(&s); + + for (i = 0; i < ELEMENTSOF(test_states); i++) { + s.state = test_states[i]; + printf("\nWith collapsing for service state %s\n" + "=========================================\n", service_state_to_string(s.state)); + for (a = 0; a < _JOB_TYPE_MAX_MERGING; a++) { + for (b = 0; b < _JOB_TYPE_MAX_MERGING; b++) { + + ab = a; + merged_ab = (job_type_merge_and_collapse(&ab, b, u) >= 0); + + if (!job_type_is_mergeable(a, b)) { + assert(!merged_ab); + printf("Not mergeable: %s + %s\n", job_type_to_string(a), job_type_to_string(b)); + continue; + } - /* Verify transitivity of mergeability - * of job types */ - assert(!job_type_is_mergeable(a, b) || - !job_type_is_mergeable(b, c) || - job_type_is_mergeable(a, c)); + assert(merged_ab); + printf("%s + %s = %s\n", job_type_to_string(a), job_type_to_string(b), job_type_to_string(ab)); - d = a; - if (job_type_merge(&d, b) >= 0) { + for (c = 0; c < _JOB_TYPE_MAX_MERGING; c++) { - printf("%s + %s = %s\n", job_type_to_string(a), job_type_to_string(b), job_type_to_string(d)); + /* Verify transitivity of mergeability of job types */ + assert(!job_type_is_mergeable(a, b) || + !job_type_is_mergeable(b, c) || + job_type_is_mergeable(a, c)); - /* Verify that merged entries can be - * merged with the same entries they - * can be merged with separately */ - assert(!job_type_is_mergeable(a, c) || job_type_is_mergeable(d, c)); - assert(!job_type_is_mergeable(b, c) || job_type_is_mergeable(d, c)); + /* Verify that merged entries can be merged with the same entries + * they can be merged with separately */ + assert(!job_type_is_mergeable(a, c) || job_type_is_mergeable(ab, c)); + assert(!job_type_is_mergeable(b, c) || job_type_is_mergeable(ab, c)); - /* Verify that if a merged - * with b is not mergeable with - * c then either a or b is not - * mergeable with c either. */ - assert(job_type_is_mergeable(d, c) || !job_type_is_mergeable(a, c) || !job_type_is_mergeable(b, c)); + /* Verify that if a merged with b is not mergeable with c, then + * either a or b is not mergeable with c either. */ + assert(job_type_is_mergeable(ab, c) || !job_type_is_mergeable(a, c) || !job_type_is_mergeable(b, c)); - e = b; - if (job_type_merge(&e, c) >= 0) { + bc = b; + if (job_type_merge_and_collapse(&bc, c, u) >= 0) { /* Verify associativity */ - f = d; - assert(job_type_merge(&f, c) == 0); + ab_c = ab; + assert(job_type_merge_and_collapse(&ab_c, c, u) == 0); + + bc_a = bc; + assert(job_type_merge_and_collapse(&bc_a, a, u) == 0); - g = e; - assert(job_type_merge(&g, a) == 0); + a_bc = a; + assert(job_type_merge_and_collapse(&a_bc, bc, u) == 0); - assert(f == g); + assert(ab_c == bc_a); + assert(ab_c == a_bc); - printf("%s + %s + %s = %s\n", job_type_to_string(a), job_type_to_string(b), job_type_to_string(c), job_type_to_string(d)); + printf("%s + %s + %s = %s\n", job_type_to_string(a), job_type_to_string(b), job_type_to_string(c), job_type_to_string(ab_c)); } } } } + } return 0; -- cgit v1.2.3-54-g00ecf