One of the basic principles of SQL on Hadoop design is to move the compute task to the node where the data resides rather than vice versa

Source: Internet
Author: User

3.1 Data localization

One of the basic principles of SQL on Hadoop design is to move compute tasks to the node where the data resides rather than vice versa . This is mainly for the purpose of network optimization, because the data distributed in different nodes, if the mobile data will result in a large number of inefficient network data transmission. Data localization is generally divided into three types: node Locality, rack locality (Rack Locality), and global locality (Globals Locality). Node locality is the assignment of computing tasks to the node where the data resides, without any data transfer, which is the best efficiency. Rack locality refers to moving the computing task to the rack where the data resides, although the computing tasks and data belong to different computing nodes, but because the internal network transmission speed of the rack is significantly higher than the network transmission between the racks, the rack locality is also a good way. Other situations are global in nature, which requires network transmission across racks, resulting in very large network transport overhead.

Scheduling system in the task scheduling, should be as far as possible to ensure the node locality, and then the rack locality, if the above two can not be satisfied, the dispatch system will also be transmitted over the network data to the Computing task node, although the performance is relatively inefficient, but also more than the resource vacancy is better.

In order to realize the localization of data, the scheduling system will combine the scheduling algorithm with the delay scheduling. The core idea is to prioritize the calculation task to the node I where the data resides, and if node I does not have enough compute resources, wait a few seconds and if node I still does not have compute resources available, then discard the data localization to dispatch the compute task to other compute nodes.

3.2 Reducing the materialization of intermediate results

In a low latency SQL on Hadoop system, the ability to minimize intermediate results of disk materialization can greatly improve query performance. For example, the Hive execution engine uses pull to fetch data, which has the advantage of fine-grained fault tolerance, with the disadvantage that the downstream mapreduce must wait for the upstream mapreduce to completely write data to disk before the pull data can begin. Presto uses push to obtain data, the data is completely streamed between different stages of transmission, intermediate results do not need to be materialized to the disk , so that the Presto has a very efficient execution speed, the disadvantage is not to support fine-grained fault tolerance .

(Click to enlarge image)

Figure 3push and pull

3.3 Column Storage

The traditional relational storage model stores the columns of a tuple continuously, even if only one column is queried, and the entire tuple needs to be read, and it can be found that performance is very low when there are only a few columns in the query.

The idea of Columnstore is to divide a tuple vertically into a collection of column families, each of which is stored independently, and the column family can degenerate into a trivial column family that contains only one column. When querying a small number of columns, the Columnstore model can greatly reduce disk IO operations and improve query performance. When a queried column spans multiple column families, it is necessary to stitch the column data stored in different column families into raw data, resulting in a large amount of data being transmitted across the network due to different column families being stored on different HDFS nodes, thus reducing query performance. therefore, when you actually use the column family, the frequently accessed columns are usually placed in a column family based on the characteristics of the business query .

In the traditional database field, the column storage has been studied very deeply, and many research results have been applied to the industrial field, including the lightweight compression algorithm, direct manipulation of compressed data, delay materialization, vectorization execution engine. But throughout the current SQL on Hadoop system, these technologies are still far behind the traditional database, in some recent SQL on Hadoop has been added to the quantization execution engine, lightweight compression algorithm, but such as direct manipulation of compressed data, Technology such as delayed decompression has not yet been applied to the SQL on Hadop system. For more information about Columnstore, see [20].

Column Storage compression

The Columnstore compression algorithm has the following characteristics:

The compression ratio of the Columnstore model has a very high compression ratio, typically up to 10:1, while the row storage compression ratio is typically only 4:1. 4:

(Click to enlarge image)

Figure 4 Heavy-weight compression algorithm

The lightweight compression algorithm (Leight-weight Compression) lightweight compression algorithm is CPU-friendly. The row storage model can only use Zip,lzo,snappy and other heavyweight compression algorithms, the biggest disadvantage of these algorithms is that the compression and decompression speed is slow, usually can only extract up to hundreds of trillion data per second. Instead, the Columnstore model can use not only the heavyweight compression algorithm, but also some very lightweight compression algorithms, such as the Run-length encode,bit Vector. the lightweight compression algorithm not only has a good compression ratio, but also has a very high compression and decompression speed . Lightweight compression algorithms such as Bit packing,run-length enode,dictionary encode are now supported in ORC File and parquet storage.

Direct manipulation of compressed data (Operating Directly on compressed data) when using a lightweight compression algorithm, it is possible to get the results directly without decompression . For example: The Run Length Encode algorithm compresses consecutive characters into characters and characters, such as AAAAAABBCCCCAAAA will be compressed to 6A2B4C4A, where 6a indicates a continuous 6 characters. Now suppose a column contains the compressed string above, and when you execute SELECT COUNT (*) from table where columna= ' a ', you do not need to unzip the 6a2b4c4a to know the number of a is 10.

It is important to note that since row storage can only use the heavyweight compression algorithm, direct manipulation of compressed data cannot be applied to row storage.

