Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / HeartbeatMap.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2011 Sage Weil <sage@newdream.net>
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software 
11  * Foundation.  See file COPYING.
12  * 
13  */
14
15 #ifndef CEPH_HEARTBEATMAP_H
16 #define CEPH_HEARTBEATMAP_H
17
18 #include <list>
19 #include <atomic>
20 #include <string>
21
22 #include <pthread.h>
23
24 #include "RWLock.h"
25
26 class CephContext;
27
28 namespace ceph {
29
30 /*
31  * HeartbeatMap -
32  *
33  * Maintain a set of handles for internal subsystems to periodically
34  * check in with a health check and timeout.  Each user can register
35  * and get a handle they can use to set or reset a timeout.  
36  *
37  * A simple is_healthy() method checks for any users who are not within
38  * their grace period for a heartbeat.
39  */
40
41 struct heartbeat_handle_d {
42   const std::string name;
43   pthread_t thread_id;
44   std::atomic<unsigned> timeout = { 0 }, suicide_timeout = { 0 };
45   time_t grace, suicide_grace;
46   std::list<heartbeat_handle_d*>::iterator list_item;
47
48   explicit heartbeat_handle_d(const std::string& n)
49     : name(n), thread_id(0), grace(0), suicide_grace(0)
50   { }
51 };
52
53 class HeartbeatMap {
54  public:
55   // register/unregister
56   heartbeat_handle_d *add_worker(const std::string& name, pthread_t thread_id);
57   void remove_worker(const heartbeat_handle_d *h);
58
59   // reset the timeout so that it expects another touch within grace amount of time
60   void reset_timeout(heartbeat_handle_d *h, time_t grace, time_t suicide_grace);
61   // clear the timeout so that it's not checked on
62   void clear_timeout(heartbeat_handle_d *h);
63
64   // return false if any of the timeouts are currently expired.
65   bool is_healthy();
66
67   // touch cct->_conf->heartbeat_file if is_healthy()
68   void check_touch_file();
69
70   // get the number of unhealthy workers
71   int get_unhealthy_workers() const;
72
73   // get the number of total workers
74   int get_total_workers() const;
75
76   explicit HeartbeatMap(CephContext *cct);
77   ~HeartbeatMap();
78
79  private:
80   CephContext *m_cct;
81   RWLock m_rwlock;
82   time_t m_inject_unhealthy_until;
83   std::list<heartbeat_handle_d*> m_workers;
84   std::atomic<unsigned> m_unhealthy_workers = { 0 };
85   std::atomic<unsigned> m_total_workers = { 0 };
86
87   bool _check(const heartbeat_handle_d *h, const char *who, time_t now);
88 };
89
90 }
91 #endif