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

hrpc, region: Allow setting priority on Scan and Get requests #267

Merged
merged 1 commit into from
Aug 14, 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
1 change: 1 addition & 0 deletions hrpc/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type hasQueryOptions interface {
setResultOffset(offset uint32)
setCacheBlocks(cacheBlocks bool)
setConsistency(consistency ConsistencyType)
setPriority(priority uint32)
}

// RPCResult is struct that will contain both the resulting message from an RPC
Expand Down
20 changes: 20 additions & 0 deletions hrpc/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type baseQuery struct {
maxVersions uint32
storeLimit uint32
storeOffset uint32
priority uint32
cacheBlocks bool
consistency ConsistencyType
}
Expand Down Expand Up @@ -97,6 +98,15 @@ func (bq *baseQuery) setCacheBlocks(cacheBlocks bool) {
func (bq *baseQuery) setConsistency(consistency ConsistencyType) {
bq.consistency = consistency
}
func (bq *baseQuery) setPriority(priority uint32) {
bq.priority = priority
}
func (bq *baseQuery) Priority() *uint32 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning by point here is weird. I understand that when we marshal to proto it wants pointers-to-int and maybe doing it this way avoids an allocation (because otherwise in marshalProto() you'd have to create an int and take its address thereby causing a heap allocation of a new integer), but from an API perspective it's odd. Did you do it this way to avoid an allocation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, to avoid an allocation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And now this has become slightly awkward in #268, which fixes something I missed here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I didn't notice that. I think returning just uint32 make more sense. Returning a pointer or a uint32 is the same in the end

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's same except for the added allocation. The extra allocation is not a big deal, so I'll just clean this up in #268.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated #268

if bq.priority == 0 {
return nil
}
return &bq.priority
}

// Families option adds families constraint to a Scan or Get request.
func Families(f map[string][]string) func(Call) error {
Expand Down Expand Up @@ -218,3 +228,13 @@ func Consistency(consistency ConsistencyType) func(Call) error {
return errors.New("'Consistency' option can only be used with Get or Scan requests")
}
}

func Priority(priority uint32) func(Call) error {
return func(hc Call) error {
if c, ok := hc.(hasQueryOptions); ok {
c.setPriority(priority)
return nil
}
return errors.New("'Priority' option can only be used with Get or Scan requests")
}
}
46 changes: 46 additions & 0 deletions hrpc/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,49 @@ func TestCacheBlocks(t *testing.T) {
t.Error(err)
}
}

func TestPriority(t *testing.T) {
get, err := NewGet(nil, nil, nil)
if err != nil {
t.Fatal(err)
}
if got := get.Priority(); got != nil {
t.Errorf("expected nil, got %v", got)
}
get, err = NewGet(nil, nil, nil, Priority(0))
if err != nil {
t.Fatal(err)
}
if got := get.Priority(); got != nil {
t.Errorf("expected nil, got %v", got)
}
get, err = NewGet(nil, nil, nil, Priority(5))
if err != nil {
t.Fatal(err)
}
if got := get.Priority(); *got != 5 {
t.Errorf("expected priority 5, got %v", got)
}

scan, err := NewScan(nil, nil)
if err != nil {
t.Fatal(err)
}
if got := scan.Priority(); got != nil {
t.Errorf("expected nil, got %v", got)
}
scan, err = NewScan(nil, nil, Priority(0))
if err != nil {
t.Fatal(err)
}
if got := scan.Priority(); got != nil {
t.Errorf("expected nil, got %v", got)
}
scan, err = NewScan(nil, nil, Priority(5))
if err != nil {
t.Fatal(err)
}
if got := scan.Priority(); *got != 5 {
t.Errorf("expected priority 5, got %v", got)
}
}
5 changes: 4 additions & 1 deletion region/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,13 @@ var pbTrue = proto.Bool(true)
func marshalProto(rpc hrpc.Call, callID uint32, request proto.Message,
cellblocksLen uint32) ([]byte, error) {
header := getHeader()
defer returnHeader(header)
header.MethodName = proto.String(rpc.Name())
header.RequestParam = pbTrue
header.CallId = &callID
defer returnHeader(header)
if p, ok := rpc.(interface{ Priority() *uint32 }); ok {
header.Priority = p.Priority()
}

if cellblocksLen > 0 {
header.CellBlockMeta = &pb.CellBlockMeta{
Expand Down
Loading