Skip to content

Commit 1bd6483

Browse files
committed
Refactor Storage_migrate.find_vdi
Extract common logic on finding vdi_info given vdi, and also add a parameter to specify where to find the VDI (locally or remotely). Signed-off-by: Vincent Liu <[email protected]>
1 parent 65f6222 commit 1bd6483

File tree

4 files changed

+15
-30
lines changed

4 files changed

+15
-30
lines changed

ocaml/xapi/storage_migrate.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ module MigrateLocal = struct
278278

279279
let (module Remote) = get_remote_backend url verify_dest in
280280
(* Find the local VDI *)
281-
let local_vdi = find_local_vdi ~dbg ~sr ~vdi in
281+
let local_vdi, _ = find_vdi ~dbg ~sr ~vdi (module Local) in
282282
let mirror_id = State.mirror_id_of (sr, local_vdi.vdi) in
283283
debug "%s: Adding to active local mirrors before sending: id=%s"
284284
__FUNCTION__ mirror_id ;

ocaml/xapi/storage_migrate_helper.ml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,13 @@ let get_remote_backend url verify_dest =
346346
end)) in
347347
(module Remote : SMAPIv2)
348348

349-
let find_local_vdi ~dbg ~sr ~vdi =
350-
(* Find the local VDI *)
351-
let vdis, _ = Local.SR.scan2 dbg sr in
349+
let find_vdi ~dbg ~sr ~vdi (module SMAPIv2 : SMAPIv2) =
350+
let vdis, _ = SMAPIv2.SR.scan2 dbg sr in
352351
match List.find_opt (fun x -> x.vdi = vdi) vdis with
353352
| None ->
354-
failwith "Local VDI not found"
353+
failwith_fmt "VDI %s not found" (Storage_interface.Vdi.string_of vdi)
355354
| Some v ->
356-
v
355+
(v, vdis)
357356

358357
(** [similar_vdis dbg sr vdi] returns a list of content_ids of vdis
359358
which are similar to the input [vdi] in [sr] *)

ocaml/xapi/storage_migrate_helper.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ module Local : SMAPIv2
261261

262262
val get_remote_backend : string -> bool -> (module SMAPIv2)
263263

264-
val find_local_vdi : dbg:string -> sr:sr -> vdi:vdi -> vdi_info
264+
val find_vdi :
265+
dbg:string -> sr:sr -> vdi:vdi -> (module SMAPIv2) -> vdi_info * vdi_info list
265266

266267
val similar_vdis : dbg:string -> sr:sr -> vdi:vdi -> uuid list

ocaml/xapi/storage_smapiv1_migrate.ml

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -162,26 +162,11 @@ module Copy = struct
162162
(Printf.sprintf "Remote SR %s not found"
163163
(Storage_interface.Sr.string_of dest)
164164
) ;
165-
let vdis = Remote.SR.scan dbg dest in
166-
let remote_vdi =
167-
try List.find (fun x -> x.vdi = dest_vdi) vdis
168-
with Not_found ->
169-
failwith
170-
(Printf.sprintf "Remote VDI %s not found"
171-
(Storage_interface.Vdi.string_of dest_vdi)
172-
)
173-
in
165+
166+
let remote_vdi, _ = find_vdi ~dbg ~sr:dest ~vdi:dest_vdi (module Remote) in
174167
let dest_content_id = remote_vdi.content_id in
175168
(* Find the local VDI *)
176-
let vdis = Local.SR.scan dbg sr in
177-
let local_vdi =
178-
try List.find (fun x -> x.vdi = vdi) vdis
179-
with Not_found ->
180-
failwith
181-
(Printf.sprintf "Local VDI %s not found"
182-
(Storage_interface.Vdi.string_of vdi)
183-
)
184-
in
169+
let local_vdi, vdis = find_vdi ~dbg ~sr ~vdi (module Local) in
185170
D.debug "copy local content_id=%s" local_vdi.content_id ;
186171
D.debug "copy remote content_id=%s" dest_content_id ;
187172
if local_vdi.virtual_size > remote_vdi.virtual_size then (
@@ -293,6 +278,10 @@ module Copy = struct
293278
(* PR-1255: XXX: this is useful because we don't have content_ids by default *)
294279
D.debug "setting local content_id <- %s" local_vdi.content_id ;
295280
Local.VDI.set_content_id dbg sr local_vdi.vdi local_vdi.content_id ;
281+
(* Re-find the VDI to get the updated content_id info *)
282+
let remote_vdi, _ =
283+
find_vdi ~dbg ~sr:dest ~vdi:dest_vdi (module Remote)
284+
in
296285
Some (Vdi_info remote_vdi)
297286
with e ->
298287
D.error "Caught %s: performing cleanup actions" (Printexc.to_string e) ;
@@ -312,11 +301,7 @@ module Copy = struct
312301
let (module Remote) = get_remote_backend url verify_dest in
313302
(* Find the local VDI *)
314303
try
315-
let vdis = Local.SR.scan dbg sr in
316-
let local_vdi =
317-
try List.find (fun x -> x.vdi = vdi) vdis
318-
with Not_found -> failwith (Printf.sprintf "Local VDI not found")
319-
in
304+
let local_vdi, _ = find_vdi ~dbg ~sr ~vdi (module Local) in
320305
try
321306
let similar_vdis = Local.VDI.similar_content dbg sr vdi in
322307
let similars = List.map (fun vdi -> vdi.content_id) similar_vdis in

0 commit comments

Comments
 (0)