Skip to content

Commit 1b4cfac

Browse files
authored
[26.1-lcm] CA-424844 Revert Update VDI links after import (#6956)
This is the 26.1-lcm version of #6952. It reverts PR #6913.
2 parents a785717 + 8f63fb2 commit 1b4cfac

1 file changed

Lines changed: 31 additions & 52 deletions

File tree

ocaml/xapi/import.ml

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,57 +2073,37 @@ let handlers =
20732073
; (Datamodel_common._vtpm, VTPMHandler.handle)
20742074
]
20752075

2076-
(* If snapshot_of(x) = y, and both x and y are freshly imported VDIs,
2077-
the field must be rewritten to a real database reference. The same
2078-
relationship formed by parental links must be preserved as well.
2079-
2080-
We can't do this on-the-fly during an import because we can't
2081-
guarantee that the VDIs will be imported in reverse topological
2082-
order of the relation induced by snapshot_of. *)
2083-
let update_vdi_links ~__context state =
2084-
(* Map an exported reference to a (freshly) imported reference, or
2085-
null if it's not present in the import. *)
2086-
let resolve_import =
2087-
let tbl = Hashtbl.create 16 in
2088-
let go (_, a, b) = Hashtbl.replace tbl a (Ref.of_string b) in
2089-
List.iter go state.table ;
2090-
fun r ->
2091-
Hashtbl.find_opt tbl (Ref.string_of r) |> Option.value ~default:Ref.null
2092-
in
2093-
let update x =
2094-
let x_r = Db.VDI.get_record ~__context ~self:x in
2095-
let parent = resolve_import x_r.API.vDI_parent in
2096-
let snapshot_of = resolve_import x_r.API.vDI_snapshot_of in
2097-
Db.VDI.set_parent ~__context ~self:x ~value:parent ;
2098-
Db.VDI.set_snapshot_of ~__context ~self:x ~value:snapshot_of ;
2099-
Db.VDI.set_is_a_snapshot ~__context ~self:x ~value:(snapshot_of <> Ref.null)
2100-
in
2101-
let go (cls, _, r) =
2102-
if cls = Datamodel_common._vdi then update (Ref.of_string r)
2103-
in
2104-
List.iter go state.table
2105-
2106-
(* Same as [update_vdi_links] but over VMs instead. *)
2107-
let update_vm_links ~__context state =
2108-
let resolve_import =
2109-
let tbl = Hashtbl.create 16 in
2110-
let go (_, a, b) = Hashtbl.replace tbl a (Ref.of_string b) in
2111-
List.iter go state.table ;
2112-
fun r ->
2113-
Hashtbl.find_opt tbl (Ref.string_of r) |> Option.value ~default:Ref.null
2114-
in
2115-
let update x =
2116-
let x_r = Db.VM.get_record ~__context ~self:x in
2117-
let parent = resolve_import x_r.API.vM_parent in
2118-
let snapshot_of = resolve_import x_r.API.vM_snapshot_of in
2119-
Db.VM.set_parent ~__context ~self:x ~value:parent ;
2120-
Db.VM.set_snapshot_of ~__context ~self:x ~value:snapshot_of ;
2121-
Db.VM.set_is_a_snapshot ~__context ~self:x ~value:(snapshot_of <> Ref.null)
2122-
in
2123-
let go (cls, _, r) =
2124-
if cls = Datamodel_common._vm then update (Ref.of_string r)
2076+
let update_snapshot_and_parent_links ~__context state =
2077+
let aux (cls, _, ref) =
2078+
let ref = Ref.of_string ref in
2079+
( if
2080+
cls = Datamodel_common._vm
2081+
&& Db.VM.get_is_a_snapshot ~__context ~self:ref
2082+
then
2083+
let snapshot_of = Db.VM.get_snapshot_of ~__context ~self:ref in
2084+
if snapshot_of <> Ref.null then (
2085+
debug "lookup for snapshot_of = '%s'" (Ref.string_of snapshot_of) ;
2086+
log_reraise
2087+
("Failed to find the VM which is snapshot of "
2088+
^ Db.VM.get_name_label ~__context ~self:ref
2089+
)
2090+
(fun table ->
2091+
let snapshot_of = (lookup snapshot_of) table in
2092+
Db.VM.set_snapshot_of ~__context ~self:ref ~value:snapshot_of
2093+
)
2094+
state.table
2095+
)
2096+
) ;
2097+
if cls = Datamodel_common._vm then (
2098+
let parent = Db.VM.get_parent ~__context ~self:ref in
2099+
debug "lookup for parent = '%s'" (Ref.string_of parent) ;
2100+
try
2101+
let parent = lookup parent state.table in
2102+
Db.VM.set_parent ~__context ~self:ref ~value:parent
2103+
with _ -> debug "no parent found"
2104+
)
21252105
in
2126-
List.iter go state.table
2106+
List.iter aux state.table
21272107

21282108
let check_references ~__context (table : table) =
21292109
let is_export_reference r =
@@ -2171,8 +2151,7 @@ let handle_all __context config rpc session_id (xs : obj list) =
21712151
false
21722152
in
21732153
if not dry_run then (
2174-
update_vm_links ~__context state ;
2175-
update_vdi_links ~__context state ;
2154+
update_snapshot_and_parent_links ~__context state ;
21762155
check_references ~__context state.table
21772156
) ;
21782157
state

0 commit comments

Comments
 (0)