Delay decompression the data in the parquet is stored in blocks, each block stores a lightweight index such as the minimum, maximum, and so on, such as a block with a minimum maximum value of 100 andthree, which indicates that any one of the blocks of data is between 100 and 120, so when we execute Select column A from table where v>120, the execution engine can skip the block without extracting it and then filtering the data. Conversely, in a row store, the data block must be fully read into memory, decompressed, and then data filtered, resulting in unnecessary disk read operations.

3.6 compression

In general, compressing the files in HDFS can greatly improve query performance. Compression can reduce the storage space occupied by data, reduce the read and write of disk IO, improve the speed of data processing, in addition, compression can reduce network transmission and improve network transmission speed. In SQL on Hadoop, compression is primarily applied to data sources in HDFS, shuffle data, and ultimately calculating results.

If the application is Io-bound , compressing the data can increase the speed of data processing because the compressed data becomes smaller, so it can increase the data read and write speed. The main requirement is that the compression algorithm is not the higher the compression ratio is better, the higher the compression rate of the algorithm compression and decompression speed is slower, the user needs a good balance between CPU and IO. For example, GZIP2 has a very high compression ratio, but its compression and decompression speed is very slow, and may even exceed the data uncompressed read and write time, so there is no SQL on hadooop system using GZIP2 algorithm, currently in the SQL on Hadoop system, the popular compression algorithm is mainly: S Nappy,lzo,glib.

If the application is Cpu-bound, then it is important to choose a compression algorithm that can be splittable , if a file is Splittabe, then the file can be cut into multiple data blocks that can be read in parallel, so that MR or Spark When a file is read, a task is assigned to each block of data to read the data, which increases the speed of the data query.

3.7 Vectorization Execution Engine

Query execution engine is a core component in a database that transforms a query plan into a physical plan and evaluates it back. The query execution engine has a large impact on the system performance of the database, and the main execution engine currently has the following four classes: volcano-style,block-oriented processing,column-at-a-time,vectored iterator Model These four execution engines are described below.

Volcano-style, the oldest query execution engine was the Volcano-style execution engine (volcano execution engines, volcano models), also called the Iterative model (iterator model), or one-tuple-at-a-time. In this model, the query plan is a tree or DAG composed of operator, where each operator contains three functions: Open,next,close. Open is used to request resources, such as allocating memory, opening files, and close to releasing resources, and next method recursively calls the next method of the operator to generate a tuple. Figure 1 depicts the query plan for the volcano model of select Id,name,age from people where age >30, which contains user,project,select,scan four operator, each operato The next method of R recursively invokes the next of the child nodes, always recursively calling to leaf node Scan Operato,scan Operator to return a tuple from the file.

(Click to enlarge image)

Fig. 3-4 Volcano model excerpt from the literature [2,page 39]

The main disadvantage of the volcano model is the expensive interpretation overhead (interpretation overhead) and the low CPU Cache hit rate. First, the next method of the volcano model is usually implemented as a virtual function, in which the virtual function call needs to look up the virtual function table, and the virtual function call is a non-direct jump (indirect jumps), which results in a bad CPU branch prediction (brance misprediction ), a bad branch prediction requires more than 10 cycles of overhead. Volcano model in order to return a tuple, multiple next methods need to be called, resulting in expensive function call overhead. [] studies have shown that the Tpc-h Q1 query is performed in MySQL using the volcano execution model, and only 10% of the time is spent on real query calculations, while the remaining 90% time is wasted explaining overhead (interpretation overhead). Next, the next method returns only one tuple at a time, usually with row storage, 3-5 row Format, and each access causes the CPU cache hit to fail (assuming that the row cannot be fully placed in the CPU cache) if the first column is accessed in a sequential manner. If Column Format is used, the cache hit failure occurs only when the first value is accessed, and subsequent accesses 2 and 3 o'clock will successfully hit the cache, greatly improving query performance.

(Click to enlarge image)

Figure 3-6 Row and column storage

block-oriented processing,block-oriented processing model is an improvement to the volcano model, which returns a batch of tuples at a time of next call, the number of tuples varies from 100-1000, next Internally, a loop is used to process this batch of tuples. In the volcanic model in Figure 1, the Select operator next method can be implemented as follows:

