distrib.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* --- BEGIN COPYRIGHT BLOCK ---
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * --- END COPYRIGHT BLOCK --- */
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include <ctype.h>
  42. #include <string.h>
  43. #include "slapi-plugin.h"
  44. /*
  45. * These are examples of distribution function as declared in mapping tree node
  46. * This function will be called for every operations
  47. * reaching this node, including subtree search operations that are started
  48. * above this node
  49. *
  50. * Parameters :
  51. * . pb is the pblock of the operation
  52. * . target_dn is the target DN of the operation
  53. * . mtn_be_names is the list of names of backends declared for this node
  54. * . be_count is the number of backends declared
  55. * . node_dn is the node where the distribution function is set
  56. *
  57. * The return value of the functions should be the indice of the backend
  58. * in the mtn_be_names table
  59. * For search operation, the value SLAPI_BE_ALL_BACKENDS can be used to
  60. * specify that all backends must be searched
  61. * The use of value SLAPI_BE_ALL_BACKENDS for operation other than search
  62. * is not supported and may give random results
  63. *
  64. */
  65. /*
  66. * Distribute the entries based on the first letter of their rdn
  67. *
  68. * . Entries starting with anything other that a-z or A-Z will always
  69. * go in backend 0
  70. * . Entries starting with letter (a-z or A-Z) will be shared between
  71. * the backends depending following the alphabetic order
  72. * Example : if 3 backends are used, entries starting with A-I will go
  73. * in backend 0, entries starting with J-R will go in backend 1, entries
  74. * starting with S-Z will go in backend 2
  75. *
  76. * Of course this won't work for all locales...
  77. *
  78. * This example only works for a flat namespace below the node DN
  79. */
  80. int alpha_distribution(Slapi_PBlock *pb, Slapi_DN * target_dn,
  81. char **mtn_be_names, int be_count, Slapi_DN * node_dn)
  82. {
  83. unsigned long op_type;
  84. Slapi_Operation *op;
  85. char *rdn_type;
  86. char *rdn_value;
  87. Slapi_RDN *rdn = NULL;
  88. char c;
  89. /* first check the operation type
  90. * searches at node level or above it should go in all backends
  91. * searches below node level should go in only one backend
  92. */
  93. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  94. op_type = slapi_op_get_type(op);
  95. if ((op_type == SLAPI_OPERATION_SEARCH) &&
  96. slapi_sdn_issuffix(node_dn, target_dn))
  97. return SLAPI_BE_ALL_BACKENDS;
  98. /* now choose the backend
  99. * anything starting with a char different from a-z or A-Z will
  100. * go in backend 0
  101. */
  102. /* get the first char of first value of rdn */
  103. rdn = slapi_rdn_new();
  104. slapi_sdn_get_rdn(target_dn, rdn);
  105. slapi_rdn_get_first(rdn, &rdn_type, &rdn_value);
  106. c = rdn_value[0];
  107. slapi_rdn_free(&rdn);
  108. if (!(((c >= 'a') && (c <= 'z')) ||
  109. ((c >= 'A') && (c <= 'Z')) ))
  110. {
  111. return 0;
  112. }
  113. /* for entries with rdn starting with alphabetic characters
  114. * use the formula : (c - 'A') * be_count/26
  115. * to calculate the backend number
  116. */
  117. return (toupper(c) - 'A') * be_count/26;
  118. }
  119. /*
  120. * Distribute the entries based on a simple hash algorithme
  121. */
  122. int hash_distribution(Slapi_PBlock *pb, Slapi_DN * target_dn,
  123. char **mtn_be_names, int be_count, Slapi_DN * node_dn)
  124. {
  125. unsigned long op_type;
  126. Slapi_Operation *op;
  127. char *rdn_type;
  128. char *rdn_value;
  129. Slapi_RDN *rdn = NULL;
  130. int hash_value;
  131. /* first check the operation type
  132. * searches at node level or above it should go in all backends
  133. * searches below node level should go in only one backend
  134. */
  135. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  136. op_type = slapi_op_get_type(op);
  137. if ((op_type == SLAPI_OPERATION_SEARCH) &&
  138. slapi_sdn_issuffix(node_dn, target_dn))
  139. return SLAPI_BE_ALL_BACKENDS;
  140. /* now choose the backend
  141. */
  142. /* get the rdn and hash it to compute the backend number
  143. * use a simple hash for this example
  144. */
  145. rdn = slapi_rdn_new();
  146. slapi_sdn_get_rdn(target_dn, rdn);
  147. slapi_rdn_get_first(rdn, &rdn_type, &rdn_value);
  148. /* compute the hash value */
  149. hash_value = 0;
  150. while (*rdn_value)
  151. {
  152. hash_value += *rdn_value;
  153. rdn_value++;
  154. }
  155. hash_value = hash_value % be_count;
  156. slapi_rdn_free(&rdn);
  157. /* use the hash_value as the returned backend number */
  158. return hash_value;
  159. }
  160. /*
  161. * This plugin allows to use a local backend in conjonction with
  162. * a chaining backend
  163. * The ldbm backend is considered a read-only replica of the data
  164. * The chaining backend point to a red-write replica of the data
  165. * This distribution logic forward the update request to the chaining
  166. * backend, and send the search request to the local dbm database
  167. *
  168. * The mechanism for updating the local read-only replica is not
  169. * taken into account by this plugin
  170. *
  171. * To be able to use it one must define one ldbm backend and one chaining
  172. * backend in the mapping tree node
  173. *
  174. */
  175. int chaining_distribution(Slapi_PBlock *pb, Slapi_DN * target_dn,
  176. char **mtn_be_names, int be_count, Slapi_DN * node_dn)
  177. {
  178. char * requestor_dn;
  179. unsigned long op_type;
  180. Slapi_Operation *op;
  181. int repl_op = 0;
  182. int local_backend = -1;
  183. int chaining_backend = -1;
  184. int i;
  185. char * name;
  186. /* first, we have to decide which backend is the local backend
  187. * and which is the chaining one
  188. * For the purpose of this example use the backend name :
  189. * the backend with name starting with ldbm is local
  190. * the bakend with name starting with chaining is remote
  191. */
  192. local_backend = -1;
  193. chaining_backend = -1;
  194. for (i=0; i<be_count; i++)
  195. {
  196. name = mtn_be_names[i];
  197. if ((0 == strncmp(name, "ldbm", 4)) ||
  198. (0 == strncmp(name, "user", 4)))
  199. local_backend = i;
  200. else if (0 == strncmp(name, "chaining", 8))
  201. chaining_backend = i;
  202. }
  203. /* Check the operation type
  204. * read-only operation will go to the local backend
  205. * updates operation will go to the chaining backend
  206. */
  207. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  208. op_type = slapi_op_get_type(op);
  209. if ((op_type == SLAPI_OPERATION_SEARCH) ||
  210. (op_type == SLAPI_OPERATION_BIND) ||
  211. (op_type == SLAPI_OPERATION_UNBIND) ||
  212. (op_type == SLAPI_OPERATION_COMPARE))
  213. return local_backend;
  214. /* if the operation is done by directory manager
  215. * use local database even for updates because it is an administrative
  216. * operation
  217. * remarks : one could also use an update DN in the same way
  218. * to let update operation go to the local backend when they are done
  219. * by specific administrator user but let all the other user
  220. * go to the read-write replica
  221. */
  222. slapi_pblock_get( pb, SLAPI_REQUESTOR_DN, &requestor_dn );
  223. if (slapi_dn_isroot(requestor_dn))
  224. return local_backend;
  225. /* if the operation is a replicated operation
  226. * use local database even for updates to avoid infinite loops
  227. */
  228. slapi_pblock_get (pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  229. if (repl_op)
  230. return local_backend;
  231. /* all other case (update while not directory manager) :
  232. * use the chaining backend
  233. */
  234. return chaining_backend;
  235. }