当前位置:网站首页>Interesting erasure code
Interesting erasure code
2022-06-24 12:34:00 【Wang Lei -ai Foundation】
Redundant backup technology
Before learning about erasure codes, let's learn about the other two commonly used redundant backup technologies : raid and Multiple copies
RAID
RAID yes "Redundant Array of Independent Disk" Abbreviation , Independent redundant disk array Is an ancient disk redundant backup technology , Maybe you never understand the principle , But I must have heard of its name . Simply explain , Will be N Hard disks pass RAID Controller( branch Hardware,Software) Combined with a virtual single high-capacity hard disk , Its characteristic is N Both hard disks can be read faster and provide fault tolerance .
RAID Depending on the implementation technology, there are RAID0~9, RAID10 wait . What is used more at present is RAID 0, RAID 1, RAID 5, RAID 10.
This technology is very effective and reliable in protecting single node disk data , At the same time, there are special computer chips to support the realization of hardware RAID Algorithm , Improve efficiency and reduce resource footprint . Single use supports redundancy RAID When the algorithm , Single disk damage is caused by RAID Algorithm can be recovered , however 8T+ The recovery time of disk can be as long as several days . meanwhile RAID The flexibility of the algorithm in the cloud age is a little poor .
Multiple copies
Multi copy technology is relatively simple and direct , Since you want to redundantly protect critical data , Just save a few more , It doesn't matter how bad a single data is , There are also backups that can be used .
This technology can be seen everywhere in modern software systems , such as mysql Synchronization of / Asynchronous backup , Three copy storage in distributed database + Agreement of conformity .
The advantages and disadvantages of this method are also obvious , The advantage is high write efficiency , No extra calculations are required , Just save multiple copies directly , Data recovery is also fast , Just copy from the replica . The disadvantage is low storage efficiency , The disk capacity previously required is directly X2 perhaps X3 了 , Cost is very high .
erasure coding
ErasureCode( Erasure code ) Provide approximately three copies of reliability at a lower cost , Attract many distributed storage / Manufacturers and users of cloud storage . It can be said that erasure codes are cloud storage , Especially the core of object storage, which is widely used nowadays . Erasure code (Erasure Coding,EC) Is a coding fault tolerance technique , The earliest solution was to solve the problem of loss of some data in transmission in the communication industry . The basic principle is to segment the transmitted signal , Add a certain amount of verification, and then make the segments related to each other , Even if some signals are lost during transmission , The receiver can still calculate the complete information through the algorithm . In the data store , Erasure codes divide data into segments , Expand and encode redundant data blocks , And store it in different locations , For example, disk 、 Storage nodes or other geographical locations .
Now think about a question : Now there is 2 Copy of the data ( It is possible that a piece of data is actually divided into two parts ), Allow you to do 2 Redundancy of ( The actual use of storage is to store data (2+2)/2 = 2 times ), It is required to achieve such an effect : Any two copies of data are lost , Data can be recovered .
How to solve this problem . A simple idea is , Back up both data .
Store data as X1, X2, X3, X4 Respectively equal to A1, A2, A1, A2; Let's assume that the data X1 X2 lost , Data can be obtained from X3,X4 Recover from . But there are problems with this storage : If the missing data happens to be X1, X3 Well , So the original data A1 It is completely lost and can not be found . But you can use one of the following storage methods X1, X2 Still the same ,X3=A1+A2, X4=A1+2*A2 In this way, any two copies of data are lost , Can recover A1 and A2 了 .
This is the core principle of erasure code , Of course, it's not that simple , This algorithm is just a simplification . In practice, a method called RS Code method , According to the set m, n value , Generate a RS matrix , Store through RS The matrix calculates the check block , Single arbitrary m When block data is corrupted , Through the data blocks that still exist , and RS The inverse matrix of a part of the matrix of , All data can be recovered . Please refer to for specific calculation principle This article
Total data block = Raw data block + Check block namely n = k + m, Erasure code technology allows arbitrary in data storage m Recover data from a scene where blocks are damaged .
The practical application and improvement of erasure code
According to the introduction above , We know that the core parameter of erasure code is n and m The ratio of the , We call this value redundancy , The higher the redundancy , The more data blocks allowed to be corrupted , The safer it is , At the same time, the higher the cost of data storage . meanwhile k Value is also important , This value determines the granularity of data chunking ,k The smaller the value. , The smaller the data dispersion , The larger the fault influence surface is , The greater the cost of reconstruction ;k The bigger the value is. , Multiple copies of data increase the network and disk load , The greater the cost of reconstruction .
- EMC Object storage system ECS 12 + 4 and 10+2 The redundancy is 1.33 、 1.2
- Alibaba cloud Pangu cluster chunk Storage 8+3 Redundancy =1.375
- Google RS(6,3) in GFS II (Colossus)
Suppose we want to provide (k, m) = (12, 4) Redundancy of , The redundancy is (12+4)/12=1.33. There is a problem with using the method described above directly , When a single data is corrupted , You need from 12 Transfer data in data blocks for calculation , Network required IO It's big .Azure The improvement is ,12 The block is divided into two groups , Each group 6 block , First work out a local Check block , And then, on the whole, calculate 2 individual global Check block , This redundancy is still 1.33, But when a single data block is corrupted ( This is the most common situation in the data center ),IO From 12 Narrowed down to 6, Reduce the cost of data recovery .
actual combat
In open source object storage projects min.io The erasure code library used in is klauspost/reedsolomon, Let's use klauspost/reedsolomon Make a small tool , Demonstrate the redundancy backup effect of erasure correction code . Here we write a small program , Set up k/s Respectively 5,3 The redundancy is 1.6, I.e. delete at will 3 File , Data is always recoverable . The complete program is in here
var ( decode = flag.Bool("decode", false, "decode or not") fileName = flag.String("file", "", "file to encode/decode") encodePath = flag.String("encode_path", "", "folder to save encode files") ) // usage // ./main --file=./file//testfile.txt -encode_path=./encode // ./main --file=./file//testfile_recover.txt -encode_path=./encode --decode func main() { flag.Parse() if fileName == nil || encodePath == nil{ panic("need filename and encode path") } encoder, err := reedsolomon.New(5, 3) if err != nil{ panic(err) } if *decode{ shards := make([][]byte, 8) for i:= 0; i< 8; i ++{ encodeFile := path.Join(*encodePath, fmt.Sprintf("encode_%d", i)) data, err := ioutil.ReadFile(encodeFile) if err == nil { shards[i] = data } else if os.IsNotExist(err){ continue }else { panic(err) } } err = encoder.Reconstruct(shards) if err != nil{ panic(err) } fmt.Printf("reconstruct data done\n") f, err := os.OpenFile(*fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil{ panic(err) } dataSize := 0 for i := 0; i < 5; i++{ dataSize += len(shards[i]) } err = encoder.Join(f, shards, dataSize) if err != nil{ panic(err) } fmt.Printf("recover file success") }else{ data, err := ioutil.ReadFile(*fileName) if err != nil{ panic(err) } shards, err := encoder.Split(data) if err != nil{ panic(err) } fmt.Printf("split data into 5+3=%d shards success.\n", len(shards)) err = encoder.Encode(shards) if err != nil{ panic(err) } fmt.Printf("encode data success.") err = os.MkdirAll(*encodePath, 0777) if err != nil{ panic(err) } for i, s := range shards{ err := ioutil.WriteFile(path.Join(*encodePath, fmt.Sprintf("encode_%d", i)), s, 0644) if err != nil{ panic(err) } } } }
demonstration
# 1. First, let's try the storage effect * cat ./file//testfile.txt this is just a test file content is meaningless% * ./main --file=./file//testfile.txt -encode_path=./encode split data into 5+3=8 shards success. encode data success.% # Here we put the document testfile.txt Divided into 5 Share , add 3 Copies of verification data are stored ,encode The folder is the stored data content , We assume that in a real scene 7 Copies of data are stored on different nodes * ls ./encode encode_0 encode_1 encode_2 encode_3 encode_4 encode_5 encode_6 encode_7 # 2. Now we ( Because the disk of the node is damaged ) Lost one of them Any three copies of data * rm ./encode/encode_3 ./encode/encode_5 ./encode/encode_7 # 3. Try to recover data , It can be seen that , Data recovery was successful , encode file All recovered , and recover No problem with the raw data * ./main --file=./file//testfile_recover.txt -encode_path=./encode --decode reconstruct data done recover file success% * ls ./encode encode_0 encode_1 encode_2 encode_3 encode_4 encode_5 encode_6 encode_7 * cat ./file/testfile_recover.txt this is just a test file content is meaningless% # 4. Delete more data , Try to recover again , Recovery failures can be found : too few shards given * rm ./encode/encode_1 ./encode/encode_3 ./encode/encode_5 ./encode/encode_7 * ./main --file=./file//testfile_recover.txt -encode_path=./encode --decode panic: too few shards given
Reference resources
边栏推荐
- [mysql_16] variables, process control and cursors
- JVM GC garbage collection detailed introduction quick check of learning notes
- Pipeline post instruction
- How to apply for new bonds is it safe to open an account
- Continuous testing | making testing more free: practicing automated execution of use cases in coding
- 11+ article - machine learning builds Protics framework - deeply reveals the impact of tumor infiltrating immune cells in different molecular subtypes on prognosis
- From theory to practice, decipher Alibaba's internal MySQL optimization scheme in simple terms
- mRNA疫苗的研制怎么做?27+ 胰腺癌抗原和免疫亚型的解析来告诉你答案!
- Listed JD Logistics: breaking through again
- Making daily menu applet with micro build low code
猜你喜欢
How stupid of me to hire a bunch of programmers who can only "Google"!
使用开源工具 k8tz 优雅设置 Kubernetes Pod 时区
MySQL 外键影响
How to write controller layer code gracefully?
Opencv learning notes - loading and saving images
Database migration tool flyway vs liquibase (II)
一纸英雄帖,激起千层浪,横跨10国,一线大厂都派人来了!-GWEI 2022-新加坡
Opencv learning notes - regions of interest (ROI) and image blending
文本转语音功能上线,可以体验专业播音员的服务,诚邀试用
Group planning - General Review
随机推荐
Deep learning ~11+ a new perspective on disease-related miRNA research
ArrayList # sublist these four holes, you get caught accidentally
一纸英雄帖,激起千层浪,横跨10国,一线大厂都派人来了!-GWEI 2022-新加坡
Istio practical skills: implement header based authorization
2021-06-02: given the head node of a search binary tree, it will be transformed into an ordered two-way linked list with head and tail connected.
Popular science of data annotation: ten common image annotation methods
A good habit that makes your programming ability soar
Tencent Youtu, together with Tencent security Tianyu and wechat, jointly launched an infringement protection scheme
Ten thousand campus developers play AI in a fancy way. It's enough to see this picture!
Installing sqlserver extension PDO of PHP under Linux_ sqlsrv
嵌入式必学!硬件资源接口详解——基于ARM AM335X开发板 (下)
What should music website SEO do?
LS-DYNA新手入门经验
生成 4维 的 气压温度的 nc文件,之后进行代码读取(提供代码)
Conceptual analysis of DDD Domain Driven Design
Istio practical skills: using prism to construct multi version test services
The idea of "6 points + gene family" without experiment~
[2021 techo youth dry goods sorting post, there is always one you are interested in]
GTEST from getting started to getting started
In depth analysis, from ordinary clock system to various time service modes