Def next (): array[tuple]={  //Call the next method of the child node and return a tuple vector that contains 1024 tuples  val tuples=child.next ()  Val result=new Arraybuffer[tuple] for  (i=0;i<tuples.length;i++) {30= "" age= "" val= "" >30 "result.append (tuples (i))  }  result//return result}

The advantage of the block-oriented processing model is that it returns multiple tuples at a time, reducing the cost of interpretation and proving that the CPU cache is increased by the hit rate, which is loaded into the CPU cache when the CPU accesses a column in the tuple (if the tuple is large Smaller than the size of the CPU cache row), access to subsequent columns is taken directly from the CPU cache, resulting in a high CPU cache hit rate, but the CPU hit rate is still not ideal if you access a column or a few columns. One of the biggest drawbacks of this model is the inability to take full advantage of modern compiler techniques, such as in the above loops, where it is difficult to manipulate data using SIMD instructions.

Column-at-a-time model, to quantify the earliest history of implementation can be traced to monetdb[], in MONETDB proposed a query execution model called Column-at-a-time, the model each time the next call returns one or more columns, Each column is returned as an array. The advantage of this model is that it has very high query efficiency, and the disadvantage is that a column data needs to be materialized into memory and even disk, resulting in high memory footprint and IO overhead, while data cannot be placed in the CPU cache, resulting in a lower CPU cache hit rate.

Vectored iterator Model,vectorwise proposes a vectored iterator model, which is an improvement to column-at-a-time, and next call does not return a complete column, Instead, it returns a vector that can be placed into the CPU Cache. This model avoids the disadvantage of low column-at-a-tim CPU Cache hit rate. The biggest advantage of the vectored iterator model is that it can use the runtime compiler (JIT) to dynamically generate instructions that are more suitable for modern processors, such as JIT can generate SIMD instructions to handle vectors. Consider tpc-h Q1 query: SELECT l_extprice* (1-l_discount) * (1+l_tax) from LineItem. The execution plan for this SQL query is as follows:

(Click to enlarge image)

The next method of Project operator can be implemented as follows (Scala pseudo code):

Def next (): array[tuple]={  val tuples=child.next ()  var result=new arraybuffer[int] for  (i=0;i< tuples.length;i++) {r= "tuples.l_extprice* (1-tuple.l_discount) * (1+tuple.l_tax)" retult= "" tuple= "tuples (i)" val= "" >

In recent years, some SQL on Hadoop systems have introduced vectorization execution engines, such as Hive,impala,presto,spark, although the implementation details are different, but the core idea is the same: return multiple data at the next method call as much as possible. Then use dynamic code generation techniques to optimize loops, expression calculations to reduce interpretation overhead, increase CPU Cache hit rates, and reduce branch forecasts.

The vectorization execution engine in Impala, which essentially belongs to the block-oriented Processing,imapla, returns a batch of tuples that still have a low CPU Cache hit rate and are difficult to optimize with instructions such as SIMD, To alleviate this problem, Impala uses dynamic code generation techniques to optimize for cycle, expression calculations, and so on using dynamic code generation.

In Spark2.0, a parquet-based vectorization execution engine [12] is implemented, which belongs to the vectored iterator model, which returns a batch of tuples in Columnstore format when the next method is called, and can be used to process the batch tuple using a loop. In addition, to take advantage of modern CPU features, Spark also supports full-stage code generation techniques, with the core idea of compiling multiple operator into a single method, thus reducing the cost of interpretation.

3.8 Dynamic code Generation

Dynamic code generation is generally used in conjunction with the vectorization execution engine, because the next method of the vector execution engine can use a for loop to handle tuple vectors or column vectors, and dynamic code generation techniques can generate more efficient execution code for the next method at run time. The research proves that the vectorization execution engine and the dynamic code generation can reduce the explanatory overhead (interpretation overhead), see document [18], the main impact of the following three aspects:

    • Select, when complex expression calculations are included in a SELECT statement, such as Avg,sum,count,select's computational performance is primarily affected by CPU caches and SIMD instructions. When the data cannot be placed on the CPU cache, the CPU spends most of its time waiting for data to be loaded from memory to the CPU cache, so the CPU can greatly improve the computational performance when it performs the calculations required by the CPU cache. An SIMD instruction can compute multiple data at the same time, so performing an expression calculation with SIMD instructions can improve computational performance.

    • Where, unlike the Select statement, where statements generally do not require complex calculations, the effect of where performance is more branch prediction. If the CPU branch predicts an error, then the previous CPU pipelining will be fully cleaned, and a CPU branch prediction error may waste at least more than 10 instruction cycle overhead. By using dynamic code generation techniques, the JIT compiler is able to automatically generate branch predictive friendly instructions.

    • The Hash,hash algorithm affects the query performance of Equal-join,group, and the hash algorithm has a low CPU Cache hit rate. [18] describes a cache-friendly hash algorithm, can significantly improve the hash calculation performance.

There are two types of dynamic code generation: the C + + system and the Java system. where the C + + system can directly generate the cost machine executable binary code, and be able to produce efficient SIMD instructions, such as Impala using C + + implementation of the query execution engine, while using the LLVM compiler to dynamically generate native executable binary code, LLVM can generate SIMD instructions to the expression execution meter Count The Java system generates Java bytecode dynamically using the reflection mechanism, which in general cannot be optimized with SIMD instructions, and Spark generates Java bytecode dynamically using the reflection mechanism, which is often difficult to use directly with SIMD for expression optimization. In addition, the full-stage code generation (Whole-stage code Generation) technology provided in Spark2.0 is also a dynamic code generation technique that compiles multiple Operator into one method for optimization.

It is important to note that dynamic code generation technology is not always the panacea, in that Impala's dynamic code generation technology does not improve the query speed of Tpc-ds q42,q52,q55, mainly because there is no complex computation in the SELECT statements of these SQL statements.

One of the basic principles of SQL on Hadoop design is to move the compute task to the node where the data resides rather than vice versa

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.