Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / mon / QuorumService.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) 2013 Inktank, Inc
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 #ifndef CEPH_MON_QUORUM_SERVICE_H
15 #define CEPH_MON_QUORUM_SERVICE_H
16
17 #include <errno.h>
18
19 #include "include/types.h"
20 #include "include/Context.h"
21 #include "common/RefCountedObj.h"
22 #include "common/config.h"
23
24 #include "mon/Monitor.h"
25
26 class QuorumService
27 {
28   Context *tick_event = nullptr;
29   double tick_period;
30
31 public:
32   enum {
33     SERVICE_HEALTH                   = 0x01,
34     SERVICE_TIMECHECK                = 0x02,
35     SERVICE_CONFIG_KEY               = 0x03,
36   };
37
38 protected:
39   Monitor *mon;
40   epoch_t epoch;
41
42   QuorumService(Monitor *m) :
43     tick_period(g_conf->mon_tick_interval),
44     mon(m),
45     epoch(0)
46   {
47   }
48
49   void cancel_tick() {
50     if (tick_event)
51       mon->timer.cancel_event(tick_event);
52     tick_event = NULL;
53   }
54
55   void start_tick() {
56     generic_dout(10) << __func__ << dendl;
57
58     cancel_tick();
59     if (tick_period <= 0)
60       return;
61
62     tick_event = new C_MonContext(mon, [this](int r) {
63         if (r < 0)
64           return;
65         tick();
66       });
67     mon->timer.add_event_after(tick_period, tick_event);
68   }
69
70   void set_update_period(double t) {
71     tick_period = t;
72   }
73
74   bool in_quorum() {
75     return (mon->is_leader() || mon->is_peon());
76   }
77
78   virtual bool service_dispatch(MonOpRequestRef op) = 0;
79   virtual void service_tick() = 0;
80   virtual void service_shutdown() = 0;
81
82   virtual void start_epoch() = 0;
83   virtual void finish_epoch() = 0;
84   virtual void cleanup() = 0;
85
86 public:
87   virtual ~QuorumService() { }
88
89   void start(epoch_t new_epoch) {
90     epoch = new_epoch;
91     start_epoch();
92   }
93
94   void finish() {
95     generic_dout(20) << "QuorumService::finish" << dendl;
96     finish_epoch();
97   }
98
99   epoch_t get_epoch() const {
100     return epoch;
101   }
102
103   bool dispatch(MonOpRequestRef op) {
104     return service_dispatch(op);
105   }
106
107   void tick() {
108     service_tick();
109     start_tick();
110   }
111
112   void shutdown() {
113     generic_dout(0) << "quorum service shutdown" << dendl;
114     cancel_tick();
115     service_shutdown();
116   }
117
118   virtual void init() { }
119
120   virtual void get_health(list<pair<health_status_t,string> >& summary,
121                           list<pair<health_status_t,string> > *detail) = 0;
122   virtual int get_type() = 0;
123   virtual string get_name() const = 0;
124
125 };
126
127 #endif /* CEPH_MON_QUORUM_SERVICE_H */