diff --git a/storage/src/main/java/org/zstack/storage/volume/VolumeBase.java b/storage/src/main/java/org/zstack/storage/volume/VolumeBase.java index ff345892fc3..8e06d5c7609 100755 --- a/storage/src/main/java/org/zstack/storage/volume/VolumeBase.java +++ b/storage/src/main/java/org/zstack/storage/volume/VolumeBase.java @@ -3408,7 +3408,16 @@ public void run(FlowTrigger trigger, Map data) { VolumeEncryptionConversionContext ctx = conversionContext.get(); updateVolumeEncryptionConversionInDb(targetEncrypted, ctx.snapshots, ctx.oldAndNewInstallPaths, (Map) data.get("actualSizes")); - refreshVO(); + try { + refreshVO(); + } catch (RuntimeException e) { + logger.warn(String.format("failed to refresh volume[uuid:%s] after encryption conversion DB update: %s", + self.getUuid(), e.getMessage()), e); + VolumeVO latest = dbf.findByUuid(self.getUuid(), VolumeVO.class); + if (latest != null) { + self = latest; + } + } trigger.next(); } @@ -3741,34 +3750,38 @@ private void deleteConvertedVolumeEncryptionBits(List snapshots, Map oldAndNewInstallPaths, Map actualSizes) { - VolumeSnapshotReferenceUtils.handleVolumeInstallUrlChange(self.getUuid(), oldAndNewInstallPaths); - - UpdateQuery q = SQL.New(VolumeVO.class) - .eq(VolumeVO_.uuid, self.getUuid()) - .set(VolumeVO_.installPath, oldAndNewInstallPaths.get(self.getInstallPath())) - .set(VolumeVO_.encrypted, targetEncrypted); - if (actualSizes != null && actualSizes.get(self.getUuid()) != null) { - q.set(VolumeVO_.actualSize, actualSizes.get(self.getUuid())); - } - q.update(); + new SQLBatch() { + @Override + protected void scripts() { + VolumeSnapshotReferenceUtils.handleVolumeInstallUrlChange(self.getUuid(), oldAndNewInstallPaths); + + UpdateQuery q = SQL.New(VolumeVO.class) + .eq(VolumeVO_.uuid, self.getUuid()) + .set(VolumeVO_.installPath, oldAndNewInstallPaths.get(self.getInstallPath())) + .set(VolumeVO_.encrypted, targetEncrypted); + if (actualSizes != null && actualSizes.get(self.getUuid()) != null) { + q.set(VolumeVO_.actualSize, actualSizes.get(self.getUuid())); + } + q.update(); - Map convertedSnapshotInstallPaths = new LinkedHashMap<>(); - for (VolumeSnapshotVO snapshot : snapshots) { - String newPath = oldAndNewInstallPaths.get(snapshot.getPrimaryStorageInstallPath()); - if (StringUtils.isBlank(newPath)) { - continue; - } - convertedSnapshotInstallPaths.put(snapshot.getUuid(), newPath); - } + Map convertedSnapshotInstallPaths = new LinkedHashMap<>(); + for (VolumeSnapshotVO snapshot : snapshots) { + String newPath = oldAndNewInstallPaths.get(snapshot.getPrimaryStorageInstallPath()); + if (StringUtils.isBlank(newPath)) { + continue; + } + convertedSnapshotInstallPaths.put(snapshot.getUuid(), newPath); + } - updateConvertedSnapshotPaths(convertedSnapshotInstallPaths, targetEncrypted); - if (targetEncrypted && !convertedSnapshotInstallPaths.isEmpty()) { - volumeEncryptedResourceKeyBackend.copyVolumeKeyRefToSnapshots(self.getUuid(), convertedSnapshotInstallPaths.keySet()); - } + updateConvertedSnapshotPaths(convertedSnapshotInstallPaths, targetEncrypted); + if (targetEncrypted && !convertedSnapshotInstallPaths.isEmpty()) { + volumeEncryptedResourceKeyBackend.copyVolumeKeyRefToSnapshots(self.getUuid(), convertedSnapshotInstallPaths.keySet()); + } + } + }.execute(); } private void updateConvertedSnapshotPaths(Map snapshotInstallPaths, boolean targetEncrypted) {