repl5_ruv.c 53 KB

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