<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.
 *)
(* Manages persistent connection from slave->master over which the db is accessed.

   The state of this connection is used by the slave to determine whether it can see its
   master or not. After the slave has not been able to see the master for a while, xapi
   restarts in emergency mode.
*)

open Stdext.Threadext

type db_record = (string * string) list * (string * (string list)) list
module D = Debug.Make(struct let name = "master_connection" end)
open D

let my_connection : Stunnel.t option ref = ref None

let is_slave = ref (fun () -> false)
let get_master_address = ref (fun () -> "")
let master_rpc_path = ref ""

exception Cannot_connect_to_master

(* kill the stunnel underlying the connection.. When the master dies
   then read/writes to the connection block for ages waiting for the
   TCP timeout. By killing the stunnel, we can get these calls to
   unblock. (We do not clean up after killing stunnel, it is still the
   duty of the person who initiated the stunnel connection to call
   Stunnel.disconnect which closes the relevant fds and does wait_pid on
   the (now dead!) stunnel process.
*)
let force_connection_reset () =
  (* Cleanup cached stunnel connections to the master, so that future API
     	   calls won't be blocked. *)
  if (!is_slave) () then begin
    let host = (!get_master_address) () in
    let port = !Db_globs.https_port in
    (* We don't currently have a method to enumerate all the stunnel links
       		   to an address in the cache. The easiest way is, for each valid config
       		   combination, we pop out (remove) its links until Not_found is raised.
       		   Here, we have two such combinations, i.e. verify_cert=true/false, as
       		   host and port are fixed values. *)
    let purge_stunnels verify_cert =
      try
        while true do
          let st = Stunnel_cache.remove host port verify_cert in
          try Stunnel.disconnect ~wait:false ~force:true st with _ -> ()<commit_msg>Fail and log when database is used uninitialised<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.
 *)
(* Manages persistent connection from slave->master over which the db is accessed.

   The state of this connection is used by the slave to determine whether it can see its
   master or not. After the slave has not been able to see the master for a while, xapi
   restarts in emergency mode.
*)

open Stdext.Threadext

type db_record = (string * string) list * (string * (string list)) list
module D = Debug.Make(struct let name = "master_connection" end)
open D

let my_connection : Stunnel.t option ref = ref None

exception Uninitialised

let is_slave : (unit -> bool) ref = ref (fun () -> error "is_slave called without having been set. This is a fatal error."; raise Uninitialised)
let get_master_address = ref (fun () -> error "get_master_address called without having been set. This is a fatal error"; raise Uninitialised)
let master_rpc_path = ref "<invalid>"

exception Cannot_connect_to_master

(* kill the stunnel underlying the connection.. When the master dies
   then read/writes to the connection block for ages waiting for the
   TCP timeout. By killing the stunnel, we can get these calls to
   unblock. (We do not clean up after killing stunnel, it is still the
   duty of the person who initiated the stunnel connection to call
   Stunnel.disconnect which closes the relevant fds and does wait_pid on
   the (now dead!) stunnel process.
*)
let force_connection_reset () =
  (* Cleanup cached stunnel connections to the master, so that future API
     	   calls won't be blocked. *)
  if (!is_slave) () then begin
    let host = (!get_master_address) () in
    let port = !Db_globs.https_port in
    (* We don't currently have a method to enumerate all the stunnel links
       		   to an address in the cache. The easiest way is, for each valid config
       		   combination, we pop out (remove) its links until Not_found is raised.
       		   Here, we have two such combinations, i.e. verify_cert=true/false, as
       		   host and port are fixed values. *)
    let purge_stunnels verify_cert =
      try
        while true do
          let st = Stunnel_cache.remove host port verify_cert in
          try Stunnel.disconnect ~wait:false ~force:true st with _ -> ()
{'fraction_non_alphanumeric': 0.05976243504083148, 'fraction_numerical': 0.003711952487008166, 'mean_word_length': 3.38130081300813, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 13, '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': '3934544', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1413, 'n_words': 744}