repl5_ruv.c 68 KB

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