Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Concurrent ensure add #1218

Merged
merged 2 commits into from
Sep 28, 2017
Merged

Conversation

darkowlzz
Copy link
Collaborator

@darkowlzz darkowlzz commented Sep 24, 2017

What does this do / why do we need it?

This change makes ensure -add concurrently add the dependencies and
uses golang.org/x/sync/syncmap for a concurrent map to replace
addInstructions map
.

Also, logs a message when the sources are being fetched.

$ dep ensure -v -add github.com/darkowlzz/openurl
Fetching sources...
(1/1) github.com/darkowlzz/openurl
Root project is "github.com/darkowlzz/depfoo"

Sources are enumerated only in verbose mode.

Related to TODOs:
TODO(sdboyer) return all errors, not just the first one we encounter
TODO(sdboyer) do these concurrently?

What should your reviewer look out for in this PR?

Concurrency code and the usage of syncmap.

Do you need help or clarification on anything?

No.

Which issue(s) does this PR fix?

fixes #1202

This change makes `ensure -add` concurrently add the dependencies and
uses golang.org/x/sync/syncmap for a concurrent map to replace
addInstructions map.

Also, logs a message when the sources are being fetched.
return errors.Errorf("can only specify rules once per project being added; rules were given at least twice for %s", pc.Ident.ProjectRoot)
someConstraint := !gps.IsAny(pc.Constraint) || pc.Ident.Source != ""

instrInterface, has := addInstructions.LoadOrStore(pc.Ident.ProjectRoot, addInstruction{})
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is racy. In between storing the zero value and executing the else block to set the fields, another goroutine could load and read the (zero) fields in the has block.

I also wonder the same thing about two goroutines loading, and seeing no constraint set (so no error detected), then racing to set the constraint field.

Copy link
Collaborator Author

@darkowlzz darkowlzz Sep 25, 2017

Choose a reason for hiding this comment

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

ah! right. So creating a new struct with sync.Mutex and maintaining the lock through out this would be better right?

Also, I see the same syncmap is available in sync/map. Same function signatures. Is it like the golang.org packages are eventually moved into the the stdlib?

EDIT: ah ! I see syncmap was a prototype. It's in stdlib officially in go1.9. Will remove it.

Package syncmap provides a concurrent map implementation. It is a prototype for a proposed addition to the sync package in the standard library. (https://golang.org/issue/18177)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

And if we use mutex, there's no need of syncmap.

if has {
// Multiple packages from the same project were specified as
// arguments; make sure they agree on declared constraints.
// TODO(sdboyer) until we have a general method for checking constraint equality, only allow one to declare
Copy link
Collaborator

Choose a reason for hiding this comment

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

We have Constraint.identical now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Let's fix this in another PR.


addInstructions[pc.Ident.ProjectRoot] = instr
addInstructions.Store(pc.Ident.ProjectRoot, instr)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Isn't instr still in the syncmap the whole time?

Copy link
Collaborator

@jmank88 jmank88 Sep 24, 2017

Choose a reason for hiding this comment

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

If not, then my race comment is probably off too (or half wrong at least).

@@ -2,6 +2,8 @@

IMPROVEMENTS:

* `dep ensure -add` now concurrently fetches the source and adds the projects.
(#1218)
Copy link
Collaborator

Choose a reason for hiding this comment

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

❤️

@jmank88 jmank88 merged commit 82b3908 into golang:master Sep 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dep ensure -add waits for a long time without any feedback
4 participants