distrib.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #include <ctype.h>
  39. #include <string.h>
  40. #include "slapi-plugin.h"
  41. /*
  42. * These are examples of distribution function as declared in mapping tree node
  43. * This function will be called for every operations
  44. * reaching this node, including subtree search operations that are started
  45. * above this node
  46. *
  47. * Parameters :
  48. * . pb is the pblock of the operation
  49. * . target_dn is the target DN of the operation
  50. * . mtn_be_names is the list of names of backends declared for this node
  51. * . be_count is the number of backends declared
  52. * . node_dn is the node where the distribution function is set
  53. *
  54. * The return value of the functions should be the indice of the backend
  55. * in the mtn_be_names table
  56. * For search operation, the value SLAPI_BE_ALL_BACKENDS can be used to
  57. * specify that all backends must be searched
  58. * The use of value SLAPI_BE_ALL_BACKENDS for operation other than search
  59. * is not supported and may give random results
  60. *
  61. */
  62. /*
  63. * Distribute the entries based on the first letter of their rdn
  64. *
  65. * . Entries starting with anything other that a-z or A-Z will always
  66. * go in backend 0
  67. * . Entries starting with letter (a-z or A-Z) will be shared between
  68. * the backends depending following the alphabetic order
  69. * Example : if 3 backends are used, entries starting with A-I will go
  70. * in backend 0, entries starting with J-R will go in backend 1, entries
  71. * starting with S-Z will go in backend 2
  72. *
  73. * Of course this won't work for all locales...
  74. *
  75. * This example only works for a flat namespace below the node DN
  76. */
  77. int alpha_distribution(Slapi_PBlock *pb, Slapi_DN * target_dn,
  78. char **mtn_be_names, int be_count, Slapi_DN * node_dn)
  79. {
  80. unsigned long op_type;
  81. Slapi_Operation *op;
  82. char *rdn_type;
  83. char *rdn_value;
  84. Slapi_RDN *rdn = NULL;
  85. char c;
  86. /* first check the operation type
  87. * searches at node level or above it should go in all backends
  88. * searches below node level should go in only one backend
  89. */
  90. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  91. op_type = slapi_op_get_type(op);
  92. if ((op_type == SLAPI_OPERATION_SEARCH) &&
  93. slapi_sdn_issuffix(node_dn, target_dn))
  94. return SLAPI_BE_ALL_BACKENDS;
  95. /* now choose the backend
  96. * anything starting with a char different from a-z or A-Z will
  97. * go in backend 0
  98. */
  99. /* get the first char of first value of rdn */
  100. rdn = slapi_rdn_new();
  101. slapi_sdn_get_rdn(target_dn, rdn);
  102. slapi_rdn_get_first(rdn, &rdn_type, &rdn_value);
  103. c = rdn_value[0];
  104. slapi_rdn_free(&rdn);
  105. if (!(((c >= 'a') && (c <= 'z')) ||
  106. ((c >= 'A') && (c <= 'Z')) ))
  107. {
  108. return 0;
  109. }
  110. /* for entries with rdn starting with alphabetic characters
  111. * use the formula : (c - 'A') * be_count/26
  112. * to calculate the backend number
  113. */
  114. return (toupper(c) - 'A') * be_count/26;
  115. }
  116. /*
  117. * Distribute the entries based on a simple hash algorithme
  118. */
  119. int hash_distribution(Slapi_PBlock *pb, Slapi_DN * target_dn,
  120. char **mtn_be_names, int be_count, Slapi_DN * node_dn)
  121. {
  122. unsigned long op_type;
  123. Slapi_Operation *op;
  124. char *rdn_type;
  125. char *rdn_value;
  126. Slapi_RDN *rdn = NULL;
  127. int hash_value;
  128. /* first check the operation type
  129. * searches at node level or above it should go in all backends
  130. * searches below node level should go in only one backend
  131. */
  132. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  133. op_type = slapi_op_get_type(op);
  134. if ((op_type == SLAPI_OPERATION_SEARCH) &&
  135. slapi_sdn_issuffix(node_dn, target_dn))
  136. return SLAPI_BE_ALL_BACKENDS;
  137. /* now choose the backend
  138. */
  139. /* get the rdn and hash it to compute the backend number
  140. * use a simple hash for this example
  141. */
  142. rdn = slapi_rdn_new();
  143. slapi_sdn_get_rdn(target_dn, rdn);
  144. slapi_rdn_get_first(rdn, &rdn_type, &rdn_value);
  145. /* compute the hash value */
  146. hash_value = 0;
  147. while (*rdn_value)
  148. {
  149. hash_value += *rdn_value;
  150. rdn_value++;
  151. }
  152. hash_value = hash_value % be_count;
  153. slapi_rdn_free(&rdn);
  154. /* use the hash_value as the returned backend number */
  155. return hash_value;
  156. }
  157. /*
  158. * This plugin allows to use a local backend in conjonction with
  159. * a chaining backend
  160. * The ldbm backend is considered a read-only replica of the data
  161. * The chaining backend point to a red-write replica of the data
  162. * This distribution logic forward the update request to the chaining
  163. * backend, and send the search request to the local dbm database
  164. *
  165. * The mechanism for updating the local read-only replica is not
  166. * taken into account by this plugin
  167. *
  168. * To be able to use it one must define one ldbm backend and one chaining
  169. * backend in the mapping tree node
  170. *
  171. */
  172. int chaining_distribution(Slapi_PBlock *pb, Slapi_DN * target_dn,
  173. char **mtn_be_names, int be_count, Slapi_DN * node_dn)
  174. {
  175. char * requestor_dn;
  176. unsigned long op_type;
  177. Slapi_Operation *op;
  178. int repl_op = 0;
  179. int local_backend = -1;
  180. int chaining_backend = -1;
  181. int i;
  182. char * name;
  183. /* first, we have to decide which backend is the local backend
  184. * and which is the chaining one
  185. * For the purpose of this example use the backend name :
  186. * the backend with name starting with ldbm is local
  187. * the bakend with name starting with chaining is remote
  188. */
  189. local_backend = -1;
  190. chaining_backend = -1;
  191. for (i=0; i<be_count; i++)
  192. {
  193. name = mtn_be_names[i];
  194. if ((0 == strncmp(name, "ldbm", 4)) ||
  195. (0 == strncmp(name, "user", 4)))
  196. local_backend = i;
  197. else if (0 == strncmp(name, "chaining", 8))
  198. chaining_backend = i;
  199. }
  200. /* Check the operation type
  201. * read-only operation will go to the local backend
  202. * updates operation will go to the chaining backend
  203. */
  204. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  205. op_type = slapi_op_get_type(op);
  206. if ((op_type == SLAPI_OPERATION_SEARCH) ||
  207. (op_type == SLAPI_OPERATION_BIND) ||
  208. (op_type == SLAPI_OPERATION_UNBIND) ||
  209. (op_type == SLAPI_OPERATION_COMPARE))
  210. return local_backend;
  211. /* if the operation is done by directory manager
  212. * use local database even for updates because it is an administrative
  213. * operation
  214. * remarks : one could also use an update DN in the same way
  215. * to let update operation go to the local backend when they are done
  216. * by specific administrator user but let all the other user
  217. * go to the read-write replica
  218. */
  219. slapi_pblock_get( pb, SLAPI_REQUESTOR_DN, &requestor_dn );
  220. if (slapi_dn_isroot(requestor_dn))
  221. return local_backend;
  222. /* if the operation is a replicated operation
  223. * use local database even for updates to avoid infinite loops
  224. */
  225. slapi_pblock_get (pb, SLAPI_IS_REPLICATED_OPERATION, &repl_op);
  226. if (repl_op)
  227. return local_backend;
  228. /* all other case (update while not directory manager) :
  229. * use the chaining backend
  230. */
  231. return chaining_backend;
  232. }