Tidb: Method to get connection is not thread safe

Created on 11 Jan 2021  路  6Comments  路  Source: pingcap/tidb

From github.com/pingcap/tidb/store/tikv/client.go

func (a *connArray) Get() *grpc.ClientConn {
    next := atomic.AddUint32(&a.index, 1) % uint32(len(a.v))
    return a.v[next]
}

It is possible that multiple concurrent request call Get method.

typquestion

All 6 comments

connArray.v is initialized after newConnArray function called and this array never changed until Close function called. So return a.v[next] is read-only during lifecycle. I think this is thread-safe.

Let's say length of connArray.v is 2, if there are three concurrent Get call, the Get method may return the same ClientConn.

After dive more deeply I think connArray is used as a load balancer. So Get return same ClientConn is OK.

Thank you, Could you file an PR and add some comments to make it more clear?

sure

@ngaut I think connArray should rename to connPool. That's name is more meaningful.

Was this page helpful?
0 / 5 - 0 ratings