Skip to content

Commit

Permalink
Log DatasetDoesNotExist with Debug level (#4)
Browse files Browse the repository at this point in the history
We don't need log messages like this

```
2024/10/18 21:38:01 ERROR command exited with error subsystem=zfs.cmd job=mpool-to-bpool cmd="zfs get -Hp -o name,property,value,source zrepl:placeholder bpool/zrepl/localhost" err="wait \"/sbin/zfs get -Hp -o name,property,value,source zrepl:placeholder bpool/zrepl/localhost\": exit status 1" status=1 systemtime_s=0 total_time_s=0.003441288 usertime_s=0.003835
```

because it's OK a placeholder dataset doesn't exist. So let's log it with
`Debug` level, instead of `Error`.
  • Loading branch information
dsh2dsh committed Oct 19, 2024
1 parent 22b5e8c commit 04fb39f
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions internal/zfs/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ type ZFSListResult struct {
func ZFSListIter(ctx context.Context, properties []string,
notExistHint *DatasetPath, zfsArgs ...string,
) iter.Seq[ZFSListResult] {
cmd := zfscmd.CommandContext(ctx, ZfsBin, zfsListArgs(properties, zfsArgs)...)
cmd := zfscmd.CommandContext(ctx, ZfsBin,
zfsListArgs(properties, zfsArgs)...).WithLogError(false)
var stderrBuf bytes.Buffer
stdout, err := cmd.StdoutPipeWithErrorBuf(&stderrBuf)

Expand All @@ -226,7 +227,7 @@ func ZFSListIter(ctx context.Context, properties []string,
})
if err != nil {
if notExistHint != nil {
err = maybeDatasetNotExists(notExistHint.ToString(), err)
err = maybeDatasetNotExists(cmd, notExistHint.ToString(), err)
}
yield(ZFSListResult{Err: err})
}
Expand Down Expand Up @@ -267,14 +268,21 @@ func scanCmdOutput(cmd *zfscmd.Cmd, r io.Reader, stderrBuf *bytes.Buffer,
return
}

func maybeDatasetNotExists(path string, err error) error {
func maybeDatasetNotExists(cmd *zfscmd.Cmd, path string, err error) error {
var zfsError *ZFSError
if errors.As(err, &zfsError) && len(zfsError.Stderr) != 0 {
if !errors.As(err, &zfsError) {
return err
}

if len(zfsError.Stderr) != 0 {
enotexist := tryDatasetDoesNotExist(path, zfsError.Stderr)
if enotexist != nil {
cmd.LogError(err, true)
return enotexist
}
}

cmd.LogError(err, false)
return err
}

Expand Down Expand Up @@ -1473,7 +1481,7 @@ func ZFSGetRecursive(ctx context.Context, path string, depth int,
dstypes []string, props []string, allowedSources PropertySource,
) (map[string]*ZFSProperties, error) {
cmd := zfscmd.CommandContext(ctx, ZfsBin,
zfsGetArgs(path, depth, dstypes, props)...)
zfsGetArgs(path, depth, dstypes, props)...).WithLogError(false)
var stderrBuf bytes.Buffer
stdout, err := cmd.StdoutPipeWithErrorBuf(&stderrBuf)
if err != nil {
Expand All @@ -1493,7 +1501,7 @@ func ZFSGetRecursive(ctx context.Context, path string, depth int,
return nil, true
})
if err != nil {
return nil, maybeDatasetNotExists(path, err)
return nil, maybeDatasetNotExists(cmd, path, err)
}

// validate we got expected output
Expand Down

0 comments on commit 04fb39f

Please sign in to comment.