Skip to content

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

Merged
wolfstudy merged 1 commit into
apache:masterfrom
dferstay:lock-free-consumer-compression-providers
Dec 26, 2021
Merged

wolfstudy merged 1 commit into
apache:masterfrom
dferstay:lock-free-consumer-compression-providers

Conversation

@dferstay

Copy link
Copy Markdown
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

@wolfstudy wolfstudy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM +1

Comment thread pulsar/consumer_partition.go Outdated
pc.providersMutex.RLock()
provider, ok := pc.compressionProviders[msgMeta.GetCompression()]
pc.providersMutex.RUnlock()
v, ok := pc.compressionProviders.Load(msgMeta.GetCompression())

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 <[email protected]>
@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