repl5_ruv.c 66 KB

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