Thrift source code analysis (6) -- transport Transport Layer Analysis

Source: Internet
Author: User

As a special network programming method, RPC encapsulates a layer of transport layer to support underlying network communication. Thrift uses transport to encapsulate the transport layer, but transport is not only the underlying network transmission, but also the encapsulation of the upper stream.

In terms of the transport design, both the IO stream and the network stream are in the category of I/O. It is not necessary to abstract them using a unified interface, but I personally feel that when I look at thrift's code, the transport is used to represent the stream. I don't know whether it is a normal Io stream or an underlying network stream. It is better to use Java to isolate common I/O and network interfaces with different abstractions. At least the code logic is clearer.


Let's talk about the class structure of trasport. TTransport is the top-level abstraction. It uses abstract classes and does not use interfaces. I personally feel that this practice is still better than using interfaces as top-layer abstraction, and better interface scalability.

There are several considerations:

1. The structure of tiostreamtransport and tsocket correspond to blocking synchronous Io, and tsocket encapsulates the socket interface.

2. tnonblockingtrasnsort and tnonblockingsocket correspond to non-blocking Io

3. tmemoryinputtransport encapsulates a byte array byte [] to encapsulate the input stream.

4. tmemorybuffer uses bytearrayoutputstream, a byte array output stream, to encapsulate the output stream.

5. tframedtransport encapsulates tmemoryinputtransport as the input stream and tbytearryoutputstream as the output stream as an encapsulation of the memory read/write buffer. In the flush method of tframedtransport, the length of the output stream of four bytes is written as the message header, and then the message body is written. Corresponds to the read message of framebuffer. When framebuffer is used for a message, it first reads the length of four bytes and then the message body.

6. tfastframedtransport is a memory read/write cache zone with a higher memory utilization. It uses an Automatically increasing byte [] (not long enough to be new) instead of a new byte [] each time. improves memory usage. Like tframedtransport, flush also writes a four-byte message header to indicate the message length.



Like java I/O, thrift's transport also uses the decorator mode to implement the so-called packaging stream. We also use the concepts of encapsulated stream and node stream to differentiate each transport.

The node stream indicates that it uses byte [] to provide Io read/write classes:

Autoexpandingbufferreadtransport

Autoexpandingbufferwritetransport

Tmemoryinputtransport

Tbytearrayoutputstream

Tmemorybuffer


Two network-related objects are special. We can also think of them as node streams, which are objects that directly perform network read/write operations.

Tnonblockingsocket

Tsocket


The encapsulated stream encapsulates other transport classes that provide Io read/write:

Tframedtransport

Tfastframedtransport


The packaging stream provided by Thrift is mainly two transort whose names start with tframe. When writing the message flush, the two transport will add a four-byte message header to indicate the length, when reading a message, the system first reads a four-byte message header.


Since the NIO server of thrift uses framebuffer as the buffer when reading messages and first reads the 4-byte message header when decoding, it can be inferred that when the client sends messages, the tframedxxxtransport packaging stream is used to transmit data.

Let's take a look at the actual client Object Construction

            TSocket socket = new TSocket(host, port);            socket.setTimeout(timeout);            TTransport transport = new TFramedTransport(socket);            TProtocol protocol = new TCompactProtocol(transport);            transport.open();

In addition, when talking about the thrift protocol, thrift is bound to a specific transmission object. The protocol uses a specific transport to read and write data.


Thrift source code analysis (6) -- transport Transport Layer Analysis

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.