storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/erasure-coding.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2017 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"encoding/hex"
    23  	"fmt"
    24  	"os"
    25  	"reflect"
    26  	"sync"
    27  
    28  	"github.com/cespare/xxhash/v2"
    29  	"github.com/klauspost/reedsolomon"
    30  
    31  	"storj.io/minio/cmd/logger"
    32  )
    33  
    34  // Erasure - erasure encoding details.
    35  type Erasure struct {
    36  	encoder                  func() reedsolomon.Encoder
    37  	dataBlocks, parityBlocks int
    38  	blockSize                int64
    39  }
    40  
    41  // NewErasure creates a new ErasureStorage.
    42  func NewErasure(ctx context.Context, dataBlocks, parityBlocks int, blockSize int64) (e Erasure, err error) {
    43  	// Check the parameters for sanity now.
    44  	if dataBlocks <= 0 || parityBlocks <= 0 {
    45  		return e, reedsolomon.ErrInvShardNum
    46  	}
    47  
    48  	if dataBlocks+parityBlocks > 256 {
    49  		return e, reedsolomon.ErrMaxShardNum
    50  	}
    51  
    52  	e = Erasure{
    53  		dataBlocks:   dataBlocks,
    54  		parityBlocks: parityBlocks,
    55  		blockSize:    blockSize,
    56  	}
    57  
    58  	// Encoder when needed.
    59  	var enc reedsolomon.Encoder
    60  	var once sync.Once
    61  	e.encoder = func() reedsolomon.Encoder {
    62  		once.Do(func() {
    63  			e, err := reedsolomon.New(dataBlocks, parityBlocks, reedsolomon.WithAutoGoroutines(int(e.ShardSize())))
    64  			if err != nil {
    65  				// Error conditions should be checked above.
    66  				panic(err)
    67  			}
    68  			enc = e
    69  		})
    70  		return enc
    71  	}
    72  	return
    73  }
    74  
    75  // EncodeData encodes the given data and returns the erasure-coded data.
    76  // It returns an error if the erasure coding failed.
    77  func (e *Erasure) EncodeData(ctx context.Context, data []byte) ([][]byte, error) {
    78  	if len(data) == 0 {
    79  		return make([][]byte, e.dataBlocks+e.parityBlocks), nil
    80  	}
    81  	encoded, err := e.encoder().Split(data)
    82  	if err != nil {
    83  		logger.LogIf(ctx, err)
    84  		return nil, err
    85  	}
    86  	if err = e.encoder().Encode(encoded); err != nil {
    87  		logger.LogIf(ctx, err)
    88  		return nil, err
    89  	}
    90  	return encoded, nil
    91  }
    92  
    93  // DecodeDataBlocks decodes the given erasure-coded data.
    94  // It only decodes the data blocks but does not verify them.
    95  // It returns an error if the decoding failed.
    96  func (e *Erasure) DecodeDataBlocks(data [][]byte) error {
    97  	var isZero = 0
    98  	for _, b := range data[:] {
    99  		if len(b) == 0 {
   100  			isZero++
   101  			break
   102  		}
   103  	}
   104  	if isZero == 0 || isZero == len(data) {
   105  		// If all are zero, payload is 0 bytes.
   106  		return nil
   107  	}
   108  	return e.encoder().ReconstructData(data)
   109  }
   110  
   111  // DecodeDataAndParityBlocks decodes the given erasure-coded data and verifies it.
   112  // It returns an error if the decoding failed.
   113  func (e *Erasure) DecodeDataAndParityBlocks(ctx context.Context, data [][]byte) error {
   114  	if err := e.encoder().Reconstruct(data); err != nil {
   115  		logger.LogIf(ctx, err)
   116  		return err
   117  	}
   118  	return nil
   119  }
   120  
   121  // ShardSize - returns actual shared size from erasure blockSize.
   122  func (e *Erasure) ShardSize() int64 {
   123  	return ceilFrac(e.blockSize, int64(e.dataBlocks))
   124  }
   125  
   126  // ShardFileSize - returns final erasure size from original size.
   127  func (e *Erasure) ShardFileSize(totalLength int64) int64 {
   128  	if totalLength == 0 {
   129  		return 0
   130  	}
   131  	if totalLength == -1 {
   132  		return -1
   133  	}
   134  	numShards := totalLength / e.blockSize
   135  	lastBlockSize := totalLength % e.blockSize
   136  	lastShardSize := ceilFrac(lastBlockSize, int64(e.dataBlocks))
   137  	return numShards*e.ShardSize() + lastShardSize
   138  }
   139  
   140  // ShardFileOffset - returns the effective offset where erasure reading begins.
   141  func (e *Erasure) ShardFileOffset(startOffset, length, totalLength int64) int64 {
   142  	shardSize := e.ShardSize()
   143  	shardFileSize := e.ShardFileSize(totalLength)
   144  	endShard := (startOffset + length) / e.blockSize
   145  	tillOffset := endShard*shardSize + shardSize
   146  	if tillOffset > shardFileSize {
   147  		tillOffset = shardFileSize
   148  	}
   149  	return tillOffset
   150  }
   151  
   152  // erasureSelfTest performs a self-test to ensure that erasure
   153  // algorithms compute expected erasure codes. If any algorithm
   154  // produces an incorrect value it fails with a hard error.
   155  //
   156  // erasureSelfTest tries to catch any issue in the erasure implementation
   157  // early instead of silently corrupting data.
   158  func erasureSelfTest() {
   159  	// Approx runtime ~1ms
   160  	var testConfigs [][2]uint8
   161  	for total := uint8(4); total < 16; total++ {
   162  		for data := total / 2; data < total; data++ {
   163  			parity := total - data
   164  			testConfigs = append(testConfigs, [2]uint8{data, parity})
   165  		}
   166  	}
   167  	got := make(map[[2]uint8]map[ErasureAlgo]uint64, len(testConfigs))
   168  	// Copied from output of fmt.Printf("%#v", got) at the end.
   169  	want := map[[2]uint8]map[ErasureAlgo]uint64{{0x2, 0x2}: {0x1: 0x23fb21be2496f5d3}, {0x2, 0x3}: {0x1: 0xa5cd5600ba0d8e7c}, {0x3, 0x1}: {0x1: 0x60ab052148b010b4}, {0x3, 0x2}: {0x1: 0xe64927daef76435a}, {0x3, 0x3}: {0x1: 0x672f6f242b227b21}, {0x3, 0x4}: {0x1: 0x571e41ba23a6dc6}, {0x4, 0x1}: {0x1: 0x524eaa814d5d86e2}, {0x4, 0x2}: {0x1: 0x62b9552945504fef}, {0x4, 0x3}: {0x1: 0xcbf9065ee053e518}, {0x4, 0x4}: {0x1: 0x9a07581dcd03da8}, {0x4, 0x5}: {0x1: 0xbf2d27b55370113f}, {0x5, 0x1}: {0x1: 0xf71031a01d70daf}, {0x5, 0x2}: {0x1: 0x8e5845859939d0f4}, {0x5, 0x3}: {0x1: 0x7ad9161acbb4c325}, {0x5, 0x4}: {0x1: 0xc446b88830b4f800}, {0x5, 0x5}: {0x1: 0xabf1573cc6f76165}, {0x5, 0x6}: {0x1: 0x7b5598a85045bfb8}, {0x6, 0x1}: {0x1: 0xe2fc1e677cc7d872}, {0x6, 0x2}: {0x1: 0x7ed133de5ca6a58e}, {0x6, 0x3}: {0x1: 0x39ef92d0a74cc3c0}, {0x6, 0x4}: {0x1: 0xcfc90052bc25d20}, {0x6, 0x5}: {0x1: 0x71c96f6baeef9c58}, {0x6, 0x6}: {0x1: 0x4b79056484883e4c}, {0x6, 0x7}: {0x1: 0xb1a0e2427ac2dc1a}, {0x7, 0x1}: {0x1: 0x937ba2b7af467a22}, {0x7, 0x2}: {0x1: 0x5fd13a734d27d37a}, {0x7, 0x3}: {0x1: 0x3be2722d9b66912f}, {0x7, 0x4}: {0x1: 0x14c628e59011be3d}, {0x7, 0x5}: {0x1: 0xcc3b39ad4c083b9f}, {0x7, 0x6}: {0x1: 0x45af361b7de7a4ff}, {0x7, 0x7}: {0x1: 0x456cc320cec8a6e6}, {0x7, 0x8}: {0x1: 0x1867a9f4db315b5c}, {0x8, 0x1}: {0x1: 0xbc5756b9a9ade030}, {0x8, 0x2}: {0x1: 0xdfd7d9d0b3e36503}, {0x8, 0x3}: {0x1: 0x72bb72c2cdbcf99d}, {0x8, 0x4}: {0x1: 0x3ba5e9b41bf07f0}, {0x8, 0x5}: {0x1: 0xd7dabc15800f9d41}, {0x8, 0x6}: {0x1: 0xb482a6169fd270f}, {0x8, 0x7}: {0x1: 0x50748e0099d657e8}, {0x9, 0x1}: {0x1: 0xc77ae0144fcaeb6e}, {0x9, 0x2}: {0x1: 0x8a86c7dbebf27b68}, {0x9, 0x3}: {0x1: 0xa64e3be6d6fe7e92}, {0x9, 0x4}: {0x1: 0x239b71c41745d207}, {0x9, 0x5}: {0x1: 0x2d0803094c5a86ce}, {0x9, 0x6}: {0x1: 0xa3c2539b3af84874}, {0xa, 0x1}: {0x1: 0x7d30d91b89fcec21}, {0xa, 0x2}: {0x1: 0xfa5af9aa9f1857a3}, {0xa, 0x3}: {0x1: 0x84bc4bda8af81f90}, {0xa, 0x4}: {0x1: 0x6c1cba8631de994a}, {0xa, 0x5}: {0x1: 0x4383e58a086cc1ac}, {0xb, 0x1}: {0x1: 0x4ed2929a2df690b}, {0xb, 0x2}: {0x1: 0xecd6f1b1399775c0}, {0xb, 0x3}: {0x1: 0xc78cfbfc0dc64d01}, {0xb, 0x4}: {0x1: 0xb2643390973702d6}, {0xc, 0x1}: {0x1: 0x3b2a88686122d082}, {0xc, 0x2}: {0x1: 0xfd2f30a48a8e2e9}, {0xc, 0x3}: {0x1: 0xd5ce58368ae90b13}, {0xd, 0x1}: {0x1: 0x9c88e2a9d1b8fff8}, {0xd, 0x2}: {0x1: 0xcb8460aa4cf6613}, {0xe, 0x1}: {0x1: 0x78a28bbaec57996e}}
   170  	var testData [256]byte
   171  	for i := range testData {
   172  		testData[i] = byte(i)
   173  	}
   174  	ok := true
   175  	for algo := invalidErasureAlgo + 1; algo < lastErasureAlgo; algo++ {
   176  		for _, conf := range testConfigs {
   177  			failOnErr := func(err error) {
   178  				if err != nil {
   179  					logger.Fatal(errSelfTestFailure, "%v: error on self-test [d:%d,p:%d]: %v. Unsafe to start server.\n", algo, conf[0], conf[1], err)
   180  				}
   181  			}
   182  			e, err := NewErasure(context.Background(), int(conf[0]), int(conf[1]), blockSizeV2)
   183  			failOnErr(err)
   184  			encoded, err := e.EncodeData(GlobalContext, testData[:])
   185  			failOnErr(err)
   186  			hash := xxhash.New()
   187  			for i, data := range encoded {
   188  				// Write index to keep track of sizes of each.
   189  				_, err = hash.Write([]byte{byte(i)})
   190  				failOnErr(err)
   191  				_, err = hash.Write(data)
   192  				failOnErr(err)
   193  				got[conf] = map[ErasureAlgo]uint64{algo: hash.Sum64()}
   194  			}
   195  
   196  			if a, b := want[conf], got[conf]; !reflect.DeepEqual(a, b) {
   197  				fmt.Fprintf(os.Stderr, "%v: error on self-test [d:%d,p:%d]: want %#v, got %#v\n", algo, conf[0], conf[1], a, b)
   198  				ok = false
   199  				continue
   200  			}
   201  			// Delete first shard and reconstruct...
   202  			first := encoded[0]
   203  			encoded[0] = nil
   204  			failOnErr(e.DecodeDataBlocks(encoded))
   205  			if a, b := first, encoded[0]; !bytes.Equal(a, b) {
   206  				fmt.Fprintf(os.Stderr, "%v: error on self-test [d:%d,p:%d]: want %#v, got %#v\n", algo, conf[0], conf[1], hex.EncodeToString(a), hex.EncodeToString(b))
   207  				ok = false
   208  				continue
   209  			}
   210  
   211  		}
   212  	}
   213  	if !ok {
   214  		logger.Fatal(errSelfTestFailure, "Erasure Coding self test failed")
   215  	}
   216  }