Skip to content

Commit

Permalink
hrpc: add an option to skip retry
Browse files Browse the repository at this point in the history
This let the caller tell GoHBase to no retry calls that failed.
Otherwise, GoHBase will retry forever until cancellation of the context.
But sometime, it is useful to to be able to say "try only once", without
just relying on having a small context timeout.

One of the place where this is useful is when we try to close a scanner
that we prematurely stopped, as this is just the client trying to be a
good citizen. But most likely, if the request failed with a retryable
error (region closed or moved, call queue too big, etc), by the time we
manage to send the request the scanner lease would have been already
expired on HBase side.
  • Loading branch information
dethi committed Jul 5, 2024
1 parent a25cbc0 commit 032b058
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 18 deletions.
25 changes: 25 additions & 0 deletions hrpc/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Call interface {
ResultChan() chan RPCResult
Description() string // Used for tracing and metrics
Context() context.Context
SkipRetry() bool
}

type withOptions interface {
Expand Down Expand Up @@ -90,6 +91,20 @@ func SkipBatch() func(Call) error {
}
}

func SkipRetry() func(Call) error {
return func(c Call) error {
if b, ok := c.(canSetSkipRetry); ok {
b.setSkipRetry(true)
return nil
}
return errors.New("'SkipRetry' is not implemented for this call")
}
}

type canSetSkipRetry interface {
setSkipRetry(v bool)
}

// hasQueryOptions is interface that needs to be implemented by calls
// that allow to provide Families and Filters options.
type hasQueryOptions interface {
Expand Down Expand Up @@ -119,12 +134,22 @@ type base struct {

region RegionInfo
resultch chan RPCResult

skipRetry bool
}

func (b *base) Context() context.Context {
return b.ctx
}

func (b *base) SkipRetry() bool {
return b.skipRetry
}

func (b *base) setSkipRetry() {
b.skipRetry = true
}

func (b *base) Region() RegionInfo {
return b.region
}
Expand Down
5 changes: 5 additions & 0 deletions region/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ func (m *multi) Context() context.Context {
return context.Background()
}

// SkipRetry always returns false for Multi.
func (m *multi) SkipRetry() bool {
return false
}

// String returns a description of this call
func (m *multi) String() string {
return "MULTI"
Expand Down
20 changes: 11 additions & 9 deletions rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,18 @@ func (c *client) SendRPC(rpc hrpc.Call) (msg proto.Message, err error) {
return nil, err
}
msg, err = c.sendRPCToRegionClient(ctx, rpc, rc)
switch err.(type) {
case region.RetryableError:
sp.AddEvent("retrySleep")
backoff, err = sleepAndIncreaseBackoff(ctx, backoff)
if err != nil {
return msg, err
if !rpc.SkipRetry() {
switch err.(type) {
case region.RetryableError:
sp.AddEvent("retrySleep")
backoff, err = sleepAndIncreaseBackoff(ctx, backoff)
if err != nil {
return msg, err
}
continue // retry
case region.ServerError, region.NotServingRegionError:
continue // retry
}
continue // retry
case region.ServerError, region.NotServingRegionError:
continue // retry
}
return msg, err
}
Expand Down
1 change: 1 addition & 0 deletions rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestSendRPCSanity(t *testing.T) {
expMsg := &pb.ScanResponse{}
result <- hrpc.RPCResult{Msg: expMsg}
mockCall.EXPECT().ResultChan().Return(result).Times(1)
mockCall.EXPECT().SkipRetry().Return(false)
msg, err := c.SendRPC(mockCall)
if err != nil {
t.Fatal(err)
Expand Down
16 changes: 7 additions & 9 deletions scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,22 +337,20 @@ func (s *scanner) closeRegionScanner() {
return
}
if !s.rpc.IsClosing() {
// Not closed at server side
// if we are closing in the middle of scanning a region,
// send a close scanner request
// TODO: add a deadline
// Not closed at server side if we are closing in the middle of scanning
// a region, so send a close scanner request. This is a fire-and-forget
// call, as if we fail the scanner lease will expire and be closed
// automatically by HBase.
rpc, err := hrpc.NewScanRange(context.Background(),
s.rpc.Table(), s.startRow, nil,
hrpc.ScannerID(s.curRegionScannerID),
hrpc.CloseScanner(),
hrpc.NumberOfRows(0))
hrpc.NumberOfRows(0),
hrpc.SkipRetry(),
)
if err != nil {
panic(fmt.Sprintf("should not happen: %s", err))
}

// If the request fails, the scanner lease will be expired
// and it will be closed automatically by hbase.
// No need to bother clients about that.
go s.SendRPC(rpc)
}
s.curRegionScannerID = noScannerID
Expand Down
14 changes: 14 additions & 0 deletions test/mock/call.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 032b058

Please sign in to comment.