repl5_ruv.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  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. PR_RWLock_Rlock (ruv->lock);
  708. if (ruv != NULL && ruv->replGen != NULL)
  709. {
  710. return_str = slapi_ch_strdup(ruv->replGen);
  711. }
  712. PR_RWLock_Unlock (ruv->lock);
  713. return return_str;
  714. }
  715. static PRBool
  716. ruv_covers_csn_internal(const RUV *ruv, const CSN *csn, PRBool strict)
  717. {
  718. RUVElement *replica;
  719. ReplicaId rid;
  720. PRBool return_value;
  721. if (ruv == NULL || csn == NULL)
  722. {
  723. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn: NULL argument\n");
  724. return_value = PR_FALSE;
  725. }
  726. else
  727. {
  728. rid = csn_get_replicaid(csn);
  729. replica = ruvGetReplica (ruv, rid);
  730. if (replica == NULL)
  731. {
  732. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn: replica for id %d not found\n", rid);
  733. return_value = PR_FALSE;
  734. }
  735. else
  736. {
  737. if (strict)
  738. {
  739. return_value = (csn_compare (csn, replica->csn) < 0);
  740. }
  741. else
  742. {
  743. return_value = (csn_compare (csn, replica->csn) <= 0);
  744. }
  745. }
  746. }
  747. return return_value;
  748. }
  749. PRBool
  750. ruv_covers_csn(const RUV *ruv, const CSN *csn)
  751. {
  752. PRBool rc;
  753. PR_RWLock_Rlock (ruv->lock);
  754. rc = ruv_covers_csn_internal(ruv, csn, PR_FALSE);
  755. PR_RWLock_Unlock (ruv->lock);
  756. return rc;
  757. }
  758. PRBool
  759. ruv_covers_csn_strict(const RUV *ruv, const CSN *csn)
  760. {
  761. PRBool rc;
  762. PR_RWLock_Rlock (ruv->lock);
  763. rc = ruv_covers_csn_internal(ruv, csn, PR_TRUE);
  764. PR_RWLock_Unlock (ruv->lock);
  765. return rc;
  766. }
  767. /*
  768. * The function gets min{maxcsns of all ruv elements} if get_the_max=0,
  769. * or max{maxcsns of all ruv elements} if get_the_max != 0.
  770. */
  771. static int
  772. ruv_get_min_or_max_csn(const RUV *ruv, CSN **csn, int get_the_max)
  773. {
  774. int return_value;
  775. if (ruv == NULL || csn == NULL)
  776. {
  777. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_get_min_or_max_csn: NULL argument\n");
  778. return_value = RUV_BAD_DATA;
  779. }
  780. else
  781. {
  782. CSN *found = NULL;
  783. RUVElement *replica;
  784. int cookie;
  785. PR_RWLock_Rlock (ruv->lock);
  786. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  787. replica = dl_get_next (ruv->elements, &cookie))
  788. {
  789. /*
  790. * Skip replica whose maxcsn is NULL otherwise
  791. * the code will return different min_csn if
  792. * the sequence of the replicas is altered.
  793. *
  794. * don't use READ_ONLY replicas for computing the value of
  795. * "found", as they seem to have NULL csn and min_csn
  796. */
  797. if (replica->csn == NULL || replica->rid == READ_ONLY_REPLICA_ID)
  798. {
  799. continue;
  800. }
  801. if (found == NULL ||
  802. (!get_the_max && csn_compare(found, replica->csn)>0) ||
  803. ( get_the_max && csn_compare(found, replica->csn)<0))
  804. {
  805. found = replica->csn;
  806. }
  807. }
  808. if (found == NULL)
  809. {
  810. *csn = NULL;
  811. }
  812. else
  813. {
  814. *csn = csn_dup (found);
  815. }
  816. PR_RWLock_Unlock (ruv->lock);
  817. return_value = RUV_SUCCESS;
  818. }
  819. return return_value;
  820. }
  821. int
  822. ruv_get_max_csn(const RUV *ruv, CSN **csn)
  823. {
  824. return ruv_get_min_or_max_csn(ruv, csn, 1 /* get the max */);
  825. }
  826. int
  827. ruv_get_min_csn(const RUV *ruv, CSN **csn)
  828. {
  829. return ruv_get_min_or_max_csn(ruv, csn, 0 /* get the min */);
  830. }
  831. int
  832. ruv_enumerate_elements (const RUV *ruv, FNEnumRUV fn, void *arg)
  833. {
  834. int cookie;
  835. RUVElement *elem;
  836. int rc = 0;
  837. ruv_enum_data enum_data = {0};
  838. if (ruv == NULL || fn == NULL)
  839. {
  840. /* ONREPL - log error */
  841. return -1;
  842. }
  843. PR_RWLock_Rlock (ruv->lock);
  844. for (elem = (RUVElement*)dl_get_first (ruv->elements, &cookie); elem;
  845. elem = (RUVElement*)dl_get_next (ruv->elements, &cookie))
  846. {
  847. /* we only return elements that contains both minimal and maximal CSNs */
  848. if (elem->csn && elem->min_csn)
  849. {
  850. enum_data.csn = elem->csn;
  851. enum_data.min_csn = elem->min_csn;
  852. rc = fn (&enum_data, arg);
  853. if (rc != 0)
  854. break;
  855. }
  856. }
  857. PR_RWLock_Unlock (ruv->lock);
  858. return rc;
  859. }
  860. /*
  861. * Convert a replica update vector to a NULL-terminated array
  862. * of bervals. The caller is responsible for freeing the bervals.
  863. */
  864. int
  865. ruv_to_bervals(const RUV *ruv, struct berval ***bvals)
  866. {
  867. struct berval **returned_bervals = NULL;
  868. int return_value;
  869. if (ruv == NULL || bvals == NULL)
  870. {
  871. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_bervals: NULL argument\n");
  872. return_value = RUV_BAD_DATA;
  873. }
  874. else
  875. {
  876. int count;
  877. int i;
  878. RUVElement *replica;
  879. char csnStr1 [CSN_STRSIZE];
  880. char csnStr2 [CSN_STRSIZE];
  881. int cookie;
  882. PR_RWLock_Rlock (ruv->lock);
  883. count = dl_get_count (ruv->elements) + 2;
  884. returned_bervals = (struct berval **)slapi_ch_malloc(sizeof(struct berval *) * count);
  885. returned_bervals[count - 1] = NULL;
  886. returned_bervals[0] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  887. returned_bervals[0]->bv_val = slapi_ch_smprintf("%s %s",
  888. prefix_replicageneration, ruv->replGen);
  889. returned_bervals[0]->bv_len = strlen(returned_bervals[0]->bv_val);
  890. for (i = 1, replica = dl_get_first (ruv->elements, &cookie); replica;
  891. i++, replica = dl_get_next (ruv->elements, &cookie))
  892. {
  893. returned_bervals[i] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  894. returned_bervals[i]->bv_val = slapi_ch_smprintf("%s%d%s%s}%s%s%s%s",
  895. prefix_ruvcsn, replica->rid,
  896. replica->replica_purl == NULL ? "" : " ",
  897. replica->replica_purl == NULL ? "" : replica->replica_purl,
  898. replica->min_csn == NULL ? "" : " ",
  899. replica->min_csn == NULL ? "" : csn_as_string (replica->min_csn, PR_FALSE, csnStr1),
  900. replica->csn == NULL ? "" : " ",
  901. replica->csn == NULL ? "" : csn_as_string (replica->csn, PR_FALSE, csnStr2));
  902. returned_bervals[i]->bv_len = strlen(returned_bervals[i]->bv_val);
  903. }
  904. PR_RWLock_Unlock (ruv->lock);
  905. return_value = RUV_SUCCESS;
  906. *bvals = returned_bervals;
  907. }
  908. return return_value;
  909. }
  910. int
  911. ruv_to_smod(const RUV *ruv, Slapi_Mod *smod)
  912. {
  913. int return_value;
  914. if (ruv == NULL || smod == NULL)
  915. {
  916. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_smod: NULL argument\n");
  917. return_value = RUV_BAD_DATA;
  918. }
  919. else
  920. {
  921. struct berval val;
  922. RUVElement *replica;
  923. int cookie;
  924. char csnStr1 [CSN_STRSIZE];
  925. char csnStr2 [CSN_STRSIZE];
  926. #define B_SIZ 1024
  927. char buf[B_SIZ];
  928. PR_RWLock_Rlock (ruv->lock);
  929. slapi_mod_init (smod, dl_get_count (ruv->elements) + 1);
  930. slapi_mod_set_type (smod, type_ruvElement);
  931. slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  932. PR_snprintf(buf, B_SIZ, "%s %s", prefix_replicageneration, ruv->replGen);
  933. val.bv_val = buf;
  934. val.bv_len = strlen(buf);
  935. slapi_mod_add_value(smod, &val);
  936. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  937. replica = dl_get_next (ruv->elements, &cookie))
  938. {
  939. PR_snprintf(buf, B_SIZ, "%s%d%s%s}%s%s%s%s", prefix_ruvcsn, replica->rid,
  940. replica->replica_purl == NULL ? "" : " ",
  941. replica->replica_purl == NULL ? "" : replica->replica_purl,
  942. replica->min_csn == NULL ? "" : " ",
  943. replica->min_csn == NULL ? "" : csn_as_string (replica->min_csn, PR_FALSE, csnStr1),
  944. replica->csn == NULL ? "" : " ",
  945. replica->csn == NULL ? "" : csn_as_string (replica->csn, PR_FALSE, csnStr2));
  946. val.bv_len = strlen(buf);
  947. slapi_mod_add_value(smod, &val);
  948. }
  949. PR_RWLock_Unlock (ruv->lock);
  950. return_value = RUV_SUCCESS;
  951. }
  952. return return_value;
  953. }
  954. int
  955. ruv_last_modified_to_smod(const RUV *ruv, Slapi_Mod *smod)
  956. {
  957. int return_value;
  958. if (ruv == NULL || smod == NULL)
  959. {
  960. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_last_modified_to_smod: NULL argument\n");
  961. return_value = RUV_BAD_DATA;
  962. }
  963. else
  964. {
  965. struct berval val;
  966. RUVElement *replica;
  967. int cookie;
  968. char buf[B_SIZ];
  969. PR_RWLock_Rlock (ruv->lock);
  970. slapi_mod_init (smod, dl_get_count (ruv->elements));
  971. slapi_mod_set_type (smod, type_ruvElementUpdatetime);
  972. slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  973. val.bv_val = buf;
  974. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  975. replica = dl_get_next (ruv->elements, &cookie))
  976. {
  977. PR_snprintf(buf, B_SIZ, "%s%d%s%s} %08lx", prefix_ruvcsn, replica->rid,
  978. replica->replica_purl == NULL ? "" : " ",
  979. replica->replica_purl == NULL ? "" : replica->replica_purl,
  980. replica->last_modified);
  981. val.bv_len = strlen(buf);
  982. slapi_mod_add_value(smod, &val);
  983. }
  984. PR_RWLock_Unlock (ruv->lock);
  985. return_value = RUV_SUCCESS;
  986. }
  987. return return_value;
  988. }
  989. /*
  990. * XXXggood do we need "ruv_covers_ruv_strict" ???? */
  991. PRBool
  992. ruv_covers_ruv(const RUV *covering_ruv, const RUV *covered_ruv)
  993. {
  994. PRBool return_value = PR_TRUE;
  995. RUVElement *replica;
  996. int cookie;
  997. /* compare replica generations first */
  998. if (covering_ruv->replGen == NULL || covered_ruv->replGen == NULL)
  999. return PR_FALSE;
  1000. if (strcasecmp (covered_ruv->replGen, covering_ruv->replGen))
  1001. return PR_FALSE;
  1002. /* replica generation is the same, now compare element by element */
  1003. for (replica = dl_get_first (covered_ruv->elements, &cookie);
  1004. NULL != replica;
  1005. replica = dl_get_next (covered_ruv->elements, &cookie))
  1006. {
  1007. if (replica->csn &&
  1008. (ruv_covers_csn(covering_ruv, replica->csn) == PR_FALSE))
  1009. {
  1010. return_value = PR_FALSE;
  1011. /* Don't break here - may leave something referenced? */
  1012. }
  1013. }
  1014. return return_value;
  1015. }
  1016. PRInt32
  1017. ruv_replica_count (const RUV *ruv)
  1018. {
  1019. if (ruv == NULL)
  1020. return 0;
  1021. else
  1022. {
  1023. int count;
  1024. PR_RWLock_Rlock (ruv->lock);
  1025. count = dl_get_count (ruv->elements);
  1026. PR_RWLock_Unlock (ruv->lock);
  1027. return count;
  1028. }
  1029. }
  1030. /*
  1031. * Extract all the referral URL's from the RUV (but self URL),
  1032. * returning them in an array of strings, that
  1033. * the caller must free.
  1034. */
  1035. char **
  1036. ruv_get_referrals(const RUV *ruv)
  1037. {
  1038. char **r= NULL;
  1039. int n;
  1040. const char *mypurl = multimaster_get_local_purl();
  1041. PR_RWLock_Rlock (ruv->lock);
  1042. n = ruv_replica_count(ruv);
  1043. if(n>0)
  1044. {
  1045. RUVElement *replica;
  1046. int cookie;
  1047. int i= 0;
  1048. r= (char**)slapi_ch_calloc(sizeof(char*),n+1);
  1049. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1050. replica = dl_get_next (ruv->elements, &cookie))
  1051. {
  1052. /* Add URL into referrals if doesn't match self URL */
  1053. if((replica->replica_purl!=NULL) &&
  1054. (slapi_utf8casecmp((unsigned char *)replica->replica_purl,
  1055. (unsigned char *)mypurl) != 0))
  1056. {
  1057. r[i]= slapi_ch_strdup(replica->replica_purl);
  1058. i++;
  1059. }
  1060. }
  1061. }
  1062. PR_RWLock_Unlock (ruv->lock);
  1063. return r; /* Caller must free this */
  1064. }
  1065. void
  1066. ruv_dump(const RUV *ruv, char *ruv_name, PRFileDesc *prFile)
  1067. {
  1068. RUVElement *replica;
  1069. int cookie;
  1070. char csnstr1[CSN_STRSIZE];
  1071. char csnstr2[CSN_STRSIZE];
  1072. char buff[RUVSTR_SIZE];
  1073. int len = sizeof (buff);
  1074. PR_ASSERT(NULL != ruv);
  1075. PR_RWLock_Rlock (ruv->lock);
  1076. PR_snprintf (buff, len, "%s: {replicageneration} %s\n",
  1077. ruv_name ? ruv_name : type_ruvElement,
  1078. ruv->replGen == NULL ? "" : ruv->replGen);
  1079. if (prFile)
  1080. {
  1081. slapi_write_buffer (prFile, buff, strlen(buff));
  1082. }
  1083. else
  1084. {
  1085. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, buff);
  1086. }
  1087. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1088. replica = dl_get_next (ruv->elements, &cookie))
  1089. {
  1090. /* prefix_ruvcsn = "{replica " */
  1091. PR_snprintf (buff, len, "%s: %s%d%s%s} %s %s\n",
  1092. ruv_name ? ruv_name : type_ruvElement,
  1093. prefix_ruvcsn, replica->rid,
  1094. replica->replica_purl == NULL ? "" : " ",
  1095. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1096. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  1097. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  1098. if (strlen (csnstr1) > 0) {
  1099. PR_snprintf (buff + strlen(buff) - 1, len - strlen(buff), " %08lx\n",
  1100. replica->last_modified);
  1101. }
  1102. if (prFile)
  1103. {
  1104. slapi_write_buffer (prFile, buff, strlen(buff));
  1105. }
  1106. else
  1107. {
  1108. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, buff);
  1109. }
  1110. }
  1111. PR_RWLock_Unlock (ruv->lock);
  1112. }
  1113. /* this function notifies the ruv that there are operations in progress so that
  1114. they can be added to the pending list for the appropriate client. */
  1115. int ruv_add_csn_inprogress (RUV *ruv, const CSN *csn)
  1116. {
  1117. RUVElement* replica;
  1118. char csn_str[CSN_STRSIZE];
  1119. int rc = RUV_SUCCESS;
  1120. PR_ASSERT (ruv && csn);
  1121. /* locate ruvElement */
  1122. PR_RWLock_Wlock (ruv->lock);
  1123. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1124. if (replica == NULL)
  1125. {
  1126. replica = ruvAddReplicaNoCSN (ruv, csn_get_replicaid (csn), NULL/*purl*/);
  1127. if (replica == NULL)
  1128. {
  1129. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to add replica"
  1130. " that created csn %s\n", csn_as_string (csn, PR_FALSE, csn_str));
  1131. rc = RUV_MEMORY_ERROR;
  1132. goto done;
  1133. }
  1134. }
  1135. /* check first that this csn is not already covered by this RUV */
  1136. if (ruv_covers_csn_internal(ruv, csn, PR_FALSE))
  1137. {
  1138. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1139. "the csn %s has already be seen - ignoring\n",
  1140. csn_as_string (csn, PR_FALSE, csn_str));
  1141. rc = RUV_COVERS_CSN;
  1142. goto done;
  1143. }
  1144. rc = csnplInsert (replica->csnpl, csn);
  1145. if (rc == 1) /* we already seen this csn */
  1146. {
  1147. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1148. "the csn %s has already be seen - ignoring\n",
  1149. csn_as_string (csn, PR_FALSE, csn_str));
  1150. rc = RUV_COVERS_CSN;
  1151. }
  1152. else if(rc != 0)
  1153. {
  1154. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to insert csn %s"
  1155. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1156. rc = RUV_UNKNOWN_ERROR;
  1157. }
  1158. else
  1159. {
  1160. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: successfully inserted csn %s"
  1161. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1162. rc = RUV_SUCCESS;
  1163. }
  1164. done:
  1165. PR_RWLock_Unlock (ruv->lock);
  1166. return rc;
  1167. }
  1168. int ruv_cancel_csn_inprogress (RUV *ruv, const CSN *csn)
  1169. {
  1170. RUVElement* replica;
  1171. int rc = RUV_SUCCESS;
  1172. PR_ASSERT (ruv && csn);
  1173. /* locate ruvElement */
  1174. PR_RWLock_Wlock (ruv->lock);
  1175. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1176. if (replica == NULL)
  1177. {
  1178. /* ONREPL - log error */
  1179. rc = RUV_NOTFOUND;
  1180. goto done;
  1181. }
  1182. rc = csnplRemove (replica->csnpl, csn);
  1183. if (rc != 0)
  1184. rc = RUV_NOTFOUND;
  1185. else
  1186. rc = RUV_SUCCESS;
  1187. done:
  1188. PR_RWLock_Unlock (ruv->lock);
  1189. return rc;
  1190. }
  1191. int ruv_update_ruv (RUV *ruv, const CSN *csn, const char *replica_purl, PRBool isLocal)
  1192. {
  1193. int rc=RUV_SUCCESS;
  1194. char csn_str[CSN_STRSIZE];
  1195. CSN *max_csn;
  1196. CSN *first_csn = NULL;
  1197. RUVElement *replica;
  1198. PR_ASSERT (ruv && csn);
  1199. PR_RWLock_Wlock (ruv->lock);
  1200. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1201. if (replica == NULL)
  1202. {
  1203. /* we should have a ruv element at this point because it would have
  1204. been added by ruv_add_inprogress function */
  1205. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1206. "can't locate RUV element for replica %d\n", csn_get_replicaid (csn));
  1207. goto done;
  1208. }
  1209. if (csnplCommit(replica->csnpl, csn) != 0)
  1210. {
  1211. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "ruv_update_ruv: cannot commit csn %s\n",
  1212. csn_as_string(csn, PR_FALSE, csn_str));
  1213. rc = RUV_CSNPL_ERROR;
  1214. goto done;
  1215. }
  1216. else
  1217. {
  1218. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1219. "successfully committed csn %s\n", csn_as_string(csn, PR_FALSE, csn_str));
  1220. }
  1221. if ((max_csn = csnplRollUp(replica->csnpl, &first_csn)) != NULL)
  1222. {
  1223. #ifdef DEBUG
  1224. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: rolled up to csn %s\n",
  1225. csn_as_string(max_csn, PR_FALSE, csn_str)); /* XXXggood remove debugging */
  1226. #endif
  1227. /* replica object sets min csn for local replica */
  1228. if (!isLocal && replica->min_csn == NULL) {
  1229. /* bug 559223 - it seems that, under huge stress, a server might pass
  1230. * through this code when more than 1 change has already been sent and commited into
  1231. * the pending lists... Therefore, as we are trying to set the min_csn ever
  1232. * generated by this replica, we need to set the first_csn as the min csn in the
  1233. * ruv */
  1234. set_min_csn_nolock(ruv, first_csn, replica_purl);
  1235. }
  1236. set_max_csn_nolock(ruv, max_csn, replica_purl);
  1237. /* It is possible that first_csn points to max_csn.
  1238. We need to free it once */
  1239. if (max_csn != first_csn) {
  1240. csn_free(&first_csn);
  1241. }
  1242. csn_free(&max_csn);
  1243. }
  1244. done:
  1245. PR_RWLock_Unlock (ruv->lock);
  1246. return rc;
  1247. }
  1248. /* Helper functions */
  1249. static int
  1250. ruvInit (RUV **ruv, int initCount)
  1251. {
  1252. PR_ASSERT (ruv);
  1253. /* allocate new RUV */
  1254. *ruv = (RUV *)slapi_ch_calloc (1, sizeof (RUV));
  1255. /* allocate elements */
  1256. (*ruv)->elements = dl_new (); /* never returns NULL */
  1257. dl_init ((*ruv)->elements, initCount);
  1258. /* create lock */
  1259. (*ruv)->lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "ruv_lock");
  1260. if ((*ruv)->lock == NULL)
  1261. {
  1262. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1263. "ruvInit: failed to create lock\n");
  1264. if (*ruv) {
  1265. dl_free(&(*ruv)->elements);
  1266. }
  1267. slapi_ch_free((void**)ruv);
  1268. return RUV_NSPR_ERROR;
  1269. }
  1270. return RUV_SUCCESS;
  1271. }
  1272. static void
  1273. ruvFreeReplica (void **data)
  1274. {
  1275. RUVElement *element = *(RUVElement**)data;
  1276. if (NULL != element)
  1277. {
  1278. if (NULL != element->csn)
  1279. {
  1280. csn_free (&element->csn);
  1281. }
  1282. if (NULL != element->min_csn)
  1283. {
  1284. csn_free (&element->min_csn);
  1285. }
  1286. /* slapi_ch_free accepts NULL pointer */
  1287. slapi_ch_free((void **)&element->replica_purl);
  1288. if (element->csnpl)
  1289. {
  1290. csnplFree (&(element->csnpl));
  1291. }
  1292. slapi_ch_free ((void **)&element);
  1293. }
  1294. }
  1295. static RUVElement*
  1296. ruvAddReplica (RUV *ruv, const CSN *csn, const char *replica_purl)
  1297. {
  1298. RUVElement *replica;
  1299. PR_ASSERT (NULL != ruv);
  1300. PR_ASSERT (NULL != csn);
  1301. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1302. if (replica == NULL)
  1303. {
  1304. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1305. "ruvAddReplica: memory allocation failed\n");
  1306. return NULL;
  1307. }
  1308. replica->rid = csn_get_replicaid (csn);
  1309. /* PR_ASSERT(replica->rid != READ_ONLY_REPLICA_ID); */
  1310. replica->csn = csn_dup (csn);
  1311. replica->last_modified = current_time();
  1312. replica->min_csn = csn_dup (csn);
  1313. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1314. replica->csnpl = csnplNew ();
  1315. dl_add (ruv->elements, replica);
  1316. return replica;
  1317. }
  1318. static RUVElement*
  1319. ruvAddReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl)
  1320. {
  1321. RUVElement *replica;
  1322. PR_ASSERT (NULL != ruv);
  1323. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1324. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1325. if (replica == NULL)
  1326. {
  1327. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1328. "ruvAddReplicaNoCSN: memory allocation failed\n");
  1329. return NULL;
  1330. }
  1331. replica->rid = rid;
  1332. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1333. replica->csnpl = csnplNew ();
  1334. dl_add (ruv->elements, replica);
  1335. return replica;
  1336. }
  1337. static RUVElement*
  1338. ruvAddIndexReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl, int index)
  1339. {
  1340. RUVElement *replica;
  1341. PR_ASSERT (NULL != ruv);
  1342. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1343. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1344. if (replica == NULL)
  1345. {
  1346. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1347. "ruvAddIndexReplicaNoCSN: memory allocation failed\n");
  1348. return NULL;
  1349. }
  1350. replica->rid = rid;
  1351. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1352. replica->csnpl = csnplNew ();
  1353. dl_add_index (ruv->elements, replica, index);
  1354. return replica;
  1355. }
  1356. static RUVElement *
  1357. ruvGetReplica (const RUV *ruv, ReplicaId rid)
  1358. {
  1359. PR_ASSERT (ruv /* && rid >= 0 -- rid can't be negative */);
  1360. return (RUVElement *)dl_get (ruv->elements, (const void*)&rid, ruvReplicaCompare);
  1361. }
  1362. static int
  1363. ruvReplicaCompare (const void *el1, const void *el2)
  1364. {
  1365. RUVElement *replica = (RUVElement*)el1;
  1366. ReplicaId *rid1 = (ReplicaId*) el2;
  1367. if (replica == NULL || rid1 == NULL)
  1368. return -1;
  1369. if (*rid1 == replica->rid)
  1370. return 0;
  1371. if (*rid1 < replica->rid)
  1372. return -1;
  1373. else
  1374. return 1;
  1375. }
  1376. /*
  1377. * Given a berval that points to a string of the form:
  1378. * "{dbgen} generation-id", return a copy of the
  1379. * "generation-id" part in a null-terminated string.
  1380. * Returns NULL if the berval is malformed.
  1381. */
  1382. static char *
  1383. get_replgen_from_berval(const struct berval *bval)
  1384. {
  1385. char *ret_string = NULL;
  1386. if (NULL != bval && NULL != bval->bv_val &&
  1387. (bval->bv_len > strlen(prefix_replicageneration)) &&
  1388. strncasecmp(bval->bv_val, prefix_replicageneration,
  1389. strlen(prefix_replicageneration)) == 0)
  1390. {
  1391. unsigned int index = strlen(prefix_replicageneration);
  1392. /* Skip any whitespace */
  1393. while (index++ < bval->bv_len && bval->bv_val[index] == ' ');
  1394. if (index < bval->bv_len)
  1395. {
  1396. unsigned int ret_len = bval->bv_len - index;
  1397. ret_string = slapi_ch_malloc(ret_len + 1);
  1398. memcpy(ret_string, &bval->bv_val[index], ret_len);
  1399. ret_string[ret_len] = '\0';
  1400. }
  1401. }
  1402. return ret_string;
  1403. }
  1404. /*
  1405. * Given a berval that points to a string of the form:
  1406. * "{replica ldap[s]//host:port} <min_csn> <csn>", parse out the
  1407. * partial URL and the CSNs into an RUVElement, and return
  1408. * a pointer to the copy. Returns NULL if the berval is
  1409. * malformed.
  1410. */
  1411. static RUVElement *
  1412. get_ruvelement_from_berval(const struct berval *bval)
  1413. {
  1414. RUVElement *ret_ruve = NULL;
  1415. char *purl = NULL;
  1416. ReplicaId rid = 0;
  1417. char ridbuff [RIDSTR_SIZE];
  1418. int i;
  1419. if (NULL == bval || NULL == bval->bv_val ||
  1420. bval->bv_len <= strlen(prefix_ruvcsn) ||
  1421. strncasecmp(bval->bv_val, prefix_ruvcsn, strlen(prefix_ruvcsn)) != 0)
  1422. {
  1423. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1424. "get_ruvelement_from_berval: invalid berval\n");
  1425. goto loser;
  1426. }
  1427. {
  1428. unsigned int urlbegin = strlen(prefix_ruvcsn);
  1429. unsigned int urlend;
  1430. unsigned int mincsnbegin;
  1431. /* replica id must be here */
  1432. i = 0;
  1433. while (isdigit (bval->bv_val[urlbegin]))
  1434. {
  1435. ridbuff [i] = bval->bv_val[urlbegin];
  1436. i++;
  1437. urlbegin ++;
  1438. }
  1439. if (i == 0) /* replicaid is missing */
  1440. goto loser;
  1441. ridbuff[i] = '\0';
  1442. rid = atoi (ridbuff);
  1443. if (bval->bv_val[urlbegin] == '}')
  1444. {
  1445. /* No purl in this value */
  1446. purl = NULL;
  1447. mincsnbegin = urlbegin + 1;
  1448. }
  1449. else
  1450. {
  1451. while (urlbegin++ < bval->bv_len && bval->bv_val[urlbegin] == ' ');
  1452. urlend = urlbegin;
  1453. while (urlend++ < bval->bv_len && bval->bv_val[urlend] != '}');
  1454. purl = slapi_ch_malloc(urlend - urlbegin + 1);
  1455. memcpy(purl, &bval->bv_val[urlbegin], urlend - urlbegin);
  1456. purl[urlend - urlbegin] = '\0';
  1457. mincsnbegin = urlend;
  1458. }
  1459. /* Skip any whitespace before the first (minimum) CSN */
  1460. while (mincsnbegin++ < (bval->bv_len-1) && bval->bv_val[mincsnbegin] == ' ');
  1461. /* Now, mincsnbegin should contain the index of the beginning of the first csn */
  1462. if (mincsnbegin >= bval->bv_len)
  1463. {
  1464. /* Missing the entire content*/
  1465. if (purl == NULL)
  1466. goto loser;
  1467. else /* we have just purl - no changes from the replica has been seen */
  1468. {
  1469. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1470. ret_ruve->rid = rid;
  1471. ret_ruve->replica_purl = purl;
  1472. }
  1473. }
  1474. else
  1475. {
  1476. if (bval->bv_len - mincsnbegin != (_CSN_VALIDCSN_STRLEN * 2) + 1)
  1477. {
  1478. /* Malformed - incorrect length for 2 CSNs + space */
  1479. goto loser;
  1480. }
  1481. else
  1482. {
  1483. char mincsnstr[CSN_STRSIZE];
  1484. char maxcsnstr[CSN_STRSIZE];
  1485. memset(mincsnstr, '\0', CSN_STRSIZE);
  1486. memset(maxcsnstr, '\0', CSN_STRSIZE);
  1487. memcpy(mincsnstr, &bval->bv_val[mincsnbegin], _CSN_VALIDCSN_STRLEN);
  1488. memcpy(maxcsnstr, &bval->bv_val[mincsnbegin + _CSN_VALIDCSN_STRLEN + 1], _CSN_VALIDCSN_STRLEN);
  1489. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1490. ret_ruve->min_csn = csn_new_by_string(mincsnstr);
  1491. ret_ruve->csn = csn_new_by_string(maxcsnstr);
  1492. ret_ruve->rid = rid;
  1493. ret_ruve->replica_purl = purl;
  1494. if (NULL == ret_ruve->min_csn || NULL == ret_ruve->csn)
  1495. {
  1496. goto loser;
  1497. }
  1498. }
  1499. }
  1500. }
  1501. /* initialize csn pending list */
  1502. ret_ruve->csnpl = csnplNew ();
  1503. if (ret_ruve->csnpl == NULL)
  1504. {
  1505. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1506. "get_ruvelement_from_berval: failed to create csn pending list\n");
  1507. goto loser;
  1508. }
  1509. return ret_ruve;
  1510. loser:
  1511. /* slapi_ch_free accepts NULL pointer */
  1512. slapi_ch_free((void **)&purl);
  1513. if (NULL != ret_ruve)
  1514. {
  1515. if (NULL != ret_ruve->min_csn)
  1516. {
  1517. csn_free(&ret_ruve->min_csn);
  1518. }
  1519. if (NULL != ret_ruve->csn)
  1520. {
  1521. csn_free(&ret_ruve->csn);
  1522. }
  1523. slapi_ch_free((void **)&ret_ruve);
  1524. }
  1525. return NULL;
  1526. }
  1527. int
  1528. ruv_move_local_supplier_to_first(RUV *ruv, ReplicaId aRid)
  1529. {
  1530. RUVElement * elem = NULL;
  1531. int rc = RUV_NOTFOUND;
  1532. PR_ASSERT(ruv);
  1533. PR_RWLock_Wlock (ruv->lock);
  1534. elem = (RUVElement *)dl_delete(ruv->elements,(const void*)&aRid, ruvReplicaCompare, 0);
  1535. if (elem) {
  1536. dl_add_index(ruv->elements, elem, 1);
  1537. rc = RUV_SUCCESS;
  1538. }
  1539. PR_RWLock_Unlock (ruv->lock);
  1540. return rc;
  1541. }
  1542. int
  1543. ruv_get_first_id_and_purl(RUV *ruv, ReplicaId *rid, char **replica_purl )
  1544. {
  1545. RUVElement * first = NULL;
  1546. int cookie;
  1547. int rc;
  1548. PR_ASSERT(ruv);
  1549. PR_RWLock_Rlock (ruv->lock);
  1550. first = dl_get_first(ruv->elements, &cookie);
  1551. if ( first == NULL )
  1552. {
  1553. rc = RUV_MEMORY_ERROR;
  1554. }
  1555. else
  1556. {
  1557. *rid = first->rid;
  1558. *replica_purl = first->replica_purl;
  1559. rc = RUV_SUCCESS;
  1560. }
  1561. PR_RWLock_Unlock (ruv->lock);
  1562. return rc;
  1563. }
  1564. int ruv_local_contains_supplier(RUV *ruv, ReplicaId rid)
  1565. {
  1566. int cookie;
  1567. RUVElement *elem = NULL;
  1568. PR_ASSERT(ruv);
  1569. PR_RWLock_Rlock (ruv->lock);
  1570. for (elem = dl_get_first (ruv->elements, &cookie);
  1571. elem;
  1572. elem = dl_get_next (ruv->elements, &cookie))
  1573. {
  1574. if (elem->rid == rid){
  1575. PR_RWLock_Unlock (ruv->lock);
  1576. return 1;
  1577. }
  1578. }
  1579. PR_RWLock_Unlock (ruv->lock);
  1580. return 0;
  1581. }
  1582. PRBool ruv_has_csns(const RUV *ruv)
  1583. {
  1584. PRBool retval = PR_TRUE;
  1585. CSN *mincsn = NULL;
  1586. CSN *maxcsn = NULL;
  1587. ruv_get_min_csn(ruv, &mincsn);
  1588. ruv_get_max_csn(ruv, &maxcsn);
  1589. if (mincsn) {
  1590. csn_free(&mincsn);
  1591. csn_free(&maxcsn);
  1592. } else if (maxcsn) {
  1593. csn_free(&maxcsn);
  1594. } else {
  1595. retval = PR_FALSE; /* both min and max are false */
  1596. }
  1597. return retval;
  1598. }
  1599. PRBool ruv_has_both_csns(const RUV *ruv)
  1600. {
  1601. PRBool retval = PR_TRUE;
  1602. CSN *mincsn = NULL;
  1603. CSN *maxcsn = NULL;
  1604. ruv_get_min_csn(ruv, &mincsn);
  1605. ruv_get_max_csn(ruv, &maxcsn);
  1606. if (mincsn) {
  1607. csn_free(&mincsn);
  1608. csn_free(&maxcsn);
  1609. } else if (maxcsn) {
  1610. csn_free(&maxcsn);
  1611. retval = PR_FALSE; /* it has a maxcsn but no mincsn */
  1612. } else {
  1613. retval = PR_FALSE; /* both min and max are false */
  1614. }
  1615. return retval;
  1616. }
  1617. /* Check if the first ruv is newer than the second one */
  1618. PRBool
  1619. ruv_is_newer (Object *sruvobj, Object *cruvobj)
  1620. {
  1621. RUV *sruv, *cruv;
  1622. RUVElement *sreplica, *creplica;
  1623. int scookie, ccookie;
  1624. int is_newer = PR_FALSE;
  1625. if ( sruvobj == NULL ) {
  1626. return 0;
  1627. }
  1628. if ( cruvobj == NULL ) {
  1629. return 1;
  1630. }
  1631. sruv = (RUV *) object_get_data ( sruvobj );
  1632. cruv = (RUV *) object_get_data ( cruvobj );
  1633. for (sreplica = dl_get_first (sruv->elements, &scookie); sreplica;
  1634. sreplica = dl_get_next (sruv->elements, &scookie))
  1635. {
  1636. /* A hub may have a dummy ruv with rid 65535 */
  1637. if ( sreplica->csn == NULL ) continue;
  1638. if ( cruv->elements == NULL )
  1639. {
  1640. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1641. "ruv_is_newer, consumer RUV has no elements\n");
  1642. is_newer=PR_FALSE;
  1643. break;
  1644. }
  1645. for (creplica = dl_get_first (cruv->elements, &ccookie); creplica;
  1646. creplica = dl_get_next (cruv->elements, &ccookie))
  1647. {
  1648. if ( sreplica->rid == creplica->rid ) {
  1649. if ( csn_compare ( sreplica->csn, creplica->csn ) > 0 ) {
  1650. is_newer = PR_TRUE;
  1651. }
  1652. break;
  1653. }
  1654. }
  1655. if ( creplica == NULL || is_newer ) {
  1656. is_newer = PR_TRUE;
  1657. break;
  1658. }
  1659. }
  1660. return is_newer;
  1661. }
  1662. void
  1663. ruv_force_csn_update (RUV *ruv, CSN *csn)
  1664. {
  1665. CSN *max = NULL;
  1666. if (ruv != NULL)
  1667. {
  1668. ruv_get_max_csn(ruv, &max);
  1669. if (csn_compare(max, csn))
  1670. {
  1671. ruv_set_max_csn(ruv, csn, NULL);
  1672. }
  1673. csn_free(&max);
  1674. }
  1675. }
  1676. #ifdef TESTING /* Some unit tests for code in this file */
  1677. static void
  1678. ruv_dump_internal(RUV *ruv)
  1679. {
  1680. RUVElement *replica;
  1681. int cookie;
  1682. char csnstr1[CSN_STRSIZE];
  1683. char csnstr2[CSN_STRSIZE];
  1684. PR_ASSERT(NULL != ruv);
  1685. printf("{replicageneration} %s\n", ruv->replGen == NULL ? "NULL" : ruv->replGen);
  1686. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1687. replica = dl_get_next (ruv->elements, &cookie))
  1688. {
  1689. printf("{replica%s%s} %s %s\n",
  1690. replica->replica_purl == NULL ? "" : " ",
  1691. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1692. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  1693. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  1694. }
  1695. }
  1696. void
  1697. ruv_test()
  1698. {
  1699. const struct berval *vals[5];
  1700. struct berval val0, val1, val2, val3;
  1701. RUV *ruv;
  1702. Slapi_Attr *attr;
  1703. Slapi_Value *sv0, *sv1, *sv2, *sv3;
  1704. int rc;
  1705. char csnstr[CSN_STRSIZE];
  1706. char *gen;
  1707. CSN *newcsn;
  1708. ReplicaId *ids;
  1709. int nids;
  1710. Slapi_Mod smods;
  1711. PRBool covers;
  1712. vals[0] = &val0;
  1713. vals[1] = &val1;
  1714. vals[2] = &val2;
  1715. vals[3] = &val3;
  1716. vals[4] = NULL;
  1717. val0.bv_val = "{replicageneration} 0440FDC0A33F";
  1718. val0.bv_len = strlen(val0.bv_val);
  1719. val1.bv_val = "{replica ldap://ggood.mcom.com:389} 12345670000000FE0000 12345671000000FE0000";
  1720. val1.bv_len = strlen(val1.bv_val);
  1721. val2.bv_val = "{replica ldaps://an-impossibly-long-host-name-that-drags-on-forever-and-forever.mcom.com:389} 11112110000000FF0000 11112111000000FF0000";
  1722. val2.bv_len = strlen(val2.bv_val);
  1723. val3.bv_val = "{replica} 12345672000000FD0000 12345673000000FD0000";
  1724. val3.bv_len = strlen(val3.bv_val);
  1725. rc = ruv_init_from_bervals(vals, &ruv);
  1726. ruv_dump_internal(ruv);
  1727. attr = slapi_attr_new();
  1728. attr = slapi_attr_init(attr, "ruvelement");
  1729. sv0 = slapi_value_new();
  1730. sv1 = slapi_value_new();
  1731. sv2 = slapi_value_new();
  1732. sv3 = slapi_value_new();
  1733. slapi_value_init_berval(sv0, &val0);
  1734. slapi_value_init_berval(sv1, &val1);
  1735. slapi_value_init_berval(sv2, &val2);
  1736. slapi_value_init_berval(sv3, &val3);
  1737. slapi_attr_add_value(attr, sv0);
  1738. slapi_attr_add_value(attr, sv1);
  1739. slapi_attr_add_value(attr, sv2);
  1740. slapi_attr_add_value(attr, sv3);
  1741. rc = ruv_init_from_slapi_attr(attr, &ruv);
  1742. ruv_dump_internal(ruv);
  1743. rc = ruv_delete_replica(ruv, 0xFF);
  1744. /* Should delete one replica */
  1745. ruv_dump_internal(ruv);
  1746. rc = ruv_delete_replica(ruv, 0xAA);
  1747. /* No such replica - should not do anything */
  1748. ruv_dump_internal(ruv);
  1749. rc = ruv_get_largest_csn_for_replica(ruv, 0xFE, &newcsn);
  1750. if (NULL != newcsn)
  1751. {
  1752. csn_as_string(newcsn, PR_FALSE, csnstr);
  1753. printf("Replica 0x%X has largest csn \"%s\"\n", 0xFE, csnstr);
  1754. }
  1755. else
  1756. {
  1757. printf("BAD - can't get largest CSN for replica 0x%X\n", 0xFE);
  1758. }
  1759. rc = ruv_get_smallest_csn_for_replica(ruv, 0xFE, &newcsn);
  1760. if (NULL != newcsn)
  1761. {
  1762. csn_as_string(newcsn, PR_FALSE, csnstr);
  1763. printf("Replica 0x%X has smallest csn \"%s\"\n", 0xFE, csnstr);
  1764. }
  1765. else
  1766. {
  1767. printf("BAD - can't get smallest CSN for replica 0x%X\n", 0xFE);
  1768. }
  1769. rc = ruv_get_largest_csn_for_replica(ruv, 0xAA, &newcsn);
  1770. printf("ruv_get_largest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  1771. rc = ruv_get_smallest_csn_for_replica(ruv, 0xAA, &newcsn);
  1772. printf("ruv_get_smallest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  1773. newcsn = csn_new_by_string("12345674000000FE0000"); /* Old replica 0xFE */
  1774. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1775. /* Should update replica FE's CSN */
  1776. ruv_dump_internal(ruv);
  1777. newcsn = csn_new_by_string("12345675000000FB0000"); /* New replica 0xFB */
  1778. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1779. /* Should get a new replica in the list with min == max csn */
  1780. ruv_dump_internal(ruv);
  1781. newcsn = csn_new_by_string("12345676000000FD0000"); /* Old replica 0xFD */
  1782. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1783. /* Should update replica 0xFD so new CSN is newer than min CSN */
  1784. ruv_dump_internal(ruv);
  1785. gen = ruv_get_replica_generation(ruv);
  1786. printf("replica generation is \"%s\"\n", gen);
  1787. newcsn = csn_new_by_string("12345673000000FE0000"); /* Old replica 0xFE */
  1788. covers = ruv_covers_csn(ruv, newcsn); /* should say "true" */
  1789. newcsn = csn_new_by_string("12345675000000FE0000"); /* Old replica 0xFE */
  1790. covers = ruv_covers_csn(ruv, newcsn); /* Should say "false" */
  1791. newcsn = csn_new_by_string("123456700000000A0000"); /* New replica 0A */
  1792. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  1793. ruv_dump_internal(ruv);
  1794. newcsn = csn_new_by_string("123456710000000A0000"); /* New replica 0A */
  1795. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  1796. ruv_dump_internal(ruv);
  1797. newcsn = csn_new_by_string("123456700000000B0000"); /* New replica 0B */
  1798. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  1799. ruv_dump_internal(ruv);
  1800. newcsn = csn_new_by_string("123456710000000B0000"); /* New replica 0B */
  1801. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  1802. ruv_dump_internal(ruv);
  1803. /* ONREPL test ruv enumeration */
  1804. rc = ruv_to_smod(ruv, &smods);
  1805. ruv_destroy(&ruv);
  1806. }
  1807. #endif /* TESTING */