Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: ca-85776
Fetching contributors…

Cannot retrieve contributors at this time

file 70 lines (59 sloc) 1.76 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
(*
* Copyright (C) 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.
*)
(**
* @group Pool Management
*)

open Stringext
open Threadext

(** The role of this node *)
type t =
| Master
| Slave of string (* IP address *)
| Broken

let role = ref None
let role_m = Mutex.create ()

let string_of = function
| Master -> "master"
| Slave x -> "slave:" ^ x
| Broken -> "broken"

let read_pool_role () =
try
let s = String.strip String.isspace
(Unixext.string_of_file Constants.pool_config_file) in
match String.split ~limit:2 ':' s with
| [ "master" ] -> Master
| [ "slave"; m_ip ] -> Slave m_ip
| [ "broken" ] -> Broken
| _ -> failwith "cannot parse pool_role from pool config file"
with _ -> Broken

let get_role () =
Mutex.execute role_m (fun _ ->
match !role with
| Some x -> x
| None ->
let r = read_pool_role () in
role := Some r;
r
)

let is_master () = get_role () = Master

let is_slave () = match get_role () with
| Slave _ -> true
| _ -> false

let is_broken () = get_role () = Broken

exception This_host_is_a_master
exception This_host_is_broken

let get_master_address () = match get_role () with
| Slave ip -> ip
| Master -> raise This_host_is_a_master
| Broken -> raise This_host_is_broken
Something went wrong with that request. Please try again.