Skip to content

lxd: Run replicator migration source in the Replicating instance operation - #19580

Open
18Abhinav07 wants to merge 5 commits into
canonical:mainfrom
18Abhinav07:fix/19452-replicator-runs-create-a-separate-top-le
Open

18Abhinav07 wants to merge 5 commits into
canonical:mainfrom
18Abhinav07:fix/19452-replicator-runs-create-a-separate-top-le

Conversation

@18Abhinav07

@18Abhinav07 18Abhinav07 commented Nov 24, 2026 •

Copy link
Copy Markdown

What changed

  • lxd/replicator_run: the local migration source runs inside "Replicating instance". Waits on other members use the run context, and on cancel the member's op is cancelled over a fresh connection. A forward op that starts after a cancel skips its instance. Target sinks are cancelled over a fresh context when the source fails.
  • New test_clustering_replicator_cancel, with instances on two members.

Why

Checklist

…ation

The local migration source was scheduled as a separate top-level "Migrating
instance" operation, so clients showed the instance as migrating and the
transfer progress was missing from the replicator run's operation tree.

Cancelling the run now interrupts the transfer, so wrap the context error in
that case to keep the operations ending as cancelled.

Fixes canonical#19452

Signed-off-by: Abhinav Pangaria <127080854+18Abhinav07@users.noreply.github.com>
…tance operation

Signed-off-by: Abhinav Pangaria <127080854+18Abhinav07@users.noreply.github.com>
kadinsayani

This comment was marked as duplicate.

@kadinsayani kadinsayani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is the right direction. Running the source inside "Replicating instance" and disconnecting it on ctx.Done() also fixes the replicating-phase hang in #19599 for local instances, so it's worth mentioning that issue in the description too.

A few things:

  • The path for an instance on another cluster member (replicator_run.go L1219-L1239) still uses srcMigrateOp.Wait() and destOp.Wait(), which ignore the run's context. Cancelling the run there still leaves the replicator in "Running". I wrote that up on #19599. Fine as a follow-up, but could the description say that this PR only fixes the local-member case?
  • The old "Migrating instance" operation was attached to the instance's entity URL. Has anything relied on it to show the instance as busy, or to keep other requests off it during the transfer? I'd expect the lock that MigrateSend takes to cover that, but it's worth confirming.
  • Neither the PR nor the follow-up has a test for cancelling a run yet. When you add test_clustering_replicator_cancel, please cover an instance on the replicator's member and one on another member.

Comment thread lxd/replicator_run.go Outdated

// Guard against leaving the destination sink operation running if we fail
// before starting the source; disarmed once the source op is scheduled.
// before starting the source; disarmed once the transfer starts.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the guard is now disarmed before the transfer starts, not once it starts (see the comment on L1297). Something like "disarmed just before the source connects" would match the code.

Comment thread lxd/replicator_run.go
return srcMigration.Do(ctx, s, innerOp)
},
}
destOpCancelled = true // source connects via websockets; cancel would interrupt an in-flight transfer

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This disarms the guard before srcMigration.Do() runs. If Do() fails before the source connects (for example, during the 10-second wait for the control connection), the destination sink operation is left to time out on its own. Main has the same window, but now that Do() runs inline it's easy to close. One way is to cancel the sink explicitly on error:

err = srcMigration.Do(ctx, s, op)
if err != nil {
	_ = destOp.Cancel()
	...
}

Cancelling a sink that has already failed is harmless, so this doesn't need to know how far the transfer got.

