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

[Partition Consumer] Provide lock-free access to compression providers #689

Merged

Conversation

dferstay
Copy link
Contributor

Motivation

The compression providers map in the partition consumer is a textbook case
for using go's lock-free sync.Map: the set of map entries is stable and
access is read-only.

On machines with 4 cores or greater, read contention on the sync.RWMutex
outweighs the cost of using a sync.Map.

Below is an old article on the subject, but it still holds true today:
https://medium.com/@deckarep/the-new-kid-in-town-gos-sync-map-de24a6bf7c2c

Modifications

The sync.RWMutex and map of compression providers (map[pb.CompressionType]compression.Provider) in the partition consumer has been replaced with a sync.Map

Verifying this change

This change is already covered by existing tests, such as:

  • pulsar/consumer_partitiion_test.go

Does this pull request potentially affect one of the following parts:

  • Dependencies (does it add or upgrade a dependency): No
  • The public API: No
  • The schema: No
  • The default values of configurations: No
  • The wire protocol: No

Documentation

  • Does this pull request introduce a new feature? No

@dferstay dferstay force-pushed the lock-free-consumer-compression-providers branch from 9bf63d9 to 96bf631 Compare December 24, 2021 05:41
@dferstay dferstay changed the title Provide lock-free access to partition consumer compression providers [Partition Consumer] Provide lock-free access to compression providers Dec 24, 2021
@wolfstudy wolfstudy added this to the v0.8.0 milestone Dec 24, 2021
Copy link
Member

@wolfstudy wolfstudy left a comment

Choose a reason for hiding this comment

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

LGTM +1

pc.providersMutex.RLock()
provider, ok := pc.compressionProviders[msgMeta.GetCompression()]
pc.providersMutex.RUnlock()
v, ok := pc.compressionProviders.Load(msgMeta.GetCompression())
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can replace v with meaningful variable names

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wolfstudy , I've renamed the variables in this method for additional clarity.

Copy link
Member

Choose a reason for hiding this comment

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

Cool, nice work

The compression providers map in the partition consumer is a textbook case
for using go's lock-free sync.Map: the set of map entries is stable and
access is read-only.

On machines with 4 cores or greater, read contention on the sync.RWMutex
outweighs the cost of using a sync.Map.

Below is an old article on the subject, but it still holds true today:
https://medium.com/@deckarep/the-new-kid-in-town-gos-sync-map-de24a6bf7c2c

Signed-off-by: Daniel Ferstay <dferstay@splunk.com>
@dferstay dferstay force-pushed the lock-free-consumer-compression-providers branch from 0a8e77c to ac0a159 Compare December 25, 2021 04:57
@wolfstudy wolfstudy merged commit d5d4903 into apache:master Dec 26, 2021
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.

2 participants