Reduce lock contention when creating shard group

pull/6587/head
Jason Wilder 2016-05-09 16:10:38 -06:00
parent 4f39cb2f97
commit adddb0e6b8
1 changed files with 9 additions and 1 deletions

View File

@ -694,11 +694,19 @@ func (c *Client) DropShard(id uint64) error {
// CreateShardGroup creates a shard group on a database and policy for a given timestamp.
func (c *Client) CreateShardGroup(database, policy string, timestamp time.Time) (*ShardGroupInfo, error) {
// Check under a read-lock
c.mu.RLock()
if sg, _ := c.cacheData.ShardGroupByTimestamp(database, policy, timestamp); sg != nil {
c.mu.RUnlock()
return sg, nil
}
c.mu.RUnlock()
c.mu.Lock()
defer c.mu.Unlock()
// Check again under the write lock
data := c.cacheData.Clone()
if sg, _ := data.ShardGroupByTimestamp(database, policy, timestamp); sg != nil {
return sg, nil
}