Netty interview topics

1.BIO, the difference between the NIO and AIO?

BIO : a connecting thread, the client has the server when the connection request will need to start a thread for processing. Large thread overhead.
Pseudo Asynchronous IO : The request connection into the thread pool, to many, but the thread is still very valuable resource. The NIO : a request for a thread, but the connection request sent by the client are registered to the multiplexer, the multiplexer is connected to the polling I / O start request when a thread for processing.
AIO : a valid request a thread, client I / O requests are completed by the OS first and then inform the server application to start a thread for processing,

BIO stream oriented, NIO buffer is oriented; BIO various flow is blocked. And is non-blocking NIO; BIO Stream is the way, and the NIO channel is bidirectional.

NIO features: event-driven model, single-threaded multi-task processing, non-blocking I / O, I / O read and write is no longer blocked, but returns 0, transmission is more efficient than block-based transport stream based on more advanced IO function zero-copy, IO multiplexed greatly improved scalability and practicality Java network application. Reactor threading model.

In Reactor mode, waiting for an event dispatcher events or operations may be applied or a state occurs, the event dispatcher put this event passed event handler callback function registered in advance or the actual reading and writing done by the latter operating. As implemented in Reactor read: registered ready for reading event and the corresponding event handler, the event dispatcher wait for an event, the event comes to activate the distributor, the distributor call processor corresponding to the event, the event handler to complete the actual read operation, handling read data, register a new event, and then return control.

2.NIO composition?

Buffer: Channel interact with, the data is read into the buffer from the Channel, Channel is written from a buffer
flip Method: This buffer is reversed, to position the limit, then the position is set to 0, in fact switching to read and write mode
clear: Clear this buffer, the position is set to 0, the value to the capacity limit. Method rewind: rewind this buffer, the position is set to 0
DirectByteBuffer system space can be reduced to a copy of the user space. But higher costs Buffer creation and destruction, uncontrolled, often with the memory pool to improve performance. Large, sustained buffer direct buffers assigned to the main operating base system who are vulnerable to the influence of the native I / O's. If the data is smaller than the small applications, consider using heapBuffer, managed by the JVM.
Channel: IO represents the open source connected to the target, in both directions, but can not directly access the data and can only interact with Buffer. By Source found, read and write methods FileChannel method of data replication have led twice!
Selector allows a separate thread to manage multiple Channel, open method to create a Selector, register method to register the channel multiplexer, you can listen Event Type: reading, writing, connect, accept. After registration event will produce a SelectionKey: It represents the relationship between registered and SelectableChannel Selector, wakeup method: make the first selection operation has not yet returned to return immediately because the wake is: registered a new channel or event; channel is closed, unregister; a higher priority event triggers (such as timer events), I hope in time.

Selector implementation class in Linux is EPollSelectorImpl, entrusted to EPollArrayWrapper achieve, which is a package of three native methods epoll, and EPollSelectorImpl. ImplRegister method to register an event epoll instance, will be registered by calling epoll_ctl file descriptor (fd ) correspondence between the SelectionKey added to fdToKey, this map maintains a file descriptor and SelectionKey mapping.

fdToKey sometimes become very large, because the registration on the Channel Selector very large (millions connection); expired or invalid Channel did not close. fdToKey serial always read, reading is performed in the select method, the method is not thread-safe.

Pipe: one-way data connection between two threads, the data is written to the sink channel, the channel is read from the source

NIO's server build process: Selector.open (): open a Selector; ServerSocketChannel.open (): Create a Channel service side;
the bind (): bind to a port. And configure the non-blocking mode;
the Register (): Registration Channel and events of interest to the Selector; select () polling get ready event

3.Netty features?

