token.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. "github.com/gocql/gocql/internal/murmur"
  14. )
  15. // a token partitioner
  16. type partitioner interface {
  17. Name() string
  18. Hash([]byte) token
  19. ParseString(string) token
  20. }
  21. // a token
  22. type token interface {
  23. fmt.Stringer
  24. Less(token) bool
  25. }
  26. // murmur3 partitioner and token
  27. type murmur3Partitioner struct{}
  28. type murmur3Token int64
  29. func (p murmur3Partitioner) Name() string {
  30. return "Murmur3Partitioner"
  31. }
  32. func (p murmur3Partitioner) Hash(partitionKey []byte) token {
  33. h1 := murmur.Murmur3H1(partitionKey)
  34. return murmur3Token(int64(h1))
  35. }
  36. // murmur3 little-endian, 128-bit hash, but returns only h1
  37. func (p murmur3Partitioner) ParseString(str string) token {
  38. val, _ := strconv.ParseInt(str, 10, 64)
  39. return murmur3Token(val)
  40. }
  41. func (m murmur3Token) String() string {
  42. return strconv.FormatInt(int64(m), 10)
  43. }
  44. func (m murmur3Token) Less(token token) bool {
  45. return m < token.(murmur3Token)
  46. }
  47. // order preserving partitioner and token
  48. type orderedPartitioner struct{}
  49. type orderedToken []byte
  50. func (p orderedPartitioner) Name() string {
  51. return "OrderedPartitioner"
  52. }
  53. func (p orderedPartitioner) Hash(partitionKey []byte) token {
  54. // the partition key is the token
  55. return orderedToken(partitionKey)
  56. }
  57. func (p orderedPartitioner) ParseString(str string) token {
  58. return orderedToken([]byte(str))
  59. }
  60. func (o orderedToken) String() string {
  61. return string([]byte(o))
  62. }
  63. func (o orderedToken) Less(token token) bool {
  64. return -1 == bytes.Compare(o, token.(orderedToken))
  65. }
  66. // random partitioner and token
  67. type randomPartitioner struct{}
  68. type randomToken big.Int
  69. func (r randomPartitioner) Name() string {
  70. return "RandomPartitioner"
  71. }
  72. func (p randomPartitioner) Hash(partitionKey []byte) token {
  73. hash := md5.New()
  74. sum := hash.Sum(partitionKey)
  75. val := new(big.Int)
  76. val = val.SetBytes(sum)
  77. val = val.Abs(val)
  78. return (*randomToken)(val)
  79. }
  80. func (p randomPartitioner) ParseString(str string) token {
  81. val := new(big.Int)
  82. val.SetString(str, 10)
  83. return (*randomToken)(val)
  84. }
  85. func (r *randomToken) String() string {
  86. return (*big.Int)(r).String()
  87. }
  88. func (r *randomToken) Less(token token) bool {
  89. return -1 == (*big.Int)(r).Cmp((*big.Int)(token.(*randomToken)))
  90. }
  91. // a data structure for organizing the relationship between tokens and hosts
  92. type tokenRing struct {
  93. partitioner partitioner
  94. tokens []token
  95. hosts []*HostInfo
  96. }
  97. func newTokenRing(partitioner string, hosts []*HostInfo) (*tokenRing, error) {
  98. tokenRing := &tokenRing{
  99. tokens: []token{},
  100. hosts: []*HostInfo{},
  101. }
  102. if strings.HasSuffix(partitioner, "Murmur3Partitioner") {
  103. tokenRing.partitioner = murmur3Partitioner{}
  104. } else if strings.HasSuffix(partitioner, "OrderedPartitioner") {
  105. tokenRing.partitioner = orderedPartitioner{}
  106. } else if strings.HasSuffix(partitioner, "RandomPartitioner") {
  107. tokenRing.partitioner = randomPartitioner{}
  108. } else {
  109. return nil, fmt.Errorf("Unsupported partitioner '%s'", partitioner)
  110. }
  111. for _, host := range hosts {
  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. }