Skip to content

Commit

Permalink
refactor(category): ignore partially deleted data dir (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
dakimura authored Sep 19, 2021
1 parent a015238 commit df956c0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
13 changes: 11 additions & 2 deletions catalog/catalog.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package catalog

import (
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -10,6 +11,8 @@ import (
"strings"
"sync"

"github.com/alpacahq/marketstore/v4/utils/log"

"github.com/alpacahq/marketstore/v4/utils/io"
)

Expand Down Expand Up @@ -55,7 +58,7 @@ func load(rootDmap *sync.Map, d *Directory, subPath, rootPath string) error {
catFilePath := subPath + "/" + "category_name"
catname, err := ioutil.ReadFile(catFilePath)
if err != nil {
return &ErrCategoryFileNotFound{filePath: catFilePath, msg: io.GetCallerFileContext(0) + err.Error()}
return ErrCategoryFileNotFound{filePath: catFilePath, msg: io.GetCallerFileContext(0) + err.Error()}
}
d.category = string(catname)

Expand All @@ -76,7 +79,13 @@ func load(rootDmap *sync.Map, d *Directory, subPath, rootPath string) error {

d.datafile = nil
if err := load(rootDmap, d.subDirs[itemName], leafPath, rootPath); err != nil {
return fmt.Errorf(io.GetCallerFileContext(0) + ", " + err.Error())
var e ErrCategoryFileNotFound
if errors.As(err, &e) {
log.Warn(fmt.Sprintf("category_name file not found under the directory."+
"%s will be ignored:%v", leafPath, err.Error()))
} else {
return fmt.Errorf(io.GetCallerFileContext(0) + ", " + err.Error())
}
}
} else if filepath.Ext(leafPath) == ".bin" {
rootDmap.Store(d.pathToItemName, d)
Expand Down
2 changes: 1 addition & 1 deletion catalog/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestAddAndRemoveDataItem(t *testing.T) {
func TestAddAndRemoveDataItemFromEmptyDirectory(t *testing.T) {
rootDir, _ := ioutil.TempDir("", "catalog_test-TestAddAndRemoveDataItemFromEmptyDirectory")
catalogDir, err := catalog.NewDirectory(rootDir)
var e *catalog.ErrCategoryFileNotFound
var e catalog.ErrCategoryFileNotFound
if err != nil && !errors.As(err, &e) {
t.Fatal("failed to create a catalog dir.err=" + err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion catalog/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ type ErrCategoryFileNotFound struct {
msg string
}

func (e *ErrCategoryFileNotFound) Error() string {
func (e ErrCategoryFileNotFound) Error() string {
return "Could not find a category_name file under:" + e.filePath + ", msg=" + e.msg
}
2 changes: 1 addition & 1 deletion executor/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestAddDir(t *testing.T) {

// make catelog directory
catDir, err := NewDirectory(tempRootDir)
var e *ErrCategoryFileNotFound
var e ErrCategoryFileNotFound
if err != nil && !errors.As(err, &e) {
t.Fatal("failed to create a catalog dir.err=" + err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion executor/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func NewInstanceSetup(relRootDir string, rs ReplicationSender, tm []*trigger.Tri
if initCatalog {
ThisInstance.CatalogDir, err = catalog.NewDirectory(rootDir)
if err != nil {
var e *catalog.ErrCategoryFileNotFound
var e catalog.ErrCategoryFileNotFound
if errors.As(err, &e) {
log.Debug("new root directory found:" + rootDir)
} else {
Expand Down

0 comments on commit df956c0

Please sign in to comment.