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

Try to make tag.Map threadsafe. #89

Merged
merged 1 commit into from
Aug 23, 2019

Conversation

krnowak
Copy link
Member

@krnowak krnowak commented Aug 12, 2019

This adds a lockedMap type which implements tag.Map interface. The
implementation contains tagMap instance and a mutex, so all the
operations are forwarded to the tagMap under the locked mutex.

An additional care was needed for the functions returning contents of
the map, because core.Value contains a byte slice, which has pointer
like semantics. So to avoid accidental changes, we copy the value if
it is of BYTES type. This likely should be handled by the core.Value
itself, e.g. through some Copy function.

The downside of locking here is that the users of Foreach function
need to be careful to not call into the same map, otherwise deadlock
will happen.

While writing this code I think I have got an understanding of the
issue at hand - the implementation of the tag.Map should basically be
immutable (so the modification of the map actually produces a new map,
instead of doing in-place updates, thus making the map threadsafe),
but that is not easy (or even possible) to enforce. So maybe it's
indeed better to avoid providing the ability of having a different,
vendor-specific implementation of tag.Map and have a good-by-default
implemetation as a part of API.

Still, to preserve the immutability of the map, the core.Value structs
need to be deep-copied.

Fixes #59

@krnowak
Copy link
Member Author

krnowak commented Aug 13, 2019

Updated to make tag.Map a concrete type. Shuffled the code a bit to avoid dumping everything into a single file.

@@ -82,68 +109,24 @@ func (m tagMap) Foreach(f func(kv core.KeyValue) bool) {
}
}

func (m tagMap) apply(mutator Mutator) {
if m == nil {
Copy link
Member Author

Choose a reason for hiding this comment

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

This never happens, so I dropped the check.

@krnowak krnowak mentioned this pull request Aug 20, 2019
@rghetia
Copy link
Contributor

rghetia commented Aug 23, 2019

@krnowak can you please resolve the conflict?

This is to make tag.Map an immutable type, so it is safe to use
concurrently. The safety is not yet fully achieved because of the
functions returning contents of the map (Value and Foreach). The
functions give callers an access to core.Value objects, which contain
a byte slice, which has pointer like semantics. So to avoid accidental
changes, we will need to copy the value if it is of BYTES type.

Fixes open-telemetry#59
@krnowak
Copy link
Member Author

krnowak commented Aug 23, 2019

Updated. And filed #105.

@rghetia rghetia merged commit 670b036 into open-telemetry:master Aug 23, 2019
@krnowak krnowak deleted the krnowak/locked-tag-map branch August 27, 2019 07:42
@pellared pellared added this to the untracked milestone Nov 8, 2024
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.

Eliminate tag.Map API
4 participants