Permalink
Browse files

improved logging + Hub,Engine,Scheduler are Configurable

  • Loading branch information...
1 parent 8554e33 commit 600411fc4546d469bae7c722f144b968d8a08959 @minrk minrk committed Feb 2, 2011
View
@@ -21,7 +21,3 @@ def root_topic(self):
else:
return "engine"
-
-logger = logging.getLogger('ipzmq')
-logger.setLevel(logging.DEBUG)
-
@@ -90,11 +90,6 @@ def defaultblock(f, self, *args, **kwargs):
# Classes
#--------------------------------------------------------------------------
-class AbortedTask(object):
- """A basic wrapper object describing an aborted task."""
- def __init__(self, msg_id):
- self.msg_id = msg_id
-
class ResultDict(dict):
"""A subclass of dict that raises errors if it has them."""
def __getitem__(self, key):
@@ -332,10 +327,10 @@ def connect_socket(s, addr):
msg = ss.Message(msg)
content = msg.content
if content.status == 'ok':
- if content.queue:
+ if content.mux:
self._mux_socket = self.context.socket(zmq.PAIR)
self._mux_socket.setsockopt(zmq.IDENTITY, self.session.session)
- connect_socket(self._mux_socket, content.queue)
+ connect_socket(self._mux_socket, content.mux)
if content.task:
self._task_socket = self.context.socket(zmq.PAIR)
self._task_socket.setsockopt(zmq.IDENTITY, self.session.session)
@@ -17,6 +17,7 @@
import os
import time
+import logging
from multiprocessing import Process
import zmq
@@ -29,7 +30,8 @@
from hub import Hub
from entry_point import (make_base_argument_parser, select_random_ports, split_ports,
- connect_logger, parse_url, signal_children, generate_exec_key)
+ connect_logger, parse_url, signal_children, generate_exec_key,
+ local_logger)
import streamsession as session
@@ -118,8 +120,6 @@ def main(argv=None):
ctx = zmq.Context()
loop = ioloop.IOLoop.instance()
- # setup logging
- connect_logger(ctx, iface%args.logport, root="controller", loglevel=args.loglevel)
# Registrar socket
reg = ZMQStream(ctx.socket(zmq.XREP), loop)
@@ -207,7 +207,9 @@ def main(argv=None):
q.start()
children.append(q.launcher)
else:
- sargs = (iface%task[0],iface%task[1],iface%monport,iface%nport,args.scheduler)
+ log_addr = iface%args.logport if args.logport else None
+ sargs = (iface%task[0], iface%task[1], iface%monport, iface%nport,
+ log_addr, args.loglevel, args.scheduler)
print (sargs)
q = Process(target=launch_scheduler, args=sargs)
q.daemon=True
@@ -224,7 +226,7 @@ def main(argv=None):
# build connection dicts
engine_addrs = {
'control' : iface%control[1],
- 'queue': iface%mux[1],
+ 'mux': iface%mux[1],
'heartbeat': (iface%hb[0], iface%hb[1]),
'task' : iface%task[1],
'iopub' : iface%iopub[1],
@@ -234,15 +236,24 @@ def main(argv=None):
client_addrs = {
'control' : iface%control[0],
'query': iface%cport,
- 'queue': iface%mux[0],
+ 'mux': iface%mux[0],
'task' : iface%task[0],
'iopub' : iface%iopub[0],
'notification': iface%nport
}
+ # setup logging
+ if args.logport:
+ connect_logger(ctx, iface%args.logport, root="controller", loglevel=args.loglevel)
+ else:
+ local_logger(args.loglevel)
+
# register relay of signals to the children
signal_children(children)
- hub = Hub(loop, thesession, sub, reg, hmon, c, n, db, engine_addrs, client_addrs)
+ hub = Hub(loop=loop, session=thesession, monitor=sub, heartmonitor=hmon,
+ registrar=reg, clientele=c, notifier=n, db=db,
+ engine_addrs=engine_addrs, client_addrs=client_addrs)
+
dc = ioloop.DelayedCallback(lambda : print("Controller started..."), 100, loop)
dc.start()
loop.start()
@@ -8,49 +8,48 @@
import time
import traceback
import uuid
+import logging
from pprint import pprint
import zmq
from zmq.eventloop import ioloop, zmqstream
-from IPython.utils.traitlets import HasTraits
-from IPython.utils.localinterfaces import LOCALHOST
+# internal
+from IPython.config.configurable import Configurable
+from IPython.utils.traitlets import Instance, Str, Dict
+# from IPython.utils.localinterfaces import LOCALHOST
from streamsession import Message, StreamSession
-from client import Client
from streamkernel import Kernel, make_kernel
import heartmonitor
-from entry_point import make_base_argument_parser, connect_logger, parse_url
+from entry_point import (make_base_argument_parser, connect_engine_logger, parse_url,
+ local_logger)
# import taskthread
-# from log import logger
-
+logger = logging.getLogger()
def printer(*msg):
- pprint(msg, stream=sys.__stdout__)
+ # print (logger.handlers, file=sys.__stdout__)
+ logger.info(str(msg))
-class Engine(object):
+class Engine(Configurable):
"""IPython engine"""
- id=None
- context=None
- loop=None
- session=None
- ident=None
- registrar=None
- heart=None
kernel=None
- user_ns=None
+ id=None
- def __init__(self, context, loop, session, registrar, client=None, ident=None,
- heart_id=None, user_ns=None):
- self.context = context
- self.loop = loop
- self.session = session
- self.registrar = registrar
- self.client = client
- self.ident = ident if ident else str(uuid.uuid4())
+ # configurables:
+ context=Instance(zmq.Context)
+ loop=Instance(ioloop.IOLoop)
+ session=Instance(StreamSession)
+ ident=Str()
+ registrar=Instance(zmqstream.ZMQStream)
+ user_ns=Dict()
+
+ def __init__(self, **kwargs):
+ super(Engine, self).__init__(**kwargs)
+ if not self.ident:
+ self.ident = str(uuid.uuid4())
self.registrar.on_send(printer)
- self.user_ns = user_ns
def register(self):
@@ -64,9 +63,10 @@ def complete_registration(self, msg):
idents,msg = self.session.feed_identities(msg)
msg = Message(self.session.unpack_message(msg))
if msg.content.status == 'ok':
- self.session.username = str(msg.content.id)
- queue_addr = msg.content.queue
- shell_addrs = [str(queue_addr)]
+ self.id = int(msg.content.id)
+ self.session.username = 'engine-%i'%self.id
+ queue_addr = msg.content.mux
+ shell_addrs = [ str(queue_addr) ]
control_addr = str(msg.content.control)
task_addr = msg.content.task
iopub_addr = msg.content.iopub
@@ -75,7 +75,7 @@ def complete_registration(self, msg):
hb_addrs = msg.content.heartbeat
# ioloop.DelayedCallback(self.heart.start, 1000, self.loop).start()
- k = make_kernel(self.ident, control_addr, shell_addrs, iopub_addr,
+ k = make_kernel(self.id, self.ident, control_addr, shell_addrs, iopub_addr,
hb_addrs, client_addr=None, loop=self.loop,
context=self.context, key=self.session.key)[-1]
self.kernel = k
@@ -84,20 +84,20 @@ def complete_registration(self, msg):
self.kernel.user_ns = self.user_ns
else:
- # logger.error("Registration Failed: %s"%msg)
+ logger.error("Registration Failed: %s"%msg)
raise Exception("Registration Failed: %s"%msg)
- # logger.info("engine::completed registration with id %s"%self.session.username)
+ logger.info("completed registration with id %i"%self.id)
- print (msg,file=sys.__stdout__)
+ # logger.info(str(msg))
def unregister(self):
self.session.send(self.registrar, "unregistration_request", content=dict(id=int(self.session.username)))
time.sleep(1)
sys.exit(0)
def start(self):
- print ("registering",file=sys.__stdout__)
+ logger.info("registering")
self.register()
@@ -118,7 +118,6 @@ def main(argv=None, user_ns=None):
ctx = zmq.Context()
# setup logging
- connect_logger(ctx, iface%args.logport, root="engine", loglevel=args.loglevel)
reg_conn = iface % args.regport
print (reg_conn, file=sys.__stdout__)
@@ -127,10 +126,16 @@ def main(argv=None, user_ns=None):
reg = ctx.socket(zmq.PAIR)
reg.connect(reg_conn)
reg = zmqstream.ZMQStream(reg, loop)
- client = None
- e = Engine(ctx, loop, session, reg, client, args.ident, user_ns=user_ns)
- dc = ioloop.DelayedCallback(e.start, 100, loop)
+ e = Engine(context=ctx, loop=loop, session=session, registrar=reg,
+ ident=args.ident or '', user_ns=user_ns)
+ if args.logport:
+ print ("connecting logger to %s"%(iface%args.logport), file=sys.__stdout__)
+ connect_engine_logger(ctx, iface%args.logport, e, loglevel=args.loglevel)
+ else:
+ local_logger(args.loglevel)
+
+ dc = ioloop.DelayedCallback(e.start, 0, loop)
dc.start()
loop.start()
@@ -22,7 +22,7 @@
# Local imports.
from IPython.core.ultratb import FormattedTB
from IPython.external.argparse import ArgumentParser
-from IPython.zmq.log import logger
+from IPython.zmq.log import EnginePUBHandler
def split_ports(s, n):
"""Parser helper for multiport strings"""
@@ -82,17 +82,18 @@ def make_base_argument_parser():
""" Creates an ArgumentParser for the generic arguments supported by all
ipcluster entry points.
"""
+
parser = ArgumentParser()
parser.add_argument('--ip', type=str, default='127.0.0.1',
help='set the controller\'s IP address [default: local]')
parser.add_argument('--transport', type=str, default='tcp',
help='set the transport to use [default: tcp]')
parser.add_argument('--regport', type=int, metavar='PORT', default=10101,
help='set the XREP port for registration [default: 10101]')
- parser.add_argument('--logport', type=int, metavar='PORT', default=20202,
- help='set the PUB port for logging [default: 10201]')
- parser.add_argument('--loglevel', type=str, metavar='LEVEL', default=logging.DEBUG,
- help='set the log level [default: DEBUG]')
+ parser.add_argument('--logport', type=int, metavar='PORT', default=0,
+ help='set the PUB port for remote logging [default: log to stdout]')
+ parser.add_argument('--loglevel', type=str, metavar='LEVEL', default=logging.INFO,
+ help='set the log level [default: INFO]')
parser.add_argument('--ident', type=str,
help='set the ZMQ identity [default: random]')
parser.add_argument('--packer', type=str, default='json',
@@ -105,17 +106,42 @@ def make_base_argument_parser():
return parser
-
-def connect_logger(context, iface, root="ip", loglevel=logging.DEBUG):
+def integer_loglevel(loglevel):
try:
loglevel = int(loglevel)
except ValueError:
if isinstance(loglevel, str):
loglevel = getattr(logging, loglevel)
+ return loglevel
+
+def connect_logger(context, iface, root="ip", loglevel=logging.DEBUG):
+ loglevel = integer_loglevel(loglevel)
lsock = context.socket(zmq.PUB)
lsock.connect(iface)
handler = handlers.PUBHandler(lsock)
handler.setLevel(loglevel)
handler.root_topic = root
+ logger = logging.getLogger()
+ logger.addHandler(handler)
+ logger.setLevel(loglevel)
+
+def connect_engine_logger(context, iface, engine, loglevel=logging.DEBUG):
+ logger = logging.getLogger()
+ loglevel = integer_loglevel(loglevel)
+ lsock = context.socket(zmq.PUB)
+ lsock.connect(iface)
+ handler = EnginePUBHandler(engine, lsock)
+ handler.setLevel(loglevel)
+ logger.addHandler(handler)
+ logger.setLevel(loglevel)
+
+def local_logger(loglevel=logging.DEBUG):
+ loglevel = integer_loglevel(loglevel)
+ logger = logging.getLogger()
+ if logger.handlers:
+ # if there are any handlers, skip the hookup
+ return
+ handler = logging.StreamHandler()
+ handler.setLevel(loglevel)
logger.addHandler(handler)
-
+ logger.setLevel(loglevel)
@@ -7,13 +7,13 @@
from __future__ import print_function
import time
import uuid
+import logging
import zmq
from zmq.devices import ProcessDevice,ThreadDevice
from zmq.eventloop import ioloop, zmqstream
-#internal
-from IPython.zmq.log import logger
+logger = logging.getLogger()
class Heart(object):
"""A basic heart object for responding to a HeartMonitor.
@@ -53,6 +53,7 @@ class HeartMonitor(object):
hearts=None
on_probation=None
last_ping=None
+ # debug=False
def __init__(self, loop, pingstream, pongstream, period=1000):
self.loop = loop
@@ -84,19 +85,6 @@ def add_heart_failure_handler(self, handler):
"""add a new handler for heart failure"""
logger.debug("heartbeat::new heart failure handler: %s"%handler)
self._failure_handlers.add(handler)
-
- # def _flush(self):
- # """override IOLoop triggers"""
- # while True:
- # try:
- # msg = self.pongstream.socket.recv_multipart(zmq.NOBLOCK)
- # logger.warn("IOLoop triggered beat with incoming heartbeat waiting to be handled")
- # except zmq.ZMQError:
- # return
- # else:
- # self.handle_pong(msg)
- # # print '.'
- #
def beat(self):
self.pongstream.flush()
@@ -105,7 +93,7 @@ def beat(self):
toc = time.time()
self.lifetime += toc-self.tic
self.tic = toc
- logger.debug("heartbeat::%s"%self.lifetime)
+ # logger.debug("heartbeat::%s"%self.lifetime)
goodhearts = self.hearts.intersection(self.responses)
missed_beats = self.hearts.difference(goodhearts)
heartfailures = self.on_probation.intersection(missed_beats)
@@ -144,7 +132,7 @@ def handle_pong(self, msg):
"a heart just beat"
if msg[1] == str(self.lifetime):
delta = time.time()-self.tic
- logger.debug("heartbeat::heart %r took %.2f ms to respond"%(msg[0], 1000*delta))
+ # logger.debug("heartbeat::heart %r took %.2f ms to respond"%(msg[0], 1000*delta))
self.responses.add(msg[0])
elif msg[1] == str(self.last_ping):
delta = time.time()-self.tic + (self.lifetime-self.last_ping)
Oops, something went wrong.

0 comments on commit 600411f

Please sign in to comment.