1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-08 02:57:16 +03:00
systemd/src/test/test-job-type.c
Michal Schmidt e0209d83e7 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
2012-04-25 18:38:27 +02:00

106 lines
4.6 KiB
C

/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "job.h"
#include "unit.h"
#include "service.h"
int main(int argc, char*argv[]) {
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;
}
assert(merged_ab);
printf("%s + %s = %s\n", job_type_to_string(a), job_type_to_string(b), job_type_to_string(ab));
for (c = 0; c < _JOB_TYPE_MAX_MERGING; c++) {
/* 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(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(ab, c) || !job_type_is_mergeable(a, c) || !job_type_is_mergeable(b, c));
bc = b;
if (job_type_merge_and_collapse(&bc, c, u) >= 0) {
/* Verify associativity */
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);
a_bc = a;
assert(job_type_merge_and_collapse(&a_bc, bc, u) == 0);
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(ab_c));
}
}
}
}
}
return 0;
}