Apache Traffic Server architecture

Source: Internet
Author: User
Tags hosting
function:

The main function of Trafficserver is caching, and of course you can use it as a pure reverse proxy (as usual with nginx). Often the best way to cut into a large system is to see how it works, with two main portals using traffic server: Profiles and Plug-ins. All users need a profile, and a plug-in is required for advanced users.

Traffic support for large-scale cluster processing, unlike the Nginx single point (which requires OSPF equalization links for redundancy), all profiles can be changed to one notification all. The program is divided into several subroutines according to the function, there are the procedures used in the service running, and the management is used. See below for details. 

configuration file:

L Cache Cache.config

n Upstream pull data congestion control: Congestion.config

N Cache partitioning and upstream allocation: Hosting.config

n divides different kinds of cache types (with Hosting.config can implement different kinds of data caching arrangement)

n defines the upstream Peer:Icp.config

n definition can use the cache white list: Ip_allow.config

N Cache can define multilevel, define level configuration: Parent.config

N Cache Persistence: Storage.config

L Log Configuration

N put the different upstream log in a different log file: Log_hosts.config

n defines a different log format: Logs_xml.config

N

L Plugin Management Plugins.config

L Main program can adjust parameter: records.config

L Agent:

n Request and Response URL modification configuration: remap.config

L Domain Name resolution: Splitdns.config

L Security: Configure multiple SSL certificates: Ssl_multicert.config 

plug-in system:

Standard procedure-oriented process for plug-ins. An HTTP has a processing process, including Request header processing (you can change the URL), DNS query (you can decide which background to get the data), from the background or cache pull data, return content, etc. As long as it is an HTTP request, the process is fixed. So the plug-in system registers the callback function on these processes. The callback function here is not a direct call, but it also passes an event argument to indicate what is happening on the current hook so that plugin can handle it better.

In addition to being invoked, Trafficserver also provides the calling method. The invocation method provided here is not a function call in general sense, but rather a remote procedure call. The plug-in sends the Code (action) that it wants to execute to the server (even sending is to indicate the IP address and port), and then gets the state of the action execution by querying the interface returned by the server. The action here is the traffic concept within the server, and the entire process resembles the Golang go func () {} () keyword operation.

In addition to this remote invocation, many function plug-ins can also be called directly.

Co-process:

Trafficserver's hyper concurrency naturally requires the concept of a coprocessor (Ng is also). Traffic server's own implementation of the coprocessor is called continuation, the structure is represented by Tscont.

A tscont represents an asynchronous execution block of code that has its own execution state and can be

A coprocessor is a user-space-managed thread, which means that the scheduling algorithm is implemented in user-space programs. You can save the state of your program execution, and you can pull it out at some point. Multiple threads execute on a thread on an operating system, or an M thread executes on n threads. The advantage of this is that it can be willful congestion without worrying about the waste of resources. So the process is essentially a way of coping with blocking calls. Other important ideas are also asynchronous. It seems that the operating system prefers to be asynchronous rather than a coprocessor.

The Trafficserver is based on a large number of asynchronous, but up-supply concurrency is heavily based on the concept of a coprocessor.

Plug-in type:

Content transformation

Content transformation is to modify the content of the request or response content. Because the content is longer, the traffic server defines vconnection (structural tsvconn) and vio. The vconnection represents a connection from a buffer to another buffer, through which data can be changed according to the change method specified by the connection. This is also the essence of content transformation. In essence Tsvconn is a continuation, so also have the continuation have the ability to inform the data.

And vio is vconnection ends. An input of output, due to multiple vconnection serial, so a vconnection output vio can be another vconnection input. The essence of Vconnection is transformation, and the essence of vio is memory buffer.

Other Protocol Plug-ins

This is the bottom of the comparison. General Plug-ins are services to the HTTP protocol, you can also skip the HTTP protocol to support other protocols, or to support other protocols above HTTP. Courseware traffic server's confidence in its network infrastructure.

Plug-in improvements:

Each plug-in must contain the voidtsplugininit (int argc, const char *argv[]) function, familiar with C is easy to understand, fixed names and parameters corresponding to the fixed symbol table symbol, when the plug-in is loaded, The main program can be executed directly by this symbol table, which is the entrance.

1. Register the plugin to the main program: Tspluginregister. Can not be registered, mainly for compatibility

2. Add hook callback to a global hook position: Tshttphookadd

A The registered hooks may be global or trasaction, session-related. If it is transaction related, through Tshttptxn TXNP = (TSHTTPTXN) edata, obtain a pointer to the transaction. Add Transactionhook using the Tshttptxnhookadd function.

b If the session is relevant, use Tshttpssnhookadd for registration. The method of obtaining session in plugin becomes tshttpssn ssion = (tshttpssn) edata;

The plug-in allows you to initiate a network connection, using Tsnetconnect () to initiate an HTTP connection that connects only traffic server, and Tshttpconnect () initiates an HTTP connection to any address. 

Cache System:

