Skip to content

Commit bcd2304

Browse files
committed
Improve VDI.open_database error handling.
Signed-off-by: John Else <[email protected]>
1 parent 42ec66c commit bcd2304

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

ocaml/idl/datamodel.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ let _ =
10621062
error Api_errors.no_more_redo_logs_allowed []
10631063
~doc:"The upper limit of active redo log instances was reached." ();
10641064

1065-
error Api_errors.could_not_import_database []
1065+
error Api_errors.could_not_import_database ["reason"]
10661066
~doc:"An error occurred while attempting to import a database from a metadata VDI" ();
10671067

10681068
error Api_errors.vm_incompatible_with_this_host []

ocaml/xapi/xapi_vdi.ml

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,10 @@ let open_database ~__context ~self =
575575
debug "%s" "Attaching VDI for metadata import";
576576
Static_vdis.permanent_vdi_attach ~__context ~vdi ~reason
577577
in
578+
let detach vdi =
579+
debug "%s" "Detaching VDI after metadata import";
580+
Static_vdis.permanent_vdi_detach ~__context ~vdi
581+
in
578582
(* Open the database contained in the VDI *)
579583
let db_ref_of_attached_vdi reason =
580584
(* Read db to temporary file *)
@@ -589,25 +593,22 @@ let open_database ~__context ~self =
589593
Db_ref.update_database db_ref (fun db -> Database.reindex db);
590594
db_ref
591595
in
592-
let detach vdi =
593-
debug "%s" "Detaching VDI after metadata import";
594-
Static_vdis.permanent_vdi_detach ~__context ~vdi
596+
let reason = Printf.sprintf "%s %s"
597+
Xapi_globs.foreign_metadata_vdi_reason
598+
(Db.VDI.get_uuid ~__context ~self)
595599
in
596600
try
597-
let reason = Printf.sprintf "%s %s"
598-
Xapi_globs.foreign_metadata_vdi_reason
599-
(Db.VDI.get_uuid ~__context ~self)
600-
in
601-
debug "Attaching database VDI to master with reason [%s]" reason;
602601
attach self reason;
603-
debug "%s" "Attempting to read database";
604602
let db_ref = db_ref_of_attached_vdi reason in
605-
debug "%s" "Detaching metadata VDI";
606603
detach self;
607604
(* Create a new session to query the database, and associate it with the db ref *)
608605
debug "%s" "Creating readonly session";
609606
let read_only_session = Xapi_session.create_readonly_session ~__context in
610607
Db_backend.register_session_with_database read_only_session db_ref;
611608
read_only_session
612-
with _ ->
613-
raise (Api_errors.Server_error(Api_errors.could_not_import_database, []))
609+
with e ->
610+
(* Make sure to detach if either the attach or database read fail. *)
611+
detach self;
612+
let error = Printexc.to_string e in
613+
debug "Caught %s while trying to open database" error;
614+
raise (Api_errors.Server_error(Api_errors.could_not_import_database, [error]))

0 commit comments

Comments
 (0)