Skip to content

Instantly share code, notes, and snippets.

@chaosong
Created March 6, 2017 08:31
Show Gist options
  • Save chaosong/252afa4933886cc0c9763e2d141bf23f to your computer and use it in GitHub Desktop.
Save chaosong/252afa4933886cc0c9763e2d141bf23f to your computer and use it in GitHub Desktop.
get token ring for batch accumulation
func (t *tokenRing) GetIndexForPartitionKey(partitionKey []byte) int {
if t == nil {
return 0
}
hash := t.partitioner.Hash(partitionKey)
l := len(t.tokens)
// no host tokens, no available hosts
if l == 0 {
return 0
}
// find the primary replica
ringIndex := sort.Search(
l,
func(i int) bool {
return !t.tokens[i].Less(hash)
},
)
if ringIndex == l {
// wrap around to the first in the ring
ringIndex = 0
}
return ringIndex
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment