Skip to content

Commit

Permalink
job: @force parameter for job_cancel_sync()
Browse files Browse the repository at this point in the history
Callers should be able to specify whether they want job_cancel_sync() to
force-cancel the job or not.

In fact, almost all invocations do not care about consistency of the
result and just want the job to terminate as soon as possible, so they
should pass force=true.  The replication block driver is the exception,
specifically the active commit job it runs.

As for job_cancel_sync_all(), all callers want it to force-cancel all
jobs, because that is the point of it: To cancel all remaining jobs as
quickly as possible (generally on process termination).  So make it
invoke job_cancel_sync() with force=true.

This changes some iotest outputs, because quitting qemu while a mirror
job is active will now lead to it being cancelled instead of completed,
which is what we want.  (Cancelling a READY mirror job with force=false
may take an indefinite amount of time, which we do not want when
quitting.  If users want consistent results, they must have all jobs be
done before they quit qemu.)

Buglink: https://gitlab.com/qemu-project/qemu/-/issues/462
Signed-off-by: Hanna Reitz <[email protected]>
Reviewed-by: Eric Blake <[email protected]>
Reviewed-by: Vladimir Sementsov-Ogievskiy <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: Vladimir Sementsov-Ogievskiy <[email protected]>
  • Loading branch information
XanClic authored and Vladimir Sementsov-Ogievskiy committed Oct 7, 2021
1 parent 1d4a43e commit 4cfb3f0
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 50 deletions.
4 changes: 2 additions & 2 deletions block/replication.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static void replication_close(BlockDriverState *bs)
if (s->stage == BLOCK_REPLICATION_FAILOVER) {
commit_job = &s->commit_job->job;
assert(commit_job->aio_context == qemu_get_current_aio_context());
job_cancel_sync(commit_job);
job_cancel_sync(commit_job, false);
}

if (s->mode == REPLICATION_MODE_SECONDARY) {
Expand Down Expand Up @@ -726,7 +726,7 @@ static void replication_stop(ReplicationState *rs, bool failover, Error **errp)
* disk, secondary disk in backup_job_completed().
*/
if (s->backup_job) {
job_cancel_sync(&s->backup_job->job);
job_cancel_sync(&s->backup_job->job, true);
}

if (!failover) {
Expand Down
4 changes: 2 additions & 2 deletions blockdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,7 @@ static void drive_backup_abort(BlkActionState *common)
aio_context = bdrv_get_aio_context(state->bs);
aio_context_acquire(aio_context);

job_cancel_sync(&state->job->job);
job_cancel_sync(&state->job->job, true);

aio_context_release(aio_context);
}
Expand Down Expand Up @@ -1948,7 +1948,7 @@ static void blockdev_backup_abort(BlkActionState *common)
aio_context = bdrv_get_aio_context(state->bs);
aio_context_acquire(aio_context);

job_cancel_sync(&state->job->job);
job_cancel_sync(&state->job->job, true);

aio_context_release(aio_context);
}
Expand Down
10 changes: 5 additions & 5 deletions include/qemu/job.h
Original file line number Diff line number Diff line change
Expand Up @@ -506,18 +506,18 @@ void job_user_cancel(Job *job, bool force, Error **errp);

/**
* Synchronously cancel the @job. The completion callback is called
* before the function returns. The job may actually complete
* instead of canceling itself; the circumstances under which this
* happens depend on the kind of job that is active.
* before the function returns. If @force is false, the job may
* actually complete instead of canceling itself; the circumstances
* under which this happens depend on the kind of job that is active.
*
* Returns the return value from the job if the job actually completed
* during the call, or -ECANCELED if it was canceled.
*
* Callers must hold the AioContext lock of job->aio_context.
*/
int job_cancel_sync(Job *job);
int job_cancel_sync(Job *job, bool force);

/** Synchronously cancels all jobs using job_cancel_sync(). */
/** Synchronously force-cancels all jobs using job_cancel_sync(). */
void job_cancel_sync_all(void);

/**
Expand Down
18 changes: 15 additions & 3 deletions job.c
Original file line number Diff line number Diff line change
Expand Up @@ -981,9 +981,21 @@ static void job_cancel_err(Job *job, Error **errp)
job_cancel(job, false);
}

int job_cancel_sync(Job *job)
/**
* Same as job_cancel_err(), but force-cancel.
*/
static void job_force_cancel_err(Job *job, Error **errp)
{
return job_finish_sync(job, &job_cancel_err, NULL);
job_cancel(job, true);
}

int job_cancel_sync(Job *job, bool force)
{
if (force) {
return job_finish_sync(job, &job_force_cancel_err, NULL);
} else {
return job_finish_sync(job, &job_cancel_err, NULL);
}
}

void job_cancel_sync_all(void)
Expand All @@ -994,7 +1006,7 @@ void job_cancel_sync_all(void)
while ((job = job_next(NULL))) {
aio_context = job->aio_context;
aio_context_acquire(aio_context);
job_cancel_sync(job);
job_cancel_sync(job, true);
aio_context_release(aio_context);
}
}
Expand Down
60 changes: 24 additions & 36 deletions tests/qemu-iotests/109.out

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/qemu-iotests/tests/qsd-jobs.out
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ QMP_VERSION
{"return": {}}
{"return": {}}
{"return": {}}
{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "BLOCK_JOB_COMPLETED", "data": {"device": "job0", "len": 0, "offset": 0, "speed": 0, "type": "commit"}}
{"timestamp": {"seconds": TIMESTAMP, "microseconds": TIMESTAMP}, "event": "BLOCK_JOB_CANCELLED", "data": {"device": "job0", "len": 0, "offset": 0, "speed": 0, "type": "commit"}}

=== Streaming can't get permission on base node ===

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test-blockjob.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ static void cancel_common(CancelJob *s)
ctx = job->job.aio_context;
aio_context_acquire(ctx);

job_cancel_sync(&job->job);
job_cancel_sync(&job->job, true);
if (sts != JOB_STATUS_CREATED && sts != JOB_STATUS_CONCLUDED) {
Job *dummy = &job->job;
job_dismiss(&dummy, &error_abort);
Expand Down

0 comments on commit 4cfb3f0

Please sign in to comment.