The use of ATS is nothing more than reverse proxy and cache two, where the cache is the most important ATS function. To understand the ATS cache structure, it is good to understand several key words and concepts.

There is the ATS cache can be composed of clusters, there are two ways: one is configured to share, one is the unified cache. Configuration sharing is only to ensure that the configuration of each node is the same, each node cache or their respective caches (repeat is certain), and the unified cache is the collaboration between the various nodes in the sort of cache, in this machine can not find automatically to pull data next door.

ATS do not use the file concept when using the disk, so you can directly use the bare disk (if you use a file, there is only a large file), ATS will arrange the use of disk and data organization.

Concept:

Agent: 

HTTP requests are not necessarily all retrieved from the server and can be cached near the user's room. Especially the picture video and other resources, this caching process is called Proxy.

Freshness: 

The agent must ensure that the cached data is current, and how to confirm with the upstream server is a specialized topic. The HTTP protocol itself provides a number of headers to control the freshness of the cache. such as Max-age, last-modified, expires, date and so on. ATS calculates the freshness of an object based on the configuration of the head and the user, decides whether to, when to go to the Server section pull (when it is about to expire to pull the call Fuzz revalidation).

Cache control: 

You can control some do not cache, when the upstream server congestion when the stop pull, the same URL cache different versions.

Caching level: 

Cache itself can also be layered, like the CPU's first-level cache, the concept of level two caching. In ATS, this concept is called parent, each cache can have sibling can also have parent,cache in the local search, or in the local cluster can not find, will go to the parent to check, parent can't find the source back to the background server query. These caches can also use the ICP Protocol (the Cache Control Protocol) to query the cache status in parent or sibling to update their cache.

Keywords:

L Bare Disk: ATS cache support hardware storage, not only supporting file system files, but also support the bare disk. The bare disk support may be removed in Linux because direct access to the disk can be replaced by O_direct. The bare disk is actually the disk that does not use the file system, because there is no file system nature also does not have the concept of the file. In the kernel is to go directly to the SD drive, elevator layer, to SCSI to disk, do not need to go to the file system layer and cache layer. The same is true of O_direct.

L Cache Span: a continuous physical storage space, typically a disk.

L Cache Volumn: A logical and business storage space that can span multiple cache spans. This is like a logical partition that LVM divides across multiple physical disks.

L Cache Strip: A piece of storage tape located on cache span (volumn). The data is organized in the cache strip.

L cache ID, cache Key:cache key is uniquely labeled a caching object, generally by URL, cache ID is calculated from the cache key 128-bit MD5 value.

l  directory: Data in the cache strip is organized by your directory. There are multiple directory in a cache strip, with multiple entries in each directory. Each directory corresponds to a cache, indexed by the cache ID. But directory is just an index of the cache, and directory allows you to find information and entities that are in the cache on disk. And all of the directory is loaded into memory, so if a cache query result is miss, you do not need a disk (through the URL to calculate the cache ID, but query memory found that the cache ID does not correspond to directory), you can return. So the existence of directory allows the miss process to accelerate, but if directory is found, each cache query needs to read the disk next. It is noteworthy that only directory in memory, does not contain entities, and directory size is fixed, the disk is also fixed, as long as the program started to create as many as possible directory, Therefore, the program is running in the process of the ATS memory requirements will not be increased (because the number of cache can be supported is fixed, each cache in memory of the record size is also fixed).

l  segment, bucket: It is not the strip below that is the next directory, and the directory is organized. 4 directory is a bucket, multiple bucket is a segment. On each strip head there is an idle list of every segment directory free list, that is, how many segment are on the strip's head. In fact, the cache ID does not locate the directory, but the Bucket,strip free list does not contain the first directory of each bucket, but the order contains the 4th, 3rd, and 2nd. So, a cache ID (128-bit) of cache object can be positioned to navigate to a bucket, and then see if the bucket's first directory is used, If used shows that the entire bucket is full (only the last 3 are used up to use the 1th), the cache object adds a failure. Otherwise, it will begin to be used sequentially from 4/3/2/1. So, it can be seen that bucket is actually a hash bucket, used to come out of the hash function of the collision, only gave 4, the description can only handle 4 cache ID consistent situation. So the segment and bucket of these two kinds of organization structure are introduced in order to solve the management problem.

L Content: We know that directory is only metadata, is to resident memory, store the cache index. So you can determine whether a cache exists according to your directory. If you find the corresponding directory, you have to go to the directory's corresponding cache of the real content, which is placed in the content, location by the directory specified. The number of directory is dynamically computed, the total size divided by the average size of an object can be obtained, the average size of an object can be set by Proxy.config.cache.min_average_object_size, thereby controlling the number of directory. The size of the content is dynamic and finite, so when the content is full it will automatically start covering from the beginning. However, directory is not updated. Update your directory until the next time you read to directory that the content does not exist. One problem here is that it's not accurate to look at the statistics of your directory, and once you've run full volumes of data.

