Helios architecture (1) server architecture and helios Architecture

Source: Internet
Author: User

Helios architecture (1) server architecture and helios Architecture

I saw the introduction and demonstration of ". NET open-source high-performance Socket communication middleware Helios of" cainiao ". I think this is a good thing. However, because I do not have network programming knowledge, I cannot tell the high performance part. I mainly want to share with you the Helios architecture based on the open source code.

Source code: https://github.com/helios-io/helios

First, we provide the server structure:

This is a big image, and it is estimated that many places are quite confused. We will explain in detail the logic in the next phase.

ServerBootstrap class

This class is the core class of the server, and the server provides services as the ServerBootstrap object (actually its subclass, And the subclass is created by this object ).

When creating code, we use the code

 var serverFactory =                new ServerBootstrap()                    .SetTransport(TransportType.Tcp)                    .Build();
  • This class has three core attributes: IExecutor, IServerFactory (IConnectionFactory), and NetworkEventLoop.
Public class ServerBootstrap: AbstractBootstrap {protected IExecutor InternalExecutor {get; set;} protected NetworkEventLoop EventLoop {get {return EventLoopFactory. createNetworkEventLoop (Workers, InternalExecutor) ;}} protected override IConnectionFactory BuildInternal () {switch (Type) {case TransportType. tcp: return new TcpServerFactory (this); case TransportType. udp: return new UdpServerFactory (this); default: throw new InvalidOperationException ("This shouldn't happen") ;}} public new IServerFactory Build () {return (IServerFactory) buildInternal ();}}Core Attributes and Methods
  • Another feature is chained programming (which may be referred to in jquery). this pointer is returned for all objects.
Public class ServerBootstrap: AbstractBootstrap {public ServerBootstrap workersmember fiber (bool member fiber) {UseSharedFiber = Member fiber; SetOption ("principal", UseSharedFiber); return this;} public new ServerBootstrap SetTransport (TransportType) {base. setTransport (type); return this;} public ServerBootstrap WorkerThreads (int workerThreadCount) {if (workerThreadCount <1) throw new ArgumentException ("Can't be below 1 ", "workerThreadCount"); Workers = workerThreadCount; return this;} public ServerBootstrap BufferSize (int bufferSize) {if (bufferSize <1024) throw new ArgumentException ("Can't be below 1024 ", "bufferSize"); BufferBytes = bufferSize; return this;} public ServerBootstrap WorkersAreProxies (bool useProxies) {UseProxies = useProxies; return this;} public ServerBootstrap Executor (IExecutor executor) {if (executor = null) throw new ArgumentNullException ("executor"); InternalExecutor = executor; return this;} public new ServerBootstrap SetConfig (IConnectionConfig config) {base. setConfig (config); return this;} public new ServerBootstrap SetDecoder (IMessageDecoder decoder) {base. setDecoder (decoder); return this;} public new ServerBootstrap SetEncoder (IMessageEncoder encoder) {base. setEncoder (encoder); return this;} public new ServerBootstrap SetAllocator (IByteBufAllocator allocator) {base. setAllocator (allocator); return this;} public new ServerBootstrap OnConnect (ConnectionEstablishedCallback connectionEstablishedCallback) {base. onConnect (connectionEstablishedCallback); return this;} public new ServerBootstrap OnDisconnect (ConnectionTerminatedCallback connectionTerminatedCallback) {base. onDisconnect (connectionTerminatedCallback); return this;} public new ServerBootstrap OnReceive (ReceivedDataCallback receivedDataCallback) {base. onReceive (receivedDataCallback); return this;} public new ServerBootstrap OnError (ExceptionCallback exceptionCallback) {base. onError (exceptionCallback); return this;} public new ServerBootstrap SetOption (string optionKey, object optionValue) {base. setOption (optionKey, optionValue); return this ;}}Chain Programming

At the end of the call, we must use the build method. The build method actually calls the BuildInternal internal method, and this is a factory mode (and an abstract factory composed of full ServerFactory ??), TcpServerFactory or UdpServerFactory is returned.

TcpServerFactory and UdpServerFactory

These two classes have no core code, but when you trace the parent class online, you will find that TcpServerFactory (UdpServerFactory) => ServerFactoryBase => ServerBootstrap. They are still ServerBootstrap objects. But the difference is that they have a mom besides Dad, ServerFactoryBase => IServerFactory => IConnectionFactory.

Let's take a look at the source code of ServerFactoryBase:

    public abstract class ServerFactoryBase : ServerBootstrap, IServerFactory    {        protected ServerFactoryBase(ServerBootstrap other)            : base(other)        {        }        protected abstract ReactorBase NewReactorInternal(INode listenAddress);        public IReactor NewReactor(INode listenAddress)        {            var reactor = NewReactorInternal(listenAddress);            reactor.Configure(Config);            if (ReceivedData != null)                reactor.OnReceive += (ReceivedDataCallback)ReceivedData.Clone();            if (ConnectionEstablishedCallback != null)                reactor.OnConnection += (ConnectionEstablishedCallback)ConnectionEstablishedCallback.Clone();            if (ConnectionTerminatedCallback != null)                reactor.OnDisconnection += (ConnectionTerminatedCallback)ConnectionTerminatedCallback.Clone();            if (ExceptionCallback != null)                reactor.OnError += (ExceptionCallback) ExceptionCallback.Clone();            return reactor;        }        public IConnection NewConnection()        {            return NewConnection(Node.Any());        }        public IConnection NewConnection(INode localEndpoint)        {            var reactor = (ReactorBase)NewReactor(localEndpoint);            return reactor.ConnectionAdapter;        }        public IConnection NewConnection(INode localEndpoint, INode remoteEndpoint)        {            return NewConnection(localEndpoint);        }    }

 