A high-performance, NIO asynchronous event-driven framework, which provides support for TCP, UDP and file transfer
using the more efficient the bottom socket, cpu occupancy on the air epoll surge caused by polling process performed within, to avoid direct use of NIO trap simplifies the handling of NIO.
Using a variety of decoder / encoder support, stick package for TCP / sub automated process
may be used to accept / processing thread pool to improve the efficiency of ligation of reconnection, the simple support heartbeat detection
may configure the number of IO threads, TCP parameters, receiving TCP and a transmission buffer memory instead of directly using the heap memory, the memory pool by way of recycling ByteBuf
subject application by reference timely release of a counter is no longer referenced, to reduce the frequency of GC
reactor single-threaded thread model serialization embodiment, efficient
large used volitale, CAS and using atomic classes that use thread-safe type, read-write lock

4.Netty threading model?

Reactor model Netty received by the multiplexer and processing user requests, the internal implementation of the two thread pool,
boss thread pool and work thread pools, wherein thread pool threads boss accept event responsible for handling the request, upon receiving the accept event when the request corresponding to the socket into a NioSocketChannel the package, and to work the thread pool, where the pool of threads work event read and write requests, the process by the corresponding Handler.

Single-threaded model : all I / O operations by a thread to complete, namely multiplexing, events, distribution and processing is done on a Reactor thread. Both client receiving the connection request, initiates the connection to the server, and also transmits / or answer a read request / response messages. NIO a thread to handle hundreds or thousands of links, can not support the performance is slow, if the thread into the loop, the entire program is not available, for high load, high concurrency scenarios inappropriate.

Multithreading Model : a NIO thread (Acceptor) is only responsible for monitoring the server, receiving the client's TCP connection request; NIO pool of threads network IO operation, i.e. reading, decoding, encoding and transmitting messages; 1 NIO threads can be N links simultaneously processed, but only a link corresponding to a NIO thread, which is to prevent concurrency problems. However, when one million concurrent client connections or require security authentication, a thread Acceptor there may be insufficient performance issues.

Multithreading master-slave model : Acceptor listening port for binding thread, the receiving client is connected to the main SocketChannel removed from the thread pool threads Reactor multiplexer, to re-register on the thread of the thread pool Sub, with the processing of I / O read and write operations to ensure mainReactor only responsible for access authentication, handshaking operation;

Cause and Solution 5.TCP stick package / unpacking?
TCP is a way to process the data stream, a full TCP packet may be split into multiple packets for transmission, the small package is also possible as a large data packet transmission.

Cause stick package TCP / subcontracting:
byte written is larger than the size of the application sending buffer size of the socket, unpacking phenomenon occurs, and the application to write data is smaller than the size of the socket buffer, the application card multiple writes transmission data to the network, this phenomenon will stick package;
a TCP segment size MSS, when the payload length of the TCP packet header length -TCP> MSS when the Ethernet frame unpacking occurs (payload) is greater than the MTU (1500 bytes) ip fragmentation. Resolution
message given length: FixedLengthFrameDecoder class
trailer add special character segmentation: line separator categories: LineBasedFrameDecoder or
custom class separator: DelimiterBasedFrameDecoder
message into a message header and a message body: LengthFieldBasedFrameDecoder class.
Unpacking divided into head and stick package, and the length field preceding a head of the stick package unpacking, multiple head extensions and stick package unpacking.

6. Learn what kinds of serialization protocol?

Serialization (encoded) into a target sequence is a binary form (a byte array), it is mainly used for network transmission, data persistence and the like; and the byte deserialization (decoding) sucked read from the network, disk, etc. restore the original object array, for decoding the main object of transmission networks, in order to complete the remote call.

The key factors affecting the performance of serialization: stream size (network bandwidth) serialized, serialized performance (CPU resource consumption); cross-language support (heterogeneous systems and development languages ​​docking switch).

Java serialization provides default : You can not cross-language, stream serialized too much, poor performance serialization

XML , advantages: good human readability, you can specify the name of the element or attribute. Disadvantages: serialized data structure contains only the data itself, and the like, does not include information type identification and assembly; only public properties and sequence fields; not serialization method; file large, complex file format, representing the transmission bandwidth. Applicable scene: as a configuration file to store data, real-time data conversion.

