

\begin{frame}
  \frametitle{Introduction}

  I describe a design for a system providing \textbf{Inter-process Communication} (IPC) for the DUNE far detector.
  \begin{itemize}
  \item Actually, I outline a set of \textbf{design elements} which need not be taken all-or-nothing.
  \item Cover \textbf{infrastructure} and \textbf{communication patterns} for IPC to be used to interface between subsystems and for internal comms for some subsystems.
  \item The scope for the IPC is DUNE FD DAQ but in fact some elements could/should be used also in CISC/Offline scope.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Basic Info Flow Pattern}
  Strive for loose coupling by deconstructing three distinct elements:

  \begin{center}
    producers $\to$ transmission $\to$ consumers
  \end{center}

  \begin{itemize}
  \item Overall system has multiple producers and consumers.
  \item Concrete implementations may tightly bind producers and consumers.
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Generalize the High-Level Design}
  The elements of the system implement a ``ported graph'':
  \begin{description}
  \item[node] a \textbf{procedure} operating asynchronously from others.
  \item[port] a \textbf{labeled point} of a node through which data flows.
  \item[edge] a \textbf{connection between two ports} which transmits \textbf{messages} following one or more \textbf{protocols}
  \item[message] a typed \textbf{unit of data} transmitted along an edge
  \item[protocol] a set of \textbf{message types} and \textbf{expected behavior} of their transmission.
  \end{description}
  From these various useful \textbf{roles} and \textbf{patterns} of may be defined.
\end{frame}

\begin{frame}
  \frametitle{Ported Graph Scaling}

  \begin{center}
    Any connected subgraph of a ported graph\\ may be recast into a single ported node.    
  \end{center}

  fixme: add diagram

  \vfill

  Define:
  \begin{description}
  \item[composite] node if it is composed of other nodes.
  \item[primitive] node if it may not be decomposed.
  \end{description}

  \vfill

  This composability hugely helps maintain/hide complexity.

\end{frame}

\begin{frame}
  \frametitle{Map Abstract to Concrete: ZeroMQ}
  Select elements of the ZeroMQ ecosystem:

  \begin{description}
  \item[libzmq] low-dependency, portable base library providing implementations of \textbf{high-level network abstractions and patterns}.
  \item[CZMQ] high-level C/C++ library of behavior patterns:
    \begin{itemize}
    \item actor, poller, looper, auth/auth/crypto
    \end{itemize}
  \item[Zyre] an \textit{actor} providing network discovery and presence

  \end{description}

  \footnotesize

  I think these ZeroMQ libraries should form the basis of the DUNE FD IPC system but at least implements an ``existence proof'' of this design and provides a language to discuss it.
\end{frame}

\begin{frame}
  \frametitle{Some Important ZeroMQ concepts}

  \begin{itemize}
  \item 3 types of message transmission supported:
    \begin{itemize}\footnotesize
    \item[o] \textbf{in-proc} (inter-thread), \textbf{inter-proc} (on same computer), \textbf{network}
    \item[o] selected via configuration string at run time, no code changes!
    \end{itemize}
  \item Socket type implements a specific communication pattern:
    \begin{itemize}\footnotesize
    \item[o] PUB, SUB, REP, REQ, DEALER, ROUTER, (exclusive) PAIR, etc.
    \end{itemize}
  \item A socket endpoint \texttt{bind()}'s or \texttt{connect()}'s
    \begin{itemize}\footnotesize
    \item[o] Order of \texttt{bind/connect} doesn't matter
    \item[o] Need not map to strict client/server roles (but can).
    \end{itemize}
  \item Messages are atomic.
  \item Transparent send/receive buffers:
    \begin{itemize}\footnotesize
    \item[o] Endpoints may disappear/reappear/join late without message loss.
    \item[o] Peak bottlenecks smoothed out.
    \end{itemize}
  \item PUB/SUB: topics are filtered on PUB end, unwanted messages not sent.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{CZMQ Actor}

  \begin{center}
    \textbf{function} running in a \textbf{thread} communicating \\
    via a thread-safe, bidirectional \textbf{pipe} (PAIR socket).
  \end{center}

  In this design:
  \begin{itemize}
  \item An actor implements a \textit{primitive} \textbf{node}.
  \item The function creates and services additional sockets.
    \begin{itemize}\footnotesize
    \item[o] Each socket is identified as a \textbf{port}.
    \end{itemize}
  \item The pipe is used as a Control/Configure/Monitor conduit.
  \item Actors may spawn other actors
    \begin{itemize}\footnotesize
    \item[o] One, special ``agent'' manages multiple actors (awful pun) 
    \item[o] Provides aggregation/simplification (ported subgraph-is-node pattern).
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Zyre Actor}
  \begin{center}
    An actor which implements network \textbf{discovery} and \textbf{presenece} as well as a simple form of many-to-many communication.
  \end{center}

  Discovery:
  \begin{itemize}
  \item Send UDP broadcast beacon with: \textbf{identifier} (a name), \textbf{endpoint} and set of \textbf{key/value} pairs.
  \item Listen on endpoint, respond to peer beacon by sending above info to peer's endpoint.
  \item Time scale for discovery is seconds.
  \end{itemize}
  Presence:
  \begin{itemize}
  \item Peer heartbeat messages at configurable interval.
    \begin{itemize}\footnotesize
    \item[o] heartbeats use UDP and TCP (efficient vs reliable)
    \end{itemize}
  \item Timeout interpreted as loss of presence.
  \end{itemize}

