Skip to content

Commit

Permalink
fix: improve error messages opening index partitions (#23532)
Browse files Browse the repository at this point in the history
Where possible, add the file path path to any errors
on opening, reading, (un)marshaling, or validating
the various files comprising a partition

closes #23506

(cherry picked from commit a2dd708)

closes #23534
  • Loading branch information
davidby-influx committed Jul 12, 2022
1 parent 4da4d03 commit f4ff623
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
3 changes: 2 additions & 1 deletion tsdb/index/tsi1/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tsi1_test

import (
"compress/gzip"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -295,7 +296,7 @@ func TestIndex_Open(t *testing.T) {
// Opening this index should return an error because the MANIFEST has an
// incompatible version.
err = idx.Open()
if err != tsi1.ErrIncompatibleVersion {
if !errors.Is(err, tsi1.ErrIncompatibleVersion) {
idx.Close()
t.Fatalf("got error %v, expected %v", err, tsi1.ErrIncompatibleVersion)
}
Expand Down
20 changes: 11 additions & 9 deletions tsdb/index/tsi1/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/influxdata/influxdb/v2/logger"
"github.com/influxdata/influxdb/v2/models"
"github.com/influxdata/influxdb/v2/pkg/bytesutil"
errors2 "github.com/influxdata/influxdb/v2/pkg/errors"
"github.com/influxdata/influxdb/v2/pkg/estimator"
"github.com/influxdata/influxdb/v2/tsdb"
"github.com/influxdata/influxql"
Expand Down Expand Up @@ -151,23 +152,24 @@ func (p *Partition) Open() (rErr error) {
p.closing = make(chan struct{})

if p.opened {
return errors.New("index partition already open")
return fmt.Errorf("index partition already open: %q", p.path)
}

// Validate path is correct.
p.id = filepath.Base(p.path)
_, err := strconv.Atoi(p.id)
if err != nil {
return err
return fmt.Errorf("poorly formed manifest file path, %q: %w", p.path, err)
}

// Create directory if it doesn't exist.
if err := os.MkdirAll(p.path, 0777); err != nil {
return err
}

filename := filepath.Join(p.path, ManifestFileName)
// Read manifest file.
m, manifestSize, err := ReadManifestFile(filepath.Join(p.path, ManifestFileName))
m, manifestSize, err := ReadManifestFile(filename)
if os.IsNotExist(err) {
m = NewManifest(p.ManifestPath())
} else if err != nil {
Expand Down Expand Up @@ -277,12 +279,12 @@ func (p *Partition) openIndexFile(path string) (*IndexFile, error) {
}

// deleteNonManifestFiles removes all files not in the manifest.
func (p *Partition) deleteNonManifestFiles(m *Manifest) error {
func (p *Partition) deleteNonManifestFiles(m *Manifest) (rErr error) {
dir, err := os.Open(p.path)
if err != nil {
return err
}
defer dir.Close()
defer errors2.Capture(&rErr, dir.Close)()

fis, err := dir.Readdir(-1)
if err != nil {
Expand All @@ -301,7 +303,7 @@ func (p *Partition) deleteNonManifestFiles(m *Manifest) error {
}
}

return dir.Close()
return nil
}

func (p *Partition) buildSeriesSet() error {
Expand Down Expand Up @@ -1379,7 +1381,7 @@ func (m *Manifest) Validate() error {
// If we don't have an explicit version in the manifest file then we know
// it's not compatible with the latest tsi1 Index.
if m.Version != Version {
return ErrIncompatibleVersion
return fmt.Errorf("%q: %w", m.path, ErrIncompatibleVersion)
}
return nil
}
Expand All @@ -1389,7 +1391,7 @@ func (m *Manifest) Validate() error {
func (m *Manifest) Write() (int64, error) {
buf, err := json.MarshalIndent(m, "", " ")
if err != nil {
return 0, err
return 0, fmt.Errorf("failed marshaling %q: %w", m.path, err)
}
buf = append(buf, '\n')

Expand All @@ -1410,7 +1412,7 @@ func ReadManifestFile(path string) (*Manifest, int64, error) {
// Decode manifest.
var m Manifest
if err := json.Unmarshal(buf, &m); err != nil {
return nil, 0, err
return nil, 0, fmt.Errorf("failed unmarshaling %q: %w", path, err)
}

// Set the path of the manifest.
Expand Down
3 changes: 2 additions & 1 deletion tsdb/index/tsi1/partition_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tsi1_test

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -61,7 +62,7 @@ func TestPartition_Open(t *testing.T) {
// Opening this index should return an error because the MANIFEST has an
// incompatible version.
err = p.Open()
if err != tsi1.ErrIncompatibleVersion {
if !errors.Is(err, tsi1.ErrIncompatibleVersion) {
p.Close()
t.Fatalf("got error %v, expected %v", err, tsi1.ErrIncompatibleVersion)
}
Expand Down

0 comments on commit f4ff623

Please sign in to comment.