The JSON , a lightweight data-interchange format, advantages: high compatibility, the data format is simple, easy to read, the serialized data is small, good scalability, good compatibility, as compared to XML, the which agreement is relatively simple, analytical faster. Disadvantages: descriptive data worse than XML, for performance requirements is not the case ms level, additional space overhead is relatively large. Application scenarios (alternative XML): cross-firewall access, adjustable high requirements, Ajax-based Web browser request, a relatively small amount of data transmission, real-time requirements are relatively low (for example, second level) services.

FASTJSON , use a "rapid and orderly assumed to match" algorithm. Pros: Easy to use interfaces, the fastest currently java language json library. Cons: too much emphasis on fast, and deviate from the "standard" and functionality, code quality is not high, incomplete documentation. Applicable scene: protocol interaction, Web output, Android Client

Thrift , is not only a sequence of protocol, or a RPC framework. Advantages: small volume after serialization, fast, support multiple languages, and rich data types, additions and deletions to the data field has a strong compatibility, support binary compression. Disadvantages: fewer users, when cross-firewall access, insecurity, not readable, relatively difficult when debugging code can not be used (for example, HTTP) in conjunction with other transport layer protocol can not support directly read and write data to the persistence layer, that is not suitable for data persistence serialization protocol. Applicable scene: RPC distributed system solutions

Avro a sub-project, Hadoop to solve the JSON lengthy and no IDL problems. Pros: support for rich data types, simple dynamic language binding function, self-descriptive attributes, improve the speed of data analysis, rapid compressible binary data, you can achieve remote procedure call RPC, support cross-programming language. Cons: not intuitive for users accustomed to statically typed language. Applicable scene: do Hive, Pig and MapReduce persistent data format in Hadoop.

Protobuf , the data file structure .proto be described by code generator may generate a data structure corresponding to the object POJO Protobuf and associated methods and properties. Advantages: the serialization stream, high performance, structured data storage format (XML JSON, etc.), by the sequence identifier field can be achieved prior to the compatible protocol, structured document easier to manage and maintain. Disadvantages: need to rely on code generation tools, relatively few supported languages, the official only supports Java, C ++, python. Applicable scene: high performance requirements of RPC calls with good cross-firewall access properties for the application layer object persistence

other

protostuff protobuf based protocol, but it does not require proto configuration file directly to the leader packet
Jboss marshaling java class can be serialized directly, without a real java.io.Serializable interface Message pack efficient binary serialization format
Hessian binary protocol Lightweight level remoting onhttp tool
kryo protobuf based protocol, supports only java language, requires registration (registration), then serialize (Output), deserialization (Input)

7. How to choose a sequence of protocol? Specific scenes

For system calls between companies, if the performance requirements of the service in more than 100ms, the XML-based SOAP protocol is a solution worth considering. It is the preferred based communications, JSON protocol between the Web browser of Ajax, as well as Mobile app with the server. For the performance requirements are not too high, or the language based dynamic type, or the transmission of data using a low load scenario, JSON is a very good choice. For debugging environment is bad scene, using JSON or XML can greatly improve debugging efficiency, reduce system development costs. When the scene of the most demanding performance and simplicity, has a competitive relationship between Protobuf, Thrift, Avro. For persistent scenarios T-level data, Protobuf and Avro first choice. If the data is stored in the persistent sub-projects in hadoop, Avro would be a better choice. For the persistence of non-Hadoop project to statically typed language-based scenarios, Protobuf would be more in line with statically typed languages engineers develop habits. Since the Avro design biased in favor of dynamically typed languages, the main language for dynamic application scenarios, Avro is a better choice.
If you need RPC provides a complete solution, Thrift is a good choice. If, after the sequence of the need to support different transport protocols, or need cross-firewall access, high-performance scene, Protobuf be given priority. There are a variety of data types protobuf: bool, double, float, int32 , int64, string, bytes, enum, message. protobuf qualifier: required: the assignment must not be empty, optional: fields can be assigned, the assignment may not, repeated: This field can be repeated any number of times (including zero), the enumeration; only the specified set of constants a value as its value;

