forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
<fix>[storage]: symmetric snapshot group disband on chain delete #4043
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ZStack-Robot
wants to merge
2
commits into
zsv_5.1.0
Choose a base branch
from
sync/tao.gan/fix/ZSV-10538-group-disband-integrity
base: zsv_5.1.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
.../src/main/java/org/zstack/storage/snapshot/group/VolumeSnapshotGroupCascadeExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package org.zstack.storage.snapshot.group; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.zstack.core.cascade.AbstractAsyncCascadeExtension; | ||
| import org.zstack.core.cascade.CascadeAction; | ||
| import org.zstack.core.cascade.CascadeConstant; | ||
| import org.zstack.core.db.DatabaseFacade; | ||
| import org.zstack.core.db.Q; | ||
| import org.zstack.core.db.SQL; | ||
| import org.zstack.header.core.Completion; | ||
| import org.zstack.header.storage.snapshot.group.VolumeSnapshotGroupRefVO; | ||
| import org.zstack.header.storage.snapshot.group.VolumeSnapshotGroupRefVO_; | ||
| import org.zstack.header.storage.snapshot.group.VolumeSnapshotGroupVO; | ||
| import org.zstack.header.storage.snapshot.group.VolumeSnapshotGroupVO_; | ||
| import org.zstack.header.vm.VmDeletionStruct; | ||
| import org.zstack.header.vm.VmInstanceVO; | ||
| import org.zstack.header.vm.additions.VmHostBackupFileVO; | ||
| import org.zstack.header.vm.additions.VmHostBackupFileVO_; | ||
| import org.zstack.header.vm.additions.VmHostFileManager; | ||
| import org.zstack.header.vm.devices.VmInstanceResourceMetadataManager; | ||
| import org.zstack.utils.Utils; | ||
| import org.zstack.utils.logging.CLogger; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Cascade extension keyed on VmInstance for cleaning up VolumeSnapshotGroup VOs | ||
| * when a VM is destroyed. | ||
| * | ||
| * Background: snapshot groups are VM-scoped. When a VM is destroyed, any remaining | ||
| * group VOs (whether complete or incomplete due to partial single-snapshot deletions) | ||
| * become orphaned. Without this cleanup, those rows would survive beyond the VM | ||
| * and pollute downstream queries. | ||
| * | ||
| * On DELETION_CHECK we do NOT block — VM destroy should proceed even with | ||
| * incomplete groups (per product decision); cleanup is automatic. | ||
| */ | ||
| public class VolumeSnapshotGroupCascadeExtension extends AbstractAsyncCascadeExtension { | ||
| private static final CLogger logger = Utils.getLogger(VolumeSnapshotGroupCascadeExtension.class); | ||
|
|
||
| private static final String NAME = VolumeSnapshotGroupVO.class.getSimpleName(); | ||
|
|
||
| @Autowired | ||
| private DatabaseFacade dbf; | ||
| @Autowired | ||
| private VmInstanceResourceMetadataManager vidm; | ||
| @Autowired | ||
| private VmHostFileManager vmHostFileManager; | ||
|
|
||
| @Override | ||
| public void asyncCascade(CascadeAction action, Completion completion) { | ||
| if (action.isActionCode(CascadeConstant.DELETION_CLEANUP_CODE)) { | ||
| handleDeletionCleanup(action, completion); | ||
| } else if (action.isActionCode(CascadeConstant.DELETION_DELETE_CODE, | ||
| CascadeConstant.DELETION_FORCE_DELETE_CODE)) { | ||
| handleDeletion(action, completion); | ||
| } else { | ||
| completion.success(); | ||
| } | ||
| } | ||
|
|
||
| private void handleDeletion(CascadeAction action, Completion completion) { | ||
| if (!VmInstanceVO.class.getSimpleName().equals(action.getParentIssuer())) { | ||
| completion.success(); | ||
| return; | ||
| } | ||
|
|
||
| List<String> vmUuids = vmUuidsFromAction(action); | ||
| if (vmUuids.isEmpty()) { | ||
| completion.success(); | ||
| return; | ||
| } | ||
|
|
||
| List<String> groupUuids = Q.New(VolumeSnapshotGroupVO.class) | ||
| .select(VolumeSnapshotGroupVO_.uuid) | ||
| .in(VolumeSnapshotGroupVO_.vmInstanceUuid, vmUuids) | ||
| .listValues(); | ||
| if (groupUuids.isEmpty()) { | ||
| completion.success(); | ||
| return; | ||
| } | ||
|
|
||
| logger.debug(String.format("VM destroy cascade: force-removing %d snapshot group(s) %s for vm(s) %s " + | ||
| "(includes any incomplete groups from prior single-snapshot deletions)", | ||
| groupUuids.size(), groupUuids, vmUuids)); | ||
|
|
||
| // 1. drop all refs first (FK-like constraint via business logic) | ||
| SQL.New(VolumeSnapshotGroupRefVO.class) | ||
| .in(VolumeSnapshotGroupRefVO_.volumeSnapshotGroupUuid, groupUuids) | ||
| .delete(); | ||
|
|
||
| // 2. clean associated metadata + backup files | ||
| groupUuids.forEach(vidm::deleteArchiveVmInstanceResourceMetadataGroup); | ||
| cleanVmHostBackupFilesForGroup(groupUuids); | ||
|
|
||
| // 3. remove group VOs | ||
| dbf.removeByPrimaryKeys(groupUuids, VolumeSnapshotGroupVO.class); | ||
|
|
||
| completion.success(); | ||
| } | ||
|
|
||
| private void cleanVmHostBackupFilesForGroup(List<String> groupUuids) { | ||
| if (groupUuids.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| List<String> backupUuidList = Q.New(VmHostBackupFileVO.class) | ||
| .in(VmHostBackupFileVO_.resourceUuid, groupUuids) | ||
| .select(VmHostBackupFileVO_.uuid) | ||
| .listValues(); | ||
|
|
||
| backupUuidList.forEach(vmHostFileManager::cleanVmHostBackupFile); | ||
| } | ||
|
|
||
| private void handleDeletionCleanup(CascadeAction action, Completion completion) { | ||
| try { | ||
| dbf.eoCleanup(VolumeSnapshotGroupVO.class); | ||
| } catch (Throwable t) { | ||
| logger.warn("eoCleanup VolumeSnapshotGroupVO failed: " + t.getMessage()); | ||
| } finally { | ||
| completion.success(); | ||
| } | ||
| } | ||
|
|
||
| private List<String> vmUuidsFromAction(CascadeAction action) { | ||
| Object ctx = action.getParentIssuerContext(); | ||
| if (ctx == null) { | ||
| return Collections.emptyList(); | ||
| } | ||
| List<String> uuids = new ArrayList<>(); | ||
| if (ctx instanceof List) { | ||
| for (Object o : (List<?>) ctx) { | ||
| if (o instanceof VmDeletionStruct) { | ||
| uuids.add(((VmDeletionStruct) o).getInventory().getUuid()); | ||
| } | ||
| } | ||
| } | ||
| return uuids; | ||
| } | ||
|
|
||
| @Override | ||
| public List<String> getEdgeNames() { | ||
| return Arrays.asList(VmInstanceVO.class.getSimpleName()); | ||
| } | ||
|
|
||
| @Override | ||
| public String getCascadeResourceName() { | ||
| return NAME; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里会把同组的 Memory ref 永久算成“未删”,导致快照组无法解散。
Line 2162 开始的
remaining统计会把整组里所有snapshotDeleted=false的 ref 都算进去,但这里前面只标记了snapshots本身的 ref。链路删除 root/data 快照时,同组的 memory snapshot 会在后面的cleanUpMemorySnapshots()里被直接硬删 VO/tree,却不会把对应的VolumeSnapshotGroupRefVO.snapshotDeleted置为true。这样只要组里带有 memory ref,这里就会一直得到remaining > 0,最终把该 VM 长期留在“不完整快照组”状态。建议把本次一并删除的 memory snapshot ref 也纳入标记,或者让cleanUpMemorySnapshots()复用同样的 group-ref 清理逻辑。🤖 Prompt for AI Agents