csnpl.c 8.2 KB

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