1212import org .zstack .core .db .Q ;
1313import org .zstack .core .db .SQL ;
1414import org .zstack .core .db .SQLBatch ;
15+ import org .zstack .core .timeout .TimeHelper ;
1516import org .zstack .core .workflow .SimpleFlowChain ;
1617import org .zstack .header .AbstractService ;
1718import org .zstack .header .core .ReturnValueCompletion ;
@@ -101,6 +102,8 @@ public class KvmSecureBootManager extends AbstractService {
101102 private ResourceConfigFacade resourceConfigFacade ;
102103 @ Autowired
103104 private KvmVmHostFileFactory vmHostFileFactory ;
105+ @ Autowired
106+ private TimeHelper timeHelper ;
104107
105108 @ Override
106109 public boolean start () {
@@ -217,6 +220,7 @@ private void handle(SyncVmHostFilesFromHostMsg msg) {
217220 to .setType (VmHostFileType .NvRam .toString ());
218221 cmd .getHostFiles ().add (to );
219222 }
223+ long now = timeHelper .getCurrentTimeMillis ();
220224
221225 SyncVmHostFilesFromHostReply reply = new SyncVmHostFilesFromHostReply ();
222226 sender .send (cmd , READ_VM_HOST_FILE_PATH , wrapper -> {
@@ -237,7 +241,7 @@ public void success(KvmResponseWrapper wrapper) {
237241 if (msg .isSyncToBackup ()) {
238242 error = syncToBackupFiles (msg , readRsp );
239243 } else {
240- error = syncToHostFiles (msg , cmd , readRsp );
244+ error = syncToHostFiles (msg , cmd , readRsp , now );
241245 }
242246
243247 if (error != null ) {
@@ -256,7 +260,8 @@ public void fail(ErrorCode errorCode) {
256260
257261 private ErrorCode syncToHostFiles (SyncVmHostFilesFromHostMsg msg ,
258262 KVMAgentCommands .ReadVmHostFileContentCmd cmd ,
259- KVMAgentCommands .ReadVmHostFileContentResponse readRsp ) {
263+ KVMAgentCommands .ReadVmHostFileContentResponse readRsp ,
264+ long timeBeforeSync ) {
260265 final List <VmHostFileVO > existsFiles = Q .New (VmHostFileVO .class )
261266 .eq (VmHostFileVO_ .vmInstanceUuid , msg .getVmUuid ())
262267 .eq (VmHostFileVO_ .hostUuid , msg .getHostUuid ())
@@ -272,6 +277,7 @@ private ErrorCode syncToHostFiles(SyncVmHostFilesFromHostMsg msg,
272277 existsContentUuid = Collections .emptyList ();
273278 }
274279
280+ Timestamp syncTime = new Timestamp (timeBeforeSync );
275281 List <ErrorCode > errors = new ArrayList <>();
276282 for (String path : cmd .getPaths ()) {
277283 KVMAgentCommands .VmHostFileTO to = findOneOrNull (readRsp .getHostFiles (), item -> item .getPath ().equals (path ));
@@ -291,13 +297,25 @@ private ErrorCode syncToHostFiles(SyncVmHostFilesFromHostMsg msg,
291297 VmHostFileVO file = findOneOrNull (existsFiles , item -> item .getPath ().equals (path ));
292298 boolean fileExists = file != null ;
293299
294- Timestamp now = Timestamp .from (Instant .now ());
295300 if (fileExists ) {
296- SQL .New (VmHostFileVO .class )
297- .eq (VmHostFileVO_ .uuid , file .getUuid ())
298- .set (VmHostFileVO_ .lastOpDate , now )
299- .set (VmHostFileVO_ .lastSyncReason , msg .getSyncReason ())
300- .update ();
301+ String fileUuid = file .getUuid ();
302+ new SQLBatch () {
303+ @ Override
304+ protected void scripts () {
305+ sql (VmHostFileVO .class )
306+ .eq (VmHostFileVO_ .uuid , fileUuid )
307+ .set (VmHostFileVO_ .lastSyncReason , msg .getSyncReason ())
308+ .set (VmHostFileVO_ .lastSyncDate , syncTime )
309+ .set (VmHostFileVO_ .lastOpDate , syncTime )
310+ .update ();
311+ sql (VmHostFileVO .class )
312+ .eq (VmHostFileVO_ .uuid , fileUuid )
313+ .lt (VmHostFileVO_ .changeDate , syncTime )
314+ .set (VmHostFileVO_ .changeDate , null ) // CAS update
315+ .update ();
316+ }
317+ }.execute ();
318+
301319 } else {
302320 file = new VmHostFileVO ();
303321 file .setUuid (Platform .getUuid ());
@@ -306,8 +324,10 @@ private ErrorCode syncToHostFiles(SyncVmHostFilesFromHostMsg msg,
306324 file .setPath (path );
307325 file .setType (type );
308326 file .setLastSyncReason (msg .getSyncReason ());
309- file .setCreateDate (now );
310- file .setLastOpDate (now );
327+ file .setLastSyncDate (syncTime );
328+ file .setChangeDate (null );
329+ file .setLastOpDate (syncTime );
330+ file .setCreateDate (syncTime );
311331 file .setResourceName (String .format ("%s file for %s" , type , msg .getVmUuid ()));
312332 databaseFacade .persist (file );
313333 }
@@ -318,15 +338,15 @@ private ErrorCode syncToHostFiles(SyncVmHostFilesFromHostMsg msg,
318338 .eq (VmHostFileContentVO_ .uuid , file .getUuid ())
319339 .set (VmHostFileContentVO_ .content , bytes )
320340 .set (VmHostFileContentVO_ .format , VmHostFileContentFormat .valueOf (to .getFileFormat ()))
321- .set (VmHostFileContentVO_ .lastOpDate , now )
341+ .set (VmHostFileContentVO_ .lastOpDate , syncTime )
322342 .update ();
323343 } else {
324344 VmHostFileContentVO content = new VmHostFileContentVO ();
325345 content .setUuid (file .getUuid ());
326346 content .setContent (bytes );
327347 content .setFormat (VmHostFileContentFormat .valueOf (to .getFileFormat ()));
328- content .setCreateDate (now );
329- content .setLastOpDate (now );
348+ content .setCreateDate (syncTime );
349+ content .setLastOpDate (syncTime );
330350 databaseFacade .persist (content );
331351 }
332352
@@ -489,7 +509,7 @@ public void run(FlowTrigger trigger, Map data) {
489509 VmHostFileVO file = Q .New (VmHostFileVO .class )
490510 .eq (VmHostFileVO_ .vmInstanceUuid , msg .getSrcVmUuid ())
491511 .eq (VmHostFileVO_ .type , type )
492- .orderByDesc (VmHostFileVO_ .lastOpDate )
512+ .orderByDesc (VmHostFileVO_ .lastSyncDate )
493513 .limit (1 )
494514 .find ();
495515 if (file == null ) {
@@ -884,6 +904,7 @@ public void fail(ErrorCode errorCode) {
884904 .eq (VmHostFileVO_ .uuid , currentFile .getUuid ())
885905 .set (VmHostFileVO_ .lastSyncReason , Restore .reason (msg .getSyncReason ()))
886906 .set (VmHostFileVO_ .lastOpDate , now )
907+ .set (VmHostFileVO_ .lastSyncDate , now )
887908 .update ();
888909
889910 VmHostFileContentVO existingContent = contentMap .get (currentFile .getUuid ());
@@ -913,6 +934,7 @@ public void fail(ErrorCode errorCode) {
913934 newFile .setType (type );
914935 newFile .setPath (buildPathForVmHostFileType (type , msg .getVmInstanceUuid ()));
915936 newFile .setLastSyncReason (Restore .reason (msg .getSyncReason ()));
937+ newFile .setLastSyncDate (now );
916938 newFile .setCreateDate (now );
917939 newFile .setLastOpDate (now );
918940 databaseFacade .persist (newFile );
0 commit comments