repl5_ruv.c 67 KB

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