repl5_ruv.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524
  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. }
  609. return return_value;
  610. }
  611. int
  612. ruv_set_min_csn(RUV *ruv, const CSN *min_csn, const char *replica_purl)
  613. {
  614. int return_value;
  615. slapi_rwlock_wrlock (ruv->lock);
  616. return_value = set_min_csn_nolock(ruv, min_csn, replica_purl);
  617. slapi_rwlock_unlock (ruv->lock);
  618. return return_value;
  619. }
  620. int
  621. ruv_set_max_csn(RUV *ruv, const CSN *max_csn, const char *replica_purl)
  622. {
  623. return ruv_set_max_csn_ext(ruv, max_csn, replica_purl, PR_FALSE);
  624. }
  625. int
  626. ruv_set_max_csn_ext(RUV *ruv, const CSN *max_csn, const char *replica_purl, PRBool must_be_greater)
  627. {
  628. int return_value;
  629. slapi_rwlock_wrlock (ruv->lock);
  630. return_value = set_max_csn_nolock_ext(ruv, max_csn, replica_purl, must_be_greater);
  631. slapi_rwlock_unlock (ruv->lock);
  632. return return_value;
  633. }
  634. int
  635. ruv_set_csns(RUV *ruv, const CSN *csn, const char *replica_purl)
  636. {
  637. RUVElement *replica;
  638. ReplicaId rid;
  639. int return_value;
  640. if (ruv == NULL || csn == NULL)
  641. {
  642. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_set_csns: NULL argument\n");
  643. return_value = RUV_BAD_DATA;
  644. }
  645. else
  646. {
  647. rid = csn_get_replicaid (csn);
  648. /* prevent element from being destroyed while we get its data */
  649. slapi_rwlock_wrlock (ruv->lock);
  650. replica = ruvGetReplica (ruv, rid);
  651. if (replica == NULL) /* add new replica */
  652. {
  653. replica = ruvAddReplica (ruv, csn, replica_purl);
  654. if (replica)
  655. return_value = RUV_SUCCESS;
  656. else
  657. return_value = RUV_MEMORY_ERROR;
  658. }
  659. else
  660. {
  661. if (csn_compare (csn, replica->csn) > 0)
  662. {
  663. if (replica->csn != NULL)
  664. {
  665. csn_init_by_csn ( replica->csn, csn );
  666. }
  667. else
  668. {
  669. replica->csn = csn_dup(csn);
  670. }
  671. replica->last_modified = current_time();
  672. if (replica_purl && (NULL == replica->replica_purl ||
  673. strcmp(replica->replica_purl, replica_purl) != 0))
  674. {
  675. /* slapi_ch_free accepts NULL pointer */
  676. slapi_ch_free((void **)&replica->replica_purl);
  677. replica->replica_purl = slapi_ch_strdup(replica_purl);
  678. }
  679. }
  680. /* XXXggood only need to worry about this if real min csn not committed to changelog yet */
  681. if (csn_compare (csn, replica->min_csn) < 0)
  682. {
  683. csn_free(&replica->min_csn);
  684. replica->min_csn = csn_dup(csn);
  685. }
  686. return_value = RUV_SUCCESS;
  687. }
  688. slapi_rwlock_unlock (ruv->lock);
  689. }
  690. return return_value;
  691. }
  692. /* This function, for each replica keeps the smallest CSN its seen so far.
  693. Used for initial setup of changelog purge vector */
  694. int
  695. ruv_set_csns_keep_smallest(RUV *ruv, const CSN *csn)
  696. {
  697. RUVElement *replica;
  698. ReplicaId rid;
  699. int return_value;
  700. if (ruv == NULL || csn == NULL)
  701. {
  702. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  703. "ruv_set_csns_keep_smallest: NULL argument\n");
  704. return_value = RUV_BAD_DATA;
  705. }
  706. else
  707. {
  708. rid = csn_get_replicaid (csn);
  709. /* prevent element from being destroyed while we get its data */
  710. slapi_rwlock_wrlock (ruv->lock);
  711. replica = ruvGetReplica (ruv, rid);
  712. if (replica == NULL) /* add new replica */
  713. {
  714. replica = ruvAddReplica (ruv, csn, NULL);
  715. if (replica)
  716. return_value = RUV_SUCCESS;
  717. else
  718. return_value = RUV_MEMORY_ERROR;
  719. }
  720. else
  721. {
  722. if (csn_compare (csn, replica->csn) < 0)
  723. {
  724. csn_free(&replica->csn);
  725. replica->csn = csn_dup(csn);
  726. replica->last_modified = current_time();
  727. }
  728. return_value = RUV_SUCCESS;
  729. }
  730. slapi_rwlock_unlock (ruv->lock);
  731. }
  732. return return_value;
  733. }
  734. void
  735. ruv_set_replica_generation(RUV *ruv, const char *csnstr)
  736. {
  737. if (NULL != csnstr && NULL != ruv)
  738. {
  739. slapi_rwlock_wrlock (ruv->lock);
  740. if (NULL != ruv->replGen)
  741. {
  742. slapi_ch_free((void **)&ruv->replGen);
  743. }
  744. ruv->replGen = slapi_ch_strdup(csnstr);
  745. slapi_rwlock_unlock (ruv->lock);
  746. }
  747. }
  748. char *
  749. ruv_get_replica_generation(const RUV *ruv)
  750. {
  751. char *return_str = NULL;
  752. if (!ruv) {
  753. return return_str;
  754. }
  755. slapi_rwlock_rdlock (ruv->lock);
  756. if (ruv != NULL && ruv->replGen != NULL)
  757. {
  758. return_str = slapi_ch_strdup(ruv->replGen);
  759. }
  760. slapi_rwlock_unlock (ruv->lock);
  761. return return_str;
  762. }
  763. static PRBool
  764. ruv_covers_csn_internal(const RUV *ruv, const CSN *csn, PRBool strict)
  765. {
  766. RUVElement *replica;
  767. ReplicaId rid;
  768. PRBool return_value;
  769. if (ruv == NULL || csn == NULL)
  770. {
  771. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn: NULL argument\n");
  772. return_value = PR_FALSE;
  773. }
  774. else
  775. {
  776. rid = csn_get_replicaid(csn);
  777. replica = ruvGetReplica (ruv, rid);
  778. if (replica == NULL)
  779. {
  780. /*
  781. * We don't know anything about this replica change in the cl, mark it to be zapped.
  782. * This could of been a previously cleaned ruv, but the server was restarted before
  783. * the change could be trimmed.
  784. *
  785. * Only the change log trimming calls this function with "strict" set. So we'll return success
  786. * if strict is set.
  787. */
  788. if(strict){
  789. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn: replica for id %d not found.\n", rid);
  790. return_value = PR_TRUE;
  791. } else {
  792. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "ruv_covers_csn: replica for id %d not found.\n", rid);
  793. return_value = PR_FALSE;
  794. }
  795. }
  796. else
  797. {
  798. if (strict)
  799. {
  800. return_value = (csn_compare (csn, replica->csn) < 0);
  801. }
  802. else
  803. {
  804. return_value = (csn_compare (csn, replica->csn) <= 0);
  805. }
  806. }
  807. }
  808. return return_value;
  809. }
  810. PRBool
  811. ruv_covers_csn(const RUV *ruv, const CSN *csn)
  812. {
  813. PRBool rc;
  814. slapi_rwlock_rdlock (ruv->lock);
  815. rc = ruv_covers_csn_internal(ruv, csn, PR_FALSE);
  816. slapi_rwlock_unlock (ruv->lock);
  817. return rc;
  818. }
  819. PRBool
  820. ruv_covers_csn_strict(const RUV *ruv, const CSN *csn)
  821. {
  822. PRBool rc;
  823. slapi_rwlock_rdlock (ruv->lock);
  824. rc = ruv_covers_csn_internal(ruv, csn, PR_TRUE);
  825. slapi_rwlock_unlock (ruv->lock);
  826. return rc;
  827. }
  828. /*
  829. * Used by the cleanallruv task
  830. *
  831. * We want to return TRUE if replica is NULL,
  832. * and we want to use "csn_compare() <="
  833. */
  834. PRBool
  835. ruv_covers_csn_cleanallruv(const RUV *ruv, const CSN *csn)
  836. {
  837. RUVElement *replica;
  838. ReplicaId rid;
  839. PRBool return_value;
  840. if (ruv == NULL || csn == NULL){
  841. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_covers_csn_cleanallruv: NULL argument\n");
  842. return_value = PR_FALSE;
  843. } else {
  844. rid = csn_get_replicaid(csn);
  845. replica = ruvGetReplica (ruv, rid);
  846. if (replica == NULL){
  847. /* already cleaned */
  848. return_value = PR_TRUE;
  849. } else {
  850. return_value = (csn_compare (csn, replica->csn) <= 0);
  851. }
  852. }
  853. return return_value;
  854. }
  855. /*
  856. * The function gets min{maxcsns of all ruv elements} if get_the_max=0,
  857. * or max{maxcsns of all ruv elements} if get_the_max != 0.
  858. */
  859. static int
  860. ruv_get_min_or_max_csn(const RUV *ruv, CSN **csn, int get_the_max, ReplicaId rid, int ignore_cleaned_rid)
  861. {
  862. int return_value = RUV_SUCCESS;
  863. if (ruv == NULL || csn == NULL)
  864. {
  865. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_get_min_or_max_csn: NULL argument\n");
  866. return_value = RUV_BAD_DATA;
  867. }
  868. else
  869. {
  870. CSN *found = NULL;
  871. RUVElement *replica;
  872. int cookie;
  873. slapi_rwlock_rdlock (ruv->lock);
  874. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  875. replica = dl_get_next (ruv->elements, &cookie))
  876. {
  877. /*
  878. * Skip replica whose maxcsn is NULL otherwise
  879. * the code will return different min_csn if
  880. * the sequence of the replicas is altered.
  881. *
  882. * don't use READ_ONLY replicas for computing the value of
  883. * "found", as they seem to have NULL csn and min_csn
  884. */
  885. if (replica->csn == NULL || replica->rid == READ_ONLY_REPLICA_ID)
  886. {
  887. continue;
  888. }
  889. if(ignore_cleaned_rid && is_cleaned_rid(replica->rid)){
  890. continue;
  891. }
  892. if(rid){ /* we are only interested in this rid's maxcsn */
  893. if(replica->rid == rid){
  894. found = replica->csn;
  895. break;
  896. }
  897. } else {
  898. if (found == NULL ||
  899. (!get_the_max && csn_compare(found, replica->csn)>0) ||
  900. ( get_the_max && csn_compare(found, replica->csn)<0))
  901. {
  902. found = replica->csn;
  903. }
  904. }
  905. }
  906. if (found == NULL)
  907. {
  908. *csn = NULL;
  909. }
  910. else
  911. {
  912. *csn = csn_dup (found);
  913. }
  914. slapi_rwlock_unlock (ruv->lock);
  915. }
  916. return return_value;
  917. }
  918. int
  919. ruv_get_rid_max_csn(const RUV *ruv, CSN **csn, ReplicaId rid)
  920. {
  921. return ruv_get_rid_max_csn_ext(ruv, csn, rid, 0);
  922. }
  923. int
  924. ruv_get_rid_max_csn_ext(const RUV *ruv, CSN **csn, ReplicaId rid, int ignore_cleaned_rid)
  925. {
  926. return ruv_get_min_or_max_csn(ruv, csn, 1 /* get the max */, rid, ignore_cleaned_rid);
  927. }
  928. int
  929. ruv_get_max_csn(const RUV *ruv, CSN **csn)
  930. {
  931. return ruv_get_max_csn_ext(ruv, csn, 0);
  932. }
  933. int
  934. ruv_get_max_csn_ext(const RUV *ruv, CSN **csn, int ignore_cleaned_rid)
  935. {
  936. return ruv_get_min_or_max_csn(ruv, csn, 1 /* get the max */, 0 /* rid */, ignore_cleaned_rid);
  937. }
  938. int
  939. ruv_get_min_csn(const RUV *ruv, CSN **csn)
  940. {
  941. return ruv_get_min_csn_ext(ruv, csn, 0);
  942. }
  943. int
  944. ruv_get_min_csn_ext(const RUV *ruv, CSN **csn, int ignore_cleaned_rid)
  945. {
  946. return ruv_get_min_or_max_csn(ruv, csn, 0 /* get the min */, 0 /* rid */, ignore_cleaned_rid);
  947. }
  948. int
  949. ruv_enumerate_elements (const RUV *ruv, FNEnumRUV fn, void *arg)
  950. {
  951. int cookie;
  952. RUVElement *elem;
  953. int rc = 0;
  954. ruv_enum_data enum_data = {0};
  955. if (ruv == NULL || fn == NULL)
  956. {
  957. /* ONREPL - log error */
  958. return -1;
  959. }
  960. slapi_rwlock_rdlock (ruv->lock);
  961. for (elem = (RUVElement*)dl_get_first (ruv->elements, &cookie); elem;
  962. elem = (RUVElement*)dl_get_next (ruv->elements, &cookie))
  963. {
  964. /* we only return elements that contains both minimal and maximal CSNs */
  965. if (elem->csn && elem->min_csn)
  966. {
  967. enum_data.csn = elem->csn;
  968. enum_data.min_csn = elem->min_csn;
  969. rc = fn (&enum_data, arg);
  970. if (rc != 0)
  971. break;
  972. }
  973. }
  974. slapi_rwlock_unlock (ruv->lock);
  975. return rc;
  976. }
  977. void
  978. ruv_element_to_string(RUVElement *ruvelem, struct berval *bv, char *buf, size_t bufsize)
  979. {
  980. char csnStr1[CSN_STRSIZE];
  981. char csnStr2[CSN_STRSIZE];
  982. const char *fmtstr = "%s%d%s%s}%s%s%s%s";
  983. if (buf && bufsize) {
  984. PR_snprintf(buf, bufsize, fmtstr,
  985. prefix_ruvcsn, ruvelem->rid,
  986. ruvelem->replica_purl == NULL ? "" : " ",
  987. ruvelem->replica_purl == NULL ? "" : ruvelem->replica_purl,
  988. ruvelem->min_csn == NULL ? "" : " ",
  989. ruvelem->min_csn == NULL ? "" : csn_as_string (ruvelem->min_csn, PR_FALSE, csnStr1),
  990. ruvelem->csn == NULL ? "" : " ",
  991. ruvelem->csn == NULL ? "" : csn_as_string (ruvelem->csn, PR_FALSE, csnStr2));
  992. } else {
  993. bv->bv_val = slapi_ch_smprintf(fmtstr,
  994. prefix_ruvcsn, ruvelem->rid,
  995. ruvelem->replica_purl == NULL ? "" : " ",
  996. ruvelem->replica_purl == NULL ? "" : ruvelem->replica_purl,
  997. ruvelem->min_csn == NULL ? "" : " ",
  998. ruvelem->min_csn == NULL ? "" : csn_as_string (ruvelem->min_csn, PR_FALSE, csnStr1),
  999. ruvelem->csn == NULL ? "" : " ",
  1000. ruvelem->csn == NULL ? "" : csn_as_string (ruvelem->csn, PR_FALSE, csnStr2));
  1001. bv->bv_len = strlen(bv->bv_val);
  1002. }
  1003. }
  1004. /*
  1005. * Convert a replica update vector to a NULL-terminated array
  1006. * of bervals. The caller is responsible for freeing the bervals.
  1007. */
  1008. int
  1009. ruv_to_bervals(const RUV *ruv, struct berval ***bvals)
  1010. {
  1011. struct berval **returned_bervals = NULL;
  1012. int return_value;
  1013. if (ruv == NULL || bvals == NULL)
  1014. {
  1015. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_bervals: NULL argument\n");
  1016. return_value = RUV_BAD_DATA;
  1017. }
  1018. else
  1019. {
  1020. int count;
  1021. int i;
  1022. RUVElement *replica;
  1023. int cookie;
  1024. slapi_rwlock_rdlock (ruv->lock);
  1025. count = dl_get_count (ruv->elements) + 2;
  1026. returned_bervals = (struct berval **)slapi_ch_malloc(sizeof(struct berval *) * count);
  1027. returned_bervals[count - 1] = NULL;
  1028. returned_bervals[0] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  1029. returned_bervals[0]->bv_val = slapi_ch_smprintf("%s %s",
  1030. prefix_replicageneration, ruv->replGen);
  1031. returned_bervals[0]->bv_len = strlen(returned_bervals[0]->bv_val);
  1032. for (i = 1, replica = dl_get_first (ruv->elements, &cookie); replica;
  1033. i++, replica = dl_get_next (ruv->elements, &cookie))
  1034. {
  1035. returned_bervals[i] = (struct berval *)slapi_ch_malloc(sizeof(struct berval));
  1036. ruv_element_to_string(replica, returned_bervals[i], NULL, 0);
  1037. }
  1038. slapi_rwlock_unlock (ruv->lock);
  1039. return_value = RUV_SUCCESS;
  1040. *bvals = returned_bervals;
  1041. }
  1042. return return_value;
  1043. }
  1044. void
  1045. ruv_get_cleaned_rids(RUV *ruv, ReplicaId *rids)
  1046. {
  1047. RUVElement *replica;
  1048. int cookie;
  1049. int i = 0;
  1050. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1051. replica = dl_get_next (ruv->elements, &cookie))
  1052. {
  1053. if(is_cleaned_rid(replica->rid)){
  1054. rids[i++] = replica->rid;
  1055. }
  1056. }
  1057. }
  1058. /*
  1059. * This routine dump the ruv (last modified time) into a value array
  1060. * the call must free the returned value array
  1061. */
  1062. Slapi_Value **
  1063. ruv_last_modified_to_valuearray(RUV *ruv)
  1064. {
  1065. RUVElement *ruv_e;
  1066. int cookie;
  1067. Slapi_Value *value;
  1068. Slapi_Value **values = NULL;
  1069. char *buffer;
  1070. /* Acquire the ruv lock */
  1071. slapi_rwlock_rdlock(ruv->lock);
  1072. /* Now loop for each RUVElement and store its string value into the valueset*/
  1073. for (ruv_e = dl_get_first(ruv->elements, &cookie);
  1074. NULL != ruv_e;
  1075. ruv_e = dl_get_next(ruv->elements, &cookie)) {
  1076. buffer = slapi_ch_smprintf("%s%d%s%s} %08lx", prefix_ruvcsn, ruv_e->rid,
  1077. ruv_e->replica_purl == NULL ? "" : " ",
  1078. ruv_e->replica_purl == NULL ? "" : ruv_e->replica_purl,
  1079. ruv_e->last_modified);
  1080. value = slapi_value_new_string_passin(buffer);
  1081. valuearray_add_value(&values, value);
  1082. slapi_value_free(&value);
  1083. }
  1084. slapi_rwlock_unlock(ruv->lock);
  1085. return (values);
  1086. }
  1087. /*
  1088. * This routine dump the ruv (replicageneration and ruvElements) into a value array
  1089. * the call must free the returned value array
  1090. */
  1091. Slapi_Value **
  1092. ruv_to_valuearray(RUV *ruv)
  1093. {
  1094. RUVElement *ruv_e;
  1095. int cookie;
  1096. Slapi_Value *value;
  1097. Slapi_Value **values = NULL;
  1098. struct berval bv;
  1099. char *buffer;
  1100. /* Acquire the ruv lock */
  1101. slapi_rwlock_rdlock(ruv->lock);
  1102. /* dump the replicageneration */
  1103. buffer = slapi_ch_smprintf("%s %s", prefix_replicageneration, ruv->replGen);
  1104. value = slapi_value_new_string_passin(buffer);
  1105. valuearray_add_value(&values, value);
  1106. slapi_value_free(&value);
  1107. /* Now loop for each RUVElement and store its string value into the valueset*/
  1108. for (ruv_e = dl_get_first(ruv->elements, &cookie);
  1109. NULL != ruv_e;
  1110. ruv_e = dl_get_next(ruv->elements, &cookie)) {
  1111. ruv_element_to_string(ruv_e, &bv, NULL, 0);
  1112. value = slapi_value_new_berval(&bv);
  1113. slapi_ber_bvdone(&bv);
  1114. valuearray_add_value(&values, value);
  1115. slapi_value_free(&value);
  1116. }
  1117. slapi_rwlock_unlock(ruv->lock);
  1118. return (values);
  1119. }
  1120. int
  1121. ruv_to_smod(const RUV *ruv, Slapi_Mod *smod)
  1122. {
  1123. int return_value;
  1124. if (ruv == NULL || smod == NULL)
  1125. {
  1126. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_to_smod: NULL argument\n");
  1127. return_value = RUV_BAD_DATA;
  1128. }
  1129. else
  1130. {
  1131. struct berval val;
  1132. RUVElement *replica;
  1133. int cookie;
  1134. #define B_SIZ 1024
  1135. char buf[B_SIZ];
  1136. slapi_rwlock_rdlock (ruv->lock);
  1137. slapi_mod_init (smod, dl_get_count (ruv->elements) + 1);
  1138. slapi_mod_set_type (smod, type_ruvElement);
  1139. slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  1140. PR_snprintf(buf, sizeof(buf), "%s %s", prefix_replicageneration, ruv->replGen);
  1141. val.bv_val = buf;
  1142. val.bv_len = strlen(buf);
  1143. slapi_mod_add_value(smod, &val);
  1144. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1145. replica = dl_get_next (ruv->elements, &cookie))
  1146. {
  1147. ruv_element_to_string(replica, NULL, buf, sizeof(buf));
  1148. val.bv_val = buf;
  1149. val.bv_len = strlen(buf);
  1150. slapi_mod_add_value(smod, &val);
  1151. }
  1152. slapi_rwlock_unlock (ruv->lock);
  1153. return_value = RUV_SUCCESS;
  1154. }
  1155. return return_value;
  1156. }
  1157. int
  1158. ruv_last_modified_to_smod(const RUV *ruv, Slapi_Mod *smod)
  1159. {
  1160. int return_value;
  1161. if (ruv == NULL || smod == NULL)
  1162. {
  1163. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_last_modified_to_smod: NULL argument\n");
  1164. return_value = RUV_BAD_DATA;
  1165. }
  1166. else
  1167. {
  1168. struct berval val;
  1169. RUVElement *replica;
  1170. int cookie;
  1171. char buf[B_SIZ];
  1172. slapi_rwlock_rdlock (ruv->lock);
  1173. slapi_mod_init (smod, dl_get_count (ruv->elements));
  1174. slapi_mod_set_type (smod, type_ruvElementUpdatetime);
  1175. slapi_mod_set_operation (smod, LDAP_MOD_REPLACE | LDAP_MOD_BVALUES);
  1176. val.bv_val = buf;
  1177. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1178. replica = dl_get_next (ruv->elements, &cookie))
  1179. {
  1180. PR_snprintf(buf, B_SIZ, "%s%d%s%s} %08lx", prefix_ruvcsn, replica->rid,
  1181. replica->replica_purl == NULL ? "" : " ",
  1182. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1183. replica->last_modified);
  1184. val.bv_len = strlen(buf);
  1185. slapi_mod_add_value(smod, &val);
  1186. }
  1187. slapi_rwlock_unlock (ruv->lock);
  1188. return_value = RUV_SUCCESS;
  1189. }
  1190. return return_value;
  1191. }
  1192. /*
  1193. * XXXggood do we need "ruv_covers_ruv_strict" ???? */
  1194. PRBool
  1195. ruv_covers_ruv(const RUV *covering_ruv, const RUV *covered_ruv)
  1196. {
  1197. PRBool return_value = PR_TRUE;
  1198. RUVElement *replica;
  1199. int cookie;
  1200. /* compare replica generations first */
  1201. if (covering_ruv->replGen == NULL || covered_ruv->replGen == NULL)
  1202. return PR_FALSE;
  1203. if (strcasecmp (covered_ruv->replGen, covering_ruv->replGen))
  1204. return PR_FALSE;
  1205. /* replica generation is the same, now compare element by element */
  1206. for (replica = dl_get_first (covered_ruv->elements, &cookie);
  1207. NULL != replica;
  1208. replica = dl_get_next (covered_ruv->elements, &cookie))
  1209. {
  1210. if (replica->csn &&
  1211. (ruv_covers_csn(covering_ruv, replica->csn) == PR_FALSE))
  1212. {
  1213. return_value = PR_FALSE;
  1214. /* Don't break here - may leave something referenced? */
  1215. }
  1216. }
  1217. return return_value;
  1218. }
  1219. /*
  1220. * This compares two ruvs to see if they are compatible. This is
  1221. * used, for example, when the data is reloaded, to see if the ruv
  1222. * from the database is compatible with the ruv from the changelog.
  1223. * If the replica generation is empty or does not match, the data
  1224. * is not compatible.
  1225. * If the maxcsns are not compatible, the ruvs are not compatible.
  1226. * However, if the first ruv has replica IDs that the second RUV
  1227. * does not have, and this is the only difference, the application
  1228. * may allow that with a warning.
  1229. */
  1230. int
  1231. ruv_compare_ruv(const RUV *ruv1, const char *ruv1name, const RUV *ruv2, const char *ruv2name, int strict, int loglevel)
  1232. {
  1233. int rc = 0;
  1234. int ii = 0;
  1235. const RUV *ruvalist[] = {ruv1, ruv2};
  1236. const RUV *ruvblist[] = {ruv2, ruv1};
  1237. int missinglist[2] = {0, 0};
  1238. const char *ruvanames[] = {ruv1name, ruv2name};
  1239. const char *ruvbnames[] = {ruv2name, ruv1name};
  1240. const int nitems = 2;
  1241. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1242. ruv_dump(ruv1, (char *)ruv1name, NULL);
  1243. ruv_dump(ruv2, (char *)ruv2name, NULL);
  1244. }
  1245. /* compare replica generations first */
  1246. if (ruv1->replGen == NULL || ruv2->replGen == NULL) {
  1247. slapi_log_error(loglevel, repl_plugin_name,
  1248. "ruv_compare_ruv: RUV [%s] is missing the replica generation\n",
  1249. ruv1->replGen ? ruv2name : ruv1name);
  1250. return RUV_COMP_NO_GENERATION;
  1251. }
  1252. if (strcasecmp (ruv1->replGen, ruv2->replGen)) {
  1253. slapi_log_error(loglevel, repl_plugin_name,
  1254. "ruv_compare_ruv: RUV [%s] replica generation [%s] does not match RUV [%s] [%s]\n",
  1255. ruv1name, ruv1->replGen, ruv2name, ruv2->replGen);
  1256. return RUV_COMP_GENERATION_DIFFERS;
  1257. }
  1258. /* replica generation is the same, now compare element by element */
  1259. for (ii = 0; ii < nitems; ++ii) {
  1260. int cookie;
  1261. const RUV *ruva = ruvalist[ii];
  1262. const RUV *ruvb = ruvblist[ii];
  1263. int *missing = &missinglist[ii];
  1264. const char *ruvaname = ruvanames[ii];
  1265. const char *ruvbname = ruvbnames[ii];
  1266. RUVElement *replicab;
  1267. for (replicab = dl_get_first (ruvb->elements, &cookie);
  1268. NULL != replicab;
  1269. replicab = dl_get_next (ruvb->elements, &cookie)) {
  1270. if (replicab->csn) {
  1271. ReplicaId rid = csn_get_replicaid(replicab->csn);
  1272. RUVElement *replicaa = ruvGetReplica(ruva, rid);
  1273. char csnstra[CSN_STRSIZE];
  1274. char csnstrb[CSN_STRSIZE];
  1275. char ruvelem[1024];
  1276. ruv_element_to_string(replicab, NULL, ruvelem, sizeof(ruvelem));
  1277. csn_as_string(replicab->csn, PR_FALSE, csnstrb);
  1278. if (replicaa == NULL) {
  1279. (*missing)++;
  1280. slapi_log_error(loglevel, repl_plugin_name,
  1281. "ruv_compare_ruv: RUV [%s] does not contain element [%s] "
  1282. "which is present in RUV [%s]\n",
  1283. ruvaname, ruvelem, ruvbname);
  1284. } else if (strict && (csn_compare (replicab->csn, replicaa->csn) >= 0)) {
  1285. csn_as_string(replicaa->csn, PR_FALSE, csnstra);
  1286. slapi_log_error(loglevel, repl_plugin_name,
  1287. "ruv_compare_ruv: the max CSN [%s] from RUV [%s] is larger "
  1288. "than or equal to the max CSN [%s] from RUV [%s] for element [%s]\n",
  1289. csnstrb, ruvbname, csnstra, ruvaname, ruvelem);
  1290. rc = RUV_COMP_CSN_DIFFERS;
  1291. } else if (csn_compare (replicab->csn, replicaa->csn) > 0) {
  1292. csn_as_string(replicaa->csn, PR_FALSE, csnstra);
  1293. slapi_log_error(loglevel, repl_plugin_name,
  1294. "ruv_compare_ruv: the max CSN [%s] from RUV [%s] is larger "
  1295. "than the max CSN [%s] from RUV [%s] for element [%s]\n",
  1296. csnstrb, ruvbname, csnstra, ruvaname, ruvelem);
  1297. rc = RUV_COMP_CSN_DIFFERS;
  1298. } else {
  1299. csn_as_string(replicaa->csn, PR_FALSE, csnstra);
  1300. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1301. "ruv_compare_ruv: the max CSN [%s] from RUV [%s] is less than "
  1302. "or equal to the max CSN [%s] from RUV [%s] for element [%s]\n",
  1303. csnstrb, ruvbname, csnstra, ruvaname, ruvelem);
  1304. }
  1305. } else {
  1306. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1307. "ruv_compare_ruv: RUV [%s] has an empty CSN\n",
  1308. ruvbname);
  1309. }
  1310. }
  1311. }
  1312. if (!rc) {
  1313. if (missinglist[0]) {
  1314. rc = RUV_COMP_RUV1_MISSING;
  1315. } else if (missinglist[1]) {
  1316. rc = RUV_COMP_RUV2_MISSING;
  1317. }
  1318. }
  1319. return rc;
  1320. }
  1321. PRInt32
  1322. ruv_replica_count (const RUV *ruv)
  1323. {
  1324. if (ruv == NULL)
  1325. return 0;
  1326. else
  1327. {
  1328. int count;
  1329. slapi_rwlock_rdlock (ruv->lock);
  1330. count = dl_get_count (ruv->elements);
  1331. slapi_rwlock_unlock (ruv->lock);
  1332. return count;
  1333. }
  1334. }
  1335. /*
  1336. * Extract all the referral URL's from the RUV (but self URL),
  1337. * returning them in an array of strings, that
  1338. * the caller must free.
  1339. */
  1340. char **
  1341. ruv_get_referrals(const RUV *ruv)
  1342. {
  1343. char **r= NULL;
  1344. int n;
  1345. const char *mypurl = multimaster_get_local_purl();
  1346. slapi_rwlock_rdlock (ruv->lock);
  1347. n = ruv_replica_count(ruv);
  1348. if(n>0)
  1349. {
  1350. RUVElement *replica;
  1351. int cookie;
  1352. int i= 0;
  1353. r= (char**)slapi_ch_calloc(sizeof(char*),n+1);
  1354. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1355. replica = dl_get_next (ruv->elements, &cookie))
  1356. {
  1357. /* Add URL into referrals if doesn't match self URL */
  1358. if((replica->replica_purl!=NULL) &&
  1359. (slapi_utf8casecmp((unsigned char *)replica->replica_purl,
  1360. (unsigned char *)mypurl) != 0))
  1361. {
  1362. r[i]= slapi_ch_strdup(replica->replica_purl);
  1363. i++;
  1364. }
  1365. }
  1366. }
  1367. slapi_rwlock_unlock (ruv->lock);
  1368. return r; /* Caller must free this */
  1369. }
  1370. void
  1371. ruv_dump(const RUV *ruv, char *ruv_name, PRFileDesc *prFile)
  1372. {
  1373. RUVElement *replica;
  1374. int cookie;
  1375. char csnstr1[CSN_STRSIZE];
  1376. char csnstr2[CSN_STRSIZE];
  1377. char buff[RUVSTR_SIZE];
  1378. int len = sizeof (buff);
  1379. PR_ASSERT(NULL != ruv);
  1380. if (!slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1381. return;
  1382. }
  1383. slapi_rwlock_rdlock (ruv->lock);
  1384. PR_snprintf (buff, len, "%s: {replicageneration} %s\n",
  1385. ruv_name ? ruv_name : type_ruvElement,
  1386. ruv->replGen == NULL ? "" : ruv->replGen);
  1387. if (prFile)
  1388. {
  1389. slapi_write_buffer (prFile, buff, strlen(buff));
  1390. }
  1391. else
  1392. {
  1393. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "%s", buff);
  1394. }
  1395. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  1396. replica = dl_get_next (ruv->elements, &cookie))
  1397. {
  1398. /* prefix_ruvcsn = "{replica " */
  1399. PR_snprintf (buff, len, "%s: %s%d%s%s}%s%s%s%s\n",
  1400. ruv_name ? ruv_name : type_ruvElement,
  1401. prefix_ruvcsn, replica->rid,
  1402. replica->replica_purl == NULL ? "" : " ",
  1403. replica->replica_purl == NULL ? "" : replica->replica_purl,
  1404. replica->min_csn == NULL ? "" : " ",
  1405. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  1406. replica->csn == NULL ? "" : " ",
  1407. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  1408. if (strlen (csnstr1) > 0) {
  1409. PR_snprintf (buff + strlen(buff) - 1, len - strlen(buff), " %08lx\n",
  1410. replica->last_modified);
  1411. }
  1412. if (prFile)
  1413. {
  1414. slapi_write_buffer (prFile, buff, strlen(buff));
  1415. }
  1416. else
  1417. {
  1418. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "%s", buff);
  1419. }
  1420. }
  1421. slapi_rwlock_unlock (ruv->lock);
  1422. }
  1423. /* this function notifies the ruv that there are operations in progress so that
  1424. they can be added to the pending list for the appropriate client. */
  1425. int ruv_add_csn_inprogress (RUV *ruv, const CSN *csn)
  1426. {
  1427. RUVElement* replica;
  1428. char csn_str[CSN_STRSIZE];
  1429. int rc = RUV_SUCCESS;
  1430. int rid = csn_get_replicaid (csn);
  1431. PR_ASSERT (ruv && csn);
  1432. /* locate ruvElement */
  1433. slapi_rwlock_wrlock (ruv->lock);
  1434. if(is_cleaned_rid(rid)){
  1435. /* return success because we want to consume the update, but not perform it */
  1436. rc = RUV_COVERS_CSN;
  1437. goto done;
  1438. }
  1439. replica = ruvGetReplica (ruv, rid);
  1440. if (replica == NULL)
  1441. {
  1442. replica = ruvAddReplicaNoCSN (ruv, rid, NULL/*purl*/);
  1443. if (replica == NULL)
  1444. {
  1445. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1446. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to add replica"
  1447. " that created csn %s\n", csn_as_string (csn, PR_FALSE, csn_str));
  1448. }
  1449. rc = RUV_MEMORY_ERROR;
  1450. goto done;
  1451. }
  1452. }
  1453. /* check first that this csn is not already covered by this RUV */
  1454. if (ruv_covers_csn_internal(ruv, csn, PR_FALSE))
  1455. {
  1456. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1457. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1458. "the csn %s has already be seen - ignoring\n",
  1459. csn_as_string (csn, PR_FALSE, csn_str));
  1460. }
  1461. rc = RUV_COVERS_CSN;
  1462. goto done;
  1463. }
  1464. rc = csnplInsert (replica->csnpl, csn);
  1465. if (rc == 1) /* we already seen this csn */
  1466. {
  1467. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1468. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: "
  1469. "the csn %s has already be seen - ignoring\n",
  1470. csn_as_string (csn, PR_FALSE, csn_str));
  1471. }
  1472. rc = RUV_COVERS_CSN;
  1473. }
  1474. else if(rc != 0)
  1475. {
  1476. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1477. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: failed to insert csn %s"
  1478. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1479. }
  1480. rc = RUV_UNKNOWN_ERROR;
  1481. }
  1482. else
  1483. {
  1484. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1485. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_add_csn_inprogress: successfully inserted csn %s"
  1486. " into pending list\n", csn_as_string (csn, PR_FALSE, csn_str));
  1487. }
  1488. rc = RUV_SUCCESS;
  1489. }
  1490. done:
  1491. slapi_rwlock_unlock (ruv->lock);
  1492. return rc;
  1493. }
  1494. int ruv_cancel_csn_inprogress (RUV *ruv, const CSN *csn)
  1495. {
  1496. RUVElement* replica;
  1497. int rc = RUV_SUCCESS;
  1498. PR_ASSERT (ruv && csn);
  1499. /* locate ruvElement */
  1500. slapi_rwlock_wrlock (ruv->lock);
  1501. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1502. if (replica == NULL)
  1503. {
  1504. /* ONREPL - log error */
  1505. rc = RUV_NOTFOUND;
  1506. goto done;
  1507. }
  1508. rc = csnplRemove (replica->csnpl, csn);
  1509. if (rc != 0)
  1510. rc = RUV_NOTFOUND;
  1511. else
  1512. rc = RUV_SUCCESS;
  1513. done:
  1514. slapi_rwlock_unlock (ruv->lock);
  1515. return rc;
  1516. }
  1517. int ruv_update_ruv (RUV *ruv, const CSN *csn, const char *replica_purl, PRBool isLocal)
  1518. {
  1519. int rc=RUV_SUCCESS;
  1520. char csn_str[CSN_STRSIZE];
  1521. CSN *max_csn;
  1522. CSN *first_csn = NULL;
  1523. RUVElement *replica;
  1524. PR_ASSERT (ruv && csn);
  1525. slapi_rwlock_wrlock (ruv->lock);
  1526. replica = ruvGetReplica (ruv, csn_get_replicaid (csn));
  1527. if (replica == NULL)
  1528. {
  1529. /* we should have a ruv element at this point because it would have
  1530. been added by ruv_add_inprogress function */
  1531. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1532. "can't locate RUV element for replica %d\n", csn_get_replicaid (csn));
  1533. goto done;
  1534. }
  1535. if (csnplCommit(replica->csnpl, csn) != 0)
  1536. {
  1537. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "ruv_update_ruv: cannot commit csn %s\n",
  1538. csn_as_string(csn, PR_FALSE, csn_str));
  1539. rc = RUV_CSNPL_ERROR;
  1540. goto done;
  1541. }
  1542. else
  1543. {
  1544. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1545. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: "
  1546. "successfully committed csn %s\n", csn_as_string(csn, PR_FALSE, csn_str));
  1547. }
  1548. }
  1549. if ((max_csn = csnplRollUp(replica->csnpl, &first_csn)) != NULL)
  1550. {
  1551. #ifdef DEBUG
  1552. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "ruv_update_ruv: rolled up to csn %s\n",
  1553. csn_as_string(max_csn, PR_FALSE, csn_str)); /* XXXggood remove debugging */
  1554. #endif
  1555. /* replica object sets min csn for local replica */
  1556. if (!isLocal && replica->min_csn == NULL) {
  1557. /* bug 559223 - it seems that, under huge stress, a server might pass
  1558. * through this code when more than 1 change has already been sent and commited into
  1559. * the pending lists... Therefore, as we are trying to set the min_csn ever
  1560. * generated by this replica, we need to set the first_csn as the min csn in the
  1561. * ruv */
  1562. set_min_csn_nolock(ruv, first_csn, replica_purl);
  1563. }
  1564. /* only update the max_csn in the RUV if it is greater than the existing one */
  1565. rc = set_max_csn_nolock_ext(ruv, max_csn, replica_purl, PR_TRUE /* must be greater */);
  1566. /* It is possible that first_csn points to max_csn.
  1567. We need to free it once */
  1568. if (max_csn != first_csn) {
  1569. csn_free(&first_csn);
  1570. }
  1571. csn_free(&max_csn);
  1572. }
  1573. done:
  1574. slapi_rwlock_unlock (ruv->lock);
  1575. return rc;
  1576. }
  1577. /* Helper functions */
  1578. static int
  1579. ruvInit (RUV **ruv, int initCount)
  1580. {
  1581. PR_ASSERT (ruv);
  1582. if (ruv == NULL) {
  1583. return RUV_NSPR_ERROR;
  1584. }
  1585. /* allocate new RUV */
  1586. *ruv = (RUV *)slapi_ch_calloc (1, sizeof (RUV));
  1587. /* allocate elements */
  1588. (*ruv)->elements = dl_new (); /* never returns NULL */
  1589. dl_init ((*ruv)->elements, initCount);
  1590. /* create lock */
  1591. (*ruv)->lock = slapi_new_rwlock();
  1592. if ((*ruv)->lock == NULL)
  1593. {
  1594. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1595. "ruvInit: failed to create lock\n");
  1596. dl_free(&(*ruv)->elements);
  1597. slapi_ch_free((void**)ruv);
  1598. return RUV_NSPR_ERROR;
  1599. }
  1600. return RUV_SUCCESS;
  1601. }
  1602. static void
  1603. ruvFreeReplica (void **data)
  1604. {
  1605. RUVElement *element = *(RUVElement**)data;
  1606. if (NULL != element)
  1607. {
  1608. if (NULL != element->csn)
  1609. {
  1610. csn_free (&element->csn);
  1611. }
  1612. if (NULL != element->min_csn)
  1613. {
  1614. csn_free (&element->min_csn);
  1615. }
  1616. /* slapi_ch_free accepts NULL pointer */
  1617. slapi_ch_free((void **)&element->replica_purl);
  1618. if (element->csnpl)
  1619. {
  1620. csnplFree (&(element->csnpl));
  1621. }
  1622. slapi_ch_free ((void **)&element);
  1623. }
  1624. }
  1625. static RUVElement*
  1626. ruvAddReplica (RUV *ruv, const CSN *csn, const char *replica_purl)
  1627. {
  1628. RUVElement *replica;
  1629. PR_ASSERT (NULL != ruv);
  1630. PR_ASSERT (NULL != csn);
  1631. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1632. if (replica == NULL)
  1633. {
  1634. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1635. "ruvAddReplica: memory allocation failed\n");
  1636. return NULL;
  1637. }
  1638. replica->rid = csn_get_replicaid (csn);
  1639. /* PR_ASSERT(replica->rid != READ_ONLY_REPLICA_ID); */
  1640. replica->csn = csn_dup (csn);
  1641. replica->last_modified = current_time();
  1642. replica->min_csn = csn_dup (csn);
  1643. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1644. replica->csnpl = csnplNew ();
  1645. dl_add (ruv->elements, replica);
  1646. return replica;
  1647. }
  1648. static RUVElement*
  1649. ruvAddReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl)
  1650. {
  1651. RUVElement *replica;
  1652. PR_ASSERT (NULL != ruv);
  1653. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1654. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1655. if (replica == NULL)
  1656. {
  1657. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1658. "ruvAddReplicaNoCSN: memory allocation failed\n");
  1659. return NULL;
  1660. }
  1661. replica->rid = rid;
  1662. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1663. replica->csnpl = csnplNew ();
  1664. dl_add (ruv->elements, replica);
  1665. return replica;
  1666. }
  1667. static RUVElement*
  1668. ruvAddIndexReplicaNoCSN (RUV *ruv, ReplicaId rid, const char *replica_purl, int index)
  1669. {
  1670. RUVElement *replica;
  1671. PR_ASSERT (NULL != ruv);
  1672. /* PR_ASSERT(rid != READ_ONLY_REPLICA_ID); */
  1673. replica = (RUVElement *)slapi_ch_calloc (1, sizeof (RUVElement));
  1674. if (replica == NULL)
  1675. {
  1676. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1677. "ruvAddIndexReplicaNoCSN: memory allocation failed\n");
  1678. return NULL;
  1679. }
  1680. replica->rid = rid;
  1681. replica->replica_purl = slapi_ch_strdup(replica_purl);
  1682. replica->csnpl = csnplNew ();
  1683. dl_add_index (ruv->elements, replica, index);
  1684. return replica;
  1685. }
  1686. static RUVElement *
  1687. ruvGetReplica (const RUV *ruv, ReplicaId rid)
  1688. {
  1689. PR_ASSERT (ruv /* && rid >= 0 -- rid can't be negative */);
  1690. return (RUVElement *)dl_get (ruv->elements, (const void*)&rid, ruvReplicaCompare);
  1691. }
  1692. static int
  1693. ruvReplicaCompare (const void *el1, const void *el2)
  1694. {
  1695. RUVElement *replica = (RUVElement*)el1;
  1696. ReplicaId *rid1 = (ReplicaId*) el2;
  1697. if (replica == NULL || rid1 == NULL)
  1698. return -1;
  1699. if (*rid1 == replica->rid)
  1700. return 0;
  1701. if (*rid1 < replica->rid)
  1702. return -1;
  1703. else
  1704. return 1;
  1705. }
  1706. /*
  1707. * Given a berval that points to a string of the form:
  1708. * "{dbgen} generation-id", return a copy of the
  1709. * "generation-id" part in a null-terminated string.
  1710. * Returns NULL if the berval is malformed.
  1711. */
  1712. static char *
  1713. get_replgen_from_berval(const struct berval *bval)
  1714. {
  1715. char *ret_string = NULL;
  1716. if (NULL != bval && NULL != bval->bv_val &&
  1717. (bval->bv_len > strlen(prefix_replicageneration)) &&
  1718. strncasecmp(bval->bv_val, prefix_replicageneration,
  1719. strlen(prefix_replicageneration)) == 0)
  1720. {
  1721. unsigned int index = strlen(prefix_replicageneration);
  1722. /* Skip any whitespace */
  1723. while (index++ < bval->bv_len && bval->bv_val[index] == ' ');
  1724. if (index < bval->bv_len)
  1725. {
  1726. unsigned int ret_len = bval->bv_len - index;
  1727. ret_string = slapi_ch_malloc(ret_len + 1);
  1728. memcpy(ret_string, &bval->bv_val[index], ret_len);
  1729. ret_string[ret_len] = '\0';
  1730. }
  1731. }
  1732. return ret_string;
  1733. }
  1734. /*
  1735. * Given a berval that points to a string of the form:
  1736. * "{replica ldap[s]//host:port} <min_csn> <csn>", parse out the
  1737. * partial URL and the CSNs into an RUVElement, and return
  1738. * a pointer to the copy. Returns NULL if the berval is
  1739. * malformed.
  1740. */
  1741. static RUVElement *
  1742. get_ruvelement_from_berval(const struct berval *bval)
  1743. {
  1744. RUVElement *ret_ruve = NULL;
  1745. char *purl = NULL;
  1746. ReplicaId rid = 0;
  1747. char ridbuff [RIDSTR_SIZE];
  1748. int i;
  1749. if (NULL == bval || NULL == bval->bv_val ||
  1750. bval->bv_len <= strlen(prefix_ruvcsn) ||
  1751. strncasecmp(bval->bv_val, prefix_ruvcsn, strlen(prefix_ruvcsn)) != 0)
  1752. {
  1753. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1754. "get_ruvelement_from_berval: invalid berval\n");
  1755. goto loser;
  1756. }
  1757. {
  1758. unsigned int urlbegin = strlen(prefix_ruvcsn);
  1759. unsigned int urlend;
  1760. unsigned int mincsnbegin;
  1761. /* replica id must be here */
  1762. i = 0;
  1763. while (isdigit (bval->bv_val[urlbegin]))
  1764. {
  1765. ridbuff [i] = bval->bv_val[urlbegin];
  1766. i++;
  1767. urlbegin ++;
  1768. }
  1769. if (i == 0) /* replicaid is missing */
  1770. goto loser;
  1771. ridbuff[i] = '\0';
  1772. rid = atoi (ridbuff);
  1773. if (bval->bv_val[urlbegin] == '}')
  1774. {
  1775. /* No purl in this value */
  1776. purl = NULL;
  1777. mincsnbegin = urlbegin + 1;
  1778. }
  1779. else
  1780. {
  1781. while (urlbegin++ < bval->bv_len && bval->bv_val[urlbegin] == ' ');
  1782. urlend = urlbegin;
  1783. while (urlend++ < bval->bv_len && bval->bv_val[urlend] != '}');
  1784. purl = slapi_ch_malloc(urlend - urlbegin + 1);
  1785. memcpy(purl, &bval->bv_val[urlbegin], urlend - urlbegin);
  1786. purl[urlend - urlbegin] = '\0';
  1787. mincsnbegin = urlend;
  1788. }
  1789. /* Skip any whitespace before the first (minimum) CSN */
  1790. while (mincsnbegin++ < (bval->bv_len-1) && bval->bv_val[mincsnbegin] == ' ');
  1791. /* Now, mincsnbegin should contain the index of the beginning of the first csn */
  1792. if (mincsnbegin >= bval->bv_len)
  1793. {
  1794. /* Missing the entire content*/
  1795. if (purl == NULL)
  1796. goto loser;
  1797. else /* we have just purl - no changes from the replica has been seen */
  1798. {
  1799. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1800. ret_ruve->rid = rid;
  1801. ret_ruve->replica_purl = purl;
  1802. }
  1803. }
  1804. else
  1805. {
  1806. if ((bval->bv_len - mincsnbegin != (_CSN_VALIDCSN_STRLEN * 2) + 1)
  1807. && (bval->bv_len - mincsnbegin != (_CSN_VALIDCSN_STRLEN * 2) + 10))
  1808. {
  1809. /* Malformed - incorrect length for 2 CSNs + space AND
  1810. * incorrect length for 2 CSNs + 2 spaces +
  1811. * last_modified (see ruv_dump) */
  1812. goto loser;
  1813. }
  1814. else
  1815. {
  1816. char mincsnstr[CSN_STRSIZE];
  1817. char maxcsnstr[CSN_STRSIZE];
  1818. memset(mincsnstr, '\0', CSN_STRSIZE);
  1819. memset(maxcsnstr, '\0', CSN_STRSIZE);
  1820. memcpy(mincsnstr, &bval->bv_val[mincsnbegin], _CSN_VALIDCSN_STRLEN);
  1821. memcpy(maxcsnstr, &bval->bv_val[mincsnbegin + _CSN_VALIDCSN_STRLEN + 1], _CSN_VALIDCSN_STRLEN);
  1822. ret_ruve = (RUVElement *)slapi_ch_calloc(1, sizeof(RUVElement));
  1823. ret_ruve->min_csn = csn_new_by_string(mincsnstr);
  1824. ret_ruve->csn = csn_new_by_string(maxcsnstr);
  1825. ret_ruve->rid = rid;
  1826. ret_ruve->replica_purl = purl;
  1827. if (NULL == ret_ruve->min_csn || NULL == ret_ruve->csn)
  1828. {
  1829. goto loser;
  1830. }
  1831. }
  1832. }
  1833. }
  1834. /* initialize csn pending list */
  1835. ret_ruve->csnpl = csnplNew ();
  1836. if (ret_ruve->csnpl == NULL)
  1837. {
  1838. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1839. "get_ruvelement_from_berval: failed to create csn pending list\n");
  1840. goto loser;
  1841. }
  1842. return ret_ruve;
  1843. loser:
  1844. /* slapi_ch_free accepts NULL pointer */
  1845. slapi_ch_free((void **)&purl);
  1846. if (NULL != ret_ruve)
  1847. {
  1848. if (NULL != ret_ruve->min_csn)
  1849. {
  1850. csn_free(&ret_ruve->min_csn);
  1851. }
  1852. if (NULL != ret_ruve->csn)
  1853. {
  1854. csn_free(&ret_ruve->csn);
  1855. }
  1856. slapi_ch_free((void **)&ret_ruve);
  1857. }
  1858. return NULL;
  1859. }
  1860. int
  1861. ruv_move_local_supplier_to_first(RUV *ruv, ReplicaId aRid)
  1862. {
  1863. RUVElement * elem = NULL;
  1864. int rc = RUV_NOTFOUND;
  1865. PR_ASSERT(ruv);
  1866. slapi_rwlock_wrlock (ruv->lock);
  1867. elem = (RUVElement *)dl_delete(ruv->elements,(const void*)&aRid, ruvReplicaCompare, 0);
  1868. if (elem) {
  1869. dl_add_index(ruv->elements, elem, 1);
  1870. rc = RUV_SUCCESS;
  1871. }
  1872. slapi_rwlock_unlock (ruv->lock);
  1873. return rc;
  1874. }
  1875. int
  1876. ruv_get_first_id_and_purl(RUV *ruv, ReplicaId *rid, char **replica_purl )
  1877. {
  1878. RUVElement * first = NULL;
  1879. int cookie;
  1880. int rc;
  1881. PR_ASSERT(ruv);
  1882. slapi_rwlock_rdlock (ruv->lock);
  1883. first = dl_get_first(ruv->elements, &cookie);
  1884. if ( first == NULL )
  1885. {
  1886. rc = RUV_MEMORY_ERROR;
  1887. }
  1888. else
  1889. {
  1890. *rid = first->rid;
  1891. *replica_purl = first->replica_purl;
  1892. rc = RUV_SUCCESS;
  1893. }
  1894. slapi_rwlock_unlock (ruv->lock);
  1895. return rc;
  1896. }
  1897. int ruv_local_contains_supplier(RUV *ruv, ReplicaId rid)
  1898. {
  1899. int cookie;
  1900. RUVElement *elem = NULL;
  1901. PR_ASSERT(ruv);
  1902. slapi_rwlock_rdlock (ruv->lock);
  1903. for (elem = dl_get_first (ruv->elements, &cookie);
  1904. elem;
  1905. elem = dl_get_next (ruv->elements, &cookie))
  1906. {
  1907. if (elem->rid == rid){
  1908. slapi_rwlock_unlock (ruv->lock);
  1909. return 1;
  1910. }
  1911. }
  1912. slapi_rwlock_unlock (ruv->lock);
  1913. return 0;
  1914. }
  1915. PRBool ruv_has_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. } else {
  1928. retval = PR_FALSE; /* both min and max are false */
  1929. }
  1930. return retval;
  1931. }
  1932. PRBool ruv_has_both_csns(const RUV *ruv)
  1933. {
  1934. PRBool retval = PR_TRUE;
  1935. CSN *mincsn = NULL;
  1936. CSN *maxcsn = NULL;
  1937. ruv_get_min_csn(ruv, &mincsn);
  1938. ruv_get_max_csn(ruv, &maxcsn);
  1939. if (mincsn) {
  1940. csn_free(&mincsn);
  1941. csn_free(&maxcsn);
  1942. } else if (maxcsn) {
  1943. csn_free(&maxcsn);
  1944. retval = PR_FALSE; /* it has a maxcsn but no mincsn */
  1945. } else {
  1946. retval = PR_FALSE; /* both min and max are false */
  1947. }
  1948. return retval;
  1949. }
  1950. /* Check if the first ruv is newer than the second one */
  1951. PRBool
  1952. ruv_is_newer (Object *sruvobj, Object *cruvobj)
  1953. {
  1954. RUV *sruv, *cruv;
  1955. RUVElement *sreplica, *creplica;
  1956. int scookie, ccookie;
  1957. int is_newer = PR_FALSE;
  1958. if ( sruvobj == NULL ) {
  1959. return 0;
  1960. }
  1961. if ( cruvobj == NULL ) {
  1962. return 1;
  1963. }
  1964. sruv = (RUV *) object_get_data ( sruvobj );
  1965. cruv = (RUV *) object_get_data ( cruvobj );
  1966. for (sreplica = dl_get_first (sruv->elements, &scookie); sreplica;
  1967. sreplica = dl_get_next (sruv->elements, &scookie))
  1968. {
  1969. /* A hub may have a dummy ruv with rid 65535 */
  1970. if ( sreplica->csn == NULL ) continue;
  1971. if ( cruv->elements == NULL )
  1972. {
  1973. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1974. "ruv_is_newer, consumer RUV has no elements\n");
  1975. is_newer=PR_FALSE;
  1976. break;
  1977. }
  1978. for (creplica = dl_get_first (cruv->elements, &ccookie); creplica;
  1979. creplica = dl_get_next (cruv->elements, &ccookie))
  1980. {
  1981. if ( sreplica->rid == creplica->rid ) {
  1982. if ( csn_compare ( sreplica->csn, creplica->csn ) > 0 ) {
  1983. is_newer = PR_TRUE;
  1984. }
  1985. break;
  1986. }
  1987. }
  1988. if ( creplica == NULL || is_newer ) {
  1989. is_newer = PR_TRUE;
  1990. break;
  1991. }
  1992. }
  1993. return is_newer;
  1994. }
  1995. /*
  1996. * This routine is called after a disordely shutdown
  1997. * The Database RUV was found late compare to the changelog RUV
  1998. */
  1999. void
  2000. ruv_force_csn_update_from_ruv(RUV *src_ruv, RUV *tgt_ruv, char *msg, int logLevel) {
  2001. RUVElement *replica = NULL;
  2002. int cookie;
  2003. slapi_rwlock_rdlock(src_ruv->lock);
  2004. for (replica = dl_get_first(src_ruv->elements, &cookie);
  2005. NULL != replica;
  2006. replica = dl_get_next(src_ruv->elements, &cookie)) {
  2007. /*
  2008. * In case the DB RUV (tgt_ruv) is behind the CL RUV (src_ruv)
  2009. * updates the DB RUV.
  2010. */
  2011. if (!ruv_covers_csn(tgt_ruv, replica->csn)) {
  2012. char csnStr[CSN_STRSIZE];
  2013. ruv_force_csn_update(tgt_ruv, replica->csn);
  2014. csn_as_string(replica->csn, PR_FALSE, csnStr);
  2015. slapi_log_error(logLevel, repl_plugin_name, "%s %s\n",
  2016. msg, csnStr);
  2017. }
  2018. }
  2019. slapi_rwlock_unlock(src_ruv->lock);
  2020. }
  2021. void
  2022. ruv_force_csn_update (RUV *ruv, CSN *csn)
  2023. {
  2024. CSN *max = NULL;
  2025. if (ruv != NULL)
  2026. {
  2027. ruv_get_max_csn(ruv, &max);
  2028. if (csn_compare(max, csn))
  2029. {
  2030. ruv_set_max_csn(ruv, csn, NULL);
  2031. }
  2032. csn_free(&max);
  2033. }
  2034. }
  2035. /* This routine is used to iterate the elements in a RUV and set each vector's
  2036. + * minimal CSN to a dummy with just the rid set, e.g. 00000000000000010000 */
  2037. void
  2038. ruv_insert_dummy_min_csn (RUV *ruv)
  2039. {
  2040. RUVElement *r;
  2041. int cookie;
  2042. for (r = dl_get_first (ruv->elements, &cookie); r;
  2043. r = dl_get_next (ruv->elements, &cookie)) {
  2044. if (r->csn && !r->min_csn) {
  2045. CSN *dummycsn = csn_new();
  2046. csn_init(dummycsn);
  2047. csn_set_replicaid(dummycsn, csn_get_replicaid(r->csn));
  2048. r->min_csn = dummycsn;
  2049. }
  2050. }
  2051. }
  2052. #ifdef TESTING /* Some unit tests for code in this file */
  2053. static void
  2054. ruv_dump_internal(RUV *ruv)
  2055. {
  2056. RUVElement *replica;
  2057. int cookie;
  2058. char csnstr1[CSN_STRSIZE];
  2059. char csnstr2[CSN_STRSIZE];
  2060. PR_ASSERT(NULL != ruv);
  2061. printf("{replicageneration} %s\n", ruv->replGen == NULL ? "NULL" : ruv->replGen);
  2062. for (replica = dl_get_first (ruv->elements, &cookie); replica;
  2063. replica = dl_get_next (ruv->elements, &cookie))
  2064. {
  2065. printf("{replica%s%s} %s %s\n",
  2066. replica->replica_purl == NULL ? "" : " ",
  2067. replica->replica_purl == NULL ? "" : replica->replica_purl,
  2068. csn_as_string(replica->min_csn, PR_FALSE, csnstr1),
  2069. csn_as_string(replica->csn, PR_FALSE, csnstr2));
  2070. }
  2071. }
  2072. void
  2073. ruv_test()
  2074. {
  2075. const struct berval *vals[5];
  2076. struct berval val0, val1, val2, val3;
  2077. RUV *ruv;
  2078. Slapi_Attr *attr;
  2079. Slapi_Value *sv0, *sv1, *sv2, *sv3;
  2080. int rc;
  2081. char csnstr[CSN_STRSIZE];
  2082. char *gen;
  2083. CSN *newcsn;
  2084. ReplicaId *ids;
  2085. int nids;
  2086. Slapi_Mod smods;
  2087. PRBool covers;
  2088. vals[0] = &val0;
  2089. vals[1] = &val1;
  2090. vals[2] = &val2;
  2091. vals[3] = &val3;
  2092. vals[4] = NULL;
  2093. val0.bv_val = "{replicageneration} 0440FDC0A33F";
  2094. val0.bv_len = strlen(val0.bv_val);
  2095. val1.bv_val = "{replica ldap://ggood.mcom.com:389} 12345670000000FE0000 12345671000000FE0000";
  2096. val1.bv_len = strlen(val1.bv_val);
  2097. val2.bv_val = "{replica ldaps://an-impossibly-long-host-name-that-drags-on-forever-and-forever.mcom.com:389} 11112110000000FF0000 11112111000000FF0000";
  2098. val2.bv_len = strlen(val2.bv_val);
  2099. val3.bv_val = "{replica} 12345672000000FD0000 12345673000000FD0000";
  2100. val3.bv_len = strlen(val3.bv_val);
  2101. rc = ruv_init_from_bervals(vals, &ruv);
  2102. ruv_dump_internal(ruv);
  2103. attr = slapi_attr_new();
  2104. attr = slapi_attr_init(attr, "ruvelement");
  2105. sv0 = slapi_value_new();
  2106. sv1 = slapi_value_new();
  2107. sv2 = slapi_value_new();
  2108. sv3 = slapi_value_new();
  2109. slapi_value_init_berval(sv0, &val0);
  2110. slapi_value_init_berval(sv1, &val1);
  2111. slapi_value_init_berval(sv2, &val2);
  2112. slapi_value_init_berval(sv3, &val3);
  2113. slapi_attr_add_value(attr, sv0);
  2114. slapi_attr_add_value(attr, sv1);
  2115. slapi_attr_add_value(attr, sv2);
  2116. slapi_attr_add_value(attr, sv3);
  2117. rc = ruv_init_from_slapi_attr(attr, &ruv);
  2118. ruv_dump_internal(ruv);
  2119. rc = ruv_delete_replica(ruv, 0xFF);
  2120. /* Should delete one replica */
  2121. ruv_dump_internal(ruv);
  2122. rc = ruv_delete_replica(ruv, 0xAA);
  2123. /* No such replica - should not do anything */
  2124. ruv_dump_internal(ruv);
  2125. rc = ruv_get_largest_csn_for_replica(ruv, 0xFE, &newcsn);
  2126. if (NULL != newcsn)
  2127. {
  2128. csn_as_string(newcsn, PR_FALSE, csnstr);
  2129. printf("Replica 0x%X has largest csn \"%s\"\n", 0xFE, csnstr);
  2130. }
  2131. else
  2132. {
  2133. printf("BAD - can't get largest CSN for replica 0x%X\n", 0xFE);
  2134. }
  2135. rc = ruv_get_smallest_csn_for_replica(ruv, 0xFE, &newcsn);
  2136. if (NULL != newcsn)
  2137. {
  2138. csn_as_string(newcsn, PR_FALSE, csnstr);
  2139. printf("Replica 0x%X has smallest csn \"%s\"\n", 0xFE, csnstr);
  2140. }
  2141. else
  2142. {
  2143. printf("BAD - can't get smallest CSN for replica 0x%X\n", 0xFE);
  2144. }
  2145. rc = ruv_get_largest_csn_for_replica(ruv, 0xAA, &newcsn);
  2146. printf("ruv_get_largest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  2147. rc = ruv_get_smallest_csn_for_replica(ruv, 0xAA, &newcsn);
  2148. printf("ruv_get_smallest_csn_for_replica on non-existent replica ID returns %d\n", rc);
  2149. newcsn = csn_new_by_string("12345674000000FE0000"); /* Old replica 0xFE */
  2150. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  2151. /* Should update replica FE's CSN */
  2152. ruv_dump_internal(ruv);
  2153. newcsn = csn_new_by_string("12345675000000FB0000"); /* New replica 0xFB */
  2154. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  2155. /* Should get a new replica in the list with min == max csn */
  2156. ruv_dump_internal(ruv);
  2157. newcsn = csn_new_by_string("12345676000000FD0000"); /* Old replica 0xFD */
  2158. rc = ruv_set_csns(ruv, newcsn, "ldaps://foobar.mcom.com");
  2159. /* Should update replica 0xFD so new CSN is newer than min CSN */
  2160. ruv_dump_internal(ruv);
  2161. gen = ruv_get_replica_generation(ruv);
  2162. printf("replica generation is \"%s\"\n", gen);
  2163. newcsn = csn_new_by_string("12345673000000FE0000"); /* Old replica 0xFE */
  2164. covers = ruv_covers_csn(ruv, newcsn); /* should say "true" */
  2165. newcsn = csn_new_by_string("12345675000000FE0000"); /* Old replica 0xFE */
  2166. covers = ruv_covers_csn(ruv, newcsn); /* Should say "false" */
  2167. newcsn = csn_new_by_string("123456700000000A0000"); /* New replica 0A */
  2168. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  2169. ruv_dump_internal(ruv);
  2170. newcsn = csn_new_by_string("123456710000000A0000"); /* New replica 0A */
  2171. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0a.mcom.com");
  2172. ruv_dump_internal(ruv);
  2173. newcsn = csn_new_by_string("123456700000000B0000"); /* New replica 0B */
  2174. rc = ruv_set_max_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  2175. ruv_dump_internal(ruv);
  2176. newcsn = csn_new_by_string("123456710000000B0000"); /* New replica 0B */
  2177. rc = ruv_set_min_csn(ruv, newcsn, "ldap://repl0b.mcom.com");
  2178. ruv_dump_internal(ruv);
  2179. /* ONREPL test ruv enumeration */
  2180. rc = ruv_to_smod(ruv, &smods);
  2181. ruv_destroy(&ruv);
  2182. }
  2183. #endif /* TESTING */