|
| 1 | +\documentclass[a4paper,twoside,12pt]{article} |
| 2 | +\usepackage{zed-cm} |
| 3 | +\usepackage{graphicx} |
| 4 | +\markboth{Draft}{Version 0.1} |
| 5 | +\pagestyle{myheadings} |
| 6 | + |
| 7 | +\begin{document} |
| 8 | +\parskip 2 pt |
| 9 | +\parindent 10 pt |
| 10 | + |
| 11 | +\title{Channeling Work} |
| 12 | +\author{Steve Powell \and Rob Harrop} |
| 13 | +\maketitle |
| 14 | +% The following three commands ensure the title page is stamped as |
| 15 | +% confidential without a page number. Page numbering is started at the |
| 16 | +% table of contents. |
| 17 | +\thispagestyle{myheadings} |
| 18 | +\pagenumbering{roman} |
| 19 | +\setcounter{page}{1} |
| 20 | + |
| 21 | +%============================================================================= |
| 22 | +\abstract{A short specification arising from Rob's investigation into serving messages on a collection of channels with a limited number of threads, preserving ordering constraints.} |
| 23 | + |
| 24 | +% Cntrl-Cmd-M -- \emph{} |
| 25 | +% Cntrl-Cmd-Z -- \zed{} |
| 26 | +% Cntrl-Cmd-X -- \axdef{} |
| 27 | +% Cntrl-Cmd-S -- \schema{} |
| 28 | +% Cntrl-Cmd-C -- \texttt{} |
| 29 | + |
| 30 | + |
| 31 | +% Type checking hacks |
| 32 | +\newcommand{\true}{true} |
| 33 | +\newcommand{\false}{false} |
| 34 | +\renewcommand{\emptyset}{\varnothing} |
| 35 | + |
| 36 | +%===================================================================== |
| 37 | +\clearpage |
| 38 | +\tableofcontents |
| 39 | + |
| 40 | +\clearpage |
| 41 | +\pagenumbering{arabic} |
| 42 | + |
| 43 | +%===================================================================== |
| 44 | +\section{Introduction} |
| 45 | +The primitives in this description are $Channel$s and items of $Work$. It is assumed that work is a message transfer or acknowledgement of some kind. It is not important. What is important is that a series of items of $Work$ needs to be done for a $Channel$ and we cannot allow two items of work to be processed for the same $Channel$ at the same time. So we introduce the primitive sets: |
| 46 | +\begin{zed} |
| 47 | +[ Channel, Work ] |
| 48 | +\end{zed} |
| 49 | +and with these we can describe the general state. |
| 50 | + |
| 51 | +%===================================================================== |
| 52 | +\section{The general state of things} |
| 53 | +The basic state of the system is a collection of $Channel$s with a sequence of items of work associated with each: |
| 54 | +\begin{schema}{Pool} |
| 55 | +pool : Channel \pfun \seq Work |
| 56 | +\end{schema} |
| 57 | + |
| 58 | +The $pool$ of known channels ($\dom pool$) is partitioned into those which are $dormant$, those $ready$ for work to be done, and those which are currently being processed ($inprogress$). |
| 59 | + |
| 60 | +We define a convenience schema for each partition. This is to name them for use in explicitly stating preservation later. |
| 61 | + |
| 62 | +The $dormant$ ones have no work (but we cannot impose this constraint without the $pool$ -- we do it later): |
| 63 | +\begin{schema}{Dormant} |
| 64 | +dormant : \power Channel |
| 65 | +\end{schema} |
| 66 | +the $ready$ ones are ordered: |
| 67 | +\begin{schema}{Ready} |
| 68 | +ready : \iseq Channel |
| 69 | +\end{schema} |
| 70 | +and the rest are `in progress': |
| 71 | +\begin{schema}{InProgress} |
| 72 | +inprogress : \power Channel |
| 73 | +\end{schema} |
| 74 | + |
| 75 | +\subsection{The state of the union} |
| 76 | +We can now assemble the entire system state as follows: |
| 77 | +\begin{schema}{State} |
| 78 | +Pool \\ |
| 79 | +Dormant \\ |
| 80 | +Ready \\ |
| 81 | +InProgress |
| 82 | +\where |
| 83 | +\langle dormant, \ran ready, inprogress \rangle \partition \dom pool |
| 84 | +\also |
| 85 | +\forall c : dormant @ pool ~ c = \langle \rangle |
| 86 | +\end{schema} |
| 87 | +where we make explicit that these channel collections partition those known (in $\dom pool$), and can also impose the constraint that the $dormant$ channels have no work. |
| 88 | + |
| 89 | +%ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ |
| 90 | +\section{Work delivery} |
| 91 | +In general, as it comes in, work is added to the sequence of items associated with a channel in the $pool$. However, the precise change of state depends upon which partition the channel is in at the time. |
| 92 | + |
| 93 | +We therefore define three `deliver work' state transitions; one for each partition. In each case different partitions change as a result. They share the same signature, and underlying pool change, however: |
| 94 | +\begin{schema}{DeliverWorkCommon} |
| 95 | +\Delta State \\ |
| 96 | +w? : Work \\ |
| 97 | +c? : Channel |
| 98 | +\where |
| 99 | +c? \in \dom pool \\ |
| 100 | +pool ' = pool \oplus \{ c? \mapsto pool ~ c? \cat \langle w? \rangle \} |
| 101 | +\end{schema} |
| 102 | +each of them changes the $State$, and takes an item of work and a channel as input. In every case, the work is added to the $pool$. |
| 103 | + |
| 104 | +When the channel is dormant it moves into the ready queue (and the `in progress' partition remains unchanged): |
| 105 | +\begin{schema}{DeliverDormant} |
| 106 | +DeliverWorkCommon \\ |
| 107 | +\Xi InProgress |
| 108 | +\where |
| 109 | +c? \in dormant \\ |
| 110 | +dormant ' = dormant \setminus \{c?\} \\ |
| 111 | +ready' = ready \cat \langle c? \rangle |
| 112 | +\end{schema} |
| 113 | +(Note that in this case the resulting sequence of work is simply $\langle w? \rangle$.) |
| 114 | + |
| 115 | +When the channel is already `in progress' the partitions stay unchanged: |
| 116 | +\begin{schema}{DeliverInProgress} |
| 117 | +DeliverWorkCommon \\ |
| 118 | +\Xi InProgress \\ |
| 119 | +\Xi Dormant \\ |
| 120 | +\Xi Ready |
| 121 | +\where |
| 122 | +c? \in inprogress |
| 123 | +\end{schema} |
| 124 | +(and we need say nothing more). |
| 125 | + |
| 126 | +When the channel is ready (in the $ready$ queue) the partitions stay unchanged as well: |
| 127 | +\begin{schema}{DeliverReady} |
| 128 | +DeliverWorkCommon \\ |
| 129 | +\Xi InProgress \\ |
| 130 | +\Xi Dormant \\ |
| 131 | +\Xi Ready |
| 132 | +\where |
| 133 | +c? \in \ran ready |
| 134 | +\end{schema} |
| 135 | +(and we need say nothing more). |
| 136 | + |
| 137 | +Since the preconditions of the $Deliver$ schemas are disjoint, we may combine them without introducing any further non-determinism: |
| 138 | +\begin{zed} |
| 139 | +DeliverWork\defs DeliverDormant \lor DeliverInProgress \lor DeliverReady |
| 140 | +\end{zed} |
| 141 | + |
| 142 | +\section{Start work} |
| 143 | +When there is time (and available computing resources) we can start some work. We consider only doing one piece of work at a time, though it is easy to consider `batching' work items together. |
| 144 | + |
| 145 | +The operation that starts it picks a piece of work to do, and gives the work and channel as outputs. The dormant channels are unaffected, and the channel moves from ready to `in progress': |
| 146 | +\begin{schema}{StartWork} |
| 147 | +\Delta State \\ |
| 148 | +\Xi Dormant \\ |
| 149 | +c! : Channel \\ |
| 150 | +w! : Work |
| 151 | +\where |
| 152 | +ready \neq \langle \rangle \\ |
| 153 | +\langle c! \rangle \cat ready' = ready \\ |
| 154 | +inprogress' = inprogress \cup \{ c! \} \\ |
| 155 | +\langle w! \rangle \cat pool' ~ c! = pool ~ c! \\ |
| 156 | +\{ c! \} \ndres pool' = \{ c! \} \ndres pool |
| 157 | +\end{schema} |
| 158 | +where the channel simply gets `taken' from the front of the ready queue; the work gets `taken' from the front of the work queue for the channel; the channel gets put in the `in progress' partition and no other channels are affected. |
| 159 | + |
| 160 | +Notice that here, although apparently there might be no work left, the channel is not put into the $dormant$ partition until the current work is completed. It is quite in order for non-$dormant$ channels to have no work \emph{pro tem}, though we expect such a channel to be placed in $dormant$ eventually. |
| 161 | + |
| 162 | +\section{Our work is done} |
| 163 | +After the work is completed the channel can be taken out of `in progress'. Of course, there might already be more work to do (or not) and these cases are distinguished. |
| 164 | + |
| 165 | +We define a `partial operation' to identify the essence of work completion: |
| 166 | +\begin{schema}{EndWorkCommon} |
| 167 | +\Delta State \\ |
| 168 | +\Xi Pool \\ |
| 169 | +c? : Channel |
| 170 | +\where |
| 171 | +c? \in inprogress \\ |
| 172 | +inprogress' = inprogress \setminus \{c?\} \\ |
| 173 | +pool' = pool |
| 174 | +\end{schema} |
| 175 | +This only happens for channels in $inprogress$ and this channel is always removed from there. None of the work queues change as a result of this transition. |
| 176 | + |
| 177 | +If there is no more work to do, the channel becomes dormant (and the ready queue remains unchanged): |
| 178 | +\begin{schema}{EndWorkNoMoreToDo} |
| 179 | +EndWorkCommon \\ |
| 180 | +\Xi Ready |
| 181 | +\where |
| 182 | +pool ~ c? = \langle \rangle \\ |
| 183 | +dormant' = dormant \cup \{ c? \} |
| 184 | +\end{schema} |
| 185 | +(We could have deduced the dormant relation -- and, as it happens, the pool constraint -- from the core work and the fact that the ready queue does not change.) |
| 186 | + |
| 187 | +If there \emph{is} more work to do, the channel becomes $ready$ (though not so ready as some \emph{al}ready): |
| 188 | +\begin{schema}{EndWorkMoreToDo} |
| 189 | +EndWorkCommon \\ |
| 190 | +\Xi Dormant |
| 191 | +\where |
| 192 | +pool ~ c? \neq \langle \rangle \\ |
| 193 | +ready ' = ready \cat \langle c? \rangle |
| 194 | +\end{schema} |
| 195 | +The channel is placed on the `end' of the ready queue. |
| 196 | + |
| 197 | +Should we be so inclined, since the preconditions of the two $EndWork$ schemas are disjoint, we can unambiguously combine them: |
| 198 | +\begin{zed} |
| 199 | +EndWork \defs EndWorkMoreToDo \lor EndWorkNoMoreToDo |
| 200 | +\end{zed} |
| 201 | + |
| 202 | +\section{To begin with...} |
| 203 | +After constructing this description, we note that there is no channel creation nor deletion described, and that the initial state is not given. This is an ideal opportunity to record our intentions in these areas. We can even document the rules for pieces of work left over when a channel `crashes', in some way, or if the item of work `fails'. |
| 204 | + |
| 205 | +We limit this brief note to talk about the initial state. |
| 206 | + |
| 207 | +In the absence of channel creation or deletion, we will provide the set of channels initially: |
| 208 | +\begin{schema}{InitialState} |
| 209 | +State' \\ |
| 210 | +cs? : \power Channel |
| 211 | +\where |
| 212 | +dormant' = cs? \\ |
| 213 | +pool' = \{ c : cs? @ c \mapsto \langle \rangle \} |
| 214 | +\end{schema} |
| 215 | +The set of channels is precisely the dormant set initially, and the pool records no work at all. |
| 216 | +The $\partition$ constraint on $State$ ensures that the rest of the initial state is determined: |
| 217 | +\begin{zed} |
| 218 | +InitialState \vdash ready' = \langle \rangle \land inprogress' = \emptyset |
| 219 | +\end{zed} |
| 220 | + |
| 221 | +\section{Summary} |
| 222 | +Essentially, this simple arrangement makes sure that pieces of work for the same channel never overtake each other, even if there are free servers ready to do more work at all times. |
| 223 | + |
| 224 | +\begin{figure}[h] |
| 225 | +\centering |
| 226 | +\includegraphics*[scale=0.7]{worktransition.pdf} |
| 227 | +\caption{Channel state movements} |
| 228 | +\label{fig:statediag} |
| 229 | +\end{figure} |
| 230 | +The operations defined here can be diagrammed (see figure \ref{fig:statediag}) as transitions between three `states' of a channel, corresponding to the partitions of the $\dom pool$ collection. |
| 231 | + |
| 232 | +This picture was constructed \emph{after} the specification was written, and seems bleedin' obvious. 'Twas ever thus. |
| 233 | + |
| 234 | +This document fully type-checks (with Fuzz). |
| 235 | + |
| 236 | +\subsection{...footnote} |
| 237 | +\begin{figure}[h] |
| 238 | +\centering |
| 239 | +\includegraphics*[width=1.5\linewidth,angle=90]{whiteboard.JPG} |
| 240 | +\caption{board marks} |
| 241 | +\label{fig:whiteboard} |
| 242 | +\end{figure} |
| 243 | +Figure \ref{fig:whiteboard} is a picture of the ``whiteboard'' resulting from the original discussion and from which this document was created. |
| 244 | + |
| 245 | +\end{document} |
0 commit comments