Skip to content

Commit

Permalink
fix: address comments, change path params to query params
Browse files Browse the repository at this point in the history
  • Loading branch information
annielz committed May 10, 2023
1 parent 4bb1a96 commit 2743b7e
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 52 deletions.
4 changes: 4 additions & 0 deletions model/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ const (
ListBucketReadRecordQuery = "list-read-record"
// ListBucketReadRecordMaxRecordsQuery defines list read record max num
ListBucketReadRecordMaxRecordsQuery = "max-records"
// GetBucketMetaByNameQuery defines get bucket metadata query, which is used to route request
GetBucketMetaByNameQuery = "bucket-meta"
// GetObjectMetaByNameQuery defines get object metadata query, which is used to route request
GetObjectMetaByNameQuery = "object-meta"
// StartTimestampUs defines start timestamp in microsecond, which is used by list read record, [start_ts,end_ts)
StartTimestampUs = "start-timestamp"
// EndTimestampUs defines end timestamp in microsecond, which is used by list read record, [start_ts,end_ts)
Expand Down
12 changes: 6 additions & 6 deletions proto/service/metadata/types/metadata.proto
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,16 @@ message GetPaymentByBucketIDRequest {
bool is_full_list = 2;
}

// GetBucketWithPaymentRequest is request type for the GetBucketWithPayment RPC method
message GetBucketWithPaymentRequest {
// GetBucketMetaByNameRequest is request type for the GetBucketMetaByName RPC method
message GetBucketMetaByNameRequest {
// bucket_name is the name of the bucket
string bucket_name = 1;
// is_full_list indicates whether this request can get the private buckets information
bool is_full_list = 2;
}

// GetBucketWithPaymentResponse is response type for the GetBucketWithPayment RPC method
message GetBucketWithPaymentResponse {
// GetBucketMetaByNameResponse is response type for the GetBucketMetaByName RPC method
message GetBucketMetaByNameResponse {
// bucket defines the information of a bucket
Bucket bucket = 1;
// stream_record defines stream payment record of a stream account
Expand Down Expand Up @@ -239,6 +239,6 @@ service MetadataService {
rpc GetPaymentByBucketID(GetPaymentByBucketIDRequest) returns (GetPaymentByBucketIDResponse) {};
// VerifyPermission Verify the input items permission.
rpc VerifyPermission(bnbchain.greenfield.storage.QueryVerifyPermissionRequest) returns (bnbchain.greenfield.storage.QueryVerifyPermissionResponse) {};
// GetBucketWithPayment get bucket info with payment info by a bucket name
rpc GetBucketWithPayment(GetBucketWithPaymentRequest) returns (GetBucketWithPaymentResponse) {};
// GetBucketMetaByName get bucket info with related info such as payment by name
rpc GetBucketMetaByName(GetBucketMetaByNameRequest) returns (GetBucketMetaByNameResponse) {};
}
26 changes: 13 additions & 13 deletions service/gateway/metadata_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ func (gateway *Gateway) listObjectsByBucketNameHandler(w http.ResponseWriter, r
w.Write(b.Bytes())
}

// getObjectMetaByNameHandler handle get object info by name request
func (gateway *Gateway) getObjectByNameHandler(w http.ResponseWriter, r *http.Request) {
// getObjectMetaByNameHandler handle get object metadata by name request
func (gateway *Gateway) getObjectMetaByNameHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
b bytes.Buffer
Expand All @@ -140,9 +140,9 @@ func (gateway *Gateway) getObjectByNameHandler(w http.ResponseWriter, r *http.Re
_ = errDescription.errorJSONResponse(w, reqContext)
}
if errDescription != nil && errDescription.statusCode != http.StatusOK {
log.Errorf("action(%v) statusCode(%v) %v", getObjectByNameRouterName, errDescription.statusCode, reqContext.generateRequestDetail())
log.Errorf("action(%v) statusCode(%v) %v", getObjectMetaByNameRouterName, errDescription.statusCode, reqContext.generateRequestDetail())
} else {
log.Infof("action(%v) statusCode(200) %v", getObjectByNameRouterName, reqContext.generateRequestDetail())
log.Infof("action(%v) statusCode(200) %v", getObjectMetaByNameRouterName, reqContext.generateRequestDetail())
}
}()

Expand Down Expand Up @@ -189,8 +189,8 @@ func (gateway *Gateway) getObjectByNameHandler(w http.ResponseWriter, r *http.Re
w.Write(b.Bytes())
}

// getBucketWithPaymentHandler handle get bucket info with payment request
func (gateway *Gateway) getBucketWithPaymentHandler(w http.ResponseWriter, r *http.Request) {
// getBucketMetaByNameHandler handle get bucket metadata by name request
func (gateway *Gateway) getBucketMetaByNameHandler(w http.ResponseWriter, r *http.Request) {
var (
err error
b bytes.Buffer
Expand All @@ -204,14 +204,14 @@ func (gateway *Gateway) getBucketWithPaymentHandler(w http.ResponseWriter, r *ht
_ = errDescription.errorJSONResponse(w, reqContext)
}
if errDescription != nil && errDescription.statusCode != http.StatusOK {
log.Errorf("action(%v) statusCode(%v) %v", getBucketAndPaymentRouterName, errDescription.statusCode, reqContext.generateRequestDetail())
log.Errorf("action(%v) statusCode(%v) %v", getBucketMetaByNameRouterName, errDescription.statusCode, reqContext.generateRequestDetail())
} else {
log.Infof("action(%v) statusCode(200) %v", getBucketAndPaymentRouterName, reqContext.generateRequestDetail())
log.Infof("action(%v) statusCode(200) %v", getBucketMetaByNameRouterName, reqContext.generateRequestDetail())
}
}()

if gateway.metadata == nil {
log.Error("failed to get bucket and payment info due to not config metadata")
log.Error("failed to get bucket info due to not config metadata")
errDescription = NotExistComponentError
return
}
Expand All @@ -222,22 +222,22 @@ func (gateway *Gateway) getBucketWithPaymentHandler(w http.ResponseWriter, r *ht
return
}

req := &metatypes.GetBucketWithPaymentRequest{
req := &metatypes.GetBucketMetaByNameRequest{
BucketName: reqContext.bucketName,
IsFullList: true,
}

ctx := log.Context(context.Background(), req)
resp, err := gateway.metadata.GetBucketWithPayment(ctx, req)
resp, err := gateway.metadata.GetBucketMetaByName(ctx, req)
if err != nil {
log.Errorf("failed to get bucket with payment", "error", err)
log.Errorf("failed to get bucket metadata", "error", err)
errDescription = makeErrorDescription(err)
return
}

m := jsonpb.Marshaler{EmitDefaults: true, OrigName: true, EnumsAsInts: true}
if err = m.Marshal(&b, resp); err != nil {
log.Errorf("failed to get bucket with payment", "error", err)
log.Errorf("failed to get bucket metadata", "error", err)
errDescription = makeErrorDescription(err)
return
}
Expand Down
34 changes: 17 additions & 17 deletions service/gateway/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const (
queryUploadProgressRouterName = "queryUploadProgress"
downloadObjectByUniversalEndpointName = "DownloadObjectByUniversalEndpoint"
viewObjectByUniversalEndpointName = "ViewObjectByUniversalEndpoint"
getObjectByNameRouterName = "getObjectByName"
getBucketAndPaymentRouterName = "getBucketAndPayment"
getObjectMetaByNameRouterName = "getObjectMetaByName"
getBucketMetaByNameRouterName = "getBucketMetaByName"
)

const (
Expand Down Expand Up @@ -62,15 +62,16 @@ func (g *Gateway) registerHandler(r *mux.Router) {
Queries(model.UploadProgressQuery, "").
HandlerFunc(g.queryUploadProgressHandler)
hostBucketRouter.NewRoute().
Name(getBucketAndPaymentRouterName).
Name(getBucketMetaByNameRouterName).
Methods(http.MethodGet).
Path("/bucket/payment").
HandlerFunc(g.getBucketWithPaymentHandler)
Queries(model.GetBucketMetaByNameQuery, "").
HandlerFunc(g.getBucketMetaByNameHandler)
hostBucketRouter.NewRoute().
Name(getObjectByNameRouterName).
Name(getObjectMetaByNameRouterName).
Methods(http.MethodGet).
Path("/{object:.+}/metadata").
HandlerFunc(g.getObjectByNameHandler)
Path("/{object:.+}").
Queries(model.GetObjectMetaByNameQuery, "").
HandlerFunc(g.getObjectMetaByNameHandler)
hostBucketRouter.NewRoute().
Name(getObjectRouterName).
Methods(http.MethodGet).
Expand Down Expand Up @@ -98,11 +99,9 @@ func (g *Gateway) registerHandler(r *mux.Router) {
hostBucketRouter.NotFoundHandler = http.HandlerFunc(g.notFoundHandler)

// bucket list router, path style
bucketListRouter := r.Host(g.config.Domain).Subrouter()
bucketListRouter.NewRoute().
r.Path("/").
Name(getUserBucketsRouterName).
Methods(http.MethodGet).
Path("/").
HandlerFunc(g.getUserBucketsHandler)

// admin router, path style
Expand Down Expand Up @@ -157,15 +156,16 @@ func (g *Gateway) registerHandler(r *mux.Router) {
Queries(model.UploadProgressQuery, "").
HandlerFunc(g.queryUploadProgressHandler)
pathBucketRouter.NewRoute().
Name(getBucketAndPaymentRouterName).
Name(getBucketMetaByNameRouterName).
Methods(http.MethodGet).
Path("/bucket/payment").
HandlerFunc(g.getBucketWithPaymentHandler)
Queries(model.GetBucketMetaByNameQuery, "").
HandlerFunc(g.getBucketMetaByNameHandler)
pathBucketRouter.NewRoute().
Name(getObjectByNameRouterName).
Name(getObjectMetaByNameRouterName).
Methods(http.MethodGet).
Path("/{object:.+}/metadata").
HandlerFunc(g.getObjectByNameHandler)
Path("/{object:.+}").
Queries(model.GetObjectMetaByNameQuery, "").
HandlerFunc(g.getObjectMetaByNameHandler)
pathBucketRouter.NewRoute().
Name(getObjectRouterName).
Methods(http.MethodGet).
Expand Down
6 changes: 3 additions & 3 deletions service/metadata/client/metadata_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ func (client *MetadataClient) VerifyPermission(ctx context.Context, in *storaget
return resp, nil
}

// GetBucketWithPayment get bucket info with its related payment info
func (client *MetadataClient) GetBucketWithPayment(ctx context.Context, in *metatypes.GetBucketWithPaymentRequest, opts ...grpc.CallOption) (*metatypes.GetBucketWithPaymentResponse, error) {
resp, err := client.metadata.GetBucketWithPayment(ctx, in, opts...)
// GetBucketMetaByName get bucket info along with its related info such as payment
func (client *MetadataClient) GetBucketMetaByName(ctx context.Context, in *metatypes.GetBucketMetaByNameRequest, opts ...grpc.CallOption) (*metatypes.GetBucketMetaByNameResponse, error) {
resp, err := client.metadata.GetBucketMetaByName(ctx, in, opts...)
ctx = log.Context(ctx, resp)
if err != nil {
log.CtxErrorw(ctx, "failed to send get bucket with payment rpc by bucket name", "error", err)
Expand Down
8 changes: 5 additions & 3 deletions service/metadata/service/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (metadata *Metadata) ListExpiredBucketsBySp(ctx context.Context, req *metat
return resp, nil
}

func (metadata *Metadata) GetBucketWithPayment(ctx context.Context, req *metatypes.GetBucketWithPaymentRequest) (resp *metatypes.GetBucketWithPaymentResponse, err error) {
func (metadata *Metadata) GetBucketMetaByName(ctx context.Context, req *metatypes.GetBucketMetaByNameRequest) (resp *metatypes.GetBucketMetaByNameResponse, err error) {
var (
bucket *model.Bucket
bucketRes *metatypes.Bucket
Expand All @@ -223,11 +223,13 @@ func (metadata *Metadata) GetBucketWithPayment(ctx context.Context, req *metatyp
)

ctx = log.Context(ctx, req)
bucket, streamRecord, err = metadata.bsDB.GetBucketWithPayment(req.GetBucketName(), req.GetIsFullList())
bucketFullMeta, err := metadata.bsDB.GetBucketMetaByName(req.GetBucketName(), req.GetIsFullList())
if err != nil {
log.CtxErrorw(ctx, "failed to get bucket info with payment info", "error", err)
return
}
bucket = &bucketFullMeta.Bucket
streamRecord = &bucketFullMeta.StreamRecord

if bucket != nil {
bucketRes = &metatypes.Bucket{
Expand Down Expand Up @@ -278,7 +280,7 @@ func (metadata *Metadata) GetBucketWithPayment(ctx context.Context, req *metatyp
}
}

resp = &metatypes.GetBucketWithPaymentResponse{Bucket: bucketRes, StreamRecord: streamRecordRes}
resp = &metatypes.GetBucketMetaByNameResponse{Bucket: bucketRes, StreamRecord: streamRecordRes}
log.CtxInfow(ctx, "succeed to get bucket info with payment info")
return resp, nil
}
12 changes: 6 additions & 6 deletions store/bsdb/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,26 @@ func (b *BsDBImpl) ListExpiredBucketsBySp(createAt int64, primarySpAddress strin
return buckets, err
}

func (b *BsDBImpl) GetBucketWithPayment(bucketName string, isFullList bool) (*Bucket, *StreamRecord, error) {
func (b *BsDBImpl) GetBucketMetaByName(bucketName string, isFullList bool) (*BucketFullMeta, error) {
var (
bucketWithPayment *BucketWithPayment
err error
bucketFullMeta *BucketFullMeta
err error
)

if isFullList {
err = b.db.Table((&Bucket{}).TableName()).
Select("*").
Joins("left join stream_records on buckets.payment_address = stream_records.account").
Where("buckets.bucket_name = ?", bucketName).
Take(&bucketWithPayment).Error
Take(&bucketFullMeta).Error
} else {
err = b.db.Table((&Bucket{}).TableName()).
Select("*").
Joins("left join stream_records on buckets.payment_address = stream_records.account").
Where("buckets.bucket_name = ? and "+
"buckets.visibility='VISIBILITY_TYPE_PUBLIC_READ'", bucketName).
Take(&bucketWithPayment).Error
Take(&bucketFullMeta).Error
}

return &bucketWithPayment.Bucket, &bucketWithPayment.StreamRecord, err
return bucketFullMeta, err
}
4 changes: 2 additions & 2 deletions store/bsdb/bucket_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func (b *Bucket) TableName() string {
return BucketTableName
}

// BucketWithPayment is the structure for user bucket with its related payment info
type BucketWithPayment struct {
// BucketFullMeta is the structure for user bucket with its related info
type BucketFullMeta struct {
Bucket
StreamRecord
}
4 changes: 2 additions & 2 deletions store/bsdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type Metadata interface {
GetObjectByName(objectName string, bucketName string, isFullList bool) (*Object, error)
// GetSwitchDBSignal check if there is a signal to switch the database
GetSwitchDBSignal() (*MasterDB, error)
// GetBucketWithPayment get bucket info with its related payment info
GetBucketWithPayment(bucketName string, isFullList bool) (*Bucket, *StreamRecord, error)
// GetBucketMetaByName get bucket info with its related info
GetBucketMetaByName(bucketName string, isFullList bool) (*BucketFullMeta, error)
}

// BSDB contains all the methods required by block syncer database
Expand Down

0 comments on commit 2743b7e

Please sign in to comment.