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

Explore possibility of sharding the keyspace for reducing lock scope #93

Open
the123saurav opened this issue Dec 23, 2021 · 3 comments
Open
Assignees
Labels
enhancement New feature or request

Comments

@the123saurav
Copy link
Contributor

Issue

Currently we are taking a global lock for every request so as to guarantee the order of execution for commands.

Possible Solve

CM uses segments under the hood just like ConcurrentHashMap for parallel access.
If we can use the same hashing mechanism and just take a lock on the segment, we can guarantee ordering for same key.

@the123saurav the123saurav added the enhancement New feature or request label Dec 23, 2021
@tuhuynh27
Copy link
Member

tuhuynh27 commented Dec 23, 2021

So let's say if we do this in order:

  • Get shard for key and take lock K on that
  • Generate a monotonic number atomically which decides the order of command, write this number along with the command to AOF under a global lock G.
  • Release the lock G now
  • Process for key
  • Release lock K

So basically limit the global lock scope(barring Tx)
The number that we persisted would be used for sorting at boot
CM uses this function XxHash_r39 which they have adapted, we can also adapt that and if we specify the number of segments when constructing CM, we can know what segment a key hash would fall in (https://github.com/OpenHFT/Chronicle-Algorithms/blob/ea/src/main/java/net/openhft/chronicle/algo/hashing/XxHash_r39.java)

Suggested flow by @the123saurav

@haphananhtuan
Copy link
Collaborator

Just a quick thought, if we gonna support sharding by putting a lock for each shard then we could remove the global lock. Because with shard level lock we have already guarantee the order of command within that shard.

@tuhuynh27
Copy link
Member

Yes @haphananhtuan, ChronicleMap already has an internal shard lock to handle concurrency, but we (from outside) can't access that lock thus don't know the command order to write to the AOF log / replication log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants