📄 architecture.tex.svn-base
字号:
Diameter protocol functions are provided by multiple threads. The threadhandling are based on the ACE thread implementations. The issue ofcontention and serialization between components within theimplementation is solved by using protected queues. As with threading,the queue and locks (which provided protection) are ACE based. Note thatthe ACE classes used in the libdiameter library will be mentioned inthis document. However, it is assumed that the reader is familiar withthese ACE classes. Detailed description of these classes is best servedby ACE\cite{ace}.As shown in Figure~\ref{fig:architecture}, several threads exist withinthe Diameter program. Each of these threads are based on ACEthread/task classes which not only provides platform abstractedthreading but also a software design pattern that strictly defines theboundaries of an executing module. Within each thread, a protectedmessage queue exist that allows threads to pass pre-formed messages toeach other. For the purpose of the Diameter implementation, these queueswill be termed job queues. Further discussion on the use of job queuesare described in the types of threads that exist in the program. Theseare as follows:\subsection{Master Thread\label{sec:mastert}}A master thread exists to orchestrate the existence of all other threadsin the program. The master thread, also known as the factory, isresponsible for spawning a thread for each active peer connection (bothfor connected or accepted communication request) It has theresponsibility of initiating and monitoring local asynchronousconnection request as well as accepting remote connection request. Onsuccess of any of these request, it spawns a thread for the newlyestablished connection and passes ownership of the connection to the newthread. An advantage of using ACE for this specific communicationspattern is that the underlying communication transport used isindependent of this pattern. Meaning that the thread spawning behaviorbased on successful connections will remain the same whether using TCPor SCTP as an underlying transport.The factory is also responsible for maintaining the existence of athread pool. This thread pool is used as a load balanced executioncontext for session management. Beyond being created and destroyed thefactory, the threads in the pool is fairly independent of thefactory. Details about the thread pool and its role is discussed in thesucceeding topics Section~\ref{sec:architecture}. In addition, thefactory also provides global timer services for all internal timingneeds. All timer requests are callback based and this global timer runswithin the context of the master thread.\subsection{Peer Connection Threads\label{sec:peert}}As mentioned in Section~\ref{sec:mastert}, the factory spawns a threadto handle each successful peer connection. The responsibility of a peerthread is to adhere to the peer state machine as stated in the Diameterdraft \cite{basep}, to send and receive Diameter messages destined forother peers (forwarding) or local processing (session management), tomaintaining the peer table and other functions necessary forestablishing Diameter peer connection.In libdiameter, a singleton class (AAAPeerService) exist to service theinitialization and termination requirements for transport management. Inthis singleton class, the peer table is examined to see if a connectionsto locally configure peers be attempted on startup. If so requests aremade to the master thread to initiate asynchronous connection to thepeers. These pending connections are monitored by the master thread forsuccess or failure. On success, a peer thread is spawned and ownershipof the newly established connection is passed to the thread. Inaddition, the singleton contains globally accessible protected databasesthat each peer thread requires, i.e. the routing table and the peertable.For incoming connections, the master thread is constantly monitoring thewell known Diameter port for connection request. Once, a connectionrequest is received, a peer thread is spawned. As with connectionattempts, the ownership of the established connection is passedon. Regardless of whether, connecting or accepting, the peer threadsdesign pattern is the same. An established connection needs to beserviced within the thread. The Diameter peer state machine, which isindependent of the design pattern, will dictate the behavior of theexecuting thread based on whether it connected or accepted therequest. As an example, in the case of accepted request, the statemachine would need to wait for a complete CER to arrive then determinewhether to accept the peer or not. If accepted, the peer thread may addthis new peer as a dynamic entry into the peer table. As for connectingpeers, it would need to send the CER on start of the state machine.Since ownership of the connection is passed on to the peer thread, it isit's responsibility to formally disconnect or handle disconnectionevents from the remote peer. These events are common and thus part ofthe design pattern. The ACE design pattern used are based on\begin{verbatim}ACE_Svc_handler\end{verbatim} template class. Also,since each peer thread has a message queue built in, it has toconstantly monitor the message queue and the established peer connectionand act accordingly once a message from any of those sources appear.\subsection{Thread Pool\label{sec:pool}}The thread pool design pattern exists to facilitate the use of loadbalancing in session management. All required Diameter protocolactivities beyond the peer task will be performed by a thread in thispool. The pool is composed of a collection of threads that constantlymonitors a job request queue as shown inFigure~\ref{fig:architecture}. The job request queue is protected by amutex lock that allows only one thread to dequeue an entry at atime. The entries in these queue contains specific job instructions aswell as data, i.e. parsing a message, look up a session and switch itsstate machine, call user subscription etc. The ability to sendinstructions, along with any associated data, via this queue gives theopportunity to assign a pending job to the next available idlethread. Parallelized load balancing on a fair, first come first servebasis based on the mutex lock can then be achieved.The contents of the job queues are generic enough to allow instances ofobjects to be enqueued and dequeued. With this ability, a design patternbased on job instance is used to as a generic way of sendinginstructions and data to a thread in the pool for processing. In theimplementation, a job instance is defined as follows:\begin{figure}[htbp]\begin{center}\begin{verbatim}class AAAJobInstance { public: AAAJobInstance(void *p) { payload = p; }; virtual u_int32_t Job() { return (0); }; protected: void* payload;};\end{verbatim}\caption{Generic job instance base class {\bf AAAJobInstance}\label{fig:jobsource}}\end{center}\end{figure}The AAAJobInstance class is used as a base class for purpose specificderived classes, i.e., message parsing class, timeout event processingclass, etc. As an example, a message parsing class can overrideAAAJobInstance and implement the Job() method where actually parsing isto be done. The raw message itself is reference by payload. Thisderived class can then be injected into a job request queue and the nextidle thread in the pool can dequeue the job instance and invoke it'sJob() method. Hence, all threads in the pool monitors the job requestqueue and waits to dequeue the next available job instance so they caninvoke it's Job() method. To continue the parsing example, once themessage has been parsed, the Job() method may also try to determine ifthe belongs to an existing session. If it does, it creates a sessionmessage job instance and enqueues it to the job request queue so thatthe next available thread can process switch the session state machinebased on the parse message.The granularity of the task that each Job() method must perform isrestricted to a very specific function. The success of a load balancedsystem depends on how well distributed and finely divided each jobis. In libdiameter, the number of threads in the pool is controlled bylocal configuration.\section{Basic Message Processing\label{sec:processing}}This section describes how incoming and outgoing messages are processedby the transport and session manager and the message parser so as toprovide more insight to the Open Diameter software architecture.\subsection{Message Transport and Session Processing\label{sec:msgtransport}}An incoming message from a remote peer is initially received andprocessed by the transport manager within one of the peer threadsservicing the remote peer. Once fully received, the transport managercan partially parse the header and parts of the message body if neededto determine where to forward the message. If the message is for anotherremote host, the message is enqueued into the job queue of the peerthread servicing the destination peer. If the message is for localconsumption, the transport manager passes the message to the sessionmanager by injecting the message into the message parsing job queue, seeFigure~\ref{fig:architecture}. This queue serves as the logical boundarybetween the transport manager and session manager on the ingresspath. Once a message is enqueued into the message parsing job queue, anew thread pool job request is made so that entries in the messageparsing job queue can actually be processed. This new job request isinjected into the thread pools job request queue where the nextavailable thread can dequeue it and start processesing. On processing,the message is dequeued from the message parsing job queue and thencompletely parsed and passed on directly to the application subscriberwho has specifically registered to receive such a message. Afterinvoking the application subscription, the job is completed.Within the subscription, an application may need to query the sessiondatabase using the message. The query will return the appropriatesession object associated with the message. The session object can thenbe used to update the session state machine or transmitt a reply to thereceived message. If the parsed message, however, is a base protocolmessage used for use only by the Diameter library (ASR,ASA,STR,STA),then the message subscription is internal, i.e. the library itselfsubscribes for those base protocol messages. And hence, the subscriptioninvocation is also internal and any adjustments that this subscriptionmakes to the session state machine follows the same process of creatinga new thread pool job request and injecting it into the thread pool jobrequest queue. This new job is a session job which is again waiting tobe processed by the next available thread in the pool. Once dequeued byan available thread then updates to the session state machine based onthe associated message can be made.For outgoing messages generated by an application, the applicationinvokes a Diameter API transmission class which associates the message
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -