Skip to content

Commit

Permalink
Fix import dashboards when ulimit -n is low (elastic#4246)
Browse files Browse the repository at this point in the history
The unzip routine was deferring close functions in a loop, so it kept
each file open. This adds a closure so the files are closed immediately.

Fixes elastic#4244.

This also improves the error handling to always report the original error.
  • Loading branch information
tsg authored and ruflin committed May 8, 2017
1 parent 85d448d commit 8c5949f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ https://github.com/elastic/beats/compare/v6.0.0-alpha1...master[Check the HEAD d

*Affecting all Beats*

- Fix importing the dashboards when the limit for max open files is too low. {issue}4244[4244]

*Filebeat*

*Heartbeat*
Expand Down
23 changes: 16 additions & 7 deletions libbeat/dashboards/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (imp Importer) CreateKibanaIndex() error {
},
})
if err != nil {
fmt.Fprintln(os.Stderr, fmt.Sprintf("Failed to set the mapping - %s", err))
imp.statusMsg("Failed to set the mapping: %v", err)
}
return nil
}
Expand Down Expand Up @@ -360,12 +360,13 @@ func (imp Importer) unzip(archive, target string) error {
return err
}

for _, file := range reader.File {
// Closure to close the files on each iteration
unzipFile := func(file *zip.File) error {
filePath := filepath.Join(target, file.Name)

if file.FileInfo().IsDir() {
os.MkdirAll(filePath, file.Mode())
continue
return nil
}
fileReader, err := file.Open()
if err != nil {
Expand All @@ -382,6 +383,14 @@ func (imp Importer) unzip(archive, target string) error {
if _, err := io.Copy(targetFile, fileReader); err != nil {
return err
}
return nil
}

for _, file := range reader.File {
err := unzipFile(file)
if err != nil {
return err
}
}
return nil
}
Expand All @@ -392,16 +401,16 @@ func (imp Importer) ImportArchive() error {

target, err := ioutil.TempDir("", "tmp")
if err != nil {
return errors.New("Failed to generate a temporary directory name")
return fmt.Errorf("Failed to generate a temporary directory name: %v", err)
}

if err = os.MkdirAll(target, 0755); err != nil {
return fmt.Errorf("Failed to create a temporary directory: %v", target)
return fmt.Errorf("Failed to create a temporary directory %s: %v", target, err)
}

defer os.RemoveAll(target) // clean up

imp.statusMsg("Create temporary directory %s", target)
imp.statusMsg("Created temporary directory %s", target)
if imp.cfg.File != "" {
archive = imp.cfg.File
} else if imp.cfg.Snapshot {
Expand All @@ -422,7 +431,7 @@ func (imp Importer) ImportArchive() error {

err = imp.unzip(archive, target)
if err != nil {
return fmt.Errorf("Failed to unzip the archive: %s", archive)
return fmt.Errorf("Failed to unzip the archive: %s: %v", archive, err)
}
dirs, err := getDirectories(target)
if err != nil {
Expand Down

0 comments on commit 8c5949f

Please sign in to comment.