ketama.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * HashKit library
  4. *
  5. * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2006-2009 Brian Aker All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following disclaimer
  17. * in the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * * The names of its contributors may not be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  27. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  28. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  30. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. */
  37. #include <libhashkit/common.h>
  38. #include <math.h>
  39. #if 0
  40. static uint32_t ketama_server_hash(const char *key, unsigned int key_length, int alignment)
  41. {
  42. unsigned char results[16];
  43. md5_signature((unsigned char*)key, key_length, results);
  44. return ((uint32_t) (results[3 + alignment * 4] & 0xFF) << 24)
  45. | ((uint32_t) (results[2 + alignment * 4] & 0xFF) << 16)
  46. | ((uint32_t) (results[1 + alignment * 4] & 0xFF) << 8)
  47. | (results[0 + alignment * 4] & 0xFF);
  48. }
  49. static int continuum_points_cmp(const void *t1, const void *t2)
  50. {
  51. hashkit_continuum_point_st *ct1= (hashkit_continuum_point_st *)t1;
  52. hashkit_continuum_point_st *ct2= (hashkit_continuum_point_st *)t2;
  53. if (ct1->value == ct2->value)
  54. return 0;
  55. else if (ct1->value > ct2->value)
  56. return 1;
  57. else
  58. return -1;
  59. }
  60. int update_continuum(hashkit_st *hashkit)
  61. {
  62. uint32_t count;
  63. uint32_t continuum_index= 0;
  64. uint32_t value;
  65. uint32_t points_index;
  66. uint32_t points_count= 0;
  67. uint32_t points_per_server;
  68. uint32_t points_per_hash;
  69. uint64_t total_weight= 0;
  70. uint32_t live_servers;
  71. uint8_t *context;
  72. if (hashkit->active_fn != NULL || hashkit->weight_fn != NULL)
  73. {
  74. live_servers= 0;
  75. for (count= 0, context= hashkit->list; count < hashkit->list_size;
  76. count++, context+= hashkit->context_size)
  77. {
  78. if (hashkit->active_fn != NULL)
  79. {
  80. if (hashkit->active_fn(context))
  81. live_servers++;
  82. else
  83. continue;
  84. }
  85. if (hashkit->weight_fn != NULL)
  86. total_weight+= hashkit->weight_fn(context);
  87. }
  88. }
  89. if (hashkit->active_fn == NULL)
  90. live_servers= (uint32_t)hashkit->list_size;
  91. if (live_servers == 0)
  92. return 0;
  93. if (hashkit->weight_fn == NULL)
  94. {
  95. points_per_server= HASHKIT_POINTS_PER_NODE;
  96. points_per_hash= 1;
  97. }
  98. else
  99. {
  100. points_per_server= HASHKIT_POINTS_PER_NODE_WEIGHTED;
  101. points_per_hash= 4;
  102. }
  103. if (live_servers > hashkit->continuum_count)
  104. {
  105. hashkit_continuum_point_st *new_continuum;
  106. new_continuum= realloc(hashkit->continuum,
  107. sizeof(hashkit_continuum_point_st) *
  108. (live_servers + HASHKIT_CONTINUUM_ADDITION) *
  109. points_per_server);
  110. if (new_continuum == NULL)
  111. return ENOMEM;
  112. hashkit->continuum= new_continuum;
  113. hashkit->continuum_count= live_servers + HASHKIT_CONTINUUM_ADDITION;
  114. }
  115. for (count= 0, context= hashkit->list; count < hashkit->list_size;
  116. count++, context+= hashkit->context_size)
  117. {
  118. if (hashkit->active_fn != NULL && hashkit->active_fn(context) == false)
  119. continue;
  120. if (hashkit->weight_fn != NULL)
  121. {
  122. float pct = (float)hashkit->weight_fn(context) / (float)total_weight;
  123. points_per_server= (uint32_t) ((floorf((float) (pct * HASHKIT_POINTS_PER_NODE_WEIGHTED / 4 * (float)live_servers + 0.0000000001))) * 4);
  124. }
  125. for (points_index= 0;
  126. points_index < points_per_server / points_per_hash;
  127. points_index++)
  128. {
  129. char sort_host[HASHKIT_CONTINUUM_KEY_SIZE]= "";
  130. size_t sort_host_length;
  131. if (hashkit->continuum_key_fn == NULL)
  132. {
  133. sort_host_length= (size_t) snprintf(sort_host, HASHKIT_CONTINUUM_KEY_SIZE, "%u",
  134. points_index);
  135. }
  136. else
  137. {
  138. sort_host_length= hashkit->continuum_key_fn(sort_host, HASHKIT_CONTINUUM_KEY_SIZE,
  139. points_index, context);
  140. }
  141. if (hashkit->weight_fn == NULL)
  142. {
  143. if (hashkit->continuum_hash_fn == NULL)
  144. value= hashkit_default(sort_host, sort_host_length);
  145. else
  146. value= hashkit->continuum_hash_fn(sort_host, sort_host_length);
  147. hashkit->continuum[continuum_index].index= count;
  148. hashkit->continuum[continuum_index++].value= value;
  149. }
  150. else
  151. {
  152. unsigned int i;
  153. for (i = 0; i < points_per_hash; i++)
  154. {
  155. value= ketama_server_hash(sort_host, (uint32_t) sort_host_length, (int) i);
  156. hashkit->continuum[continuum_index].index= count;
  157. hashkit->continuum[continuum_index++].value= value;
  158. }
  159. }
  160. }
  161. points_count+= points_per_server;
  162. }
  163. hashkit->continuum_points_count= points_count;
  164. qsort(hashkit->continuum, hashkit->continuum_points_count, sizeof(hashkit_continuum_point_st),
  165. continuum_points_cmp);
  166. return 0;
  167. }
  168. #endif