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.
466 lines
13 KiB
466 lines
13 KiB
8 years ago
|
/*
|
||
5 years ago
|
* MinIO Cloud Storage, (C) 2016-2020 MinIO, Inc.
|
||
8 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
|
package cmd
|
||
|
|
||
|
import (
|
||
8 years ago
|
"bytes"
|
||
5 years ago
|
"context"
|
||
5 years ago
|
"crypto/rand"
|
||
|
"os"
|
||
|
"path"
|
||
|
"reflect"
|
||
8 years ago
|
"testing"
|
||
5 years ago
|
"time"
|
||
6 years ago
|
|
||
5 years ago
|
"github.com/dustin/go-humanize"
|
||
6 years ago
|
"github.com/minio/minio/pkg/madmin"
|
||
8 years ago
|
)
|
||
|
|
||
5 years ago
|
// Tests both object and bucket healing.
|
||
|
func TestHealing(t *testing.T) {
|
||
|
ctx, cancel := context.WithCancel(context.Background())
|
||
|
defer cancel()
|
||
|
|
||
|
obj, fsDirs, err := prepareErasure16(ctx)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
defer removeRoots(fsDirs)
|
||
|
|
||
|
z := obj.(*erasureZones)
|
||
|
er := z.zones[0].sets[0]
|
||
|
|
||
|
// Create "bucket"
|
||
|
err = obj.MakeBucketWithLocation(ctx, "bucket", BucketOptions{})
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
bucket := "bucket"
|
||
|
object := "object"
|
||
|
|
||
|
data := make([]byte, 1*humanize.MiByte)
|
||
|
length := int64(len(data))
|
||
|
_, err = rand.Read(data)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
_, err = obj.PutObject(ctx, bucket, object, mustGetPutObjReader(t, bytes.NewReader(data), length, "", ""), ObjectOptions{})
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
disk := er.getDisks()[0]
|
||
|
fileInfoPreHeal, err := disk.ReadVersion(bucket, object, "")
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// Remove the object - to simulate the case where the disk was down when the object
|
||
|
// was created.
|
||
|
err = removeAll(pathJoin(disk.String(), bucket, object))
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
_, err = er.HealObject(ctx, bucket, object, "", madmin.HealOpts{ScanMode: madmin.HealNormalScan})
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
fileInfoPostHeal, err := disk.ReadVersion(bucket, object, "")
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// After heal the meta file should be as expected.
|
||
|
if !reflect.DeepEqual(fileInfoPreHeal, fileInfoPostHeal) {
|
||
|
t.Fatal("HealObject failed")
|
||
|
}
|
||
|
|
||
|
err = os.RemoveAll(path.Join(fsDirs[0], bucket, object, "er.meta"))
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// Write er.meta with different modtime to simulate the case where a disk had
|
||
|
// gone down when an object was replaced by a new object.
|
||
|
fileInfoOutDated := fileInfoPreHeal
|
||
|
fileInfoOutDated.ModTime = time.Now()
|
||
|
err = disk.WriteMetadata(bucket, object, fileInfoOutDated)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
_, err = er.HealObject(ctx, bucket, object, "", madmin.HealOpts{ScanMode: madmin.HealDeepScan})
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
fileInfoPostHeal, err = disk.ReadVersion(bucket, object, "")
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// After heal the meta file should be as expected.
|
||
|
if !reflect.DeepEqual(fileInfoPreHeal, fileInfoPostHeal) {
|
||
|
t.Fatal("HealObject failed")
|
||
|
}
|
||
|
|
||
|
// Remove the bucket - to simulate the case where bucket was
|
||
|
// created when the disk was down.
|
||
|
err = os.RemoveAll(path.Join(fsDirs[0], bucket))
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
// This would create the bucket.
|
||
|
_, err = er.HealBucket(ctx, bucket, false, false)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
// Stat the bucket to make sure that it was created.
|
||
|
_, err = er.getDisks()[0].StatVol(bucket)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
func TestHealObjectCorrupted(t *testing.T) {
|
||
5 years ago
|
ctx, cancel := context.WithCancel(context.Background())
|
||
|
defer cancel()
|
||
5 years ago
|
|
||
5 years ago
|
resetGlobalHealState()
|
||
5 years ago
|
defer resetGlobalHealState()
|
||
|
|
||
6 years ago
|
nDisks := 16
|
||
|
fsDirs, err := getRandomDisks(nDisks)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
defer removeRoots(fsDirs)
|
||
|
|
||
|
// Everything is fine, should return nil
|
||
5 years ago
|
objLayer, _, err := initObjectLayer(ctx, mustGetZoneEndpoints(fsDirs...))
|
||
6 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
bucket := "bucket"
|
||
|
object := "object"
|
||
|
data := bytes.Repeat([]byte("a"), 5*1024*1024)
|
||
|
var opts ObjectOptions
|
||
|
|
||
5 years ago
|
err = objLayer.MakeBucketWithLocation(ctx, bucket, BucketOptions{})
|
||
6 years ago
|
if err != nil {
|
||
|
t.Fatalf("Failed to make a bucket - %v", err)
|
||
|
}
|
||
|
|
||
|
// Create an object with multiple parts uploaded in decreasing
|
||
|
// part number.
|
||
5 years ago
|
uploadID, err := objLayer.NewMultipartUpload(ctx, bucket, object, opts)
|
||
6 years ago
|
if err != nil {
|
||
|
t.Fatalf("Failed to create a multipart upload - %v", err)
|
||
|
}
|
||
|
|
||
|
var uploadedParts []CompletePart
|
||
|
for _, partID := range []int{2, 1} {
|
||
5 years ago
|
pInfo, err1 := objLayer.PutObjectPart(ctx, bucket, object, uploadID, partID, mustGetPutObjReader(t, bytes.NewReader(data), int64(len(data)), "", ""), opts)
|
||
6 years ago
|
if err1 != nil {
|
||
|
t.Fatalf("Failed to upload a part - %v", err1)
|
||
|
}
|
||
|
uploadedParts = append(uploadedParts, CompletePart{
|
||
|
PartNumber: pInfo.PartNumber,
|
||
|
ETag: pInfo.ETag,
|
||
|
})
|
||
|
}
|
||
|
|
||
5 years ago
|
_, err = objLayer.CompleteMultipartUpload(ctx, bucket, object, uploadID, uploadedParts, ObjectOptions{})
|
||
6 years ago
|
if err != nil {
|
||
|
t.Fatalf("Failed to complete multipart upload - %v", err)
|
||
|
}
|
||
|
|
||
6 years ago
|
// Test 1: Remove the object backend files from the first disk.
|
||
5 years ago
|
z := objLayer.(*erasureZones)
|
||
|
er := z.zones[0].sets[0]
|
||
|
erasureDisks := er.getDisks()
|
||
|
firstDisk := erasureDisks[0]
|
||
|
err = firstDisk.DeleteFile(bucket, pathJoin(object, xlStorageFormatFile))
|
||
6 years ago
|
if err != nil {
|
||
|
t.Fatalf("Failed to delete a file - %v", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
_, err = objLayer.HealObject(ctx, bucket, object, "", madmin.HealOpts{ScanMode: madmin.HealNormalScan})
|
||
6 years ago
|
if err != nil {
|
||
|
t.Fatalf("Failed to heal object - %v", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
fileInfos, errs := readAllFileInfo(ctx, erasureDisks, bucket, object, "")
|
||
|
fi, err := getLatestFileInfo(ctx, fileInfos, errs)
|
||
6 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Failed to getLatestFileInfo - %v", err)
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
if err = firstDisk.CheckFile(bucket, object); err != nil {
|
||
|
t.Errorf("Expected er.meta file to be present but stat failed - %v", err)
|
||
6 years ago
|
}
|
||
5 years ago
|
|
||
|
err = firstDisk.DeleteFile(bucket, pathJoin(object, fi.DataDir, "part.1"))
|
||
6 years ago
|
if err != nil {
|
||
5 years ago
|
t.Errorf("Failure during deleting part.1 - %v", err)
|
||
6 years ago
|
}
|
||
5 years ago
|
|
||
|
err = firstDisk.WriteAll(bucket, pathJoin(object, fi.DataDir, "part.1"), bytes.NewReader([]byte{}))
|
||
6 years ago
|
if err != nil {
|
||
|
t.Errorf("Failure during creating part.1 - %v", err)
|
||
|
}
|
||
5 years ago
|
|
||
|
_, err = objLayer.HealObject(ctx, bucket, object, "", madmin.HealOpts{DryRun: false, Remove: true, ScanMode: madmin.HealDeepScan})
|
||
6 years ago
|
if err != nil {
|
||
|
t.Errorf("Expected nil but received %v", err)
|
||
|
}
|
||
5 years ago
|
|
||
|
fileInfos, errs = readAllFileInfo(ctx, erasureDisks, bucket, object, "")
|
||
|
nfi, err := getLatestFileInfo(ctx, fileInfos, errs)
|
||
6 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Failed to getLatestFileInfo - %v", err)
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
if !reflect.DeepEqual(fi, nfi) {
|
||
|
t.Fatalf("FileInfo not equal after healing")
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
err = firstDisk.DeleteFile(bucket, pathJoin(object, fi.DataDir, "part.1"))
|
||
5 years ago
|
if err != nil {
|
||
|
t.Errorf("Failure during deleting part.1 - %v", err)
|
||
|
}
|
||
5 years ago
|
|
||
|
bdata := bytes.Repeat([]byte("b"), int(nfi.Size))
|
||
|
err = firstDisk.WriteAll(bucket, pathJoin(object, fi.DataDir, "part.1"), bytes.NewReader(bdata))
|
||
5 years ago
|
if err != nil {
|
||
|
t.Errorf("Failure during creating part.1 - %v", err)
|
||
|
}
|
||
5 years ago
|
|
||
|
_, err = objLayer.HealObject(ctx, bucket, object, "", madmin.HealOpts{DryRun: false, Remove: true, ScanMode: madmin.HealDeepScan})
|
||
5 years ago
|
if err != nil {
|
||
|
t.Errorf("Expected nil but received %v", err)
|
||
|
}
|
||
5 years ago
|
|
||
|
fileInfos, errs = readAllFileInfo(ctx, erasureDisks, bucket, object, "")
|
||
|
nfi, err = getLatestFileInfo(ctx, fileInfos, errs)
|
||
5 years ago
|
if err != nil {
|
||
5 years ago
|
t.Fatalf("Failed to getLatestFileInfo - %v", err)
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
if !reflect.DeepEqual(fi, nfi) {
|
||
|
t.Fatalf("FileInfo not equal after healing")
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
// Test 4: checks if HealObject returns an error when xl.meta is not found
|
||
6 years ago
|
// in more than read quorum number of disks, to create a corrupted situation.
|
||
5 years ago
|
for i := 0; i <= len(er.getDisks())/2; i++ {
|
||
|
er.getDisks()[i].DeleteFile(bucket, pathJoin(object, xlStorageFormatFile))
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
// Try healing now, expect to receive errFileNotFound.
|
||
5 years ago
|
_, err = objLayer.HealObject(ctx, bucket, object, "", madmin.HealOpts{DryRun: false, Remove: true, ScanMode: madmin.HealDeepScan})
|
||
6 years ago
|
if err != nil {
|
||
5 years ago
|
if _, ok := err.(ObjectNotFound); !ok {
|
||
|
t.Errorf("Expect %v but received %v", ObjectNotFound{Bucket: bucket, Object: object}, err)
|
||
|
}
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
// since majority of xl.meta's are not available, object should be successfully deleted.
|
||
5 years ago
|
_, err = objLayer.GetObjectInfo(ctx, bucket, object, ObjectOptions{})
|
||
6 years ago
|
if _, ok := err.(ObjectNotFound); !ok {
|
||
|
t.Errorf("Expect %v but received %v", ObjectNotFound{Bucket: bucket, Object: object}, err)
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
// Tests healing of object.
|
||
5 years ago
|
func TestHealObjectErasure(t *testing.T) {
|
||
5 years ago
|
ctx, cancel := context.WithCancel(context.Background())
|
||
|
defer cancel()
|
||
|
|
||
8 years ago
|
nDisks := 16
|
||
|
fsDirs, err := getRandomDisks(nDisks)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
defer removeRoots(fsDirs)
|
||
|
|
||
|
// Everything is fine, should return nil
|
||
5 years ago
|
obj, _, err := initObjectLayer(ctx, mustGetZoneEndpoints(fsDirs...))
|
||
8 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
bucket := "bucket"
|
||
|
object := "object"
|
||
8 years ago
|
data := bytes.Repeat([]byte("a"), 5*1024*1024)
|
||
6 years ago
|
var opts ObjectOptions
|
||
8 years ago
|
|
||
5 years ago
|
err = obj.MakeBucketWithLocation(ctx, bucket, BucketOptions{})
|
||
8 years ago
|
if err != nil {
|
||
|
t.Fatalf("Failed to make a bucket - %v", err)
|
||
|
}
|
||
|
|
||
8 years ago
|
// Create an object with multiple parts uploaded in decreasing
|
||
|
// part number.
|
||
5 years ago
|
uploadID, err := obj.NewMultipartUpload(ctx, bucket, object, opts)
|
||
8 years ago
|
if err != nil {
|
||
|
t.Fatalf("Failed to create a multipart upload - %v", err)
|
||
|
}
|
||
|
|
||
7 years ago
|
var uploadedParts []CompletePart
|
||
8 years ago
|
for _, partID := range []int{2, 1} {
|
||
5 years ago
|
pInfo, err1 := obj.PutObjectPart(ctx, bucket, object, uploadID, partID, mustGetPutObjReader(t, bytes.NewReader(data), int64(len(data)), "", ""), opts)
|
||
8 years ago
|
if err1 != nil {
|
||
|
t.Fatalf("Failed to upload a part - %v", err1)
|
||
8 years ago
|
}
|
||
7 years ago
|
uploadedParts = append(uploadedParts, CompletePart{
|
||
8 years ago
|
PartNumber: pInfo.PartNumber,
|
||
|
ETag: pInfo.ETag,
|
||
|
})
|
||
|
}
|
||
|
|
||
5 years ago
|
// Remove the object backend files from the first disk.
|
||
|
z := obj.(*erasureZones)
|
||
|
er := z.zones[0].sets[0]
|
||
|
firstDisk := er.getDisks()[0]
|
||
|
|
||
5 years ago
|
_, err = obj.CompleteMultipartUpload(ctx, bucket, object, uploadID, uploadedParts, ObjectOptions{})
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
t.Fatalf("Failed to complete multipart upload - %v", err)
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
err = firstDisk.DeleteFile(bucket, pathJoin(object, xlStorageFormatFile))
|
||
8 years ago
|
if err != nil {
|
||
|
t.Fatalf("Failed to delete a file - %v", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
_, err = obj.HealObject(ctx, bucket, object, "", madmin.HealOpts{ScanMode: madmin.HealNormalScan})
|
||
8 years ago
|
if err != nil {
|
||
|
t.Fatalf("Failed to heal object - %v", err)
|
||
|
}
|
||
|
|
||
5 years ago
|
if err = firstDisk.CheckFile(bucket, object); err != nil {
|
||
|
t.Errorf("Expected er.meta file to be present but stat failed - %v", err)
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
erasureDisks := er.getDisks()
|
||
|
z.zones[0].erasureDisksMu.Lock()
|
||
|
er.getDisks = func() []StorageAPI {
|
||
5 years ago
|
// Nil more than half the disks, to remove write quorum.
|
||
5 years ago
|
for i := 0; i <= len(erasureDisks)/2; i++ {
|
||
|
erasureDisks[i] = nil
|
||
5 years ago
|
}
|
||
5 years ago
|
return erasureDisks
|
||
8 years ago
|
}
|
||
5 years ago
|
z.zones[0].erasureDisksMu.Unlock()
|
||
8 years ago
|
|
||
|
// Try healing now, expect to receive errDiskNotFound.
|
||
5 years ago
|
_, err = obj.HealObject(ctx, bucket, object, "", madmin.HealOpts{ScanMode: madmin.HealDeepScan})
|
||
|
// since majority of er.meta's are not available, object quorum can't be read properly and error will be errErasureReadQuorum
|
||
7 years ago
|
if _, ok := err.(InsufficientReadQuorum); !ok {
|
||
6 years ago
|
t.Errorf("Expected %v but received %v", InsufficientReadQuorum{}, err)
|
||
8 years ago
|
}
|
||
|
}
|
||
5 years ago
|
|
||
|
// Tests healing of empty directories
|
||
5 years ago
|
func TestHealEmptyDirectoryErasure(t *testing.T) {
|
||
5 years ago
|
ctx, cancel := context.WithCancel(context.Background())
|
||
|
defer cancel()
|
||
|
|
||
5 years ago
|
nDisks := 16
|
||
|
fsDirs, err := getRandomDisks(nDisks)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
defer removeRoots(fsDirs)
|
||
|
|
||
|
// Everything is fine, should return nil
|
||
5 years ago
|
obj, _, err := initObjectLayer(ctx, mustGetZoneEndpoints(fsDirs...))
|
||
5 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
bucket := "bucket"
|
||
|
object := "empty-dir/"
|
||
|
var opts ObjectOptions
|
||
|
|
||
5 years ago
|
err = obj.MakeBucketWithLocation(ctx, bucket, BucketOptions{})
|
||
5 years ago
|
if err != nil {
|
||
|
t.Fatalf("Failed to make a bucket - %v", err)
|
||
|
}
|
||
|
|
||
|
// Upload an empty directory
|
||
5 years ago
|
_, err = obj.PutObject(ctx, bucket, object, mustGetPutObjReader(t,
|
||
5 years ago
|
bytes.NewReader([]byte{}), 0, "", ""), opts)
|
||
5 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// Remove the object backend files from the first disk.
|
||
5 years ago
|
z := obj.(*erasureZones)
|
||
|
er := z.zones[0].sets[0]
|
||
|
firstDisk := er.getDisks()[0]
|
||
5 years ago
|
err = firstDisk.DeleteFile(bucket, object)
|
||
|
if err != nil {
|
||
|
t.Fatalf("Failed to delete a file - %v", err)
|
||
|
}
|
||
|
|
||
|
// Heal the object
|
||
5 years ago
|
hr, err := obj.HealObject(ctx, bucket, object, "", madmin.HealOpts{ScanMode: madmin.HealNormalScan})
|
||
5 years ago
|
if err != nil {
|
||
|
t.Fatalf("Failed to heal object - %v", err)
|
||
|
}
|
||
|
|
||
|
// Check if the empty directory is restored in the first disk
|
||
|
_, err = firstDisk.StatVol(pathJoin(bucket, object))
|
||
|
if err != nil {
|
||
|
t.Fatalf("Expected object to be present but stat failed - %v", err)
|
||
|
}
|
||
|
|
||
|
// Check the state of the object in the first disk (should be missing)
|
||
|
if hr.Before.Drives[0].State != madmin.DriveStateMissing {
|
||
|
t.Fatalf("Unexpected drive state: %v", hr.Before.Drives[0].State)
|
||
|
}
|
||
|
|
||
|
// Check the state of all other disks (should be ok)
|
||
|
for i, h := range append(hr.Before.Drives[1:], hr.After.Drives...) {
|
||
|
if h.State != madmin.DriveStateOk {
|
||
|
t.Fatalf("Unexpected drive state (%d): %v", i+1, h.State)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Heal the same object again
|
||
5 years ago
|
hr, err = obj.HealObject(ctx, bucket, object, "", madmin.HealOpts{ScanMode: madmin.HealNormalScan})
|
||
5 years ago
|
if err != nil {
|
||
|
t.Fatalf("Failed to heal object - %v", err)
|
||
|
}
|
||
|
|
||
|
// Check that Before & After states are all okay
|
||
|
for i, h := range append(hr.Before.Drives, hr.After.Drives...) {
|
||
|
if h.State != madmin.DriveStateOk {
|
||
|
t.Fatalf("Unexpected drive state (%d): %v", i+1, h.State)
|
||
|
}
|
||
|
}
|
||
|
}
|