<commit_before>(*
 * Copyright (C) 2006-2009 Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)
open Pervasiveext

module D=Debug.Debugger(struct let name="dbsync" end)
open D

let safe_wrapper n f x =
  try
    f x
  with e -> 
    debug "Caught exception while cancelling tasks (%s): %s" n (ExnHelper.string_of_exn e);
    log_backtrace ()

let update_all_allowed_operations ~__context =
	let open Stats in
	let all_vms = Db.VM.get_all ~__context
	and all_vbds = Db.VBD.get_all ~__context
	and all_vifs = Db.VIF.get_all ~__context
	and all_vdis = Db.VDI.get_all ~__context
	and all_srs = Db.SR.get_all ~__context
	and all_pbds = Db.PBD.get_all ~__context
	and all_hosts = Db.Host.get_all ~__context in
	time_this "Cancel_tasks.update_all_allowed_operations: VM" (fun () ->
		debug "Updating allowed operations: VM";
		List.iter (safe_wrapper "allowed_ops - VMs" (fun self -> Xapi_vm_lifecycle.update_allowed_operations ~__context ~self)) all_vms;
		debug "Finished updating allowed operations: VM");
	time_this "Cancel_tasks.update_all_allowed_operations: VBD" (fun () ->
		debug "Updating allowed operations: VBD";
		List.iter (safe_wrapper "allowed_ops - VBDs" (fun self -> Xapi_vbd_helpers.update_allowed_operations ~__context ~self)) all_vbds;
		debug "Finished updating allowed operations: VBD");
	time_this "Cancel_tasks.update_all_allowed_operations: VIF" (fun () ->
		debug "Updating allowed operations: VIF";
		List.iter (safe_wrapper "allowed_ops - VIFs" (fun self -> Xapi_vif_helpers.update_allowed_operations ~__context ~self)) all_vifs;
		debug "Finished updating allowed operations: VIF");
	time_this "Cancel_tasks.update_all_allowed_operations: VDI" (fun () ->
		debug "Updating allowed operations: VDI";
		let sr_records = List.map (fun sr -> (sr, Db.SR.get_record_internal ~__context ~self:sr)) all_srs in
		let pbd_records = List.map (fun pbd -> (pbd, Db.PBD.get_record ~__context ~self:pbd)) all_pbds in
		let vbd_records = List.map (fun vbd -> (vbd, Db.VBD.get_record_internal ~__context ~self:vbd)) all_vbds in
		List.iter (safe_wrapper "allowed_ops - VDIs"
			(fun self -> Xapi_vdi.update_allowed_operations_internal ~__context ~self ~sr_records ~pbd_records ~vbd_records)) all_vdis;
		debug "Finished updating allowed operations: VDI");
	time_this "Cancel_tasks.update_all_allowed_operations: SR" (fun () ->
		debug "Updating allowed operations: SR";
		List.iter (safe_wrapper "allowed_ops" (fun self ->
			Db.SR.set_current_operations ~__context ~self ~value:[];
			Xapi_sr_operations.update_allowed_operations ~__context ~self)) all_srs;
			debug "Finished updating allowed operations: SR";
			debug "Updating allowed operations: host";
			List.iter (safe_wrapper "allowed_ops - host" (fun self -> Xapi_host_helpers.update_allowed_operations ~__context ~self)) all_hosts;
			debug "Finished updating allowed operations: host")

(* !!! This code was written in a world when tasks, current_operations and allowed_operations were persistent.
   This is no longer the case (we changed this to reduce writes to flash for OEM case + to simplify xapi logic elsewhere).
   The case where the slave calls the master to cancel its tasks is still useful; however, when the master restarts
   then all the current-operations,allowed-operations and tasks for the whole pool will have been lost. We rely on the
   resyncing code + persistent fields such as "currently-attached" etc. to provide enough lock-state across xapi restart
   to ensure safe storage access. However, long-running xapi managed tasks are going to float off into the ether over a
   master restart... *)

(* When the master restarts, it updates all allowed_operations. The first time a slave says Pool.hello, it is not
   necessary to update the allowed_operations again. The next time the slave says hello (eg after a slave but not master restart)
   we do need to update the allowed_operations. This optimises the cold-boot/master-failover case. *)<commit_msg>Fix timing code in cancel_tasks.ml<commit_after>(*
 * Copyright (C) 2006-2009 Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)
open Pervasiveext

module D=Debug.Debugger(struct let name="dbsync" end)
open D

