keva icon indicating copy to clipboard operation
keva copied to clipboard

Explore possibility of sharding the keyspace for reducing lock scope

Open the123saurav opened this issue 4 years ago • 3 comments

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 avatar Dec 23 '21 04:12 the123saurav

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

tuhuynh27 avatar Dec 23 '21 04:12 tuhuynh27

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.

haphananhtuan avatar Dec 23 '21 17:12 haphananhtuan

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

tuhuynh27 avatar Dec 23 '21 18:12 tuhuynh27