fix: return error for unexpected quorum in pickValidFileInfo (#12363)

This commit is contained in:
Harshavardhana 2021-05-24 18:31:56 -07:00 committed by GitHub
parent abd32065aa
commit cacdeca8cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 17 deletions

View File

@ -239,7 +239,11 @@ func (fi FileInfo) ObjectToPartOffset(ctx context.Context, offset int64) (partIn
return 0, 0, InvalidRange{} return 0, 0, InvalidRange{}
} }
func findFileInfoInQuorum(ctx context.Context, metaArr []FileInfo, modTime time.Time, dataDir string, quorum int) (xmv FileInfo, e error) { func findFileInfoInQuorum(ctx context.Context, metaArr []FileInfo, modTime time.Time, dataDir string, quorum int) (FileInfo, error) {
// with less quorum return error.
if quorum < 2 {
return FileInfo{}, errErasureReadQuorum
}
metaHashes := make([]string, len(metaArr)) metaHashes := make([]string, len(metaArr))
h := sha256.New() h := sha256.New()
for i, meta := range metaArr { for i, meta := range metaArr {
@ -278,7 +282,9 @@ func findFileInfoInQuorum(ctx context.Context, metaArr []FileInfo, modTime time.
for i, hash := range metaHashes { for i, hash := range metaHashes {
if hash == maxHash { if hash == maxHash {
return metaArr[i], nil if metaArr[i].IsValid() {
return metaArr[i], nil
}
} }
} }
@ -287,7 +293,7 @@ func findFileInfoInQuorum(ctx context.Context, metaArr []FileInfo, modTime time.
// pickValidFileInfo - picks one valid FileInfo content and returns from a // pickValidFileInfo - picks one valid FileInfo content and returns from a
// slice of FileInfo. // slice of FileInfo.
func pickValidFileInfo(ctx context.Context, metaArr []FileInfo, modTime time.Time, dataDir string, quorum int) (xmv FileInfo, e error) { func pickValidFileInfo(ctx context.Context, metaArr []FileInfo, modTime time.Time, dataDir string, quorum int) (FileInfo, error) {
return findFileInfoInQuorum(ctx, metaArr, modTime, dataDir, quorum) return findFileInfoInQuorum(ctx, metaArr, modTime, dataDir, quorum)
} }
@ -329,12 +335,16 @@ func objectQuorumFromMeta(ctx context.Context, partsMetaData []FileInfo, errs []
return 0, 0, err return 0, 0, err
} }
dataBlocks := latestFileInfo.Erasure.DataBlocks if !latestFileInfo.IsValid() {
return 0, 0, errErasureReadQuorum
}
parityBlocks := globalStorageClass.GetParityForSC(latestFileInfo.Metadata[xhttp.AmzStorageClass]) parityBlocks := globalStorageClass.GetParityForSC(latestFileInfo.Metadata[xhttp.AmzStorageClass])
if parityBlocks <= 0 { if parityBlocks <= 0 {
parityBlocks = defaultParityCount parityBlocks = defaultParityCount
} }
dataBlocks := len(partsMetaData) - parityBlocks
writeQuorum := dataBlocks writeQuorum := dataBlocks
if dataBlocks == parityBlocks { if dataBlocks == parityBlocks {
writeQuorum++ writeQuorum++

View File

@ -176,29 +176,39 @@ func TestFindFileInfoInQuorum(t *testing.T) {
} }
tests := []struct { tests := []struct {
fis []FileInfo fis []FileInfo
modTime time.Time modTime time.Time
dataDir string dataDir string
expectedErr error expectedErr error
expectedQuorum int
}{ }{
{ {
fis: getNFInfo(16, 16, 1603863445, "36a21454-a2ca-11eb-bbaa-93a81c686f21"), fis: getNFInfo(16, 16, 1603863445, "36a21454-a2ca-11eb-bbaa-93a81c686f21"),
modTime: time.Unix(1603863445, 0), modTime: time.Unix(1603863445, 0),
dataDir: "36a21454-a2ca-11eb-bbaa-93a81c686f21", dataDir: "36a21454-a2ca-11eb-bbaa-93a81c686f21",
expectedErr: nil, expectedErr: nil,
expectedQuorum: 8,
}, },
{ {
fis: getNFInfo(16, 7, 1603863445, "36a21454-a2ca-11eb-bbaa-93a81c686f21"), fis: getNFInfo(16, 7, 1603863445, "36a21454-a2ca-11eb-bbaa-93a81c686f21"),
modTime: time.Unix(1603863445, 0), modTime: time.Unix(1603863445, 0),
dataDir: "36a21454-a2ca-11eb-bbaa-93a81c686f21", dataDir: "36a21454-a2ca-11eb-bbaa-93a81c686f21",
expectedErr: errErasureReadQuorum, expectedErr: errErasureReadQuorum,
expectedQuorum: 8,
},
{
fis: getNFInfo(16, 16, 1603863445, "36a21454-a2ca-11eb-bbaa-93a81c686f21"),
modTime: time.Unix(1603863445, 0),
dataDir: "36a21454-a2ca-11eb-bbaa-93a81c686f21",
expectedErr: errErasureReadQuorum,
expectedQuorum: 0,
}, },
} }
for _, test := range tests { for _, test := range tests {
test := test test := test
t.Run("", func(t *testing.T) { t.Run("", func(t *testing.T) {
_, err := findFileInfoInQuorum(context.Background(), test.fis, test.modTime, test.dataDir, 8) _, err := findFileInfoInQuorum(context.Background(), test.fis, test.modTime, test.dataDir, test.expectedQuorum)
if err != test.expectedErr { if err != test.expectedErr {
t.Errorf("Expected %s, got %s", test.expectedErr, err) t.Errorf("Expected %s, got %s", test.expectedErr, err)
} }