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

Fix data race while accessing connection in partitionProducer #701

Merged
merged 3 commits into from
Jan 12, 2022

Conversation

wolfstudy
Copy link
Member

@wolfstudy wolfstudy commented Jan 11, 2022

Signed-off-by: xiaolongran rxl@apache.org

Motivation

In #700, we use a separate go rutine to handle the logic of reconnect, so here you may encounter the same data race problem as #535

Modifications

Now, the conn field is read and written atomically; avoiding race conditions.

Signed-off-by: xiaolongran <xiaolongran@tencent.com>
@wolfstudy wolfstudy added this to the v0.8.0 milestone Jan 11, 2022
@wolfstudy wolfstudy self-assigned this Jan 11, 2022
@wolfstudy
Copy link
Member Author

cc @dferstay PTAL thanks

Signed-off-by: xiaolongran <xiaolongran@tencent.com>
Signed-off-by: xiaolongran <xiaolongran@tencent.com>
@wolfstudy wolfstudy requested a review from zymap January 11, 2022 06:39
@@ -800,7 +801,7 @@ func (p *partitionProducer) ReceivedSendReceipt(response *pb.CommandSendReceipt)
// the state discrepancy.
p.log.Warnf("Received ack for %v on sequenceId %v - expected: %v, closing connection", response.GetMessageId(),
response.GetSequenceId(), pi.sequenceID)
p.cnx.Close()
p._getConn().Close()
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe clean up the cached connection when the cnx is closed? otherwise _getConn() will return a closed connection.

Copy link
Member Author

Choose a reason for hiding this comment

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

Looks like #703 is working on this

Copy link
Member Author

Choose a reason for hiding this comment

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

@freeznet Looking at the implementation of the current Close method, when triggering the logic of Close, the state of the current connection will first be set to connectionClosed. When GetConnection() is executed, if the state of the connection is detected to be closed, the connection will be removed from the cache.

func (c *connection) Close() {
	c.closeOnce.Do(func() {
		c.Lock()
		cnx := c.cnx
		c.Unlock()
		c.changeState(connectionClosed)
if conn.closed() {
			delete(p.connections, key)
			p.log.Infof("Removed connection from pool key=%s logical_addr=%+v physical_addr=%+v",
				key, conn.logicalAddr, conn.physicalAddr)
			conn = nil // set to nil so we create a new one
		}

Copy link
Contributor

@leizhiyuan leizhiyuan left a comment

Choose a reason for hiding this comment

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

LGTM

@wolfstudy wolfstudy merged commit 2bcf7c7 into apache:master Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants