Skip to content

Commit

Permalink
Centralize uses of ghw.Block to prevent misuse.
Browse files Browse the repository at this point in the history
Document that errors are not fatal.
  • Loading branch information
cmacknz committed Oct 26, 2023
1 parent ef15d85 commit 599e267
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
15 changes: 6 additions & 9 deletions internal/pkg/agent/application/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"runtime"
"strings"

"github.com/jaypipes/ghw"

"github.com/otiai10/copy"
"go.elastic.co/apm"

Expand Down Expand Up @@ -389,13 +387,12 @@ func copyDir(l *logger.Logger, from, to string, ignoreErrs bool) error {
// Try to detect if we are running with SSDs. If we are increase the copy concurrency,
// otherwise fall back to the default.
copyConcurrency := 1
block, err := ghw.Block()
if err != nil {
l.Infow("Could not determine block storage type, disabling copy concurrency", "error.message", err)
} else {
if install.HasAllSSDs(*block) {
copyConcurrency = runtime.NumCPU() * 4
}
hasSSDs, detectHWErr := install.HasAllSSDs()
if detectHWErr != nil {
l.Infow("Could not determine block storage type, disabling copy concurrency", "error.message", detectHWErr)
}
if hasSSDs {
copyConcurrency = runtime.NumCPU() * 4
}

return copy.Copy(from, to, copy.Options{
Expand Down
35 changes: 25 additions & 10 deletions internal/pkg/agent/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@ func Install(cfgFile, topPath string, pt *progressbar.ProgressBar, streams *cli.
}

// copy source into install path
// Detecting the block HW type may fail (particularly on MacOS) but this is not a fatal error.
//
// Try to detect if we are running with SSDs. If we are increase the copy concurrency,
// otherwise fall back to the default.
copyConcurrency := 1
block, err := ghw.Block()
if err != nil {
fmt.Fprintf(streams.Out, "Could not determine block hardware type, disabling copy concurrency: %s\n", err)
} else {
if HasAllSSDs(*block) {
copyConcurrency = runtime.NumCPU() * 4
}
hasSSDs, detectHWErr := HasAllSSDs()
if detectHWErr != nil {
fmt.Fprintf(streams.Out, "Could not determine block hardware type, disabling copy concurrency: %s\n", detectHWErr)
}
if hasSSDs {
copyConcurrency = runtime.NumCPU() * 4
}

pt.Describe("Copying install files")
Expand Down Expand Up @@ -266,8 +267,22 @@ func verifyDirectory(dir string) error {
}

// HasAllSSDs returns true if the host we are on uses SSDs for
// all its persistent storage; false otherwise or on error
func HasAllSSDs(block ghw.BlockInfo) bool {
// all its persistent storage; false otherwise. Returns any error
// encountered detecting the hardware type for informational purposes.
// Errors from this function are not fatal. Note that errors may be
// returned on some Mac hardware configurations as the ghw package
// does not fully support MacOS.
func HasAllSSDs() (bool, error) {
block, err := ghw.Block()
if err != nil {
return false, err
}

return hasAllSSDs(*block), nil
}

// Internal version of HasAllSSDs for testing.
func hasAllSSDs(block ghw.BlockInfo) bool {
for _, disk := range block.Disks {
switch disk.DriveType {
case ghw.DRIVE_TYPE_FDD, ghw.DRIVE_TYPE_ODD:
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/install/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestHasAllSSDs(t *testing.T) {

for name, test := range cases {
t.Run(name, func(t *testing.T) {
actual := HasAllSSDs(test.block)
actual := hasAllSSDs(test.block)
require.Equal(t, test.expected, actual)
})
}
Expand Down

0 comments on commit 599e267

Please sign in to comment.