repl5_ruv.c 69 KB

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