repl5_ruv.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  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. if (!ruv) {
  708. return return_str;
  709. }
  710. PR_RWLock_Rlock (ruv->lock);
  711. if (ruv != NULL && ruv->replGen != NULL)
  712. {
  713. return_str = slapi_ch_strdup(ruv->replGen);
  714. }
  715. PR_RWLock_Unlock (ruv->lock);
  716. return return_str;
  717. }
  718. static PRBool
  719. ruv_covers_csn_internal(const RUV *ruv, const CSN *csn, PRBool strict)
  720. {
  721. RUVElement *replica;
  722. ReplicaId rid;
  723. PRBool return_value;
  724. if (ruv == NULL || csn == NULL)
  725. {
  726. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn: NULL argument\n");
  727. return_value = PR_FALSE;
  728. }
  729. else
  730. {
  731. rid = csn_get_replicaid(csn);
  732. replica = ruvGetReplica (ruv, rid);
  733. if (replica == NULL)
  734. {
  735. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn: replica for id %d not found\n", rid);
  736. return_value = PR_FALSE;
  737. }
  738. else
  739. {
  740. if (strict)
  741. {
  742. return_value = (csn_compare (csn, replica->csn) < 0);
  743. }
  744. else
  745. {
  746. return_value = (csn_compare (csn, replica->csn) <= 0);
  747. }
  748. }
  749. }
  750. return return_value;
  751. }
  752. PRBool
  753. ruv_covers_csn(const RUV *ruv, const CSN *csn)
  754. {
  755. PRBool rc;
  756. PR_RWLock_Rlock (ruv->lock);
  757. rc = ruv_covers_csn_internal(ruv, csn, PR_FALSE);
  758. PR_RWLock_Unlock (ruv->lock);
  759. return rc;
  760. }
  761. PRBool
  762. ruv_covers_csn_strict(const RUV *ruv, const CSN *csn)
  763. {
  764. PRBool rc;
  765. PR_RWLock_Rlock (ruv->lock);
  766. rc = ruv_covers_csn_internal(ruv, csn, PR_TRUE);
  767. PR_RWLock_Unlock (ruv->lock);
  768. return rc;
  769. }
  770. /*
  771. * The function gets min{maxcsns of all ruv elements} if get_the_max=0,
  772. * or max{maxcsns of all ruv elements} if get_the_max != 0.
  773. */
  774. static int
  775. ruv_get_min_or_max_csn(const RUV *ruv, CSN **csn, int get_the_max)
  776. {
  777. int return_value;
  778. if (ruv == NULL || csn == NULL)
  779. {
  780. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_get_min_or_max_csn: NULL argument\n");
  781. return_value = RUV_BAD_DATA;
  782. }
  783. else
  784. {
  785. CSN *found = NULL;
  786. RUVElement *replica;
  787. int cookie;
  788. PR_RWLock_Rlock (ruv->lock);
  789. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  790. replica = dl_get_next (ruv->elements, &cookie))
  791. {
  792. /*
  793. * Skip replica whose maxcsn is NULL otherwise
  794. * the code will return different min_csn if
  795. * the sequence of the replicas is altered.
  796. *
  797. * don't use READ_ONLY replicas for computing the value of
  798. * "found", as they seem to have NULL csn and min_csn
  799. */
  800. if (replica->csn == NULL || replica->rid == READ_ONLY_REPLICA_ID)
  801. {
  802. continue;
  803. }
  804. if (found == NULL ||
  805. (!get_the_max && csn_compare(found, replica->csn)>0) ||
  806. ( get_the_max && csn_compare(found, replica->csn)<0))
  807. {
  808. found = replica->csn;
  809. }
  810. }
  811. if (found == NULL)
  812. {
  813. *csn = NULL;
  814. }
  815. else
  816. {
  817. *csn = csn_dup (found);
  818. }
  819. PR_RWLock_Unlock (ruv->lock);
  820. return_value = RUV_SUCCESS;
  821. }
  822. return return_value;
  823. }
  824. int
  825. ruv_get_max_csn(const RUV *ruv, CSN **csn)
  826. {
  827. return ruv_get_min_or_max_csn(ruv, csn, 1 /* get the max */);
  828. }
  829. int
  830. ruv_get_min_csn(const RUV *ruv, CSN **csn)
  831. {
  832. return ruv_get_min_or_max_csn(ruv, csn, 0 /* get the min */);
  833. }
  834. int
  835. ruv_enumerate_elements (const RUV *ruv, FNEnumRUV fn, void *arg)
  836. {
  837. int cookie;
  838. RUVElement *elem;
  839. int rc = 0;
  840. ruv_enum_data enum_data = {0};
  841. if (ruv == NULL || fn == NULL)
  842. {
  843. /* ONREPL - log error */
  844. return -1;
  845. }
  846. PR_RWLock_Rlock (ruv->lock);
  847. for (elem = (RUVElement*)dl_get_first (ruv->elements, &cookie); elem;
  848. elem = (RUVElement*)dl_get_next (ruv->elements, &cookie))
  849. {
  850. /* we only return elements that contains both minimal and maximal CSNs */
  851. if (elem->csn && elem->min_csn)
  852. {
  853. enum_data.csn = elem->csn;
  854. enum_data.min_csn = elem->min_csn;
  855. rc = fn (&enum_data, arg);
  856. if (rc != 0)
  857. break;
  858. }
  859. }
  860. PR_RWLock_Unlock (ruv->lock);
  861. return rc;
  862. }
  863. /*
  864. * Convert a replica update vector to a NULL-terminated array
  865. * of bervals. The caller is responsible for freeing the bervals.
  866. */
  867. int
  868. ruv_to_bervals(const RUV *ruv, struct berval ***bvals)
  869. {
  870. struct berval **returned_bervals = NULL;
  871. int return_value;
  872. if (ruv == NULL || bvals == NULL)
  873. {
  874. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_bervals: NULL argument\n");
  875. return_value = RUV_BAD_DATA;
  876. }
  877. else
  878. {
  879. int count;
  880. int i;
  881. RUVElement *replica;
  882. char csnStr1 [CSN_STRSIZE];
  883. char csnStr2 [CSN_STRSIZE];
  884. int cookie;
  885. PR_RWLock_Rlock (ruv->lock);
  886. count = dl_get_count (ruv->elements) + 2;
  887. returned_bervals = (struct berval **)slapi_ch_malloc(sizeof(struct berval *) * count);
  888. returned_bervals[count - 1] = NULL;
  889. returned_bervals[0] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  890. returned_bervals[0]->bv_val = slapi_ch_smprintf("%s %s",
  891. prefix_replicageneration, ruv->replGen);
  892. returned_bervals[0]->bv_len = strlen(returned_bervals[0]->bv_val);
  893. for (i = 1, replica = dl_get_first (ruv->elements, &cookie); replica;
  894. i++, replica = dl_get_next (ruv->elements, &cookie))
  895. {
  896. returned_bervals[i] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  897. returned_bervals[i]->bv_val = slapi_ch_smprintf("%s%d%s%s}%s%s%s%s",
  898. prefix_ruvcsn, replica->rid,
  899. replica->replica_purl == NULL ? "" : " ",
  900. replica->replica_purl == NULL ? "" : replica->replica_purl,
  901. replica->min_csn == NULL ? "" : " ",
  902. replica->min_csn == NULL ? "" : csn_as_string (replica->min_csn, PR_FALSE, csnStr1),
  903. replica->csn == NULL ? "" : " ",
  904. replica->csn == NULL ? "" : csn_as_string (replica->csn, PR_FALSE, csnStr2));
  905. returned_bervals[i]->bv_len = strlen(returned_bervals[i]->bv_val);
  906. }
  907. PR_RWLock_Unlock (ruv->lock);
  908. return_value = RUV_SUCCESS;
  909. *bvals = returned_bervals;
  910. }
  911. return return_value;
  912. }
  913. int
  914. ruv_to_smod(const RUV *ruv, Slapi_Mod *smod)
  915. {
  916. int return_value;
  917. if (ruv == NULL || smod == NULL)
  918. {
  919. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_smod: NULL argument\n");
  920. return_value = RUV_BAD_DATA;
  921. }
  922. else
  923. {
  924. struct berval val;
  925. RUVElement *replica;
  926. int cookie;
  927. char csnStr1 [CSN_STRSIZE];
  928. char csnStr2 [CSN_STRSIZE];
  929. #define B_SIZ 1024
  930. char buf[B_SIZ];
  931. PR_RWLock_Rlock (ruv->lock);
  932. slapi_mod_init (smod, dl_get_count (ruv->elements) + 1);
  933. slapi_mod_set_type (smod, type_ruvElement);
  934. slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  935. PR_snprintf(buf, B_SIZ, "%s %s", prefix_replicageneration, ruv->replGen);
  936. val.bv_val = buf;
  937. val.bv_len = strlen(buf);
  938. slapi_mod_add_value(smod, &val);
  939. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  940. replica = dl_get_next (ruv->elements, &cookie))
  941. {
  942. PR_snprintf(buf, B_SIZ, "%s%d%s%s}%s%s%s%s", prefix_ruvcsn, replica->rid,
  943. replica->replica_purl == NULL ? "" : " ",
  944. replica->replica_purl == NULL ? "" : replica->replica_purl,
  945. replica->min_csn == NULL ? "" : " ",
  946. replica->min_csn == NULL ? "" : csn_as_string (replica->min_csn, PR_FALSE, csnStr1),
  947. replica->csn == NULL ? "" : " ",
  948. replica->csn == NULL ? "" : csn_as_string (replica->csn, PR_FALSE, csnStr2));
  949. val.bv_len = strlen(buf);
  950. slapi_mod_add_value(smod, &val);
  951. }
  952. PR_RWLock_Unlock (ruv->lock);
  953. return_value = RUV_SUCCESS;
  954. }
  955. return return_value;
  956. }
  957. int
  958. ruv_last_modified_to_smod(const RUV *ruv, Slapi_Mod *smod)
  959. {
  960. int return_value;
  961. if (ruv == NULL || smod == NULL)
  962. {
  963. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_last_modified_to_smod: NULL argument\n");
  964. return_value = RUV_BAD_DATA;
  965. }
  966. else
  967. {
  968. struct berval val;
  969. RUVElement *replica;
  970. int cookie;
  971. char buf[B_SIZ];
  972. PR_RWLock_Rlock (ruv->lock);
  973. slapi_mod_init (smod, dl_get_count (ruv->elements));
  974. slapi_mod_set_type (smod, type_ruvElementUpdatetime);
  975. slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  976. val.bv_val = buf;
  977. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  978. replica = dl_get_next (ruv->elements, &cookie))
  979. {
  980. PR_snprintf(buf, B_SIZ, "%s%d%s%s} %08lx", prefix_ruvcsn, replica->rid,
  981. replica->replica_purl == NULL ? "" : " ",
  982. replica->replica_purl == NULL ? "" : replica->replica_purl,
  983. replica->last_modified);
  984. val.bv_len = strlen(buf);
  985. slapi_mod_add_value(smod, &val);
  986. }
  987. PR_RWLock_Unlock (ruv->lock);
  988. return_value = RUV_SUCCESS;
  989. }
  990. return return_value;
  991. }
  992. /*
  993. * XXXggood do we need "ruv_covers_ruv_strict" ???? */
  994. PRBool
  995. ruv_covers_ruv(const RUV *covering_ruv, const RUV *covered_ruv)
  996. {
  997. PRBool return_value = PR_TRUE;
  998. RUVElement *replica;
  999. int cookie;
  1000. /* compare replica generations first */
  1001. if (covering_ruv->replGen == NULL || covered_ruv->replGen == NULL)
  1002. return PR_FALSE;
  1003. if (strcasecmp (covered_ruv->replGen, covering_ruv->replGen))
  1004. return PR_FALSE;
  1005. /* replica generation is the same, now compare element by element */
  1006. for (replica = dl_get_first (covered_ruv->elements, &cookie);
  1007. NULL != replica;
  1008. replica = dl_get_next (covered_ruv->elements, &cookie))
  1009. {
  1010. if (replica->csn &&
  1011. (ruv_covers_csn(covering_ruv, replica->csn) == PR_FALSE))
  1012. {
  1013. return_value = PR_FALSE;
  1014. /* Don't break here - may leave something referenced? */
  1015. }
  1016. }
  1017. return return_value;
  1018. }
  1019. PRInt32
  1020. ruv_replica_count (const RUV *ruv)
  1021. {
  1022. if (ruv == NULL)
  1023. return 0;
  1024. else
  1025. {
  1026. int count;
  1027. PR_RWLock_Rlock (ruv->lock);
  1028. count = dl_get_count (ruv->elements);
  1029. PR_RWLock_Unlock (ruv->lock);
  1030. return count;
  1031. }
  1032. }
  1033. /*
  1034. * Extract all the referral URL's from the RUV (but self URL),
  1035. * returning them in an array of strings, that
  1036. * the caller must free.
  1037. */
  1038. char **
  1039. ruv_get_referrals(const RUV *ruv)
  1040. {
  1041. char **r= NULL;
  1042. int n;
  1043. const char *mypurl = multimaster_get_local_purl();
  1044. PR_RWLock_Rlock (ruv->lock);
  1045. n = ruv_replica_count(ruv);
  1046. if(n>0)
  1047. {
  1048. RUVElement *replica;
  1049. int cookie;
  1050. int i= 0;
  1051. r= (char**)slapi_ch_calloc(sizeof(char*),n+1);
  1052. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1053. replica = dl_get_next (ruv->elements, &cookie))
  1054. {
  1055. /* Add URL into referrals if doesn't match self URL */
  1056. if((replica->replica_purl!=NULL) &&
  1057. (slapi_utf8casecmp((unsigned char *)replica->replica_purl,
  1058. (unsigned char *)mypurl) != 0))
  1059. {
  1060. r[i]= slapi_ch_strdup(replica->replica_purl);
  1061. i++;
  1062. }
  1063. }
  1064. }
  1065. PR_RWLock_Unlock (ruv->lock);
  1066. return r; /* Caller must free this */
  1067. }
  1068. void
  1069. ruv_dump(const RUV *ruv, char *ruv_name, PRFileDesc *prFile)
  1070. {
  1071. RUVElement *replica;
  1072. int cookie;
  1073. char csnstr1[CSN_STRSIZE];
  1074. char csnstr2[CSN_STRSIZE];
  1075. char buff[RUVSTR_SIZE];
  1076. int len = sizeof (buff);
  1077. PR_ASSERT(NULL != ruv);
  1078. PR_RWLock_Rlock (ruv->lock);
  1079. PR_snprintf (buff, len, "%s: {replicageneration} %s\n",
  1080. ruv_name ? ruv_name : type_ruvElement,
  1081. ruv->replGen == NULL ? "" : ruv->replGen);
  1082. if (prFile)
  1083. {
  1084. slapi_write_buffer (prFile, buff, strlen(buff));
  1085. }
  1086. else
  1087. {
  1088. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, buff);
  1089. }
  1090. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1091. replica = dl_get_next (ruv->elements, &cookie))
  1092. {
  1093. /* prefix_ruvcsn = "{replica " */
  1094. PR_snprintf (buff, len, "%s: %s%d%s%s} %s %s\n",
  1095. ruv_name ? ruv_name : type_ruvElement,
  1096. prefix_ruvcsn, replica->rid,
  1097. replica->replica_purl == NULL ? "" : " ",
  1098. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1099. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  1100. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  1101. if (strlen (csnstr1) > 0) {
  1102. PR_snprintf (buff + strlen(buff) - 1, len - strlen(buff), " %08lx\n",
  1103. replica->last_modified);
  1104. }
  1105. if (prFile)
  1106. {
  1107. slapi_write_buffer (prFile, buff, strlen(buff));
  1108. }
  1109. else
  1110. {
  1111. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, buff);
  1112. }
  1113. }
  1114. PR_RWLock_Unlock (ruv->lock);
  1115. }
  1116. /* this function notifies the ruv that there are operations in progress so that
  1117. they can be added to the pending list for the appropriate client. */
  1118. int ruv_add_csn_inprogress (RUV *ruv, const CSN *csn)
  1119. {
  1120. RUVElement* replica;
  1121. char csn_str[CSN_STRSIZE];
  1122. int rc = RUV_SUCCESS;
  1123. PR_ASSERT (ruv && csn);
  1124. /* locate ruvElement */
  1125. PR_RWLock_Wlock (ruv->lock);
  1126. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1127. if (replica == NULL)
  1128. {
  1129. replica = ruvAddReplicaNoCSN (ruv, csn_get_replicaid (csn), NULL/*purl*/);
  1130. if (replica == NULL)
  1131. {
  1132. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to add replica"
  1133. " that created csn %s\n", csn_as_string (csn, PR_FALSE, csn_str));
  1134. rc = RUV_MEMORY_ERROR;
  1135. goto done;
  1136. }
  1137. }
  1138. /* check first that this csn is not already covered by this RUV */
  1139. if (ruv_covers_csn_internal(ruv, csn, PR_FALSE))
  1140. {
  1141. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1142. "the csn %s has already be seen - ignoring\n",
  1143. csn_as_string (csn, PR_FALSE, csn_str));
  1144. rc = RUV_COVERS_CSN;
  1145. goto done;
  1146. }
  1147. rc = csnplInsert (replica->csnpl, csn);
  1148. if (rc == 1) /* we already seen this csn */
  1149. {
  1150. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1151. "the csn %s has already be seen - ignoring\n",
  1152. csn_as_string (csn, PR_FALSE, csn_str));
  1153. rc = RUV_COVERS_CSN;
  1154. }
  1155. else if(rc != 0)
  1156. {
  1157. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to insert csn %s"
  1158. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1159. rc = RUV_UNKNOWN_ERROR;
  1160. }
  1161. else
  1162. {
  1163. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: successfully inserted csn %s"
  1164. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1165. rc = RUV_SUCCESS;
  1166. }
  1167. done:
  1168. PR_RWLock_Unlock (ruv->lock);
  1169. return rc;
  1170. }
  1171. int ruv_cancel_csn_inprogress (RUV *ruv, const CSN *csn)
  1172. {
  1173. RUVElement* replica;
  1174. int rc = RUV_SUCCESS;
  1175. PR_ASSERT (ruv && csn);
  1176. /* locate ruvElement */
  1177. PR_RWLock_Wlock (ruv->lock);
  1178. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1179. if (replica == NULL)
  1180. {
  1181. /* ONREPL - log error */
  1182. rc = RUV_NOTFOUND;
  1183. goto done;
  1184. }
  1185. rc = csnplRemove (replica->csnpl, csn);
  1186. if (rc != 0)
  1187. rc = RUV_NOTFOUND;
  1188. else
  1189. rc = RUV_SUCCESS;
  1190. done:
  1191. PR_RWLock_Unlock (ruv->lock);
  1192. return rc;
  1193. }
  1194. int ruv_update_ruv (RUV *ruv, const CSN *csn, const char *replica_purl, PRBool isLocal)
  1195. {
  1196. int rc=RUV_SUCCESS;
  1197. char csn_str[CSN_STRSIZE];
  1198. CSN *max_csn;
  1199. CSN *first_csn = NULL;
  1200. RUVElement *replica;
  1201. PR_ASSERT (ruv && csn);
  1202. PR_RWLock_Wlock (ruv->lock);
  1203. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1204. if (replica == NULL)
  1205. {
  1206. /* we should have a ruv element at this point because it would have
  1207. been added by ruv_add_inprogress function */
  1208. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1209. "can't locate RUV element for replica %d\n", csn_get_replicaid (csn));
  1210. goto done;
  1211. }
  1212. if (csnplCommit(replica->csnpl, csn) != 0)
  1213. {
  1214. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "ruv_update_ruv: cannot commit csn %s\n",
  1215. csn_as_string(csn, PR_FALSE, csn_str));
  1216. rc = RUV_CSNPL_ERROR;
  1217. goto done;
  1218. }
  1219. else
  1220. {
  1221. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1222. "successfully committed csn %s\n", csn_as_string(csn, PR_FALSE, csn_str));
  1223. }
  1224. if ((max_csn = csnplRollUp(replica->csnpl, &first_csn)) != NULL)
  1225. {
  1226. #ifdef DEBUG
  1227. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: rolled up to csn %s\n",
  1228. csn_as_string(max_csn, PR_FALSE, csn_str)); /* XXXggood remove debugging */
  1229. #endif
  1230. /* replica object sets min csn for local replica */
  1231. if (!isLocal && replica->min_csn == NULL) {
  1232. /* bug 559223 - it seems that, under huge stress, a server might pass
  1233. * through this code when more than 1 change has already been sent and commited into
  1234. * the pending lists... Therefore, as we are trying to set the min_csn ever
  1235. * generated by this replica, we need to set the first_csn as the min csn in the
  1236. * ruv */
  1237. set_min_csn_nolock(ruv, first_csn, replica_purl);
  1238. }
  1239. set_max_csn_nolock(ruv, max_csn, replica_purl);
  1240. /* It is possible that first_csn points to max_csn.
  1241. We need to free it once */
  1242. if (max_csn != first_csn) {
  1243. csn_free(&first_csn);
  1244. }
  1245. csn_free(&max_csn);
  1246. }
  1247. done:
  1248. PR_RWLock_Unlock (ruv->lock);
  1249. return rc;
  1250. }
  1251. /* Helper functions */
  1252. static int
  1253. ruvInit (RUV **ruv, int initCount)
  1254. {
  1255. PR_ASSERT (ruv);
  1256. /* allocate new RUV */
  1257. *ruv = (RUV *)slapi_ch_calloc (1, sizeof (RUV));
  1258. /* allocate elements */
  1259. (*ruv)->elements = dl_new (); /* never returns NULL */
  1260. dl_init ((*ruv)->elements, initCount);
  1261. /* create lock */
  1262. (*ruv)->lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "ruv_lock");
  1263. if ((*ruv)->lock == NULL)
  1264. {
  1265. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1266. "ruvInit: failed to create lock\n");
  1267. if (*ruv) {
  1268. dl_free(&(*ruv)->elements);
  1269. }
  1270. slapi_ch_free((void**)ruv);
  1271. return RUV_NSPR_ERROR;
  1272. }
  1273. return RUV_SUCCESS;
  1274. }
  1275. static void
  1276. ruvFreeReplica (void **data)
  1277. {
  1278. RUVElement *element = *(RUVElement**)data;
  1279. if (NULL != element)
  1280. {
  1281. if (NULL != element->csn)
  1282. {
  1283. csn_free (&element->csn);
  1284. }
  1285. if (NULL != element->min_csn)
  1286. {
  1287. csn_free (&element->min_csn);
  1288. }
  1289. /* slapi_ch_free accepts NULL pointer */
  1290. slapi_ch_free((void **)&element->replica_purl);
  1291. if (element->csnpl)
  1292. {
  1293. csnplFree (&(element->csnpl));
  1294. }
  1295. slapi_ch_free ((void **)&element);
  1296. }
  1297. }
  1298. static RUVElement*
  1299. ruvAddReplica (RUV *ruv, const CSN *csn, const char *replica_purl)
  1300. {
  1301. RUVElement *replica;
  1302. PR_ASSERT (NULL != ruv);
  1303. PR_ASSERT (NULL != csn);
  1304. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1305. if (replica == NULL)
  1306. {
  1307. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1308. "ruvAddReplica: memory allocation failed\n");
  1309. return NULL;
  1310. }
  1311. replica->rid = csn_get_replicaid (csn);
  1312. /* PR_ASSERT(replica->rid != READ_ONLY_REPLICA_ID); */
  1313. replica->csn = csn_dup (csn);
  1314. replica->last_modified = current_time();
  1315. replica->min_csn = csn_dup (csn);
  1316. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1317. replica->csnpl = csnplNew ();
  1318. dl_add (ruv->elements, replica);
  1319. return replica;
  1320. }
  1321. static RUVElement*
  1322. ruvAddReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl)
  1323. {
  1324. RUVElement *replica;
  1325. PR_ASSERT (NULL != ruv);
  1326. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1327. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1328. if (replica == NULL)
  1329. {
  1330. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1331. "ruvAddReplicaNoCSN: memory allocation failed\n");
  1332. return NULL;
  1333. }
  1334. replica->rid = rid;
  1335. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1336. replica->csnpl = csnplNew ();
  1337. dl_add (ruv->elements, replica);
  1338. return replica;
  1339. }
  1340. static RUVElement*
  1341. ruvAddIndexReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl, int index)
  1342. {
  1343. RUVElement *replica;
  1344. PR_ASSERT (NULL != ruv);
  1345. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1346. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1347. if (replica == NULL)
  1348. {
  1349. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1350. "ruvAddIndexReplicaNoCSN: memory allocation failed\n");
  1351. return NULL;
  1352. }
  1353. replica->rid = rid;
  1354. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1355. replica->csnpl = csnplNew ();
  1356. dl_add_index (ruv->elements, replica, index);
  1357. return replica;
  1358. }
  1359. static RUVElement *
  1360. ruvGetReplica (const RUV *ruv, ReplicaId rid)
  1361. {
  1362. PR_ASSERT (ruv /* && rid >= 0 -- rid can't be negative */);
  1363. return (RUVElement *)dl_get (ruv->elements, (const void*)&rid, ruvReplicaCompare);
  1364. }
  1365. static int
  1366. ruvReplicaCompare (const void *el1, const void *el2)
  1367. {
  1368. RUVElement *replica = (RUVElement*)el1;
  1369. ReplicaId *rid1 = (ReplicaId*) el2;
  1370. if (replica == NULL || rid1 == NULL)
  1371. return -1;
  1372. if (*rid1 == replica->rid)
  1373. return 0;
  1374. if (*rid1 < replica->rid)
  1375. return -1;
  1376. else
  1377. return 1;
  1378. }
  1379. /*
  1380. * Given a berval that points to a string of the form:
  1381. * "{dbgen} generation-id", return a copy of the
  1382. * "generation-id" part in a null-terminated string.
  1383. * Returns NULL if the berval is malformed.
  1384. */
  1385. static char *
  1386. get_replgen_from_berval(const struct berval *bval)
  1387. {
  1388. char *ret_string = NULL;
  1389. if (NULL != bval && NULL != bval->bv_val &&
  1390. (bval->bv_len > strlen(prefix_replicageneration)) &&
  1391. strncasecmp(bval->bv_val, prefix_replicageneration,
  1392. strlen(prefix_replicageneration)) == 0)
  1393. {
  1394. unsigned int index = strlen(prefix_replicageneration);
  1395. /* Skip any whitespace */
  1396. while (index++ < bval->bv_len && bval->bv_val[index] == ' ');
  1397. if (index < bval->bv_len)
  1398. {
  1399. unsigned int ret_len = bval->bv_len - index;
  1400. ret_string = slapi_ch_malloc(ret_len + 1);
  1401. memcpy(ret_string, &bval->bv_val[index], ret_len);
  1402. ret_string[ret_len] = '\0';
  1403. }
  1404. }
  1405. return ret_string;
  1406. }
  1407. /*
  1408. * Given a berval that points to a string of the form:
  1409. * "{replica ldap[s]//host:port} <min_csn> <csn>", parse out the
  1410. * partial URL and the CSNs into an RUVElement, and return
  1411. * a pointer to the copy. Returns NULL if the berval is
  1412. * malformed.
  1413. */
  1414. static RUVElement *
  1415. get_ruvelement_from_berval(const struct berval *bval)
  1416. {
  1417. RUVElement *ret_ruve = NULL;
  1418. char *purl = NULL;
  1419. ReplicaId rid = 0;
  1420. char ridbuff [RIDSTR_SIZE];
  1421. int i;
  1422. if (NULL == bval || NULL == bval->bv_val ||
  1423. bval->bv_len <= strlen(prefix_ruvcsn) ||
  1424. strncasecmp(bval->bv_val, prefix_ruvcsn, strlen(prefix_ruvcsn)) != 0)
  1425. {
  1426. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1427. "get_ruvelement_from_berval: invalid berval\n");
  1428. goto loser;
  1429. }
  1430. {
  1431. unsigned int urlbegin = strlen(prefix_ruvcsn);
  1432. unsigned int urlend;
  1433. unsigned int mincsnbegin;
  1434. /* replica id must be here */
  1435. i = 0;
  1436. while (isdigit (bval->bv_val[urlbegin]))
  1437. {
  1438. ridbuff [i] = bval->bv_val[urlbegin];
  1439. i++;
  1440. urlbegin ++;
  1441. }
  1442. if (i == 0) /* replicaid is missing */
  1443. goto loser;
  1444. ridbuff[i] = '\0';
  1445. rid = atoi (ridbuff);
  1446. if (bval->bv_val[urlbegin] == '}')
  1447. {
  1448. /* No purl in this value */
  1449. purl = NULL;
  1450. mincsnbegin = urlbegin + 1;
  1451. }
  1452. else
  1453. {
  1454. while (urlbegin++ < bval->bv_len && bval->bv_val[urlbegin] == ' ');
  1455. urlend = urlbegin;
  1456. while (urlend++ < bval->bv_len && bval->bv_val[urlend] != '}');
  1457. purl = slapi_ch_malloc(urlend - urlbegin + 1);
  1458. memcpy(purl, &bval->bv_val[urlbegin], urlend - urlbegin);
  1459. purl[urlend - urlbegin] = '\0';
  1460. mincsnbegin = urlend;
  1461. }
  1462. /* Skip any whitespace before the first (minimum) CSN */
  1463. while (mincsnbegin++ < (bval->bv_len-1) && bval->bv_val[mincsnbegin] == ' ');
  1464. /* Now, mincsnbegin should contain the index of the beginning of the first csn */
  1465. if (mincsnbegin >= bval->bv_len)
  1466. {
  1467. /* Missing the entire content*/
  1468. if (purl == NULL)
  1469. goto loser;
  1470. else /* we have just purl - no changes from the replica has been seen */
  1471. {
  1472. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1473. ret_ruve->rid = rid;
  1474. ret_ruve->replica_purl = purl;
  1475. }
  1476. }
  1477. else
  1478. {
  1479. if (bval->bv_len - mincsnbegin != (_CSN_VALIDCSN_STRLEN * 2) + 1)
  1480. {
  1481. /* Malformed - incorrect length for 2 CSNs + space */
  1482. goto loser;
  1483. }
  1484. else
  1485. {
  1486. char mincsnstr[CSN_STRSIZE];
  1487. char maxcsnstr[CSN_STRSIZE];
  1488. memset(mincsnstr, '\0', CSN_STRSIZE);
  1489. memset(maxcsnstr, '\0', CSN_STRSIZE);
  1490. memcpy(mincsnstr, &bval->bv_val[mincsnbegin], _CSN_VALIDCSN_STRLEN);
  1491. memcpy(maxcsnstr, &bval->bv_val[mincsnbegin + _CSN_VALIDCSN_STRLEN + 1], _CSN_VALIDCSN_STRLEN);
  1492. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1493. ret_ruve->min_csn = csn_new_by_string(mincsnstr);
  1494. ret_ruve->csn = csn_new_by_string(maxcsnstr);
  1495. ret_ruve->rid = rid;
  1496. ret_ruve->replica_purl = purl;
  1497. if (NULL == ret_ruve->min_csn || NULL == ret_ruve->csn)
  1498. {
  1499. goto loser;
  1500. }
  1501. }
  1502. }
  1503. }
  1504. /* initialize csn pending list */
  1505. ret_ruve->csnpl = csnplNew ();
  1506. if (ret_ruve->csnpl == NULL)
  1507. {
  1508. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1509. "get_ruvelement_from_berval: failed to create csn pending list\n");
  1510. goto loser;
  1511. }
  1512. return ret_ruve;
  1513. loser:
  1514. /* slapi_ch_free accepts NULL pointer */
  1515. slapi_ch_free((void **)&purl);
  1516. if (NULL != ret_ruve)
  1517. {
  1518. if (NULL != ret_ruve->min_csn)
  1519. {
  1520. csn_free(&ret_ruve->min_csn);
  1521. }
  1522. if (NULL != ret_ruve->csn)
  1523. {
  1524. csn_free(&ret_ruve->csn);
  1525. }
  1526. slapi_ch_free((void **)&ret_ruve);
  1527. }
  1528. return NULL;
  1529. }
  1530. int
  1531. ruv_move_local_supplier_to_first(RUV *ruv, ReplicaId aRid)
  1532. {
  1533. RUVElement * elem = NULL;
  1534. int rc = RUV_NOTFOUND;
  1535. PR_ASSERT(ruv);
  1536. PR_RWLock_Wlock (ruv->lock);
  1537. elem = (RUVElement *)dl_delete(ruv->elements,(const void*)&aRid, ruvReplicaCompare, 0);
  1538. if (elem) {
  1539. dl_add_index(ruv->elements, elem, 1);
  1540. rc = RUV_SUCCESS;
  1541. }
  1542. PR_RWLock_Unlock (ruv->lock);
  1543. return rc;
  1544. }
  1545. int
  1546. ruv_get_first_id_and_purl(RUV *ruv, ReplicaId *rid, char **replica_purl )
  1547. {
  1548. RUVElement * first = NULL;
  1549. int cookie;
  1550. int rc;
  1551. PR_ASSERT(ruv);
  1552. PR_RWLock_Rlock (ruv->lock);
  1553. first = dl_get_first(ruv->elements, &cookie);
  1554. if ( first == NULL )
  1555. {
  1556. rc = RUV_MEMORY_ERROR;
  1557. }
  1558. else
  1559. {
  1560. *rid = first->rid;
  1561. *replica_purl = first->replica_purl;
  1562. rc = RUV_SUCCESS;
  1563. }
  1564. PR_RWLock_Unlock (ruv->lock);
  1565. return rc;
  1566. }
  1567. int ruv_local_contains_supplier(RUV *ruv, ReplicaId rid)
  1568. {
  1569. int cookie;
  1570. RUVElement *elem = NULL;
  1571. PR_ASSERT(ruv);
  1572. PR_RWLock_Rlock (ruv->lock);
  1573. for (elem = dl_get_first (ruv->elements, &cookie);
  1574. elem;
  1575. elem = dl_get_next (ruv->elements, &cookie))
  1576. {
  1577. if (elem->rid == rid){
  1578. PR_RWLock_Unlock (ruv->lock);
  1579. return 1;
  1580. }
  1581. }
  1582. PR_RWLock_Unlock (ruv->lock);
  1583. return 0;
  1584. }
  1585. PRBool ruv_has_csns(const RUV *ruv)
  1586. {
  1587. PRBool retval = PR_TRUE;
  1588. CSN *mincsn = NULL;
  1589. CSN *maxcsn = NULL;
  1590. ruv_get_min_csn(ruv, &mincsn);
  1591. ruv_get_max_csn(ruv, &maxcsn);
  1592. if (mincsn) {
  1593. csn_free(&mincsn);
  1594. csn_free(&maxcsn);
  1595. } else if (maxcsn) {
  1596. csn_free(&maxcsn);
  1597. } else {
  1598. retval = PR_FALSE; /* both min and max are false */
  1599. }
  1600. return retval;
  1601. }
  1602. PRBool ruv_has_both_csns(const RUV *ruv)
  1603. {
  1604. PRBool retval = PR_TRUE;
  1605. CSN *mincsn = NULL;
  1606. CSN *maxcsn = NULL;
  1607. ruv_get_min_csn(ruv, &mincsn);
  1608. ruv_get_max_csn(ruv, &maxcsn);
  1609. if (mincsn) {
  1610. csn_free(&mincsn);
  1611. csn_free(&maxcsn);
  1612. } else if (maxcsn) {
  1613. csn_free(&maxcsn);
  1614. retval = PR_FALSE; /* it has a maxcsn but no mincsn */
  1615. } else {
  1616. retval = PR_FALSE; /* both min and max are false */
  1617. }
  1618. return retval;
  1619. }
  1620. /* Check if the first ruv is newer than the second one */
  1621. PRBool
  1622. ruv_is_newer (Object *sruvobj, Object *cruvobj)
  1623. {
  1624. RUV *sruv, *cruv;
  1625. RUVElement *sreplica, *creplica;
  1626. int scookie, ccookie;
  1627. int is_newer = PR_FALSE;
  1628. if ( sruvobj == NULL ) {
  1629. return 0;
  1630. }
  1631. if ( cruvobj == NULL ) {
  1632. return 1;
  1633. }
  1634. sruv = (RUV *) object_get_data ( sruvobj );
  1635. cruv = (RUV *) object_get_data ( cruvobj );
  1636. for (sreplica = dl_get_first (sruv->elements, &scookie); sreplica;
  1637. sreplica = dl_get_next (sruv->elements, &scookie))
  1638. {
  1639. /* A hub may have a dummy ruv with rid 65535 */
  1640. if ( sreplica->csn == NULL ) continue;
  1641. if ( cruv->elements == NULL )
  1642. {
  1643. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1644. "ruv_is_newer, consumer RUV has no elements\n");
  1645. is_newer=PR_FALSE;
  1646. break;
  1647. }
  1648. for (creplica = dl_get_first (cruv->elements, &ccookie); creplica;
  1649. creplica = dl_get_next (cruv->elements, &ccookie))
  1650. {
  1651. if ( sreplica->rid == creplica->rid ) {
  1652. if ( csn_compare ( sreplica->csn, creplica->csn ) > 0 ) {
  1653. is_newer = PR_TRUE;
  1654. }
  1655. break;
  1656. }
  1657. }
  1658. if ( creplica == NULL || is_newer ) {
  1659. is_newer = PR_TRUE;
  1660. break;
  1661. }
  1662. }
  1663. return is_newer;
  1664. }
  1665. void
  1666. ruv_force_csn_update (RUV *ruv, CSN *csn)
  1667. {
  1668. CSN *max = NULL;
  1669. if (ruv != NULL)
  1670. {
  1671. ruv_get_max_csn(ruv, &max);
  1672. if (csn_compare(max, csn))
  1673. {
  1674. ruv_set_max_csn(ruv, csn, NULL);
  1675. }
  1676. csn_free(&max);
  1677. }
  1678. }
  1679. #ifdef TESTING /* Some unit tests for code in this file */
  1680. static void
  1681. ruv_dump_internal(RUV *ruv)
  1682. {
  1683. RUVElement *replica;
  1684. int cookie;
  1685. char csnstr1[CSN_STRSIZE];
  1686. char csnstr2[CSN_STRSIZE];
  1687. PR_ASSERT(NULL != ruv);
  1688. printf("{replicageneration} %s\n", ruv->replGen == NULL ? "NULL" : ruv->replGen);
  1689. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1690. replica = dl_get_next (ruv->elements, &cookie))
  1691. {
  1692. printf("{replica%s%s} %s %s\n",
  1693. replica->replica_purl == NULL ? "" : " ",
  1694. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1695. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  1696. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  1697. }
  1698. }
  1699. void
  1700. ruv_test()
  1701. {
  1702. const struct berval *vals[5];
  1703. struct berval val0, val1, val2, val3;
  1704. RUV *ruv;
  1705. Slapi_Attr *attr;
  1706. Slapi_Value *sv0, *sv1, *sv2, *sv3;
  1707. int rc;
  1708. char csnstr[CSN_STRSIZE];
  1709. char *gen;
  1710. CSN *newcsn;
  1711. ReplicaId *ids;
  1712. int nids;
  1713. Slapi_Mod smods;
  1714. PRBool covers;
  1715. vals[0] = &val0;
  1716. vals[1] = &val1;
  1717. vals[2] = &val2;
  1718. vals[3] = &val3;
  1719. vals[4] = NULL;
  1720. val0.bv_val = "{replicageneration} 0440FDC0A33F";
  1721. val0.bv_len = strlen(val0.bv_val);
  1722. val1.bv_val = "{replica ldap://ggood.mcom.com:389} 12345670000000FE0000 12345671000000FE0000";
  1723. val1.bv_len = strlen(val1.bv_val);
  1724. val2.bv_val = "{replica ldaps://an-impossibly-long-host-name-that-drags-on-forever-and-forever.mcom.com:389} 11112110000000FF0000 11112111000000FF0000";
  1725. val2.bv_len = strlen(val2.bv_val);
  1726. val3.bv_val = "{replica} 12345672000000FD0000 12345673000000FD0000";
  1727. val3.bv_len = strlen(val3.bv_val);
  1728. rc = ruv_init_from_bervals(vals, &ruv);
  1729. ruv_dump_internal(ruv);
  1730. attr = slapi_attr_new();
  1731. attr = slapi_attr_init(attr, "ruvelement");
  1732. sv0 = slapi_value_new();
  1733. sv1 = slapi_value_new();
  1734. sv2 = slapi_value_new();
  1735. sv3 = slapi_value_new();
  1736. slapi_value_init_berval(sv0, &val0);
  1737. slapi_value_init_berval(sv1, &val1);
  1738. slapi_value_init_berval(sv2, &val2);
  1739. slapi_value_init_berval(sv3, &val3);
  1740. slapi_attr_add_value(attr, sv0);
  1741. slapi_attr_add_value(attr, sv1);
  1742. slapi_attr_add_value(attr, sv2);
  1743. slapi_attr_add_value(attr, sv3);
  1744. rc = ruv_init_from_slapi_attr(attr, &ruv);
  1745. ruv_dump_internal(ruv);
  1746. rc = ruv_delete_replica(ruv, 0xFF);
  1747. /* Should delete one replica */
  1748. ruv_dump_internal(ruv);
  1749. rc = ruv_delete_replica(ruv, 0xAA);
  1750. /* No such replica - should not do anything */
  1751. ruv_dump_internal(ruv);
  1752. rc = ruv_get_largest_csn_for_replica(ruv, 0xFE, &newcsn);
  1753. if (NULL != newcsn)
  1754. {
  1755. csn_as_string(newcsn, PR_FALSE, csnstr);
  1756. printf("Replica 0x%X has largest csn \"%s\"\n", 0xFE, csnstr);
  1757. }
  1758. else
  1759. {
  1760. printf("BAD - can't get largest CSN for replica 0x%X\n", 0xFE);
  1761. }
  1762. rc = ruv_get_smallest_csn_for_replica(ruv, 0xFE, &newcsn);
  1763. if (NULL != newcsn)
  1764. {
  1765. csn_as_string(newcsn, PR_FALSE, csnstr);
  1766. printf("Replica 0x%X has smallest csn \"%s\"\n", 0xFE, csnstr);
  1767. }
  1768. else
  1769. {
  1770. printf("BAD - can't get smallest CSN for replica 0x%X\n", 0xFE);
  1771. }
  1772. rc = ruv_get_largest_csn_for_replica(ruv, 0xAA, &newcsn);
  1773. printf("ruv_get_largest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  1774. rc = ruv_get_smallest_csn_for_replica(ruv, 0xAA, &newcsn);
  1775. printf("ruv_get_smallest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  1776. newcsn = csn_new_by_string("12345674000000FE0000"); /* Old replica 0xFE */
  1777. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1778. /* Should update replica FE's CSN */
  1779. ruv_dump_internal(ruv);
  1780. newcsn = csn_new_by_string("12345675000000FB0000"); /* New replica 0xFB */
  1781. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1782. /* Should get a new replica in the list with min == max csn */
  1783. ruv_dump_internal(ruv);
  1784. newcsn = csn_new_by_string("12345676000000FD0000"); /* Old replica 0xFD */
  1785. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1786. /* Should update replica 0xFD so new CSN is newer than min CSN */
  1787. ruv_dump_internal(ruv);
  1788. gen = ruv_get_replica_generation(ruv);
  1789. printf("replica generation is \"%s\"\n", gen);
  1790. newcsn = csn_new_by_string("12345673000000FE0000"); /* Old replica 0xFE */
  1791. covers = ruv_covers_csn(ruv, newcsn); /* should say "true" */
  1792. newcsn = csn_new_by_string("12345675000000FE0000"); /* Old replica 0xFE */
  1793. covers = ruv_covers_csn(ruv, newcsn); /* Should say "false" */
  1794. newcsn = csn_new_by_string("123456700000000A0000"); /* New replica 0A */
  1795. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  1796. ruv_dump_internal(ruv);
  1797. newcsn = csn_new_by_string("123456710000000A0000"); /* New replica 0A */
  1798. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  1799. ruv_dump_internal(ruv);
  1800. newcsn = csn_new_by_string("123456700000000B0000"); /* New replica 0B */
  1801. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  1802. ruv_dump_internal(ruv);
  1803. newcsn = csn_new_by_string("123456710000000B0000"); /* New replica 0B */
  1804. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  1805. ruv_dump_internal(ruv);
  1806. /* ONREPL test ruv enumeration */
  1807. rc = ruv_to_smod(ruv, &smods);
  1808. ruv_destroy(&ruv);
  1809. }
  1810. #endif /* TESTING */