<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[12044] CalendarServer/trunk/twext/application/masterchild.py</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/12044">12044</a></dd>
<dt>Author</dt> <dd>wsanchez@apple.com</dd>
<dt>Date</dt> <dd>2013-12-09 14:31:51 -0800 (Mon, 09 Dec 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>Borrow more from metafd.py</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktwextapplicationmasterchildpy">CalendarServer/trunk/twext/application/masterchild.py</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktwextapplicationmasterchildpy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twext/application/masterchild.py (12043 => 12044)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/twext/application/masterchild.py        2013-12-09 22:01:25 UTC (rev 12043)
+++ CalendarServer/trunk/twext/application/masterchild.py        2013-12-09 22:31:51 UTC (rev 12044)
</span><span class="lines">@@ -37,12 +37,14 @@
</span><span class="cx"> import sys
</span><span class="cx"> from os import close, unlink
</span><span class="cx"> from tempfile import mkstemp
</span><ins>+from functools import total_ordering
</ins><span class="cx">
</span><span class="cx"> from zope.interface import implementer
</span><span class="cx">
</span><span class="cx"> from twisted.python.sendmsg import getsockfam
</span><span class="cx"> from twisted.python.usage import Options, UsageError
</span><span class="cx"> from twisted.python.reflect import namedClass
</span><ins>+from twisted.python.util import FancyStrMixin
</ins><span class="cx"> from twisted.application.service import MultiService, Service
</span><span class="cx"> from twisted.application.service import IServiceMaker
</span><span class="cx"> from twisted.application.internet import TCPServer
</span><span class="lines">@@ -166,13 +168,13 @@
</span><span class="cx"> self.dispatcher = InheritedSocketDispatcher(self)
</span><span class="cx">
</span><span class="cx"> # Child Processes
</span><del>- log.info("Setting up master/child spawning service...")
</del><ins>+ self.log.info("Setting up master/child spawning service...")
</ins><span class="cx"> self.spawningService = ChildSpawningService(self.dispatcher)
</span><span class="cx"> self.spawningService.setServiceParent(self)
</span><span class="cx">
</span><span class="cx">
</span><span class="cx"> def addProtocol(self, protocol, port):
</span><del>- log.info(
</del><ins>+ self.log.info(
</ins><span class="cx"> "Setting service for protocol {protocol!r} on port {port}...",
</span><span class="cx"> protocol=protocol, port=port,
</span><span class="cx"> )
</span><span class="lines">@@ -199,56 +201,77 @@
</span><span class="cx"> @staticmethod
</span><span class="cx"> def initialStatus():
</span><span class="cx"> log.info("Status: init")
</span><del>- return ChildStatus(sentCount=0, ackedCount=0)
</del><span class="cx">
</span><ins>+ return ChildStatus()
</ins><span class="cx">
</span><ins>+
</ins><span class="cx"> @staticmethod
</span><span class="cx"> def newConnectionStatus(previousStatus):
</span><span class="cx"> log.info("Status: {0} new".format(previousStatus))
</span><del>- return ChildStatus(
- sentCount=previousStatus.sentCount + 1,
- ackedCount=previousStatus.ackedCount,
- )
</del><span class="cx">
</span><ins>+ return previousStatus + ChildStatus(unacknowledged=1)
</ins><span class="cx">
</span><ins>+
</ins><span class="cx"> @staticmethod
</span><span class="cx"> def statusFromMessage(previousStatus, message):
</span><del>- log.info("Status: {0}{1!r}".format(previousStatus, message))
- if message == b"-":
- return ChildStatus(
- sentCount=previousStatus.sentCount - 1,
- ackedCount=previousStatus.ackedCount,
</del><ins>+ log.info("Status: {0} {1!r}".format(previousStatus, message))
+
+ if message == "-":
+ # A connection has gone away in a subprocess; we should start
+ # accepting connections again if we paused (see
+ # newConnectionStatus)
+ return previousStatus - ChildStatus(acknowledged=1)
+
+ elif message == "0":
+ # A new process just started accepting new connections. It might
+ # still have some unacknowledged connections, but any connections
+ # that it acknowledged working on are now completed. (We have no
+ # way of knowing whether the acknowledged connections were acted
+ # upon or dropped, so we have to treat that number with a healthy
+ # amount of skepticism.)
+ return previousStatus.restarted()
+
+ elif message == "+":
+ # Acknowledges that the subprocess has taken on the work.
+ return (
+ previousStatus +
+ ChildStatus(acknowledged=1, unacknowledged=-1, unclosed=1)
</ins><span class="cx"> )
</span><del>- elif message == b"+":
- return ChildStatus(
- sentCount=previousStatus.sentCount,
- ackedCount=previousStatus.ackedCount + 1,
- )
</del><ins>+
</ins><span class="cx"> else:
</span><del>- raise AssertionError("Unknown message: {}".format(message))
</del><ins>+ raise AssertionError("Unknown message: {0}".format(message))
</ins><span class="cx">
</span><span class="cx">
</span><span class="cx"> @staticmethod
</span><span class="cx"> def closeCountFromStatus(previousStatus):
</span><span class="cx"> log.info("Status: {0} close".format(previousStatus))
</span><del>- return (
- previousStatus.ackedCount,
- ChildStatus(
- sentCount=previousStatus.sentCount,
- ackedCount=0,
- )
- )
</del><span class="cx">
</span><ins>+ toClose = previousStatus.unclosed
+ return (toClose, previousStatus - ChildStatus(unclosed=toClose))
</ins><span class="cx">
</span><del>- @staticmethod
- def statusesChanged(statuses):
- log.info("Status changed: {0}".format(tuple(statuses)))
</del><ins>+
+ def statusesChanged(self, statuses):
</ins><span class="cx"> # FIXME: This isn't in IStatusWatcher, but is called by
</span><span class="cx"> # InheritedSocketDispatcher.
</span><del>- pass
</del><span class="cx">
</span><ins>+ self.log.info("Status changed: {0}".format(tuple(statuses)))
</ins><span class="cx">
</span><ins>+ # current = sum(
+ # status.effective()
+ # for status in self.dispatcher.statuses
+ # )
</ins><span class="cx">
</span><ins>+ # maximum = self.maxRequests
+ # overloaded = (current >= maximum)
+
+ # for f in self.factories:
+ # if overloaded:
+ # f.loadAboveMaximum()
+ # else:
+ # f.loadNominal()
+
+
+
</ins><span class="cx"> @implementer(IServiceMaker)
</span><span class="cx"> class MasterServiceMaker(object):
</span><span class="cx"> """
</span><span class="lines">@@ -496,6 +519,9 @@
</span><span class="cx"> Service for child processes.
</span><span class="cx"> """
</span><span class="cx">
</span><ins>+ log = Logger()
+
+
</ins><span class="cx"> def __init__(self, fd, protocolFactory):
</span><span class="cx"> self.fd = fd
</span><span class="cx"> self.protocolFactory = protocolFactory
</span><span class="lines">@@ -530,7 +556,7 @@
</span><span class="cx">
</span><span class="cx"> factory = self.wrappedProtocolFactory
</span><span class="cx"> factory.inheritedPort.reportStatus("+")
</span><del>- log.info("{factory.inheritedPort.statusQueue}", factory=factory)
</del><ins>+ self.log.info("{factory.inheritedPort.statusQueue}", factory=factory)
</ins><span class="cx">
</span><span class="cx"> socketFD = socket.fileno()
</span><span class="cx"> transport = reactor.adoptStreamConnection(
</span><span class="lines">@@ -543,8 +569,11 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx"> class ReportingProtocolWrapper(ProtocolWrapper, object):
</span><ins>+ log = Logger()
+
+
</ins><span class="cx"> def connectionLost(self, reason):
</span><del>- log.info("CONNECTION LOST")
</del><ins>+ self.log.info("CONNECTION LOST")
</ins><span class="cx"> self.factory.inheritedPort.reportStatus("-")
</span><span class="cx"> return super(ReportingProtocolWrapper, self).connectionLost(reason)
</span><span class="cx">
</span><span class="lines">@@ -559,10 +588,101 @@
</span><span class="cx">
</span><span class="cx">
</span><span class="cx">
</span><del>-class ChildStatus(object):
- def __init__(self, sentCount, ackedCount):
- self.sentCount = sentCount
- self.ackedCount = ackedCount
</del><ins>+@total_ordering
+class ChildStatus(FancyStrMixin, object):
+ """
+ The status of a child process.
+ """
</ins><span class="cx">
</span><del>- def __repr__(self):
- return "({self.sentCount},{self.ackedCount})".format(self=self)
</del><ins>+ showAttributes = (
+ "acknowledged",
+ "unacknowledged",
+ "started",
+ "abandoned",
+ "unclosed",
+ )
+
+
+ def __init__(self, acknowledged=0, unacknowledged=0, started=0,
+ abandoned=0, unclosed=0):
+ """
+ Create a L{ConnectionStatus} with a number of sent connections and a
+ number of un-acknowledged connections.
+
+ @param acknowledged: the number of connections which we know the
+ subprocess to be presently processing; i.e. those which have been
+ transmitted to the subprocess.
+
+ @param unacknowledged: The number of connections which we have sent to
+ the subprocess which have never received a status response (a
+ "C{+}" status message).
+
+ @param abandoned: The number of connections which have been sent to
+ this worker, but were not acknowledged at the moment that the
+ worker restarted.
+
+ @param started: The number of times this worker has been started.
+
+ @param unclosed: The number of sockets which have been sent to the
+ subprocess but not yet closed.
+ """
+ self.acknowledged = acknowledged
+ self.unacknowledged = unacknowledged
+ self.started = started
+ self.abandoned = abandoned
+ self.unclosed = unclosed
+
+
+ def effectiveLoad(self):
+ """
+ The current effective load.
+ """
+ return self.acknowledged + self.unacknowledged
+
+
+ def restarted(self):
+ """
+ The L{ChildStatus} derived from the current status of a process and
+ the fact that it just restarted.
+ """
+ return self.__class__(0, 0, self.started + 1, self.unacknowledged)
+
+
+ def _tuplify(self):
+ return tuple(getattr(self, attr) for attr in self.showAttributes)
+
+
+ def __lt__(self, other):
+ if not isinstance(other, ChildStatus):
+ return NotImplemented
+
+ return self.effectiveLoad() < other.effectiveLoad()
+
+
+ def __eq__(self, other):
+ if not isinstance(other, ChildStatus):
+ return NotImplemented
+
+ return self._tuplify() == other._tuplify()
+
+
+ def __add__(self, other):
+ if not isinstance(other, ChildStatus):
+ return NotImplemented
+
+ a = self._tuplify()
+ b = other._tuplify()
+ sum = [a1 + b1 for (a1, b1) in zip(a, b)]
+
+ return self.__class__(*sum)
+
+
+ def __sub__(self, other):
+ if not isinstance(other, ChildStatus):
+ return NotImplemented
+
+ a = self._tuplify()
+ b = other._tuplify()
+ difference = [a1 - b1 for (a1, b1) in zip(a, b)]
+
+ return self + self.__class__(*difference)
</ins></span></pre>
</div>
</div>
</body>
</html>