\end{frame}

\begin{frame}
  \frametitle{Edges}
  An edge:
  \begin{itemize}
  \item[+] Specified as a pair of endpoint strings
    \begin{itemize}\footnotesize
    \item[o] \texttt{inproc://thread-label}
    \item[o] \texttt{ipc:///path/to/pipe}
    \item[o] \texttt{tcp://hostname.dune.daq:1234}
    \end{itemize}
  \item[+] Assigned to a \textbf{node port}
  \item[+] Along with a \texttt{bind()} or \texttt{connect()} action.
  \item[$\to$] Assignment may be done:
    \begin{itemize}\footnotesize
    \item[+] at time of initial configuration
    \item[+] and/or later during a reconfiguration phase
    \item[$\to$] more on initialization and configuration later
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Messages}

  \begin{center}
      All messages shall be ``well defined'' \\in terms of format and schema.
  \end{center}

  \begin{itemize}
  \item ZeroMQ messages composed into \textit{frames}
    \begin{itemize}
    \item[o] One or more, sequential frames in a message.
    \item[o] Can construct, read frames independently.
    \item[o] Frames may follow different format/schema.
    \end{itemize}
  \item What formats/schemas depend on use
    \begin{itemize}\footnotesize
    \item[o] Packed bits/custom for high-rate / high-volume.
    \item[o] Google Protobuffers for high-rate/volume + structure.
    \item[o] JSON for low-rate + structured + lowest common denominator.
    \item[o] Strings for free form logs
    \end{itemize}
  \end{itemize}

  R\&D to make informed, optimal choice on case-by-case basis.

  Choice influenced by choice of message \textit{protocols}.

\end{frame}

\begin{frame}
  \frametitle{Protocols}
  ZeroMQ typically uses TCP with some thin application level protocol on top.

  We must design the ``real'' application-level protocols.
  \begin{itemize}
  \item Roles for both endpoints.
  \item A set of message types.
  \item Message exchange patterns. 
    \begin{itemize}
    \item[o] Client/server \textbf{not} always the desired pattern
    \end{itemize}
  \end{itemize}

  We can implement reusable protocol handlers that can be aggregated into applications.
\end{frame}

\section{DAQ Applications}
\begin{frame}
  \frametitle{Architecture of a DAQ application}
  \begin{itemize}
  \item A single, generic \texttt{main()} (\texttt{dexnet-agent})
    \begin{itemize}\footnotesize
    \item[o] thin interface between CLI/config and actors
    \item[o] aggregation of actors driven by CLI/config
    \item[o] shared library plugin / factory pattern.
    \item[o] manages actors via their pipe and ``actor control protocol''
    \end{itemize}
  \item Generic \texttt{Node} aggregates \texttt{Port} and \texttt{ProtocolHandler} instances
    \begin{itemize}\footnotesize
    \item[o] dynamic aggregation via plugin/factory pattern. 
    \end{itemize}
  \item Main functionality provided by implementing protocol handlers.

  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Prototype Implementation}
  Various prototypes at \url{https://github.com/brettviren/digrex/}.

  Current thoughts in \href{https://github.com/brettviren/digrex/tree/master/dexnet}{dexnet}.
  \begin{itemize}
  \item Based on ZeroMQ, Boost, Google Protobuf, JSON/JSonnet
  \item Portable Waf build, 
  \item Protobuf files and \texttt{protoc} code generation.
  \item Unit tests, source for shared library, main app.
  \item Plugin/factory implementation, \texttt{Node} and \texttt{Port}, initial protocol handlers.
  \end{itemize}
  
\end{frame}


\section{Configuration}

\begin{frame}
  \frametitle{Types of Configuration}
  \begin{description}
  \item[message] schema definitions
    \begin{itemize}
    \item Protobuf \texttt{.proto} files
    \end{itemize}

  \item[application] both initialization and later reconfiguration
    \begin{itemize}
    \item node aggregation directives, port endpoints, protocol handler parameters
    \item graph structure
    \end{itemize}
  \item[infrastructure] describe which DAQ programs should be run on what hosts when
    \begin{itemize}
    \item \texttt{supervisord}
    \end{itemize}
  \item[operating system] OS version it's packages and system config
    \begin{itemize}
    \item Ansible
    \end{itemize}
  \end{description}
\end{frame}

\begin{frame}
  \frametitle{Static vs Dynamic Configuration}

  \begin{description}
  \item[static] initial application configuration, message schema, infrastructure and OS
  \item[dynamic] applications must reconfigure at runtime, eg to support zero-downtime run changes and self-healing.
  \end{description}
\end{frame}

\begin{frame}
  \frametitle{Jsonnet: an all-purpose configuration language}

  \begin{itemize}
  \item A better JSON than JSON, compiles to JSON and others (YAML, INI).
    \begin{itemize}\footnotesize
    \item[o] coupled with, eg Jinja2, support for arbitrary formats given a common JSON data structure, (this is how dune-tdr/dune-params works)
    \end{itemize}
  \item Supports functional programming, data structure inheritance, lazy evaluation, external parameter/code injection.
  \item C++ library, C++ or Go executable
  \item Used in Kubernetes.
  \item Used for a couple years as  Wire-Cell's configuration language.
    \begin{itemize}\footnotesize
    \item[o] Support developed for scale-independent ported-graph construction/configuration. 
    \end{itemize}
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Proposal}

  \begin{enumerate}
  \item Maintain \textbf{entire}, definitive DAQ configuration in the Jsonnet language.
    \begin{itemize}\footnotesize
    \item static and dynamic
    \end{itemize}
  \item Develop Jinja2 templates to produce config file formats not already supported.
  \end{enumerate}

  \begin{itemize}
  \item Proven in large Kubernetes configurations.
  \item Assures consistency through DRY pattern.
  \item Allows relatively compact and complete single source with complexity expressed in simple functional programming code.
  \item A plain text configuration source lends well to \texttt{git} versioning.
  \end{itemize}
  
  \vfill

  \tiny
Surely, this is a controversial proposal....

\end{frame}

\begin{frame}
  \frametitle{Application Run Management}
  \begin{itemize}
  \item Run supervisord at boot on all hosts.
  \item Sync configuration files generated from Jsonnet.
  \item Use supervisord XmlRpc for remote control and monitoring.
  \item Extend at Python level if needed.
  \end{itemize}

  I've not done much prototyping yet.
\end{frame}

\begin{frame}
  \frametitle{OS configuration}
  Use Ansible
  \begin{itemize}
  \item Good experience at BNL CE testing lab and for BNL server/workstation config.
  \item Can be driven ultimately central by Jsonnet config.
  \end{itemize}
\end{frame}

\section{Scaling and Redundancy}

\begin{frame}
  \frametitle{FD DAQ Redundancy and Scaling}


  \begin{itemize}
  \item Current design and estimates say: 75 FE computers for SP 10kt, 5-25 for triggering, dozen for artDAQ BE.
    \begin{itemize}
    \item[$\to$] DAQ scale is probably good
    \end{itemize}
  \item Need to focus on redundancy.
    \begin{itemize}
    \item No single point of failure.
    \item At least two Module Level Triggers per trigger with failover
    \item Idle TP/TC processes.
    \item Listen but don't send until peer death pattern.
    \item Ignore duplicate message pattern.
    \end{itemize}
  \end{itemize}
  
\end{frame}

\begin{frame}
  \frametitle{ZeroMQ Redundancy and Scaling}

  \begin{itemize}
  \item ZeroMQ one of the fastest messaging systems to start with.
  \item PUB/SUB is N-to-M, allows natural redundancy mechanism.
  \item Extra layers of PUB/SUB can spread load if needed. 
  \item Zyre presence detects node failures.
  \item Round robin and fair queue patterns built in.
  \item Easy scaling by adding more nodes and reconfiguring endpoint strings.
  \item Load-balancing brokers can be inserted into graph, up/downstream unchanged.
  \end{itemize}

\end{frame}
