repl5_ruv.c 53 KB

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