Protobuf basic rules: Each message must leave at least one type of field required, contains zero or more optional type of field; repeated field represents 0 or may comprise a plurality of transactions; within [1,15] of the identification number will be encoded when one byte (common), the identification number within the [16,2047] is of 2 bytes, identification number must not be repeated, using the message type, the message may be arbitrarily nested The multilayer, can be used in place of a nested set of message types.

protobuf message upgrades principle: Do not change any existing numerical identifier field; can not remove the required fields already exist, optional and repeated type of field can be removed, but want to keep the label can not be reused. The newly added fields must be optional or repeated. Because the old version of the program can not read or write the new required qualifier field.

The compiler generates a .java files, and a special class of Builder for each message type (class is used to create a message class interface). Such as: UserProto.User.Builder builder = UserProto.User.newBuilder (); builder.build ();

Netty use in : ProtobufVarint32FrameDecoder is half the packet for decoding the message handling; ProtobufDecoder (UserProto.User.getDefaultInstance ()) which is created in the file UserProto.java decoding class; ProtobufVarint32LengthFieldPrepender message protocol head plus protobuf shaping a length field 32 for the length of the sign of the message class; ProtobufEncoder is coded class

StringBuilder converted to ByteBuf Type: copiedBuffer () method

8.Netty zero-copy achieved?

Netty ByteBuffer receive and transmit using DIRECT BUFFERS, the direct use of the heap memory for Socket reader, does not require a secondary copy of the byte buffer. Heap more than a memory copy, JVM heap memory Buffer will copy directly to memory, and then only into the Socket. ByteBuffer by the ChannelConfig distribution, and ChannelConfig create ByteBufAllocator default Direct Buffer

CompositeByteBuf class can be merged into a plurality ByteBuf ByteBuf a logical avoid the traditional manner by the memory copy several small Buffer Buffer merge into one large. The header and body addComponents method ByteBuf merged into a logical, two ByteBuf inside CompositeByteBuf is present alone, but as a whole CompositeByteBuf logic

File transfer method by FileChannel.tranferTo FileRegion packaging, can directly send data to the target file buffer Channel, avoiding the problems of the conventional memory copy mode write cycle caused by.

By wrap method, we can byte [] array, ByteBuf, ByteBuffer Netty ByteBuf packaging into a target, thereby avoiding the copy operation.

Selector BUG: If the result of the polling Selector is empty, or no wakeup process a new message, empty polling occurs, CPU usage 100%

Netty solution : Cycle Selector to select the operating statistics, each complete with an air of select operations to conduct a count, if N times continuously empty polling place within a certain period, triggered epoll infinite loop bug. Selector reconstruction, rebuilding determine whether the request is initiated by another thread, if not then the original SocketChannel removal from the old Selector registration, re-registration to the new Selector, and the original Selector closed.

9.Netty performance in what areas?

Heartbeat, the server of : periodically remove unused sessions inactive (netty5), the client: means for detecting whether the session is disconnected, whether again, network delay is detected, wherein the class is used to detect a session state idleStateHandler

Serial no lock design , which processes the message as much as possible in the same thread to complete, without thread switching period, thus avoiding competition and multi-thread synchronization lock. On the surface, the serial design seems CPU utilization rate is not high enough degree of concurrency. However, by adjusting parameters NIO thread pool threads may be started simultaneously a plurality of threads run in parallel serialized, no such localized locking thread design comparison of a serial queue - a plurality of threading model work better performance.

Reliability , validity detect link: link is idle detection mechanism, read / write idle time-out mechanism; memory protection mechanism: by reusing ByteBuf memory pool; decoding of protection ByteBuf; elegantly down: no longer receive new messages, pre-front exit processing operations, the release of resources to operate.

Netty Security : Supports security protocols: SSL V2 and V3, TLS, SSL-way authentication, mutual authentication and third-party CA certificate.

Efficient concurrent programming reflects : volatile lot, proper use; CAS and widely used classes atoms; thread-safe container; improve performance by concurrent read-write lock. IO communication performance three principles: a transmission (AIO), protocol (Http), threads (multithreaded master-slave)

