Created
March 6, 2017 08:31
-
-
Save chaosong/252afa4933886cc0c9763e2d141bf23f to your computer and use it in GitHub Desktop.
get token ring for batch accumulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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