repl5_ruv.c 53 KB

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