let safe_wrapper n f x =
  try
    f x
  with e -> 
    debug "Caught exception while cancelling tasks (%s): %s" n (ExnHelper.string_of_exn e);
    log_backtrace ()

let update_all_allowed_operations ~__context =
	let open Stats in
	let all_vms = Db.VM.get_all ~__context
	and all_vbds = Db.VBD.get_all ~__context
	and all_vifs = Db.VIF.get_all ~__context
	and all_vdis = Db.VDI.get_all ~__context
	and all_srs = Db.SR.get_all ~__context
	and all_pbds = Db.PBD.get_all ~__context
	and all_hosts = Db.Host.get_all ~__context in
	time_this "Cancel_tasks.update_all_allowed_operations: VM" (fun () ->
		debug "Updating allowed operations: VM";
		List.iter (safe_wrapper "allowed_ops - VMs" (fun self -> Xapi_vm_lifecycle.update_allowed_operations ~__context ~self)) all_vms;
		debug "Finished updating allowed operations: VM");
	time_this "Cancel_tasks.update_all_allowed_operations: VBD" (fun () ->
		debug "Updating allowed operations: VBD";
		List.iter (safe_wrapper "allowed_ops - VBDs" (fun self -> Xapi_vbd_helpers.update_allowed_operations ~__context ~self)) all_vbds;
		debug "Finished updating allowed operations: VBD");
	time_this "Cancel_tasks.update_all_allowed_operations: VIF" (fun () ->
		debug "Updating allowed operations: VIF";
		List.iter (safe_wrapper "allowed_ops - VIFs" (fun self -> Xapi_vif_helpers.update_allowed_operations ~__context ~self)) all_vifs;
		debug "Finished updating allowed operations: VIF");
	time_this "Cancel_tasks.update_all_allowed_operations: VDI" (fun () ->
		debug "Updating allowed operations: VDI";
		let sr_records = List.map (fun sr -> (sr, Db.SR.get_record_internal ~__context ~self:sr)) all_srs in
		let pbd_records = List.map (fun pbd -> (pbd, Db.PBD.get_record ~__context ~self:pbd)) all_pbds in
		let vbd_records = List.map (fun vbd -> (vbd, Db.VBD.get_record_internal ~__context ~self:vbd)) all_vbds in
		List.iter (safe_wrapper "allowed_ops - VDIs"
			(fun self -> Xapi_vdi.update_allowed_operations_internal ~__context ~self ~sr_records ~pbd_records ~vbd_records)) all_vdis;
		debug "Finished updating allowed operations: VDI");
	time_this "Cancel_tasks.update_all_allowed_operations: SR" (fun () ->
		debug "Updating allowed operations: SR";
		List.iter (safe_wrapper "allowed_ops" (fun self ->
			Db.SR.set_current_operations ~__context ~self ~value:[];
			Xapi_sr_operations.update_allowed_operations ~__context ~self)) all_srs;
		debug "Finished updating allowed operations: SR");
	time_this "Cancel_tasks.update_all_allowed_operations: host" (fun () ->
		debug "Updating allowed operations: host";
		List.iter (safe_wrapper "allowed_ops - host" (fun self -> Xapi_host_helpers.update_allowed_operations ~__context ~self)) all_hosts;
		debug "Finished updating allowed operations: host")

(* !!! This code was written in a world when tasks, current_operations and allowed_operations were persistent.
   This is no longer the case (we changed this to reduce writes to flash for OEM case + to simplify xapi logic elsewhere).
   The case where the slave calls the master to cancel its tasks is still useful; however, when the master restarts
   then all the current-operations,allowed-operations and tasks for the whole pool will have been lost. We rely on the
   resyncing code + persistent fields such as "currently-attached" etc. to provide enough lock-state across xapi restart
   to ensure safe storage access. However, long-running xapi managed tasks are going to float off into the ether over a
   master restart... *)

(* When the master restarts, it updates all allowed_operations. The first time a slave says Pool.hello, it is not
   necessary to update the allowed_operations again. The next time the slave says hello (eg after a slave but not master restart)
   we do need to update the allowed_operations. This optimises the cold-boot/master-failover case. *)
{'fraction_non_alphanumeric': 0.08265658026476805, 'fraction_numerical': 0.002224941595283124, 'mean_word_length': 4.737077217613273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7289573', 'n_tokens_mistral': 2803, 'n_tokens_neox': 2692, 'n_words': 1012}