I found that all the things their mother (IConnectionFactory) has to do are done by IReactor. However, they (TcpServerFactory and UdpServerFactory) only find the appropriate tor actor object. On the other hand, we can also see that the IReactor object is really responsible for network connection. It is the logic to ensure the underlying communication.

Public sealed class TcpServerFactory: ServerFactoryBase {public TcpServerFactory (ServerBootstrap other): base (other) {} protected override ReactorBase partition (INode listenAddress) {if (UseProxies) return new partition (listenAddress. host, listenAddress. port, EventLoop, Encoder, Decoder, Allocator, BufferBytes); else throw new NotImplementedException ("Have not implemented non-TCP proxies ");}}TcpServerFactory public sealed class UdpServerFactory: ServerFactoryBase {public UdpServerFactory (ServerBootstrap other): base (other) {} protected override ReactorBase partition (INode listenAddress) {return new partition (listenAddress. host, listenAddress. port, EventLoop, Encoder, Decoder, Allocator, BufferBytes );}}UdpServerFactory

 IReactor

Here, TcpProxyReactor used in TcpServerFactory, UdpProxyReactor used in UdpServerFactory, and their base classes ProxyReactorBase and ReactorBase are included. The relationship between them is:

  • TcpProxyReactor => ProxyReactorBase => ReactorBase => IReactor
  • UdpProxyReactor => ProxyReactorBase => ReactorBase => IReactor
  • ReactorConnectionAdapter => IConnection (adapter mode, internal encapsulation of IReactor)

* Strictly speaking, ReactorConnectionAdapter is not an IReactor. It is only an adapter mode, so that the IReactor object can adapt to the IConnection object mode.

Although there are not many classes, it is estimated that the core of helios's efficiency may be related to this part. However, I don't know much about communication. I can only give a rough description of the construction method. Interested people can study it on their own.

  • ReactorBase: defines basic operations and events. For receiving, send provides default operations
  • ProxyReactorBase: addedReactorResponseChannelObject that calls the OnReceive of the ReactorResponseChannel)
  • TcpProxyReactor: Reload the StartInternal method and useTcpReactorResponseChannelReceive data
  • UdpProxyReactor: Reload the StartInternal method and useReactorProxyResponseChannelReceive data

ReactorResponseChannel

There are three types of ReactorResponseChannel, TcpReactorResponseChannel, and ReactorProxyResponseChannel.

  • ReactorResponseChannel base class that defines basic operations. The main method is the Send method.
  • TcpReactorResponseChannel, The ReactorResponseChannel implementation under the TCP protocol.
  • The ReactorProxyResponseChannel and ReactorResponseChannel proxy actually convert the ReactorResponseChannel virtual method into an empty method.

The OnReceive method in ReactorResponseChannel calls"NetworkEventLoop. Receive (data, this );", Send dataEventLoop(Message Queue.

EventLoop (Message Queue)

MQ has three levels of inheritance: NetworkEventLoop, ThreadedEventLoop, and AbstractEventLoop.

The inheritance relationship is NetworkEventLoop => ThreadedEventLoop => AbstractEventLoop.

  • AbstractEventLoop: uses an IFiber object internally to process messages. All the Processing Methods finally go through the IFiber object (in fact, the IFiber maintains a list, and then the IFiber object determines how to handle it)
  • ThreadedEventLoop: constructor to build its own IFiber object (default: DedicatedThreadPoolFiber)
  • NetworkEventLoop: processes network events and data receiving events with IFiber objects.

IFiber

The function of IFiber is not to process received data, but to process data in what way, such as several threads, synchronous or asynchronous processing. There are several IFiber objects, but in fact only one (DedicatedThreadPoolFiber) is actually used, but it does not prevent us from looking at these objects.

  • DedicatedThreadPoolFiber uses the hebios thread pool technology (DedicatedThreadPool), and the underlying layer processes data through the thread pool.
  • SynchronousFiber: it is processed immediately when an operation enters the message queue.
  • ThreadPoolFiber uses the thread pool technology to process data through the thread pool at the underlying layer.
  • SharedFiber shares the Fiber. When NetworkEventLoop. clone () is used, the IFiber object of NetworkEventLoop is simply passed to achieve the goal of sharing IFiber among multiple NetworkEventLoop.

Final processing class: BasicExecutor/TryCatchExecutor

In IFiber, we construct the BasicExecutor object by default (TryCatchExecutor inherits from BasicExecutor and can catch exceptions). This class will eventually process server-side data requests.

Conclusion: The server processes data in the following sequence: CreateServerBootstrapObject, build its subclass (IConnectionFactory), and then perform network communication (IReactor), Communication Pipeline (ReactorResponseChannel) Manage the data receiving and sending, and then the data enters the Message Queue (EventLoop), The thread technology that the server decides to process data (IFiber(BasicExecutor)

* This is not the real transport logic, but the logic order of the source code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.