repl5_ruv.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  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. * Copyright (C) 2010 Hewlett-Packard Development Company, L.P.
  37. * All rights reserved.
  38. * END COPYRIGHT BLOCK **/
  39. #ifdef HAVE_CONFIG_H
  40. # include <config.h>
  41. #endif
  42. /* repl5_ruv.c - implementation of replica update vector */
  43. /*
  44. * The replica update vector is stored in the nsds50ruv attribute. The LDIF
  45. * representation of the ruv is:
  46. * nsds50ruv: {replicageneration} <gen-id-for-this-replica>
  47. * nsds50ruv: {replica <rid>[ <url>]}[ <mincsn> <maxcsn>]
  48. * nsds50ruv: {replica <rid>[ <url>]}[ <mincsn> <maxcsn>]
  49. * ...
  50. * nsds50ruv: {replica <rid>[ <url>]}[ <mincsn> <maxcsn>]
  51. *
  52. * nsruvReplicaLastModified: {replica <rid>[ <url>]} lastModifiedTime
  53. * nsruvReplicaLastModified: {replica <rid>[ <url>]} lastModifiedTime
  54. * ...
  55. *
  56. * For readability, ruv_dump appends nsruvReplicaLastModified to nsds50ruv:
  57. * nsds50ruv: {replica <rid>[ <url>]}[ <mincsn> <maxcsn> [<lastModifiedTime>]]
  58. */
  59. #include <string.h>
  60. #include <ctype.h> /* For isdigit() */
  61. #include "csnpl.h"
  62. #include "repl5_ruv.h"
  63. #include "repl_shared.h"
  64. #include "repl5.h"
  65. #define RIDSTR_SIZE 16 /* string large enough to hold replicaid*/
  66. #define RUVSTR_SIZE 256 /* string large enough to hold ruv and lastmodifiedtime */
  67. /* Data Structures */
  68. /* replica */
  69. typedef struct ruvElement
  70. {
  71. ReplicaId rid; /* replica id for this element */
  72. CSN *csn; /* largest csn that we know about that originated at the master */
  73. CSN *min_csn; /* smallest csn that originated at the master */
  74. char *replica_purl; /* Partial URL for replica */
  75. CSNPL *csnpl; /* list of operations in progress */
  76. time_t last_modified; /* timestamp the modification of csn */
  77. } RUVElement;
  78. /* replica update vector */
  79. struct _ruv
  80. {
  81. char *replGen; /* replicated area generation: identifies replica
  82. in space and in time */
  83. DataList *elements; /* replicas */
  84. PRRWLock *lock; /* concurrency control */
  85. };
  86. /* forward declarations */
  87. static int ruvInit (RUV **ruv, int initCount);
  88. static void ruvFreeReplica (void **data);
  89. static RUVElement* ruvGetReplica (const RUV *ruv, ReplicaId rid);
  90. static RUVElement* ruvAddReplica (RUV *ruv, const CSN *csn, const char *replica_purl);
  91. static RUVElement* ruvAddReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl);
  92. static RUVElement* ruvAddIndexReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl, int index);
  93. static int ruvReplicaCompare (const void *el1, const void *el2);
  94. static RUVElement *get_ruvelement_from_berval(const struct berval *bval);
  95. static char *get_replgen_from_berval(const struct berval *bval);
  96. static const char * const prefix_replicageneration = "{replicageneration}";
  97. static const char * const prefix_ruvcsn = "{replica "; /* intentionally missing '}' */
  98. /* API implementation */
  99. /*
  100. * Allocate a new ruv and set its replica generation to the given generation.
  101. */
  102. int
  103. ruv_init_new(const char *replGen, ReplicaId rid, const char *purl, RUV **ruv)
  104. {
  105. int rc;
  106. RUVElement *replica;
  107. if (ruv == NULL || replGen == NULL)
  108. {
  109. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_init_new: NULL argument\n");
  110. return RUV_BAD_DATA;
  111. }
  112. rc = ruvInit (ruv, 0);
  113. if (rc != RUV_SUCCESS)
  114. return rc;
  115. (*ruv)->replGen = slapi_ch_strdup (replGen);
  116. /* we want to add the local writable replica to the RUV before any csns are created */
  117. /* this is so that it can be referred to even before it accepted any changes */
  118. if (purl)
  119. {
  120. replica = ruvAddReplicaNoCSN (*ruv, rid, purl);
  121. if (replica == NULL)
  122. return RUV_MEMORY_ERROR;
  123. }
  124. return RUV_SUCCESS;
  125. }
  126. int ruv_private_new(RUV **ruv, RUV *clone )
  127. {
  128. int rc;
  129. rc = ruvInit (ruv, dl_get_count(clone->elements) );
  130. if (rc != RUV_SUCCESS)
  131. return rc;
  132. (*ruv)->replGen = slapi_ch_strdup (clone->replGen);
  133. return RUV_SUCCESS;
  134. }
  135. /*
  136. * Create a new RUV and initialize its contents from the provided Slapi_Attr.
  137. * Returns:
  138. * RUV_BAD_DATA if the values in the attribute were malformed.
  139. * RUV_SUCCESS if all went well
  140. */
  141. int
  142. ruv_init_from_slapi_attr(Slapi_Attr *attr, RUV **ruv)
  143. {
  144. ReplicaId dummy = 0;
  145. return (ruv_init_from_slapi_attr_and_check_purl(attr, ruv, &dummy));
  146. }
  147. /*
  148. * Create a new RUV and initialize its contents from the provided Slapi_Attr.
  149. * Returns:
  150. * RUV_BAD_DATA if the values in the attribute were malformed.
  151. * RUV_SUCCESS if all went well
  152. * contain_purl is 0 if the ruv doesn't contain the local purl
  153. * contain_purl is != 0 if the ruv contains the local purl (contains the rid)
  154. */
  155. int
  156. ruv_init_from_slapi_attr_and_check_purl(Slapi_Attr *attr, RUV **ruv, ReplicaId *contain_purl)
  157. {
  158. int return_value;
  159. PR_ASSERT(NULL != attr && NULL != ruv);
  160. if (NULL == ruv || NULL == attr)
  161. {
  162. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  163. "ruv_init_from_slapi_attr: NULL argument\n");
  164. return_value = RUV_BAD_DATA;
  165. }
  166. else
  167. {
  168. int rc;
  169. int numvalues;
  170. slapi_attr_get_numvalues(attr, &numvalues);
  171. if ((rc = ruvInit(ruv, numvalues)) != RUV_SUCCESS)
  172. {
  173. return_value = rc;
  174. }
  175. else
  176. {
  177. int hint;
  178. Slapi_Value *value;
  179. const struct berval *bval;
  180. const char *purl = NULL;
  181. return_value = RUV_SUCCESS;
  182. purl = multimaster_get_local_purl();
  183. *contain_purl = 0;
  184. for (hint = slapi_attr_first_value(attr, &value);
  185. hint != -1; hint = slapi_attr_next_value(attr, hint, &value))
  186. {
  187. bval = slapi_value_get_berval(value);
  188. if (NULL != bval && NULL != bval->bv_val)
  189. {
  190. if (strncmp(bval->bv_val, prefix_replicageneration, strlen(prefix_replicageneration)) == 0) {
  191. if (NULL == (*ruv)->replGen)
  192. {
  193. (*ruv)->replGen = get_replgen_from_berval(bval);
  194. } else {
  195. /* Twice replicageneration is wrong, just log and ignore */
  196. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  197. "ruv_init_from_slapi_attr: %s is present more than once\n",
  198. prefix_replicageneration);
  199. }
  200. }
  201. else
  202. {
  203. RUVElement *ruve = get_ruvelement_from_berval(bval);
  204. if (NULL != ruve)
  205. {
  206. /* Is the local purl already in the ruv ? */
  207. if ( (*contain_purl==0) && ruve->replica_purl && purl && (strncmp(ruve->replica_purl, purl, strlen(purl))==0) )
  208. {
  209. *contain_purl = ruve->rid;
  210. }
  211. dl_add ((*ruv)->elements, ruve);
  212. }
  213. }
  214. }
  215. }
  216. }
  217. }
  218. return return_value;
  219. }
  220. /*
  221. * Same as ruv_init_from_slapi_attr, but takes an array of pointers to bervals.
  222. * I wish this wasn't a cut-n-paste of the above function, but I don't see a
  223. * clean way to define one API in terms of the other.
  224. */
  225. int
  226. ruv_init_from_bervals(struct berval **vals, RUV **ruv)
  227. {
  228. int return_value;
  229. PR_ASSERT(NULL != vals && NULL != ruv);
  230. if (NULL == ruv || NULL == vals)
  231. {
  232. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  233. "ruv_init_from_slapi_value: NULL argument\n");
  234. return_value = RUV_BAD_DATA;
  235. }
  236. else
  237. {
  238. int i, rc;
  239. i = 0;
  240. while (vals[i] != NULL)
  241. {
  242. i++;
  243. }
  244. if ((rc = ruvInit (ruv, i)) != RUV_SUCCESS)
  245. {
  246. return_value = rc;
  247. }
  248. else
  249. {
  250. return_value = RUV_SUCCESS;
  251. for (i = 0; NULL != vals[i]; i++)
  252. {
  253. if (NULL != vals[i]->bv_val)
  254. {
  255. if (strncmp(vals[i]->bv_val, prefix_replicageneration, strlen(prefix_replicageneration)) == 0) {
  256. if (NULL == (*ruv)->replGen)
  257. {
  258. (*ruv)->replGen = get_replgen_from_berval(vals[i]);
  259. } else {
  260. /* Twice replicageneration is wrong, just log and ignore */
  261. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  262. "ruv_init_from_slapi_value: %s is present more than once\n",
  263. prefix_replicageneration);
  264. }
  265. }
  266. else
  267. {
  268. RUVElement *ruve = get_ruvelement_from_berval(vals[i]);
  269. if (NULL != ruve)
  270. {
  271. dl_add ((*ruv)->elements, ruve);
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. return return_value;
  279. }
  280. RUV*
  281. ruv_dup (const RUV *ruv)
  282. {
  283. int rc;
  284. RUVElement *replica, *dupReplica;
  285. int cookie;
  286. RUV *dupRUV = NULL;
  287. if (ruv == NULL)
  288. return NULL;
  289. PR_RWLock_Rlock (ruv->lock);
  290. rc = ruvInit (&dupRUV, dl_get_count (ruv->elements));
  291. if (rc != RUV_SUCCESS || dupRUV == NULL)
  292. goto done;
  293. dupRUV->replGen = slapi_ch_strdup (ruv->replGen);
  294. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  295. replica = dl_get_next (ruv->elements, &cookie))
  296. {
  297. dupReplica = (RUVElement *)slapi_ch_calloc (1, sizeof (*dupReplica));
  298. dupReplica->rid = replica->rid;
  299. if (replica->csn)
  300. dupReplica->csn = csn_dup (replica->csn);
  301. if (replica->min_csn)
  302. dupReplica->min_csn = csn_dup (replica->min_csn);
  303. if (replica->replica_purl)
  304. dupReplica->replica_purl = slapi_ch_strdup (replica->replica_purl);
  305. dupReplica->last_modified = replica->last_modified;
  306. /* ONREPL - we don't make copy of the pernding list. For now
  307. we don't need it. */
  308. dl_add (dupRUV->elements, dupReplica);
  309. }
  310. done:
  311. PR_RWLock_Unlock (ruv->lock);
  312. return dupRUV;
  313. }
  314. void
  315. ruv_destroy (RUV **ruv)
  316. {
  317. if (ruv != NULL && *ruv != NULL)
  318. {
  319. if ((*ruv)->elements)
  320. {
  321. dl_cleanup ((*ruv)->elements, ruvFreeReplica);
  322. dl_free (&(*ruv)->elements);
  323. }
  324. /* slapi_ch_free accepts NULL pointer */
  325. slapi_ch_free ((void **)&((*ruv)->replGen));
  326. if ((*ruv)->lock)
  327. {
  328. PR_DestroyRWLock ((*ruv)->lock);
  329. }
  330. slapi_ch_free ((void**)ruv);
  331. }
  332. }
  333. /*
  334. * [610948]
  335. * copy elements in srcruv to destruv
  336. * destruv is the live wrapper, which could be referred by other threads.
  337. * srcruv is cleaned up after copied.
  338. */
  339. void
  340. ruv_copy_and_destroy (RUV **srcruv, RUV **destruv)
  341. {
  342. DataList *elemp = NULL;
  343. char *replgp = NULL;
  344. if (NULL == srcruv || NULL == *srcruv || NULL == destruv)
  345. {
  346. return;
  347. }
  348. if (NULL == *destruv)
  349. {
  350. *destruv = *srcruv;
  351. *srcruv = NULL;
  352. }
  353. else
  354. {
  355. PR_RWLock_Wlock((*destruv)->lock);
  356. elemp = (*destruv)->elements;
  357. (*destruv)->elements = (*srcruv)->elements;
  358. if (elemp)
  359. {
  360. dl_cleanup (elemp, ruvFreeReplica);
  361. dl_free (&elemp);
  362. }
  363. /* slapi_ch_free accepts NULL pointer */
  364. replgp = (*destruv)->replGen;
  365. (*destruv)->replGen = (*srcruv)->replGen;
  366. slapi_ch_free ((void **)&replgp);
  367. if ((*srcruv)->lock)
  368. {
  369. PR_DestroyRWLock ((*srcruv)->lock);
  370. }
  371. slapi_ch_free ((void**)srcruv);
  372. PR_RWLock_Unlock((*destruv)->lock);
  373. }
  374. PR_ASSERT (*destruv != NULL && *srcruv == NULL);
  375. }
  376. int
  377. ruv_delete_replica (RUV *ruv, ReplicaId rid)
  378. {
  379. int return_value;
  380. if (ruv == NULL)
  381. {
  382. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_delete_replica: NULL argument\n");
  383. return_value = RUV_BAD_DATA;
  384. }
  385. else
  386. {
  387. /* check for duplicates */
  388. PR_RWLock_Wlock (ruv->lock);
  389. dl_delete (ruv->elements, (const void*)&rid, ruvReplicaCompare, ruvFreeReplica);
  390. PR_RWLock_Unlock (ruv->lock);
  391. return_value = RUV_SUCCESS;
  392. }
  393. return return_value;
  394. }
  395. int
  396. ruv_add_replica (RUV *ruv, ReplicaId rid, const char *replica_purl)
  397. {
  398. RUVElement* replica;
  399. PR_ASSERT (ruv && replica_purl);
  400. PR_RWLock_Wlock (ruv->lock);
  401. replica = ruvGetReplica (ruv, rid);
  402. if (replica == NULL)
  403. {
  404. replica = ruvAddReplicaNoCSN (ruv, rid, replica_purl);
  405. }
  406. PR_RWLock_Unlock (ruv->lock);
  407. if (replica)
  408. return RUV_SUCCESS;
  409. else
  410. return RUV_MEMORY_ERROR;
  411. }
  412. int
  413. ruv_replace_replica_purl (RUV *ruv, ReplicaId rid, const char *replica_purl)
  414. {
  415. RUVElement* replica;
  416. int rc = RUV_NOTFOUND;
  417. PR_ASSERT (ruv && replica_purl);
  418. PR_RWLock_Wlock (ruv->lock);
  419. replica = ruvGetReplica (ruv, rid);
  420. if (replica != NULL)
  421. {
  422. slapi_ch_free((void **)&(replica->replica_purl));
  423. replica->replica_purl = slapi_ch_strdup(replica_purl);
  424. rc = RUV_SUCCESS;
  425. }
  426. PR_RWLock_Unlock (ruv->lock);
  427. return rc;
  428. }
  429. int
  430. ruv_add_index_replica (RUV *ruv, ReplicaId rid, const char *replica_purl, int index)
  431. {
  432. RUVElement* replica;
  433. PR_ASSERT (ruv && replica_purl);
  434. PR_RWLock_Wlock (ruv->lock);
  435. replica = ruvGetReplica (ruv, rid);
  436. if (replica == NULL)
  437. {
  438. replica = ruvAddIndexReplicaNoCSN (ruv, rid, replica_purl, index);
  439. }
  440. PR_RWLock_Unlock (ruv->lock);
  441. if (replica)
  442. return RUV_SUCCESS;
  443. else
  444. return RUV_MEMORY_ERROR;
  445. }
  446. PRBool
  447. ruv_contains_replica (const RUV *ruv, ReplicaId rid)
  448. {
  449. RUVElement *replica;
  450. if (ruv == NULL)
  451. return PR_FALSE;
  452. PR_RWLock_Rlock (ruv->lock);
  453. replica = ruvGetReplica (ruv, rid);
  454. PR_RWLock_Unlock (ruv->lock);
  455. return replica != NULL;
  456. }
  457. #define GET_LARGEST_CSN 231
  458. #define GET_SMALLEST_CSN 232
  459. static int
  460. get_csn_internal(const RUV *ruv, ReplicaId rid, CSN **csn, int whichone)
  461. {
  462. RUVElement *replica;
  463. int return_value = RUV_SUCCESS;
  464. if (ruv == NULL || csn == NULL)
  465. {
  466. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_get_largest_csn_for_replica: NULL argument\n");
  467. return_value = RUV_BAD_DATA;
  468. }
  469. else
  470. {
  471. *csn = NULL;
  472. /* prevent element from being destroyed while we get its data */
  473. PR_RWLock_Rlock (ruv->lock);
  474. replica = ruvGetReplica (ruv, rid);
  475. /* replica without min csn is treated as a non-existent replica */
  476. if (replica == NULL || replica->min_csn == NULL)
  477. {
  478. return_value = RUV_NOTFOUND;
  479. }
  480. else
  481. {
  482. switch (whichone)
  483. {
  484. case GET_LARGEST_CSN:
  485. *csn = replica->csn ? csn_dup (replica->csn) : NULL;
  486. break;
  487. case GET_SMALLEST_CSN:
  488. *csn = replica->min_csn ? csn_dup (replica->min_csn) : NULL;
  489. break;
  490. default:
  491. *csn = NULL;
  492. }
  493. }
  494. PR_RWLock_Unlock (ruv->lock);
  495. }
  496. return return_value;
  497. }
  498. int
  499. ruv_get_largest_csn_for_replica(const RUV *ruv, ReplicaId rid, CSN **csn)
  500. {
  501. return get_csn_internal(ruv, rid, csn, GET_LARGEST_CSN);
  502. }
  503. int
  504. ruv_get_smallest_csn_for_replica(const RUV *ruv, ReplicaId rid, CSN **csn)
  505. {
  506. return get_csn_internal(ruv, rid, csn, GET_SMALLEST_CSN);
  507. }
  508. const char *
  509. ruv_get_purl_for_replica(const RUV *ruv, ReplicaId rid)
  510. {
  511. RUVElement *replica;
  512. const char *return_value = NULL;
  513. PR_RWLock_Rlock (ruv->lock);
  514. replica = ruvGetReplica (ruv, rid);
  515. if (replica != NULL)
  516. {
  517. return_value = replica->replica_purl;
  518. }
  519. PR_RWLock_Unlock (ruv->lock);
  520. return return_value;
  521. }
  522. static int
  523. set_min_csn_nolock(RUV *ruv, const CSN *min_csn, const char *replica_purl)
  524. {
  525. int return_value;
  526. ReplicaId rid = csn_get_replicaid (min_csn);
  527. RUVElement *replica = ruvGetReplica (ruv, rid);
  528. if (NULL == replica)
  529. {
  530. replica = ruvAddReplica (ruv, min_csn, replica_purl);
  531. if (replica)
  532. return_value = RUV_SUCCESS;
  533. else
  534. return_value = RUV_MEMORY_ERROR;
  535. }
  536. else
  537. {
  538. if (replica->min_csn == NULL || csn_compare (min_csn, replica->min_csn) < 0)
  539. {
  540. csn_free(&replica->min_csn);
  541. replica->min_csn = csn_dup(min_csn);
  542. }
  543. return_value = RUV_SUCCESS;
  544. }
  545. return return_value;
  546. }
  547. static int
  548. set_max_csn_nolock(RUV *ruv, const CSN *max_csn, const char *replica_purl)
  549. {
  550. int return_value;
  551. ReplicaId rid = csn_get_replicaid (max_csn);
  552. RUVElement *replica = ruvGetReplica (ruv, rid);
  553. if (NULL == replica)
  554. {
  555. replica = ruvAddReplica (ruv, max_csn, replica_purl);
  556. if (replica)
  557. return_value = RUV_SUCCESS;
  558. else
  559. return_value = RUV_MEMORY_ERROR;
  560. }
  561. else
  562. {
  563. if (replica_purl && replica->replica_purl == NULL)
  564. replica->replica_purl = slapi_ch_strdup (replica_purl);
  565. csn_free(&replica->csn);
  566. replica->csn = csn_dup(max_csn);
  567. replica->last_modified = current_time();
  568. return_value = RUV_SUCCESS;
  569. }
  570. return return_value;
  571. }
  572. int
  573. ruv_set_min_csn(RUV *ruv, const CSN *min_csn, const char *replica_purl)
  574. {
  575. int return_value;
  576. PR_RWLock_Wlock (ruv->lock);
  577. return_value = set_min_csn_nolock(ruv, min_csn, replica_purl);
  578. PR_RWLock_Unlock (ruv->lock);
  579. return return_value;
  580. }
  581. int
  582. ruv_set_max_csn(RUV *ruv, const CSN *max_csn, const char *replica_purl)
  583. {
  584. int return_value;
  585. PR_RWLock_Wlock (ruv->lock);
  586. return_value = set_max_csn_nolock(ruv, max_csn, replica_purl);
  587. PR_RWLock_Unlock (ruv->lock);
  588. return return_value;
  589. }
  590. int
  591. ruv_set_csns(RUV *ruv, const CSN *csn, const char *replica_purl)
  592. {
  593. RUVElement *replica;
  594. ReplicaId rid;
  595. int return_value;
  596. if (ruv == NULL || csn == NULL)
  597. {
  598. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_set_csns: NULL argument\n");
  599. return_value = RUV_BAD_DATA;
  600. }
  601. else
  602. {
  603. rid = csn_get_replicaid (csn);
  604. /* prevent element from being destroyed while we get its data */
  605. PR_RWLock_Wlock (ruv->lock);
  606. replica = ruvGetReplica (ruv, rid);
  607. if (replica == NULL) /* add new replica */
  608. {
  609. replica = ruvAddReplica (ruv, csn, replica_purl);
  610. if (replica)
  611. return_value = RUV_SUCCESS;
  612. else
  613. return_value = RUV_MEMORY_ERROR;
  614. }
  615. else
  616. {
  617. if (csn_compare (csn, replica->csn) > 0)
  618. {
  619. if (replica->csn != NULL)
  620. {
  621. csn_init_by_csn ( replica->csn, csn );
  622. }
  623. else
  624. {
  625. replica->csn = csn_dup(csn);
  626. }
  627. replica->last_modified = current_time();
  628. if (replica_purl && (NULL == replica->replica_purl ||
  629. strcmp(replica->replica_purl, replica_purl) != 0))
  630. {
  631. /* slapi_ch_free accepts NULL pointer */
  632. slapi_ch_free((void **)&replica->replica_purl);
  633. replica->replica_purl = slapi_ch_strdup(replica_purl);
  634. }
  635. }
  636. /* XXXggood only need to worry about this if real min csn not committed to changelog yet */
  637. if (csn_compare (csn, replica->min_csn) < 0)
  638. {
  639. csn_free(&replica->min_csn);
  640. replica->min_csn = csn_dup(csn);
  641. }
  642. return_value = RUV_SUCCESS;
  643. }
  644. PR_RWLock_Unlock (ruv->lock);
  645. }
  646. return return_value;
  647. }
  648. /* This function, for each replica keeps the smallest CSN its seen so far.
  649. Used for initial setup of changelog purge vector */
  650. int
  651. ruv_set_csns_keep_smallest(RUV *ruv, const CSN *csn)
  652. {
  653. RUVElement *replica;
  654. ReplicaId rid;
  655. int return_value;
  656. if (ruv == NULL || csn == NULL)
  657. {
  658. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  659. "ruv_set_csns_keep_smallest: NULL argument\n");
  660. return_value = RUV_BAD_DATA;
  661. }
  662. else
  663. {
  664. rid = csn_get_replicaid (csn);
  665. /* prevent element from being destroyed while we get its data */
  666. PR_RWLock_Wlock (ruv->lock);
  667. replica = ruvGetReplica (ruv, rid);
  668. if (replica == NULL) /* add new replica */
  669. {
  670. replica = ruvAddReplica (ruv, csn, NULL);
  671. if (replica)
  672. return_value = RUV_SUCCESS;
  673. else
  674. return_value = RUV_MEMORY_ERROR;
  675. }
  676. else
  677. {
  678. if (csn_compare (csn, replica->csn) < 0)
  679. {
  680. csn_free(&replica->csn);
  681. replica->csn = csn_dup(csn);
  682. replica->last_modified = current_time();
  683. }
  684. return_value = RUV_SUCCESS;
  685. }
  686. PR_RWLock_Unlock (ruv->lock);
  687. }
  688. return return_value;
  689. }
  690. void
  691. ruv_set_replica_generation(RUV *ruv, const char *csnstr)
  692. {
  693. if (NULL != csnstr && NULL != ruv)
  694. {
  695. PR_RWLock_Wlock (ruv->lock);
  696. if (NULL != ruv->replGen)
  697. {
  698. slapi_ch_free((void **)&ruv->replGen);
  699. }
  700. ruv->replGen = slapi_ch_strdup(csnstr);
  701. PR_RWLock_Unlock (ruv->lock);
  702. }
  703. }
  704. char *
  705. ruv_get_replica_generation(const RUV *ruv)
  706. {
  707. char *return_str = NULL;
  708. if (!ruv) {
  709. return return_str;
  710. }
  711. PR_RWLock_Rlock (ruv->lock);
  712. if (ruv != NULL && ruv->replGen != NULL)
  713. {
  714. return_str = slapi_ch_strdup(ruv->replGen);
  715. }
  716. PR_RWLock_Unlock (ruv->lock);
  717. return return_str;
  718. }
  719. static PRBool
  720. ruv_covers_csn_internal(const RUV *ruv, const CSN *csn, PRBool strict)
  721. {
  722. RUVElement *replica;
  723. ReplicaId rid;
  724. PRBool return_value;
  725. if (ruv == NULL || csn == NULL)
  726. {
  727. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn: NULL argument\n");
  728. return_value = PR_FALSE;
  729. }
  730. else
  731. {
  732. rid = csn_get_replicaid(csn);
  733. replica = ruvGetReplica (ruv, rid);
  734. if (replica == NULL)
  735. {
  736. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn: replica for id %d not found\n", rid);
  737. return_value = PR_FALSE;
  738. }
  739. else
  740. {
  741. if (strict)
  742. {
  743. return_value = (csn_compare (csn, replica->csn) < 0);
  744. }
  745. else
  746. {
  747. return_value = (csn_compare (csn, replica->csn) <= 0);
  748. }
  749. }
  750. }
  751. return return_value;
  752. }
  753. PRBool
  754. ruv_covers_csn(const RUV *ruv, const CSN *csn)
  755. {
  756. PRBool rc;
  757. PR_RWLock_Rlock (ruv->lock);
  758. rc = ruv_covers_csn_internal(ruv, csn, PR_FALSE);
  759. PR_RWLock_Unlock (ruv->lock);
  760. return rc;
  761. }
  762. PRBool
  763. ruv_covers_csn_strict(const RUV *ruv, const CSN *csn)
  764. {
  765. PRBool rc;
  766. PR_RWLock_Rlock (ruv->lock);
  767. rc = ruv_covers_csn_internal(ruv, csn, PR_TRUE);
  768. PR_RWLock_Unlock (ruv->lock);
  769. return rc;
  770. }
  771. /*
  772. * The function gets min{maxcsns of all ruv elements} if get_the_max=0,
  773. * or max{maxcsns of all ruv elements} if get_the_max != 0.
  774. */
  775. static int
  776. ruv_get_min_or_max_csn(const RUV *ruv, CSN **csn, int get_the_max)
  777. {
  778. int return_value;
  779. if (ruv == NULL || csn == NULL)
  780. {
  781. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_get_min_or_max_csn: NULL argument\n");
  782. return_value = RUV_BAD_DATA;
  783. }
  784. else
  785. {
  786. CSN *found = NULL;
  787. RUVElement *replica;
  788. int cookie;
  789. PR_RWLock_Rlock (ruv->lock);
  790. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  791. replica = dl_get_next (ruv->elements, &cookie))
  792. {
  793. /*
  794. * Skip replica whose maxcsn is NULL otherwise
  795. * the code will return different min_csn if
  796. * the sequence of the replicas is altered.
  797. *
  798. * don't use READ_ONLY replicas for computing the value of
  799. * "found", as they seem to have NULL csn and min_csn
  800. */
  801. if (replica->csn == NULL || replica->rid == READ_ONLY_REPLICA_ID)
  802. {
  803. continue;
  804. }
  805. if (found == NULL ||
  806. (!get_the_max && csn_compare(found, replica->csn)>0) ||
  807. ( get_the_max && csn_compare(found, replica->csn)<0))
  808. {
  809. found = replica->csn;
  810. }
  811. }
  812. if (found == NULL)
  813. {
  814. *csn = NULL;
  815. }
  816. else
  817. {
  818. *csn = csn_dup (found);
  819. }
  820. PR_RWLock_Unlock (ruv->lock);
  821. return_value = RUV_SUCCESS;
  822. }
  823. return return_value;
  824. }
  825. int
  826. ruv_get_max_csn(const RUV *ruv, CSN **csn)
  827. {
  828. return ruv_get_min_or_max_csn(ruv, csn, 1 /* get the max */);
  829. }
  830. int
  831. ruv_get_min_csn(const RUV *ruv, CSN **csn)
  832. {
  833. return ruv_get_min_or_max_csn(ruv, csn, 0 /* get the min */);
  834. }
  835. int
  836. ruv_enumerate_elements (const RUV *ruv, FNEnumRUV fn, void *arg)
  837. {
  838. int cookie;
  839. RUVElement *elem;
  840. int rc = 0;
  841. ruv_enum_data enum_data = {0};
  842. if (ruv == NULL || fn == NULL)
  843. {
  844. /* ONREPL - log error */
  845. return -1;
  846. }
  847. PR_RWLock_Rlock (ruv->lock);
  848. for (elem = (RUVElement*)dl_get_first (ruv->elements, &cookie); elem;
  849. elem = (RUVElement*)dl_get_next (ruv->elements, &cookie))
  850. {
  851. /* we only return elements that contains both minimal and maximal CSNs */
  852. if (elem->csn && elem->min_csn)
  853. {
  854. enum_data.csn = elem->csn;
  855. enum_data.min_csn = elem->min_csn;
  856. rc = fn (&enum_data, arg);
  857. if (rc != 0)
  858. break;
  859. }
  860. }
  861. PR_RWLock_Unlock (ruv->lock);
  862. return rc;
  863. }
  864. /*
  865. * Convert a replica update vector to a NULL-terminated array
  866. * of bervals. The caller is responsible for freeing the bervals.
  867. */
  868. int
  869. ruv_to_bervals(const RUV *ruv, struct berval ***bvals)
  870. {
  871. struct berval **returned_bervals = NULL;
  872. int return_value;
  873. if (ruv == NULL || bvals == NULL)
  874. {
  875. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_bervals: NULL argument\n");
  876. return_value = RUV_BAD_DATA;
  877. }
  878. else
  879. {
  880. int count;
  881. int i;
  882. RUVElement *replica;
  883. char csnStr1 [CSN_STRSIZE];
  884. char csnStr2 [CSN_STRSIZE];
  885. int cookie;
  886. PR_RWLock_Rlock (ruv->lock);
  887. count = dl_get_count (ruv->elements) + 2;
  888. returned_bervals = (struct berval **)slapi_ch_malloc(sizeof(struct berval *) * count);
  889. returned_bervals[count - 1] = NULL;
  890. returned_bervals[0] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  891. returned_bervals[0]->bv_val = slapi_ch_smprintf("%s %s",
  892. prefix_replicageneration, ruv->replGen);
  893. returned_bervals[0]->bv_len = strlen(returned_bervals[0]->bv_val);
  894. for (i = 1, replica = dl_get_first (ruv->elements, &cookie); replica;
  895. i++, replica = dl_get_next (ruv->elements, &cookie))
  896. {
  897. returned_bervals[i] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  898. returned_bervals[i]->bv_val = slapi_ch_smprintf("%s%d%s%s}%s%s%s%s",
  899. prefix_ruvcsn, replica->rid,
  900. replica->replica_purl == NULL ? "" : " ",
  901. replica->replica_purl == NULL ? "" : replica->replica_purl,
  902. replica->min_csn == NULL ? "" : " ",
  903. replica->min_csn == NULL ? "" : csn_as_string (replica->min_csn, PR_FALSE, csnStr1),
  904. replica->csn == NULL ? "" : " ",
  905. replica->csn == NULL ? "" : csn_as_string (replica->csn, PR_FALSE, csnStr2));
  906. returned_bervals[i]->bv_len = strlen(returned_bervals[i]->bv_val);
  907. }
  908. PR_RWLock_Unlock (ruv->lock);
  909. return_value = RUV_SUCCESS;
  910. *bvals = returned_bervals;
  911. }
  912. return return_value;
  913. }
  914. int
  915. ruv_to_smod(const RUV *ruv, Slapi_Mod *smod)
  916. {
  917. int return_value;
  918. if (ruv == NULL || smod == NULL)
  919. {
  920. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_smod: NULL argument\n");
  921. return_value = RUV_BAD_DATA;
  922. }
  923. else
  924. {
  925. struct berval val;
  926. RUVElement *replica;
  927. int cookie;
  928. char csnStr1 [CSN_STRSIZE];
  929. char csnStr2 [CSN_STRSIZE];
  930. #define B_SIZ 1024
  931. char buf[B_SIZ];
  932. PR_RWLock_Rlock (ruv->lock);
  933. slapi_mod_init (smod, dl_get_count (ruv->elements) + 1);
  934. slapi_mod_set_type (smod, type_ruvElement);
  935. slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  936. PR_snprintf(buf, B_SIZ, "%s %s", prefix_replicageneration, ruv->replGen);
  937. val.bv_val = buf;
  938. val.bv_len = strlen(buf);
  939. slapi_mod_add_value(smod, &val);
  940. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  941. replica = dl_get_next (ruv->elements, &cookie))
  942. {
  943. PR_snprintf(buf, B_SIZ, "%s%d%s%s}%s%s%s%s", prefix_ruvcsn, replica->rid,
  944. replica->replica_purl == NULL ? "" : " ",
  945. replica->replica_purl == NULL ? "" : replica->replica_purl,
  946. replica->min_csn == NULL ? "" : " ",
  947. replica->min_csn == NULL ? "" : csn_as_string (replica->min_csn, PR_FALSE, csnStr1),
  948. replica->csn == NULL ? "" : " ",
  949. replica->csn == NULL ? "" : csn_as_string (replica->csn, PR_FALSE, csnStr2));
  950. val.bv_len = strlen(buf);
  951. slapi_mod_add_value(smod, &val);
  952. }
  953. PR_RWLock_Unlock (ruv->lock);
  954. return_value = RUV_SUCCESS;
  955. }
  956. return return_value;
  957. }
  958. int
  959. ruv_last_modified_to_smod(const RUV *ruv, Slapi_Mod *smod)
  960. {
  961. int return_value;
  962. if (ruv == NULL || smod == NULL)
  963. {
  964. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_last_modified_to_smod: NULL argument\n");
  965. return_value = RUV_BAD_DATA;
  966. }
  967. else
  968. {
  969. struct berval val;
  970. RUVElement *replica;
  971. int cookie;
  972. char buf[B_SIZ];
  973. PR_RWLock_Rlock (ruv->lock);
  974. slapi_mod_init (smod, dl_get_count (ruv->elements));
  975. slapi_mod_set_type (smod, type_ruvElementUpdatetime);
  976. slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  977. val.bv_val = buf;
  978. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  979. replica = dl_get_next (ruv->elements, &cookie))
  980. {
  981. PR_snprintf(buf, B_SIZ, "%s%d%s%s} %08lx", prefix_ruvcsn, replica->rid,
  982. replica->replica_purl == NULL ? "" : " ",
  983. replica->replica_purl == NULL ? "" : replica->replica_purl,
  984. replica->last_modified);
  985. val.bv_len = strlen(buf);
  986. slapi_mod_add_value(smod, &val);
  987. }
  988. PR_RWLock_Unlock (ruv->lock);
  989. return_value = RUV_SUCCESS;
  990. }
  991. return return_value;
  992. }
  993. /*
  994. * XXXggood do we need "ruv_covers_ruv_strict" ???? */
  995. PRBool
  996. ruv_covers_ruv(const RUV *covering_ruv, const RUV *covered_ruv)
  997. {
  998. PRBool return_value = PR_TRUE;
  999. RUVElement *replica;
  1000. int cookie;
  1001. /* compare replica generations first */
  1002. if (covering_ruv->replGen == NULL || covered_ruv->replGen == NULL)
  1003. return PR_FALSE;
  1004. if (strcasecmp (covered_ruv->replGen, covering_ruv->replGen))
  1005. return PR_FALSE;
  1006. /* replica generation is the same, now compare element by element */
  1007. for (replica = dl_get_first (covered_ruv->elements, &cookie);
  1008. NULL != replica;
  1009. replica = dl_get_next (covered_ruv->elements, &cookie))
  1010. {
  1011. if (replica->csn &&
  1012. (ruv_covers_csn(covering_ruv, replica->csn) == PR_FALSE))
  1013. {
  1014. return_value = PR_FALSE;
  1015. /* Don't break here - may leave something referenced? */
  1016. }
  1017. }
  1018. return return_value;
  1019. }
  1020. PRInt32
  1021. ruv_replica_count (const RUV *ruv)
  1022. {
  1023. if (ruv == NULL)
  1024. return 0;
  1025. else
  1026. {
  1027. int count;
  1028. PR_RWLock_Rlock (ruv->lock);
  1029. count = dl_get_count (ruv->elements);
  1030. PR_RWLock_Unlock (ruv->lock);
  1031. return count;
  1032. }
  1033. }
  1034. /*
  1035. * Extract all the referral URL's from the RUV (but self URL),
  1036. * returning them in an array of strings, that
  1037. * the caller must free.
  1038. */
  1039. char **
  1040. ruv_get_referrals(const RUV *ruv)
  1041. {
  1042. char **r= NULL;
  1043. int n;
  1044. const char *mypurl = multimaster_get_local_purl();
  1045. PR_RWLock_Rlock (ruv->lock);
  1046. n = ruv_replica_count(ruv);
  1047. if(n>0)
  1048. {
  1049. RUVElement *replica;
  1050. int cookie;
  1051. int i= 0;
  1052. r= (char**)slapi_ch_calloc(sizeof(char*),n+1);
  1053. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1054. replica = dl_get_next (ruv->elements, &cookie))
  1055. {
  1056. /* Add URL into referrals if doesn't match self URL */
  1057. if((replica->replica_purl!=NULL) &&
  1058. (slapi_utf8casecmp((unsigned char *)replica->replica_purl,
  1059. (unsigned char *)mypurl) != 0))
  1060. {
  1061. r[i]= slapi_ch_strdup(replica->replica_purl);
  1062. i++;
  1063. }
  1064. }
  1065. }
  1066. PR_RWLock_Unlock (ruv->lock);
  1067. return r; /* Caller must free this */
  1068. }
  1069. void
  1070. ruv_dump(const RUV *ruv, char *ruv_name, PRFileDesc *prFile)
  1071. {
  1072. RUVElement *replica;
  1073. int cookie;
  1074. char csnstr1[CSN_STRSIZE];
  1075. char csnstr2[CSN_STRSIZE];
  1076. char buff[RUVSTR_SIZE];
  1077. int len = sizeof (buff);
  1078. PR_ASSERT(NULL != ruv);
  1079. PR_RWLock_Rlock (ruv->lock);
  1080. PR_snprintf (buff, len, "%s: {replicageneration} %s\n",
  1081. ruv_name ? ruv_name : type_ruvElement,
  1082. ruv->replGen == NULL ? "" : ruv->replGen);
  1083. if (prFile)
  1084. {
  1085. slapi_write_buffer (prFile, buff, strlen(buff));
  1086. }
  1087. else
  1088. {
  1089. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, buff);
  1090. }
  1091. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1092. replica = dl_get_next (ruv->elements, &cookie))
  1093. {
  1094. /* prefix_ruvcsn = "{replica " */
  1095. PR_snprintf (buff, len, "%s: %s%d%s%s}%s%s%s%s\n",
  1096. ruv_name ? ruv_name : type_ruvElement,
  1097. prefix_ruvcsn, replica->rid,
  1098. replica->replica_purl == NULL ? "" : " ",
  1099. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1100. replica->min_csn == NULL ? "" : " ",
  1101. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  1102. replica->csn == NULL ? "" : " ",
  1103. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  1104. if (strlen (csnstr1) > 0) {
  1105. PR_snprintf (buff + strlen(buff) - 1, len - strlen(buff), " %08lx\n",
  1106. replica->last_modified);
  1107. }
  1108. if (prFile)
  1109. {
  1110. slapi_write_buffer (prFile, buff, strlen(buff));
  1111. }
  1112. else
  1113. {
  1114. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, buff);
  1115. }
  1116. }
  1117. PR_RWLock_Unlock (ruv->lock);
  1118. }
  1119. /* this function notifies the ruv that there are operations in progress so that
  1120. they can be added to the pending list for the appropriate client. */
  1121. int ruv_add_csn_inprogress (RUV *ruv, const CSN *csn)
  1122. {
  1123. RUVElement* replica;
  1124. char csn_str[CSN_STRSIZE];
  1125. int rc = RUV_SUCCESS;
  1126. PR_ASSERT (ruv && csn);
  1127. /* locate ruvElement */
  1128. PR_RWLock_Wlock (ruv->lock);
  1129. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1130. if (replica == NULL)
  1131. {
  1132. replica = ruvAddReplicaNoCSN (ruv, csn_get_replicaid (csn), NULL/*purl*/);
  1133. if (replica == NULL)
  1134. {
  1135. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to add replica"
  1136. " that created csn %s\n", csn_as_string (csn, PR_FALSE, csn_str));
  1137. rc = RUV_MEMORY_ERROR;
  1138. goto done;
  1139. }
  1140. }
  1141. /* check first that this csn is not already covered by this RUV */
  1142. if (ruv_covers_csn_internal(ruv, csn, PR_FALSE))
  1143. {
  1144. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1145. "the csn %s has already be seen - ignoring\n",
  1146. csn_as_string (csn, PR_FALSE, csn_str));
  1147. rc = RUV_COVERS_CSN;
  1148. goto done;
  1149. }
  1150. rc = csnplInsert (replica->csnpl, csn);
  1151. if (rc == 1) /* we already seen this csn */
  1152. {
  1153. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1154. "the csn %s has already be seen - ignoring\n",
  1155. csn_as_string (csn, PR_FALSE, csn_str));
  1156. rc = RUV_COVERS_CSN;
  1157. }
  1158. else if(rc != 0)
  1159. {
  1160. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to insert csn %s"
  1161. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1162. rc = RUV_UNKNOWN_ERROR;
  1163. }
  1164. else
  1165. {
  1166. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: successfully inserted csn %s"
  1167. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1168. rc = RUV_SUCCESS;
  1169. }
  1170. done:
  1171. PR_RWLock_Unlock (ruv->lock);
  1172. return rc;
  1173. }
  1174. int ruv_cancel_csn_inprogress (RUV *ruv, const CSN *csn)
  1175. {
  1176. RUVElement* replica;
  1177. int rc = RUV_SUCCESS;
  1178. PR_ASSERT (ruv && csn);
  1179. /* locate ruvElement */
  1180. PR_RWLock_Wlock (ruv->lock);
  1181. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1182. if (replica == NULL)
  1183. {
  1184. /* ONREPL - log error */
  1185. rc = RUV_NOTFOUND;
  1186. goto done;
  1187. }
  1188. rc = csnplRemove (replica->csnpl, csn);
  1189. if (rc != 0)
  1190. rc = RUV_NOTFOUND;
  1191. else
  1192. rc = RUV_SUCCESS;
  1193. done:
  1194. PR_RWLock_Unlock (ruv->lock);
  1195. return rc;
  1196. }
  1197. int ruv_update_ruv (RUV *ruv, const CSN *csn, const char *replica_purl, PRBool isLocal)
  1198. {
  1199. int rc=RUV_SUCCESS;
  1200. char csn_str[CSN_STRSIZE];
  1201. CSN *max_csn;
  1202. CSN *first_csn = NULL;
  1203. RUVElement *replica;
  1204. PR_ASSERT (ruv && csn);
  1205. PR_RWLock_Wlock (ruv->lock);
  1206. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1207. if (replica == NULL)
  1208. {
  1209. /* we should have a ruv element at this point because it would have
  1210. been added by ruv_add_inprogress function */
  1211. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1212. "can't locate RUV element for replica %d\n", csn_get_replicaid (csn));
  1213. goto done;
  1214. }
  1215. if (csnplCommit(replica->csnpl, csn) != 0)
  1216. {
  1217. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "ruv_update_ruv: cannot commit csn %s\n",
  1218. csn_as_string(csn, PR_FALSE, csn_str));
  1219. rc = RUV_CSNPL_ERROR;
  1220. goto done;
  1221. }
  1222. else
  1223. {
  1224. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1225. "successfully committed csn %s\n", csn_as_string(csn, PR_FALSE, csn_str));
  1226. }
  1227. if ((max_csn = csnplRollUp(replica->csnpl, &first_csn)) != NULL)
  1228. {
  1229. #ifdef DEBUG
  1230. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: rolled up to csn %s\n",
  1231. csn_as_string(max_csn, PR_FALSE, csn_str)); /* XXXggood remove debugging */
  1232. #endif
  1233. /* replica object sets min csn for local replica */
  1234. if (!isLocal && replica->min_csn == NULL) {
  1235. /* bug 559223 - it seems that, under huge stress, a server might pass
  1236. * through this code when more than 1 change has already been sent and commited into
  1237. * the pending lists... Therefore, as we are trying to set the min_csn ever
  1238. * generated by this replica, we need to set the first_csn as the min csn in the
  1239. * ruv */
  1240. set_min_csn_nolock(ruv, first_csn, replica_purl);
  1241. }
  1242. set_max_csn_nolock(ruv, max_csn, replica_purl);
  1243. /* It is possible that first_csn points to max_csn.
  1244. We need to free it once */
  1245. if (max_csn != first_csn) {
  1246. csn_free(&first_csn);
  1247. }
  1248. csn_free(&max_csn);
  1249. }
  1250. done:
  1251. PR_RWLock_Unlock (ruv->lock);
  1252. return rc;
  1253. }
  1254. /* Helper functions */
  1255. static int
  1256. ruvInit (RUV **ruv, int initCount)
  1257. {
  1258. PR_ASSERT (ruv);
  1259. if (ruv == NULL) {
  1260. return RUV_NSPR_ERROR;
  1261. }
  1262. /* allocate new RUV */
  1263. *ruv = (RUV *)slapi_ch_calloc (1, sizeof (RUV));
  1264. /* allocate elements */
  1265. (*ruv)->elements = dl_new (); /* never returns NULL */
  1266. dl_init ((*ruv)->elements, initCount);
  1267. /* create lock */
  1268. (*ruv)->lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "ruv_lock");
  1269. if ((*ruv)->lock == NULL)
  1270. {
  1271. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1272. "ruvInit: failed to create lock\n");
  1273. dl_free(&(*ruv)->elements);
  1274. slapi_ch_free((void**)ruv);
  1275. return RUV_NSPR_ERROR;
  1276. }
  1277. return RUV_SUCCESS;
  1278. }
  1279. static void
  1280. ruvFreeReplica (void **data)
  1281. {
  1282. RUVElement *element = *(RUVElement**)data;
  1283. if (NULL != element)
  1284. {
  1285. if (NULL != element->csn)
  1286. {
  1287. csn_free (&element->csn);
  1288. }
  1289. if (NULL != element->min_csn)
  1290. {
  1291. csn_free (&element->min_csn);
  1292. }
  1293. /* slapi_ch_free accepts NULL pointer */
  1294. slapi_ch_free((void **)&element->replica_purl);
  1295. if (element->csnpl)
  1296. {
  1297. csnplFree (&(element->csnpl));
  1298. }
  1299. slapi_ch_free ((void **)&element);
  1300. }
  1301. }
  1302. static RUVElement*
  1303. ruvAddReplica (RUV *ruv, const CSN *csn, const char *replica_purl)
  1304. {
  1305. RUVElement *replica;
  1306. PR_ASSERT (NULL != ruv);
  1307. PR_ASSERT (NULL != csn);
  1308. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1309. if (replica == NULL)
  1310. {
  1311. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1312. "ruvAddReplica: memory allocation failed\n");
  1313. return NULL;
  1314. }
  1315. replica->rid = csn_get_replicaid (csn);
  1316. /* PR_ASSERT(replica->rid != READ_ONLY_REPLICA_ID); */
  1317. replica->csn = csn_dup (csn);
  1318. replica->last_modified = current_time();
  1319. replica->min_csn = csn_dup (csn);
  1320. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1321. replica->csnpl = csnplNew ();
  1322. dl_add (ruv->elements, replica);
  1323. return replica;
  1324. }
  1325. static RUVElement*
  1326. ruvAddReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl)
  1327. {
  1328. RUVElement *replica;
  1329. PR_ASSERT (NULL != ruv);
  1330. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1331. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1332. if (replica == NULL)
  1333. {
  1334. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1335. "ruvAddReplicaNoCSN: memory allocation failed\n");
  1336. return NULL;
  1337. }
  1338. replica->rid = rid;
  1339. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1340. replica->csnpl = csnplNew ();
  1341. dl_add (ruv->elements, replica);
  1342. return replica;
  1343. }
  1344. static RUVElement*
  1345. ruvAddIndexReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl, int index)
  1346. {
  1347. RUVElement *replica;
  1348. PR_ASSERT (NULL != ruv);
  1349. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1350. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1351. if (replica == NULL)
  1352. {
  1353. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1354. "ruvAddIndexReplicaNoCSN: memory allocation failed\n");
  1355. return NULL;
  1356. }
  1357. replica->rid = rid;
  1358. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1359. replica->csnpl = csnplNew ();
  1360. dl_add_index (ruv->elements, replica, index);
  1361. return replica;
  1362. }
  1363. static RUVElement *
  1364. ruvGetReplica (const RUV *ruv, ReplicaId rid)
  1365. {
  1366. PR_ASSERT (ruv /* && rid >= 0 -- rid can't be negative */);
  1367. return (RUVElement *)dl_get (ruv->elements, (const void*)&rid, ruvReplicaCompare);
  1368. }
  1369. static int
  1370. ruvReplicaCompare (const void *el1, const void *el2)
  1371. {
  1372. RUVElement *replica = (RUVElement*)el1;
  1373. ReplicaId *rid1 = (ReplicaId*) el2;
  1374. if (replica == NULL || rid1 == NULL)
  1375. return -1;
  1376. if (*rid1 == replica->rid)
  1377. return 0;
  1378. if (*rid1 < replica->rid)
  1379. return -1;
  1380. else
  1381. return 1;
  1382. }
  1383. /*
  1384. * Given a berval that points to a string of the form:
  1385. * "{dbgen} generation-id", return a copy of the
  1386. * "generation-id" part in a null-terminated string.
  1387. * Returns NULL if the berval is malformed.
  1388. */
  1389. static char *
  1390. get_replgen_from_berval(const struct berval *bval)
  1391. {
  1392. char *ret_string = NULL;
  1393. if (NULL != bval && NULL != bval->bv_val &&
  1394. (bval->bv_len > strlen(prefix_replicageneration)) &&
  1395. strncasecmp(bval->bv_val, prefix_replicageneration,
  1396. strlen(prefix_replicageneration)) == 0)
  1397. {
  1398. unsigned int index = strlen(prefix_replicageneration);
  1399. /* Skip any whitespace */
  1400. while (index++ < bval->bv_len && bval->bv_val[index] == ' ');
  1401. if (index < bval->bv_len)
  1402. {
  1403. unsigned int ret_len = bval->bv_len - index;
  1404. ret_string = slapi_ch_malloc(ret_len + 1);
  1405. memcpy(ret_string, &bval->bv_val[index], ret_len);
  1406. ret_string[ret_len] = '\0';
  1407. }
  1408. }
  1409. return ret_string;
  1410. }
  1411. /*
  1412. * Given a berval that points to a string of the form:
  1413. * "{replica ldap[s]//host:port} <min_csn> <csn>", parse out the
  1414. * partial URL and the CSNs into an RUVElement, and return
  1415. * a pointer to the copy. Returns NULL if the berval is
  1416. * malformed.
  1417. */
  1418. static RUVElement *
  1419. get_ruvelement_from_berval(const struct berval *bval)
  1420. {
  1421. RUVElement *ret_ruve = NULL;
  1422. char *purl = NULL;
  1423. ReplicaId rid = 0;
  1424. char ridbuff [RIDSTR_SIZE];
  1425. int i;
  1426. if (NULL == bval || NULL == bval->bv_val ||
  1427. bval->bv_len <= strlen(prefix_ruvcsn) ||
  1428. strncasecmp(bval->bv_val, prefix_ruvcsn, strlen(prefix_ruvcsn)) != 0)
  1429. {
  1430. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1431. "get_ruvelement_from_berval: invalid berval\n");
  1432. goto loser;
  1433. }
  1434. {
  1435. unsigned int urlbegin = strlen(prefix_ruvcsn);
  1436. unsigned int urlend;
  1437. unsigned int mincsnbegin;
  1438. /* replica id must be here */
  1439. i = 0;
  1440. while (isdigit (bval->bv_val[urlbegin]))
  1441. {
  1442. ridbuff [i] = bval->bv_val[urlbegin];
  1443. i++;
  1444. urlbegin ++;
  1445. }
  1446. if (i == 0) /* replicaid is missing */
  1447. goto loser;
  1448. ridbuff[i] = '\0';
  1449. rid = atoi (ridbuff);
  1450. if (bval->bv_val[urlbegin] == '}')
  1451. {
  1452. /* No purl in this value */
  1453. purl = NULL;
  1454. mincsnbegin = urlbegin + 1;
  1455. }
  1456. else
  1457. {
  1458. while (urlbegin++ < bval->bv_len && bval->bv_val[urlbegin] == ' ');
  1459. urlend = urlbegin;
  1460. while (urlend++ < bval->bv_len && bval->bv_val[urlend] != '}');
  1461. purl = slapi_ch_malloc(urlend - urlbegin + 1);
  1462. memcpy(purl, &bval->bv_val[urlbegin], urlend - urlbegin);
  1463. purl[urlend - urlbegin] = '\0';
  1464. mincsnbegin = urlend;
  1465. }
  1466. /* Skip any whitespace before the first (minimum) CSN */
  1467. while (mincsnbegin++ < (bval->bv_len-1) && bval->bv_val[mincsnbegin] == ' ');
  1468. /* Now, mincsnbegin should contain the index of the beginning of the first csn */
  1469. if (mincsnbegin >= bval->bv_len)
  1470. {
  1471. /* Missing the entire content*/
  1472. if (purl == NULL)
  1473. goto loser;
  1474. else /* we have just purl - no changes from the replica has been seen */
  1475. {
  1476. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1477. ret_ruve->rid = rid;
  1478. ret_ruve->replica_purl = purl;
  1479. }
  1480. }
  1481. else
  1482. {
  1483. if ((bval->bv_len - mincsnbegin != (_CSN_VALIDCSN_STRLEN * 2) + 1)
  1484. && (bval->bv_len - mincsnbegin != (_CSN_VALIDCSN_STRLEN * 2) + 10))
  1485. {
  1486. /* Malformed - incorrect length for 2 CSNs + space AND
  1487. * incorrect length for 2 CSNs + 2 spaces +
  1488. * last_modified (see ruv_dump) */
  1489. goto loser;
  1490. }
  1491. else
  1492. {
  1493. char mincsnstr[CSN_STRSIZE];
  1494. char maxcsnstr[CSN_STRSIZE];
  1495. memset(mincsnstr, '\0', CSN_STRSIZE);
  1496. memset(maxcsnstr, '\0', CSN_STRSIZE);
  1497. memcpy(mincsnstr, &bval->bv_val[mincsnbegin], _CSN_VALIDCSN_STRLEN);
  1498. memcpy(maxcsnstr, &bval->bv_val[mincsnbegin + _CSN_VALIDCSN_STRLEN + 1], _CSN_VALIDCSN_STRLEN);
  1499. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1500. ret_ruve->min_csn = csn_new_by_string(mincsnstr);
  1501. ret_ruve->csn = csn_new_by_string(maxcsnstr);
  1502. ret_ruve->rid = rid;
  1503. ret_ruve->replica_purl = purl;
  1504. if (NULL == ret_ruve->min_csn || NULL == ret_ruve->csn)
  1505. {
  1506. goto loser;
  1507. }
  1508. }
  1509. }
  1510. }
  1511. /* initialize csn pending list */
  1512. ret_ruve->csnpl = csnplNew ();
  1513. if (ret_ruve->csnpl == NULL)
  1514. {
  1515. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1516. "get_ruvelement_from_berval: failed to create csn pending list\n");
  1517. goto loser;
  1518. }
  1519. return ret_ruve;
  1520. loser:
  1521. /* slapi_ch_free accepts NULL pointer */
  1522. slapi_ch_free((void **)&purl);
  1523. if (NULL != ret_ruve)
  1524. {
  1525. if (NULL != ret_ruve->min_csn)
  1526. {
  1527. csn_free(&ret_ruve->min_csn);
  1528. }
  1529. if (NULL != ret_ruve->csn)
  1530. {
  1531. csn_free(&ret_ruve->csn);
  1532. }
  1533. slapi_ch_free((void **)&ret_ruve);
  1534. }
  1535. return NULL;
  1536. }
  1537. int
  1538. ruv_move_local_supplier_to_first(RUV *ruv, ReplicaId aRid)
  1539. {
  1540. RUVElement * elem = NULL;
  1541. int rc = RUV_NOTFOUND;
  1542. PR_ASSERT(ruv);
  1543. PR_RWLock_Wlock (ruv->lock);
  1544. elem = (RUVElement *)dl_delete(ruv->elements,(const void*)&aRid, ruvReplicaCompare, 0);
  1545. if (elem) {
  1546. dl_add_index(ruv->elements, elem, 1);
  1547. rc = RUV_SUCCESS;
  1548. }
  1549. PR_RWLock_Unlock (ruv->lock);
  1550. return rc;
  1551. }
  1552. int
  1553. ruv_get_first_id_and_purl(RUV *ruv, ReplicaId *rid, char **replica_purl )
  1554. {
  1555. RUVElement * first = NULL;
  1556. int cookie;
  1557. int rc;
  1558. PR_ASSERT(ruv);
  1559. PR_RWLock_Rlock (ruv->lock);
  1560. first = dl_get_first(ruv->elements, &cookie);
  1561. if ( first == NULL )
  1562. {
  1563. rc = RUV_MEMORY_ERROR;
  1564. }
  1565. else
  1566. {
  1567. *rid = first->rid;
  1568. *replica_purl = first->replica_purl;
  1569. rc = RUV_SUCCESS;
  1570. }
  1571. PR_RWLock_Unlock (ruv->lock);
  1572. return rc;
  1573. }
  1574. int ruv_local_contains_supplier(RUV *ruv, ReplicaId rid)
  1575. {
  1576. int cookie;
  1577. RUVElement *elem = NULL;
  1578. PR_ASSERT(ruv);
  1579. PR_RWLock_Rlock (ruv->lock);
  1580. for (elem = dl_get_first (ruv->elements, &cookie);
  1581. elem;
  1582. elem = dl_get_next (ruv->elements, &cookie))
  1583. {
  1584. if (elem->rid == rid){
  1585. PR_RWLock_Unlock (ruv->lock);
  1586. return 1;
  1587. }
  1588. }
  1589. PR_RWLock_Unlock (ruv->lock);
  1590. return 0;
  1591. }
  1592. PRBool ruv_has_csns(const RUV *ruv)
  1593. {
  1594. PRBool retval = PR_TRUE;
  1595. CSN *mincsn = NULL;
  1596. CSN *maxcsn = NULL;
  1597. ruv_get_min_csn(ruv, &mincsn);
  1598. ruv_get_max_csn(ruv, &maxcsn);
  1599. if (mincsn) {
  1600. csn_free(&mincsn);
  1601. csn_free(&maxcsn);
  1602. } else if (maxcsn) {
  1603. csn_free(&maxcsn);
  1604. } else {
  1605. retval = PR_FALSE; /* both min and max are false */
  1606. }
  1607. return retval;
  1608. }
  1609. PRBool ruv_has_both_csns(const RUV *ruv)
  1610. {
  1611. PRBool retval = PR_TRUE;
  1612. CSN *mincsn = NULL;
  1613. CSN *maxcsn = NULL;
  1614. ruv_get_min_csn(ruv, &mincsn);
  1615. ruv_get_max_csn(ruv, &maxcsn);
  1616. if (mincsn) {
  1617. csn_free(&mincsn);
  1618. csn_free(&maxcsn);
  1619. } else if (maxcsn) {
  1620. csn_free(&maxcsn);
  1621. retval = PR_FALSE; /* it has a maxcsn but no mincsn */
  1622. } else {
  1623. retval = PR_FALSE; /* both min and max are false */
  1624. }
  1625. return retval;
  1626. }
  1627. /* Check if the first ruv is newer than the second one */
  1628. PRBool
  1629. ruv_is_newer (Object *sruvobj, Object *cruvobj)
  1630. {
  1631. RUV *sruv, *cruv;
  1632. RUVElement *sreplica, *creplica;
  1633. int scookie, ccookie;
  1634. int is_newer = PR_FALSE;
  1635. if ( sruvobj == NULL ) {
  1636. return 0;
  1637. }
  1638. if ( cruvobj == NULL ) {
  1639. return 1;
  1640. }
  1641. sruv = (RUV *) object_get_data ( sruvobj );
  1642. cruv = (RUV *) object_get_data ( cruvobj );
  1643. for (sreplica = dl_get_first (sruv->elements, &scookie); sreplica;
  1644. sreplica = dl_get_next (sruv->elements, &scookie))
  1645. {
  1646. /* A hub may have a dummy ruv with rid 65535 */
  1647. if ( sreplica->csn == NULL ) continue;
  1648. if ( cruv->elements == NULL )
  1649. {
  1650. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1651. "ruv_is_newer, consumer RUV has no elements\n");
  1652. is_newer=PR_FALSE;
  1653. break;
  1654. }
  1655. for (creplica = dl_get_first (cruv->elements, &ccookie); creplica;
  1656. creplica = dl_get_next (cruv->elements, &ccookie))
  1657. {
  1658. if ( sreplica->rid == creplica->rid ) {
  1659. if ( csn_compare ( sreplica->csn, creplica->csn ) > 0 ) {
  1660. is_newer = PR_TRUE;
  1661. }
  1662. break;
  1663. }
  1664. }
  1665. if ( creplica == NULL || is_newer ) {
  1666. is_newer = PR_TRUE;
  1667. break;
  1668. }
  1669. }
  1670. return is_newer;
  1671. }
  1672. void
  1673. ruv_force_csn_update (RUV *ruv, CSN *csn)
  1674. {
  1675. CSN *max = NULL;
  1676. if (ruv != NULL)
  1677. {
  1678. ruv_get_max_csn(ruv, &max);
  1679. if (csn_compare(max, csn))
  1680. {
  1681. ruv_set_max_csn(ruv, csn, NULL);
  1682. }
  1683. csn_free(&max);
  1684. }
  1685. }
  1686. /* This routine is used to iterate the elements in a RUV and set each vector's
  1687. + * minimal CSN to a dummy with just the rid set, e.g. 00000000000000010000 */
  1688. void
  1689. ruv_insert_dummy_min_csn (RUV *ruv)
  1690. {
  1691. RUVElement *r;
  1692. int cookie;
  1693. for (r = dl_get_first (ruv->elements, &cookie); r;
  1694. r = dl_get_next (ruv->elements, &cookie)) {
  1695. if (r->csn && !r->min_csn) {
  1696. CSN *dummycsn = csn_new();
  1697. csn_init(dummycsn);
  1698. csn_set_replicaid(dummycsn, csn_get_replicaid(r->csn));
  1699. r->min_csn = dummycsn;
  1700. }
  1701. }
  1702. }
  1703. #ifdef TESTING /* Some unit tests for code in this file */
  1704. static void
  1705. ruv_dump_internal(RUV *ruv)
  1706. {
  1707. RUVElement *replica;
  1708. int cookie;
  1709. char csnstr1[CSN_STRSIZE];
  1710. char csnstr2[CSN_STRSIZE];
  1711. PR_ASSERT(NULL != ruv);
  1712. printf("{replicageneration} %s\n", ruv->replGen == NULL ? "NULL" : ruv->replGen);
  1713. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1714. replica = dl_get_next (ruv->elements, &cookie))
  1715. {
  1716. printf("{replica%s%s} %s %s\n",
  1717. replica->replica_purl == NULL ? "" : " ",
  1718. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1719. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  1720. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  1721. }
  1722. }
  1723. void
  1724. ruv_test()
  1725. {
  1726. const struct berval *vals[5];
  1727. struct berval val0, val1, val2, val3;
  1728. RUV *ruv;
  1729. Slapi_Attr *attr;
  1730. Slapi_Value *sv0, *sv1, *sv2, *sv3;
  1731. int rc;
  1732. char csnstr[CSN_STRSIZE];
  1733. char *gen;
  1734. CSN *newcsn;
  1735. ReplicaId *ids;
  1736. int nids;
  1737. Slapi_Mod smods;
  1738. PRBool covers;
  1739. vals[0] = &val0;
  1740. vals[1] = &val1;
  1741. vals[2] = &val2;
  1742. vals[3] = &val3;
  1743. vals[4] = NULL;
  1744. val0.bv_val = "{replicageneration} 0440FDC0A33F";
  1745. val0.bv_len = strlen(val0.bv_val);
  1746. val1.bv_val = "{replica ldap://ggood.mcom.com:389} 12345670000000FE0000 12345671000000FE0000";
  1747. val1.bv_len = strlen(val1.bv_val);
  1748. val2.bv_val = "{replica ldaps://an-impossibly-long-host-name-that-drags-on-forever-and-forever.mcom.com:389} 11112110000000FF0000 11112111000000FF0000";
  1749. val2.bv_len = strlen(val2.bv_val);
  1750. val3.bv_val = "{replica} 12345672000000FD0000 12345673000000FD0000";
  1751. val3.bv_len = strlen(val3.bv_val);
  1752. rc = ruv_init_from_bervals(vals, &ruv);
  1753. ruv_dump_internal(ruv);
  1754. attr = slapi_attr_new();
  1755. attr = slapi_attr_init(attr, "ruvelement");
  1756. sv0 = slapi_value_new();
  1757. sv1 = slapi_value_new();
  1758. sv2 = slapi_value_new();
  1759. sv3 = slapi_value_new();
  1760. slapi_value_init_berval(sv0, &val0);
  1761. slapi_value_init_berval(sv1, &val1);
  1762. slapi_value_init_berval(sv2, &val2);
  1763. slapi_value_init_berval(sv3, &val3);
  1764. slapi_attr_add_value(attr, sv0);
  1765. slapi_attr_add_value(attr, sv1);
  1766. slapi_attr_add_value(attr, sv2);
  1767. slapi_attr_add_value(attr, sv3);
  1768. rc = ruv_init_from_slapi_attr(attr, &ruv);
  1769. ruv_dump_internal(ruv);
  1770. rc = ruv_delete_replica(ruv, 0xFF);
  1771. /* Should delete one replica */
  1772. ruv_dump_internal(ruv);
  1773. rc = ruv_delete_replica(ruv, 0xAA);
  1774. /* No such replica - should not do anything */
  1775. ruv_dump_internal(ruv);
  1776. rc = ruv_get_largest_csn_for_replica(ruv, 0xFE, &newcsn);
  1777. if (NULL != newcsn)
  1778. {
  1779. csn_as_string(newcsn, PR_FALSE, csnstr);
  1780. printf("Replica 0x%X has largest csn \"%s\"\n", 0xFE, csnstr);
  1781. }
  1782. else
  1783. {
  1784. printf("BAD - can't get largest CSN for replica 0x%X\n", 0xFE);
  1785. }
  1786. rc = ruv_get_smallest_csn_for_replica(ruv, 0xFE, &newcsn);
  1787. if (NULL != newcsn)
  1788. {
  1789. csn_as_string(newcsn, PR_FALSE, csnstr);
  1790. printf("Replica 0x%X has smallest csn \"%s\"\n", 0xFE, csnstr);
  1791. }
  1792. else
  1793. {
  1794. printf("BAD - can't get smallest CSN for replica 0x%X\n", 0xFE);
  1795. }
  1796. rc = ruv_get_largest_csn_for_replica(ruv, 0xAA, &newcsn);
  1797. printf("ruv_get_largest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  1798. rc = ruv_get_smallest_csn_for_replica(ruv, 0xAA, &newcsn);
  1799. printf("ruv_get_smallest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  1800. newcsn = csn_new_by_string("12345674000000FE0000"); /* Old replica 0xFE */
  1801. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1802. /* Should update replica FE's CSN */
  1803. ruv_dump_internal(ruv);
  1804. newcsn = csn_new_by_string("12345675000000FB0000"); /* New replica 0xFB */
  1805. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1806. /* Should get a new replica in the list with min == max csn */
  1807. ruv_dump_internal(ruv);
  1808. newcsn = csn_new_by_string("12345676000000FD0000"); /* Old replica 0xFD */
  1809. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1810. /* Should update replica 0xFD so new CSN is newer than min CSN */
  1811. ruv_dump_internal(ruv);
  1812. gen = ruv_get_replica_generation(ruv);
  1813. printf("replica generation is \"%s\"\n", gen);
  1814. newcsn = csn_new_by_string("12345673000000FE0000"); /* Old replica 0xFE */
  1815. covers = ruv_covers_csn(ruv, newcsn); /* should say "true" */
  1816. newcsn = csn_new_by_string("12345675000000FE0000"); /* Old replica 0xFE */
  1817. covers = ruv_covers_csn(ruv, newcsn); /* Should say "false" */
  1818. newcsn = csn_new_by_string("123456700000000A0000"); /* New replica 0A */
  1819. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  1820. ruv_dump_internal(ruv);
  1821. newcsn = csn_new_by_string("123456710000000A0000"); /* New replica 0A */
  1822. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  1823. ruv_dump_internal(ruv);
  1824. newcsn = csn_new_by_string("123456700000000B0000"); /* New replica 0B */
  1825. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  1826. ruv_dump_internal(ruv);
  1827. newcsn = csn_new_by_string("123456710000000B0000"); /* New replica 0B */
  1828. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  1829. ruv_dump_internal(ruv);
  1830. /* ONREPL test ruv enumeration */
  1831. rc = ruv_to_smod(ruv, &smods);
  1832. ruv_destroy(&ruv);
  1833. }
  1834. #endif /* TESTING */