You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
151 lines
7.4 KiB
151 lines
7.4 KiB
8 years ago
|
/*
|
||
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
||
|
*
|
||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
* you may not use this file except in compliance with the License.
|
||
|
* You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
8 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
7 years ago
|
"context"
|
||
8 years ago
|
"crypto/rand"
|
||
7 years ago
|
"io"
|
||
8 years ago
|
"testing"
|
||
|
)
|
||
|
|
||
6 years ago
|
var erasureHealTests = []struct {
|
||
7 years ago
|
dataBlocks, disks int
|
||
|
|
||
|
// number of offline disks is also number of staleDisks for
|
||
|
// erasure reconstruction in this test
|
||
|
offDisks int
|
||
|
|
||
|
// bad disks are online disks which return errors
|
||
|
badDisks, badStaleDisks int
|
||
|
|
||
|
blocksize, size int64
|
||
|
algorithm BitrotAlgorithm
|
||
|
shouldFail bool
|
||
7 years ago
|
}{
|
||
7 years ago
|
{dataBlocks: 2, disks: 4, offDisks: 1, badDisks: 0, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: SHA256, shouldFail: false}, // 0
|
||
|
{dataBlocks: 3, disks: 6, offDisks: 2, badDisks: 0, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: BLAKE2b512, shouldFail: false}, // 1
|
||
|
{dataBlocks: 4, disks: 8, offDisks: 2, badDisks: 1, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: BLAKE2b512, shouldFail: false}, // 2
|
||
|
{dataBlocks: 5, disks: 10, offDisks: 3, badDisks: 1, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: DefaultBitrotAlgorithm, shouldFail: false}, // 3
|
||
|
{dataBlocks: 6, disks: 12, offDisks: 2, badDisks: 3, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: SHA256, shouldFail: false}, // 4
|
||
|
{dataBlocks: 7, disks: 14, offDisks: 4, badDisks: 1, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: DefaultBitrotAlgorithm, shouldFail: false}, // 5
|
||
7 years ago
|
{dataBlocks: 8, disks: 16, offDisks: 6, badDisks: 1, badStaleDisks: 1, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: DefaultBitrotAlgorithm, shouldFail: false}, // 6
|
||
7 years ago
|
{dataBlocks: 7, disks: 14, offDisks: 2, badDisks: 3, badStaleDisks: 0, blocksize: int64(oneMiByte / 2), size: oneMiByte, algorithm: BLAKE2b512, shouldFail: false}, // 7
|
||
|
{dataBlocks: 6, disks: 12, offDisks: 1, badDisks: 0, badStaleDisks: 1, blocksize: int64(oneMiByte - 1), size: oneMiByte, algorithm: DefaultBitrotAlgorithm, shouldFail: true}, // 8
|
||
|
{dataBlocks: 5, disks: 10, offDisks: 3, badDisks: 0, badStaleDisks: 3, blocksize: int64(oneMiByte / 2), size: oneMiByte, algorithm: SHA256, shouldFail: true}, // 9
|
||
|
{dataBlocks: 4, disks: 8, offDisks: 1, badDisks: 1, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: DefaultBitrotAlgorithm, shouldFail: false}, // 10
|
||
|
{dataBlocks: 2, disks: 4, offDisks: 1, badDisks: 0, badStaleDisks: 1, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: DefaultBitrotAlgorithm, shouldFail: true}, // 11
|
||
|
{dataBlocks: 6, disks: 12, offDisks: 8, badDisks: 3, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: DefaultBitrotAlgorithm, shouldFail: true}, // 12
|
||
|
{dataBlocks: 7, disks: 14, offDisks: 3, badDisks: 4, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: BLAKE2b512, shouldFail: false}, // 13
|
||
|
{dataBlocks: 7, disks: 14, offDisks: 6, badDisks: 1, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: DefaultBitrotAlgorithm, shouldFail: false}, // 14
|
||
|
{dataBlocks: 8, disks: 16, offDisks: 4, badDisks: 5, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: DefaultBitrotAlgorithm, shouldFail: true}, // 15
|
||
7 years ago
|
{dataBlocks: 2, disks: 4, offDisks: 1, badDisks: 0, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: DefaultBitrotAlgorithm, shouldFail: false}, // 16
|
||
6 years ago
|
{dataBlocks: 12, disks: 16, offDisks: 2, badDisks: 1, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: DefaultBitrotAlgorithm, shouldFail: false}, // 17
|
||
|
{dataBlocks: 6, disks: 8, offDisks: 1, badDisks: 0, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte, algorithm: BLAKE2b512, shouldFail: false}, // 18
|
||
|
{dataBlocks: 2, disks: 4, offDisks: 1, badDisks: 0, badStaleDisks: 0, blocksize: int64(blockSizeV1), size: oneMiByte * 64, algorithm: SHA256, shouldFail: false}, // 19
|
||
7 years ago
|
}
|
||
8 years ago
|
|
||
6 years ago
|
func TestErasureHeal(t *testing.T) {
|
||
|
for i, test := range erasureHealTests {
|
||
7 years ago
|
if test.offDisks < test.badStaleDisks {
|
||
|
// test case sanity check
|
||
|
t.Fatalf("Test %d: Bad test case - number of stale disks cannot be less than number of badstale disks", i)
|
||
|
}
|
||
|
|
||
|
// create some test data
|
||
7 years ago
|
setup, err := newErasureTestSetup(test.dataBlocks, test.disks-test.dataBlocks, test.blocksize)
|
||
8 years ago
|
if err != nil {
|
||
7 years ago
|
t.Fatalf("Test %d: failed to setup XL environment: %v", i, err)
|
||
8 years ago
|
}
|
||
6 years ago
|
disks := setup.disks
|
||
6 years ago
|
erasure, err := NewErasure(context.Background(), test.dataBlocks, test.disks-test.dataBlocks, test.blocksize)
|
||
7 years ago
|
if err != nil {
|
||
|
setup.Remove()
|
||
|
t.Fatalf("Test %d: failed to create ErasureStorage: %v", i, err)
|
||
8 years ago
|
}
|
||
7 years ago
|
data := make([]byte, test.size)
|
||
|
if _, err = io.ReadFull(rand.Reader, data); err != nil {
|
||
|
setup.Remove()
|
||
|
t.Fatalf("Test %d: failed to create random test data: %v", i, err)
|
||
8 years ago
|
}
|
||
7 years ago
|
buffer := make([]byte, test.blocksize, 2*test.blocksize)
|
||
6 years ago
|
writers := make([]*bitrotWriter, len(disks))
|
||
|
for i, disk := range disks {
|
||
|
writers[i] = newBitrotWriter(disk, "testbucket", "testobject", test.algorithm)
|
||
|
}
|
||
6 years ago
|
_, err = erasure.Encode(context.Background(), bytes.NewReader(data), writers, buffer, erasure.dataBlocks+1)
|
||
8 years ago
|
if err != nil {
|
||
7 years ago
|
setup.Remove()
|
||
|
t.Fatalf("Test %d: failed to create random test data: %v", i, err)
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
readers := make([]*bitrotReader, len(disks))
|
||
|
for i, disk := range disks {
|
||
6 years ago
|
shardFilesize := getErasureShardFileSize(test.blocksize, test.size, erasure.dataBlocks)
|
||
6 years ago
|
readers[i] = newBitrotReader(disk, "testbucket", "testobject", test.algorithm, shardFilesize, writers[i].Sum())
|
||
|
}
|
||
|
|
||
7 years ago
|
// setup stale disks for the test case
|
||
6 years ago
|
staleDisks := make([]StorageAPI, len(disks))
|
||
|
copy(staleDisks, disks)
|
||
|
for j := 0; j < len(staleDisks); j++ {
|
||
7 years ago
|
if j < test.offDisks {
|
||
6 years ago
|
readers[j] = nil
|
||
7 years ago
|
} else {
|
||
|
staleDisks[j] = nil
|
||
|
}
|
||
|
}
|
||
|
for j := 0; j < test.badDisks; j++ {
|
||
6 years ago
|
readers[test.offDisks+j].disk = badDisk{nil}
|
||
7 years ago
|
}
|
||
|
for j := 0; j < test.badStaleDisks; j++ {
|
||
|
staleDisks[j] = badDisk{nil}
|
||
|
}
|
||
|
|
||
6 years ago
|
staleWriters := make([]*bitrotWriter, len(staleDisks))
|
||
|
for i, disk := range staleDisks {
|
||
|
if disk == nil {
|
||
|
continue
|
||
|
}
|
||
|
staleWriters[i] = newBitrotWriter(disk, "testbucket", "testobject", test.algorithm)
|
||
|
}
|
||
|
|
||
7 years ago
|
// test case setup is complete - now call Healfile()
|
||
6 years ago
|
err = erasure.Heal(context.Background(), readers, staleWriters, test.size)
|
||
7 years ago
|
if err != nil && !test.shouldFail {
|
||
|
t.Errorf("Test %d: should pass but it failed with: %v", i, err)
|
||
|
}
|
||
|
if err == nil && test.shouldFail {
|
||
|
t.Errorf("Test %d: should fail but it passed", i)
|
||
|
}
|
||
|
if err == nil {
|
||
7 years ago
|
// Verify that checksums of staleDisks
|
||
|
// match expected values
|
||
6 years ago
|
for i := range staleWriters {
|
||
|
if staleWriters[i] == nil {
|
||
7 years ago
|
continue
|
||
7 years ago
|
}
|
||
6 years ago
|
if !bytes.Equal(staleWriters[i].Sum(), writers[i].Sum()) {
|
||
7 years ago
|
t.Errorf("Test %d: heal returned different bitrot checksums", i)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
setup.Remove()
|
||
8 years ago
|
}
|
||
|
}
|