repl5_ruv.c 54 KB

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