Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds get folder method in bucket.go #2041

Merged
merged 8 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/gcsx/prefix_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"
"unicode/utf8"

"cloud.google.com/go/storage/control/apiv2/controlpb"
"github.com/googlecloudplatform/gcsfuse/v2/internal/storage/gcs"
"golang.org/x/net/context"
)
Expand Down Expand Up @@ -216,3 +217,8 @@ func (b *prefixBucket) DeleteFolder(ctx context.Context, folderName string) (err
mFolderName := b.wrappedName(folderName)
return b.wrapped.DeleteFolder(ctx, mFolderName)
}

func (b *prefixBucket) GetFolder(ctx context.Context, folderName string) (folder *controlpb.Folder, err error) {
mFolderName := b.wrappedName(folderName)
return b.wrapped.GetFolder(ctx, mFolderName)
}
22 changes: 22 additions & 0 deletions internal/gcsx/prefix_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,25 @@ func (t *PrefixBucketTest) DeleteObject() {
var notFoundErr *gcs.NotFoundError
ExpectTrue(errors.As(err, &notFoundErr))
}

func (t *PrefixBucketTest) GetFolder_Prefix() {
var err error

// Replace the use of CreateObject with CreateFolder once the CreateFolder API has been successfully implemented.
err = storageutil.CreateObjects(
t.ctx,
t.wrapped,
map[string][]byte{
"something": []byte(""),
})

AssertEq(nil, err)

result, err := t.bucket.GetFolder(
t.ctx,
"something")

AssertEq(nil, err)
AssertEq("projects/_/buckets/some_bucket/folders/foo_something", result.GetName())

}
8 changes: 8 additions & 0 deletions internal/monitor/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"io"
"time"

"cloud.google.com/go/storage/control/apiv2/controlpb"
"github.com/googlecloudplatform/gcsfuse/v2/internal/logger"
"github.com/googlecloudplatform/gcsfuse/v2/internal/monitor/tags"
"github.com/googlecloudplatform/gcsfuse/v2/internal/storage/gcs"
Expand Down Expand Up @@ -202,6 +203,13 @@ func (mb *monitoringBucket) DeleteFolder(ctx context.Context, folderName string)
return err
}

func (mb *monitoringBucket) GetFolder(ctx context.Context, folderName string) (*controlpb.Folder, error) {
startTime := time.Now()
folder, err := mb.wrapped.GetFolder(ctx, folderName)
recordRequest(ctx, "GetFolder", startTime)
return folder, err
}