Traffic shaping effects (transformer) : to prevent the performance of the downstream network element caused by unbalanced downstream network element is crushed, interrupted traffic flow; acceptance message communication module to prevent excessive back end service does not lead to timely Chengsi threading problems.

TCP parameters : SO_RCVBUF and SO_SNDBUF: generally recommended value of 128K or 256K; SO_TCPNODELAY: NAGLE algorithm small packets in the buffer is automatically connected to form a larger packet from a large number of small packet transmission network congestion, thereby improving network application effectiveness. But for delay-sensitive applications scenarios need to close the optimization algorithm;

10.NIOEventLoopGroup source?

NioEventLoopGroup (actually MultithreadEventExecutorGroup) maintains a type of internal EventExecutor children [], default size is 2 * number of processor cores, this constitutes a thread pool, initialization
when overloaded EventExecutor NioEventLoopGroup newChild method, the actual type of the children of the element NioEventLoop.

SingleThreadEventExecutor constructor call when the thread starts, run method performs NioEventLoop class first calls hasTasks () method to determine whether there is currently taskQueue elements. If there are elements to perform selectNow () method taskQueue, the final execution selector.selectNow (), which returns immediately. If taskQueue no elements to perform select (oldWakenUp) method

select (oldWakenUp) method to solve the Nio the bug, selectCnt used to record the number of executions and identify whether selector.select method performed selector.selectNow (), if triggered empty polling bug epoll, and will be repeated selector. select (timeoutMillis), variable selectCnt will gradually become larger, when selectCnt reaches a threshold value (default 512) is performed rebuildSelector method, reconstruction selector, 100% cpu occupancy solve bug.

rebuildSelector method to create a new selector by openSelector method. Then the old selector of selectionKey execution cancel. Finally, the old selector of the channel to re-register in the new selector. After the rebuild, the need to re-execute method selectNow, check whether there have been ready in selectionKey.

When the next call processSelectedKeys method (handle I / O tasks), when selectedKeys! = Null, call processSelectedKeysOptimized method, iterative selectedKeys get selectkey ready IO events stored in an array selectedKeys, and then for each event are calling processSelectedKey to handle it , processSelectedKey respectively processing OP_READ; OP_WRITE; OP_CONNECT event.

The last call runAllTasks method (non-IO task), this method first calls fetchFromScheduledTaskQueue ways to bring in more than scheduledTaskQueue delay task execution time to move taskQueue waiting to be executed, followed by taking the task execution from taskQueue, each perform 64 tasks , time-consuming checks, if the time exceeds the predetermined execution time is execution, stop performing non-IO tasks, too many tasks to avoid non-IO, IO affect the execution of the task.

Each NioEventLoop one thread and a Selector, NioServerSocketChannel will take the initiative to register a NioEventLoop of a Selector, NioEventLoop responsible for event polling.

Outbound request events are events, the initiator is Channel, is the unsafe handler, event notified by Outbound propagation direction tail to head. Inbound event originator is unsafe, the event handler is the Channel, is a notification event, the propagation direction is from start to finish.

Memory management, a pre-application will first chunk of memory Arena, Arena composed of many Chunk, Chunk default and each page consisting of 2048. Chunk AVL tree organized by Page, a Page each leaf node represents, while the intermediate node represents a memory area, recording its own node offset address in the entire Arena. When the region to be dispensed, the intermediate node flag is marked, so that all the nodes represent intermediate nodes have been assigned the following. Greater than in poolChunkList 8k memory allocation, whereas PoolSubpage for allocating memory is less than the 8k, it will page into a plurality of pieces, for memory allocation.

ByteBuf features: support automatic expansion (4M), the method put to ensure no exception is thrown by the built-in type composite buffer, copy zero (zero-copy); do not call Flip () to switch read / write mode, read and write index points
apart; chain method; reference count-based garbage collector for AtomicIntegerFieldUpdater; PooledByteBuf binary tree to realize a memory pool, centralized management of the memory allocation and release, without a new buffer every use object. UnpooledHeapByteBuf every time a new buffer

Published 74 original articles · won praise 233 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_43107323/article/details/104842078