Skip to content

Commit

Permalink
Consistent check for non-existing objects (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanEngelbrecht authored Dec 7, 2021
1 parent f3e200c commit 883905d
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion commands/cmd_clonestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func cloneOneVersion(
return cloneVersionIndex(currentVersionIndex), nil
}

if !errors.Is(err, os.ErrNotExist) {
if !longtaillib.IsNotExist(err) {
return cloneVersionIndex(currentVersionIndex), errors.Wrap(err, fname)
}

Expand Down
5 changes: 2 additions & 3 deletions longtailstorelib/blobStore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"fmt"
"log"
"os"
"sort"
"strings"
"testing"
"time"

"github.com/pkg/errors"
"github.com/DanEngelbrecht/golongtail/longtaillib"
)

func TestCreateStoreAndClient(t *testing.T) {
Expand Down Expand Up @@ -38,7 +37,7 @@ func TestListObjectsInEmptyStore(t *testing.T) {
}
obj, _ := client.NewObject("should-not-exist")
data, err := obj.Read()
if !errors.Is(err, os.ErrNotExist) {
if !longtaillib.IsNotExist(err) {
t.Errorf("TestListObjectsInEmptyStore() obj.Read()) %s", err)
}
if data != nil {
Expand Down
10 changes: 5 additions & 5 deletions longtailstorelib/fsstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"path/filepath"
"strings"

"github.com/DanEngelbrecht/golongtail/longtaillib"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -101,11 +102,10 @@ func (blobClient *fsBlobClient) String() string {
func (blobObject *fsBlobObject) Exists() (bool, error) {
const fname = "fsBlobObject.Exists"
_, err := os.Stat(blobObject.path)
if errors.Is(err, os.ErrNotExist) {
if longtaillib.IsNotExist(err) {
return false, nil
}
if err != nil {
err = errors.Wrap(err, fmt.Sprintf("Failed to check for existance `%s`", blobObject.path))
return false, errors.Wrap(err, fname)
}
return true, nil
Expand Down Expand Up @@ -138,7 +138,7 @@ func (blobObject *fsBlobObject) getMetaGeneration() (int64, error) {
const fname = "fsBlobObject.getMetaGeneration"
metapath := blobObject.path + ".gen"
data, err := ioutil.ReadFile(metapath)
if errors.Is(err, os.ErrNotExist) {
if longtaillib.IsNotExist(err) {
return 0, nil
}
if err != nil {
Expand All @@ -165,7 +165,7 @@ func (blobObject *fsBlobObject) deleteGeneration() error {
const fname = "fsBlobObject.deleteGeneration"
metapath := blobObject.path + ".gen"
_, err := os.Stat(metapath)
if errors.Is(err, os.ErrNotExist) {
if longtaillib.IsNotExist(err) {
return nil
}
err = os.Remove(metapath)
Expand Down Expand Up @@ -299,7 +299,7 @@ func (blobObject *fsBlobObject) Delete() error {
if err != nil {
return errors.Wrap(err, fname)
}
} else if errors.Is(err, os.ErrNotExist) {
} else if longtaillib.IsNotExist(err) {
return nil
}
return err
Expand Down
6 changes: 3 additions & 3 deletions longtailstorelib/fsstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package longtailstorelib
import (
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"testing"

"github.com/pkg/errors"
"golang.org/x/net/context"

"github.com/DanEngelbrecht/golongtail/longtaillib"
)

func TestFSBlobStore(t *testing.T) {
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestListObjectsInEmptyFSStore(t *testing.T) {
}
obj, _ := client.NewObject("should-not-exist")
data, err := obj.Read()
if !errors.Is(err, os.ErrNotExist) {
if !longtaillib.IsNotExist(err) {
t.Errorf("TestListObjectsInEmptyFSStore() obj.Read()) %s", err)
}
if data != nil {
Expand Down
6 changes: 3 additions & 3 deletions longtailstorelib/gcsBlobStore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package longtailstorelib
import (
"fmt"
"net/url"
"os"
"strings"
"sync"
"testing"

"github.com/pkg/errors"
"golang.org/x/net/context"

"github.com/DanEngelbrecht/golongtail/longtaillib"
)

func TestGCSBlobStore(t *testing.T) {
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestListObjectsInEmptyGCSStore(t *testing.T) {
}
obj, _ := client.NewObject("should-not-exist")
data, err := obj.Read()
if !errors.Is(err, os.ErrNotExist) {
if !longtaillib.IsNotExist(err) {
t.Errorf("TestListObjectsInEmptyGCSStore() obj.Read()) %s", err)
}
if data != nil {
Expand Down
2 changes: 2 additions & 0 deletions longtailstorelib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ require (
github.com/pkg/errors v0.9.1
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf
google.golang.org/api v0.57.0

github.com/DanEngelbrecht/golongtail/longtaillib v0.0.0-00010101000000-000000000000
)

require (
Expand Down
7 changes: 6 additions & 1 deletion longtailstorelib/s3Store.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,18 @@ func (blobObject *s3BlobObject) LockWriteVersion() (bool, error) {
}

func (blobObject *s3BlobObject) Exists() (bool, error) {
const fname = "s3BlobObject.Exists()"
input := &s3.GetObjectAclInput{
Bucket: aws.String(blobObject.client.store.bucketName),
Key: aws.String(blobObject.path),
}
_, err := blobObject.client.client.GetObjectAcl(blobObject.client.ctx, input)
if err != nil {
return false, nil
var nsk *types.NoSuchKey
if errors.As(err, &nsk) {
return false, nil
}
return false, errors.Wrap(err, fname)
}
return true, nil
}
Expand Down
6 changes: 3 additions & 3 deletions longtailstorelib/s3Store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package longtailstorelib

import (
"net/url"
"os"
"testing"

"github.com/pkg/errors"
"golang.org/x/net/context"

"github.com/DanEngelbrecht/golongtail/longtaillib"
)

func TestS3BlobStore(t *testing.T) {
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestListObjectsInEmptyS3Store(t *testing.T) {
}
obj, _ := client.NewObject("should-not-exist")
data, err := obj.Read()
if !errors.Is(err, os.ErrNotExist) {
if !longtaillib.IsNotExist(err) {
t.Errorf("TestListObjectsInEmptyS3Store() obj.Read()) %s", err)
}
if data != nil {
Expand Down
5 changes: 2 additions & 3 deletions longtailutils/longtailutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package longtailutils
import (
"context"
"fmt"
"os"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -359,7 +358,7 @@ func DeleteByURI(uri string) error {
return errors.Wrap(err, fname)
}
err = object.Delete()
if err != nil && !errors.Is(err, os.ErrNotExist) {
if err != nil && !longtaillib.IsNotExist(err) {
return errors.Wrap(err, fname)
}
return nil
Expand Down Expand Up @@ -393,7 +392,7 @@ func ReadBlobWithRetry(
retryDelay := []time.Duration{0, 100 * time.Millisecond, 250 * time.Millisecond, 500 * time.Millisecond, 1 * time.Second, 2 * time.Second}
blobData, err := objHandle.Read()
for err != nil {
if errors.Is(err, os.ErrNotExist) {
if longtaillib.IsNotExist(err) {
return nil, retryCount, errors.Wrap(err, fname)
}
if retryCount == len(retryDelay) {
Expand Down
14 changes: 9 additions & 5 deletions remotestore/remotestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ func tryAddRemoteStoreIndexWithLocking(
if err != nil {
return false, longtaillib.Longtail_StoreIndex{}, errors.Wrap(err, fname)
}
if errors.Is(err, os.ErrNotExist) {
if longtaillib.IsNotExist(err) {
return false, longtaillib.Longtail_StoreIndex{}, nil
}

Expand Down Expand Up @@ -1605,7 +1605,7 @@ func readStoreStoreIndexFromPath(
return longtaillib.Longtail_StoreIndex{}, errors.Wrapf(err, fname)
}
if len(blobData) == 0 {
err = errors.Wrap(longtaillib.NotExistErr(), fmt.Sprintf("%s contains no data", key))
err = errors.Wrap(os.ErrNotExist, fmt.Sprintf("%s contains no data", key))
return longtaillib.Longtail_StoreIndex{}, errors.Wrap(err, fname)
}
storeIndex, err := longtaillib.ReadStoreIndexFromBuffer(blobData)
Expand Down Expand Up @@ -1657,10 +1657,14 @@ func mergeStoreIndexItems(
storeIndex := longtaillib.Longtail_StoreIndex{}
for _, item := range items {
tmpStoreIndex, err := readStoreStoreIndexFromPath(ctx, item, client)

if err != nil || (!tmpStoreIndex.IsValid()) {
// The file we expected is no longer there, tell caller that we need to try again
storeIndex.Dispose()
return longtaillib.Longtail_StoreIndex{}, nil, nil
if longtaillib.IsNotExist(err) {
// The file we expected is no longer there, tell caller that we need to try again
return longtaillib.Longtail_StoreIndex{}, nil, nil
}
return longtaillib.Longtail_StoreIndex{}, nil, errors.Wrap(err, fname)
}
if !storeIndex.IsValid() {
storeIndex = tmpStoreIndex
Expand Down Expand Up @@ -1774,7 +1778,7 @@ func readRemoteStoreIndex(
err = errors.Wrap(err, fmt.Sprintf("Cant parse optional store index from `%s`", optionalStoreIndexPath))
log.WithError(err).Info("Failed parsing optional store index")
}
} else if !errors.Is(err, os.ErrNotExist) {
} else if !longtaillib.IsNotExist(err) {
log.WithError(err).Info("Failed reading optional store index")
}
}
Expand Down
5 changes: 0 additions & 5 deletions remotestore/remotestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,21 +760,18 @@ func testStoreIndexSync(blobStore longtailstorelib.BlobStore, t *testing.T) {

blocksIndex, err := longtaillib.CreateStoreIndexFromBlocks(blocks)
if err != nil {
wg.Done()
t.Errorf("longtaillib.CreateStoreIndexFromBlocks() failed with %s", err)
}
newStoreIndex, err := addToRemoteStoreIndex(context.Background(), client, blocksIndex)
blocksIndex.Dispose()
if err != nil {
wg.Done()
t.Errorf("addToRemoteStoreIndex() failed with %s", err)
}
newStoreIndex.Dispose()
}

readStoreIndex, _, err := readStoreStoreIndexWithItems(context.Background(), client)
if err != nil {
wg.Done()
t.Errorf("readStoreStoreIndexWithItems() failed with %s", err)
}
readStoreIndex.Dispose()
Expand All @@ -787,12 +784,10 @@ func testStoreIndexSync(blobStore longtailstorelib.BlobStore, t *testing.T) {
}
generatedBlocksIndex, err = longtaillib.CreateStoreIndexFromBlocks(blocks)
if err != nil {
wg.Done()
t.Errorf("longtaillib.CreateStoreIndexFromBlocks() failed with %s", err)
}
newStoreIndex, err := addToRemoteStoreIndex(context.Background(), client, generatedBlocksIndex)
if err != nil {
wg.Done()
t.Errorf("addToRemoteStoreIndex() failed with %s", err)
}
newStoreIndex.Dispose()
Expand Down

0 comments on commit 883905d

Please sign in to comment.