repl5_replica.c 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
  5. * All rights reserved.
  6. *
  7. * License: GPL (version 3 or any later version).
  8. * See LICENSE for details.
  9. * END COPYRIGHT BLOCK **/
  10. #ifdef HAVE_CONFIG_H
  11. # include <config.h>
  12. #endif
  13. /* repl5_replica.c */
  14. #include "slapi-plugin.h"
  15. #include "repl.h" /* ONREPL - this is bad */
  16. #include "repl5.h"
  17. #include "repl_shared.h"
  18. #include "csnpl.h"
  19. #include "cl5_api.h"
  20. #include "slap.h"
  21. #define RUV_SAVE_INTERVAL (30 * 1000) /* 30 seconds */
  22. #define REPLICA_RDN "cn=replica"
  23. #define CHANGELOG_RDN "cn=legacy changelog"
  24. /*
  25. * A replica is a locally-held copy of a portion of the DIT.
  26. */
  27. struct replica {
  28. Slapi_DN *repl_root; /* top of the replicated are */
  29. char *repl_name; /* unique replica name */
  30. PRBool new_name; /* new name was generated - need to be saved */
  31. ReplicaUpdateDNList updatedn_list; /* list of dns with which a supplier should bind
  32. to update this replica */
  33. Slapi_ValueSet *updatedn_groups; /* set of groups whose memebers are
  34. * allowed to update replica */
  35. ReplicaUpdateDNList groupdn_list; /* exploded listof dns from update group */
  36. PRUint32 updatedn_group_last_check;
  37. int updatedn_group_check_interval;
  38. ReplicaType repl_type; /* is this replica read-only ? */
  39. PRBool legacy_consumer; /* if true, this replica is supplied by 4.0 consumer */
  40. char* legacy_purl; /* partial url of the legacy supplier */
  41. ReplicaId repl_rid; /* replicaID */
  42. Object *repl_ruv; /* replica update vector */
  43. PRBool repl_ruv_dirty; /* Dirty flag for ruv */
  44. CSNPL *min_csn_pl; /* Pending list for minimal CSN */
  45. void *csn_pl_reg_id; /* registration assignment for csn callbacks */
  46. unsigned long repl_state_flags; /* state flags */
  47. PRUint32 repl_flags; /* persistent, externally visible flags */
  48. PRMonitor *repl_lock; /* protects entire structure */
  49. Slapi_Eq_Context repl_eqcxt_rs; /* context to cancel event that saves ruv */
  50. Slapi_Eq_Context repl_eqcxt_tr; /* context to cancel event that reaps tombstones */
  51. Object *repl_csngen; /* CSN generator for this replica */
  52. PRBool repl_csn_assigned; /* Flag set when new csn is assigned. */
  53. PRUint32 repl_purge_delay; /* When purgeable, CSNs are held on to for this many extra seconds */
  54. PRBool tombstone_reap_stop; /* TRUE when the tombstone reaper should stop */
  55. PRBool tombstone_reap_active; /* TRUE when the tombstone reaper is running */
  56. long tombstone_reap_interval; /* Time in seconds between tombstone reaping */
  57. Slapi_ValueSet *repl_referral; /* A list of administrator provided referral URLs */
  58. PRBool state_update_inprogress; /* replica state is being updated */
  59. PRLock *agmt_lock; /* protects agreement creation, start and stop */
  60. char *locking_purl; /* supplier who has exclusive access */
  61. Slapi_Counter *protocol_timeout;/* protocol shutdown timeout */
  62. Slapi_Counter *backoff_min; /* backoff retry minimum */
  63. Slapi_Counter *backoff_max; /* backoff retry maximum */
  64. Slapi_Counter *precise_purging; /* Enable precise tombstone purging */
  65. PRUint64 agmt_count; /* Number of agmts */
  66. };
  67. typedef struct reap_callback_data
  68. {
  69. int rc;
  70. unsigned long num_entries;
  71. unsigned long num_purged_entries;
  72. CSN *purge_csn;
  73. PRBool *tombstone_reap_stop;
  74. } reap_callback_data;
  75. /* Forward declarations of helper functions*/
  76. static Slapi_Entry* _replica_get_config_entry (const Slapi_DN *root, const char **attrs);
  77. static int _replica_check_validity (const Replica *r);
  78. static int _replica_init_from_config (Replica *r, Slapi_Entry *e, char *errortext);
  79. static int _replica_update_entry (Replica *r, Slapi_Entry *e, char *errortext);
  80. static int _replica_configure_ruv (Replica *r, PRBool isLocked);
  81. static char * _replica_get_config_dn (const Slapi_DN *root);
  82. static char * _replica_type_as_string (const Replica *r);
  83. /* DBDB, I think this is probably bogus : */
  84. static int replica_create_ruv_tombstone(Replica *r);
  85. static void assign_csn_callback(const CSN *csn, void *data);
  86. static void abort_csn_callback(const CSN *csn, void *data);
  87. static void eq_cb_reap_tombstones(time_t when, void *arg);
  88. static CSN *_replica_get_purge_csn_nolock (const Replica *r);
  89. static void replica_get_referrals_nolock (const Replica *r, char ***referrals);
  90. static void replica_clear_legacy_referrals (const Slapi_DN *repl_root_sdn, char **referrals, const char *state);
  91. static void replica_remove_legacy_attr (const Slapi_DN *repl_root_sdn, const char *attr);
  92. static int replica_log_ruv_elements_nolock (const Replica *r);
  93. static void replica_replace_ruv_tombstone(Replica *r);
  94. static void start_agreements_for_replica (Replica *r, PRBool start);
  95. static void _delete_tombstone(const char *tombstone_dn, const char *uniqueid, int ext_op_flags);
  96. static void replica_strip_cleaned_rids(Replica *r);
  97. static void
  98. replica_lock(PRMonitor *lock)
  99. {
  100. PR_EnterMonitor(lock);
  101. }
  102. static void
  103. replica_unlock(PRMonitor *lock)
  104. {
  105. PR_ExitMonitor(lock);
  106. }
  107. /*
  108. * Allocates new replica and reads its state and state of its component from
  109. * various parts of the DIT.
  110. */
  111. Replica *
  112. replica_new(const Slapi_DN *root)
  113. {
  114. Replica *r = NULL;
  115. Slapi_Entry *e = NULL;
  116. char errorbuf[SLAPI_DSE_RETURNTEXT_SIZE];
  117. PR_ASSERT (root);
  118. /* check if there is a replica associated with the tree */
  119. e = _replica_get_config_entry (root, NULL);
  120. if (e)
  121. {
  122. errorbuf[0] = '\0';
  123. r = replica_new_from_entry(e, errorbuf,
  124. PR_FALSE /* not a newly added entry */);
  125. if (NULL == r)
  126. {
  127. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Unable to "
  128. "configure replica %s: %s\n",
  129. slapi_sdn_get_dn(root),
  130. errorbuf);
  131. }
  132. slapi_entry_free (e);
  133. }
  134. return r;
  135. }
  136. /* constructs the replica object from the newly added entry */
  137. Replica *
  138. replica_new_from_entry (Slapi_Entry *e, char *errortext, PRBool is_add_operation)
  139. {
  140. int rc = 0;
  141. Replica *r;
  142. char *repl_name = NULL;
  143. if (e == NULL)
  144. {
  145. if (NULL != errortext)
  146. {
  147. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "NULL entry");
  148. }
  149. return NULL;
  150. }
  151. r = (Replica *)slapi_ch_calloc(1, sizeof(Replica));
  152. if (!r)
  153. {
  154. if (NULL != errortext)
  155. {
  156. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "Out of memory");
  157. }
  158. rc = -1;
  159. goto done;
  160. }
  161. if ((r->repl_lock = PR_NewMonitor()) == NULL)
  162. {
  163. if (NULL != errortext)
  164. {
  165. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to create replica lock");
  166. }
  167. rc = -1;
  168. goto done;
  169. }
  170. if ((r->agmt_lock = PR_NewLock()) == NULL)
  171. {
  172. if (NULL != errortext)
  173. {
  174. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to create replica lock");
  175. }
  176. rc = -1;
  177. goto done;
  178. }
  179. /* init the slapi_counter/atomic settings */
  180. r->protocol_timeout = slapi_counter_new();
  181. r->backoff_min = slapi_counter_new();
  182. r->backoff_max = slapi_counter_new();
  183. r->precise_purging = slapi_counter_new();
  184. /* read parameters from the replica config entry */
  185. rc = _replica_init_from_config (r, e, errortext);
  186. if (rc != 0)
  187. {
  188. goto done;
  189. }
  190. /* configure ruv */
  191. rc = _replica_configure_ruv (r, PR_FALSE);
  192. if (rc != 0)
  193. {
  194. goto done;
  195. }
  196. /* If smallest csn exists in RUV for our local replica, it's ok to begin iteration */
  197. PR_ASSERT (object_get_data (r->repl_ruv));
  198. if (is_add_operation)
  199. {
  200. /*
  201. * This is called by an ldap add operation.
  202. * Update the entry to contain information generated
  203. * during replica initialization
  204. */
  205. rc = _replica_update_entry (r, e, errortext);
  206. }
  207. else
  208. {
  209. /*
  210. * Entry is already in dse.ldif - update it on the disk
  211. * (done by the update state event scheduled below)
  212. */
  213. }
  214. if (rc != 0)
  215. goto done;
  216. /* ONREPL - the state update can occur before the entry is added to the DIT.
  217. In that case the updated would fail but nothing bad would happen. The next
  218. scheduled update would save the state */
  219. repl_name = slapi_ch_strdup (r->repl_name);
  220. r->repl_eqcxt_rs = slapi_eq_repeat(replica_update_state, repl_name,
  221. current_time () + START_UPDATE_DELAY, RUV_SAVE_INTERVAL);
  222. if (r->tombstone_reap_interval > 0)
  223. {
  224. /*
  225. * Reap Tombstone should be started some time after the plugin started.
  226. * This will allow the server to fully start before consuming resources.
  227. */
  228. repl_name = slapi_ch_strdup (r->repl_name);
  229. r->repl_eqcxt_tr = slapi_eq_repeat(eq_cb_reap_tombstones, repl_name,
  230. current_time() + r->tombstone_reap_interval,
  231. 1000 * r->tombstone_reap_interval);
  232. }
  233. if (r->legacy_consumer)
  234. {
  235. legacy_consumer_init_referrals (r);
  236. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_new_from_entry: "
  237. "replica for %s was configured as legacy consumer\n",
  238. slapi_sdn_get_dn(r->repl_root));
  239. }
  240. replica_check_for_tasks(r, e);
  241. done:
  242. if (rc != 0 && r)
  243. {
  244. replica_destroy ((void**)&r);
  245. }
  246. return r;
  247. }
  248. void
  249. replica_flush(Replica *r)
  250. {
  251. PR_ASSERT(NULL != r);
  252. if (NULL != r)
  253. {
  254. replica_lock(r->repl_lock);
  255. /* Make sure we dump the CSNGen state */
  256. r->repl_csn_assigned = PR_TRUE;
  257. replica_unlock(r->repl_lock);
  258. /* This function take the Lock Inside */
  259. /* And also write the RUV */
  260. replica_update_state((time_t)0, r->repl_name);
  261. }
  262. }
  263. void
  264. replica_set_csn_assigned(Replica *r)
  265. {
  266. replica_lock(r->repl_lock);
  267. r->repl_csn_assigned = PR_TRUE;
  268. replica_unlock(r->repl_lock);
  269. }
  270. /*
  271. * Deallocate a replica. arg should point to the address of a
  272. * pointer that points to a replica structure.
  273. */
  274. void
  275. replica_destroy(void **arg)
  276. {
  277. Replica *r;
  278. void *repl_name;
  279. if (arg == NULL)
  280. return;
  281. r = *((Replica **)arg);
  282. PR_ASSERT(r);
  283. slapi_log_error (SLAPI_LOG_REPL, NULL, "replica_destroy\n");
  284. /*
  285. * The function will not be called unless the refcnt of its
  286. * wrapper object is 0. Hopefully this refcnt could sync up
  287. * this destruction and the events such as tombstone reap
  288. * and ruv updates.
  289. */
  290. if (r->repl_eqcxt_rs)
  291. {
  292. repl_name = slapi_eq_get_arg (r->repl_eqcxt_rs);
  293. slapi_ch_free (&repl_name);
  294. slapi_eq_cancel(r->repl_eqcxt_rs);
  295. r->repl_eqcxt_rs = NULL;
  296. }
  297. if (r->repl_eqcxt_tr)
  298. {
  299. repl_name = slapi_eq_get_arg (r->repl_eqcxt_tr);
  300. slapi_ch_free (&repl_name);
  301. slapi_eq_cancel(r->repl_eqcxt_tr);
  302. r->repl_eqcxt_tr = NULL;
  303. }
  304. if (r->repl_root)
  305. {
  306. slapi_sdn_free(&r->repl_root);
  307. }
  308. slapi_ch_free_string(&r->locking_purl);
  309. if (r->updatedn_list)
  310. {
  311. replica_updatedn_list_free(r->updatedn_list);
  312. r->updatedn_list = NULL;
  313. }
  314. if (r->groupdn_list)
  315. {
  316. replica_updatedn_list_free(r->groupdn_list);
  317. r->groupdn_list = NULL;
  318. }
  319. if (r->updatedn_groups) {
  320. slapi_valueset_free(r->updatedn_groups);
  321. }
  322. /* slapi_ch_free accepts NULL pointer */
  323. slapi_ch_free ((void**)&r->repl_name);
  324. slapi_ch_free ((void**)&r->legacy_purl);
  325. if (r->repl_lock)
  326. {
  327. PR_DestroyMonitor(r->repl_lock);
  328. r->repl_lock = NULL;
  329. }
  330. if (r->agmt_lock)
  331. {
  332. PR_DestroyLock(r->agmt_lock);
  333. r->agmt_lock = NULL;
  334. }
  335. if(NULL != r->repl_ruv)
  336. {
  337. object_release(r->repl_ruv);
  338. }
  339. if(NULL != r->repl_csngen)
  340. {
  341. if (r->csn_pl_reg_id)
  342. {
  343. csngen_unregister_callbacks((CSNGen *)object_get_data (r->repl_csngen), r->csn_pl_reg_id);
  344. }
  345. object_release(r->repl_csngen);
  346. }
  347. if (NULL != r->repl_referral)
  348. {
  349. slapi_valueset_free(r->repl_referral);
  350. }
  351. if (NULL != r->min_csn_pl)
  352. {
  353. csnplFree(&r->min_csn_pl);;
  354. }
  355. slapi_counter_destroy(&r->protocol_timeout);
  356. slapi_counter_destroy(&r->backoff_min);
  357. slapi_counter_destroy(&r->backoff_max);
  358. slapi_counter_destroy(&r->precise_purging);
  359. slapi_ch_free((void **)arg);
  360. }
  361. #define KEEP_ALIVE_ATTR "keepalivetimestamp"
  362. #define KEEP_ALIVE_ENTRY "repl keep alive"
  363. #define KEEP_ALIVE_DN_FORMAT "cn=%s %d,%s"
  364. static int
  365. replica_subentry_create(Slapi_DN *repl_root, ReplicaId rid)
  366. {
  367. char *entry_string = NULL;
  368. Slapi_Entry *e = NULL;
  369. Slapi_PBlock *pb = NULL;
  370. int return_value;
  371. int rc = 0;
  372. entry_string = slapi_ch_smprintf("dn: cn=%s %d,%s\nobjectclass: top\nobjectclass: ldapsubentry\nobjectclass: extensibleObject\ncn: %s %d",
  373. KEEP_ALIVE_ENTRY, rid, slapi_sdn_get_dn(repl_root), KEEP_ALIVE_ENTRY, rid);
  374. if (entry_string == NULL) {
  375. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  376. "replica_subentry_create add failed in slapi_ch_smprintf\n");
  377. rc = -1;
  378. goto done;
  379. }
  380. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "add %s\n", entry_string);
  381. e = slapi_str2entry(entry_string, 0);
  382. /* create the entry */
  383. pb = slapi_pblock_new();
  384. slapi_add_entry_internal_set_pb(pb, e, NULL, /* controls */
  385. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0 /* flags */);
  386. slapi_add_internal_pb(pb);
  387. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
  388. if (return_value != LDAP_SUCCESS &&
  389. return_value != LDAP_ALREADY_EXISTS &&
  390. return_value != LDAP_REFERRAL /* CONSUMER */)
  391. {
  392. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Warning: unable to "
  393. "create replication keep alive entry %s: %s\n", slapi_entry_get_dn_const(e),
  394. ldap_err2string(return_value));
  395. rc = -1;
  396. goto done;
  397. }
  398. done:
  399. slapi_pblock_destroy(pb);
  400. slapi_ch_free_string(&entry_string);
  401. return rc;
  402. }
  403. int
  404. replica_subentry_check(Slapi_DN *repl_root, ReplicaId rid)
  405. {
  406. Slapi_PBlock *pb;
  407. char *filter = NULL;
  408. Slapi_Entry **entries = NULL;
  409. int res;
  410. int rc = 0;
  411. pb = slapi_pblock_new();
  412. filter = slapi_ch_smprintf("(&(objectclass=ldapsubentry)(cn=%s %d))", KEEP_ALIVE_ENTRY, rid);
  413. slapi_search_internal_set_pb(pb, slapi_sdn_get_dn(repl_root), LDAP_SCOPE_ONELEVEL,
  414. filter, NULL, 0, NULL, NULL,
  415. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  416. slapi_search_internal_pb(pb);
  417. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &res);
  418. if (res == LDAP_SUCCESS)
  419. {
  420. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  421. if (entries && (entries[0] == NULL))
  422. {
  423. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  424. "Need to create replication keep alive entry <cn=%s %d,%s>\n", KEEP_ALIVE_ENTRY, rid, slapi_sdn_get_dn(repl_root));
  425. rc = replica_subentry_create(repl_root, rid);
  426. } else {
  427. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  428. "replication keep alive entry <cn=%s %d,%s> already exists\n", KEEP_ALIVE_ENTRY, rid, slapi_sdn_get_dn(repl_root));
  429. rc = 0;
  430. }
  431. } else {
  432. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  433. "Error accessing replication keep alive entry <cn=%s %d,%s> res=%d\n",
  434. KEEP_ALIVE_ENTRY, rid, slapi_sdn_get_dn(repl_root), res);
  435. /* The status of the entry is not clear, do not attempt to create it */
  436. rc = 1;
  437. }
  438. slapi_free_search_results_internal(pb);
  439. slapi_pblock_destroy(pb);
  440. slapi_ch_free_string(&filter);
  441. return rc;
  442. }
  443. int
  444. replica_subentry_update(Slapi_DN *repl_root, ReplicaId rid)
  445. {
  446. int ldrc;
  447. int rc = LDAP_SUCCESS; /* Optimistic default */
  448. LDAPMod * mods[2];
  449. LDAPMod mod;
  450. struct berval * vals[2];
  451. char buf[20];
  452. time_t curtime;
  453. struct tm ltm;
  454. struct berval val;
  455. Slapi_PBlock *modpb = NULL;
  456. char *dn;
  457. replica_subentry_check(repl_root, rid);
  458. curtime = current_time();
  459. gmtime_r(&curtime, &ltm);
  460. strftime(buf, sizeof (buf), "%Y%m%d%H%M%SZ", &ltm);
  461. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "subentry_update called at %s\n", buf);
  462. val.bv_val = buf;
  463. val.bv_len = strlen(val.bv_val);
  464. vals [0] = &val;
  465. vals [1] = NULL;
  466. mod.mod_op = LDAP_MOD_REPLACE | LDAP_MOD_BVALUES;
  467. mod.mod_type = KEEP_ALIVE_ATTR;
  468. mod.mod_bvalues = vals;
  469. mods[0] = &mod;
  470. mods[1] = NULL;
  471. modpb = slapi_pblock_new();
  472. dn = slapi_ch_smprintf(KEEP_ALIVE_DN_FORMAT, KEEP_ALIVE_ENTRY, rid, slapi_sdn_get_dn(repl_root));
  473. slapi_modify_internal_set_pb(modpb, dn, mods, NULL, NULL,
  474. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION), 0);
  475. slapi_modify_internal_pb(modpb);
  476. slapi_pblock_get(modpb, SLAPI_PLUGIN_INTOP_RESULT, &ldrc);
  477. if (ldrc != LDAP_SUCCESS)
  478. {
  479. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  480. "Failure (%d) to update replication keep alive entry \"%s: %s\"\n", ldrc, KEEP_ALIVE_ATTR, buf);
  481. rc = ldrc;
  482. } else {
  483. slapi_log_error(SLAPI_LOG_PLUGIN, repl_plugin_name,
  484. "Successful update of replication keep alive entry \"%s: %s\"\n", KEEP_ALIVE_ATTR, buf);
  485. }
  486. slapi_pblock_destroy(modpb);
  487. slapi_ch_free_string(&dn);
  488. return rc;
  489. }
  490. /*
  491. * Attempt to obtain exclusive access to replica (advisory only)
  492. *
  493. * Returns PR_TRUE if exclusive access was granted,
  494. * PR_FALSE otherwise
  495. * The parameter isInc tells whether or not the replica is being
  496. * locked for an incremental update session - if the replica is
  497. * successfully locked, this value is used - if the replica is already
  498. * in use, this value will be set to TRUE or FALSE, depending on what
  499. * type of update session has the replica in use currently
  500. * locking_purl is the supplier who is attempting to acquire access
  501. * current_purl is the supplier who already has access, if any
  502. */
  503. PRBool
  504. replica_get_exclusive_access(Replica *r, PRBool *isInc, PRUint64 connid, int opid,
  505. const char *locking_purl,
  506. char **current_purl)
  507. {
  508. PRBool rval = PR_TRUE;
  509. PR_ASSERT(r);
  510. replica_lock(r->repl_lock);
  511. if (r->repl_state_flags & REPLICA_IN_USE)
  512. {
  513. if (isInc)
  514. *isInc = (r->repl_state_flags & REPLICA_INCREMENTAL_IN_PROGRESS);
  515. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  516. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  517. "Replica in use locking_purl=%s\n",
  518. connid, opid,
  519. slapi_sdn_get_dn(r->repl_root),
  520. r->locking_purl ? r->locking_purl : "unknown");
  521. rval = PR_FALSE;
  522. if (current_purl)
  523. {
  524. *current_purl = slapi_ch_strdup(r->locking_purl);
  525. }
  526. }
  527. else
  528. {
  529. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  530. "conn=%" NSPRIu64 " op=%d repl=\"%s\": Acquired replica\n",
  531. connid, opid,
  532. slapi_sdn_get_dn(r->repl_root));
  533. r->repl_state_flags |= REPLICA_IN_USE;
  534. if (isInc && *isInc)
  535. {
  536. r->repl_state_flags |= REPLICA_INCREMENTAL_IN_PROGRESS;
  537. }
  538. else
  539. {
  540. /* if connid or opid != 0, it's a total update */
  541. /* Both set to 0 means we're disabling replication */
  542. if (connid || opid)
  543. {
  544. r->repl_state_flags |= REPLICA_TOTAL_IN_PROGRESS;
  545. }
  546. }
  547. slapi_ch_free_string(&r->locking_purl);
  548. r->locking_purl = slapi_ch_strdup(locking_purl);
  549. }
  550. replica_unlock(r->repl_lock);
  551. return rval;
  552. }
  553. /*
  554. * Relinquish exclusive access to the replica
  555. */
  556. void
  557. replica_relinquish_exclusive_access(Replica *r, PRUint64 connid, int opid)
  558. {
  559. PRBool isInc;
  560. PR_ASSERT(r);
  561. replica_lock(r->repl_lock);
  562. isInc = (r->repl_state_flags & REPLICA_INCREMENTAL_IN_PROGRESS);
  563. /* check to see if the replica is in use and log a warning if not */
  564. if (!(r->repl_state_flags & REPLICA_IN_USE))
  565. {
  566. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  567. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  568. "Replica not in use\n",
  569. connid, opid,
  570. slapi_sdn_get_dn(r->repl_root));
  571. } else {
  572. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  573. "conn=%" NSPRIu64 " op=%d repl=\"%s\": "
  574. "Released replica held by locking_purl=%s\n",
  575. connid, opid,
  576. slapi_sdn_get_dn(r->repl_root),
  577. r->locking_purl);
  578. slapi_ch_free_string(&r->locking_purl);
  579. r->repl_state_flags &= ~(REPLICA_IN_USE);
  580. if (isInc)
  581. r->repl_state_flags &= ~(REPLICA_INCREMENTAL_IN_PROGRESS);
  582. else
  583. r->repl_state_flags &= ~(REPLICA_TOTAL_IN_PROGRESS);
  584. }
  585. replica_unlock(r->repl_lock);
  586. }
  587. /*
  588. * Returns root of the replicated area
  589. */
  590. PRBool
  591. replica_get_tombstone_reap_active(const Replica *r)
  592. {
  593. PR_ASSERT(r);
  594. return(r->tombstone_reap_active);
  595. }
  596. /*
  597. * Returns root of the replicated area
  598. */
  599. const Slapi_DN *
  600. replica_get_root(const Replica *r) /* ONREPL - should we return copy instead? */
  601. {
  602. PR_ASSERT(r);
  603. /* replica root never changes so we don't have to lock */
  604. return(r->repl_root);
  605. }
  606. /*
  607. * Returns normalized dn of the root of the replicated area
  608. */
  609. const char *
  610. replica_get_name(const Replica *r) /* ONREPL - should we return copy instead? */
  611. {
  612. PR_ASSERT(r);
  613. /* replica name never changes so we don't have to lock */
  614. return(r->repl_name);
  615. }
  616. /*
  617. * Returns replicaid of this replica
  618. */
  619. ReplicaId
  620. replica_get_rid (const Replica *r)
  621. {
  622. ReplicaId rid;
  623. PR_ASSERT(r);
  624. replica_lock(r->repl_lock);
  625. rid = r->repl_rid;
  626. replica_unlock(r->repl_lock);
  627. return rid;
  628. }
  629. /*
  630. * Sets replicaid of this replica - should only be used when also changing the type
  631. */
  632. void
  633. replica_set_rid (Replica *r, ReplicaId rid)
  634. {
  635. PR_ASSERT(r);
  636. replica_lock(r->repl_lock);
  637. r->repl_rid = rid;
  638. replica_unlock(r->repl_lock);
  639. }
  640. /* Returns true if replica was initialized through ORC or import;
  641. * otherwise, false. An uninitialized replica should return
  642. * LDAP_UNWILLING_TO_PERFORM to all client requests
  643. */
  644. PRBool
  645. replica_is_initialized (const Replica *r)
  646. {
  647. PR_ASSERT(r);
  648. return (r->repl_ruv != NULL);
  649. }
  650. /*
  651. * Returns refcounted object that contains RUV. The caller should release the
  652. * object once it is no longer used. To release, call object_release
  653. */
  654. Object *
  655. replica_get_ruv (const Replica *r)
  656. {
  657. Object *ruv = NULL;
  658. PR_ASSERT(r);
  659. replica_lock(r->repl_lock);
  660. PR_ASSERT (r->repl_ruv);
  661. object_acquire (r->repl_ruv);
  662. ruv = r->repl_ruv;
  663. replica_unlock(r->repl_lock);
  664. return ruv;
  665. }
  666. /*
  667. * Sets RUV vector. This function should be called during replica
  668. * (re)initialization. During normal operation, the RUV is read from
  669. * the root of the replicated in the replica_new call
  670. */
  671. void
  672. replica_set_ruv (Replica *r, RUV *ruv)
  673. {
  674. PR_ASSERT(r && ruv);
  675. replica_lock(r->repl_lock);
  676. if(NULL != r->repl_ruv)
  677. {
  678. object_release(r->repl_ruv);
  679. }
  680. /* if the local replica is not in the RUV and it is writable - add it
  681. and reinitialize min_csn pending list */
  682. if (r->repl_type == REPLICA_TYPE_UPDATABLE)
  683. {
  684. CSN *csn = NULL;
  685. if (r->min_csn_pl)
  686. csnplFree (&r->min_csn_pl);
  687. if (ruv_contains_replica (ruv, r->repl_rid))
  688. {
  689. ruv_get_smallest_csn_for_replica(ruv, r->repl_rid, &csn);
  690. if (csn)
  691. csn_free (&csn);
  692. else
  693. r->min_csn_pl = csnplNew ();
  694. /* We need to make sure the local ruv element is the 1st. */
  695. ruv_move_local_supplier_to_first(ruv, r->repl_rid);
  696. }
  697. else
  698. {
  699. r->min_csn_pl = csnplNew ();
  700. /* To be sure that the local is in first */
  701. ruv_add_index_replica(ruv, r->repl_rid, multimaster_get_local_purl(), 1);
  702. }
  703. }
  704. r->repl_ruv = object_new((void*)ruv, (FNFree)ruv_destroy);
  705. r->repl_ruv_dirty = PR_TRUE;
  706. replica_unlock(r->repl_lock);
  707. }
  708. /*
  709. * Update one particular CSN in an RUV. This is meant to be called
  710. * whenever (a) the server has processed a client operation and
  711. * needs to update its CSN, or (b) the server is completing an
  712. * inbound replication session operation, and needs to update its
  713. * local RUV.
  714. */
  715. int
  716. replica_update_ruv(Replica *r, const CSN *updated_csn, const char *replica_purl)
  717. {
  718. char csn_str[CSN_STRSIZE];
  719. int rc = RUV_SUCCESS;
  720. PR_ASSERT(NULL != r);
  721. PR_ASSERT(NULL != updated_csn);
  722. #ifdef DEBUG
  723. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  724. "replica_update_ruv: csn %s\n",
  725. csn_as_string(updated_csn, PR_FALSE, csn_str)); /* XXXggood remove debugging */
  726. #endif
  727. if (NULL == r)
  728. {
  729. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_update_ruv: replica "
  730. "is NULL\n");
  731. rc = RUV_BAD_DATA;
  732. }
  733. else if (NULL == updated_csn)
  734. {
  735. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_update_ruv: csn "
  736. "is NULL when updating replica %s\n", slapi_sdn_get_dn(r->repl_root));
  737. rc = RUV_BAD_DATA;
  738. }
  739. else
  740. {
  741. RUV *ruv;
  742. replica_lock(r->repl_lock);
  743. if (r->repl_ruv != NULL)
  744. {
  745. ruv = object_get_data(r->repl_ruv);
  746. if (NULL != ruv)
  747. {
  748. ReplicaId rid = csn_get_replicaid(updated_csn);
  749. if (rid == r->repl_rid)
  750. {
  751. if (NULL != r->min_csn_pl)
  752. {
  753. CSN *min_csn;
  754. PRBool committed;
  755. (void)csnplCommit(r->min_csn_pl, updated_csn);
  756. min_csn = csnplGetMinCSN(r->min_csn_pl, &committed);
  757. if (NULL != min_csn)
  758. {
  759. if (committed)
  760. {
  761. ruv_set_min_csn(ruv, min_csn, replica_purl);
  762. csnplFree(&r->min_csn_pl);
  763. }
  764. csn_free(&min_csn);
  765. }
  766. }
  767. }
  768. /* Update max csn for local and remote replicas */
  769. rc = ruv_update_ruv (ruv, updated_csn, replica_purl, rid == r->repl_rid);
  770. if (RUV_COVERS_CSN == rc)
  771. {
  772. slapi_log_error(SLAPI_LOG_REPL,
  773. repl_plugin_name, "replica_update_ruv: RUV "
  774. "for replica %s already covers max_csn = %s\n",
  775. slapi_sdn_get_dn(r->repl_root),
  776. csn_as_string(updated_csn, PR_FALSE, csn_str));
  777. /* RUV is not dirty - no write needed */
  778. }
  779. else if (RUV_SUCCESS != rc)
  780. {
  781. slapi_log_error(SLAPI_LOG_FATAL,
  782. repl_plugin_name, "replica_update_ruv: unable "
  783. "to update RUV for replica %s, csn = %s\n",
  784. slapi_sdn_get_dn(r->repl_root),
  785. csn_as_string(updated_csn, PR_FALSE, csn_str));
  786. }
  787. else
  788. {
  789. /* RUV updated - mark as dirty */
  790. r->repl_ruv_dirty = PR_TRUE;
  791. }
  792. }
  793. else
  794. {
  795. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  796. "replica_update_ruv: unable to get RUV object for replica "
  797. "%s\n", slapi_sdn_get_dn(r->repl_root));
  798. rc = RUV_NOTFOUND;
  799. }
  800. }
  801. else
  802. {
  803. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_update_ruv: "
  804. "unable to initialize RUV for replica %s\n",
  805. slapi_sdn_get_dn(r->repl_root));
  806. rc = RUV_NOTFOUND;
  807. }
  808. replica_unlock(r->repl_lock);
  809. }
  810. return rc;
  811. }
  812. /*
  813. * Returns refcounted object that contains csn generator. The caller should release the
  814. * object once it is no longer used. To release, call object_release
  815. */
  816. Object *
  817. replica_get_csngen (const Replica *r)
  818. {
  819. Object *csngen;
  820. PR_ASSERT(r);
  821. replica_lock(r->repl_lock);
  822. object_acquire (r->repl_csngen);
  823. csngen = r->repl_csngen;
  824. replica_unlock(r->repl_lock);
  825. return csngen;
  826. }
  827. /*
  828. * Returns the replica type.
  829. */
  830. ReplicaType
  831. replica_get_type (const Replica *r)
  832. {
  833. PR_ASSERT(r);
  834. return r->repl_type;
  835. }
  836. PRUint64
  837. replica_get_protocol_timeout(Replica *r)
  838. {
  839. if(r){
  840. return slapi_counter_get_value(r->protocol_timeout);
  841. } else {
  842. return 0;
  843. }
  844. }
  845. void
  846. replica_set_protocol_timeout(Replica *r, PRUint64 timeout)
  847. {
  848. if(r){
  849. slapi_counter_set_value(r->protocol_timeout, timeout);
  850. }
  851. }
  852. void
  853. replica_set_groupdn_checkinterval(Replica *r, int interval)
  854. {
  855. if(r){
  856. r->updatedn_group_check_interval = interval ;
  857. }
  858. }
  859. /*
  860. * Sets the replica type.
  861. */
  862. void
  863. replica_set_type (Replica *r, ReplicaType type)
  864. {
  865. PR_ASSERT(r);
  866. replica_lock(r->repl_lock);
  867. r->repl_type = type;
  868. replica_unlock(r->repl_lock);
  869. }
  870. /*
  871. * Returns PR_TRUE if this replica is a consumer of 4.0 server
  872. * and PR_FALSE otherwise
  873. */
  874. PRBool
  875. replica_is_legacy_consumer (const Replica *r)
  876. {
  877. PR_ASSERT(r);
  878. return r->legacy_consumer;
  879. }
  880. /*
  881. * Sets the replica type.
  882. */
  883. void
  884. replica_set_legacy_consumer (Replica *r, PRBool legacy_consumer)
  885. {
  886. int legacy2mmr;
  887. Slapi_DN *repl_root_sdn = NULL;
  888. char **referrals = NULL;
  889. char *replstate = NULL;
  890. PR_ASSERT(r);
  891. replica_lock(r->repl_lock);
  892. legacy2mmr = r->legacy_consumer && !legacy_consumer;
  893. /* making the server a regular 5.0 replica */
  894. if (legacy2mmr)
  895. {
  896. slapi_ch_free ((void**)&r->legacy_purl);
  897. /* Remove copiedFrom/copyingFrom attributes from the root entry */
  898. /* set the right state in the mapping tree */
  899. if (r->repl_type == REPLICA_TYPE_READONLY)
  900. {
  901. replica_get_referrals_nolock (r, &referrals);
  902. replstate = STATE_UPDATE_REFERRAL;
  903. }
  904. else /* updateable */
  905. {
  906. replstate = STATE_BACKEND;
  907. }
  908. }
  909. r->legacy_consumer = legacy_consumer;
  910. repl_root_sdn = slapi_sdn_dup(r->repl_root);
  911. replica_unlock(r->repl_lock);
  912. if (legacy2mmr)
  913. {
  914. replica_clear_legacy_referrals(repl_root_sdn, referrals, replstate);
  915. /* Also change state of the mapping tree node and/or referrals */
  916. replica_remove_legacy_attr (repl_root_sdn, type_copiedFrom);
  917. replica_remove_legacy_attr (repl_root_sdn, type_copyingFrom);
  918. }
  919. charray_free(referrals);
  920. slapi_sdn_free(&repl_root_sdn);
  921. }
  922. /* Gets partial url of the legacy supplier - applicable for legacy consumer only */
  923. char *
  924. replica_get_legacy_purl (const Replica *r)
  925. {
  926. char *purl;
  927. replica_lock(r->repl_lock);
  928. PR_ASSERT (r->legacy_consumer);
  929. purl = slapi_ch_strdup(r->legacy_purl);
  930. replica_unlock(r->repl_lock);
  931. return purl;
  932. }
  933. void
  934. replica_set_legacy_purl (Replica *r, const char *purl)
  935. {
  936. replica_lock(r->repl_lock);
  937. PR_ASSERT (r->legacy_consumer);
  938. /* slapi_ch_free accepts NULL pointer */
  939. slapi_ch_free ((void**)&r->legacy_purl);
  940. r->legacy_purl = slapi_ch_strdup (purl);
  941. replica_unlock(r->repl_lock);
  942. }
  943. static PRBool
  944. valuesets_equal(Slapi_ValueSet *new_dn_groups, Slapi_ValueSet *old_dn_groups)
  945. {
  946. Slapi_Attr *attr = NULL;
  947. Slapi_Value *val = NULL;
  948. int idx = 0;
  949. PRBool rc = PR_TRUE;
  950. if (new_dn_groups == NULL) {
  951. if (old_dn_groups == NULL)
  952. return PR_TRUE;
  953. else
  954. return PR_FALSE;
  955. }
  956. if (old_dn_groups == NULL) {
  957. return PR_FALSE;
  958. }
  959. /* if there is not the same number of value, no need to check the value themselves */
  960. if (new_dn_groups->num != old_dn_groups->num) {
  961. return PR_FALSE;
  962. }
  963. attr = slapi_attr_new();
  964. slapi_attr_init(attr, attr_replicaBindDnGroup);
  965. /* Check that all values in old_dn_groups also exist in new_dn_groups */
  966. for (idx = slapi_valueset_first_value(old_dn_groups, &val);
  967. val && (idx != -1);
  968. idx = slapi_valueset_next_value(old_dn_groups, idx, &val)) {
  969. if (slapi_valueset_find(attr, new_dn_groups, val) == NULL) {
  970. rc = PR_FALSE;
  971. break;
  972. }
  973. }
  974. slapi_attr_free(&attr);
  975. return rc;
  976. }
  977. /*
  978. * Returns true if sdn is the same as updatedn and false otherwise
  979. */
  980. PRBool
  981. replica_is_updatedn (Replica *r, const Slapi_DN *sdn)
  982. {
  983. PRBool result = PR_FALSE;
  984. PR_ASSERT(r);
  985. replica_lock(r->repl_lock);
  986. if ((r->updatedn_list == NULL) && (r->groupdn_list == NULL)) {
  987. if (sdn == NULL) {
  988. result = PR_TRUE;
  989. } else {
  990. result = PR_FALSE;
  991. }
  992. replica_unlock(r->repl_lock);
  993. return result;
  994. }
  995. if (r->updatedn_list) {
  996. result = replica_updatedn_list_ismember(r->updatedn_list, sdn);
  997. if (result == PR_TRUE) {
  998. /* sdn is present in the updatedn_list */
  999. replica_unlock(r->repl_lock);
  1000. return result;
  1001. }
  1002. }
  1003. if (r->groupdn_list) {
  1004. /* check and rebuild groupdns */
  1005. if (r->updatedn_group_check_interval > -1) {
  1006. time_t now = time(NULL);
  1007. if (now - r->updatedn_group_last_check > r->updatedn_group_check_interval) {
  1008. Slapi_ValueSet *updatedn_groups_copy = NULL;
  1009. ReplicaUpdateDNList groupdn_list = replica_updatedn_list_new(NULL);
  1010. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "Authorized replication managers is resync (%ld)\n", now);
  1011. updatedn_groups_copy = slapi_valueset_new();
  1012. slapi_valueset_set_valueset(updatedn_groups_copy, r->updatedn_groups);
  1013. r->updatedn_group_last_check = now; /* Just to be sure no one will try to reload */
  1014. /* It can do internal searches, to avoid deadlock release the replica lock
  1015. * as we are working on local variables
  1016. */
  1017. replica_unlock(r->repl_lock);
  1018. replica_updatedn_list_group_replace(groupdn_list, updatedn_groups_copy);
  1019. replica_lock(r->repl_lock);
  1020. if (valuesets_equal(r->updatedn_groups, updatedn_groups_copy)) {
  1021. /* the updatedn_groups has not been updated while we release the replica
  1022. * this is fine to apply the groupdn_list
  1023. */
  1024. replica_updatedn_list_delete(r->groupdn_list, NULL);
  1025. replica_updatedn_list_free(r->groupdn_list);
  1026. r->groupdn_list = groupdn_list;
  1027. } else {
  1028. /* the unpdatedn_groups has been updated while we released the replica
  1029. * groupdn_list in the replica is up to date. Do not replace it
  1030. */
  1031. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "Authorized replication managers (%s) was updated during a refresh\n", attr_replicaBindDnGroup);
  1032. replica_updatedn_list_delete(groupdn_list, NULL);
  1033. replica_updatedn_list_free(groupdn_list);
  1034. }
  1035. slapi_valueset_free(updatedn_groups_copy);
  1036. }
  1037. }
  1038. result = replica_updatedn_list_ismember(r->groupdn_list, sdn);
  1039. }
  1040. replica_unlock(r->repl_lock);
  1041. return result;
  1042. }
  1043. /*
  1044. * Sets updatedn list for this replica
  1045. */
  1046. void
  1047. replica_set_updatedn (Replica *r, const Slapi_ValueSet *vs, int mod_op)
  1048. {
  1049. PR_ASSERT (r);
  1050. replica_lock(r->repl_lock);
  1051. if (!r->updatedn_list)
  1052. r->updatedn_list = replica_updatedn_list_new(NULL);
  1053. if (SLAPI_IS_MOD_DELETE(mod_op) || vs == NULL ||
  1054. (0 == slapi_valueset_count(vs))) /* null value also causes list deletion */
  1055. replica_updatedn_list_delete(r->updatedn_list, vs);
  1056. else if (SLAPI_IS_MOD_REPLACE(mod_op))
  1057. replica_updatedn_list_replace(r->updatedn_list, vs);
  1058. else if (SLAPI_IS_MOD_ADD(mod_op))
  1059. replica_updatedn_list_add(r->updatedn_list, vs);
  1060. replica_unlock(r->repl_lock);
  1061. }
  1062. /*
  1063. * Sets updatedn list for this replica
  1064. */
  1065. void
  1066. replica_set_groupdn (Replica *r, const Slapi_ValueSet *vs, int mod_op)
  1067. {
  1068. PR_ASSERT (r);
  1069. replica_lock(r->repl_lock);
  1070. if (!r->groupdn_list)
  1071. r->groupdn_list = replica_updatedn_list_new(NULL);
  1072. if (!r->updatedn_groups)
  1073. r->updatedn_groups = slapi_valueset_new();
  1074. if (SLAPI_IS_MOD_DELETE(mod_op) || vs == NULL ||
  1075. (0 == slapi_valueset_count(vs))) {
  1076. /* null value also causes list deletion */
  1077. slapi_valueset_free (r->updatedn_groups);
  1078. r->updatedn_groups = NULL;
  1079. replica_updatedn_list_delete(r->groupdn_list, vs);
  1080. } else if (SLAPI_IS_MOD_REPLACE(mod_op)) {
  1081. if (r->updatedn_groups) {
  1082. slapi_valueset_done(r->updatedn_groups);
  1083. } else {
  1084. r->updatedn_groups = slapi_valueset_new();
  1085. }
  1086. slapi_valueset_set_valueset(r->updatedn_groups, vs);
  1087. replica_updatedn_list_group_replace(r->groupdn_list, vs);
  1088. } else if (SLAPI_IS_MOD_ADD(mod_op)) {
  1089. if (!r->updatedn_groups) {
  1090. r->updatedn_groups = slapi_valueset_new();
  1091. }
  1092. slapi_valueset_join_attr_valueset(NULL, r->updatedn_groups, vs);
  1093. replica_updatedn_list_add_ext(r->groupdn_list, vs, 1);
  1094. }
  1095. replica_unlock(r->repl_lock);
  1096. }
  1097. void
  1098. replica_reset_csn_pl(Replica *r)
  1099. {
  1100. replica_lock(r->repl_lock);
  1101. if (NULL != r->min_csn_pl){
  1102. csnplFree (&r->min_csn_pl);
  1103. }
  1104. r->min_csn_pl = csnplNew();
  1105. replica_unlock(r->repl_lock);
  1106. }
  1107. /* gets current replica generation for this replica */
  1108. char *replica_get_generation (const Replica *r)
  1109. {
  1110. int rc = 0;
  1111. char *gen = NULL;
  1112. if (r && r->repl_ruv)
  1113. {
  1114. replica_lock(r->repl_lock);
  1115. if (rc == 0)
  1116. gen = ruv_get_replica_generation ((RUV*)object_get_data (r->repl_ruv));
  1117. replica_unlock(r->repl_lock);
  1118. }
  1119. return gen;
  1120. }
  1121. PRBool replica_is_flag_set (const Replica *r, PRUint32 flag)
  1122. {
  1123. if (r)
  1124. return (r->repl_flags & flag);
  1125. else
  1126. return PR_FALSE;
  1127. }
  1128. void replica_set_flag (Replica *r, PRUint32 flag, PRBool clear)
  1129. {
  1130. if (r == NULL)
  1131. return;
  1132. replica_lock(r->repl_lock);
  1133. if (clear)
  1134. {
  1135. r->repl_flags &= ~flag;
  1136. }
  1137. else
  1138. {
  1139. r->repl_flags |= flag;
  1140. }
  1141. replica_unlock(r->repl_lock);
  1142. }
  1143. void replica_replace_flags (Replica *r, PRUint32 flags)
  1144. {
  1145. if (r)
  1146. {
  1147. replica_lock(r->repl_lock);
  1148. r->repl_flags = flags;
  1149. replica_unlock(r->repl_lock);
  1150. }
  1151. }
  1152. void
  1153. replica_get_referrals(const Replica *r, char ***referrals)
  1154. {
  1155. replica_lock(r->repl_lock);
  1156. replica_get_referrals_nolock (r, referrals);
  1157. replica_unlock(r->repl_lock);
  1158. }
  1159. void
  1160. replica_set_referrals(Replica *r,const Slapi_ValueSet *vs)
  1161. {
  1162. int ii = 0;
  1163. Slapi_Value *vv = NULL;
  1164. if (r->repl_referral == NULL)
  1165. {
  1166. r->repl_referral = slapi_valueset_new();
  1167. }
  1168. else
  1169. {
  1170. slapi_valueset_done(r->repl_referral);
  1171. }
  1172. slapi_valueset_set_valueset(r->repl_referral, vs);
  1173. /* make sure the DN is included in the referral LDAP URL */
  1174. if (r->repl_referral)
  1175. {
  1176. Slapi_ValueSet *newvs = slapi_valueset_new();
  1177. const char *repl_root = slapi_sdn_get_dn(r->repl_root);
  1178. ii = slapi_valueset_first_value(r->repl_referral, &vv);
  1179. while (vv)
  1180. {
  1181. const char *ref = slapi_value_get_string(vv);
  1182. LDAPURLDesc *lud = NULL;
  1183. (void)slapi_ldap_url_parse(ref, &lud, 0, NULL);
  1184. /* see if the dn is already in the referral URL */
  1185. if (!lud || !lud->lud_dn) {
  1186. /* add the dn */
  1187. Slapi_Value *newval = NULL;
  1188. int len = strlen(ref);
  1189. char *tmpref = NULL;
  1190. int need_slash = 0;
  1191. if (ref[len-1] != '/') {
  1192. need_slash = 1;
  1193. }
  1194. tmpref = slapi_ch_smprintf("%s%s%s", ref, (need_slash ? "/" : ""),
  1195. repl_root);
  1196. newval = slapi_value_new_string(tmpref);
  1197. slapi_ch_free_string(&tmpref); /* sv_new_string makes a copy */
  1198. slapi_valueset_add_value(newvs, newval);
  1199. slapi_value_free(&newval); /* s_vs_add_value makes a copy */
  1200. }
  1201. if (lud)
  1202. ldap_free_urldesc(lud);
  1203. ii = slapi_valueset_next_value(r->repl_referral, ii, &vv);
  1204. }
  1205. if (slapi_valueset_count(newvs) > 0) {
  1206. slapi_valueset_done(r->repl_referral);
  1207. slapi_valueset_set_valueset(r->repl_referral, newvs);
  1208. }
  1209. slapi_valueset_free(newvs); /* s_vs_set_vs makes a copy */
  1210. }
  1211. }
  1212. int
  1213. replica_update_csngen_state_ext (Replica *r, const RUV *ruv, const CSN *extracsn)
  1214. {
  1215. int rc = 0;
  1216. CSNGen *gen;
  1217. CSN *csn = NULL;
  1218. PR_ASSERT (r && ruv);
  1219. rc = ruv_get_max_csn(ruv, &csn);
  1220. if (rc != RUV_SUCCESS)
  1221. {
  1222. return -1;
  1223. }
  1224. if ((csn == NULL) && (extracsn == NULL)) /* ruv contains no csn and no extra - we are done */
  1225. {
  1226. return 0;
  1227. }
  1228. if (csn_compare(extracsn, csn) > 0) /* extracsn > csn */
  1229. {
  1230. csn_free (&csn); /* free */
  1231. csn = (CSN*)extracsn; /* use this csn to do the update */
  1232. }
  1233. replica_lock(r->repl_lock);
  1234. gen = (CSNGen *)object_get_data (r->repl_csngen);
  1235. PR_ASSERT (gen);
  1236. rc = csngen_adjust_time (gen, csn);
  1237. /* rc will be either CSN_SUCCESS (0) or clock skew */
  1238. /* done: */
  1239. replica_unlock(r->repl_lock);
  1240. if (csn != extracsn) /* do not free the given csn */
  1241. {
  1242. csn_free (&csn);
  1243. }
  1244. return rc;
  1245. }
  1246. int
  1247. replica_update_csngen_state (Replica *r, const RUV *ruv)
  1248. {
  1249. return replica_update_csngen_state_ext(r, ruv, NULL);
  1250. }
  1251. /*
  1252. * dumps replica state for debugging purpose
  1253. */
  1254. void
  1255. replica_dump(Replica *r)
  1256. {
  1257. char *updatedn_list = NULL;
  1258. PR_ASSERT (r);
  1259. replica_lock(r->repl_lock);
  1260. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "Replica state:\n");
  1261. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\treplica root: %s\n",
  1262. slapi_sdn_get_ndn (r->repl_root));
  1263. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\treplica type: %s\n",
  1264. _replica_type_as_string (r));
  1265. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\treplica id: %d\n", r->repl_rid);
  1266. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\tflags: %d\n", r->repl_flags);
  1267. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\tstate flags: %lu\n", r->repl_state_flags);
  1268. if (r->updatedn_list)
  1269. updatedn_list = replica_updatedn_list_to_string(r->updatedn_list, "\n\t\t");
  1270. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\tupdate dn: %s\n",
  1271. updatedn_list? updatedn_list : "not configured");
  1272. slapi_ch_free_string(&updatedn_list);
  1273. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\truv: %s configured and is %sdirty\n",
  1274. r->repl_ruv ? "" : "not", r->repl_ruv_dirty ? "" : "not ");
  1275. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "\tCSN generator: %s configured\n",
  1276. r->repl_csngen ? "" : "not");
  1277. /* JCMREPL - Dump Referrals */
  1278. replica_unlock(r->repl_lock);
  1279. }
  1280. /*
  1281. * Return the CSN of the purge point. Any CSNs smaller than the
  1282. * purge point can be safely removed from entries within this
  1283. * this replica. Returns an allocated CSN that must be freed by
  1284. * the caller, or NULL if purging is disabled.
  1285. */
  1286. CSN *
  1287. replica_get_purge_csn(const Replica *r)
  1288. {
  1289. CSN *csn;
  1290. replica_lock(r->repl_lock);
  1291. csn= _replica_get_purge_csn_nolock(r);
  1292. replica_unlock(r->repl_lock);
  1293. return csn;
  1294. }
  1295. /*
  1296. * This function logs a dummy entry for the smallest csn in the RUV.
  1297. * This is necessary because, to get the next change, we need to position
  1298. * changelog on the previous change. So this function insures that we always have one.
  1299. */
  1300. /* ONREPL we will need to change this function to log all the
  1301. * ruv elements not just the smallest when changelog iteration
  1302. * algoritm changes to iterate replica by replica
  1303. */
  1304. int
  1305. replica_log_ruv_elements (const Replica *r)
  1306. {
  1307. int rc = 0;
  1308. PR_ASSERT (r);
  1309. replica_lock(r->repl_lock);
  1310. rc = replica_log_ruv_elements_nolock (r);
  1311. replica_unlock(r->repl_lock);
  1312. return rc;
  1313. }
  1314. void
  1315. consumer5_set_mapping_tree_state_for_replica(const Replica *r, RUV *supplierRuv)
  1316. {
  1317. const Slapi_DN *repl_root_sdn= replica_get_root(r);
  1318. char **ruv_referrals= NULL;
  1319. char **replica_referrals= NULL;
  1320. RUV *ruv;
  1321. int state_backend = -1;
  1322. const char *mtn_state = NULL;
  1323. replica_lock(r->repl_lock);
  1324. if ( supplierRuv == NULL )
  1325. {
  1326. ruv = (RUV*)object_get_data (r->repl_ruv);
  1327. PR_ASSERT (ruv);
  1328. ruv_referrals= ruv_get_referrals(ruv); /* ruv_referrals has to be free'd */
  1329. }
  1330. else
  1331. {
  1332. ruv_referrals = ruv_get_referrals(supplierRuv);
  1333. }
  1334. replica_get_referrals_nolock (r, &replica_referrals); /* replica_referrals has to be free'd */
  1335. /* JCMREPL - What if there's a Total update in progress? */
  1336. if( (r->repl_type==REPLICA_TYPE_READONLY) || (r->legacy_consumer) )
  1337. {
  1338. state_backend = 0;
  1339. }
  1340. else if (r->repl_type==REPLICA_TYPE_UPDATABLE)
  1341. {
  1342. state_backend = 1;
  1343. }
  1344. /* Unlock to avoid changing MTN state under repl lock */
  1345. replica_unlock(r->repl_lock);
  1346. if(state_backend == 0 )
  1347. {
  1348. /* Read-Only - The mapping tree should be refering all update operations. */
  1349. mtn_state = STATE_UPDATE_REFERRAL;
  1350. }
  1351. else if (state_backend == 1)
  1352. {
  1353. /* Updatable - The mapping tree should be accepting all update operations. */
  1354. mtn_state = STATE_BACKEND;
  1355. }
  1356. /* JCMREPL - Check the return code. */
  1357. repl_set_mtn_state_and_referrals(repl_root_sdn, mtn_state, NULL,
  1358. ruv_referrals, replica_referrals);
  1359. charray_free(ruv_referrals);
  1360. charray_free(replica_referrals);
  1361. }
  1362. void
  1363. replica_set_enabled (Replica *r, PRBool enable)
  1364. {
  1365. char *repl_name = NULL;
  1366. PR_ASSERT (r);
  1367. replica_lock(r->repl_lock);
  1368. if (enable)
  1369. {
  1370. if (r->repl_eqcxt_rs == NULL) /* event is not already registered */
  1371. {
  1372. repl_name = slapi_ch_strdup (r->repl_name);
  1373. r->repl_eqcxt_rs = slapi_eq_repeat(replica_update_state, repl_name,
  1374. current_time() + START_UPDATE_DELAY, RUV_SAVE_INTERVAL);
  1375. }
  1376. }
  1377. else /* disable */
  1378. {
  1379. if (r->repl_eqcxt_rs) /* event is still registerd */
  1380. {
  1381. repl_name = slapi_eq_get_arg (r->repl_eqcxt_rs);
  1382. slapi_ch_free ((void**)&repl_name);
  1383. slapi_eq_cancel(r->repl_eqcxt_rs);
  1384. r->repl_eqcxt_rs = NULL;
  1385. }
  1386. }
  1387. replica_unlock(r->repl_lock);
  1388. }
  1389. /* This function is generally called when replica's data store
  1390. is reloaded. It retrieves new RUV from the datastore. If new
  1391. RUV does not exist or if it is not as up to date as the purge RUV
  1392. of the corresponding changelog file, we need to remove */
  1393. /* the function minimizes the use of replica lock where ever possible.
  1394. Locking replica lock while calling changelog functions
  1395. causes a deadlock because changelog calls replica functions that
  1396. that lock the same lock */
  1397. int
  1398. replica_reload_ruv (Replica *r)
  1399. {
  1400. int rc = 0;
  1401. Object *old_ruv_obj = NULL, *new_ruv_obj = NULL;
  1402. RUV *upper_bound_ruv = NULL;
  1403. RUV *new_ruv = NULL;
  1404. Object *r_obj;
  1405. PR_ASSERT (r);
  1406. replica_lock(r->repl_lock);
  1407. old_ruv_obj = r->repl_ruv;
  1408. r->repl_ruv = NULL;
  1409. rc = _replica_configure_ruv (r, PR_TRUE);
  1410. replica_unlock(r->repl_lock);
  1411. if (rc != 0)
  1412. {
  1413. return rc;
  1414. }
  1415. /* check if there is a changelog and whether this replica logs changes */
  1416. if (cl5GetState () == CL5_STATE_OPEN && (r->repl_flags & REPLICA_LOG_CHANGES))
  1417. {
  1418. /* Compare new ruv to the changelog's upper bound ruv. We could only keep
  1419. the existing changelog if its upper bound is the same as replica's RUV.
  1420. This is because if changelog has changes not in RUV, they will be
  1421. eventually sent to the consumer's which will cause a state mismatch
  1422. (because the supplier does not actually contain the changes in its data store.
  1423. If, on the other hand, the changelog is not as up to date as the supplier,
  1424. it is not really useful since out of sync consumer's can't be brought
  1425. up to date using this changelog and hence will need to be reinitialized */
  1426. /* replace ruv to make sure we work with the correct changelog file */
  1427. replica_lock(r->repl_lock);
  1428. new_ruv_obj = r->repl_ruv;
  1429. r->repl_ruv = old_ruv_obj;
  1430. replica_unlock(r->repl_lock);
  1431. rc = cl5GetUpperBoundRUV (r, &upper_bound_ruv);
  1432. if (rc != CL5_SUCCESS && rc != CL5_NOTFOUND)
  1433. {
  1434. return -1;
  1435. }
  1436. if (upper_bound_ruv)
  1437. {
  1438. new_ruv = object_get_data (new_ruv_obj);
  1439. PR_ASSERT (new_ruv);
  1440. /* ONREPL - there are more efficient ways to establish RUV equality.
  1441. However, because this is not in the critical path and we at most
  1442. have 2 elements in the RUV, this will not effect performance */
  1443. if (!ruv_covers_ruv (new_ruv, upper_bound_ruv) ||
  1444. !ruv_covers_ruv (upper_bound_ruv, new_ruv))
  1445. {
  1446. /* create a temporary replica object to conform to the interface */
  1447. r_obj = object_new (r, NULL);
  1448. /* We can't use existing changelog - remove existing file */
  1449. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_reload_ruv: "
  1450. "Warning: new data for replica %s does not match the data in the changelog.\n"
  1451. " Recreating the changelog file. This could affect replication with replica's "
  1452. " consumers in which case the consumers should be reinitialized.\n",
  1453. slapi_sdn_get_dn(r->repl_root));
  1454. rc = cl5DeleteDBSync (r_obj);
  1455. /* reinstate new ruv */
  1456. replica_lock(r->repl_lock);
  1457. r->repl_ruv = new_ruv_obj;
  1458. object_release (r_obj);
  1459. if (rc == CL5_SUCCESS)
  1460. {
  1461. /* log changes to mark starting point for replication */
  1462. rc = replica_log_ruv_elements_nolock (r);
  1463. }
  1464. replica_unlock(r->repl_lock);
  1465. }
  1466. else
  1467. {
  1468. /* we just need to reinstate new ruv */
  1469. replica_lock(r->repl_lock);
  1470. r->repl_ruv = new_ruv_obj;
  1471. replica_unlock(r->repl_lock);
  1472. }
  1473. }
  1474. else /* upper bound vector is not there - we have no changes logged */
  1475. {
  1476. /* reinstate new ruv */
  1477. replica_lock(r->repl_lock);
  1478. r->repl_ruv = new_ruv_obj;
  1479. /* just log elements of the current RUV. This is to have
  1480. a starting point for iteration through the changes */
  1481. rc = replica_log_ruv_elements_nolock (r);
  1482. replica_unlock(r->repl_lock);
  1483. }
  1484. }
  1485. if (rc == 0)
  1486. {
  1487. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  1488. /* reset mapping tree referrals based on new local RUV */
  1489. }
  1490. if (old_ruv_obj)
  1491. object_release (old_ruv_obj);
  1492. if (upper_bound_ruv)
  1493. ruv_destroy (&upper_bound_ruv);
  1494. return rc;
  1495. }
  1496. /* this function is called during server startup for each replica
  1497. to check whether the replica's data was reloaded offline and
  1498. whether replica's changelog needs to be reinitialized */
  1499. /* the function does not use replica lock but all functions it calls are
  1500. thread safe. Locking replica lock while calling changelog functions
  1501. causes a deadlock because changelog calls replica functions that
  1502. that lock the same lock */
  1503. int replica_check_for_data_reload (Replica *r, void *arg)
  1504. {
  1505. int rc = 0;
  1506. RUV *upper_bound_ruv = NULL;
  1507. RUV *r_ruv = NULL;
  1508. Object *r_obj, *ruv_obj;
  1509. PR_ASSERT (r);
  1510. /* check that we have a changelog and if this replica logs changes */
  1511. if (cl5GetState () == CL5_STATE_OPEN && (r->repl_flags & REPLICA_LOG_CHANGES))
  1512. {
  1513. /* Compare new ruv to the purge ruv. If the new contains csns which
  1514. are smaller than those in purge ruv, we need to remove old and
  1515. create new changelog file for this replica. This is because we
  1516. will not have sufficient changes to incrementally update a consumer
  1517. to the current state of the supplier. */
  1518. rc = cl5GetUpperBoundRUV (r, &upper_bound_ruv);
  1519. if (rc != CL5_SUCCESS && rc != CL5_NOTFOUND)
  1520. {
  1521. return -1;
  1522. }
  1523. if (upper_bound_ruv)
  1524. {
  1525. ruv_obj = replica_get_ruv (r);
  1526. r_ruv = object_get_data (ruv_obj);
  1527. PR_ASSERT (r_ruv);
  1528. /* Compare new ruv to the changelog's upper bound ruv. We could only keep
  1529. the existing changelog if its upper bound is the same as replica's RUV.
  1530. This is because if changelog has changes not in RUV, they will be
  1531. eventually sent to the consumer's which will cause a state mismatch
  1532. (because the supplier does not actually contain the changes in its data store.
  1533. If, on the other hand, the changelog is not as up to date as the supplier,
  1534. it is not really useful since out of sync consumer's can't be brought
  1535. up to date using this changelog and hence will need to be reinitialized */
  1536. /*
  1537. * Actually we can ignore the scenario that the changelog's upper
  1538. * bound ruv covers data store's ruv for two reasons: (1) a change
  1539. * is always written to the changelog after it is committed to the
  1540. * data store; (2) a change will be ignored if the server has seen
  1541. * it before - this happens frequently at the beginning of replication
  1542. * sessions.
  1543. */
  1544. if (slapi_disordely_shutdown(PR_FALSE)) {
  1545. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_check_for_data_reload: "
  1546. "Warning: disordely shutdown for replica %s. Check if DB RUV needs to be updated\n",
  1547. slapi_sdn_get_dn(r->repl_root));
  1548. if (ruv_covers_ruv(upper_bound_ruv, r_ruv) && !ruv_covers_ruv(r_ruv, upper_bound_ruv)) {
  1549. /*
  1550. * The Changelog RUV is ahead of the RUV in the DB.
  1551. * RUV DB was likely not flushed on disk.
  1552. */
  1553. ruv_force_csn_update_from_ruv(upper_bound_ruv, r_ruv,
  1554. "Force update of database RUV (from CL RUV) -> ", SLAPI_LOG_FATAL);
  1555. replica_set_ruv_dirty(r);
  1556. }
  1557. } else {
  1558. rc = ruv_compare_ruv(upper_bound_ruv, "changelog max RUV", r_ruv, "database RUV", 0, SLAPI_LOG_FATAL);
  1559. if (RUV_COMP_IS_FATAL(rc)) {
  1560. /* create a temporary replica object to conform to the interface */
  1561. r_obj = object_new(r, NULL);
  1562. /* We can't use existing changelog - remove existing file */
  1563. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_check_for_data_reload: "
  1564. "Warning: data for replica %s does not match the data in the changelog. "
  1565. "Recreating the changelog file. "
  1566. "This could affect replication with replica's consumers in which case the "
  1567. "consumers should be reinitialized.\n",
  1568. slapi_sdn_get_dn(r->repl_root));
  1569. rc = cl5DeleteDBSync(r_obj);
  1570. object_release(r_obj);
  1571. if (rc == CL5_SUCCESS) {
  1572. /* log changes to mark starting point for replication */
  1573. rc = replica_log_ruv_elements(r);
  1574. }
  1575. } else if (rc) {
  1576. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_check_for_data_reload: "
  1577. "Warning: for replica %s there were some differences between the changelog max RUV and the "
  1578. "database RUV. If there are obsolete elements in the database RUV, you "
  1579. "should remove them using the CLEANALLRUV task. If they are not obsolete, "
  1580. "you should check their status to see why there are no changes from those "
  1581. "servers in the changelog.\n",
  1582. slapi_sdn_get_dn(r->repl_root));
  1583. rc = 0;
  1584. }
  1585. } // slapi_disordely_shutdown
  1586. object_release (ruv_obj);
  1587. }
  1588. else /* we have no changes currently logged for this replica */
  1589. {
  1590. /* log changes to mark starting point for replication */
  1591. rc = replica_log_ruv_elements (r);
  1592. }
  1593. }
  1594. if (rc == 0)
  1595. {
  1596. /* reset mapping tree referrals based on new local RUV */
  1597. consumer5_set_mapping_tree_state_for_replica(r, NULL);
  1598. }
  1599. if (upper_bound_ruv)
  1600. ruv_destroy (&upper_bound_ruv);
  1601. return rc;
  1602. }
  1603. /* Helper functions */
  1604. /* reads replica configuration entry. The entry is the child of the
  1605. mapping tree node for the replica's backend */
  1606. static Slapi_Entry*
  1607. _replica_get_config_entry (const Slapi_DN *root, const char **attrs)
  1608. {
  1609. int rc = 0;
  1610. char *dn = NULL;
  1611. Slapi_Entry **entries;
  1612. Slapi_Entry *e = NULL;
  1613. Slapi_PBlock *pb = NULL;
  1614. dn = _replica_get_config_dn (root);
  1615. if (NULL == dn) {
  1616. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1617. "_replica_get_config_entry: failed to get the config dn for %s\n",
  1618. slapi_sdn_get_dn (root));
  1619. return NULL;
  1620. }
  1621. pb = slapi_pblock_new ();
  1622. slapi_search_internal_set_pb (pb, dn, LDAP_SCOPE_BASE, "objectclass=*", (char **)attrs, 0, NULL,
  1623. NULL, repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  1624. slapi_search_internal_pb (pb);
  1625. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  1626. if (rc == 0)
  1627. {
  1628. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  1629. e = slapi_entry_dup (entries [0]);
  1630. }
  1631. slapi_free_search_results_internal(pb);
  1632. slapi_pblock_destroy (pb);
  1633. slapi_ch_free_string(&dn);
  1634. return e;
  1635. }
  1636. /* It does an internal search to read the in memory RUV
  1637. * of the provided suffix
  1638. */
  1639. Slapi_Entry *
  1640. get_in_memory_ruv(Slapi_DN *suffix_sdn)
  1641. {
  1642. const char *attrs[4];
  1643. /* these two attributes needs to be asked when reading the RUV */
  1644. attrs[0] = type_ruvElement;
  1645. attrs[1] = type_ruvElementUpdatetime;
  1646. attrs[2] = type_agmtMaxCSN;
  1647. attrs[3] = NULL;
  1648. return(_replica_get_config_entry(suffix_sdn, attrs));
  1649. }
  1650. char *
  1651. replica_get_dn(Replica *r)
  1652. {
  1653. return _replica_get_config_dn (r->repl_root);
  1654. }
  1655. static int
  1656. _replica_check_validity (const Replica *r)
  1657. {
  1658. PR_ASSERT (r);
  1659. if (r->repl_root == NULL || r->repl_type == 0 || r->repl_rid == 0 ||
  1660. r->repl_csngen == NULL || r->repl_name == NULL)
  1661. {
  1662. return -1;
  1663. }
  1664. else
  1665. {
  1666. return 0;
  1667. }
  1668. }
  1669. /* replica configuration entry has the following format:
  1670. dn: cn=replica,<mapping tree node dn>
  1671. objectclass: top
  1672. objectclass: nsds5Replica
  1673. objectclass: extensibleObject
  1674. nsds5ReplicaRoot: <root of the replica>
  1675. nsds5ReplicaId: <replica id>
  1676. nsds5ReplicaType: <type of the replica: primary, read-write or read-only>
  1677. nsState: <state of the csn generator> missing the first time replica is started
  1678. nsds5ReplicaBindDN: <supplier update dn> consumers only
  1679. nsds5ReplicaBindDNGroup: group, containing replicaBindDNs
  1680. nsds5ReplicaBindDNGroupCheckInterval: defines how frequently to check for update of bindGroup
  1681. nsds5ReplicaReferral: <referral URL to updatable replica> consumers only
  1682. nsds5ReplicaPurgeDelay: <time, in seconds, to keep purgeable CSNs, 0 == keep forever>
  1683. nsds5ReplicaTombstonePurgeInterval: <time, in seconds, between tombstone purge runs, 0 == don't reap>
  1684. nsds5ReplicaLegacyConsumer: <TRUE | FALSE>
  1685. richm: changed slapi entry from const to editable - if the replica id is supplied for a read
  1686. only replica, we ignore it and replace the value with the READ_ONLY_REPLICA_ID
  1687. */
  1688. static int
  1689. _replica_init_from_config (Replica *r, Slapi_Entry *e, char *errortext)
  1690. {
  1691. Slapi_Attr *a = NULL;
  1692. Slapi_Attr *attr;
  1693. CSNGen *gen;
  1694. char *precise_purging = NULL;
  1695. char buf [SLAPI_DSE_RETURNTEXT_SIZE];
  1696. char *errormsg = errortext? errortext : buf;
  1697. char *val;
  1698. int backoff_min;
  1699. int backoff_max;
  1700. int ptimeout = 0;
  1701. int rc;
  1702. PR_ASSERT (r && e);
  1703. /* get replica root */
  1704. val = slapi_entry_attr_get_charptr (e, attr_replicaRoot);
  1705. if (val == NULL){
  1706. PR_snprintf (errormsg, SLAPI_DSE_RETURNTEXT_SIZE, "failed to retrieve %s attribute from (%s)\n",
  1707. attr_replicaRoot,
  1708. (char*)slapi_entry_get_dn ((Slapi_Entry*)e));
  1709. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "_replica_init_from_config: %s\n",
  1710. errormsg);
  1711. return -1;
  1712. }
  1713. r->repl_root = slapi_sdn_new_dn_passin (val);
  1714. /* get replica type */
  1715. val = slapi_entry_attr_get_charptr (e, attr_replicaType);
  1716. if (val){
  1717. r->repl_type = atoi(val);
  1718. slapi_ch_free ((void**)&val);
  1719. } else {
  1720. r->repl_type = REPLICA_TYPE_READONLY;
  1721. }
  1722. /* get legacy consumer flag */
  1723. val = slapi_entry_attr_get_charptr (e, type_replicaLegacyConsumer);
  1724. if (val){
  1725. if (strcasecmp (val, "on") == 0 || strcasecmp (val, "yes") == 0 ||
  1726. strcasecmp (val, "true") == 0 || strcasecmp (val, "1") == 0)
  1727. {
  1728. r->legacy_consumer = PR_TRUE;
  1729. } else {
  1730. r->legacy_consumer = PR_FALSE;
  1731. }
  1732. slapi_ch_free ((void**)&val);
  1733. } else {
  1734. r->legacy_consumer = PR_FALSE;
  1735. }
  1736. /* grab and validate the backoff retry settings */
  1737. if(slapi_entry_attr_exists(e, type_replicaBackoffMin)){
  1738. backoff_min = slapi_entry_attr_get_int(e, type_replicaBackoffMin);
  1739. if(backoff_min <= 0){
  1740. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Invalid value for %s: %d Using default value (%d)\n",
  1741. type_replicaBackoffMin, backoff_min, PROTOCOL_BACKOFF_MINIMUM );
  1742. backoff_min = PROTOCOL_BACKOFF_MINIMUM;
  1743. }
  1744. } else {
  1745. backoff_min = PROTOCOL_BACKOFF_MINIMUM;
  1746. }
  1747. if(slapi_entry_attr_exists(e, type_replicaBackoffMax)){
  1748. backoff_max = slapi_entry_attr_get_int(e, type_replicaBackoffMax);
  1749. if(backoff_max <= 0){
  1750. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Invalid value for %s: %d Using default value (%d)\n",
  1751. type_replicaBackoffMax, backoff_max, PROTOCOL_BACKOFF_MAXIMUM );
  1752. backoff_max = PROTOCOL_BACKOFF_MAXIMUM;
  1753. }
  1754. } else {
  1755. backoff_max = PROTOCOL_BACKOFF_MAXIMUM;
  1756. }
  1757. if(backoff_min > backoff_max){
  1758. /* Ok these values are invalid, reset back the defaults */
  1759. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Backoff minimum (%d) can not be greater than "
  1760. "the backoff maximum (%d). Using default values: min (%d) max (%d)\n", backoff_min, backoff_max,
  1761. PROTOCOL_BACKOFF_MINIMUM, PROTOCOL_BACKOFF_MAXIMUM);
  1762. slapi_counter_set_value(r->backoff_min, PROTOCOL_BACKOFF_MINIMUM);
  1763. slapi_counter_set_value(r->backoff_max, PROTOCOL_BACKOFF_MAXIMUM);
  1764. } else {
  1765. slapi_counter_set_value(r->backoff_min, backoff_min);
  1766. slapi_counter_set_value(r->backoff_max, backoff_max);
  1767. }
  1768. /* get the protocol timeout */
  1769. ptimeout = slapi_entry_attr_get_int(e, type_replicaProtocolTimeout);
  1770. if(ptimeout <= 0){
  1771. slapi_counter_set_value(r->protocol_timeout, DEFAULT_PROTOCOL_TIMEOUT);
  1772. } else {
  1773. slapi_counter_set_value(r->protocol_timeout, ptimeout);
  1774. }
  1775. /* check for precise tombstone purging */
  1776. precise_purging = slapi_entry_attr_get_charptr(e, type_replicaPrecisePurge);
  1777. if(precise_purging){
  1778. if (strcasecmp(precise_purging, "on") == 0){
  1779. slapi_counter_set_value(r->precise_purging, 1);
  1780. } else if (strcasecmp(precise_purging, "off") == 0){
  1781. slapi_counter_set_value(r->precise_purging, 0);
  1782. } else{
  1783. /* Invalid value */
  1784. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1785. "Invalid value for %s: %s Using default value (off)\n",
  1786. type_replicaPrecisePurge, precise_purging);
  1787. slapi_counter_set_value(r->precise_purging, 0);
  1788. }
  1789. slapi_ch_free_string(&precise_purging);
  1790. } else {
  1791. slapi_counter_set_value(r->precise_purging, 0);
  1792. }
  1793. /* get replica flags */
  1794. r->repl_flags = slapi_entry_attr_get_ulong(e, attr_flags);
  1795. /*
  1796. * Get replicaid
  1797. * The replica id is ignored for read only replicas and is set to the
  1798. * special value READ_ONLY_REPLICA_ID
  1799. */
  1800. if (r->repl_type == REPLICA_TYPE_READONLY){
  1801. r->repl_rid = READ_ONLY_REPLICA_ID;
  1802. slapi_entry_attr_set_uint(e, attr_replicaId, (unsigned int)READ_ONLY_REPLICA_ID);
  1803. }
  1804. /* a replica id is required for updatable and primary replicas */
  1805. else if (r->repl_type == REPLICA_TYPE_UPDATABLE ||
  1806. r->repl_type == REPLICA_TYPE_PRIMARY)
  1807. {
  1808. if ((val = slapi_entry_attr_get_charptr (e, attr_replicaId))){
  1809. int temprid = atoi (val);
  1810. slapi_ch_free ((void**)&val);
  1811. if (temprid <= 0 || temprid >= READ_ONLY_REPLICA_ID){
  1812. PR_snprintf (errormsg, SLAPI_DSE_RETURNTEXT_SIZE,
  1813. "attribute %s must have a value greater than 0 "
  1814. "and less than %d: entry %s",
  1815. attr_replicaId, READ_ONLY_REPLICA_ID,
  1816. (char*)slapi_entry_get_dn ((Slapi_Entry*)e));
  1817. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1818. "_replica_init_from_config: %s\n", errormsg);
  1819. return -1;
  1820. } else {
  1821. r->repl_rid = (ReplicaId)temprid;
  1822. }
  1823. } else {
  1824. PR_snprintf (errormsg, SLAPI_DSE_RETURNTEXT_SIZE,
  1825. "failed to retrieve required %s attribute from %s",
  1826. attr_replicaId,(char*)slapi_entry_get_dn ((Slapi_Entry*)e));
  1827. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1828. "_replica_init_from_config: %s\n", errormsg);
  1829. return -1;
  1830. }
  1831. }
  1832. attr = NULL;
  1833. rc = slapi_entry_attr_find(e, attr_state, &attr);
  1834. gen = csngen_new (r->repl_rid, attr);
  1835. if (gen == NULL){
  1836. PR_snprintf (errormsg, SLAPI_DSE_RETURNTEXT_SIZE,
  1837. "failed to create csn generator for replica (%s)",
  1838. (char*)slapi_entry_get_dn ((Slapi_Entry*)e));
  1839. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1840. "_replica_init_from_config: %s\n", errormsg);
  1841. return -1;
  1842. }
  1843. r->repl_csngen = object_new((void*)gen, (FNFree)csngen_free);
  1844. /* Hook generator so we can maintain min/max CSN info */
  1845. r->csn_pl_reg_id = csngen_register_callbacks(gen, assign_csn_callback, r, abort_csn_callback, r);
  1846. /* get replication bind dn */
  1847. r->updatedn_list = replica_updatedn_list_new(e);
  1848. /* get replication bind dn groups */
  1849. r->updatedn_groups = replica_updatedn_group_new(e);
  1850. r->groupdn_list = replica_groupdn_list_new(r->updatedn_groups);
  1851. r->updatedn_group_last_check = time(NULL);
  1852. /* get groupdn check interval */
  1853. val = slapi_entry_attr_get_charptr (e, attr_replicaBindDnGroupCheckInterval);
  1854. if (val) {
  1855. r->updatedn_group_check_interval = atoi(val);
  1856. slapi_ch_free ((void**)&val);
  1857. } else {
  1858. r->updatedn_group_check_interval = -1;
  1859. }
  1860. /* get replica name */
  1861. val = slapi_entry_attr_get_charptr (e, attr_replicaName);
  1862. if (val) {
  1863. r->repl_name = val;
  1864. } else {
  1865. rc = slapi_uniqueIDGenerateString (&r->repl_name);
  1866. if (rc != UID_SUCCESS){
  1867. PR_snprintf (errormsg, SLAPI_DSE_RETURNTEXT_SIZE,
  1868. "failed to assign replica name for replica (%s); uuid generator error - %d ",
  1869. (char*)slapi_entry_get_dn ((Slapi_Entry*)e), rc);
  1870. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "_replica_init_from_config: %s\n",
  1871. errormsg);
  1872. return -1;
  1873. } else
  1874. r->new_name = PR_TRUE;
  1875. }
  1876. /* get the list of referrals */
  1877. slapi_entry_attr_find( e, attr_replicaReferral, &attr );
  1878. if(attr!=NULL){
  1879. slapi_attr_get_valueset(attr, &r->repl_referral);
  1880. }
  1881. /*
  1882. * Set the purge offset (default 7 days). This is the extra
  1883. * time we allow purgeable CSNs to stick around, in case a
  1884. * replica regresses. Could also be useful when LCUP happens,
  1885. * since we don't know about LCUP replicas, and they can just
  1886. * turn up whenever they want to.
  1887. */
  1888. if (slapi_entry_attr_find(e, type_replicaPurgeDelay, &a) == -1){
  1889. /* No purge delay provided, so use default */
  1890. r->repl_purge_delay = 60 * 60 * 24 * 7; /* One week, in seconds */
  1891. } else {
  1892. r->repl_purge_delay = slapi_entry_attr_get_uint(e, type_replicaPurgeDelay);
  1893. }
  1894. if (slapi_entry_attr_find(e, type_replicaTombstonePurgeInterval, &a) == -1){
  1895. /* No reap interval provided, so use default */
  1896. r->tombstone_reap_interval = 3600 * 24; /* One day */
  1897. } else {
  1898. r->tombstone_reap_interval = slapi_entry_attr_get_int(e, type_replicaTombstonePurgeInterval);
  1899. }
  1900. r->tombstone_reap_stop = r->tombstone_reap_active = PR_FALSE;
  1901. return (_replica_check_validity (r));
  1902. }
  1903. void
  1904. replica_check_for_tasks(Replica *r, Slapi_Entry *e)
  1905. {
  1906. char **clean_vals;
  1907. if(e == NULL || ldif_dump_is_running() == PR_TRUE){
  1908. /* If db2ldif is being run, do not check if there are incomplete tasks */
  1909. return;
  1910. }
  1911. /*
  1912. * check if we are in the middle of a CLEANALLRUV task,
  1913. * if so set the cleaned rid, and fire off the thread
  1914. */
  1915. if ((clean_vals = slapi_entry_attr_get_charray(e, type_replicaCleanRUV)) != NULL)
  1916. {
  1917. PRThread *thread = NULL;
  1918. struct berval *payload = NULL;
  1919. CSN *maxcsn = NULL;
  1920. ReplicaId rid;
  1921. char csnstr[CSN_STRSIZE];
  1922. char *token = NULL;
  1923. char *forcing;
  1924. char *csnpart;
  1925. char *ridstr;
  1926. char *iter = NULL;
  1927. int i;
  1928. for(i = 0; i < CLEANRIDSIZ && clean_vals[i]; i++){
  1929. cleanruv_data *data = NULL;
  1930. /*
  1931. * Set the cleanruv data, and add the cleaned rid
  1932. */
  1933. token = ldap_utf8strtok_r(clean_vals[i], ":", &iter);
  1934. if(token){
  1935. rid = atoi(token);
  1936. if(rid <= 0 || rid >= READ_ONLY_REPLICA_ID){
  1937. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: invalid replica id(%d) "
  1938. "aborting task.\n", rid);
  1939. goto done;
  1940. }
  1941. } else {
  1942. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: unable to parse cleanallruv "
  1943. "data (%s), aborting task.\n",clean_vals[i]);
  1944. goto done;
  1945. }
  1946. csnpart = ldap_utf8strtok_r(iter, ":", &iter);
  1947. maxcsn = csn_new();
  1948. csn_init_by_string(maxcsn, csnpart);
  1949. csn_as_string(maxcsn, PR_FALSE, csnstr);
  1950. forcing = ldap_utf8strtok_r(iter, ":", &iter);
  1951. if(forcing == NULL){
  1952. forcing = "no";
  1953. }
  1954. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: cleanAllRUV task found, "
  1955. "resuming the cleaning of rid(%d)...\n", rid);
  1956. /*
  1957. * Create payload
  1958. */
  1959. ridstr = slapi_ch_smprintf("%d:%s:%s:%s", rid, slapi_sdn_get_dn(replica_get_root(r)), csnstr, forcing);
  1960. payload = create_cleanruv_payload(ridstr);
  1961. slapi_ch_free_string(&ridstr);
  1962. if(payload == NULL){
  1963. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "CleanAllRUV Task: Startup: Failed to "
  1964. "create extended op payload, aborting task");
  1965. csn_free(&maxcsn);
  1966. goto done;
  1967. }
  1968. /*
  1969. * Setup the data struct, and fire off the thread.
  1970. */
  1971. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  1972. if (data == NULL) {
  1973. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV: failed to allocate cleanruv_data.\n");
  1974. csn_free(&maxcsn);
  1975. } else {
  1976. /* setup our data */
  1977. data->repl_obj = NULL;
  1978. data->replica = NULL;
  1979. data->rid = rid;
  1980. data->task = NULL;
  1981. data->maxcsn = maxcsn;
  1982. data->payload = payload;
  1983. data->sdn = slapi_sdn_dup(r->repl_root);
  1984. data->force = slapi_ch_strdup(forcing);
  1985. data->repl_root = NULL;
  1986. thread = PR_CreateThread(PR_USER_THREAD, replica_cleanallruv_thread_ext,
  1987. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  1988. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  1989. if (thread == NULL) {
  1990. /* log an error and free everything */
  1991. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "cleanAllRUV: unable to create cleanAllRUV "
  1992. "thread for rid(%d)\n", (int)data->rid);
  1993. csn_free(&maxcsn);
  1994. slapi_sdn_free(&data->sdn);
  1995. ber_bvfree(data->payload);
  1996. slapi_ch_free_string(&data->force);
  1997. slapi_ch_free((void **)&data);
  1998. }
  1999. }
  2000. }
  2001. done:
  2002. slapi_ch_array_free(clean_vals);
  2003. }
  2004. if ((clean_vals = slapi_entry_attr_get_charray(e, type_replicaAbortCleanRUV)) != NULL)
  2005. {
  2006. PRThread *thread = NULL;
  2007. struct berval *payload;
  2008. ReplicaId rid;
  2009. char *certify = NULL;
  2010. char *ridstr = NULL;
  2011. char *token = NULL;
  2012. char *repl_root;
  2013. char *iter = NULL;
  2014. int i;
  2015. for(i = 0; clean_vals[i]; i++){
  2016. cleanruv_data *data = NULL;
  2017. token = ldap_utf8strtok_r(clean_vals[i], ":", &iter);
  2018. if(token){
  2019. rid = atoi(token);
  2020. if(rid <= 0 || rid >= READ_ONLY_REPLICA_ID){
  2021. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: invalid replica id(%d) "
  2022. "aborting abort task.\n", rid);
  2023. goto done2;
  2024. }
  2025. } else {
  2026. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: unable to parse cleanallruv "
  2027. "data (%s), aborting abort task.\n",clean_vals[i]);
  2028. goto done2;
  2029. }
  2030. repl_root = ldap_utf8strtok_r(iter, ":", &iter);
  2031. certify = ldap_utf8strtok_r(iter, ":", &iter);
  2032. if(!is_cleaned_rid(rid)){
  2033. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: replica id(%d) is not "
  2034. "being cleaned, nothing to abort. Aborting abort task.\n", rid);
  2035. delete_aborted_rid(r, rid, repl_root, 0);
  2036. goto done2;
  2037. }
  2038. add_aborted_rid(rid, r, repl_root);
  2039. stop_ruv_cleaning();
  2040. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: abort task found, "
  2041. "resuming abort of rid(%d).\n", rid);
  2042. /*
  2043. * Setup the data struct, and fire off the abort thread.
  2044. */
  2045. data = (cleanruv_data*)slapi_ch_calloc(1, sizeof(cleanruv_data));
  2046. if (data == NULL) {
  2047. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to allocate cleanruv_data.\n");
  2048. } else {
  2049. ridstr = slapi_ch_smprintf("%d:%s:%s", rid, repl_root, certify);
  2050. payload = create_cleanruv_payload(ridstr);
  2051. slapi_ch_free_string(&ridstr);
  2052. if(payload == NULL){
  2053. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: failed to create extended "
  2054. "op payload\n");
  2055. slapi_ch_free((void **)&data);
  2056. } else {
  2057. /* setup the data */
  2058. data->repl_obj = NULL;
  2059. data->replica = NULL;
  2060. data->rid = rid;
  2061. data->task = NULL;
  2062. data->payload = payload;
  2063. data->repl_root = slapi_ch_strdup(repl_root);
  2064. data->sdn = slapi_sdn_dup(r->repl_root);
  2065. data->certify = slapi_ch_strdup(certify);
  2066. thread = PR_CreateThread(PR_USER_THREAD, replica_abort_task_thread,
  2067. (void *)data, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  2068. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  2069. if (thread == NULL) {
  2070. slapi_log_error( SLAPI_LOG_FATAL, repl_plugin_name, "Abort CleanAllRUV Task: unable to create abort cleanAllRUV "
  2071. "thread for rid(%d)\n", (int)data->rid);
  2072. slapi_sdn_free(&data->sdn);
  2073. ber_bvfree(data->payload);
  2074. slapi_ch_free_string(&data->repl_root);
  2075. slapi_ch_free_string(&data->certify);
  2076. slapi_ch_free((void **)&data);
  2077. }
  2078. }
  2079. }
  2080. }
  2081. done2:
  2082. slapi_ch_array_free(clean_vals);
  2083. }
  2084. }
  2085. /* This function updates the entry to contain information generated
  2086. during replica initialization.
  2087. Returns 0 if successful and -1 otherwise */
  2088. static int
  2089. _replica_update_entry (Replica *r, Slapi_Entry *e, char *errortext)
  2090. {
  2091. int rc;
  2092. Slapi_Mod smod;
  2093. Slapi_Value *val;
  2094. PR_ASSERT (r);
  2095. /* add attribute that stores state of csn generator */
  2096. rc = csngen_get_state ((CSNGen*)object_get_data (r->repl_csngen), &smod);
  2097. if (rc != CSN_SUCCESS)
  2098. {
  2099. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to get csn generator's state; csn error - %d", rc);
  2100. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2101. "_replica_update_entry: %s\n", errortext);
  2102. return -1;
  2103. }
  2104. val = slapi_value_new_berval(slapi_mod_get_first_value(&smod));
  2105. rc = slapi_entry_add_value (e, slapi_mod_get_type (&smod), val);
  2106. slapi_value_free(&val);
  2107. slapi_mod_done (&smod);
  2108. if (rc != 0)
  2109. {
  2110. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to update replica entry");
  2111. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2112. "_replica_update_entry: %s\n", errortext);
  2113. return -1;
  2114. }
  2115. /* add attribute that stores replica name */
  2116. rc = slapi_entry_add_string (e, attr_replicaName, r->repl_name);
  2117. if (rc != 0)
  2118. {
  2119. PR_snprintf(errortext, SLAPI_DSE_RETURNTEXT_SIZE, "failed to update replica entry");
  2120. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2121. "_replica_update_entry: %s\n", errortext);
  2122. return -1;
  2123. }
  2124. else
  2125. r->new_name = PR_FALSE;
  2126. return 0;
  2127. }
  2128. /* DN format: cn=replica,cn=\"<root>\",cn=mapping tree,cn=config */
  2129. static char*
  2130. _replica_get_config_dn (const Slapi_DN *root)
  2131. {
  2132. char *dn;
  2133. /* "cn=mapping tree,cn=config" */
  2134. const char *mp_base = slapi_get_mapping_tree_config_root ();
  2135. PR_ASSERT (root);
  2136. /* This function converts the old style DN to the new style. */
  2137. dn = slapi_ch_smprintf("%s,cn=\"%s\",%s",
  2138. REPLICA_RDN, slapi_sdn_get_dn (root), mp_base);
  2139. return dn;
  2140. }
  2141. /* This function retrieves RUV from the root of the replicated tree.
  2142. * The attribute can be missing if
  2143. * (1) this replica is the first supplier and replica generation has not been assigned
  2144. * or
  2145. * (2) this is a consumer that has not been yet initialized
  2146. * In either case, replica_set_ruv should be used to further initialize the replica.
  2147. * Returns 0 on success, -1 on failure. If 0 is returned, the RUV is present in the replica.
  2148. */
  2149. static int
  2150. _replica_configure_ruv (Replica *r, PRBool isLocked)
  2151. {
  2152. Slapi_PBlock *pb = NULL;
  2153. char *attrs[2];
  2154. int rc;
  2155. int return_value = -1;
  2156. Slapi_Entry **entries = NULL;
  2157. Slapi_Attr *attr;
  2158. RUV *ruv = NULL;
  2159. CSN *csn = NULL;
  2160. ReplicaId rid = 0;
  2161. /* read ruv state from the ruv tombstone entry */
  2162. pb = slapi_pblock_new();
  2163. if (!pb) {
  2164. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2165. "_replica_configure_ruv: Out of memory\n");
  2166. goto done;
  2167. }
  2168. attrs[0] = (char*)type_ruvElement;
  2169. attrs[1] = NULL;
  2170. slapi_search_internal_set_pb(
  2171. pb,
  2172. slapi_sdn_get_dn(r->repl_root),
  2173. LDAP_SCOPE_BASE,
  2174. "objectclass=*",
  2175. attrs,
  2176. 0, /* attrsonly */
  2177. NULL, /* controls */
  2178. RUV_STORAGE_ENTRY_UNIQUEID,
  2179. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  2180. OP_FLAG_REPLICATED); /* flags */
  2181. slapi_search_internal_pb (pb);
  2182. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2183. if (rc == LDAP_SUCCESS)
  2184. {
  2185. /* get RUV attributes and construct the RUV */
  2186. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
  2187. if (NULL == entries || NULL == entries[0])
  2188. {
  2189. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2190. "_replica_configure_ruv: replica ruv tombstone entry for "
  2191. "replica %s not found\n",
  2192. slapi_sdn_get_dn(r->repl_root));
  2193. goto done;
  2194. }
  2195. rc = slapi_entry_attr_find(entries[0], type_ruvElement, &attr);
  2196. if (rc != 0) /* ruv attribute is missing - this not allowed */
  2197. {
  2198. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2199. "_replica_configure_ruv: replica ruv tombstone entry for "
  2200. "replica %s does not contain %s\n",
  2201. slapi_sdn_get_dn(r->repl_root), type_ruvElement);
  2202. goto done;
  2203. }
  2204. /* Check in the tombstone we have retrieved if the local purl is
  2205. already present:
  2206. rid == 0: the local purl is not present
  2207. rid != 0: the local purl is present ==> nothing to do
  2208. */
  2209. ruv_init_from_slapi_attr_and_check_purl (attr, &ruv, &rid);
  2210. if (ruv)
  2211. {
  2212. char *generation = NULL;
  2213. generation = ruv_get_replica_generation(ruv);
  2214. if (NULL != generation)
  2215. {
  2216. r->repl_ruv = object_new((void*)ruv, (FNFree)ruv_destroy);
  2217. /* Is the local purl in the ruv? (the port or the host could have
  2218. changed)
  2219. */
  2220. /* A consumer only doesn't have its purl in its ruv */
  2221. if (r->repl_type == REPLICA_TYPE_UPDATABLE)
  2222. {
  2223. int need_update = 0;
  2224. #define RUV_UPDATE_PARTIAL 1
  2225. #define RUV_UPDATE_FULL 2
  2226. if (rid == 0)
  2227. {
  2228. /* We can not have more than 1 ruv with the same rid
  2229. so we replace it */
  2230. const char *purl = NULL;
  2231. purl = multimaster_get_local_purl();
  2232. ruv_delete_replica(ruv, r->repl_rid);
  2233. ruv_add_index_replica(ruv, r->repl_rid, purl, 1);
  2234. need_update = RUV_UPDATE_PARTIAL; /* ruv changed, so write tombstone */
  2235. }
  2236. else /* bug 540844: make sure the local supplier rid is first in the ruv */
  2237. {
  2238. /* make sure local supplier is first in list */
  2239. ReplicaId first_rid = 0;
  2240. char *first_purl = NULL;
  2241. ruv_get_first_id_and_purl(ruv, &first_rid, &first_purl);
  2242. /* if the local supplier is not first in the list . . . */
  2243. /* rid is from changelog; first_rid is from backend */
  2244. if (rid != first_rid)
  2245. {
  2246. /* . . . move the local supplier to the beginning of the list */
  2247. ruv_move_local_supplier_to_first(ruv, rid);
  2248. need_update = RUV_UPDATE_PARTIAL; /* must update tombstone also */
  2249. }
  2250. /* r->repl_rid is from config; rid is from changelog */
  2251. if (r->repl_rid != rid)
  2252. {
  2253. /* Most likely, the replica was once deleted
  2254. * and recreated with a different rid from the
  2255. * previous. */
  2256. /* must recreate ruv tombstone */
  2257. need_update = RUV_UPDATE_FULL;
  2258. if(NULL != r->repl_ruv)
  2259. {
  2260. object_release(r->repl_ruv);
  2261. r->repl_ruv = NULL;
  2262. }
  2263. }
  2264. }
  2265. /* Update also the directory entry */
  2266. if (RUV_UPDATE_PARTIAL == need_update) {
  2267. replica_replace_ruv_tombstone(r);
  2268. } else if (RUV_UPDATE_FULL == need_update) {
  2269. _delete_tombstone(slapi_sdn_get_dn(r->repl_root),
  2270. RUV_STORAGE_ENTRY_UNIQUEID,
  2271. OP_FLAG_REPL_RUV);
  2272. rc = replica_create_ruv_tombstone(r);
  2273. if (rc) {
  2274. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2275. "_replica_configure_ruv: "
  2276. "failed to recreate replica ruv tombstone entry"
  2277. " (%s); LDAP error - %d\n",
  2278. slapi_sdn_get_dn(r->repl_root), rc);
  2279. slapi_ch_free_string(&generation);
  2280. goto done;
  2281. }
  2282. }
  2283. #undef RUV_UPDATE_PARTIAL
  2284. #undef RUV_UPDATE_FULL
  2285. }
  2286. slapi_ch_free_string(&generation);
  2287. return_value = 0;
  2288. }
  2289. else
  2290. {
  2291. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2292. "RUV for replica %s is missing replica generation\n",
  2293. slapi_sdn_get_dn(r->repl_root));
  2294. goto done;
  2295. }
  2296. }
  2297. else
  2298. {
  2299. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2300. "Unable to convert %s attribute in entry %s to a replica update vector.\n",
  2301. type_ruvElement, slapi_sdn_get_dn(r->repl_root));
  2302. goto done;
  2303. }
  2304. }
  2305. else /* search failed */
  2306. {
  2307. if (LDAP_NO_SUCH_OBJECT == rc)
  2308. {
  2309. /* The entry doesn't exist: create it */
  2310. rc = replica_create_ruv_tombstone(r);
  2311. if (LDAP_SUCCESS != rc)
  2312. {
  2313. /*
  2314. * XXXggood - the following error appears on startup if we try
  2315. * to initialize replica RUVs before the backend instance is up.
  2316. * It's alarming to see this error, and we should suppress it
  2317. * (or avoid trying to configure it) if the backend instance is
  2318. * not yet online.
  2319. */
  2320. /*
  2321. * XXXrichm - you can also get this error when the backend is in
  2322. * read only mode c.f. bug 539782
  2323. */
  2324. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2325. "_replica_configure_ruv: failed to create replica ruv tombstone "
  2326. "entry (%s); LDAP error - %d\n",
  2327. slapi_sdn_get_dn(r->repl_root), rc);
  2328. goto done;
  2329. }
  2330. else
  2331. {
  2332. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2333. "_replica_configure_ruv: No ruv tombstone found for replica %s. "
  2334. "Created a new one\n",
  2335. slapi_sdn_get_dn(r->repl_root));
  2336. return_value = 0;
  2337. }
  2338. }
  2339. else
  2340. {
  2341. /* see if the suffix is disabled */
  2342. char *state = slapi_mtn_get_state(r->repl_root);
  2343. if (state && !strcasecmp(state, "disabled"))
  2344. {
  2345. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2346. "_replica_configure_ruv: replication disabled for "
  2347. "entry (%s); LDAP error - %d\n",
  2348. slapi_sdn_get_dn(r->repl_root), rc);
  2349. slapi_ch_free_string(&state);
  2350. goto done;
  2351. }
  2352. else if (!r->repl_ruv) /* other error */
  2353. {
  2354. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2355. "_replica_configure_ruv: replication broken for "
  2356. "entry (%s); LDAP error - %d\n",
  2357. slapi_sdn_get_dn(r->repl_root), rc);
  2358. slapi_ch_free_string(&state);
  2359. goto done;
  2360. }
  2361. else /* some error but continue anyway? */
  2362. {
  2363. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2364. "_replica_configure_ruv: Error %d reading tombstone for replica %s.\n",
  2365. rc, slapi_sdn_get_dn(r->repl_root));
  2366. return_value = 0;
  2367. }
  2368. slapi_ch_free_string(&state);
  2369. }
  2370. }
  2371. if (NULL != r->min_csn_pl)
  2372. {
  2373. csnplFree (&r->min_csn_pl);
  2374. }
  2375. /* create pending list for min csn if necessary */
  2376. if (ruv_get_smallest_csn_for_replica ((RUV*)object_get_data (r->repl_ruv),
  2377. r->repl_rid, &csn) == RUV_SUCCESS)
  2378. {
  2379. csn_free (&csn);
  2380. r->min_csn_pl = NULL;
  2381. }
  2382. else
  2383. {
  2384. /*
  2385. * The local replica has not generated any of its own CSNs yet.
  2386. * We need to watch CSNs being generated and note the first
  2387. * locally-generated CSN that's committed. Once that event occurs,
  2388. * the RUV is suitable for iteration over locally generated
  2389. * changes.
  2390. */
  2391. r->min_csn_pl = csnplNew();
  2392. }
  2393. done:
  2394. if (NULL != pb)
  2395. {
  2396. slapi_free_search_results_internal(pb);
  2397. slapi_pblock_destroy (pb);
  2398. }
  2399. if (return_value != 0)
  2400. {
  2401. if (ruv)
  2402. ruv_destroy (&ruv);
  2403. }
  2404. return return_value;
  2405. }
  2406. /* NOTE - this is the only non-api function that performs locking because
  2407. it is called by the event queue */
  2408. void
  2409. replica_update_state (time_t when, void *arg)
  2410. {
  2411. int rc;
  2412. const char *replica_name = (const char *)arg;
  2413. Object *replica_object = NULL;
  2414. Replica *r;
  2415. Slapi_Mod smod;
  2416. LDAPMod *mods[3];
  2417. Slapi_PBlock *pb = NULL;
  2418. char *dn = NULL;
  2419. struct berval *vals[2];
  2420. struct berval val;
  2421. LDAPMod mod;
  2422. if (NULL == replica_name)
  2423. return;
  2424. /*
  2425. * replica_get_by_name() will acquire the replica object
  2426. * and that could prevent the replica from being destroyed
  2427. * until the object_release is called.
  2428. */
  2429. replica_object = replica_get_by_name(replica_name);
  2430. if (NULL == replica_object)
  2431. {
  2432. return;
  2433. }
  2434. /* We have a reference, so replica won't vanish on us. */
  2435. r = (Replica *)object_get_data(replica_object);
  2436. if (NULL == r)
  2437. {
  2438. goto done;
  2439. }
  2440. replica_lock(r->repl_lock);
  2441. /* replica state is currently being updated
  2442. or no CSN was assigned - bail out */
  2443. if (r->state_update_inprogress)
  2444. {
  2445. replica_unlock(r->repl_lock);
  2446. goto done;
  2447. }
  2448. /* This might be a consumer */
  2449. if (!r->repl_csn_assigned)
  2450. {
  2451. /* EY: the consumer needs to flush ruv to disk. */
  2452. replica_unlock(r->repl_lock);
  2453. if (replica_write_ruv(r)) {
  2454. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2455. "_replica_update_state: failed write RUV for %s\n",
  2456. slapi_sdn_get_dn (r->repl_root));
  2457. }
  2458. goto done;
  2459. }
  2460. /* ONREPL update csn generator state of an updatable replica only */
  2461. /* ONREPL state always changes because we update time every second and
  2462. we write state to the disk less frequently */
  2463. rc = csngen_get_state ((CSNGen*)object_get_data (r->repl_csngen), &smod);
  2464. if (rc != 0)
  2465. {
  2466. replica_unlock(r->repl_lock);
  2467. goto done;
  2468. }
  2469. r->state_update_inprogress = PR_TRUE;
  2470. r->repl_csn_assigned = PR_FALSE;
  2471. dn = _replica_get_config_dn (r->repl_root);
  2472. if (NULL == dn) {
  2473. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2474. "replica_update_state: failed to get the config dn for %s\n",
  2475. slapi_sdn_get_dn (r->repl_root));
  2476. replica_unlock(r->repl_lock);
  2477. goto done;
  2478. }
  2479. pb = slapi_pblock_new();
  2480. mods[0] = (LDAPMod*)slapi_mod_get_ldapmod_byref(&smod);
  2481. /* we don't want to held lock during operations since it causes lock contention
  2482. and sometimes deadlock. So releasing lock here */
  2483. replica_unlock(r->repl_lock);
  2484. /* replica repl_name and new_name attributes do not get changed once
  2485. the replica is configured - so it is ok that they are outside replica lock */
  2486. /* write replica name if it has not been written before */
  2487. if (r->new_name)
  2488. {
  2489. mods[1] = &mod;
  2490. mod.mod_op = LDAP_MOD_REPLACE|LDAP_MOD_BVALUES;
  2491. mod.mod_type = (char*)attr_replicaName;
  2492. mod.mod_bvalues = vals;
  2493. vals [0] = &val;
  2494. vals [1] = NULL;
  2495. val.bv_val = r->repl_name;
  2496. val.bv_len = strlen (val.bv_val);
  2497. mods[2] = NULL;
  2498. }
  2499. else
  2500. {
  2501. mods[1] = NULL;
  2502. }
  2503. slapi_modify_internal_set_pb (pb, dn, mods, NULL, NULL,
  2504. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION), 0);
  2505. slapi_modify_internal_pb (pb);
  2506. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2507. if (rc != LDAP_SUCCESS)
  2508. {
  2509. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_update_state: "
  2510. "failed to update state of csn generator for replica %s: LDAP "
  2511. "error - %d\n", slapi_sdn_get_dn(r->repl_root), rc);
  2512. }
  2513. else
  2514. {
  2515. r->new_name = PR_FALSE;
  2516. }
  2517. /* update RUV - performs its own locking */
  2518. if (replica_write_ruv(r)) {
  2519. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2520. "_replica_update_state: failed write RUV for %s\n",
  2521. slapi_sdn_get_dn (r->repl_root));
  2522. }
  2523. /* since this is the only place this value is changed and we are
  2524. guaranteed that only one thread enters the function, its ok
  2525. to change it outside replica lock */
  2526. r->state_update_inprogress = PR_FALSE;
  2527. slapi_ch_free ((void**)&dn);
  2528. slapi_pblock_destroy (pb);
  2529. slapi_mod_done (&smod);
  2530. done:
  2531. if (replica_object)
  2532. object_release (replica_object);
  2533. }
  2534. int
  2535. replica_write_ruv (Replica *r)
  2536. {
  2537. int rc = LDAP_SUCCESS;
  2538. Slapi_Mod smod, rmod;
  2539. Slapi_Mod smod_last_modified;
  2540. LDAPMod *mods [4];
  2541. Slapi_PBlock *pb;
  2542. PR_ASSERT(r);
  2543. replica_lock(r->repl_lock);
  2544. if (!r->repl_ruv_dirty)
  2545. {
  2546. replica_unlock(r->repl_lock);
  2547. return rc;
  2548. }
  2549. PR_ASSERT (r->repl_ruv);
  2550. ruv_to_smod ((RUV*)object_get_data(r->repl_ruv), &smod);
  2551. ruv_last_modified_to_smod ((RUV*)object_get_data(r->repl_ruv), &smod_last_modified);
  2552. replica_unlock (r->repl_lock);
  2553. mods [0] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod);
  2554. mods [1] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&smod_last_modified);
  2555. if(agmt_maxcsn_to_smod(r,&rmod) == LDAP_SUCCESS){
  2556. mods [2] = (LDAPMod *)slapi_mod_get_ldapmod_byref(&rmod);
  2557. } else {
  2558. mods [2] = NULL;
  2559. }
  2560. mods [3] = NULL;
  2561. pb = slapi_pblock_new();
  2562. /* replica name never changes so it is ok to reference it outside the lock */
  2563. slapi_modify_internal_set_pb_ext(
  2564. pb,
  2565. r->repl_root, /* only used to select be */
  2566. mods,
  2567. NULL, /* controls */
  2568. RUV_STORAGE_ENTRY_UNIQUEID,
  2569. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  2570. /* Add OP_FLAG_TOMBSTONE_ENTRY so that this doesn't get logged in the Retro ChangeLog */
  2571. OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | OP_FLAG_TOMBSTONE_ENTRY |
  2572. OP_FLAG_REPL_RUV );
  2573. slapi_modify_internal_pb (pb);
  2574. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  2575. /* ruv does not exist - create one */
  2576. replica_lock(r->repl_lock);
  2577. if (rc == LDAP_SUCCESS)
  2578. {
  2579. r->repl_ruv_dirty = PR_FALSE;
  2580. }
  2581. else if (rc == LDAP_NO_SUCH_OBJECT)
  2582. {
  2583. /* this includes an internal operation - but since this only happens
  2584. during server startup - its ok that we have lock around it */
  2585. rc = _replica_configure_ruv (r, PR_TRUE);
  2586. if (rc == 0)
  2587. r->repl_ruv_dirty = PR_FALSE;
  2588. }
  2589. else /* error */
  2590. {
  2591. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2592. "replica_write_ruv: failed to update RUV tombstone for %s; "
  2593. "LDAP error - %d\n",
  2594. slapi_sdn_get_dn(r->repl_root), rc);
  2595. }
  2596. replica_unlock(r->repl_lock);
  2597. slapi_mod_done (&smod);
  2598. slapi_mod_done (&rmod);
  2599. slapi_mod_done (&smod_last_modified);
  2600. slapi_pblock_destroy (pb);
  2601. return rc;
  2602. }
  2603. /* This routine figures out if an operation is for a replicated area and if so,
  2604. * pulls out the operation CSN and returns it through the smods parameter.
  2605. * It also informs the caller of the RUV entry's unique ID, since the caller
  2606. * may not have access to the macro in repl5.h. */
  2607. int
  2608. replica_ruv_smods_for_op( Slapi_PBlock *pb, char **uniqueid, Slapi_Mods **smods )
  2609. {
  2610. Object *replica_obj;
  2611. Object *ruv_obj;
  2612. Replica *replica;
  2613. RUV *ruv;
  2614. RUV *ruv_copy;
  2615. CSN *opcsn = NULL;
  2616. Slapi_Mod smod;
  2617. Slapi_Mod smod_last_modified;
  2618. Slapi_Operation *op;
  2619. Slapi_Entry *target_entry = NULL;
  2620. int rc = 0;
  2621. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &target_entry);
  2622. if (target_entry && is_ruv_tombstone_entry(target_entry)) {
  2623. /* disallow direct modification of the RUV tombstone entry
  2624. must use the CLEANRUV task instead */
  2625. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2626. "replica_ruv_smods_for_op: attempted to directly modify the tombstone RUV "
  2627. "entry [%s] - use the CLEANALLRUV task instead\n",
  2628. slapi_entry_get_dn_const(target_entry));
  2629. return (-1);
  2630. }
  2631. replica_obj = replica_get_replica_for_op (pb);
  2632. slapi_pblock_get( pb, SLAPI_OPERATION, &op );
  2633. if (NULL != replica_obj && NULL != op) {
  2634. opcsn = operation_get_csn( op );
  2635. }
  2636. /* If the op has no CSN then it's not in a replicated area, so we're done */
  2637. if (NULL == opcsn) {
  2638. return (0);
  2639. }
  2640. replica = (Replica*)object_get_data(replica_obj);
  2641. PR_ASSERT (replica);
  2642. ruv_obj = replica_get_ruv(replica);
  2643. PR_ASSERT (ruv_obj);
  2644. ruv = (RUV*)object_get_data(ruv_obj);
  2645. PR_ASSERT (ruv);
  2646. ruv_copy = ruv_dup( ruv );
  2647. object_release (ruv_obj);
  2648. object_release (replica_obj);
  2649. rc = ruv_set_max_csn_ext( ruv_copy, opcsn, NULL, PR_TRUE );
  2650. if (rc == RUV_COVERS_CSN) { /* change would "revert" RUV - ignored */
  2651. rc = 0; /* tell caller to ignore */
  2652. } else if (rc == RUV_SUCCESS) {
  2653. rc = 1; /* tell caller success */
  2654. } else { /* error */
  2655. rc = -1; /* tell caller error */
  2656. }
  2657. if (rc == 1) {
  2658. ruv_to_smod( ruv_copy, &smod );
  2659. ruv_last_modified_to_smod( ruv_copy, &smod_last_modified );
  2660. }
  2661. ruv_destroy( &ruv_copy );
  2662. if (rc == 1) {
  2663. *smods = slapi_mods_new();
  2664. slapi_mods_add_smod(*smods, &smod);
  2665. slapi_mods_add_smod(*smods, &smod_last_modified);
  2666. *uniqueid = slapi_ch_strdup( RUV_STORAGE_ENTRY_UNIQUEID );
  2667. } else {
  2668. *smods = NULL;
  2669. *uniqueid = NULL;
  2670. }
  2671. return rc;
  2672. }
  2673. const CSN *
  2674. entry_get_deletion_csn(Slapi_Entry *e)
  2675. {
  2676. const CSN *deletion_csn = NULL;
  2677. PR_ASSERT(NULL != e);
  2678. if (NULL != e)
  2679. {
  2680. Slapi_Attr *oc_attr = NULL;
  2681. if (entry_attr_find_wsi(e, SLAPI_ATTR_OBJECTCLASS, &oc_attr) == ATTRIBUTE_PRESENT)
  2682. {
  2683. Slapi_Value *tombstone_value = NULL;
  2684. struct berval v;
  2685. v.bv_val = SLAPI_ATTR_VALUE_TOMBSTONE;
  2686. v.bv_len = strlen(SLAPI_ATTR_VALUE_TOMBSTONE);
  2687. if (attr_value_find_wsi(oc_attr, &v, &tombstone_value) == VALUE_PRESENT)
  2688. {
  2689. deletion_csn = value_get_csn(tombstone_value, CSN_TYPE_VALUE_UPDATED);
  2690. }
  2691. }
  2692. }
  2693. return deletion_csn;
  2694. }
  2695. static void
  2696. _delete_tombstone(const char *tombstone_dn, const char *uniqueid, int ext_op_flags)
  2697. {
  2698. PR_ASSERT(NULL != tombstone_dn && NULL != uniqueid);
  2699. if (NULL == tombstone_dn || NULL == uniqueid)
  2700. {
  2701. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "_delete_tombstone: "
  2702. "NULL tombstone_dn or uniqueid provided.\n");
  2703. }
  2704. else
  2705. {
  2706. int ldaprc;
  2707. Slapi_PBlock *pb = slapi_pblock_new();
  2708. slapi_delete_internal_set_pb(pb, tombstone_dn, NULL, /* controls */
  2709. uniqueid, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  2710. OP_FLAG_TOMBSTONE_ENTRY | ext_op_flags);
  2711. slapi_delete_internal_pb(pb);
  2712. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ldaprc);
  2713. if (LDAP_SUCCESS != ldaprc)
  2714. {
  2715. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2716. "_delete_tombstone: unable to delete tombstone %s, "
  2717. "uniqueid %s: %s.\n", tombstone_dn, uniqueid,
  2718. ldap_err2string(ldaprc));
  2719. }
  2720. slapi_pblock_destroy(pb);
  2721. }
  2722. }
  2723. static
  2724. void get_reap_result (int rc, void *cb_data)
  2725. {
  2726. PR_ASSERT (cb_data);
  2727. ((reap_callback_data*)cb_data)->rc = rc;
  2728. }
  2729. static int
  2730. process_reap_entry (Slapi_Entry *entry, void *cb_data)
  2731. {
  2732. char deletion_csn_str[CSN_STRSIZE];
  2733. char purge_csn_str[CSN_STRSIZE];
  2734. unsigned long *num_entriesp = &((reap_callback_data *)cb_data)->num_entries;
  2735. unsigned long *num_purged_entriesp = &((reap_callback_data *)cb_data)->num_purged_entries;
  2736. CSN *purge_csn = ((reap_callback_data *)cb_data)->purge_csn;
  2737. /* this is a pointer into the actual value in the Replica object - so that
  2738. if the value is set in the replica, we will know about it immediately */
  2739. PRBool *tombstone_reap_stop = ((reap_callback_data *)cb_data)->tombstone_reap_stop;
  2740. const CSN *deletion_csn = NULL;
  2741. int rc = -1;
  2742. /* abort reaping if we've been told to stop or we're shutting down */
  2743. if (*tombstone_reap_stop || slapi_is_shutting_down()) {
  2744. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2745. "process_reap_entry: the tombstone reap process "
  2746. " has been stopped\n");
  2747. return rc;
  2748. }
  2749. /* we only ask for the objectclass in the search - the deletion csn is in the
  2750. objectclass attribute values - if we need more attributes returned by the
  2751. search in the future, see _replica_reap_tombstones below and add more to the
  2752. attrs array */
  2753. deletion_csn = entry_get_deletion_csn(entry);
  2754. if ((NULL == deletion_csn || csn_compare(deletion_csn, purge_csn) < 0) &&
  2755. (!is_ruv_tombstone_entry(entry))) {
  2756. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  2757. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2758. "process_reap_entry: removing tombstone %s "
  2759. "because its deletion csn (%s) is less than the "
  2760. "purge csn (%s).\n",
  2761. slapi_entry_get_dn(entry),
  2762. csn_as_string(deletion_csn, PR_FALSE, deletion_csn_str),
  2763. csn_as_string(purge_csn, PR_FALSE, purge_csn_str));
  2764. }
  2765. if (slapi_entry_attr_get_ulong(entry, "tombstonenumsubordinates") < 1) {
  2766. _delete_tombstone(slapi_entry_get_dn(entry),
  2767. slapi_entry_get_uniqueid(entry), 0);
  2768. (*num_purged_entriesp)++;
  2769. }
  2770. }
  2771. else {
  2772. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  2773. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2774. "process_reap_entry: NOT removing tombstone "
  2775. "%s\n", slapi_entry_get_dn(entry));
  2776. }
  2777. }
  2778. if(!is_ruv_tombstone_entry(entry)){
  2779. /* Don't update the count for the database tombstone entry */
  2780. (*num_entriesp)++;
  2781. }
  2782. return 0;
  2783. }
  2784. /* This does the actual work of searching for tombstones and deleting them.
  2785. This must be called in a separate thread because it may take a long time.
  2786. */
  2787. static void
  2788. _replica_reap_tombstones(void *arg)
  2789. {
  2790. const char *replica_name = (const char *)arg;
  2791. Slapi_PBlock *pb = NULL;
  2792. Object *replica_object = NULL;
  2793. Replica *replica = NULL;
  2794. CSN *purge_csn = NULL;
  2795. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2796. "Info: Beginning tombstone reap for replica %s.\n",
  2797. replica_name ? replica_name : "(null)");
  2798. if (NULL == replica_name)
  2799. {
  2800. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2801. "Warning: Replica name is null in tombstone reap\n");
  2802. goto done;
  2803. }
  2804. /*
  2805. * replica_get_by_name() will acquire the replica object
  2806. * and that could prevent the replica from being destroyed
  2807. * until the object_release is called.
  2808. */
  2809. replica_object = replica_get_by_name(replica_name);
  2810. if (NULL == replica_object)
  2811. {
  2812. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2813. "Warning: Replica object %s is null in tombstone reap\n", replica_name);
  2814. goto done;
  2815. }
  2816. /* We have a reference, so replica won't vanish on us. */
  2817. replica = (Replica *)object_get_data(replica_object);
  2818. if (NULL == replica)
  2819. {
  2820. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2821. "Warning: Replica %s is null in tombstone reap\n", replica_name);
  2822. goto done;
  2823. }
  2824. if (replica->tombstone_reap_stop)
  2825. {
  2826. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2827. "Info: Replica %s reap stop flag is set for tombstone reap\n", replica_name);
  2828. goto done;
  2829. }
  2830. purge_csn = replica_get_purge_csn(replica);
  2831. if (NULL != purge_csn)
  2832. {
  2833. LDAPControl **ctrls;
  2834. reap_callback_data cb_data;
  2835. char deletion_csn_str[CSN_STRSIZE];
  2836. char tombstone_filter[128];
  2837. char **attrs = NULL;
  2838. int oprc;
  2839. if (replica_get_precise_purging(replica)){
  2840. /*
  2841. * Using precise tombstone purging. Create filter to lookup the exact
  2842. * entries that need to be purged by using a range search on the new
  2843. * tombstone csn index.
  2844. */
  2845. csn_as_string(purge_csn, PR_FALSE, deletion_csn_str);
  2846. PR_snprintf(tombstone_filter, 128,
  2847. "(&(%s<=%s)(objectclass=nsTombstone))", SLAPI_ATTR_TOMBSTONE_CSN,
  2848. csn_as_string(purge_csn, PR_FALSE, deletion_csn_str));
  2849. } else {
  2850. /* Use the old inefficient filter */
  2851. PR_snprintf(tombstone_filter, 128, "(objectclass=nsTombstone)");
  2852. }
  2853. /* we just need the objectclass - for the deletion csn
  2854. and the dn and nsuniqueid - for possible deletion
  2855. and tombstonenumsubordinates to check if it has numsubordinates
  2856. saves time to return only 3 attrs
  2857. */
  2858. charray_add(&attrs, slapi_ch_strdup("objectclass"));
  2859. charray_add(&attrs, slapi_ch_strdup("nsuniqueid"));
  2860. charray_add(&attrs, slapi_ch_strdup("tombstonenumsubordinates"));
  2861. charray_add(&attrs, slapi_ch_strdup(SLAPI_ATTR_TOMBSTONE_CSN));
  2862. ctrls = (LDAPControl **)slapi_ch_calloc (3, sizeof (LDAPControl *));
  2863. ctrls[0] = create_managedsait_control();
  2864. ctrls[1] = create_backend_control(replica->repl_root);
  2865. ctrls[2] = NULL;
  2866. pb = slapi_pblock_new();
  2867. slapi_search_internal_set_pb(pb, slapi_sdn_get_dn(replica->repl_root),
  2868. LDAP_SCOPE_SUBTREE, tombstone_filter,
  2869. attrs, 0, ctrls, NULL,
  2870. repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  2871. OP_FLAG_REVERSE_CANDIDATE_ORDER);
  2872. cb_data.rc = 0;
  2873. cb_data.num_entries = 0UL;
  2874. cb_data.num_purged_entries = 0UL;
  2875. cb_data.purge_csn = purge_csn;
  2876. /* set the cb data pointer to point to the actual memory address in
  2877. the actual Replica object - so that when the value in the Replica
  2878. is set, the reap process will know about it immediately */
  2879. cb_data.tombstone_reap_stop = &(replica->tombstone_reap_stop);
  2880. slapi_search_internal_callback_pb(pb, &cb_data /* callback data */,
  2881. get_reap_result /* result callback */,
  2882. process_reap_entry /* entry callback */,
  2883. NULL /* referral callback*/);
  2884. charray_free(attrs);
  2885. oprc = cb_data.rc;
  2886. if (LDAP_SUCCESS != oprc)
  2887. {
  2888. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2889. "_replica_reap_tombstones: failed when searching for "
  2890. "tombstones in replica %s: %s. Will try again in %ld "
  2891. "seconds.\n", slapi_sdn_get_dn(replica->repl_root),
  2892. ldap_err2string(oprc), replica->tombstone_reap_interval);
  2893. }
  2894. else
  2895. {
  2896. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2897. "_replica_reap_tombstones: purged %ld of %ld tombstones "
  2898. "in replica %s. Will try again in %ld "
  2899. "seconds.\n", cb_data.num_purged_entries, cb_data.num_entries,
  2900. slapi_sdn_get_dn(replica->repl_root),
  2901. replica->tombstone_reap_interval);
  2902. }
  2903. }
  2904. else
  2905. {
  2906. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2907. "Info: No purge CSN for tombstone reap for replica %s.\n",
  2908. replica_name);
  2909. }
  2910. done:
  2911. if (replica)
  2912. {
  2913. replica_lock(replica->repl_lock);
  2914. replica->tombstone_reap_active = PR_FALSE;
  2915. replica_unlock(replica->repl_lock);
  2916. }
  2917. if (NULL != purge_csn)
  2918. {
  2919. csn_free(&purge_csn);
  2920. }
  2921. if (NULL != pb)
  2922. {
  2923. slapi_free_search_results_internal(pb);
  2924. slapi_pblock_destroy(pb);
  2925. }
  2926. if (NULL != replica_object)
  2927. {
  2928. object_release(replica_object);
  2929. replica_object = NULL;
  2930. replica = NULL;
  2931. }
  2932. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  2933. "Info: Finished tombstone reap for replica %s.\n",
  2934. replica_name ? replica_name : "(null)");
  2935. }
  2936. /*
  2937. We don't want to run the reaper function directly from the event
  2938. queue since it may hog the event queue, starving other events.
  2939. See bug 604441
  2940. The function eq_cb_reap_tombstones will fire off the actual thread
  2941. that does the real work.
  2942. */
  2943. static void
  2944. eq_cb_reap_tombstones(time_t when, void *arg)
  2945. {
  2946. const char *replica_name = (const char *)arg;
  2947. Object *replica_object = NULL;
  2948. Replica *replica = NULL;
  2949. if (NULL != replica_name)
  2950. {
  2951. /*
  2952. * replica_get_by_name() will acquire the replica object
  2953. * and that could prevent the replica from being destroyed
  2954. * until the object_release is called.
  2955. */
  2956. replica_object = replica_get_by_name(replica_name);
  2957. if (NULL != replica_object)
  2958. {
  2959. /* We have a reference, so replica won't vanish on us. */
  2960. replica = (Replica *)object_get_data(replica_object);
  2961. if (replica)
  2962. {
  2963. replica_lock(replica->repl_lock);
  2964. /* No action if purge is disabled or the previous purge is not done yet */
  2965. if (replica->tombstone_reap_interval != 0 &&
  2966. replica->tombstone_reap_active == PR_FALSE)
  2967. {
  2968. /* set the flag here to minimize race conditions */
  2969. replica->tombstone_reap_active = PR_TRUE;
  2970. if (PR_CreateThread(PR_USER_THREAD,
  2971. _replica_reap_tombstones, (void *)replica_name,
  2972. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD,
  2973. SLAPD_DEFAULT_THREAD_STACKSIZE) == NULL)
  2974. {
  2975. replica->tombstone_reap_active = PR_FALSE;
  2976. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  2977. "Error: unable to create the tombstone reap thread for replica %s. "
  2978. "Possible system resources problem\n",
  2979. replica_name);
  2980. }
  2981. }
  2982. /* reap thread will wait until this lock is released */
  2983. replica_unlock(replica->repl_lock);
  2984. }
  2985. object_release(replica_object);
  2986. replica_object = NULL;
  2987. replica = NULL;
  2988. }
  2989. }
  2990. }
  2991. static char *
  2992. _replica_type_as_string (const Replica *r)
  2993. {
  2994. switch (r->repl_type)
  2995. {
  2996. case REPLICA_TYPE_PRIMARY: return "primary";
  2997. case REPLICA_TYPE_READONLY: return "read-only";
  2998. case REPLICA_TYPE_UPDATABLE: return "updatable";
  2999. default: return "unknown";
  3000. }
  3001. }
  3002. static const char *root_glue =
  3003. "dn: %s\n"
  3004. "objectclass: top\n"
  3005. "objectclass: nsTombstone\n"
  3006. "objectclass: extensibleobject\n"
  3007. "nsuniqueid: %s\n";
  3008. static int
  3009. replica_create_ruv_tombstone(Replica *r)
  3010. {
  3011. int return_value = LDAP_LOCAL_ERROR;
  3012. char *root_entry_str;
  3013. Slapi_Entry *e = NULL;
  3014. const char *purl = NULL;
  3015. RUV *ruv;
  3016. struct berval **bvals = NULL;
  3017. Slapi_PBlock *pb = NULL;
  3018. int rc;
  3019. PR_ASSERT(NULL != r && NULL != r->repl_root);
  3020. root_entry_str = slapi_ch_smprintf(root_glue, slapi_sdn_get_ndn(r->repl_root), RUV_STORAGE_ENTRY_UNIQUEID);
  3021. e = slapi_str2entry(root_entry_str, SLAPI_STR2ENTRY_TOMBSTONE_CHECK);
  3022. if (e == NULL)
  3023. goto done;
  3024. /* Add ruv */
  3025. if (r->repl_ruv == NULL){
  3026. CSNGen *gen;
  3027. CSN *csn;
  3028. char csnstr [CSN_STRSIZE];
  3029. /* first attempt to write RUV tombstone - need to create RUV */
  3030. gen = (CSNGen *)object_get_data(r->repl_csngen);
  3031. PR_ASSERT (gen);
  3032. if (csngen_new_csn(gen, &csn, PR_FALSE /* notify */) == CSN_SUCCESS){
  3033. (void)csn_as_string(csn, PR_FALSE, csnstr);
  3034. csn_free(&csn);
  3035. /*
  3036. * if this is an updateable replica - add its own
  3037. * element to the RUV so that referrals work correctly
  3038. */
  3039. if (r->repl_type == REPLICA_TYPE_UPDATABLE)
  3040. purl = multimaster_get_local_purl();
  3041. if (ruv_init_new(csnstr, r->repl_rid, purl, &ruv) == RUV_SUCCESS){
  3042. r->repl_ruv = object_new((void*)ruv, (FNFree)ruv_destroy);
  3043. r->repl_ruv_dirty = PR_TRUE;
  3044. return_value = LDAP_SUCCESS;
  3045. } else {
  3046. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Cannot create new replica update vector for %s\n",
  3047. slapi_sdn_get_dn(r->repl_root));
  3048. ruv_destroy(&ruv);
  3049. goto done;
  3050. }
  3051. } else {
  3052. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "Cannot obtain CSN for new replica update vector for %s\n",
  3053. slapi_sdn_get_dn(r->repl_root));
  3054. csn_free(&csn);
  3055. goto done;
  3056. }
  3057. } else { /* failed to write the entry because DB was not initialized - retry */
  3058. ruv = (RUV*) object_get_data (r->repl_ruv);
  3059. PR_ASSERT (ruv);
  3060. }
  3061. PR_ASSERT (r->repl_ruv);
  3062. rc = ruv_to_bervals(ruv, &bvals);
  3063. if (rc != RUV_SUCCESS){
  3064. goto done;
  3065. }
  3066. /* ONREPL this is depricated function but there is currently no better API to use */
  3067. rc = slapi_entry_add_values(e, type_ruvElement, bvals);
  3068. if (rc != 0){
  3069. goto done;
  3070. }
  3071. pb = slapi_pblock_new();
  3072. slapi_add_entry_internal_set_pb(pb, e, NULL /* controls */, repl_get_plugin_identity(PLUGIN_MULTIMASTER_REPLICATION),
  3073. OP_FLAG_TOMBSTONE_ENTRY | OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | OP_FLAG_REPL_RUV);
  3074. slapi_add_internal_pb(pb);
  3075. e = NULL; /* add consumes e, upon success or failure */
  3076. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &return_value);
  3077. if (return_value == LDAP_SUCCESS)
  3078. r->repl_ruv_dirty = PR_FALSE;
  3079. done:
  3080. slapi_entry_free (e);
  3081. if (bvals)
  3082. ber_bvecfree(bvals);
  3083. if (pb)
  3084. slapi_pblock_destroy(pb);
  3085. slapi_ch_free_string(&root_entry_str);
  3086. return return_value;
  3087. }
  3088. static void
  3089. assign_csn_callback(const CSN *csn, void *data)
  3090. {
  3091. Replica *r = (Replica *)data;
  3092. Object *ruv_obj;
  3093. RUV *ruv;
  3094. PR_ASSERT(NULL != csn);
  3095. PR_ASSERT(NULL != r);
  3096. ruv_obj = replica_get_ruv (r);
  3097. PR_ASSERT (ruv_obj);
  3098. ruv = (RUV*)object_get_data (ruv_obj);
  3099. PR_ASSERT (ruv);
  3100. replica_lock(r->repl_lock);
  3101. r->repl_csn_assigned = PR_TRUE;
  3102. if (NULL != r->min_csn_pl)
  3103. {
  3104. if (csnplInsert(r->min_csn_pl, csn) != 0)
  3105. {
  3106. char csn_str[CSN_STRSIZE]; /* For logging only */
  3107. /* Ack, we can't keep track of min csn. Punt. */
  3108. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  3109. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "assign_csn_callback: "
  3110. "failed to insert csn %s for replica %s\n",
  3111. csn_as_string(csn, PR_FALSE, csn_str),
  3112. slapi_sdn_get_dn(r->repl_root));
  3113. }
  3114. csnplFree(&r->min_csn_pl);
  3115. }
  3116. }
  3117. ruv_add_csn_inprogress (ruv, csn);
  3118. replica_unlock(r->repl_lock);
  3119. object_release (ruv_obj);
  3120. }
  3121. static void
  3122. abort_csn_callback(const CSN *csn, void *data)
  3123. {
  3124. Replica *r = (Replica *)data;
  3125. Object *ruv_obj;
  3126. RUV *ruv;
  3127. PR_ASSERT(NULL != csn);
  3128. PR_ASSERT(NULL != data);
  3129. ruv_obj = replica_get_ruv (r);
  3130. PR_ASSERT (ruv_obj);
  3131. ruv = (RUV*)object_get_data (ruv_obj);
  3132. PR_ASSERT (ruv);
  3133. replica_lock(r->repl_lock);
  3134. if (NULL != r->min_csn_pl)
  3135. {
  3136. int rc = csnplRemove(r->min_csn_pl, csn);
  3137. if (rc) {
  3138. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "csnplRemove failed");
  3139. return;
  3140. }
  3141. }
  3142. ruv_cancel_csn_inprogress (ruv, csn);
  3143. replica_unlock(r->repl_lock);
  3144. object_release (ruv_obj);
  3145. }
  3146. static CSN *
  3147. _replica_get_purge_csn_nolock(const Replica *r)
  3148. {
  3149. CSN *purge_csn = NULL;
  3150. CSN **csns = NULL;
  3151. RUV *ruv;
  3152. int i;
  3153. if (r->repl_purge_delay > 0)
  3154. {
  3155. /* get a sorted list of all maxcsns in ruv in ascend order */
  3156. object_acquire(r->repl_ruv);
  3157. ruv = object_get_data(r->repl_ruv);
  3158. csns = cl5BuildCSNList (ruv, NULL);
  3159. object_release(r->repl_ruv);
  3160. if (csns == NULL)
  3161. return NULL;
  3162. /* locate the most recent maxcsn in the csn list */
  3163. for (i = 0; csns[i]; i++);
  3164. purge_csn = csn_dup (csns[i-1]);
  3165. /* set purge_csn to the most recent maxcsn - purge_delay */
  3166. if ((csn_get_time(purge_csn) - r->repl_purge_delay) > 0)
  3167. csn_set_time(purge_csn, csn_get_time(purge_csn) - r->repl_purge_delay);
  3168. }
  3169. if (csns)
  3170. cl5DestroyCSNList (&csns);
  3171. return purge_csn;
  3172. }
  3173. static void
  3174. replica_get_referrals_nolock (const Replica *r, char ***referrals)
  3175. {
  3176. if(referrals!=NULL)
  3177. {
  3178. int hint;
  3179. int i= 0;
  3180. Slapi_Value *v= NULL;
  3181. if (NULL == r->repl_referral)
  3182. {
  3183. *referrals = NULL;
  3184. }
  3185. else
  3186. {
  3187. /* richm: +1 for trailing NULL */
  3188. *referrals= (char**)slapi_ch_calloc(sizeof(char*),1+slapi_valueset_count(r->repl_referral));
  3189. hint= slapi_valueset_first_value( r->repl_referral, &v );
  3190. while(v!=NULL)
  3191. {
  3192. const char *s= slapi_value_get_string(v);
  3193. if(s!=NULL && s[0]!='\0')
  3194. {
  3195. (*referrals)[i]= slapi_ch_strdup(s);
  3196. i++;
  3197. }
  3198. hint= slapi_valueset_next_value( r->repl_referral, hint, &v);
  3199. }
  3200. (*referrals)[i] = NULL;
  3201. }
  3202. }
  3203. }
  3204. static void
  3205. replica_clear_legacy_referrals(const Slapi_DN *repl_root_sdn,
  3206. char **referrals, const char *state)
  3207. {
  3208. repl_set_mtn_state_and_referrals(repl_root_sdn, state, NULL, NULL, referrals);
  3209. }
  3210. static void
  3211. replica_remove_legacy_attr (const Slapi_DN *repl_root_sdn, const char *attr)
  3212. {
  3213. Slapi_PBlock *pb;
  3214. Slapi_Mods smods;
  3215. LDAPControl **ctrls;
  3216. int rc;
  3217. pb = slapi_pblock_new ();
  3218. slapi_mods_init(&smods, 1);
  3219. slapi_mods_add(&smods, LDAP_MOD_DELETE, attr, 0, NULL);
  3220. ctrls = (LDAPControl**)slapi_ch_malloc (2 * sizeof (LDAPControl*));
  3221. ctrls[0] = create_managedsait_control ();
  3222. ctrls[1] = NULL;
  3223. /* remove copiedFrom/copyingFrom first */
  3224. slapi_modify_internal_set_pb_ext (pb, repl_root_sdn,
  3225. slapi_mods_get_ldapmods_passout (&smods),
  3226. ctrls, NULL /*uniqueid */,
  3227. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION) ,
  3228. 0 /* operation_flags */);
  3229. slapi_modify_internal_pb (pb);
  3230. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3231. if (rc != LDAP_SUCCESS)
  3232. {
  3233. /* this is not a fatal error because the attribute may not be there */
  3234. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_remove_legacy_attr: "
  3235. "failed to remove legacy attribute %s for replica %s; LDAP error - %d\n",
  3236. attr, slapi_sdn_get_dn(repl_root_sdn), rc);
  3237. }
  3238. slapi_mods_done (&smods);
  3239. slapi_pblock_destroy (pb);
  3240. }
  3241. static int
  3242. replica_log_ruv_elements_nolock (const Replica *r)
  3243. {
  3244. int rc = 0;
  3245. slapi_operation_parameters op_params;
  3246. RUV *ruv;
  3247. char *repl_gen;
  3248. CSN *csn = NULL;
  3249. ruv = (RUV*) object_get_data (r->repl_ruv);
  3250. PR_ASSERT (ruv);
  3251. if ((ruv_get_min_csn(ruv, &csn) == RUV_SUCCESS) && csn)
  3252. {
  3253. /* we log it as a delete operation to have the least number of fields
  3254. to set. the entry can be identified by a special target uniqueid and
  3255. special target dn */
  3256. memset (&op_params, 0, sizeof (op_params));
  3257. op_params.operation_type = SLAPI_OPERATION_DELETE;
  3258. op_params.target_address.sdn = slapi_sdn_new_ndn_byval(START_ITERATION_ENTRY_DN);
  3259. op_params.target_address.uniqueid = START_ITERATION_ENTRY_UNIQUEID;
  3260. op_params.csn = csn;
  3261. repl_gen = ruv_get_replica_generation (ruv);
  3262. rc = cl5WriteOperation(r->repl_name, repl_gen, &op_params, PR_FALSE);
  3263. if (rc == CL5_SUCCESS)
  3264. rc = 0;
  3265. else
  3266. rc = -1;
  3267. slapi_ch_free ((void**)&repl_gen);
  3268. slapi_sdn_free(&op_params.target_address.sdn);
  3269. csn_free (&csn);
  3270. }
  3271. return rc;
  3272. }
  3273. void
  3274. replica_set_purge_delay(Replica *r, PRUint32 purge_delay)
  3275. {
  3276. PR_ASSERT(r);
  3277. replica_lock(r->repl_lock);
  3278. r->repl_purge_delay = purge_delay;
  3279. replica_unlock(r->repl_lock);
  3280. }
  3281. void
  3282. replica_set_tombstone_reap_interval (Replica *r, long interval)
  3283. {
  3284. char *repl_name;
  3285. replica_lock(r->repl_lock);
  3286. /*
  3287. * Leave the event there to purge the existing tombstones
  3288. * if we are about to turn off tombstone creation
  3289. */
  3290. if (interval > 0 && r->repl_eqcxt_tr && r->tombstone_reap_interval != interval)
  3291. {
  3292. int found;
  3293. repl_name = slapi_eq_get_arg (r->repl_eqcxt_tr);
  3294. slapi_ch_free ((void**)&repl_name);
  3295. found = slapi_eq_cancel (r->repl_eqcxt_tr);
  3296. slapi_log_error (SLAPI_LOG_REPL, NULL,
  3297. "tombstone_reap event (interval=%ld) was %s\n",
  3298. r->tombstone_reap_interval, (found ? "cancelled" : "not found"));
  3299. r->repl_eqcxt_tr = NULL;
  3300. }
  3301. r->tombstone_reap_interval = interval;
  3302. if ( interval > 0 && r->repl_eqcxt_tr == NULL )
  3303. {
  3304. repl_name = slapi_ch_strdup (r->repl_name);
  3305. r->repl_eqcxt_tr = slapi_eq_repeat (eq_cb_reap_tombstones, repl_name,
  3306. current_time() + r->tombstone_reap_interval,
  3307. 1000 * r->tombstone_reap_interval);
  3308. slapi_log_error (SLAPI_LOG_REPL, NULL,
  3309. "tombstone_reap event (interval=%ld) was %s\n",
  3310. r->tombstone_reap_interval, (r->repl_eqcxt_tr ? "scheduled" : "not scheduled successfully"));
  3311. }
  3312. replica_unlock(r->repl_lock);
  3313. }
  3314. static void
  3315. replica_strip_cleaned_rids(Replica *r)
  3316. {
  3317. Object *RUVObj;
  3318. RUV *ruv = NULL;
  3319. ReplicaId rid[32] = {0};
  3320. int i = 0;
  3321. RUVObj = replica_get_ruv(r);
  3322. ruv = (RUV*)object_get_data (RUVObj);
  3323. ruv_get_cleaned_rids(ruv, rid);
  3324. while(rid[i] != 0){
  3325. ruv_delete_replica(ruv, rid[i]);
  3326. replica_set_ruv_dirty(r);
  3327. if (replica_write_ruv(r)) {
  3328. slapi_log_error (SLAPI_LOG_REPL, "replica_strip_cleaned_rids", "failed to write RUV\n");
  3329. }
  3330. i++;
  3331. }
  3332. object_release(RUVObj);
  3333. }
  3334. /* Update the tombstone entry to reflect the content of the ruv */
  3335. static void
  3336. replica_replace_ruv_tombstone(Replica *r)
  3337. {
  3338. Slapi_PBlock *pb = NULL;
  3339. Slapi_Mod smod;
  3340. Slapi_Mod smod_last_modified;
  3341. LDAPMod *mods [3];
  3342. char *dn;
  3343. int rc;
  3344. PR_ASSERT(NULL != r && NULL != r->repl_root);
  3345. replica_strip_cleaned_rids(r);
  3346. replica_lock(r->repl_lock);
  3347. PR_ASSERT (r->repl_ruv);
  3348. ruv_to_smod ((RUV*)object_get_data(r->repl_ruv), &smod);
  3349. ruv_last_modified_to_smod ((RUV*)object_get_data(r->repl_ruv), &smod_last_modified);
  3350. dn = _replica_get_config_dn (r->repl_root);
  3351. if (NULL == dn) {
  3352. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  3353. "replica_replace_ruv_tombstone: "
  3354. "failed to get the config dn for %s\n",
  3355. slapi_sdn_get_dn (r->repl_root));
  3356. replica_unlock(r->repl_lock);
  3357. goto bail;
  3358. }
  3359. mods[0] = (LDAPMod*)slapi_mod_get_ldapmod_byref(&smod);
  3360. mods[1] = (LDAPMod*)slapi_mod_get_ldapmod_byref(&smod_last_modified);
  3361. replica_unlock(r->repl_lock);
  3362. mods [2] = NULL;
  3363. pb = slapi_pblock_new();
  3364. slapi_modify_internal_set_pb_ext(
  3365. pb,
  3366. r->repl_root, /* only used to select be */
  3367. mods,
  3368. NULL, /* controls */
  3369. RUV_STORAGE_ENTRY_UNIQUEID,
  3370. repl_get_plugin_identity (PLUGIN_MULTIMASTER_REPLICATION),
  3371. OP_FLAG_REPLICATED | OP_FLAG_REPL_FIXUP | OP_FLAG_REPL_RUV | OP_FLAG_TOMBSTONE_ENTRY);
  3372. slapi_modify_internal_pb (pb);
  3373. slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
  3374. if (rc != LDAP_SUCCESS)
  3375. {
  3376. if ((rc != LDAP_NO_SUCH_OBJECT && rc != LDAP_TYPE_OR_VALUE_EXISTS) || !replica_is_state_flag_set(r, REPLICA_IN_USE))
  3377. {
  3378. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_replace_ruv_tombstone: "
  3379. "failed to update replication update vector for replica %s: LDAP "
  3380. "error - %d\n", (char*)slapi_sdn_get_dn (r->repl_root), rc);
  3381. }
  3382. }
  3383. slapi_ch_free ((void**)&dn);
  3384. slapi_pblock_destroy (pb);
  3385. bail:
  3386. slapi_mod_done (&smod);
  3387. slapi_mod_done (&smod_last_modified);
  3388. }
  3389. void
  3390. replica_update_ruv_consumer(Replica *r, RUV *supplier_ruv)
  3391. {
  3392. ReplicaId supplier_id = 0;
  3393. char *supplier_purl = NULL;
  3394. if ( ruv_get_first_id_and_purl(supplier_ruv, &supplier_id, &supplier_purl) == RUV_SUCCESS)
  3395. {
  3396. RUV *local_ruv = NULL;
  3397. replica_lock(r->repl_lock);
  3398. local_ruv = (RUV*)object_get_data (r->repl_ruv);
  3399. if(is_cleaned_rid(supplier_id) || local_ruv == NULL){
  3400. replica_unlock(r->repl_lock);
  3401. return;
  3402. }
  3403. if ( ruv_local_contains_supplier(local_ruv, supplier_id) == 0 )
  3404. {
  3405. if ( r->repl_type == REPLICA_TYPE_UPDATABLE )
  3406. {
  3407. /* Add the new ruv right after the consumer own purl */
  3408. ruv_add_index_replica(local_ruv, supplier_id, supplier_purl, 2);
  3409. }
  3410. else
  3411. {
  3412. /* This is a consumer only, add it first */
  3413. ruv_add_index_replica(local_ruv, supplier_id, supplier_purl, 1);
  3414. }
  3415. }
  3416. else
  3417. {
  3418. /* Replace it */
  3419. ruv_replace_replica_purl(local_ruv, supplier_id, supplier_purl);
  3420. }
  3421. replica_unlock(r->repl_lock);
  3422. /* Update also the directory entry */
  3423. replica_replace_ruv_tombstone(r);
  3424. }
  3425. }
  3426. void
  3427. replica_set_ruv_dirty(Replica *r)
  3428. {
  3429. PR_ASSERT(r);
  3430. replica_lock(r->repl_lock);
  3431. r->repl_ruv_dirty = PR_TRUE;
  3432. replica_unlock(r->repl_lock);
  3433. }
  3434. PRBool
  3435. replica_is_state_flag_set(Replica *r, PRInt32 flag)
  3436. {
  3437. PR_ASSERT(r);
  3438. if (r)
  3439. return (r->repl_state_flags & flag);
  3440. else
  3441. return PR_FALSE;
  3442. }
  3443. void
  3444. replica_set_state_flag (Replica *r, PRUint32 flag, PRBool clear)
  3445. {
  3446. if (r == NULL)
  3447. return;
  3448. replica_lock(r->repl_lock);
  3449. if (clear)
  3450. {
  3451. r->repl_state_flags &= ~flag;
  3452. }
  3453. else
  3454. {
  3455. r->repl_state_flags |= flag;
  3456. }
  3457. replica_unlock(r->repl_lock);
  3458. }
  3459. /**
  3460. * Use this to tell the tombstone reap process to stop. This will
  3461. * typically be used when we (consumer) get a request to do a
  3462. * total update.
  3463. */
  3464. void
  3465. replica_set_tombstone_reap_stop(Replica *r, PRBool val)
  3466. {
  3467. if (r == NULL)
  3468. return;
  3469. replica_lock(r->repl_lock);
  3470. r->tombstone_reap_stop = val;
  3471. replica_unlock(r->repl_lock);
  3472. }
  3473. /* replica just came back online, probably after data was reloaded */
  3474. void
  3475. replica_enable_replication (Replica *r)
  3476. {
  3477. int rc;
  3478. PR_ASSERT(r);
  3479. /* prevent creation of new agreements until the replica is enabled */
  3480. PR_Lock(r->agmt_lock);
  3481. /* retrieve new ruv */
  3482. rc = replica_reload_ruv (r);
  3483. if (rc) {
  3484. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "replica_enable_replication: "
  3485. "reloading ruv failed\n");
  3486. /* What to do ? */
  3487. }
  3488. /* Replica came back online, Check if the total update was terminated.
  3489. If flag is still set, it was not terminated, therefore the data is
  3490. very likely to be incorrect, and we should not restart Replication threads...
  3491. */
  3492. if (!replica_is_state_flag_set(r, REPLICA_TOTAL_IN_PROGRESS)){
  3493. /* restart outbound replication */
  3494. start_agreements_for_replica (r, PR_TRUE);
  3495. /* enable ruv state update */
  3496. replica_set_enabled (r, PR_TRUE);
  3497. }
  3498. /* mark the replica as being available for updates */
  3499. replica_relinquish_exclusive_access(r, 0, 0);
  3500. replica_set_state_flag(r, REPLICA_AGREEMENTS_DISABLED, PR_TRUE /* clear */);
  3501. PR_Unlock(r->agmt_lock);
  3502. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_enable_replication: "
  3503. "replica %s is relinquished\n",
  3504. slapi_sdn_get_ndn (replica_get_root (r)));
  3505. }
  3506. /* replica is about to be taken offline */
  3507. void
  3508. replica_disable_replication (Replica *r, Object *r_obj)
  3509. {
  3510. char *current_purl = NULL;
  3511. char *p_locking_purl = NULL;
  3512. char *locking_purl = NULL;
  3513. ReplicaId junkrid;
  3514. PRBool isInc = PR_FALSE; /* get exclusive access, but not for inc update */
  3515. RUV *repl_ruv = NULL;
  3516. /* prevent creation of new agreements until the replica is disabled */
  3517. PR_Lock(r->agmt_lock);
  3518. /* stop ruv update */
  3519. replica_set_enabled (r, PR_FALSE);
  3520. /* disable outbound replication */
  3521. start_agreements_for_replica (r, PR_FALSE);
  3522. /* close the corresponding changelog file */
  3523. /* close_changelog_for_replica (r_obj); */
  3524. /* mark the replica as being unavailable for updates */
  3525. /* If an incremental update is in progress, we want to wait until it is
  3526. finished until we get exclusive access to the replica, because we have
  3527. to make sure no operations are in progress - it messes up replication
  3528. when a restore is in progress but we are still adding replicated entries
  3529. from a supplier
  3530. */
  3531. repl_ruv = (RUV*) object_get_data (r->repl_ruv);
  3532. ruv_get_first_id_and_purl(repl_ruv, &junkrid, &p_locking_purl);
  3533. locking_purl = slapi_ch_strdup(p_locking_purl);
  3534. p_locking_purl = NULL;
  3535. repl_ruv = NULL;
  3536. while (!replica_get_exclusive_access(r, &isInc, 0, 0, "replica_disable_replication",
  3537. &current_purl)) {
  3538. if (!isInc) /* already locked, but not by inc update - break */
  3539. break;
  3540. isInc = PR_FALSE;
  3541. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  3542. "replica_disable_replication: "
  3543. "replica %s is already locked by (%s) for incoming "
  3544. "incremental update; sleeping 100ms\n",
  3545. slapi_sdn_get_ndn (replica_get_root (r)),
  3546. current_purl ? current_purl : "unknown");
  3547. slapi_ch_free_string(&current_purl);
  3548. DS_Sleep(PR_MillisecondsToInterval(100));
  3549. }
  3550. slapi_ch_free_string(&current_purl);
  3551. slapi_ch_free_string(&locking_purl);
  3552. replica_set_state_flag(r, REPLICA_AGREEMENTS_DISABLED, PR_FALSE);
  3553. PR_Unlock(r->agmt_lock);
  3554. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name, "replica_disable_replication: "
  3555. "replica %s is acquired\n",
  3556. slapi_sdn_get_ndn (replica_get_root (r)));
  3557. }
  3558. static void
  3559. start_agreements_for_replica (Replica *r, PRBool start)
  3560. {
  3561. Object *agmt_obj;
  3562. Repl_Agmt *agmt;
  3563. agmt_obj = agmtlist_get_first_agreement_for_replica (r);
  3564. while (agmt_obj)
  3565. {
  3566. agmt = (Repl_Agmt*)object_get_data (agmt_obj);
  3567. PR_ASSERT (agmt);
  3568. if(agmt_is_enabled(agmt)){
  3569. if (start)
  3570. agmt_start (agmt);
  3571. else /* stop */
  3572. agmt_stop (agmt);
  3573. }
  3574. agmt_obj = agmtlist_get_next_agreement_for_replica (r, agmt_obj);
  3575. }
  3576. }
  3577. int replica_start_agreement(Replica *r, Repl_Agmt *ra)
  3578. {
  3579. int ret = 0;
  3580. if (r == NULL) return -1;
  3581. PR_Lock(r->agmt_lock);
  3582. if (!replica_is_state_flag_set(r, REPLICA_AGREEMENTS_DISABLED) && agmt_is_enabled(ra)) {
  3583. ret = agmt_start(ra); /* Start the replication agreement */
  3584. }
  3585. PR_Unlock(r->agmt_lock);
  3586. return ret;
  3587. }
  3588. int windows_replica_start_agreement(Replica *r, Repl_Agmt *ra)
  3589. {
  3590. int ret = 0;
  3591. if (r == NULL) return -1;
  3592. PR_Lock(r->agmt_lock);
  3593. if (!replica_is_state_flag_set(r, REPLICA_AGREEMENTS_DISABLED)) {
  3594. ret = windows_agmt_start(ra); /* Start the replication agreement */
  3595. /* ret = windows_agmt_start(ra); Start the replication agreement */
  3596. }
  3597. PR_Unlock(r->agmt_lock);
  3598. return ret;
  3599. }
  3600. /*
  3601. * A callback function registed as op->o_csngen_handler and
  3602. * called by backend ops to generate opcsn.
  3603. */
  3604. CSN *
  3605. replica_generate_next_csn ( Slapi_PBlock *pb, const CSN *basecsn )
  3606. {
  3607. CSN *opcsn = NULL;
  3608. Object *replica_obj;
  3609. replica_obj = replica_get_replica_for_op (pb);
  3610. if (NULL != replica_obj)
  3611. {
  3612. Replica *replica = (Replica*) object_get_data (replica_obj);
  3613. if ( NULL != replica )
  3614. {
  3615. Slapi_Operation *op;
  3616. slapi_pblock_get (pb, SLAPI_OPERATION, &op);
  3617. if ( replica->repl_type != REPLICA_TYPE_READONLY ||
  3618. operation_is_flag_set (op, OP_FLAG_LEGACY_REPLICATION_DN ))
  3619. {
  3620. Object *gen_obj = replica_get_csngen (replica);
  3621. if (NULL != gen_obj)
  3622. {
  3623. CSNGen *gen = (CSNGen*) object_get_data (gen_obj);
  3624. if (NULL != gen)
  3625. {
  3626. /* The new CSN should be greater than the base CSN */
  3627. csngen_new_csn (gen, &opcsn, PR_FALSE /* don't notify */);
  3628. if (csn_compare (opcsn, basecsn) <= 0)
  3629. {
  3630. char opcsnstr[CSN_STRSIZE], basecsnstr[CSN_STRSIZE];
  3631. char opcsn2str[CSN_STRSIZE];
  3632. csn_as_string (opcsn, PR_FALSE, opcsnstr);
  3633. csn_as_string (basecsn, PR_FALSE, basecsnstr);
  3634. csn_free ( &opcsn );
  3635. csngen_adjust_time (gen, basecsn);
  3636. csngen_new_csn (gen, &opcsn, PR_FALSE /* don't notify */);
  3637. csn_as_string (opcsn, PR_FALSE, opcsn2str);
  3638. slapi_log_error (SLAPI_LOG_FATAL, NULL,
  3639. "replica_generate_next_csn: "
  3640. "opcsn=%s <= basecsn=%s, adjusted opcsn=%s\n",
  3641. opcsnstr, basecsnstr, opcsn2str);
  3642. }
  3643. /*
  3644. * Insert opcsn into the csn pending list.
  3645. * This is the notify effect in csngen_new_csn().
  3646. */
  3647. assign_csn_callback (opcsn, (void *)replica);
  3648. }
  3649. object_release (gen_obj);
  3650. }
  3651. }
  3652. }
  3653. object_release (replica_obj);
  3654. }
  3655. return opcsn;
  3656. }
  3657. /*
  3658. * A callback function registed as op->o_replica_attr_handler and
  3659. * called by backend ops to get replica attributes.
  3660. */
  3661. int
  3662. replica_get_attr ( Slapi_PBlock *pb, const char* type, void *value )
  3663. {
  3664. int rc = -1;
  3665. Object *replica_obj;
  3666. replica_obj = replica_get_replica_for_op (pb);
  3667. if (NULL != replica_obj)
  3668. {
  3669. Replica *replica = (Replica*) object_get_data (replica_obj);
  3670. if ( NULL != replica )
  3671. {
  3672. if (strcasecmp (type, type_replicaTombstonePurgeInterval) == 0)
  3673. {
  3674. *((int*)value) = replica->tombstone_reap_interval;
  3675. rc = 0;
  3676. }
  3677. else if (strcasecmp (type, type_replicaPurgeDelay) == 0)
  3678. {
  3679. *((int*)value) = replica->repl_purge_delay;
  3680. rc = 0;
  3681. }
  3682. }
  3683. object_release (replica_obj);
  3684. }
  3685. return rc;
  3686. }
  3687. PRUint64
  3688. replica_get_backoff_min(Replica *r)
  3689. {
  3690. if(r){
  3691. return slapi_counter_get_value(r->backoff_min);
  3692. } else {
  3693. return PROTOCOL_BACKOFF_MINIMUM;
  3694. }
  3695. }
  3696. PRUint64
  3697. replica_get_backoff_max(Replica *r)
  3698. {
  3699. if(r){
  3700. return slapi_counter_get_value(r->backoff_max);
  3701. } else {
  3702. return PROTOCOL_BACKOFF_MAXIMUM;
  3703. }
  3704. }
  3705. void
  3706. replica_set_backoff_min(Replica *r, PRUint64 min)
  3707. {
  3708. if(r){
  3709. slapi_counter_set_value(r->backoff_min, min);
  3710. }
  3711. }
  3712. void
  3713. replica_set_backoff_max(Replica *r, PRUint64 max)
  3714. {
  3715. if(r){
  3716. slapi_counter_set_value(r->backoff_max, max);
  3717. }
  3718. }
  3719. void
  3720. replica_set_precise_purging(Replica *r, PRUint64 on_off)
  3721. {
  3722. if(r){
  3723. slapi_counter_set_value(r->precise_purging, on_off);
  3724. }
  3725. }
  3726. PRUint64
  3727. replica_get_precise_purging(Replica *r)
  3728. {
  3729. if(r){
  3730. return slapi_counter_get_value(r->precise_purging);
  3731. } else {
  3732. return 0;
  3733. }
  3734. }
  3735. int
  3736. replica_get_agmt_count(Replica *r)
  3737. {
  3738. return r->agmt_count;
  3739. }
  3740. void
  3741. replica_incr_agmt_count(Replica *r)
  3742. {
  3743. if(r){
  3744. r->agmt_count++;
  3745. }
  3746. }
  3747. void
  3748. replica_decr_agmt_count(Replica *r)
  3749. {
  3750. if(r){
  3751. if(r->agmt_count > 0){
  3752. r->agmt_count--;
  3753. }
  3754. }
  3755. }