repl5_ruv.c 68 KB

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