// recordReader increments the reader count when it's opened or closed.
func recordReader(ctx context.Context, ioMethod string) {
if err := stats.RecordWithTags(
Expand Down
14 changes: 14 additions & 0 deletions internal/ratelimit/throttled_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package ratelimit
import (
"io"

"cloud.google.com/go/storage/control/apiv2/controlpb"
"github.com/googlecloudplatform/gcsfuse/v2/internal/storage/gcs"
"golang.org/x/net/context"
)
Expand Down Expand Up @@ -197,6 +198,19 @@ func (b *throttledBucket) DeleteFolder(ctx context.Context, folderName string) (
return
}

func (b *throttledBucket) GetFolder(ctx context.Context, folderName string) (folder *controlpb.Folder, err error) {
// Wait for permission to call through.
err = b.opThrottle.Wait(ctx, 1)
if err != nil {
return
}

// Call through.
folder, err = b.wrapped.GetFolder(ctx, folderName)

return folder, err
}

////////////////////////////////////////////////////////////////////////
// readerCloser
////////////////////////////////////////////////////////////////////////
Expand Down
14 changes: 14 additions & 0 deletions internal/storage/bucket_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,20 @@ func (b *bucketHandle) getStorageLayout() (*controlpb.StorageLayout, error) {
return stoargeLayout, err
}

func (b *bucketHandle) GetFolder(ctx context.Context, folderName string) (*controlpb.Folder, error) {
var callOptions []gax.CallOption

folder, err := b.controlClient.GetFolder(ctx, &controlpb.GetFolderRequest{
Name: "projects/_/buckets/" + b.bucketName + "/folders/" + folderName,
}, callOptions...)

if err != nil {
ankitaluthra1 marked this conversation as resolved.
Show resolved Hide resolved
err = fmt.Errorf("error getting metadata for folder: %s, %w", folderName, err)
}

return folder, err
}

func isStorageConditionsNotEmpty(conditions storage.Conditions) bool {
return conditions != (storage.Conditions{})
}
38 changes: 38 additions & 0 deletions internal/storage/bucket_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

const missingObjectName string = "test/foo"
Expand Down Expand Up @@ -1291,3 +1293,39 @@ func (testSuite *BucketHandleTest) TestDeleteFolderWhenFolderNotExistForHierarch
mockClient.AssertExpectations(testSuite.T())
assert.Equal(testSuite.T(), "DeleteFolder: mock error", err.Error())
}

func (testSuite *BucketHandleTest) TestGetFolderWhenFolderExistsForHierarchicalBucket() {
ctx := context.Background()
mockClient := new(MockStorageControlClient)
folderPath := "projects/_/buckets/" + TestBucketName + "/folders/" + TestObjectName
mockFolder := controlpb.Folder{
Name: folderPath,
}
mockClient.On("GetFolder", ctx, &controlpb.GetFolderRequest{Name: folderPath}, mock.Anything).
Return(&mockFolder, nil)
testSuite.bucketHandle.controlClient = mockClient
testSuite.bucketHandle.bucketType = gcs.Hierarchical

result, err := testSuite.bucketHandle.GetFolder(ctx, TestObjectName)

mockClient.AssertExpectations(testSuite.T())
assert.Nil(testSuite.T(), err)
assert.Equal(testSuite.T(), folderPath, result.GetName())
}

func (testSuite *BucketHandleTest) TestGetFolderWhenFolderDoesNotExistsForHierarchicalBucket() {
ctx := context.Background()
mockClient := new(MockStorageControlClient)
folderPath := "projects/_/buckets/" + TestBucketName + "/folders/" + missingObjectName

mockClient.On("GetFolder", ctx, &controlpb.GetFolderRequest{Name: folderPath}, mock.Anything).
Return(nil, status.Error(codes.NotFound, "folder not found"))
testSuite.bucketHandle.controlClient = mockClient
testSuite.bucketHandle.bucketType = gcs.Hierarchical

result, err := testSuite.bucketHandle.GetFolder(ctx, missingObjectName)

mockClient.AssertExpectations(testSuite.T())
assert.Nil(testSuite.T(), result)
assert.ErrorContains(testSuite.T(), err, "folder not found")
}
18 changes: 17 additions & 1 deletion internal/storage/caching/fast_stat_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sync"
"time"

"cloud.google.com/go/storage/control/apiv2/controlpb"
"github.com/googlecloudplatform/gcsfuse/v2/internal/cache/metadata"
"github.com/googlecloudplatform/gcsfuse/v2/internal/storage/gcs"
"github.com/googlecloudplatform/gcsfuse/v2/internal/storage/storageutil"
Expand Down Expand Up @@ -140,7 +141,7 @@ func (b *fastStatBucket) CreateObject(
// Throw away any existing record for this object.
b.invalidate(req.Name)

// Create the new object.
// TODO: create object to be replaced with create folder api once integrated
o, err = b.wrapped.CreateObject(ctx, req)
if err != nil {
return
Expand Down Expand Up @@ -294,3 +295,18 @@ func (b *fastStatBucket) StatObjectFromGcs(ctx context.Context,

return
}

func (b *fastStatBucket) GetFolder(
ctx context.Context,
prefix string) (folder *controlpb.Folder, err error) {
// Fetch the listing.
folder, err = b.wrapped.GetFolder(ctx, prefix)
if err != nil {
return
}

// TODO: add folder metadata in stat cache
// b.insertFolder(folder)
ankitaluthra1 marked this conversation as resolved.
Show resolved Hide resolved

return
}
2 changes: 2 additions & 0 deletions internal/storage/control_client_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ type StorageControlClient interface {
DeleteFolder(ctx context.Context,
req *controlpb.DeleteFolderRequest,
opts ...gax.CallOption) error

GetFolder(ctx context.Context, req *controlpb.GetFolderRequest, opts ...gax.CallOption) (*controlpb.Folder, error)
}
9 changes: 9 additions & 0 deletions internal/storage/debug_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sync/atomic"
"time"

"cloud.google.com/go/storage/control/apiv2/controlpb"
"github.com/googlecloudplatform/gcsfuse/v2/internal/logger"
"github.com/googlecloudplatform/gcsfuse/v2/internal/storage/gcs"
"golang.org/x/net/context"
Expand Down Expand Up @@ -237,3 +238,11 @@ func (b *debugBucket) DeleteFolder(ctx context.Context, folderName string) (err
err = b.wrapped.DeleteFolder(ctx, folderName)
return err
}

func (b *debugBucket) GetFolder(ctx context.Context, folderName string) (folder *controlpb.Folder, err error) {
id, desc, start := b.startRequest("GetFolder(%q)", folderName)
defer b.finishRequest(id, desc, start, &err)

folder, err = b.wrapped.GetFolder(ctx, folderName)
return
}
8 changes: 8 additions & 0 deletions internal/storage/fake/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"
"unicode/utf8"

"cloud.google.com/go/storage/control/apiv2/controlpb"
"github.com/googlecloudplatform/gcsfuse/v2/internal/storage/gcs"
"github.com/googlecloudplatform/gcsfuse/v2/internal/storage/storageutil"
"github.com/jacobsa/syncutil"
Expand Down Expand Up @@ -882,3 +883,10 @@ func (b *bucket) DeleteFolder(ctx context.Context, folderName string) (err error

return
}

func (b *bucket) GetFolder(ctx context.Context, foldername string) (*controlpb.Folder, error) {
folder := controlpb.Folder{
Name: "projects/_/buckets/" + b.Name() + "/folders/" + foldername,
}
return &folder, nil
}
3 changes: 3 additions & 0 deletions internal/storage/gcs/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package gcs
import (
"io"

"cloud.google.com/go/storage/control/apiv2/controlpb"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -142,4 +143,6 @@ type Bucket interface {
req *DeleteObjectRequest) error

DeleteFolder(ctx context.Context, folderName string) error

GetFolder(ctx context.Context, folderName string) (*controlpb.Folder, error)
}
30 changes: 30 additions & 0 deletions internal/storage/mock_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
runtime "runtime"
unsafe "unsafe"

"cloud.google.com/go/storage/control/apiv2/controlpb"
"github.com/googlecloudplatform/gcsfuse/v2/internal/storage/gcs"
oglemock "github.com/jacobsa/oglemock"
context "golang.org/x/net/context"
Expand Down Expand Up @@ -345,3 +346,32 @@ func (m *mockBucket) UpdateObject(p0 context.Context, p1 *gcs.UpdateObjectReques

return
}

func (m *mockBucket) GetFolder(
ctx context.Context,
prefix string) (o0 *controlpb.Folder, o1 error) {
// Get a file name and line number for the caller.
_, file, line, _ := runtime.Caller(1)

// Hand the call off to the controller, which does most of the work.
retVals := m.controller.HandleMethodCall(
m,
"GetFolder",
file,
line,
[]interface{}{})

if len(retVals) != 2 {
panic(fmt.Sprintf("mockBucket.GetFolder: invalid return values: %v", retVals))
}

if retVals[0] != nil {
o0 = retVals[0].(*controlpb.Folder)
}

// o1 error
if retVals[1] != nil {
o1 = retVals[1].(error)
}
return
}
13 changes: 13 additions & 0 deletions internal/storage/mock_control_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,16 @@ func (m *MockStorageControlClient) DeleteFolder(ctx context.Context,
args := m.Called(ctx, req, opts)
return args.Error(0)
}

func (m *MockStorageControlClient) GetFolder(ctx context.Context,
req *controlpb.GetFolderRequest,
opts ...gax.CallOption) (*controlpb.Folder, error) {
args := m.Called(ctx, req, opts)

// Needed to assert folder in only those cases where folder is present
if folder, ok := args.Get(0).(*controlpb.Folder); ok {
ankitaluthra1 marked this conversation as resolved.
Show resolved Hide resolved
return folder, nil
}

return nil, args.Error(1)
}
Loading