forked from sagemath/sagecell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel_provider.py
executable file
·306 lines (270 loc) · 10.7 KB
/
kernel_provider.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
#! /usr/bin/env python
r"""
Kernel Provider starts compute kernels and sends connection info to Dealer.
"""
import argparse
import errno
from multiprocessing import Process
import os
import resource
import signal
import sys
import time
import uuid
from ipykernel.kernelapp import IPKernelApp
import zmq
import kernel_init
import log
logger = log.provider_logger.getChild(str(os.getpid()))
class KernelProcess(Process):
"""
Kernel from the provider point of view.
Configures a kernel process and does its best at cleaning up.
"""
def __init__(self, id, rlimits, dir, waiter_port):
super(KernelProcess, self).__init__()
self.id = id
self.rlimits = rlimits
self.dir = dir
self.waiter_port = waiter_port
def run(self):
global logger
logger = log.kernel_logger.getChild(str(os.getpid()))
logger.debug("forked kernel is running")
log.std_redirect(logger)
# Become a group leader for cleaner exit.
os.setpgrp()
dir = os.path.join(self.dir, self.id)
try:
os.mkdir(dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
os.chdir(dir)
#config = traitlets.config.loader.Config({"ip": self.ip})
#config.HistoryManager.enabled = False
app = IPKernelApp.instance(log=logger)
from namespace import InstrumentedNamespace
app.user_ns = InstrumentedNamespace()
app.initialize([]) # Redirects stdout/stderr
#log.std_redirect(logger) # Uncomment for debugging
# This function should be called via atexit, but it isn't, perhaps due
# to forking. Stale connection files do cause problems.
app.cleanup_connection_file()
kernel_init.initialize(app.kernel)
for r, limit in self.rlimits.items():
resource.setrlimit(getattr(resource, r), (limit, limit))
logger.debug("kernel ready")
context = zmq.Context.instance()
socket = context.socket(zmq.PUSH)
socket.connect("tcp://localhost:{}".format(self.waiter_port))
socket.send_json({
"id": self.id,
"connection": {
"key": app.session.key.decode(),
"ip": app.ip,
"hb": app.hb_port,
"iopub": app.iopub_port,
"shell": app.shell_port,
},
"rlimits": self.rlimits,
})
def signal_handler(signum, frame):
logger.info("received %s, shutting down", signum)
# TODO: this may not be the best way to do it.
app.kernel.do_shutdown(False)
signal.signal(signal.SIGTERM, signal_handler)
app.start()
logger.debug("Kernel.run finished")
class KernelProvider(object):
r"""
Kernel Provider handles compute kernels on the worker side.
"""
def __init__(self, dealer_address, dir):
self.is_active = False
self.dir = dir
try:
os.mkdir(dir)
logger.warning("created parent directory for kernels, "
"consider doing it yourself with appropriate attributes")
except OSError as e:
if e.errno != errno.EEXIST:
raise
context = zmq.Context()
context.IPV6 = 1
self.dealer = context.socket(zmq.DEALER)
logger.debug("connecting to %s", address)
self.dealer.connect(address)
self.dealer.send_json("get settings")
if not self.dealer.poll(5000):
logger.debug("dealer does not answer, terminating")
exit(1)
reply = self.dealer.recv_json()
logger.debug("received %s", reply)
assert reply[0] == "settings"
self.preforked_rlimits = reply[1].pop("preforked_rlimits")
self.max_kernels = reply[1].pop("max_kernels")
self.max_preforked = reply[1].pop("max_preforked")
self.waiter = context.socket(zmq.PULL)
self.waiter_port = self.waiter.bind_to_random_port("tcp://*")
self.kernels = dict() # id: KernelProcess
self.forking = None
self.preforking = None
self.preforked = []
self.ready_sent = False
self.to_kill = []
setup_sage()
def fork(self, rlimits):
r"""
Start a new kernel by forking.
INPUT:
- ``rlimits`` - dictionary with keys ``resource.RLIMIT_*``
OUTPUT:
- ID of the forked kernel
"""
logger.debug("fork with rlimits %s", rlimits)
id = str(uuid.uuid4())
kernel = KernelProcess(id, rlimits, self.dir, self.waiter_port)
kernel.start()
self.kernels[id] = kernel
return id
def kill_check(self):
"""
Kill old kernels.
"""
to_kill = []
for kernel in self.to_kill:
if kernel.is_alive():
if time.time() < kernel.deadline:
to_kill.append(kernel)
continue
else:
logger.warning(
"kernel process %d did not stop by deadline",
kernel.pid)
try:
# Kernel PGID is the same as PID
os.killpg(kernel.pid, signal.SIGKILL)
except OSError as e:
if e.errno != errno.ESRCH:
raise
logger.debug("killed kernel process group %d", kernel.pid)
self.to_kill = to_kill
def send_kernel(self, msg):
self.dealer.send_json(["kernel", msg])
def start(self):
self.is_active = True
poller = zmq.Poller()
poller.register(self.dealer, zmq.POLLIN)
poller.register(self.waiter, zmq.POLLIN)
while self.is_active:
# For pretty red lines in the log
#logger.error("%s %s %s",
# self.forking, self.preforking, self.to_kill)
# Tell the dealer if we are ready.
if (not self.ready_sent
and self.forking is None
and (self.preforked or len(self.kernels) < self.max_kernels)):
self.dealer.send_json("ready")
self.ready_sent = True
# Kill old kernel process groups.
self.kill_check()
# Process requests from the dealer ...
events = dict(poller.poll(100))
if self.dealer in events:
msg = self.dealer.recv_json()
logger.debug("received %s", msg)
if msg == "disconnect":
self.stop()
if msg[0] == "get":
# We expect a single "get" for every "ready" sent.
self.ready_sent = False
if msg[1] == self.preforked_rlimits and self.preforked:
self.send_kernel(self.preforked.pop(0))
logger.debug("%d preforked kernels left",
len(self.preforked))
elif msg[1] == self.preforked_rlimits and self.preforking:
self.forking = self.preforking
self.preforking = None
else:
if len(self.kernels) == self.max_kernels:
logger.warning("killing a preforked kernel to "
"provide a special one")
self.stop_kernel(self.preforked.pop(0)["id"])
self.forking = self.fork(msg[1])
if msg[0] == "stop":
self.stop_kernel(msg[1])
# ... and connection info from kernels.
if self.waiter in events:
msg = self.waiter.recv_json()
if self.forking == msg["id"]:
self.send_kernel(msg)
self.forking = None
if self.preforking == msg["id"]:
self.preforked.append(msg)
self.preforking = None
# Prefork more standard kernels.
if (not (self.forking or self.preforking)
and len(self.preforked) < self.max_preforked
and len(self.kernels) < self.max_kernels):
self.preforking = self.fork(self.preforked_rlimits)
for id in list(self.kernels):
self.stop_kernel(id)
while self.to_kill:
self.kill_check()
time.sleep(0.1)
def stop(self):
self.is_active = False
def stop_kernel(self, id):
kernel = self.kernels.pop(id)
if kernel.is_alive():
logger.debug("killing kernel process %d", kernel.pid)
os.kill(kernel.pid, signal.SIGTERM)
kernel.deadline = time.time() + 1
self.to_kill.append(kernel)
def setup_sage():
# Non-existing startup file that users cannot create.
os.environ["SAGE_STARTUP_FILE"] = "/init.sage"
import sage
import sage.all
# override matplotlib and pylab show functions
# TODO: use something like IPython's inline backend
def mp_show(savefig):
filename = "%s.png" % uuid.uuid4()
savefig(filename)
msg = {"text/image-filename": filename}
sys._sage_.sent_files[filename] = os.path.getmtime(filename)
sys._sage_.display_message(msg)
from functools import partial
import pylab
pylab.show = partial(mp_show, savefig=pylab.savefig)
import matplotlib.pyplot
matplotlib.pyplot.show = partial(mp_show, savefig=matplotlib.pyplot.savefig)
# The first plot takes about 2 seconds to generate (presumably
# because lots of things, like matplotlib, are imported). We plot
# something here so that worker processes don't have this overhead
try:
sage.all.plot(1, (0, 1))
except Exception:
logger.exception("plotting exception")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Launch a kernel provider for SageMathCell")
parser.add_argument("--address",
help="address of the kernel dealer (defaults to $SSH_CLIENT)")
parser.add_argument("port", type=int,
help="port of the kernel dealer")
parser.add_argument("dir",
help="directory name for user files saved by kernels")
args = parser.parse_args()
log.std_redirect(logger)
address = args.address or os.environ["SSH_CLIENT"].split()[0]
if ":" in address:
address = "[{}]".format(address)
address = "tcp://{}:{}".format(address, args.port)
provider = KernelProvider(address, args.dir)
def signal_handler(signum, frame):
logger.info("received %s, shutting down", signum)
provider.stop()
signal.signal(signal.SIGTERM, signal_handler)
provider.start()