repl5_ruv.c 66 KB

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