This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalmanager.py
331 lines (251 loc) · 10.3 KB
/
localmanager.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
from kazoo.client import KazooClient
import kazoo.exceptions
import threading
import os
import sys
import signal
import socket
import time
import ConfigParser
import redis
import logging
import threading
import ftmemfs.etc.config
import pickle
import traceback
import Queue
from ftmemfs.utils.hrw import *
from ftmemfs.utils.zookeeper_utils import *
from ftmemfs.tasks.redis_tasks import RedisUsageServerTask
from ftmemfs.tasks.general_tasks import *
from ftmemfs.tasks.fsck_tasks import FsckEnqueueTask
from ftmemfs.leader.leader import Leader
def file_content(file):
f = open(file, 'r')
c = f.read()
f.close()
return c.strip()
class ZkStateTask(FtmemfsTask):
def __init__(self, state):
self.state = state
def __repr__(self):
return "ZkStateTask({})".format(self.state)
def execute(self, lm):
print "Current state is: {}".format(self.state)
if self.state == "CONNECTED":
msg = "Localmanager {} connected, ZK-host: {}".format(lm.ip, lm.zk.command("envi").split("\n")[2].split("=")[1].split(".")[0])
print msg
lm.add_log_entry("INFO", msg)
if lm.zk_state == "SUSPENDED" and lm.potential_leader:
print "We could become the leader now, check!"
lm.queue.put((10, CheckLeaderTask(lm.zk.get("/leaders")[0])))
if self.state == "LOST":
print "Connection lost, stop!"
sys.exit(0)
lm.zk_state = self.state
class LocalManager(object):
def signal_handler(self, signum, frame):
'''Signal handler to make sure ctrl-c stops everything'''
print "Got ctrl+c, stopping"
self.zk.stop()
if self.redis_host:
self.redis_usage_server.stop()
self.redis_usage_server.join()
sys.exit(0)
def __init__(self):
'''Initialise localmanager'''
self.zk_state = None
self.ldr = None
# Perform the following steps:
# - Register signal handler
# - Read configuration and determine IP address
# - Connect to zookeeper
# - Check whether this node is already running or not in the services
# - Create tasks according to the services
# - Start the queue listener
#
signal.signal(signal.SIGINT, self.signal_handler)
self.queue = Queue.PriorityQueue()
self.config = ConfigParser.ConfigParser()
self.config.read("{}/ftmemfs/config.ini".format(os.getenv("HOME")))
self.config.read("{}/ftmemfs/config_platform.ini".format(os.getenv("HOME")))
# TODO retrieve from interface
self.ip = self.config.get("general","ip_prefix")
self.ip += "{}".format(int(socket.gethostname()[4:]))
# When locally running zookeeper, only connect to local zk
zk_hosts = sys.argv[1]
if "{}:2181".format(self.ip) in zk_hosts.split(','):
zk_hosts = "{}:2181".format(self.ip)
self.zk = KazooClient(zk_hosts, timeout = 2.0, connection_retry=kazoo.retry.KazooRetry(max_tries=5, max_delay=3600))
#self.zk.add_listener(self.kazoo_listener)
self.zk.add_listener(lambda s: self.queue.put((5, ZkStateTask(s))))
self.zk.start()
# self.fsck_lock = threading.Lock()
self.fscks_handled = []
self.leader_lock = threading.Lock()
self.my_leader_path = None
self.redises = {}
if not self.zk.exists("/services/{}".format(self.ip)):
self.add_log_entry("ERROR", "Node {} not in zookeeper database".format(self.ip))
raise Exception("Node {} not in zookeeper database".format(self.ip))
if self.zk.exists("/online_hosts/{}".format(self.ip)):
self.add_log_entry("ERROR", "Localmanager already running")
raise Exception("Localmanager already running?!")
self.zk.create("/online_hosts/{}".format(self.ip), ephemeral=True)
self.potential_leader = self.zk.exists("/services/{}/zookeeper".format(self.ip)) is not None
self.redis_host = self.zk.exists("/services/{}/redis".format(self.ip)) is not None
self.initial_leader = False
# Leaders initially do not redis
if self.potential_leader and is_zookeeper_leader(self.zk):
self.initial_leader = True
if self.redis_host:
self.zk.delete("/services/{}/redis".format(self.ip))
self.redis_host = False
# TODO: check redis_host...
self.redis_seq = None
if self.redis_host or self.get_redis_seq() != None:
self.redis_seq = self.check_redis(self.ip)
print "Redis self seq: {}".format(self.redis_seq)
self.queue.put((5, RedisUsageServerTask()))
self.worker = None
self.is_worker = False
self.leader = None
self.legacy = self.zk.exists("/legacy")
if self.zk.exists("/services/{}/worker".format(self.ip)):
self.zk.create("/workers/{}".format(self.ip), "", ephemeral=True)
if self.legacy:
time.sleep(30)
os.system("{}/mount_legacy.sh {}".format(self.config.get("general","scripts_dir"),self.config.get("worker","mount")))
else:
os.system("{}/mount_ftmemfs.sh {} /workers/{} {}".format(self.config.get("general","scripts_dir"),self.config.get("worker","mount"),self.ip, sys.argv[1]))
os.system("mkdir -p " + ftmemfs.etc.config.LOGDIR)
self.is_worker = True
self.zk.ChildrenWatch("/leaders", lambda s : self.queue.put((10,CheckLeaderTask(s))))
if not self.legacy and self.redis_seq is not None:
self.zk.ChildrenWatch("/fsck", lambda c : self.queue.put((50, FsckEnqueueTask(c))))
def queue_worker(self):
'''Queue worker'''
while True:
task = self.queue.get()[1]
print "Got from queue: {}".format(task)
task.execute(self)
self.queue.task_done()
def add_log_entry(self, severity, message):
'''Add a log to ZooKeeper for debugging/info purposes'''
log = {"time": time.time(), "ip": self.ip, "severity": severity, "message": message }
try:
self.zk.create("/logs/log", value=pickle.dumps(log), sequence=True)
except:
print "Could not create log entry"
def run(self):
self.queue_t = threading.Thread(target = self.queue_worker)
self.queue_t.daemon = True
self.queue_t.start()
while True:
time.sleep(1)
if self.redis_host and not self.is_redis_running():
self.add_log_entry("ERROR", "Redis unavailable, stopping localmanager")
break
# if self.worker:
# if not os.path.ismount('/local/rdevries/ftmemfs'):
# print "FTMEMFS crashed :-("
# break
print "Stopping"
try:
self.zk.stop()
if self.worker:
self.worker.shutdown()
self.worker.join()
if self.redis_host:
self.redis_usage_server.stop()
self.redis_usage_server.join()
except:
pass
sys.exit(0)
def start_leader(self):
'''This node is the leader, stop the worker node and start the
coordination thread'''
self.add_log_entry("INFO", "Leader")
if self.is_worker:
self.is_worker = False
# Unmount fuse!
self.zk.delete("/workers/{}".format(self.ip))
thr = None
if self.worker != None:
print "Stopping worker ZMQ!"
thr = self.worker.sched_client_thread
thr.stop()
self.worker.sched_client_thread = None
time.sleep(2)
if not self.initial_leader:
print "Block FUSE!"
self.zk.ensure_path("/fuse_block")
self.zk.create("/services/{}/leader".format(self.ip), "", ephemeral=True)
leader = Leader(self)
self.ldr = leader
leader.run()
print "We should stop the current worker!"
if self.worker != None:
thr.join()
self.worker.shutdown()
self.worker.join()
def is_redis_running(self):
r = redis.StrictRedis()
try:
response = r.ping()
except redis.ConnectionError:
return False
return True
def get_redis_seq(self):
'''Retrieve the redis sequence number from the redis server running
locally.'''
r = redis.StrictRedis()
try:
return r.get("ftmemfs_redis_id")
except redis.ConnectionError:
pass
return None
def set_redis_seq(self,id):
'''Update the redis sequence number on the redis server running
locally.'''
r = redis.StrictRedis()
try:
r.set("ftmemfs_redis_id","{}".format(id))
except redis.ConnectionError:
pass
def check_redis(self, ip):
'''Check whether redis is running and start redis if it is not.'''
# print "Check whether redis is running"
if self.is_redis_running():
# print "Redis already running"
# This should not be necessary, testing purposes only
for c in self.zk.get_children("/redis"):
d = self.zk.get("/redis/" + c)[0]
if d.split(',')[1] == self.ip and d.split(',')[0] != -1:
return int(c[5:])
return
# else:
# print "Redis not already running"
port = self.config.get("redis", "port")
os.system("{}/start_redis.sh {} {} {} {}".format(
self.config.get("general", "scripts_dir"),
port,
self.config.get("redis", "dir"),
self.config.get("redis", "bin"),
self.config.get("redis", "config_base")
))
nodeset,_ = self.zk.get("/services/{}/redis".format(ip))
seq = self.zk.create("/redis/redis", "{},{},{}".format(nodeset, ip, port), sequence=True)
self.set_redis_seq(int(seq[12:]))
return int(seq[12:])
if __name__ == "__main__":
logging.basicConfig()
if len(sys.argv) < 2:
print "Invalid number of arguments"
sys.exit()
try:
lm = LocalManager()
lm.run()
except Exception as e:
print "Something went wrong: {}".format(e)
raise