Skip to content

Commit 29e033d

Browse files
johnelsezli
authored andcommitted
CA-89978: Fix restoration of database from temporary restore file.
- Delete the temporary restore file after using it to populate the database - if we don't do this then xapi will use the restore file again the next time it starts up. - Perform the initial database flush to the global list of database connections in all cases; don't flush to the restore database file if it's being used. Signed-off-by: John Else <[email protected]>
1 parent 9abe90c commit 29e033d

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

ocaml/xapi/xapi.ml

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,35 @@ let database_ready_for_clients = ref false (* while this is false, client calls
6363

6464
open Db_cache_types
6565

66+
(** Populate the database from the default connections or the restore db file
67+
(if it is present). Perform an initial flush to the database connections
68+
which were already setup, then delete the restore file. *)
69+
let populate_db backend =
70+
let schema = Datamodel_schema.of_datamodel () in
71+
72+
let output_connections = Db_conn_store.read_db_connections () in
73+
(* If the temporary restore file is present then we must populate from that *)
74+
let restoring = Sys.file_exists Xapi_globs.db_temporary_restore_path in
75+
let input_connections =
76+
if restoring
77+
then [ Parse_db_conf.make Xapi_globs.db_temporary_restore_path ]
78+
else output_connections
79+
in
80+
Db_cache_impl.make backend input_connections schema;
81+
Db_cache_impl.sync output_connections (Db_ref.get_database backend);
82+
(* Delete the temporary restore file so that we don't revert to it again at next startup. *)
83+
if restoring then begin
84+
Unixext.unlink_safe Xapi_globs.db_temporary_restore_path;
85+
Unixext.unlink_safe (Xapi_globs.db_temporary_restore_path ^ ".generation")
86+
end
87+
6688
(** Starts the main database engine: this should be done only on the master node.
6789
The db connections must have been parsed from db.conf file and initialised before this fn is called.
6890
Also this function depends on being able to call API functions through the external interface.
6991
*)
7092
let start_database_engine () =
71-
let schema = Datamodel_schema.of_datamodel () in
72-
73-
(* If the temporary restore file is present then we must populate from that *)
74-
let connections =
75-
if Sys.file_exists Xapi_globs.db_temporary_restore_path
76-
then [ Parse_db_conf.make Xapi_globs.db_temporary_restore_path ]
77-
else Db_conn_store.read_db_connections () in
7893
let t = Db_backend.make () in
79-
Db_cache_impl.make t connections schema;
80-
Db_cache_impl.sync connections (Db_ref.get_database t);
94+
populate_db t;
8195

8296
Db_ref.update_database t (Database.register_callback "redo_log" Redo_log.database_callback);
8397
Db_ref.update_database t (Database.register_callback "events" Eventgen.database_callback);

0 commit comments

Comments
 (0)