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 request increment ref bug #1121

Merged
merged 2 commits into from
Nov 22, 2019
Merged

Fix request increment ref bug #1121

merged 2 commits into from
Nov 22, 2019

Conversation

jarifibrahim
Copy link
Contributor

@jarifibrahim jarifibrahim commented Nov 17, 2019

The sendToWriteCh() method would increment the ref of a request by 2
(one for the write and another for the publisher). The ref value was
always decremented by the doWrite() function but the publisher would
decrement it only if there were any subscribers.

This would mean that we ALWAYS create new request object in the requestPool but never put them back into the pool.
https://github.com/dgraph-io/badger/blob/eef7c122607e5c2cc60e14eb20c883efa3e6855b/value.go#L1204-L1211

With this commit, we increment the request ref for the publisher only if
there are any actual subscribers.

The adding print statements around the requestPool.Get() and requestPool.Put() functions shows the request object were never being added to the pool.


This change is Reviewable

The sendToWriteCh() method would increment the ref of a request by 2
(one for the write and another for the publisher). The ref value was
always decremented by the doWrite() function but the publisher would
decrement it only if there were any subscribers.

With this commit, we increment the request ref for the publisher only if
there are any actual subscribers.
@coveralls
Copy link

coveralls commented Nov 17, 2019

Coverage Status

Coverage increased (+0.4%) to 77.908% when pulling 098d2bb on ibrahim/requestPool-fix into ffb3450 on master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.07%) to 77.45% when pulling 9af7e00 on ibrahim/requestPool-fix into eef7c12 on master.

Copy link
Contributor

@martinmr martinmr left a comment

Choose a reason for hiding this comment

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

Looks good to me but get final approval from Manish.

Reviewed 3 of 3 files at r1.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @ashish-goswami and @manishrjain)

Copy link
Contributor

@ashish-goswami ashish-goswami left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @jarifibrahim and @manishrjain)


db.go, line 679 at r1 (raw file):

	db.elog.Printf("Sending updates to subscribers")
	db.pub.sendUpdates(reqs)

Can we add a comment saying that we increase reference of the requests inside sendUpdates? Since we are increasing/decreasing reference twice, we have to make sure reference is 0 only at end. Hence order of statements matter here.

Copy link
Contributor

@poonai poonai left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @jarifibrahim and @manishrjain)

Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

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

The increment should happen upfront. This PR would create a race cond.

:lgtm_cancel:

Reviewed 3 of 3 files at r1.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @jarifibrahim)


db.go, line 733 at r1 (raw file):

	req.Wg.Add(1)
	req.IncrRef()     // for db write
	db.writeCh <- req // Handled in doWrites.

What if db write happens before publisher has a chance to pick it up. You should still increment it here, but then once publisher gets it, it can decr ref it.


publisher.go, line 155 at r1 (raw file):

func (p *publisher) sendUpdates(reqs requests) {
	if p.noOfSubscribers() != 0 {
		reqs.IncrRef()

This is too late. This would create a race cond between db.write.

Copy link
Contributor Author

@jarifibrahim jarifibrahim left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @jarifibrahim and @manishrjain)


db.go, line 733 at r1 (raw file):

Previously, manishrjain (Manish R Jain) wrote…

What if db write happens before publisher has a chance to pick it up. You should still increment it here, but then once publisher gets it, it can decr ref it.

That will not happen because we sendupdates to subscribers before we mark the request as done. See this line
https://github.com/dgraph-io/badger/blob/5f94f68074b4a78483afd63c3f8a67141c9aeab8/db.go#L672-L681

I can make changes to increment it in sendToWriteCh and decrement it in sendUpdates

Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

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

:lgtm: Got the explanation.

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @jarifibrahim and @manishrjain)

@jarifibrahim jarifibrahim merged commit ad770ca into master Nov 22, 2019
@jarifibrahim jarifibrahim deleted the ibrahim/requestPool-fix branch November 22, 2019 11:22
jarifibrahim pushed a commit that referenced this pull request Mar 12, 2020
The sendToWriteCh() method would increment the ref of a request by 2
(one for the write and another for the publisher). The ref value was
always decremented by the doWrite() function but the publisher would
decrement it only if there were any subscribers.

With this commit, we increment the request ref for the publisher only if
there are any actual subscribers.

(cherry picked from commit ad770ca)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

6 participants