Skip to content

Commit

Permalink
deprecate listDir usage for healing (minio#9792)
Browse files Browse the repository at this point in the history
listDir was incorrectly used for healing which
is slower, instead use Walk() to heal the entire
set.
  • Loading branch information
harshavardhana authored Jun 10, 2020
1 parent 1a0b7f5 commit 342ade0
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 217 deletions.
2 changes: 1 addition & 1 deletion cmd/background-newdisks-heal-ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func monitorLocalDisksAndHeal(ctx context.Context, objAPI ObjectLayer) {
// Heal all erasure sets that need
for i, erasureSetToHeal := range erasureSetInZoneToHeal {
for _, setIndex := range erasureSetToHeal {
err := healErasureSet(ctx, setIndex, z.zones[i].sets[setIndex])
err := healErasureSet(ctx, setIndex, z.zones[i].sets[setIndex], z.zones[i].drivesPerSet)
if err != nil {
logger.LogIf(ctx, err)
}
Expand Down
40 changes: 33 additions & 7 deletions cmd/global-heal.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func getLocalBackgroundHealStatus() madmin.BgHealState {
}

// healErasureSet lists and heals all objects in a specific erasure set
func healErasureSet(ctx context.Context, setIndex int, xlObj *xlObjects) error {
func healErasureSet(ctx context.Context, setIndex int, xlObj *xlObjects, drivesPerSet int) error {
buckets, err := xlObj.ListBuckets(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -108,12 +108,38 @@ func healErasureSet(ctx context.Context, setIndex int, xlObj *xlObjects) error {
path: bucket.Name,
}

// List all objects in the current bucket and heal them
listDir := listDirFactory(ctx, xlObj.getLoadBalancedDisks()...)
walkResultCh := startTreeWalk(ctx, bucket.Name, "", "", true, listDir, nil)
for walkEntry := range walkResultCh {
var entryChs []FileInfoCh
for _, disk := range xlObj.getLoadBalancedDisks() {
if disk == nil {
// Disk can be offline
continue
}
entryCh, err := disk.Walk(bucket.Name, "", "", true, xlMetaJSONFile, readMetadata, ctx.Done())
if err != nil {
// Disk walk returned error, ignore it.
continue
}
entryChs = append(entryChs, FileInfoCh{
Ch: entryCh,
})
}

entriesValid := make([]bool, len(entryChs))
entries := make([]FileInfo, len(entryChs))

for {
entry, quorumCount, ok := lexicallySortedEntry(entryChs, entries, entriesValid)
if !ok {
return nil
}

if quorumCount == drivesPerSet {
// Skip good entries.
continue
}

bgSeq.sourceCh <- healSource{
path: pathJoin(bucket.Name, walkEntry.entry),
path: pathJoin(bucket.Name, entry.Name),
}
}
}
Expand Down Expand Up @@ -173,7 +199,7 @@ func execLeaderTasks(ctx context.Context, z *xlZones) {
for _, zone := range z.zones {
// Heal set by set
for i, set := range zone.sets {
if err := healErasureSet(ctx, i, set); err != nil {
if err := healErasureSet(ctx, i, set, zone.drivesPerSet); err != nil {
logger.LogIf(ctx, err)
continue
}
Expand Down
48 changes: 27 additions & 21 deletions cmd/tree-walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ import (
"time"
)

// Returns function "listDir" of the type listDirFunc.
// disks - used for doing disk.ListDir()
func listDirFactory(ctx context.Context, disk StorageAPI) ListDirFunc {
// Returns sorted merged entries from all the disks.
listDir := func(volume, dirPath, dirEntry string) (bool, []string) {
entries, err := disk.ListDir(volume, dirPath, -1, xlMetaJSONFile)
if err != nil {
return false, nil
}
if len(entries) == 0 {
return true, nil
}
sort.Strings(entries)
return false, filterMatchingPrefix(entries, dirEntry)
}
return listDir
}

// Fixed volume name that could be used across tests
const volume = "testvolume"

Expand Down Expand Up @@ -219,8 +237,7 @@ func TestTreeWalkTimeout(t *testing.T) {
}
}

// Test ListDir - listDir should list entries from the first disk, if the first disk is down,
// it should list from the next disk.
// Test ListDir - listDir is expected to only list one disk.
func TestListDir(t *testing.T) {
file1 := "file1"
file2 := "file2"
Expand Down Expand Up @@ -248,7 +265,8 @@ func TestListDir(t *testing.T) {
}

// create listDir function.
listDir := listDirFactory(context.Background(), disk1, disk2)
listDir1 := listDirFactory(context.Background(), disk1)
listDir2 := listDirFactory(context.Background(), disk2)

// Create file1 in fsDir1 and file2 in fsDir2.
disks := []StorageAPI{disk1, disk2}
Expand All @@ -260,35 +278,23 @@ func TestListDir(t *testing.T) {
}

// Should list "file1" from fsDir1.
_, entries := listDir(volume, "", "")
if len(entries) != 2 {
t.Fatal("Expected the number of entries to be 2")
_, entries := listDir1(volume, "", "")
if len(entries) != 1 {
t.Fatal("Expected the number of entries to be 1")
}

if entries[0] != file1 {
t.Fatal("Expected the entry to be file1")
}
if entries[1] != file2 {
t.Fatal("Expected the entry to be file2")
}

// Remove fsDir1, list should return entries from fsDir2
err = os.RemoveAll(fsDir1)
if err != nil {
t.Error(err)
}

// Should list "file2" from fsDir2.
_, entries = listDir(volume, "", "")
_, entries = listDir2(volume, "", "")
if len(entries) != 1 {
t.Fatal("Expected the number of entries to be 1")
}

if entries[0] != file2 {
t.Fatal("Expected the entry to be file2")
}
err = os.RemoveAll(fsDir2)
if err != nil {
t.Error(err)
}
}

// TestRecursiveWalk - tests if treeWalk returns entries correctly with and
Expand Down
18 changes: 9 additions & 9 deletions cmd/xl-sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,9 @@ func (f *FileInfoCh) Push(fi FileInfo) {
// we found this entry. Additionally also returns a boolean
// to indicate if the caller needs to call this function
// again to list the next entry. It is callers responsibility
// if the caller wishes to list N entries to call leastEntry
// if the caller wishes to list N entries to call lexicallySortedEntry
// N times until this boolean is 'false'.
func leastEntry(entryChs []FileInfoCh, entries []FileInfo, entriesValid []bool) (FileInfo, int, bool) {
func lexicallySortedEntry(entryChs []FileInfoCh, entries []FileInfo, entriesValid []bool) (FileInfo, int, bool) {
for i := range entryChs {
entries[i], entriesValid[i] = entryChs[i].Pop()
}
Expand Down Expand Up @@ -852,7 +852,7 @@ func leastEntry(entryChs []FileInfoCh, entries []FileInfo, entriesValid []bool)
return lentry, 0, isTruncated
}

leastEntryCount := 0
lexicallySortedEntryCount := 0
for i, valid := range entriesValid {
if !valid {
continue
Expand All @@ -861,7 +861,7 @@ func leastEntry(entryChs []FileInfoCh, entries []FileInfo, entriesValid []bool)
// Entries are duplicated across disks,
// we should simply skip such entries.
if lentry.Name == entries[i].Name && lentry.ModTime.Equal(entries[i].ModTime) {
leastEntryCount++
lexicallySortedEntryCount++
continue
}

Expand All @@ -870,7 +870,7 @@ func leastEntry(entryChs []FileInfoCh, entries []FileInfo, entriesValid []bool)
entryChs[i].Push(entries[i])
}

return lentry, leastEntryCount, isTruncated
return lentry, lexicallySortedEntryCount, isTruncated
}

// mergeEntriesCh - merges FileInfo channel to entries upto maxKeys.
Expand All @@ -879,7 +879,7 @@ func mergeEntriesCh(entryChs []FileInfoCh, maxKeys int, ndisks int) (entries Fil
entriesInfos := make([]FileInfo, len(entryChs))
entriesValid := make([]bool, len(entryChs))
for {
fi, quorumCount, valid := leastEntry(entryChs, entriesInfos, entriesValid)
fi, quorumCount, valid := lexicallySortedEntry(entryChs, entriesInfos, entriesValid)
if !valid {
// We have reached EOF across all entryChs, break the loop.
break
Expand Down Expand Up @@ -1003,7 +1003,7 @@ func (s *xlSets) listObjectsNonSlash(ctx context.Context, bucket, prefix, marker
break
}

result, quorumCount, ok := leastEntry(entryChs, entries, entriesValid)
result, quorumCount, ok := lexicallySortedEntry(entryChs, entries, entriesValid)
if !ok {
eof = true
break
Expand Down Expand Up @@ -1690,7 +1690,7 @@ func (s *xlSets) Walk(ctx context.Context, bucket, prefix string, results chan<-
defer close(results)

for {
entry, quorumCount, ok := leastEntry(entryChs, entries, entriesValid)
entry, quorumCount, ok := lexicallySortedEntry(entryChs, entries, entriesValid)
if !ok {
return
}
Expand All @@ -1716,7 +1716,7 @@ func (s *xlSets) HealObjects(ctx context.Context, bucket, prefix string, opts ma
entriesValid := make([]bool, len(entryChs))
entries := make([]FileInfo, len(entryChs))
for {
entry, quorumCount, ok := leastEntry(entryChs, entries, entriesValid)
entry, quorumCount, ok := lexicallySortedEntry(entryChs, entries, entriesValid)
if !ok {
break
}
Expand Down
Loading

0 comments on commit 342ade0

Please sign in to comment.