-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathzk-flock
executable file
·397 lines (347 loc) · 11.3 KB
/
zk-flock
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/usr/bin/python2
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012 Tyurin Anton [email protected]
#
# This file is part of Distributed-flock.
#
# Distributed-flock 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; either version 3 of the License, or
# (at your option) any later version.
#
# Combaine 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.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import errno
import json
import logging
import logging.handlers
import optparse
import os
import shlex
import signal
import subprocess
import sys
import time
from functools import partial
from threading import Condition
from distributedflock import Daemon, Zookeeper, pdeathsig
DEFAULT_ZOOKEEPER_LOG_LEVEL = "WARN"
DEFAULT_LOG_LEVEL = "INFO"
DEFAULT_LOGFILE_PATH = "/dev/null"
logger = logging.getLogger("zk-flock")
def initialize_logger(path, level):
level = getattr(logging, level.upper(), logging.ERROR)
_format = logging.Formatter(
"%(asctime)s %(levelname)-8s" "%(process)d %(message)s", "%Y-%m-%d %H:%M:%S"
)
app_log = logging.getLogger("zk-flock")
lhandler = logging.handlers.WatchedFileHandler(path, mode="a")
lhandler.setFormatter(_format)
lhandler.setLevel(level)
app_log.addHandler(lhandler)
app_log.setLevel(level)
app_log.info("Logger has been initialized successfully")
def start_child(cmd, pdeathsig_func=None):
args = shlex.split(cmd)
try:
p = subprocess.Popen(args, close_fds=True, preexec_fn=pdeathsig_func)
except OSError as err:
logger.error("Unable to start child process, because of %s", err)
sys.exit(1)
except ValueError as err:
logger.error("ValueError: %s", err)
sys.exit(1)
else:
logger.info("Start subprocess: %s (PID: %d)", cmd, p.pid)
return p
def kill_child(prcs):
# Detach SIGCHLD handler, as we will kill it manually
signal.signal(signal.SIGCHLD, signal.SIG_IGN)
if prcs.poll() is not None:
logger.info("Child exited with code: %d (PID: %d)", prcs.returncode, prcs.pid)
return prcs.returncode
elif prcs.poll() is None:
try:
logger.info("Send SIGTERM to child process (PID: %d)", prcs.pid)
prcs.terminate()
time.sleep(1)
prcs.kill()
logger.info("Send SIGKILL to child process (PID: %d)", prcs.pid)
except OSError as err:
if err.args[0] != errno.ESRCH:
# Trying to kill child two ways: kill and kill -9.
# Kill -9 may fail with no such process,
# so it should be ignored.
logger.error("Kill child error: %s", err)
else:
logger.info("Killed child %d successfully", prcs.pid)
def read_cfg(path):
try:
with open(path) as f:
cfg = json.load(f)
cfg["host"]
cfg["app_id"]
cfg["timeout"]
logger_config = cfg.pop("logger", {})
cfg["path"] = logger_config.get("path", DEFAULT_LOGFILE_PATH)
cfg["ZookeeperLog"] = (
logger_config.get("path", DEFAULT_LOGFILE_PATH),
logger_config.get("zklevel", DEFAULT_ZOOKEEPER_LOG_LEVEL),
)
cfg["level"] = logger_config.get("level", DEFAULT_LOG_LEVEL)
cfg["logger_name"] = "zk-flock"
except KeyError as err:
print("Missing parametr %s" % str(err))
except Exception as err:
print("Config error %s" % str(err))
else:
return cfg
def get_la():
return os.getloadavg()[0]
def notifier(cv):
with cv:
cv.notify()
def sigterm_and_sigchld_handle(process, minlocktime, z, signum, frame):
time.sleep(minlocktime)
# TBD - split by two handlers for each signal
if signum == signal.SIGTERM:
logger.info("Stop work by SIGTERM")
elif signum == signal.SIGCHLD:
logger.info("Stop work by SIGCHLD")
else:
logger.info("Receive SIGNAL: %d", signum)
try:
z.destroy()
except Exception as err:
logger.debug(str(err))
finally:
returncode = kill_child(process)
if returncode is not None:
# Means that child has ended work and return some sode
# That's for SIGCHLD
sys.exit(returncode)
else:
# Means we kill our child manualy
# That's true for SIGTERM
sys.exit(1)
# ToDo: accept options as the last argument
def main(
cmd_arg, zk_cfg, period=None, exitcode=0, sequence=0, pdeathsig_num=0, minlocktime=5
):
try:
z = Zookeeper.ZKLockServer(**cfg)
except Exception as err:
logger.exception("%s", err)
print(err)
sys.exit(1)
# sequnce lock
if sequence > 0:
logger.debug("Sequence lock %d", sequence)
base_lock_name = cfg["name"]
for i in range(0, sequence):
sequence_lock_name = "%s_%d" % (base_lock_name, i)
z.set_lock_name(sequence_lock_name)
# do NOT remove this print
print(sequence_lock_name)
if not z.getlock():
logger.debug("Lock %s is busy", sequence_lock_name)
continue
else:
break
if not z.check_lock():
logger.debug("Unable to acquire lock. Do exit")
sys.exit(exitcode)
# unique lock
else:
if not z.getlock():
if period is None:
logger.debug("Unable to acquire lock. Do exit")
sys.exit(exitcode)
# trying to acquire the lock during timeout
logger.info("Try to wait %d sec" % period)
limit_time = time.time() + period
cond_var = Condition()
watcher = partial(notifier, cond_var)
while limit_time - time.time() > 0.1:
time_to_wait = limit_time - time.time()
if not z.set_node_deleting_watcher(z.lockpath, watcher):
logger.error("unable to attach delete watcher")
break
with cond_var:
cond_var.wait(time_to_wait)
if z.getlock():
break
if not z.check_lock():
logger.debug("Unable to acquire lock. Do exit")
sys.exit(exitcode)
# attach watcher to the lock file
async_locker_cv = Condition()
async_check_lock_cb = partial(notifier, async_locker_cv)
if not z.set_async_check_lock(async_check_lock_cb):
logger.error("Unable to attach async watcher for lock")
sys.exit(1)
preexec_func = None
if pdeathsig.support_pdeathsig():
preexec_func = partial(pdeathsig.set_pdeathsig, pdeathsig_num)
process = start_child(cmd_arg, preexec_func)
# capture process, minilocktime, and ZKlockServer
signal_handler = partial(sigterm_and_sigchld_handle, process, minlocktime, z)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGCHLD, signal_handler)
# check if the child is still alive, as we attached SIGCHLD handler
if process.poll() is not None:
logger.error("Child process exited")
sys.exit(1)
# wait
while True:
with async_locker_cv:
async_locker_cv.wait(100)
if not z.check_lock():
logger.warning("Lock lost")
try:
kill_child(process)
z.destroy()
except Exception:
pass
finally:
sys.exit(1)
if __name__ == "__main__":
usage = "Usage: %prog LOCKNAME COMMAND [-cdhsl]"
parser = optparse.OptionParser(usage)
parser.add_option(
"-c",
"--confpath",
action="store",
dest="confpath",
default="/etc/distributed-flock.json",
help="Configuration file (/etc/distributed-flock.json)",
)
parser.add_option(
"-d",
"--daemonize",
action="store_true",
dest="isdaemonize",
default=False,
help="Daemonize this",
)
parser.add_option(
"-m",
"--maxla",
action="store",
dest="maxla",
default=-1,
help="Maximum load average to start working (-1).",
)
parser.add_option(
"-s",
"--sleep",
action="store_true",
dest="want_to_sleep",
default=False,
help="Yes, I want to sleep before work",
)
parser.add_option(
"-w",
"--wait",
action="store",
type=float,
dest="waittime",
default=None,
help="Try to acquire lock for some seconds",
)
parser.add_option(
"-x",
"--exitcode",
action="store",
type=int,
dest="exitcode",
default=0,
help="Exit code if lock isn't acquired (0).",
)
parser.add_option(
"-n",
"",
action="store",
type=int,
dest="sequence",
default=0,
help="Sequence of locks",
)
parser.add_option(
"-l",
"--minlocktime",
action="store",
type=int,
dest="minlocktime",
default=5,
help="Minimum time for lock",
)
if pdeathsig.support_pdeathsig():
parser.add_option(
"-p",
"--pdeathsig",
action="store",
type=int,
dest="pdeathsig",
default=signal.SIGTERM,
help="signal that is sent to a child process "
"if the parent dies (default SIGTERM)",
)
(options, args) = parser.parse_args()
if len(args) == 2:
pid_name, cmd_arg = args
else:
print("Invalid number of arguments")
parser.print_help()
sys.exit(1)
cfg = read_cfg(options.confpath)
if cfg is None:
print("Unable to read configuration file: %s" % options.confpath)
sys.exit(1)
try:
initialize_logger(cfg["path"], cfg["level"])
except Exception as err:
print("Couldn't initialize log file %s" % err)
cfg["name"] = pid_name # lockname
# Sleep
want_to_sleep = cfg.get("sleep", "ON" if options.want_to_sleep else "OFF")
if want_to_sleep not in ("ON", "OFF"):
logger.info("Available values for sleep are ON/OFF, not a %s", want_to_sleep)
sys.exit(1)
# Read maxla
maxla = cfg.get("maxla", options.maxla)
if 0 < maxla <= get_la():
logger.info("la is more then limit. Max la %f, current la %f", maxla, get_la())
sys.exit(1)
if want_to_sleep == "ON":
time.sleep(get_la())
pdeathsig_num = getattr(options, "pdeathsig", 0)
if options.isdaemonize:
daemon = Daemon()
daemon.run = main
daemon.start(
cmd_arg,
cfg,
options.waittime,
options.exitcode,
options.sequence,
pdeathsig_num,
options.minlocktime,
)
else:
main(
cmd_arg,
cfg,
options.waittime,
options.exitcode,
options.sequence,
pdeathsig_num,
options.minlocktime,
)