token.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright (c) 2015 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "bytes"
  7. "crypto/md5"
  8. "fmt"
  9. "math/big"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. )
  14. // a token partitioner
  15. type partitioner interface {
  16. Name() string
  17. Hash([]byte) token
  18. ParseString(string) token
  19. }
  20. // a token
  21. type token interface {
  22. fmt.Stringer
  23. Less(token) bool
  24. }
  25. // murmur3 partitioner and token
  26. type murmur3Partitioner struct{}
  27. type murmur3Token int64
  28. func (p murmur3Partitioner) Name() string {
  29. return "Murmur3Partitioner"
  30. }
  31. func (p murmur3Partitioner) Hash(partitionKey []byte) token {
  32. h1 := murmur3H1(partitionKey)
  33. return murmur3Token(int64(h1))
  34. }
  35. // murmur3 little-endian, 128-bit hash, but returns only h1
  36. func (p murmur3Partitioner) ParseString(str string) token {
  37. val, _ := strconv.ParseInt(str, 10, 64)
  38. return murmur3Token(val)
  39. }
  40. func (m murmur3Token) String() string {
  41. return strconv.FormatInt(int64(m), 10)
  42. }
  43. func (m murmur3Token) Less(token token) bool {
  44. return m < token.(murmur3Token)
  45. }
  46. // order preserving partitioner and token
  47. type orderedPartitioner struct{}
  48. type orderedToken []byte
  49. func (p orderedPartitioner) Name() string {
  50. return "OrderedPartitioner"
  51. }
  52. func (p orderedPartitioner) Hash(partitionKey []byte) token {
  53. // the partition key is the token
  54. return orderedToken(partitionKey)
  55. }
  56. func (p orderedPartitioner) ParseString(str string) token {
  57. return orderedToken([]byte(str))
  58. }
  59. func (o orderedToken) String() string {
  60. return string([]byte(o))
  61. }
  62. func (o orderedToken) Less(token token) bool {
  63. return -1 == bytes.Compare(o, token.(orderedToken))
  64. }
  65. // random partitioner and token
  66. type randomPartitioner struct{}
  67. type randomToken big.Int
  68. func (r randomPartitioner) Name() string {
  69. return "RandomPartitioner"
  70. }
  71. func (p randomPartitioner) Hash(partitionKey []byte) token {
  72. hash := md5.New()
  73. sum := hash.Sum(partitionKey)
  74. val := new(big.Int)
  75. val = val.SetBytes(sum)
  76. val = val.Abs(val)
  77. return (*randomToken)(val)
  78. }
  79. func (p randomPartitioner) ParseString(str string) token {
  80. val := new(big.Int)
  81. val.SetString(str, 10)
  82. return (*randomToken)(val)
  83. }
  84. func (r *randomToken) String() string {
  85. return (*big.Int)(r).String()
  86. }
  87. func (r *randomToken) Less(token token) bool {
  88. return -1 == (*big.Int)(r).Cmp((*big.Int)(token.(*randomToken)))
  89. }
  90. // a data structure for organizing the relationship between tokens and hosts
  91. type tokenRing struct {
  92. partitioner partitioner
  93. tokens []token
  94. hosts []*HostInfo
  95. }
  96. func newTokenRing(partitioner string, hosts []HostInfo) (*tokenRing, error) {
  97. tokenRing := &tokenRing{
  98. tokens: []token{},
  99. hosts: []*HostInfo{},
  100. }
  101. if strings.HasSuffix(partitioner, "Murmur3Partitioner") {
  102. tokenRing.partitioner = murmur3Partitioner{}
  103. } else if strings.HasSuffix(partitioner, "OrderedPartitioner") {
  104. tokenRing.partitioner = orderedPartitioner{}
  105. } else if strings.HasSuffix(partitioner, "RandomPartitioner") {
  106. tokenRing.partitioner = randomPartitioner{}
  107. } else {
  108. return nil, fmt.Errorf("Unsupported partitioner '%s'", partitioner)
  109. }
  110. for i := range hosts {
  111. host := &hosts[i]
  112. for _, strToken := range host.Tokens {
  113. token := tokenRing.partitioner.ParseString(strToken)
  114. tokenRing.tokens = append(tokenRing.tokens, token)
  115. tokenRing.hosts = append(tokenRing.hosts, host)
  116. }
  117. }
  118. sort.Sort(tokenRing)
  119. return tokenRing, nil
  120. }
  121. func (t *tokenRing) Len() int {
  122. return len(t.tokens)
  123. }
  124. func (t *tokenRing) Less(i, j int) bool {
  125. return t.tokens[i].Less(t.tokens[j])
  126. }
  127. func (t *tokenRing) Swap(i, j int) {
  128. t.tokens[i], t.hosts[i], t.tokens[j], t.hosts[j] =
  129. t.tokens[j], t.hosts[j], t.tokens[i], t.hosts[i]
  130. }
  131. func (t *tokenRing) String() string {
  132. buf := &bytes.Buffer{}
  133. buf.WriteString("TokenRing(")
  134. if t.partitioner != nil {
  135. buf.WriteString(t.partitioner.Name())
  136. }
  137. buf.WriteString("){")
  138. sep := ""
  139. for i := range t.tokens {
  140. buf.WriteString(sep)
  141. sep = ","
  142. buf.WriteString("\n\t[")
  143. buf.WriteString(strconv.Itoa(i))
  144. buf.WriteString("]")
  145. buf.WriteString(t.tokens[i].String())
  146. buf.WriteString(":")
  147. buf.WriteString(t.hosts[i].Peer)
  148. }
  149. buf.WriteString("\n}")
  150. return string(buf.Bytes())
  151. }
  152. func (t *tokenRing) GetHostForPartitionKey(partitionKey []byte) *HostInfo {
  153. if t == nil {
  154. return nil
  155. }
  156. token := t.partitioner.Hash(partitionKey)
  157. return t.GetHostForToken(token)
  158. }
  159. func (t *tokenRing) GetHostForToken(token token) *HostInfo {
  160. if t == nil {
  161. return nil
  162. }
  163. // find the primary replica
  164. ringIndex := sort.Search(
  165. len(t.tokens),
  166. func(i int) bool {
  167. return !t.tokens[i].Less(token)
  168. },
  169. )
  170. if ringIndex == len(t.tokens) {
  171. // wrap around to the first in the ring
  172. ringIndex = 0
  173. }
  174. host := t.hosts[ringIndex]
  175. return host
  176. }