repl5_ruv.c 51 KB

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