csnpl.c 7.7 KB

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