Comment thread lxd/replicator_run.go
destOpCancelled = true // source is now connected via websockets; cancel would interrupt an in-flight transfer
// Cancelling the run disconnects the migration, so report the cancellation rather than the
// resulting connection error.
if ctx.Err() != nil {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapping with %w keeps errors.Is(err, context.Canceled) true, so the step ends as Cancelled. 👍

One thing to be aware of: with no Cancelled replicator status, the finalize hook still records the run as Failed and raises a ReplicatorRunFailure warning when a user cancels it. Is that intended, or should a cancelled run be treated differently?

The waits for an instance on another cluster member ignored the run's
context, so cancelling the run left it Running. They now use it, and on
cancel the source or sink operation on that member is cancelled over a
fresh connection, because the run's member clients send every request
with the cancelled context.

Forward operations can't be cancelled while pending, so one that starts
after the run was cancelled now skips its instance. If the local source
fails, the destination sink is cancelled instead of left to time out.

Signed-off-by: Abhinav Pangaria <127080854+18Abhinav07@users.noreply.github.com>
@18Abhinav07

Copy link
Copy Markdown
Author

Thanks, all pushed. Remote member: fixed here too. The forward, snapshot and restore waits use the run context, and on cancel the member's op is cancelled over a fresh connection, since the run's member clients carry the cancelled context. Forward ops are MustRun and can't be cancelled while pending, so one that starts after a cancel now skips its instance.
Lock: MigrateSend takes the ActionMigrate operationlock (driver_lxc.go:4800, driver_qemu.go:7453), and "Replicating instance" carries the instance URL (replicator_run.go:927), so the instance still shows as busy.
Test: test_clustering_replicator_cancel puts c1 on node1 and c2 on node2, freezes the target and cancels in the snapshot and transfer stages. On main the transfer case fails because the run ignores the cancel. Nits done. The sink is cancelled when Do() fails, over a fresh context so it also works after a cancel.
Status: I kept Failed since there's no Cancelled status. Should I add one? Known gap: a sink whose create request was in flight at cancel times out on its own after about 10s.

Copilot AI left a comment

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.

🟡 Changes recommended

An active moderate-severity cancellation-path issue remains in lxd/replicator_run.go, where a cancelled wait can leave the target instance-creation operation running.

1 open issue
What changed in this PR

Updates clustered replicator runs so local migration sources execute within the existing "Replicating instance" operation, with improved cancellation propagation across source members and target sinks.

Changes:

  • Runs local migration sources directly on the replicator child operation while preserving transfer progress.
  • Uses context-aware waits and fresh connections or contexts to cancel remote source and target operations.
  • Adds clustering coverage for operation hierarchy, cancellation during snapshot and transfer phases, and successful reruns.
File Description
test/​suites/​clustering.sh Adds operation-tree assertions and an end-to-end multi-member replicator cancellation and recovery test.
test/​includes/​test-groups.sh Registers the new replicator cancellation test in the storage test group.
lxd/​replicator_run.go Runs local migration sources within replicator operations and improves distributed cancellation handling.

Review effort: Lite

Comment thread lxd/replicator_run.go Outdated
destOpCancelled = true

return destOp.Wait()
return destOp.WaitContext(ctx)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d3aaa5a: if the run is cancelled during the final wait, the sink is now cancelled too, in both paths. All 11 replicator suites pass, including clustering_replicator_cancel.

@tugbataluy

Copy link
Copy Markdown
Contributor

Hi @18Abhinav07, thanks for tackling this, CI is failing due to static analysis/ lint check. Can you check the logs and make static analysis green?

@18Abhinav07
18Abhinav07 force-pushed the fix/19452-replicator-runs-create-a-separate-top-le branch from d3aaa5a to 42f067c Compare December 1, 2026 08:33
@18Abhinav07

Copy link
Copy Markdown
Author

Fixed the empty-test-fail lint in the replicator cancel test and folded it into the test commit. Static analysis should pass now.

@tugbataluy

Copy link
Copy Markdown
Contributor

Now static analysis for test suite is failing can you check?

Signed-off-by: Abhinav Pangaria <127080854+18Abhinav07@users.noreply.github.com>
Once the source finished, the deferred cancel of the destination sink was
disabled, so cancelling the run during the final wait left the target's
Creating instance operation running. Cancel the sink in that case.

Signed-off-by: Abhinav Pangaria <127080854+18Abhinav07@users.noreply.github.com>
@18Abhinav07

Copy link
Copy Markdown
Author

Fixed the jq --exit-status lint as well and ran the full static analysis and golangci-lint locally. Both pass now.

@18Abhinav07
18Abhinav07 force-pushed the fix/19452-replicator-runs-create-a-separate-top-le branch from 42f067c to 0230390 Compare December 1, 2026 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Replicator runs create a separate top-level "Migrating instance" operation for each instance

4 participants