repl5_ruv.c 51 KB

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