1
0

repl5_ruv.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  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 || covered_ruv->replGen == NULL)
  999. return PR_FALSE;
  1000. if (strcasecmp (covered_ruv->replGen, covering_ruv->replGen))
  1001. return PR_FALSE;
  1002. /* replica generation is the same, now compare element by element */
  1003. for (replica = dl_get_first (covered_ruv->elements, &cookie);
  1004. NULL != replica;
  1005. replica = dl_get_next (covered_ruv->elements, &cookie))
  1006. {
  1007. if (replica->csn &&
  1008. (ruv_covers_csn(covering_ruv, replica->csn) == PR_FALSE))
  1009. {
  1010. return_value = PR_FALSE;
  1011. /* Don't break here - may leave something referenced? */
  1012. }
  1013. }
  1014. return return_value;
  1015. }
  1016. PRInt32
  1017. ruv_replica_count (const RUV *ruv)
  1018. {
  1019. if (ruv == NULL)
  1020. return 0;
  1021. else
  1022. {
  1023. int count;
  1024. PR_RWLock_Rlock (ruv->lock);
  1025. count = dl_get_count (ruv->elements);
  1026. PR_RWLock_Unlock (ruv->lock);
  1027. return count;
  1028. }
  1029. }
  1030. /*
  1031. * Extract all the referral URL's from the RUV (but self URL),
  1032. * returning them in an array of strings, that
  1033. * the caller must free.
  1034. */
  1035. char **
  1036. ruv_get_referrals(const RUV *ruv)
  1037. {
  1038. char **r= NULL;
  1039. int n;
  1040. const char *mypurl = multimaster_get_local_purl();
  1041. PR_RWLock_Rlock (ruv->lock);
  1042. n = ruv_replica_count(ruv);
  1043. if(n>0)
  1044. {
  1045. RUVElement *replica;
  1046. int cookie;
  1047. int i= 0;
  1048. r= (char**)slapi_ch_calloc(sizeof(char*),n+1);
  1049. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1050. replica = dl_get_next (ruv->elements, &cookie))
  1051. {
  1052. /* Add URL into referrals if doesn't match self URL */
  1053. if((replica->replica_purl!=NULL) &&
  1054. (slapi_utf8casecmp((unsigned char *)replica->replica_purl,
  1055. (unsigned char *)mypurl) != 0))
  1056. {
  1057. r[i]= slapi_ch_strdup(replica->replica_purl);
  1058. i++;
  1059. }
  1060. }
  1061. }
  1062. PR_RWLock_Unlock (ruv->lock);
  1063. return r; /* Caller must free this */
  1064. }
  1065. void
  1066. ruv_dump(const RUV *ruv, char *ruv_name, PRFileDesc *prFile)
  1067. {
  1068. RUVElement *replica;
  1069. int cookie;
  1070. char csnstr1[CSN_STRSIZE];
  1071. char csnstr2[CSN_STRSIZE];
  1072. char buff[RUVSTR_SIZE];
  1073. int len = sizeof (buff);
  1074. PR_ASSERT(NULL != ruv);
  1075. PR_RWLock_Rlock (ruv->lock);
  1076. PR_snprintf (buff, len, "%s: {replicageneration} %s\n",
  1077. ruv_name ? ruv_name : type_ruvElement,
  1078. ruv->replGen == NULL ? "" : ruv->replGen);
  1079. if (prFile)
  1080. {
  1081. slapi_write_buffer (prFile, buff, strlen(buff));
  1082. }
  1083. else
  1084. {
  1085. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, buff);
  1086. }
  1087. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1088. replica = dl_get_next (ruv->elements, &cookie))
  1089. {
  1090. /* prefix_ruvcsn = "{replica " */
  1091. PR_snprintf (buff, len, "%s: %s%d%s%s} %s %s\n",
  1092. ruv_name ? ruv_name : type_ruvElement,
  1093. prefix_ruvcsn, replica->rid,
  1094. replica->replica_purl == NULL ? "" : " ",
  1095. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1096. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  1097. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  1098. if (strlen (csnstr1) > 0) {
  1099. PR_snprintf (buff + strlen(buff) - 1, len - strlen(buff), " %08lx\n",
  1100. replica->last_modified);
  1101. }
  1102. if (prFile)
  1103. {
  1104. slapi_write_buffer (prFile, buff, strlen(buff));
  1105. }
  1106. else
  1107. {
  1108. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, buff);
  1109. }
  1110. }
  1111. PR_RWLock_Unlock (ruv->lock);
  1112. }
  1113. /* this function notifies the ruv that there are operations in progress so that
  1114. they can be added to the pending list for the appropriate client. */
  1115. int ruv_add_csn_inprogress (RUV *ruv, const CSN *csn)
  1116. {
  1117. RUVElement* replica;
  1118. char csn_str[CSN_STRSIZE];
  1119. int rc = RUV_SUCCESS;
  1120. PR_ASSERT (ruv && csn);
  1121. /* locate ruvElement */
  1122. PR_RWLock_Wlock (ruv->lock);
  1123. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1124. if (replica == NULL)
  1125. {
  1126. replica = ruvAddReplicaNoCSN (ruv, csn_get_replicaid (csn), NULL/*purl*/);
  1127. if (replica == NULL)
  1128. {
  1129. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to add replica"
  1130. " that created csn %s\n", csn_as_string (csn, PR_FALSE, csn_str));
  1131. rc = RUV_MEMORY_ERROR;
  1132. goto done;
  1133. }
  1134. }
  1135. /* check first that this csn is not already covered by this RUV */
  1136. if (ruv_covers_csn_internal(ruv, csn, PR_FALSE))
  1137. {
  1138. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1139. "the csn %s has already be seen - ignoring\n",
  1140. csn_as_string (csn, PR_FALSE, csn_str));
  1141. rc = RUV_COVERS_CSN;
  1142. goto done;
  1143. }
  1144. rc = csnplInsert (replica->csnpl, csn);
  1145. if (rc == 1) /* we already seen this csn */
  1146. {
  1147. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1148. "the csn %s has already be seen - ignoring\n",
  1149. csn_as_string (csn, PR_FALSE, csn_str));
  1150. rc = RUV_COVERS_CSN;
  1151. }
  1152. else if(rc != 0)
  1153. {
  1154. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to insert csn %s"
  1155. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1156. rc = RUV_UNKNOWN_ERROR;
  1157. }
  1158. else
  1159. {
  1160. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: successfully inserted csn %s"
  1161. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1162. rc = RUV_SUCCESS;
  1163. }
  1164. done:
  1165. PR_RWLock_Unlock (ruv->lock);
  1166. return rc;
  1167. }
  1168. int ruv_cancel_csn_inprogress (RUV *ruv, const CSN *csn)
  1169. {
  1170. RUVElement* replica;
  1171. int rc = RUV_SUCCESS;
  1172. PR_ASSERT (ruv && csn);
  1173. /* locate ruvElement */
  1174. PR_RWLock_Wlock (ruv->lock);
  1175. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1176. if (replica == NULL)
  1177. {
  1178. /* ONREPL - log error */
  1179. rc = RUV_NOTFOUND;
  1180. goto done;
  1181. }
  1182. rc = csnplRemove (replica->csnpl, csn);
  1183. if (rc != 0)
  1184. rc = RUV_NOTFOUND;
  1185. else
  1186. rc = RUV_SUCCESS;
  1187. done:
  1188. PR_RWLock_Unlock (ruv->lock);
  1189. return rc;
  1190. }
  1191. int ruv_update_ruv (RUV *ruv, const CSN *csn, const char *replica_purl, PRBool isLocal)
  1192. {
  1193. int rc=RUV_SUCCESS;
  1194. char csn_str[CSN_STRSIZE];
  1195. CSN *max_csn;
  1196. CSN *first_csn = NULL;
  1197. RUVElement *replica;
  1198. PR_ASSERT (ruv && csn);
  1199. PR_RWLock_Wlock (ruv->lock);
  1200. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1201. if (replica == NULL)
  1202. {
  1203. /* we should have a ruv element at this point because it would have
  1204. been added by ruv_add_inprogress function */
  1205. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1206. "can't locate RUV element for replica %d\n", csn_get_replicaid (csn));
  1207. goto done;
  1208. }
  1209. if (csnplCommit(replica->csnpl, csn) != 0)
  1210. {
  1211. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "ruv_update_ruv: cannot commit csn %s\n",
  1212. csn_as_string(csn, PR_FALSE, csn_str));
  1213. rc = RUV_CSNPL_ERROR;
  1214. goto done;
  1215. }
  1216. else
  1217. {
  1218. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1219. "successfully committed csn %s\n", csn_as_string(csn, PR_FALSE, csn_str));
  1220. }
  1221. if ((max_csn = csnplRollUp(replica->csnpl, &first_csn)) != NULL)
  1222. {
  1223. #ifdef DEBUG
  1224. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: rolled up to csn %s\n",
  1225. csn_as_string(max_csn, PR_FALSE, csn_str)); /* XXXggood remove debugging */
  1226. #endif
  1227. /* replica object sets min csn for local replica */
  1228. if (!isLocal && replica->min_csn == NULL) {
  1229. /* bug 559223 - it seems that, under huge stress, a server might pass
  1230. * through this code when more than 1 change has already been sent and commited into
  1231. * the pending lists... Therefore, as we are trying to set the min_csn ever
  1232. * generated by this replica, we need to set the first_csn as the min csn in the
  1233. * ruv */
  1234. set_min_csn_nolock(ruv, first_csn, replica_purl);
  1235. }
  1236. set_max_csn_nolock(ruv, max_csn, replica_purl);
  1237. /* It is possible that first_csn points to max_csn.
  1238. We need to free it once */
  1239. if (max_csn != first_csn) {
  1240. csn_free(&first_csn);
  1241. }
  1242. csn_free(&max_csn);
  1243. }
  1244. done:
  1245. PR_RWLock_Unlock (ruv->lock);
  1246. return rc;
  1247. }
  1248. /* Helper functions */
  1249. static int
  1250. ruvInit (RUV **ruv, int initCount)
  1251. {
  1252. PR_ASSERT (ruv);
  1253. /* allocate new RUV */
  1254. *ruv = (RUV *)slapi_ch_calloc (1, sizeof (RUV));
  1255. if (ruv == NULL)
  1256. {
  1257. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1258. "ruvInit: memory allocation failed\n");
  1259. return RUV_MEMORY_ERROR;
  1260. }
  1261. /* allocate elements */
  1262. (*ruv)->elements = dl_new ();
  1263. if ((*ruv)->elements == 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. dl_init ((*ruv)->elements, initCount);
  1270. /* create lock */
  1271. (*ruv)->lock = PR_NewRWLock(PR_RWLOCK_RANK_NONE, "ruv_lock");
  1272. if ((*ruv)->lock == NULL)
  1273. {
  1274. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1275. "ruvInit: failed to create lock\n");
  1276. return RUV_NSPR_ERROR;
  1277. }
  1278. return RUV_SUCCESS;
  1279. }
  1280. static void
  1281. ruvFreeReplica (void **data)
  1282. {
  1283. RUVElement *element = *(RUVElement**)data;
  1284. if (NULL != element)
  1285. {
  1286. if (NULL != element->csn)
  1287. {
  1288. csn_free (&element->csn);
  1289. }
  1290. if (NULL != element->min_csn)
  1291. {
  1292. csn_free (&element->min_csn);
  1293. }
  1294. /* slapi_ch_free accepts NULL pointer */
  1295. slapi_ch_free((void **)&element->replica_purl);
  1296. if (element->csnpl)
  1297. {
  1298. csnplFree (&(element->csnpl));
  1299. }
  1300. slapi_ch_free ((void **)&element);
  1301. }
  1302. }
  1303. static RUVElement*
  1304. ruvAddReplica (RUV *ruv, const CSN *csn, const char *replica_purl)
  1305. {
  1306. RUVElement *replica;
  1307. PR_ASSERT (NULL != ruv);
  1308. PR_ASSERT (NULL != csn);
  1309. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1310. if (replica == NULL)
  1311. {
  1312. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1313. "ruvAddReplica: memory allocation failed\n");
  1314. return NULL;
  1315. }
  1316. replica->rid = csn_get_replicaid (csn);
  1317. /* PR_ASSERT(replica->rid != READ_ONLY_REPLICA_ID); */
  1318. replica->csn = csn_dup (csn);
  1319. replica->last_modified = current_time();
  1320. replica->min_csn = csn_dup (csn);
  1321. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1322. replica->csnpl = csnplNew ();
  1323. dl_add (ruv->elements, replica);
  1324. return replica;
  1325. }
  1326. static RUVElement*
  1327. ruvAddReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl)
  1328. {
  1329. RUVElement *replica;
  1330. PR_ASSERT (NULL != ruv);
  1331. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1332. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1333. if (replica == NULL)
  1334. {
  1335. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1336. "ruvAddReplicaNoCSN: memory allocation failed\n");
  1337. return NULL;
  1338. }
  1339. replica->rid = rid;
  1340. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1341. replica->csnpl = csnplNew ();
  1342. dl_add (ruv->elements, replica);
  1343. return replica;
  1344. }
  1345. static RUVElement*
  1346. ruvAddIndexReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl, int index)
  1347. {
  1348. RUVElement *replica;
  1349. PR_ASSERT (NULL != ruv);
  1350. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1351. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1352. if (replica == NULL)
  1353. {
  1354. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1355. "ruvAddIndexReplicaNoCSN: memory allocation failed\n");
  1356. return NULL;
  1357. }
  1358. replica->rid = rid;
  1359. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1360. replica->csnpl = csnplNew ();
  1361. dl_add_index (ruv->elements, replica, index);
  1362. return replica;
  1363. }
  1364. static RUVElement *
  1365. ruvGetReplica (const RUV *ruv, ReplicaId rid)
  1366. {
  1367. PR_ASSERT (ruv /* && rid >= 0 -- rid can't be negative */);
  1368. return (RUVElement *)dl_get (ruv->elements, (const void*)&rid, ruvReplicaCompare);
  1369. }
  1370. static int
  1371. ruvReplicaCompare (const void *el1, const void *el2)
  1372. {
  1373. RUVElement *replica = (RUVElement*)el1;
  1374. ReplicaId *rid1 = (ReplicaId*) el2;
  1375. if (replica == NULL || rid1 == NULL)
  1376. return -1;
  1377. if (*rid1 == replica->rid)
  1378. return 0;
  1379. if (*rid1 < replica->rid)
  1380. return -1;
  1381. else
  1382. return 1;
  1383. }
  1384. /*
  1385. * Given a berval that points to a string of the form:
  1386. * "{dbgen} generation-id", return a copy of the
  1387. * "generation-id" part in a null-terminated string.
  1388. * Returns NULL if the berval is malformed.
  1389. */
  1390. static char *
  1391. get_replgen_from_berval(const struct berval *bval)
  1392. {
  1393. char *ret_string = NULL;
  1394. if (NULL != bval && NULL != bval->bv_val &&
  1395. (bval->bv_len > strlen(prefix_replicageneration)) &&
  1396. strncasecmp(bval->bv_val, prefix_replicageneration,
  1397. strlen(prefix_replicageneration)) == 0)
  1398. {
  1399. unsigned int index = strlen(prefix_replicageneration);
  1400. /* Skip any whitespace */
  1401. while (index++ < bval->bv_len && bval->bv_val[index] == ' ');
  1402. if (index < bval->bv_len)
  1403. {
  1404. unsigned int ret_len = bval->bv_len - index;
  1405. ret_string = slapi_ch_malloc(ret_len + 1);
  1406. memcpy(ret_string, &bval->bv_val[index], ret_len);
  1407. ret_string[ret_len] = '\0';
  1408. }
  1409. }
  1410. return ret_string;
  1411. }
  1412. /*
  1413. * Given a berval that points to a string of the form:
  1414. * "{replica ldap[s]//host:port} <min_csn> <csn>", parse out the
  1415. * partial URL and the CSNs into an RUVElement, and return
  1416. * a pointer to the copy. Returns NULL if the berval is
  1417. * malformed.
  1418. */
  1419. static RUVElement *
  1420. get_ruvelement_from_berval(const struct berval *bval)
  1421. {
  1422. RUVElement *ret_ruve = NULL;
  1423. char *purl = NULL;
  1424. ReplicaId rid = 0;
  1425. char ridbuff [RIDSTR_SIZE];
  1426. int i;
  1427. if (NULL == bval || NULL == bval->bv_val ||
  1428. bval->bv_len <= strlen(prefix_ruvcsn) ||
  1429. strncasecmp(bval->bv_val, prefix_ruvcsn, strlen(prefix_ruvcsn)) != 0)
  1430. {
  1431. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1432. "get_ruvelement_from_berval: invalid berval\n");
  1433. goto loser;
  1434. }
  1435. {
  1436. unsigned int urlbegin = strlen(prefix_ruvcsn);
  1437. unsigned int urlend;
  1438. unsigned int mincsnbegin;
  1439. /* replica id must be here */
  1440. i = 0;
  1441. while (isdigit (bval->bv_val[urlbegin]))
  1442. {
  1443. ridbuff [i] = bval->bv_val[urlbegin];
  1444. i++;
  1445. urlbegin ++;
  1446. }
  1447. if (i == 0) /* replicaid is missing */
  1448. goto loser;
  1449. ridbuff[i] = '\0';
  1450. rid = atoi (ridbuff);
  1451. if (bval->bv_val[urlbegin] == '}')
  1452. {
  1453. /* No purl in this value */
  1454. purl = NULL;
  1455. mincsnbegin = urlbegin + 1;
  1456. }
  1457. else
  1458. {
  1459. while (urlbegin++ < bval->bv_len && bval->bv_val[urlbegin] == ' ');
  1460. urlend = urlbegin;
  1461. while (urlend++ < bval->bv_len && bval->bv_val[urlend] != '}');
  1462. purl = slapi_ch_malloc(urlend - urlbegin + 1);
  1463. memcpy(purl, &bval->bv_val[urlbegin], urlend - urlbegin);
  1464. purl[urlend - urlbegin] = '\0';
  1465. mincsnbegin = urlend;
  1466. }
  1467. /* Skip any whitespace before the first (minimum) CSN */
  1468. while (mincsnbegin++ < (bval->bv_len-1) && bval->bv_val[mincsnbegin] == ' ');
  1469. /* Now, mincsnbegin should contain the index of the beginning of the first csn */
  1470. if (mincsnbegin >= bval->bv_len)
  1471. {
  1472. /* Missing the entire content*/
  1473. if (purl == NULL)
  1474. goto loser;
  1475. else /* we have just purl - no changes from the replica has been seen */
  1476. {
  1477. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1478. ret_ruve->rid = rid;
  1479. ret_ruve->replica_purl = purl;
  1480. }
  1481. }
  1482. else
  1483. {
  1484. if (bval->bv_len - mincsnbegin != (_CSN_VALIDCSN_STRLEN * 2) + 1)
  1485. {
  1486. /* Malformed - incorrect length for 2 CSNs + space */
  1487. goto loser;
  1488. }
  1489. else
  1490. {
  1491. char mincsnstr[CSN_STRSIZE];
  1492. char maxcsnstr[CSN_STRSIZE];
  1493. memset(mincsnstr, '\0', CSN_STRSIZE);
  1494. memset(maxcsnstr, '\0', CSN_STRSIZE);
  1495. memcpy(mincsnstr, &bval->bv_val[mincsnbegin], _CSN_VALIDCSN_STRLEN);
  1496. memcpy(maxcsnstr, &bval->bv_val[mincsnbegin + _CSN_VALIDCSN_STRLEN + 1], _CSN_VALIDCSN_STRLEN);
  1497. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1498. ret_ruve->min_csn = csn_new_by_string(mincsnstr);
  1499. ret_ruve->csn = csn_new_by_string(maxcsnstr);
  1500. ret_ruve->rid = rid;
  1501. ret_ruve->replica_purl = purl;
  1502. if (NULL == ret_ruve->min_csn || NULL == ret_ruve->csn)
  1503. {
  1504. goto loser;
  1505. }
  1506. }
  1507. }
  1508. }
  1509. /* initialize csn pending list */
  1510. ret_ruve->csnpl = csnplNew ();
  1511. if (ret_ruve->csnpl == NULL)
  1512. {
  1513. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1514. "get_ruvelement_from_berval: failed to create csn pending list\n");
  1515. goto loser;
  1516. }
  1517. return ret_ruve;
  1518. loser:
  1519. /* slapi_ch_free accepts NULL pointer */
  1520. slapi_ch_free((void **)&purl);
  1521. if (NULL != ret_ruve)
  1522. {
  1523. if (NULL != ret_ruve->min_csn)
  1524. {
  1525. csn_free(&ret_ruve->min_csn);
  1526. }
  1527. if (NULL != ret_ruve->csn)
  1528. {
  1529. csn_free(&ret_ruve->csn);
  1530. }
  1531. slapi_ch_free((void **)&ret_ruve);
  1532. }
  1533. return NULL;
  1534. }
  1535. int
  1536. ruv_move_local_supplier_to_first(RUV *ruv, ReplicaId aRid)
  1537. {
  1538. RUVElement * elem = NULL;
  1539. int rc = RUV_NOTFOUND;
  1540. PR_ASSERT(ruv);
  1541. PR_RWLock_Wlock (ruv->lock);
  1542. elem = (RUVElement *)dl_delete(ruv->elements,(const void*)&aRid, ruvReplicaCompare, 0);
  1543. if (elem) {
  1544. dl_add_index(ruv->elements, elem, 1);
  1545. rc = RUV_SUCCESS;
  1546. }
  1547. PR_RWLock_Unlock (ruv->lock);
  1548. return rc;
  1549. }
  1550. int
  1551. ruv_get_first_id_and_purl(RUV *ruv, ReplicaId *rid, char **replica_purl )
  1552. {
  1553. RUVElement * first = NULL;
  1554. int cookie;
  1555. int rc;
  1556. PR_ASSERT(ruv);
  1557. PR_RWLock_Rlock (ruv->lock);
  1558. first = dl_get_first(ruv->elements, &cookie);
  1559. if ( first == NULL )
  1560. {
  1561. rc = RUV_MEMORY_ERROR;
  1562. }
  1563. else
  1564. {
  1565. *rid = first->rid;
  1566. *replica_purl = first->replica_purl;
  1567. rc = RUV_SUCCESS;
  1568. }
  1569. PR_RWLock_Unlock (ruv->lock);
  1570. return rc;
  1571. }
  1572. int ruv_local_contains_supplier(RUV *ruv, ReplicaId rid)
  1573. {
  1574. int cookie;
  1575. RUVElement *elem = NULL;
  1576. PR_ASSERT(ruv);
  1577. PR_RWLock_Rlock (ruv->lock);
  1578. for (elem = dl_get_first (ruv->elements, &cookie);
  1579. elem;
  1580. elem = dl_get_next (ruv->elements, &cookie))
  1581. {
  1582. if (elem->rid == rid){
  1583. PR_RWLock_Unlock (ruv->lock);
  1584. return 1;
  1585. }
  1586. }
  1587. PR_RWLock_Unlock (ruv->lock);
  1588. return 0;
  1589. }
  1590. PRBool ruv_has_csns(const RUV *ruv)
  1591. {
  1592. PRBool retval = PR_TRUE;
  1593. CSN *mincsn = NULL;
  1594. CSN *maxcsn = NULL;
  1595. ruv_get_min_csn(ruv, &mincsn);
  1596. ruv_get_max_csn(ruv, &maxcsn);
  1597. if (mincsn) {
  1598. csn_free(&mincsn);
  1599. csn_free(&maxcsn);
  1600. } else if (maxcsn) {
  1601. csn_free(&maxcsn);
  1602. } else {
  1603. retval = PR_FALSE; /* both min and max are false */
  1604. }
  1605. return retval;
  1606. }
  1607. PRBool ruv_has_both_csns(const RUV *ruv)
  1608. {
  1609. PRBool retval = PR_TRUE;
  1610. CSN *mincsn = NULL;
  1611. CSN *maxcsn = NULL;
  1612. ruv_get_min_csn(ruv, &mincsn);
  1613. ruv_get_max_csn(ruv, &maxcsn);
  1614. if (mincsn) {
  1615. csn_free(&mincsn);
  1616. csn_free(&maxcsn);
  1617. } else if (maxcsn) {
  1618. csn_free(&maxcsn);
  1619. retval = PR_FALSE; /* it has a maxcsn but no mincsn */
  1620. } else {
  1621. retval = PR_FALSE; /* both min and max are false */
  1622. }
  1623. return retval;
  1624. }
  1625. /* Check if the first ruv is newer than the second one */
  1626. PRBool
  1627. ruv_is_newer (Object *sruvobj, Object *cruvobj)
  1628. {
  1629. RUV *sruv, *cruv;
  1630. RUVElement *sreplica, *creplica;
  1631. int scookie, ccookie;
  1632. int is_newer = PR_FALSE;
  1633. if ( sruvobj == NULL ) {
  1634. return 0;
  1635. }
  1636. if ( cruvobj == NULL ) {
  1637. return 1;
  1638. }
  1639. sruv = (RUV *) object_get_data ( sruvobj );
  1640. cruv = (RUV *) object_get_data ( cruvobj );
  1641. for (sreplica = dl_get_first (sruv->elements, &scookie); sreplica;
  1642. sreplica = dl_get_next (sruv->elements, &scookie))
  1643. {
  1644. /* A hub may have a dummy ruv with rid 65535 */
  1645. if ( sreplica->csn == NULL ) continue;
  1646. if ( cruv->elements == NULL )
  1647. {
  1648. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1649. "ruv_is_newer, consumer RUV has no elements\n");
  1650. is_newer=PR_FALSE;
  1651. break;
  1652. }
  1653. for (creplica = dl_get_first (cruv->elements, &ccookie); creplica;
  1654. creplica = dl_get_next (cruv->elements, &ccookie))
  1655. {
  1656. if ( sreplica->rid == creplica->rid ) {
  1657. if ( csn_compare ( sreplica->csn, creplica->csn ) > 0 ) {
  1658. is_newer = PR_TRUE;
  1659. }
  1660. break;
  1661. }
  1662. }
  1663. if ( creplica == NULL || is_newer ) {
  1664. is_newer = PR_TRUE;
  1665. break;
  1666. }
  1667. }
  1668. return is_newer;
  1669. }
  1670. void
  1671. ruv_force_csn_update (RUV *ruv, CSN *csn)
  1672. {
  1673. CSN *max = NULL;
  1674. if (ruv != NULL)
  1675. {
  1676. ruv_get_max_csn(ruv, &max);
  1677. if (csn_compare(max, csn))
  1678. {
  1679. ruv_set_max_csn(ruv, csn, NULL);
  1680. }
  1681. csn_free(&max);
  1682. }
  1683. }
  1684. #ifdef TESTING /* Some unit tests for code in this file */
  1685. static void
  1686. ruv_dump_internal(RUV *ruv)
  1687. {
  1688. RUVElement *replica;
  1689. int cookie;
  1690. char csnstr1[CSN_STRSIZE];
  1691. char csnstr2[CSN_STRSIZE];
  1692. PR_ASSERT(NULL != ruv);
  1693. printf("{replicageneration} %s\n", ruv->replGen == NULL ? "NULL" : ruv->replGen);
  1694. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1695. replica = dl_get_next (ruv->elements, &cookie))
  1696. {
  1697. printf("{replica%s%s} %s %s\n",
  1698. replica->replica_purl == NULL ? "" : " ",
  1699. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1700. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  1701. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  1702. }
  1703. }
  1704. void
  1705. ruv_test()
  1706. {
  1707. const struct berval *vals[5];
  1708. struct berval val0, val1, val2, val3;
  1709. RUV *ruv;
  1710. Slapi_Attr *attr;
  1711. Slapi_Value *sv0, *sv1, *sv2, *sv3;
  1712. int rc;
  1713. char csnstr[CSN_STRSIZE];
  1714. char *gen;
  1715. CSN *newcsn;
  1716. ReplicaId *ids;
  1717. int nids;
  1718. Slapi_Mod smods;
  1719. PRBool covers;
  1720. vals[0] = &val0;
  1721. vals[1] = &val1;
  1722. vals[2] = &val2;
  1723. vals[3] = &val3;
  1724. vals[4] = NULL;
  1725. val0.bv_val = "{replicageneration} 0440FDC0A33F";
  1726. val0.bv_len = strlen(val0.bv_val);
  1727. val1.bv_val = "{replica ldap://ggood.mcom.com:389} 12345670000000FE0000 12345671000000FE0000";
  1728. val1.bv_len = strlen(val1.bv_val);
  1729. val2.bv_val = "{replica ldaps://an-impossibly-long-host-name-that-drags-on-forever-and-forever.mcom.com:389} 11112110000000FF0000 11112111000000FF0000";
  1730. val2.bv_len = strlen(val2.bv_val);
  1731. val3.bv_val = "{replica} 12345672000000FD0000 12345673000000FD0000";
  1732. val3.bv_len = strlen(val3.bv_val);
  1733. rc = ruv_init_from_bervals(vals, &ruv);
  1734. ruv_dump_internal(ruv);
  1735. attr = slapi_attr_new();
  1736. attr = slapi_attr_init(attr, "ruvelement");
  1737. sv0 = slapi_value_new();
  1738. sv1 = slapi_value_new();
  1739. sv2 = slapi_value_new();
  1740. sv3 = slapi_value_new();
  1741. slapi_value_init_berval(sv0, &val0);
  1742. slapi_value_init_berval(sv1, &val1);
  1743. slapi_value_init_berval(sv2, &val2);
  1744. slapi_value_init_berval(sv3, &val3);
  1745. slapi_attr_add_value(attr, sv0);
  1746. slapi_attr_add_value(attr, sv1);
  1747. slapi_attr_add_value(attr, sv2);
  1748. slapi_attr_add_value(attr, sv3);
  1749. rc = ruv_init_from_slapi_attr(attr, &ruv);
  1750. ruv_dump_internal(ruv);
  1751. rc = ruv_delete_replica(ruv, 0xFF);
  1752. /* Should delete one replica */
  1753. ruv_dump_internal(ruv);
  1754. rc = ruv_delete_replica(ruv, 0xAA);
  1755. /* No such replica - should not do anything */
  1756. ruv_dump_internal(ruv);
  1757. rc = ruv_get_largest_csn_for_replica(ruv, 0xFE, &newcsn);
  1758. if (NULL != newcsn)
  1759. {
  1760. csn_as_string(newcsn, PR_FALSE, csnstr);
  1761. printf("Replica 0x%X has largest csn \"%s\"\n", 0xFE, csnstr);
  1762. }
  1763. else
  1764. {
  1765. printf("BAD - can't get largest CSN for replica 0x%X\n", 0xFE);
  1766. }
  1767. rc = ruv_get_smallest_csn_for_replica(ruv, 0xFE, &newcsn);
  1768. if (NULL != newcsn)
  1769. {
  1770. csn_as_string(newcsn, PR_FALSE, csnstr);
  1771. printf("Replica 0x%X has smallest csn \"%s\"\n", 0xFE, csnstr);
  1772. }
  1773. else
  1774. {
  1775. printf("BAD - can't get smallest CSN for replica 0x%X\n", 0xFE);
  1776. }
  1777. rc = ruv_get_largest_csn_for_replica(ruv, 0xAA, &newcsn);
  1778. printf("ruv_get_largest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  1779. rc = ruv_get_smallest_csn_for_replica(ruv, 0xAA, &newcsn);
  1780. printf("ruv_get_smallest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  1781. newcsn = csn_new_by_string("12345674000000FE0000"); /* Old replica 0xFE */
  1782. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1783. /* Should update replica FE's CSN */
  1784. ruv_dump_internal(ruv);
  1785. newcsn = csn_new_by_string("12345675000000FB0000"); /* New replica 0xFB */
  1786. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1787. /* Should get a new replica in the list with min == max csn */
  1788. ruv_dump_internal(ruv);
  1789. newcsn = csn_new_by_string("12345676000000FD0000"); /* Old replica 0xFD */
  1790. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  1791. /* Should update replica 0xFD so new CSN is newer than min CSN */
  1792. ruv_dump_internal(ruv);
  1793. gen = ruv_get_replica_generation(ruv);
  1794. printf("replica generation is \"%s\"\n", gen);
  1795. newcsn = csn_new_by_string("12345673000000FE0000"); /* Old replica 0xFE */
  1796. covers = ruv_covers_csn(ruv, newcsn); /* should say "true" */
  1797. newcsn = csn_new_by_string("12345675000000FE0000"); /* Old replica 0xFE */
  1798. covers = ruv_covers_csn(ruv, newcsn); /* Should say "false" */
  1799. newcsn = csn_new_by_string("123456700000000A0000"); /* New replica 0A */
  1800. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  1801. ruv_dump_internal(ruv);
  1802. newcsn = csn_new_by_string("123456710000000A0000"); /* New replica 0A */
  1803. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  1804. ruv_dump_internal(ruv);
  1805. newcsn = csn_new_by_string("123456700000000B0000"); /* New replica 0B */
  1806. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  1807. ruv_dump_internal(ruv);
  1808. newcsn = csn_new_by_string("123456710000000B0000"); /* New replica 0B */
  1809. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  1810. ruv_dump_internal(ruv);
  1811. /* ONREPL test ruv enumeration */
  1812. rc = ruv_to_smod(ruv, &smods);
  1813. ruv_destroy(&ruv);
  1814. }
  1815. #endif /* TESTING */