This repository has been archived by the owner on Dec 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod_monitor.py
308 lines (225 loc) · 11.1 KB
/
mod_monitor.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import signal
import sys
import threading
import time
import config
import mod_drbd
import mod_plugins
import mod_services
import mod_logger
log = mod_logger.initlog(__name__)
class Monitor(threading.Thread):
def __init__(self, listener, pinger):
threading.Thread.__init__(self)
self.listener = listener
self.pinger = pinger
self.loop = True
self.status = Status(self)
self.quorum_ok = 0
def run(self):
log.info("Starting monitor")
self.listener.start()
self.pinger.start()
self.status.SetDead()
# TODO: get current "real" state (DRBD, services ???)
while self.loop:
if self.listener.server.got_remote_ping.wait(config.timeout):
self.listener.server.got_remote_ping.clear()
if not self.loop:
continue
self.status.SetPeerState(self.listener.server.last_udp_data)
if self.status.state == "master":
if config.role == "master":
if self.status.pstate != "master":
self.status.SetState("master")
else:
if self.status.peer in [ "starting", "waiting", "disabling", "slave", "unknown" ]:
log.info("We are currently master, the legitimate master is slave or not ready")
elif self.status.peer in [ "master" ]:
log.info("Oops, we have a split brain")
log.info("Disabling everything")
self.status.Disable()
elif self.status.peer in [ "enabling", "failback", "failover" ]:
log.info("Peer is transitioning to master")
self.status.Disable()
else:
log.info("Oops, peer state is wrong")
elif self.status.state in [ "slave", "enabling", "failback", "failover" ]:
if config.role == "master":
if self.status.peer in [ "starting", "waiting", "slave", "unknown" ]:
log.info("We are supposed to be master, peer is slave or not ready")
self.status.Enable()
elif self.status.peer in [ "disabling" ]:
log.info("We are supposed to be master, peer is transitioning to slave, wait for him to shutdown")
elif self.status.peer in [ "enabling" ]:
log.info("We are supposed to be master, peer is transitioning to master, wait for him to come up")
elif self.status.peer in [ "master" ]:
drbd_dunknown_resources = list()
drbd_inconsistent_resources = list()
drbd_splitbrain_resources = list()
for resource in mod_drbd.resources:
log.info("%s: %s/%s" % (resource.name, resource.getLocalDiskStatus(), resource.getPeerDiskStatus()))
if resource.getLocalDiskStatus() == "uptodate" and resource.getPeerDiskStatus() == "uptodate":
# this resource is ok for failback
pass
elif resource.getPeerDiskStatus() == "uptodate":
drbd_inconsistent_resources.append("%s=%s" % (resource.name, resource.getConnectionStatus()))
elif resource.getPeerDiskStatus() == "dunknown":
drbd_dunknown_resources.append("%s=%s" % (resource.name, resource.getConnectionStatus()))
else:
drbd_splitbrain_resources.append("%s=%s" % (resource.name, resource.getConnectionStatus()))
if len(drbd_dunknown_resources):
log.info("We are supposed to be master, but DRBD resources have not synced their state : %s" % (drbd_dunknown_resources))
elif len(drbd_splitbrain_resources):
log.info("We are supposed to be master, but DRBD resources have unhandled split brain : %s" % (drbd_splitbrain_resources))
self.status.Shutdown()
self.stop()
elif len(drbd_inconsistent_resources):
log.info("We are supposed to be master, but DRBD resources are currently syncing : %s" % (drbd_inconsistent_resources))
else:
log.info("We are supposed to be master, peer is currently master, tell him to transition to slave")
self.status.NotifyMasterTransition()
else:
log.info("Oops, peer state is wrong")
else:
if self.status.pstate != "slave":
self.status.SetState("slave")
elif self.status.state in [ "starting", "waiting", "disabling", "unknown" ]:
log.info("We are transitioning, wait for us to finish")
elif self.status.state in [ "shutdown" ]:
log.info("We are shutting down ...")
else:
log.info("Oops, our state is wrong")
else:
if not self.loop:
continue
self.status.SetPeerState("unknown")
if self.status.state == "master":
if self.status.pstate != "master":
self.status.SetState("master")
if mod_plugins.quorum:
if mod_plugins.quorum.get():
log.info("We have enough quorum to remain master")
else:
log.info("We have not enough quorum to remain master")
self.status.Disable()
elif self.status.state == "slave":
if mod_plugins.quorum:
if mod_plugins.quorum.get():
if self.quorum_ok < 3:
log.info("We have enough quorum to failover to us, but wait for few attempts")
self.quorum_ok += 1
else:
log.info("We have enough quorum to failover to us")
self.status.NotifyMasterTransition()
self.quorum_ok = 0
else:
log.info("We have not enough quorum to require failover to us")
elif not mod_plugins.quorum:
log.info("No quorum plugin defined, require failover to us")
self.status.NotifyMasterTransition()
elif self.status.state in [ "failback", "failover" ]:
self.status.Enable()
elif self.status.state in [ "starting", "waiting", "disabling", "enabling", "unknown" ]:
log.info("We are transitioning, wait for us to finish")
elif self.status.state in [ "shutdown" ]:
log.info("We are shutting down ...")
else:
log.info("Oops, our state is wrong")
self.listener.stop()
self.pinger.stop()
log.info("Monitor stopped")
def stop(self):
log.info("Stopping monitor")
self.loop = False
class Status():
def __init__(self, monitor):
self.monitor = monitor
self.pstate = "unknown"
self.state = "starting"
self.peer = False
def SetState(self, newstate, immediate=False):
self.pstate = self.state
self.state = newstate
self.monitor.pinger.wake()
# Instantly send the new state to peer, without waiting for pinger mainloop
if immediate:
self.monitor.pinger.send(newstate)
def SetPeerState(self, newstate):
oldstate = self.peer
self.peer = newstate
if oldstate and newstate != oldstate:
if newstate == "unknown":
log.info("Peer is down")
elif newstate == "shutdown":
log.info("Peer is shutting down")
else:
log.info("Peer state is now '%s'" % (newstate))
def SetDead(self):
log.info("Waiting for a while before handling events")
self.SetState("waiting")
time.sleep(config.initdead)
self.SetState("slave")
log.info("We are now slave")
def NotifyMasterTransition(self):
log.info("Notifying that we are transitioning to master")
if config.role == "master":
self.SetState("failback", immediate=True)
time.sleep(0.5)
elif config.role == "slave":
self.SetState("failover", immediate=True)
time.sleep(0.5)
def Enable(self):
if self.state == "shutdown":
return
if self.state != "enabling":
self.SetState("enabling")
log.info("Transitioning to master")
# Start DRBD resources
for resource in mod_drbd.resources:
log.info("Setting DRBD resource '%s' as primary" % (resource.name))
if not resource.setPrimary():
log.fatal("Failed to set DRBD resource role")
# Start system services
mod_services.start()
# Acquire IP address
if mod_plugins.switcher:
if mod_plugins.switcher.activate():
log.info("Failover switch activated")
else:
log.error("Failed to activate failover switch")
self.SetState("master")
# Notify failback/failover
if mod_plugins.notifier:
if mod_plugins.notifier.activate():
log.info("Notification sent")
else:
log.error("Failed to send notification")
log.info("We are now master")
def Disable(self):
if self.state == "slave":
return
log.info("Transitioning to slave")
self.SetState("disabling")
# Stop system services
mod_services.stop()
# Release IP address
if mod_plugins.switcher:
if mod_plugins.switcher.deactivate():
log.info("Failover switch deactivated")
else:
log.error("Failed to deactivate failover switch")
# Stop DRBD resources
for resource in mod_drbd.resources:
log.info("Setting DRBD resource '%s' as secondary" % (resource.name))
if not resource.setSecondary():
log.fatal("Failed to set DRBD resource role")
self.SetState("slave")
log.info("We are now slave")
def Shutdown(self):
log.info("Initiating shutdown ...")
self.monitor.pinger.send("shutdown")
self.Disable()