Skip to content

Commit

Permalink
CP-51988: Fix functions not work for remote_pool repo
Browse files Browse the repository at this point in the history
1. `remote_pool` repo doesn't support periodic sync updates.
2. Periodic sync updates should be auto-disabled when calling `set_repositories`
   and `add_repository` for `remote_pool` repo.

Signed-off-by: Bengang Yuan <[email protected]>
  • Loading branch information
BengangY committed Oct 30, 2024
1 parent d10a8c0 commit 31e3399
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
8 changes: 7 additions & 1 deletion ocaml/idl/datamodel_errors.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,13 @@ let _ =
error Api_errors.bundle_repo_not_enabled []
~doc:"Cannot sync bundle as the bundle repository is not enabled." () ;
error Api_errors.can_not_sync_updates []
~doc:"Cannot sync updates as the bundle repository is enabled." () ;
~doc:"The current enabled repositories are not supported to sync updates."
() ;
error Api_errors.can_not_periodic_sync_updates []
~doc:
"The current enabled repositories are not supported to periodic sync \
updates."
() ;
error Api_errors.bundle_repo_should_be_single_enabled []
~doc:
"If the bundle repository is enabled, it should be the only one enabled \
Expand Down
2 changes: 2 additions & 0 deletions ocaml/xapi-consts/api_errors.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,8 @@ let bundle_repo_not_enabled = add_error "BUNDLE_REPO_NOT_ENABLED"

let can_not_sync_updates = add_error "CAN_NOT_SYNC_UPDATES"

let can_not_periodic_sync_updates = add_error "CAN_NOT_PERIODIC_SYNC_UPDATES"

let bundle_repo_should_be_single_enabled =
add_error "BUNDLE_REPO_SHOULD_BE_SINGLE_ENABLED"

Expand Down
48 changes: 30 additions & 18 deletions ocaml/xapi/xapi_pool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3406,24 +3406,30 @@ let enable_tls_verification ~__context =
| Some self ->
Xapi_cluster_host.set_tls_config ~__context ~self ~verify:true

let contains_bundle_repo ~__context ~repos =
List.exists
(fun repo -> Db.Repository.get_origin ~__context ~self:repo = `bundle)
repos

let assert_single_bundle_repo_can_be_enabled ~__context ~repos =
let contains_bundle_repo ~__context ~repos =
List.exists
(fun repo -> Db.Repository.get_origin ~__context ~self:repo = `bundle)
repos
in
if List.length repos > 1 && contains_bundle_repo ~__context ~repos then
raise Api_errors.(Server_error (bundle_repo_should_be_single_enabled, []))

let assert_not_bundle_repo ~__context ~repos =
if contains_bundle_repo ~__context ~repos then
raise Api_errors.(Server_error (can_not_sync_updates, []))
let assert_can_sync_updates ~__context ~repos =
List.iter
(fun repo ->
match Db.Repository.get_origin ~__context ~self:repo with
| `remote | `remote_pool ->
()
| `bundle ->
raise Api_errors.(Server_error (can_not_sync_updates, []))
)
repos

let disable_auto_update_sync_for_bundle_repo ~__context ~self ~repos =
if contains_bundle_repo ~__context ~repos then (
Pool_periodic_update_sync.set_enabled ~__context ~value:false ;
Db.Pool.set_update_sync_enabled ~__context ~self ~value:false
)
let can_periodic_sync_updates ~__context ~repos =
List.for_all
(fun repo -> Db.Repository.get_origin ~__context ~self:repo = `remote)
repos

let set_repositories ~__context ~self ~value =
Xapi_pool_helpers.with_pool_operation ~__context ~self
Expand Down Expand Up @@ -3453,7 +3459,10 @@ let set_repositories ~__context ~self ~value =
Db.Pool.set_repositories ~__context ~self ~value ;
if Db.Pool.get_repositories ~__context ~self = [] then
Db.Pool.set_last_update_sync ~__context ~self ~value:Date.epoch ;
disable_auto_update_sync_for_bundle_repo ~__context ~self ~repos:value
if not (can_periodic_sync_updates ~__context ~repos:value) then (
Pool_periodic_update_sync.set_enabled ~__context ~value:false ;
Db.Pool.set_update_sync_enabled ~__context ~self ~value:false
)

let add_repository ~__context ~self ~value =
Xapi_pool_helpers.with_pool_operation ~__context ~self
Expand All @@ -3468,8 +3477,10 @@ let add_repository ~__context ~self ~value =
Repository.reset_updates_in_cache () ;
Db.Pool.set_last_update_sync ~__context ~self ~value:Date.epoch
) ;
disable_auto_update_sync_for_bundle_repo ~__context ~self
~repos:(value :: existings)
if not (can_periodic_sync_updates ~__context ~repos:(value :: existings)) then (
Pool_periodic_update_sync.set_enabled ~__context ~value:false ;
Db.Pool.set_update_sync_enabled ~__context ~self ~value:false
)

let remove_repository ~__context ~self ~value =
Xapi_pool_helpers.with_pool_operation ~__context ~self
Expand Down Expand Up @@ -3508,7 +3519,7 @@ let sync_updates ~__context ~self ~force ~token ~token_id =
~doc:"pool.sync_updates" ~op:`sync_updates
@@ fun () ->
let repos = Repository_helpers.get_enabled_repositories ~__context in
assert_not_bundle_repo ~__context ~repos ;
assert_can_sync_updates ~__context ~repos ;
sync_repos ~__context ~self ~repos ~force ~token ~token_id

let check_update_readiness ~__context ~self:_ ~requires_reboot =
Expand Down Expand Up @@ -3793,7 +3804,8 @@ let set_update_sync_enabled ~__context ~self ~value =
repositories." ;
raise Api_errors.(Server_error (no_repositories_configured, []))
| repos ->
assert_not_bundle_repo ~__context ~repos
if not (can_periodic_sync_updates ~__context ~repos) then
raise Api_errors.(Server_error (can_not_periodic_sync_updates, []))
) ;
Pool_periodic_update_sync.set_enabled ~__context ~value ;
Db.Pool.set_update_sync_enabled ~__context ~self ~value
Expand Down

0 comments on commit 31e3399

Please sign in to comment.