csnpl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. struct csnpl
  15. {
  16. LList* csnList; /* pending list */
  17. Slapi_RWLock* csnLock; /* lock to serialize access to PL */
  18. };
  19. typedef struct _csnpldata
  20. {
  21. PRBool committed; /* True if CSN committed */
  22. CSN *csn; /* The actual CSN */
  23. Replica * prim_replica; /* The replica where the prom csn was generated */
  24. const CSN *prim_csn; /* The primary CSN of an operation consising of multiple sub ops*/
  25. } csnpldata;
  26. static PRBool csn_primary_or_nested(csnpldata *csn_data, const CSNPL_CTX *csn_ctx);
  27. /* forward declarations */
  28. #ifdef DEBUG
  29. static void _csnplDumpContentNoLock(CSNPL *csnpl, const char *caller);
  30. #endif
  31. CSNPL* csnplNew ()
  32. {
  33. CSNPL *csnpl;
  34. csnpl = (CSNPL *)slapi_ch_malloc (sizeof (CSNPL));
  35. if (csnpl == NULL)
  36. {
  37. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  38. "csnplNew - Failed to allocate pending list\n");
  39. return NULL;
  40. }
  41. csnpl->csnList = llistNew ();
  42. if (csnpl->csnList == NULL)
  43. {
  44. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  45. "csnplNew - Failed to allocate pending list\n");
  46. slapi_ch_free ((void**)&csnpl);
  47. return NULL;
  48. }
  49. /* ONREPL: do locks need different names */
  50. csnpl->csnLock = slapi_new_rwlock();
  51. if (csnpl->csnLock == NULL)
  52. {
  53. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  54. "csnplNew - Failed to create lock; NSPR error - %d\n",
  55. PR_GetError ());
  56. slapi_ch_free ((void**)&(csnpl->csnList));
  57. slapi_ch_free ((void**)&csnpl);
  58. return NULL;
  59. }
  60. return csnpl;
  61. }
  62. void
  63. csnpldata_free(csnpldata **data_to_free)
  64. {
  65. if (NULL != data_to_free)
  66. {
  67. if (NULL != (*data_to_free)->csn)
  68. {
  69. csn_free(&(*data_to_free)->csn);
  70. }
  71. slapi_ch_free((void **)data_to_free);
  72. }
  73. }
  74. void csnplFree (CSNPL **csnpl)
  75. {
  76. if ((csnpl == NULL) || (*csnpl == NULL))
  77. return;
  78. /* free all remaining nodes */
  79. llistDestroy (&((*csnpl)->csnList), (FNFree)csnpldata_free);
  80. if ((*csnpl)->csnLock)
  81. slapi_destroy_rwlock ((*csnpl)->csnLock);
  82. slapi_ch_free ((void**)csnpl);
  83. }
  84. /* This function isnerts a CSN into the pending list
  85. * Returns: 0 if the csn was successfully inserted
  86. * 1 if the csn has already been seen
  87. * -1 for any other kind of errors
  88. */
  89. int csnplInsert (CSNPL *csnpl, const CSN *csn, const CSNPL_CTX *prim_csn)
  90. {
  91. int rc;
  92. csnpldata *csnplnode;
  93. char csn_str[CSN_STRSIZE];
  94. if (csnpl == NULL || csn == NULL)
  95. {
  96. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  97. "csnplInsert - Invalid argument\n");
  98. return -1;
  99. }
  100. slapi_rwlock_wrlock (csnpl->csnLock);
  101. /* check to see if this csn is larger than the last csn in the
  102. pending list. It has to be if we have not seen it since
  103. the csns are always added in the accending order. */
  104. csnplnode = llistGetTail (csnpl->csnList);
  105. if (csnplnode && csn_compare (csnplnode->csn, csn) >= 0)
  106. {
  107. slapi_rwlock_unlock (csnpl->csnLock);
  108. return 1;
  109. }
  110. csnplnode = (csnpldata *)slapi_ch_calloc(1, sizeof(csnpldata));
  111. csnplnode->committed = PR_FALSE;
  112. csnplnode->csn = csn_dup(csn);
  113. if (prim_csn) {
  114. csnplnode->prim_csn = prim_csn->prim_csn;
  115. csnplnode->prim_replica = prim_csn->prim_repl;
  116. }
  117. csn_as_string(csn, PR_FALSE, csn_str);
  118. rc = llistInsertTail (csnpl->csnList, csn_str, csnplnode);
  119. #ifdef DEBUG
  120. _csnplDumpContentNoLock(csnpl, "csnplInsert");
  121. #endif
  122. slapi_rwlock_unlock (csnpl->csnLock);
  123. if (rc != 0)
  124. {
  125. char s[CSN_STRSIZE];
  126. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  127. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  128. "csnplInsert - Failed to insert csn (%s) into pending list\n", csn_as_string(csn,PR_FALSE,s));
  129. }
  130. return -1;
  131. }
  132. return 0;
  133. }
  134. int csnplRemove (CSNPL *csnpl, const CSN *csn)
  135. {
  136. csnpldata *data;
  137. char csn_str[CSN_STRSIZE];
  138. if (csnpl == NULL || csn == NULL)
  139. {
  140. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  141. "csnplRemove: invalid argument\n");
  142. return -1;
  143. }
  144. csn_as_string(csn, PR_FALSE, csn_str);
  145. slapi_rwlock_wrlock (csnpl->csnLock);
  146. data = (csnpldata *)llistRemove (csnpl->csnList, csn_str);
  147. if (data == NULL)
  148. {
  149. slapi_rwlock_unlock (csnpl->csnLock);
  150. return -1;
  151. }
  152. #ifdef DEBUG
  153. _csnplDumpContentNoLock(csnpl, "csnplRemove");
  154. #endif
  155. csn_free(&data->csn);
  156. slapi_ch_free((void **)&data);
  157. slapi_rwlock_unlock (csnpl->csnLock);
  158. return 0;
  159. }
  160. PRBool csn_primary(Replica *replica, const CSN *csn, const CSNPL_CTX *csn_ctx)
  161. {
  162. if (csn_ctx == NULL)
  163. return PR_FALSE;
  164. if (replica != csn_ctx->prim_repl) {
  165. /* The CSNs are not from the same replication topology
  166. * so even if the csn values are equal they are not related
  167. * to the same operation
  168. */
  169. return PR_FALSE;
  170. }
  171. /* Here the two CSNs belong to the same replication topology */
  172. /* check if the CSN identifies the primary update */
  173. if (csn_is_equal(csn, csn_ctx->prim_csn)) {
  174. return PR_TRUE;
  175. }
  176. return PR_FALSE;
  177. }
  178. static PRBool csn_primary_or_nested(csnpldata *csn_data, const CSNPL_CTX *csn_ctx)
  179. {
  180. if ((csn_data == NULL) || (csn_ctx == NULL))
  181. return PR_FALSE;
  182. if (csn_data->prim_replica != csn_ctx->prim_repl) {
  183. /* The CSNs are not from the same replication topology
  184. * so even if the csn values are equal they are not related
  185. * to the same operation
  186. */
  187. return PR_FALSE;
  188. }
  189. /* Here the two CSNs belong to the same replication topology */
  190. /* First check if the CSN identifies the primary update */
  191. if (csn_is_equal(csn_data->csn, csn_ctx->prim_csn)) {
  192. return PR_TRUE;
  193. }
  194. /* Second check if the CSN identifies a nested update */
  195. if (csn_is_equal(csn_data->prim_csn, csn_ctx->prim_csn)) {
  196. return PR_TRUE;
  197. }
  198. return PR_FALSE;
  199. }
  200. int csnplRemoveAll (CSNPL *csnpl, const CSNPL_CTX *csn_ctx)
  201. {
  202. csnpldata *data;
  203. void *iterator;
  204. slapi_rwlock_wrlock (csnpl->csnLock);
  205. data = (csnpldata *)llistGetFirst(csnpl->csnList, &iterator);
  206. while (NULL != data)
  207. {
  208. if (csn_primary_or_nested(data, csn_ctx)) {
  209. csnpldata_free(&data);
  210. data = (csnpldata *)llistRemoveCurrentAndGetNext(csnpl->csnList, &iterator);
  211. } else {
  212. data = (csnpldata *)llistGetNext (csnpl->csnList, &iterator);
  213. }
  214. }
  215. #ifdef DEBUG
  216. _csnplDumpContentNoLock(csnpl, "csnplRemoveAll");
  217. #endif
  218. slapi_rwlock_unlock (csnpl->csnLock);
  219. return 0;
  220. }
  221. int csnplCommitAll (CSNPL *csnpl, const CSNPL_CTX *csn_ctx)
  222. {
  223. csnpldata *data;
  224. void *iterator;
  225. char csn_str[CSN_STRSIZE];
  226. csn_as_string(csn_ctx->prim_csn, PR_FALSE, csn_str);
  227. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  228. "csnplCommitALL: committing all csns for csn %s\n", csn_str);
  229. slapi_rwlock_wrlock (csnpl->csnLock);
  230. data = (csnpldata *)llistGetFirst(csnpl->csnList, &iterator);
  231. while (NULL != data)
  232. {
  233. csn_as_string(data->csn, PR_FALSE, csn_str);
  234. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name,
  235. "csnplCommitALL: processing data csn %s\n", csn_str);
  236. if (csn_primary_or_nested(data, csn_ctx)) {
  237. data->committed = PR_TRUE;
  238. }
  239. data = (csnpldata *)llistGetNext (csnpl->csnList, &iterator);
  240. }
  241. slapi_rwlock_unlock (csnpl->csnLock);
  242. return 0;
  243. }
  244. int csnplCommit (CSNPL *csnpl, const CSN *csn)
  245. {
  246. csnpldata *data;
  247. char csn_str[CSN_STRSIZE];
  248. if (csnpl == NULL || csn == NULL)
  249. {
  250. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  251. "csnplCommit: invalid argument\n");
  252. return -1;
  253. }
  254. csn_as_string(csn, PR_FALSE, csn_str);
  255. slapi_rwlock_wrlock (csnpl->csnLock);
  256. #ifdef DEBUG
  257. _csnplDumpContentNoLock(csnpl, "csnplCommit");
  258. #endif
  259. data = (csnpldata*)llistGet (csnpl->csnList, csn_str);
  260. if (data == NULL)
  261. {
  262. /*
  263. * In the scenario "4.x master -> 6.x legacy-consumer -> 6.x consumer"
  264. * csn will have rid=65535. Hence 6.x consumer will get here trying
  265. * to commit r->min_csn_pl because its rid matches that in the csn.
  266. * However, r->min_csn_pl is always empty for a dedicated consumer.
  267. * Exclude READ-ONLY replica ID here from error logging.
  268. */
  269. ReplicaId rid = csn_get_replicaid (csn);
  270. if (rid < MAX_REPLICA_ID)
  271. {
  272. slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
  273. "csnplCommit: can't find csn %s\n", csn_str);
  274. }
  275. slapi_rwlock_unlock (csnpl->csnLock);
  276. return -1;
  277. }
  278. else
  279. {
  280. data->committed = PR_TRUE;
  281. }
  282. slapi_rwlock_unlock (csnpl->csnLock);
  283. return 0;
  284. }
  285. CSN* csnplGetMinCSN (CSNPL *csnpl, PRBool *committed)
  286. {
  287. csnpldata *data;
  288. CSN *csn = NULL;
  289. slapi_rwlock_rdlock (csnpl->csnLock);
  290. if ((data = (csnpldata*)llistGetHead (csnpl->csnList)) != NULL)
  291. {
  292. csn = csn_dup(data->csn);
  293. if (NULL != committed)
  294. {
  295. *committed = data->committed;
  296. }
  297. }
  298. slapi_rwlock_unlock (csnpl->csnLock);
  299. return csn;
  300. }
  301. /*
  302. * Roll up the list of pending CSNs, removing all of the CSNs at the
  303. * head of the the list that are committed and contiguous. Returns
  304. * the largest committed CSN, or NULL if no contiguous block of
  305. * committed CSNs appears at the beginning of the list. The caller
  306. * is responsible for freeing the CSN returned.
  307. */
  308. CSN *
  309. csnplRollUp(CSNPL *csnpl, CSN **first_commited)
  310. {
  311. CSN *largest_committed_csn = NULL;
  312. csnpldata *data;
  313. PRBool freeit = PR_TRUE;
  314. void *iterator;
  315. slapi_rwlock_wrlock (csnpl->csnLock);
  316. if (first_commited) {
  317. /* Avoid non-initialization issues due to careless callers */
  318. *first_commited = NULL;
  319. }
  320. data = (csnpldata *)llistGetFirst(csnpl->csnList, &iterator);
  321. while (NULL != data && data->committed)
  322. {
  323. if (NULL != largest_committed_csn && freeit)
  324. {
  325. csn_free(&largest_committed_csn);
  326. }
  327. freeit = PR_TRUE;
  328. largest_committed_csn = data->csn; /* Save it */
  329. if (first_commited && (*first_commited == NULL)) {
  330. *first_commited = data->csn;
  331. freeit = PR_FALSE;
  332. }
  333. /* llistRemoveCurrentAndGetNext will detach the current node
  334. so we have to free the data associated with it, but not the csn */
  335. data->csn = NULL;
  336. csnpldata_free(&data);
  337. data = (csnpldata *)llistRemoveCurrentAndGetNext(csnpl->csnList, &iterator);
  338. }
  339. #ifdef DEBUG
  340. _csnplDumpContentNoLock(csnpl, "csnplRollUp");
  341. #endif
  342. slapi_rwlock_unlock (csnpl->csnLock);
  343. return largest_committed_csn;
  344. }
  345. #ifdef DEBUG
  346. /* Dump current content of the list - for debugging */
  347. void
  348. csnplDumpContent(CSNPL *csnpl, const char *caller)
  349. {
  350. if (csnpl)
  351. {
  352. slapi_rwlock_rdlock (csnpl->csnLock);
  353. _csnplDumpContentNoLock (csnpl, caller);
  354. slapi_rwlock_unlock (csnpl->csnLock);
  355. }
  356. }
  357. /* helper function */
  358. static void _csnplDumpContentNoLock(CSNPL *csnpl, const char *caller)
  359. {
  360. csnpldata *data;
  361. void *iterator;
  362. char csn_str[CSN_STRSIZE];
  363. char primcsn_str[CSN_STRSIZE];
  364. data = (csnpldata *)llistGetFirst(csnpl->csnList, &iterator);
  365. if (data) {
  366. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "%s: CSN Pending list content:\n",
  367. caller ? caller : "");
  368. }
  369. while (data)
  370. {
  371. slapi_log_err(SLAPI_LOG_REPL, repl_plugin_name, "%s,(prim %s), %s\n",
  372. csn_as_string(data->csn, PR_FALSE, csn_str),
  373. data->prim_csn ? csn_as_string(data->prim_csn, PR_FALSE, primcsn_str) : " ",
  374. data->committed ? "committed" : "not committed");
  375. data = (csnpldata *)llistGetNext (csnpl->csnList, &iterator);
  376. }
  377. }
  378. #endif
  379. /* wrapper around csn_free, to satisfy NSPR thread context API */
  380. void
  381. csnplFreeCSNPL_CTX (void *arg)
  382. {
  383. CSNPL_CTX *csnpl_ctx = (CSNPL_CTX *)arg;
  384. csn_free(&csnpl_ctx->prim_csn);
  385. if (csnpl_ctx->sec_repl) {
  386. slapi_ch_free((void **)&csnpl_ctx->sec_repl);
  387. }
  388. slapi_ch_free((void **)&csnpl_ctx);
  389. }