L Fragment: Because of the parallelism of ATS, it is impossible to store too much continuous data. So large files must be fragmented (otherwise concurrent to many large file cache requests will not be able to deal with). We know that directory will point to the location of the data in the content, here is only the first fragment of the cache, in this fragment the head of a lot of information, including other fragment where to find, Also includes other storage directory versions of the same name (for example, PNG, JPG versions of the same URL)

L SPDY: Users with the same IP HTTP communication, whether or not the same Web site, are reused a TCP connection. This is useless in most cases, but the user is larger when using the agent. Because all HTTP requests from the user are sent to the agent, the protocol can be used to run HTTP all day using only one TCP connection, and the HTTP stream of each network war is only one stream inside the TCP stream. This greatly improves the efficiency of the agent and reduces the burden on the agent and the client.

Cluster

Multiple cache can be configured as a cluster, and the complete cluster pattern includes configuration file unification, and node data interaction. The configuration files for each node in the cluster are the same, so do not appear in the configuration file for native IP. When configured as cluster mode (this requires a separate configuration for each machine), configuration modifications to any one node are automatically synchronized to the other nodes. Synchronous configuration uses multicast, exchanging data using unicast. 

source code and schema:

The core code inside the Iocore, Iocore inside according to the big function divides into the following several modules.

This is going to start with the architecture of Trafficserver. These directories are almost traffic server keywords: asynchronous (AIO), caching (cache), cluster support (cluster), Domain name resolution (DNS), Event System (Eventsysytem), upstream configuration (HOSTDB), Network (NET).

Comparison of network and Nginx: http://www.cnblogs.com/liushaodong/archive/2013/02/26/2933535.html

In addition to providing the core program features, the program requires a portal, the portal is generally a startup server main program and a number of management procedures, management programs are in the CMD directory, each directory is a management program:

The main program is located in the proxy directory main.cc (. cc suffix is the C + + file glibc suffix representation). 

Common Commands

The main program name is Traffic_server,

Traffic_manager: Providing services for TRAFFIC_CTL

TRAFFIC_COP: Independent monitoring procedures, monitoring traffic_server and traffic_manager responsibilities and memory swap space usage, found abnormal restart process.

Traffic_crashlog: Started by the traffic_server process, printing a crash report to the log directory when the traffic_server crashed.

Traffic_ctl: Configure some Traffic_server parameters to be configured online

Traffic_logcat: Converts a trafficserver binary log file into a readable ASCII log

Log analysis tool for Traffic_logstats:trafficserver

Traffic_via: You can configure PROXY.CONFIG.HTTP.INSERT_REQUEST_VIA_STR, Proxy.config.http.insert_response_via_ STR two parameters enable all the HTTP headers of the data to carry via information (indicating the cache state, which can be seen from whence), wget This file will see this information in the HTTP header, and this information is encoded by traffic server, You can use the Traffic_via command to decode this information to see the cached fetch path.

Traffic_sac:standalonecollator. Log collector, used in the traffic server cluster. The logs that can be used to collect individual nodes are centralized to native processing. One node can not install Traffic server, only the SAC can be installed to play a greater log capacity.

Tspush: You do not need a user request to use this command to post content to the cache of traffic server and use this command to open the proxy.config.http.push_method_enabled option

TSXs: Plugin compile program. Used to compile and install Plug-ins.

Traffic_top: A convenient procedure for viewing the internal state of the current trafficserver. To compile this you must have a Libncurses5-dev library, otherwise it will not be installed silently. 

Architecture

Trafficserver Although most of the situation is running on Linux, but it is cross-platform. The typical operating system provides access to the network, but does not provide a large number of concurrent network access (perhaps the future operating system API can provide this function directly), so handling a lot of concurrency requires the program itself (some programming languages encapsulate this part of the logic, such as Golang, It's a matter of saving the program. The most common way to deal with this problem at present is to abstract the upper layer.

In addition to network access, the operating system generally does not provide particularly Easy-to-use event systems, DNS systems, caching systems, cluster systems and other interfaces. However, these are the underlying services that the traffic Server core functionality relies on. Traffic server's response is to encapsulate these services, the main logic being all based on encapsulated services, rather than the operating system APIs (it has to be said that the operating system and GLIBC APIs are not up-to-date ...). Others have to do it themselves. 

Upper Design

Http://dev.fastweb.com.cn/wordpress/?p=28

Code Analysis:

/proxy Directory Main program:

The core meaning of the program is known by using this directory of naming the main program.

You can see that the binaries for the generated main program are also in this folder. 

HTTP Transaction Flow:

Must understand is Trafficserver is a program, the main business is countless transaction, each transaction is a user's HTTP connection processing, not only contains the user's TCP connection, but also contains the traffic Server and back-end communication and local operations, and so on. A transaction is just a transaction performed by a user on a TCP connection, and a session concept that is a TCP conceptual connection between a client and a server. A session can include many transaction. For users, a request and response is a transaction.

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.