1
0

csnpl.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 "csnpl.h"
  42. #include "llist.h"
  43. #include "repl_shared.h"
  44. struct csnpl
  45. {
  46. LList* csnList; /* pending list */
  47. PRRWLock* csnLock; /* lock to serialize access to PL */
  48. };
  49. typedef struct _csnpldata
  50. {
  51. PRBool committed; /* True if CSN committed */
  52. CSN *csn; /* The actual CSN */
  53. } csnpldata;
  54. /* forward declarations */
  55. #ifdef DEBUG
  56. static void _csnplDumpContentNoLock(CSNPL *csnpl, const char *caller);
  57. #endif
  58. CSNPL* csnplNew ()
  59. {
  60. CSNPL *csnpl;
  61. csnpl = (CSNPL *)slapi_ch_malloc (sizeof (CSNPL));
  62. if (csnpl == NULL)
  63. {
  64. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  65. "csnplNew: failed to allocate pending list\n");
  66. return NULL;
  67. }
  68. csnpl->csnList = llistNew ();
  69. if (csnpl->csnList == NULL)
  70. {
  71. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  72. "csnplNew: failed to allocate pending list\n");
  73. slapi_ch_free ((void**)&csnpl);
  74. return NULL;
  75. }
  76. /* ONREPL: do locks need different names */
  77. csnpl->csnLock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "pl_lock");
  78. if (csnpl->csnLock == NULL)
  79. {
  80. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  81. "csnplNew: failed to create lock; NSPR error - %d\n",
  82. PR_GetError ());
  83. slapi_ch_free ((void**)&(csnpl->csnList));
  84. slapi_ch_free ((void**)&csnpl);
  85. return NULL;
  86. }
  87. return csnpl;
  88. }
  89. void
  90. csnpldata_free(void **data)
  91. {
  92. csnpldata **data_to_free = (csnpldata **)data;
  93. if (NULL != data_to_free)
  94. {
  95. if (NULL != (*data_to_free)->csn)
  96. {
  97. csn_free(&(*data_to_free)->csn);
  98. }
  99. slapi_ch_free((void **)data_to_free);
  100. }
  101. }
  102. void csnplFree (CSNPL **csnpl)
  103. {
  104. if ((csnpl == NULL) || (*csnpl == NULL))
  105. return;
  106. /* free all remaining nodes */
  107. llistDestroy (&((*csnpl)->csnList), (FNFree)csnpldata_free);
  108. if ((*csnpl)->csnLock);
  109. PR_DestroyRWLock ((*csnpl)->csnLock);
  110. slapi_ch_free ((void**)csnpl);
  111. }
  112. /* This function isnerts a CSN into the pending list
  113. * Returns: 0 if the csn was successfully inserted
  114. * 1 if the csn has already been seen
  115. * -1 for any other kind of errors
  116. */
  117. int csnplInsert (CSNPL *csnpl, const CSN *csn)
  118. {
  119. int rc;
  120. csnpldata *csnplnode;
  121. char csn_str[CSN_STRSIZE];
  122. if (csnpl == NULL || csn == NULL)
  123. {
  124. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  125. "csnplInsert: invalid argument\n");
  126. return -1;
  127. }
  128. PR_RWLock_Wlock (csnpl->csnLock);
  129. /* check to see if this csn is larger than the last csn in the
  130. pending list. It has to be if we have not seen it since
  131. the csns are always added in the accending order. */
  132. csnplnode = llistGetTail (csnpl->csnList);
  133. if (csnplnode && csn_compare (csnplnode->csn, csn) >= 0)
  134. {
  135. PR_RWLock_Unlock (csnpl->csnLock);
  136. return 1;
  137. }
  138. csnplnode = (csnpldata *)slapi_ch_malloc(sizeof(csnpldata));
  139. csnplnode->committed = PR_FALSE;
  140. csnplnode->csn = csn_dup(csn);
  141. csn_as_string(csn, PR_FALSE, csn_str);
  142. rc = llistInsertTail (csnpl->csnList, csn_str, csnplnode);
  143. #ifdef DEBUG
  144. _csnplDumpContentNoLock(csnpl, "csnplInsert");
  145. #endif
  146. PR_RWLock_Unlock (csnpl->csnLock);
  147. if (rc != 0)
  148. {
  149. char s[CSN_STRSIZE];
  150. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  151. "csnplInsert: failed to insert csn (%s) into pending list\n", csn_as_string(csn,PR_FALSE,s));
  152. return -1;
  153. }
  154. return 0;
  155. }
  156. int csnplRemove (CSNPL *csnpl, const CSN *csn)
  157. {
  158. csnpldata *data;
  159. char csn_str[CSN_STRSIZE];
  160. if (csnpl == NULL || csn == NULL)
  161. {
  162. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  163. "csnplRemove: invalid argument\n");
  164. return -1;
  165. }
  166. csn_as_string(csn, PR_FALSE, csn_str);
  167. PR_RWLock_Wlock (csnpl->csnLock);
  168. data = (csnpldata *)llistRemove (csnpl->csnList, csn_str);
  169. if (data == NULL)
  170. {
  171. PR_RWLock_Unlock (csnpl->csnLock);
  172. return -1;
  173. }
  174. #ifdef DEBUG
  175. _csnplDumpContentNoLock(csnpl, "csnplRemove");
  176. #endif
  177. csn_free(&data->csn);
  178. slapi_ch_free((void **)&data);
  179. PR_RWLock_Unlock (csnpl->csnLock);
  180. return 0;
  181. }
  182. int csnplCommit (CSNPL *csnpl, const CSN *csn)
  183. {
  184. csnpldata *data;
  185. char csn_str[CSN_STRSIZE];
  186. if (csnpl == NULL || csn == NULL)
  187. {
  188. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  189. "csnplCommit: invalid argument\n");
  190. return -1;
  191. }
  192. csn_as_string(csn, PR_FALSE, csn_str);
  193. PR_RWLock_Wlock (csnpl->csnLock);
  194. #ifdef DEBUG
  195. _csnplDumpContentNoLock(csnpl, "csnplCommit");
  196. #endif
  197. data = (csnpldata*)llistGet (csnpl->csnList, csn_str);
  198. if (data == NULL)
  199. {
  200. /*
  201. * In the scenario "4.x master -> 6.x legacy-consumer -> 6.x consumer"
  202. * csn will have rid=65535. Hence 6.x consumer will get here trying
  203. * to commit r->min_csn_pl because its rid matches that in the csn.
  204. * However, r->min_csn_pl is always empty for a dedicated consumer.
  205. * Exclude READ-ONLY replica ID here from error logging.
  206. */
  207. ReplicaId rid = csn_get_replicaid (csn);
  208. if (rid < MAX_REPLICA_ID)
  209. {
  210. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  211. "csnplCommit: can't find csn %s\n", csn_str);
  212. }
  213. PR_RWLock_Unlock (csnpl->csnLock);
  214. return -1;
  215. }
  216. else
  217. {
  218. data->committed = PR_TRUE;
  219. }
  220. PR_RWLock_Unlock (csnpl->csnLock);
  221. return 0;
  222. }
  223. CSN* csnplGetMinCSN (CSNPL *csnpl, PRBool *committed)
  224. {
  225. csnpldata *data;
  226. CSN *csn = NULL;
  227. PR_RWLock_Rlock (csnpl->csnLock);
  228. if ((data = (csnpldata*)llistGetHead (csnpl->csnList)) != NULL)
  229. {
  230. csn = csn_dup(data->csn);
  231. if (NULL != committed)
  232. {
  233. *committed = data->committed;
  234. }
  235. }
  236. PR_RWLock_Unlock (csnpl->csnLock);
  237. return csn;
  238. }
  239. /*
  240. * Roll up the list of pending CSNs, removing all of the CSNs at the
  241. * head of the the list that are committed and contiguous. Returns
  242. * the largest committed CSN, or NULL if no contiguous block of
  243. * committed CSNs appears at the beginning of the list. The caller
  244. * is responsible for freeing the CSN returned.
  245. */
  246. CSN *
  247. csnplRollUp(CSNPL *csnpl, CSN **first_commited)
  248. {
  249. CSN *largest_committed_csn = NULL;
  250. csnpldata *data;
  251. PRBool freeit = PR_TRUE;
  252. PR_RWLock_Wlock (csnpl->csnLock);
  253. if (first_commited) {
  254. /* Avoid non-initialization issues due to careless callers */
  255. *first_commited = NULL;
  256. }
  257. data = (csnpldata *)llistGetHead(csnpl->csnList);
  258. while (NULL != data && data->committed)
  259. {
  260. if (NULL != largest_committed_csn && freeit)
  261. {
  262. csn_free(&largest_committed_csn);
  263. }
  264. freeit = PR_TRUE;
  265. largest_committed_csn = data->csn; /* Save it */
  266. if (first_commited && (*first_commited == NULL)) {
  267. *first_commited = data->csn;
  268. freeit = PR_FALSE;
  269. }
  270. data = (csnpldata*)llistRemoveHead (csnpl->csnList);
  271. slapi_ch_free((void **)&data);
  272. data = (csnpldata *)llistGetHead(csnpl->csnList);
  273. }
  274. #ifdef DEBUG
  275. _csnplDumpContentNoLock(csnpl, "csnplRollUp");
  276. #endif
  277. PR_RWLock_Unlock (csnpl->csnLock);
  278. return largest_committed_csn;
  279. }
  280. #ifdef DEBUG
  281. /* Dump current content of the list - for debugging */
  282. void
  283. csnplDumpContent(CSNPL *csnpl, const char *caller)
  284. {
  285. if (csnpl)
  286. {
  287. PR_RWLock_Rlock (csnpl->csnLock);
  288. _csnplDumpContentNoLock (csnpl, caller);
  289. PR_RWLock_Unlock (csnpl->csnLock);
  290. }
  291. }
  292. /* helper function */
  293. static void _csnplDumpContentNoLock(CSNPL *csnpl, const char *caller)
  294. {
  295. csnpldata *data;
  296. void *iterator;
  297. char csn_str[CSN_STRSIZE];
  298. data = (csnpldata *)llistGetFirst(csnpl->csnList, &iterator);
  299. if (data) {
  300. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "%s: CSN Pending list content:\n",
  301. caller ? caller : "");
  302. }
  303. while (data)
  304. {
  305. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "%s, %s\n",
  306. csn_as_string(data->csn, PR_FALSE, csn_str),
  307. data->committed ? "committed" : "not committed");
  308. data = (csnpldata *)llistGetNext (csnpl->csnList, &iterator);
  309. }
  310. }
  311. #endif