Skip to content

Commit

Permalink
Add semaphore to control the number of goroutine looking up meta table
Browse files Browse the repository at this point in the history
  • Loading branch information
YutSean committed May 12, 2021
1 parent 3489911 commit 5c2545c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
17 changes: 17 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"sync"
"time"

"golang.org/x/sync/semaphore"

log "github.com/sirupsen/logrus"
"github.com/tsuna/gohbase/compression"
"github.com/tsuna/gohbase/hrpc"
Expand All @@ -27,6 +29,7 @@ const (
defaultZkRoot = "/hbase"
defaultZkTimeout = 30 * time.Second
defaultEffectiveUser = "root"
defaultMetaLimit = 50
)

// Client a regular HBase client
Expand Down Expand Up @@ -90,6 +93,12 @@ type client struct {
// regionReadTimeout is the maximum amount of time to wait for regionserver reply
regionReadTimeout time.Duration

// The max number of the goroutines that look up meta table simultaneously.
metaLimit int64

// The corresponding semaphore of metaLimit
sema *semaphore.Weighted

done chan struct{}
closeOnce sync.Once

Expand Down Expand Up @@ -126,6 +135,7 @@ func newClient(zkquorum string, options ...Option) *client {
zkRoot: defaultZkRoot,
zkTimeout: defaultZkTimeout,
effectiveUser: defaultEffectiveUser,
metaLimit: defaultMetaLimit,
regionLookupTimeout: region.DefaultLookupTimeout,
regionReadTimeout: region.DefaultReadTimeout,
done: make(chan struct{}),
Expand All @@ -139,6 +149,13 @@ func newClient(zkquorum string, options ...Option) *client {
//since the zkTimeout could be changed as an option
c.zkClient = zk.NewClient(zkquorum, c.zkTimeout)

if c.metaLimit > 0 {
c.sema = semaphore.NewWeighted(c.metaLimit)
} else {
c.sema = semaphore.NewWeighted(defaultMetaLimit)
c.metaLimit = defaultMetaLimit
}

return c
}

Expand Down
2 changes: 2 additions & 0 deletions rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,10 @@ func (c *client) metaLookup(ctx context.Context,
return nil, "", err
}

c.sema.Acquire(context.Background(), 1)
scanner := c.Scan(rpc)
resp, err := scanner.Next()
c.sema.Release(1)
if err == io.EOF {
return nil, "", TableNotFound
}
Expand Down

0 comments on commit 5c2545c

Please sign in to comment.