repl5_ruv.c 76 KB

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