repl5_inc_protocol.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* repl5_inc_protocol.c */
  13. /*
  14. The Prot_Incremental object implements the DS 5.0 multi-master incremental
  15. replication protocol.
  16. Stuff to do:
  17. - Need to figure out how asynchronous events end up in here. They are:
  18. - entry updated in replicated area.
  19. - backoff timeout
  20. - enter/leave.
  21. Perhaps these events should be properties of the main protocol.
  22. */
  23. #include <plstr.h>
  24. #include "repl.h"
  25. #include "repl5.h"
  26. #include "repl5_ruv.h"
  27. #include "repl5_prot_private.h"
  28. #include "cl5_api.h"
  29. #include "repl5.h"
  30. #include "repl5_prot_private.h"
  31. #include "cl5_api.h"
  32. #include "slapi-plugin.h"
  33. extern int slapi_log_urp;
  34. /*** from proto-slap.h ***/
  35. void ava_done(struct ava *ava);
  36. typedef struct repl5_inc_private
  37. {
  38. char *ruv; /* RUV on remote replica (use diff type for this? - ggood */
  39. Backoff_Timer *backoff;
  40. Repl_Protocol *rp;
  41. PRLock *lock;
  42. PRUint32 eventbits;
  43. } repl5_inc_private;
  44. /* Structures used to communicate with the result reading thread */
  45. #ifndef UIDSTR_SIZE
  46. #define UIDSTR_SIZE 35 /* size of the string representation of the id */
  47. #endif
  48. typedef struct repl5_inc_operation
  49. {
  50. int ldap_message_id;
  51. unsigned long operation_type;
  52. char csn_str[CSN_STRSIZE];
  53. char uniqueid[UIDSTR_SIZE+1];
  54. ReplicaId replica_id;
  55. struct repl5_inc_operation *next;
  56. } repl5_inc_operation;
  57. typedef struct result_data
  58. {
  59. Private_Repl_Protocol *prp;
  60. int rc;
  61. PRLock *lock; /* Lock to protect access to this structure, the message id list and to force memory barriers */
  62. PRThread *result_tid; /* The async result thread */
  63. repl5_inc_operation *operation_list_head; /* List of IDs for outstanding operations */
  64. repl5_inc_operation *operation_list_tail; /* List of IDs for outstanding operations */
  65. int abort; /* Flag used to tell the sending thread asyncronously that it should abort (because an error came up in a result) */
  66. PRUint32 num_changes_sent;
  67. int stop_result_thread; /* Flag used to tell the result thread to exit */
  68. int last_message_id_sent;
  69. int last_message_id_received;
  70. int flowcontrol_detection;
  71. int result; /* The UPDATE_TRANSIENT_ERROR etc */
  72. int WaitForAsyncResults;
  73. time_t abort_time;
  74. } result_data;
  75. /* Various states the incremental protocol can pass through */
  76. #define STATE_START 0 /* ONREPL - should we rename this - we don't use it just to start up? */
  77. #define STATE_WAIT_WINDOW_OPEN 1
  78. #define STATE_WAIT_CHANGES 2
  79. #define STATE_READY_TO_ACQUIRE 3
  80. #define STATE_BACKOFF_START 4 /* ONREPL - can we combine BACKOFF_START and BACKOFF states? */
  81. #define STATE_BACKOFF 5
  82. #define STATE_SENDING_UPDATES 6
  83. #define STATE_STOP_FATAL_ERROR 7
  84. #define STATE_STOP_FATAL_ERROR_PART2 8
  85. #define STATE_STOP_NORMAL_TERMINATION 9
  86. /* Events (synchronous and asynchronous; these are bits) */
  87. #define EVENT_WINDOW_OPENED 1
  88. #define EVENT_WINDOW_CLOSED 2
  89. #define EVENT_TRIGGERING_CRITERIA_MET 4 /* ONREPL - should we rename this to EVENT_CHANGE_AVAILABLE */
  90. #define EVENT_BACKOFF_EXPIRED 8
  91. #define EVENT_REPLICATE_NOW 16
  92. #define EVENT_PROTOCOL_SHUTDOWN 32
  93. #define EVENT_AGMT_CHANGED 64
  94. #define UPDATE_NO_MORE_UPDATES 201
  95. #define UPDATE_TRANSIENT_ERROR 202
  96. #define UPDATE_FATAL_ERROR 203
  97. #define UPDATE_SCHEDULE_WINDOW_CLOSED 204
  98. #define UPDATE_CONNECTION_LOST 205
  99. #define UPDATE_TIMEOUT 206
  100. #define UPDATE_YIELD 207
  101. /* Return codes from examine_update_vector */
  102. #define EXAMINE_RUV_PRISTINE_REPLICA 401
  103. #define EXAMINE_RUV_GENERATION_MISMATCH 402
  104. #define EXAMINE_RUV_REPLICA_TOO_OLD 403
  105. #define EXAMINE_RUV_OK 404
  106. #define EXAMINE_RUV_PARAM_ERROR 405
  107. #define MAX_CHANGES_PER_SESSION 10000
  108. /*
  109. * Maximum time to wait between replication sessions. If we
  110. * don't see any updates for a period equal to this interval,
  111. * we go ahead and start a replication session, just to be safe
  112. */
  113. #define MAX_WAIT_BETWEEN_SESSIONS PR_SecondsToInterval(60 * 5) /* 5 minutes */
  114. /*
  115. * tests if the protocol has been shutdown and we need to quit
  116. * event_occurred resets the bits in the bit flag, so whoever tests for shutdown
  117. * resets the flags, so the next one who tests for shutdown won't get it, so we
  118. * also look at the terminate flag
  119. */
  120. #define PROTOCOL_IS_SHUTDOWN(prp) (event_occurred(prp, EVENT_PROTOCOL_SHUTDOWN) || prp->terminate)
  121. /* mods should be LDAPMod **mods */
  122. #define MODS_ARE_EMPTY(mods) ((mods == NULL) || (mods[0] == NULL))
  123. /* Forward declarations */
  124. static PRUint32 event_occurred(Private_Repl_Protocol *prp, PRUint32 event);
  125. static void reset_events (Private_Repl_Protocol *prp);
  126. static void protocol_sleep(Private_Repl_Protocol *prp, PRIntervalTime duration);
  127. static int send_updates(Private_Repl_Protocol *prp, RUV *ruv, PRUint32 *num_changes_sent);
  128. static void repl5_inc_backoff_expired(time_t timer_fire_time, void *arg);
  129. static int examine_update_vector(Private_Repl_Protocol *prp, RUV *ruv);
  130. static const char* state2name (int state);
  131. static const char* event2name (int event);
  132. static const char* op2string (int op);
  133. static int repl5_inc_update_from_op_result(Private_Repl_Protocol *prp, ConnResult replay_crc, int connection_error, char *csn_str, char *uniqueid, ReplicaId replica_id, int* finished, PRUint32 *num_changes_sent);
  134. /* Push a newly sent operation onto the tail of the list */
  135. static void repl5_int_push_operation(result_data *rd, repl5_inc_operation *it)
  136. {
  137. repl5_inc_operation *tail = NULL;
  138. PR_Lock(rd->lock);
  139. tail = rd->operation_list_tail;
  140. if (tail)
  141. {
  142. tail->next = it;
  143. }
  144. if (NULL == rd->operation_list_head)
  145. {
  146. rd->operation_list_head = it;
  147. }
  148. rd->operation_list_tail = it;
  149. PR_Unlock(rd->lock);
  150. }
  151. /* Pop the next operation in line to respond from the list */
  152. /* The caller is expected to free the operation item */
  153. static repl5_inc_operation *repl5_inc_pop_operation(result_data *rd)
  154. {
  155. repl5_inc_operation *head = NULL;
  156. repl5_inc_operation *ret = NULL;
  157. PR_Lock(rd->lock);
  158. head = rd->operation_list_head;
  159. if (head)
  160. {
  161. ret = head;
  162. rd->operation_list_head = head->next;
  163. if (rd->operation_list_tail == head)
  164. {
  165. rd->operation_list_tail = NULL;
  166. }
  167. }
  168. PR_Unlock(rd->lock);
  169. return ret;
  170. }
  171. static void
  172. repl5_inc_op_free(repl5_inc_operation *op)
  173. {
  174. slapi_ch_free((void**)&op);
  175. }
  176. static repl5_inc_operation *repl5_inc_operation_new(void)
  177. {
  178. repl5_inc_operation *ret = NULL;
  179. ret = (repl5_inc_operation *) slapi_ch_calloc(1,sizeof(repl5_inc_operation));
  180. return ret;
  181. }
  182. /* Called when in compatibility mode, to get the next result from the wire
  183. * The operation thread will not send a second operation until it has read the
  184. * previous result. */
  185. static int
  186. repl5_inc_get_next_result(result_data *rd)
  187. {
  188. ConnResult conres = 0;
  189. int message_id = 0;
  190. /* Wait on the next result */
  191. conres = conn_read_result(rd->prp->conn, &message_id);
  192. /* Return it to the caller */
  193. return conres;
  194. }
  195. #if NEEDED_FOR_DEBUGGING
  196. static void
  197. repl5_inc_log_operation_failure(int operation_code, int ldap_error, char* ldap_error_string, const char *agreement_name)
  198. {
  199. char *op_string = slapi_op_type_to_string(operation_code);
  200. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  201. "%s: Received error %d: %s for %s operation\n",
  202. agreement_name,
  203. ldap_error, ldap_error_string ? ldap_error_string : "NULL",
  204. op_string ? op_string : "NULL");
  205. }
  206. #endif
  207. /* Thread that collects results from async operations sent to the consumer */
  208. static void
  209. repl5_inc_result_threadmain(void *param)
  210. {
  211. result_data *rd = (result_data*) param;
  212. ConnResult conres = 0;
  213. Repl_Connection *conn = rd->prp->conn;
  214. int finished = 0;
  215. int message_id = 0;
  216. int yield_session = 0;
  217. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, NULL, "repl5_inc_result_threadmain starting\n");
  218. while (!finished)
  219. {
  220. LDAPControl **returned_controls = NULL;
  221. repl5_inc_operation *op = NULL;
  222. ReplicaId replica_id = 0;
  223. char *csn_str = NULL;
  224. char *uniqueid = NULL;
  225. char *ldap_error_string = NULL;
  226. time_t time_now = 0;
  227. time_t start_time = time( NULL );
  228. int connection_error = 0;
  229. int operation_code = 0;
  230. int backoff_time = 1;
  231. /* Read the next result */
  232. /* We call the get result function with a short timeout (non-blocking)
  233. * this is so we don't block here forever, and can stop this thread when
  234. * the time comes. However, we do need to implement blocking with timeout
  235. * semantics here instead.
  236. */
  237. while (!finished)
  238. {
  239. conres = conn_read_result_ex(conn, NULL, NULL, &returned_controls, LDAP_RES_ANY, &message_id, 0);
  240. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, NULL, "repl5_inc_result_threadmain: read result for message_id %d\n", message_id);
  241. /* Timeout here means that we didn't block, not a real timeout */
  242. if (CONN_TIMEOUT == conres)
  243. {
  244. /* We need to a) check that the 'real' timeout hasn't expired and
  245. * b) implement a backoff sleep to avoid spinning */
  246. /* Did the connection's timeout expire ? */
  247. time_now = time( NULL );
  248. if (conn_get_timeout(conn) <= ( time_now - start_time ))
  249. {
  250. /* We timed out */
  251. conres = CONN_TIMEOUT;
  252. break;
  253. }
  254. /* Otherwise we backoff */
  255. DS_Sleep(PR_MillisecondsToInterval(backoff_time));
  256. if (backoff_time < 1000)
  257. {
  258. backoff_time <<= 1;
  259. }
  260. /* Should we stop ? */
  261. PR_Lock(rd->lock);
  262. if (rd->stop_result_thread)
  263. {
  264. finished = 1;
  265. }
  266. PR_Unlock(rd->lock);
  267. } else {
  268. /*
  269. * Something other than a timeout, so we exit the loop.
  270. * First check if we were told to abort the session
  271. */;
  272. Replica *r = (Replica*)object_get_data(rd->prp->replica_object);
  273. if (replica_get_release_timeout(r) &&
  274. slapi_control_present(returned_controls,
  275. REPL_ABORT_SESSION_OID,
  276. NULL, NULL))
  277. {
  278. yield_session = 1;
  279. }
  280. break;
  281. }
  282. }
  283. if (conres != CONN_TIMEOUT)
  284. {
  285. int return_value;
  286. int should_finish = 0;
  287. if (message_id)
  288. {
  289. rd->last_message_id_received = message_id;
  290. }
  291. /* Handle any error etc */
  292. /* Get the stored operation details from the queue, unless we timed out... */
  293. op = repl5_inc_pop_operation(rd);
  294. if (op)
  295. {
  296. csn_str = op->csn_str;
  297. replica_id = op->replica_id;
  298. uniqueid = op->uniqueid;
  299. }
  300. conn_get_error_ex(conn, &operation_code, &connection_error, &ldap_error_string);
  301. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, NULL,
  302. "repl5_inc_result_threadmain: result %d, %d, %d, %d, %s\n",
  303. operation_code,connection_error,conres,message_id,ldap_error_string);
  304. return_value = repl5_inc_update_from_op_result(rd->prp, conres, connection_error,
  305. csn_str, uniqueid, replica_id, &should_finish,
  306. &(rd->num_changes_sent));
  307. if (return_value || should_finish)
  308. {
  309. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, NULL,
  310. "repl5_inc_result_threadmain: got op result %d should finish %d\n",
  311. return_value, should_finish);
  312. /* If so then we need to take steps to abort the update process */
  313. PR_Lock(rd->lock);
  314. rd->result = return_value;
  315. rd->abort = ABORT_SESSION;
  316. PR_Unlock(rd->lock);
  317. /*
  318. * We also need to log the error, including details stored from
  319. * when the operation was sent. We cannot finish yet - we still
  320. * need to wait for the pending results, then the main repl code
  321. * will shut down this thread. We can finish if we have
  322. * disconnected - in that case, there will be nothing to read
  323. */
  324. if (return_value == UPDATE_CONNECTION_LOST) {
  325. finished = 1;
  326. }
  327. } else {
  328. /* old semantics had result set outside of lock */
  329. rd->result = return_value;
  330. }
  331. }
  332. /* Should we stop ? */
  333. PR_Lock(rd->lock);
  334. if (!finished && yield_session && rd->abort != SESSION_ABORTED && rd->abort_time == 0) {
  335. rd->abort_time = time( NULL );
  336. rd->abort = SESSION_ABORTED; /* only set the abort time once */
  337. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, "repl5_inc_result_threadmain",
  338. "Abort control detected, setting abort time...(%s)\n",
  339. agmt_get_long_name(rd->prp->agmt));
  340. }
  341. if (rd->stop_result_thread)
  342. {
  343. finished = 1;
  344. }
  345. PR_Unlock(rd->lock);
  346. if (op)
  347. {
  348. repl5_inc_op_free(op);
  349. }
  350. }
  351. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, NULL, "repl5_inc_result_threadmain exiting\n");
  352. }
  353. static result_data *
  354. repl5_inc_rd_new(Private_Repl_Protocol *prp)
  355. {
  356. result_data *res = NULL;
  357. res = (result_data *) slapi_ch_calloc(1,sizeof(result_data));
  358. if (res) {
  359. res->prp = prp;
  360. res->lock = PR_NewLock();
  361. if (NULL == res->lock) {
  362. slapi_ch_free((void **)&res);
  363. res = NULL;
  364. }
  365. }
  366. return res;
  367. }
  368. static void
  369. repl5_inc_rd_list_destroy(repl5_inc_operation *op)
  370. {
  371. while (op) {
  372. repl5_inc_operation *next = op->next;
  373. repl5_inc_op_free(op);
  374. op = next;
  375. }
  376. }
  377. static void
  378. repl5_inc_rd_destroy(result_data **pres)
  379. {
  380. result_data *res = *pres;
  381. if (res->lock) {
  382. PR_DestroyLock(res->lock);
  383. }
  384. /* Delete the linked list if we have one */
  385. /* Begin at the head */
  386. repl5_inc_rd_list_destroy(res->operation_list_head);
  387. slapi_ch_free((void **)pres);
  388. }
  389. static int
  390. repl5_inc_create_async_result_thread(result_data *rd)
  391. {
  392. int retval = 0;
  393. PRThread *tid = NULL;
  394. /* Create a thread that reads results from the connection and stores status in the callback_data structure */
  395. tid = PR_CreateThread(PR_USER_THREAD,
  396. repl5_inc_result_threadmain, (void*)rd,
  397. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD,
  398. SLAPD_DEFAULT_THREAD_STACKSIZE);
  399. if (NULL == tid)
  400. {
  401. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, NULL,
  402. "repl5_tot_create_async_result_thread failed. "
  403. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  404. PR_GetError(), slapd_pr_strerror( PR_GetError() ));
  405. retval = -1;
  406. } else {
  407. rd->result_tid = tid;
  408. }
  409. return retval;
  410. }
  411. static int
  412. repl5_inc_destroy_async_result_thread(result_data *rd)
  413. {
  414. int retval = 0;
  415. PRThread *tid = rd->result_tid;
  416. if (tid) {
  417. PR_Lock(rd->lock);
  418. rd->stop_result_thread = 1;
  419. PR_Unlock(rd->lock);
  420. (void)PR_JoinThread(tid);
  421. }
  422. return retval;
  423. }
  424. /* The interest of this routine is to give time to the consumer
  425. * to apply the sent updates and return the acks.
  426. * So the caller should not hold the replication connection lock
  427. * to let the RA.reader receives the acks.
  428. */
  429. static void
  430. repl5_inc_flow_control_results(Repl_Agmt *agmt, result_data *rd)
  431. {
  432. PR_Lock(rd->lock);
  433. if ((rd->last_message_id_received <= rd->last_message_id_sent) &&
  434. ((rd->last_message_id_sent - rd->last_message_id_received) >= agmt_get_flowcontrolwindow(agmt))) {
  435. rd->flowcontrol_detection++;
  436. PR_Unlock(rd->lock);
  437. DS_Sleep(PR_MillisecondsToInterval(agmt_get_flowcontrolpause(agmt)));
  438. } else {
  439. PR_Unlock(rd->lock);
  440. }
  441. }
  442. static int
  443. repl5_inc_waitfor_async_results(result_data *rd)
  444. {
  445. int done = 0;
  446. int loops = 0;
  447. int rc = UPDATE_NO_MORE_UPDATES;
  448. /* Keep pulling results off the LDAP connection until we catch up to the last message id stored in the rd */
  449. while (!done && !slapi_is_shutting_down())
  450. {
  451. /* Lock the structure to force memory barrier */
  452. PR_Lock(rd->lock);
  453. /* Are we caught up ? */
  454. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, NULL,
  455. "repl5_inc_waitfor_async_results: %d %d\n",
  456. rd->last_message_id_received, rd->last_message_id_sent);
  457. if (rd->last_message_id_received >= rd->last_message_id_sent) {
  458. /* If so then we're done */
  459. done = 1;
  460. } else if (rd->abort && (rd->result == UPDATE_CONNECTION_LOST))
  461. {
  462. done = 1; /* no connection == no more results */
  463. }
  464. /*
  465. * Return the last operation result
  466. */
  467. rc = rd->result;
  468. PR_Unlock(rd->lock);
  469. if (!done) {
  470. /* If not then sleep a bit */
  471. DS_Sleep(PR_MillisecondsToInterval(rd->WaitForAsyncResults));
  472. }
  473. loops++;
  474. /* If we sleep forever then we can conclude that something bad happened, and bail... */
  475. /* Arbitrary 30 second delay : basically we should only expect to wait as long as it takes to process a few operations, which should be on the order of a second at most */
  476. if (!done && (loops > 300))
  477. {
  478. /* Log a warning */
  479. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, NULL,
  480. "repl5_inc_waitfor_async_results timed out waiting for responses: %d %d\n",
  481. rd->last_message_id_received, rd->last_message_id_sent);
  482. done = 1;
  483. }
  484. }
  485. return rc;
  486. }
  487. /*
  488. * It's specifically ok to delete a protocol instance that
  489. * is currently running. The instance will be shut down, and
  490. * then resources will be freed. Since a graceful shutdown is
  491. * attempted, this function may take some time to complete.
  492. */
  493. static void
  494. repl5_inc_delete(Private_Repl_Protocol **prpp)
  495. {
  496. repl5_inc_private *prp_priv = (repl5_inc_private *)(*prpp)->private;
  497. /* if backoff is set, delete it (from EQ, as well) */
  498. if (prp_priv->backoff) {
  499. backoff_delete(&prp_priv->backoff);
  500. }
  501. /* First, stop the protocol if it isn't already stopped */
  502. if (!(*prpp)->stopped) {
  503. (*prpp)->stopped = 1;
  504. (*prpp)->stop(*prpp);
  505. }
  506. /* Then, delete all resources used by the protocol */
  507. if ((*prpp)->lock) {
  508. PR_DestroyLock((*prpp)->lock);
  509. (*prpp)->lock = NULL;
  510. }
  511. if ((*prpp)->cvar) {
  512. PR_DestroyCondVar((*prpp)->cvar);
  513. (*prpp)->cvar = NULL;
  514. }
  515. slapi_ch_free((void **)&(*prpp)->private);
  516. slapi_ch_free((void **)prpp);
  517. }
  518. /* helper function */
  519. void
  520. set_pause_and_busy_time(Private_Repl_Protocol *prp, long *pausetime, long *busywaittime)
  521. {
  522. /* If neither are set, set busy time to its default */
  523. if (!*pausetime && !*busywaittime)
  524. {
  525. *busywaittime = repl5_get_backoff_min(prp);
  526. }
  527. /* pause time must be at least 1 more than the busy backoff time */
  528. if (*pausetime && !*busywaittime)
  529. {
  530. /*
  531. * user specified a pause time but no busy wait time - must
  532. * set busy wait time to 1 less than pause time - if pause
  533. * time is 1, we must set it to 2
  534. */
  535. if (*pausetime < 2)
  536. {
  537. *pausetime = 2;
  538. }
  539. *busywaittime = *pausetime - 1;
  540. }
  541. else if (!*pausetime && *busywaittime)
  542. {
  543. /*
  544. * user specified a busy wait time but no pause time - must
  545. * set pause time to 1 more than busy wait time
  546. */
  547. *pausetime = *busywaittime + 1;
  548. }
  549. else if (*pausetime && *busywaittime && *pausetime <= *busywaittime)
  550. {
  551. /*
  552. * user specified both pause and busy wait times, but the pause
  553. * time was <= busy wait time - pause time must be at least
  554. * 1 more than the busy wait time
  555. */
  556. *pausetime = *busywaittime + 1;
  557. }
  558. }
  559. /*
  560. * Do the incremental protocol.
  561. *
  562. * What's going on here? This thing is a state machine. It has the
  563. * following states:
  564. *
  565. * State transition table:
  566. *
  567. * Curr State Condition/Event Next State
  568. * ---------- ------------ -----------
  569. * START schedule window is open ACQUIRE_REPLICA
  570. * schedule window is closed WAIT_WINDOW_OPEN
  571. * WAIT_WINDOW_OPEN schedule change START
  572. * replicate now ACQUIRE_REPLICA
  573. * schedule window opens ACQUIRE_REPLICA
  574. * ACQUIRE_REPLICA acquired replica SEND_CHANGES
  575. * failed to acquire - transient error START_BACKOFF
  576. * failed to acquire - fatal error STOP_FATAL_ERROR
  577. * SEND_CHANGES can't update CONSUMER_NEEDS_REINIT
  578. * no changes to send WAIT_CHANGES
  579. * can't send - thransient error START_BACKOF
  580. * can't send - window closed WAIT_WINDOW_OPEN
  581. * can'r send - fatal error STOP_FATAL_ERROR
  582. * START_BACKOF replicate now ACQUIRE_REPLICA
  583. * schedule changes START
  584. * schedule window closes WAIT_WINDOW_OPEN
  585. * backoff expires & can acquire SEND_CHANGES
  586. * backoff expires & can't acquire-trans BACKOFF
  587. * backoff expires & can't acquire-fatal STOP_FATAL_ERROR
  588. * BACKOF replicate now ACQUIRE_REPLICA
  589. * schedule changes START
  590. * schedule window closes WAIT_WINDOW_OPEN
  591. * backoff expires & can acquire SEND_CHANGES
  592. * backoff expires & can't acquire-trans BACKOFF
  593. * backoff expires & can't acquire-fatal STOP_FATAL_ERROR
  594. * WAIT_CHANGES schedule window closes WAIT_WINDOW_OPEN
  595. * replicate_now ACQUIRE_REPLICA
  596. * change available ACQUIRE_REPLICA
  597. * schedule_change START
  598. */
  599. /*
  600. * Main state machine for the incremental protocol. This routine will,
  601. * under normal circumstances, not return until the protocol is shut
  602. * down.
  603. */
  604. static void
  605. repl5_inc_run(Private_Repl_Protocol *prp)
  606. {
  607. repl5_inc_private *prp_priv = (repl5_inc_private *)prp->private;
  608. Replica *replica = NULL;
  609. CSN *cons_schema_csn;
  610. RUV *ruv = NULL;
  611. PRUint32 num_changes_sent;
  612. /* use a different backoff timer strategy for ACQUIRE_REPLICA_BUSY errors */
  613. PRBool use_busy_backoff_timer = PR_FALSE;
  614. time_t next_fire_time;
  615. time_t now;
  616. long busywaittime = 0;
  617. long pausetime = 0;
  618. long loops = 0;
  619. int wait_change_timer_set = 0;
  620. int current_state = STATE_START;
  621. int next_state = STATE_START;
  622. int done;
  623. int e1;
  624. prp->stopped = 0;
  625. prp->terminate = 0;
  626. /* establish_protocol_callbacks(prp); */
  627. done = 0;
  628. do {
  629. int rc;
  630. /* Take action, based on current state, and compute new state. */
  631. switch (current_state)
  632. {
  633. case STATE_START:
  634. dev_debug("repl5_inc_run(STATE_START)");
  635. if (PROTOCOL_IS_SHUTDOWN(prp)){
  636. done = 1;
  637. break;
  638. }
  639. /*
  640. * Our initial state. See if we're in a schedule window. If
  641. * so, then we're ready to acquire the replica and see if it
  642. * needs any updates from us. If not, then wait for the window
  643. * to open.
  644. */
  645. if (agmt_schedule_in_window_now(prp->agmt)){
  646. next_state = STATE_READY_TO_ACQUIRE;
  647. } else {
  648. next_state = STATE_WAIT_WINDOW_OPEN;
  649. }
  650. /* we can get here from other states because some events happened and were
  651. * not cleared. For instance when we wake up in STATE_WAIT_CHANGES state.
  652. * Since this is a fresh start state, we should clear all events */
  653. /* ONREPL - this does not feel right - we should take another look
  654. * at this state machine */
  655. reset_events (prp);
  656. /* Cancel any linger timer that might be in effect... */
  657. conn_cancel_linger(prp->conn);
  658. /* ... and disconnect, if currently connected */
  659. conn_disconnect(prp->conn);
  660. /* get the new pause time, if any */
  661. pausetime = agmt_get_pausetime(prp->agmt);
  662. /* get the new busy wait time, if any */
  663. busywaittime = agmt_get_busywaittime(prp->agmt);
  664. if (pausetime || busywaittime){
  665. /* helper function to make sure they are set correctly */
  666. set_pause_and_busy_time(prp, &pausetime, &busywaittime);
  667. }
  668. break;
  669. case STATE_WAIT_WINDOW_OPEN:
  670. /*
  671. * We're waiting for a schedule window to open. If one did,
  672. * or we receive a "replicate now" event, then start a protocol
  673. * session immediately. If the replication schedule changed, go
  674. * back to start. Otherwise, go back to sleep.
  675. */
  676. dev_debug("repl5_inc_run(STATE_WAIT_WINDOW_OPEN)");
  677. if (PROTOCOL_IS_SHUTDOWN(prp)){
  678. done = 1;
  679. break;
  680. } else if (event_occurred(prp, EVENT_WINDOW_OPENED)){
  681. next_state = STATE_READY_TO_ACQUIRE;
  682. } else if (event_occurred(prp, EVENT_REPLICATE_NOW)){
  683. next_state = STATE_READY_TO_ACQUIRE;
  684. } else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  685. next_state = STATE_START;
  686. conn_set_agmt_changed(prp->conn);
  687. } else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET)){ /* change available */
  688. /* just ignore it and go to sleep */
  689. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  690. } else if ((e1 = event_occurred(prp, EVENT_WINDOW_CLOSED)) ||
  691. event_occurred(prp, EVENT_BACKOFF_EXPIRED)){
  692. /* this events - should not occur - log a warning and go to sleep */
  693. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  694. "%s: Incremental protocol: "
  695. "event %s should not occur in state %s; going to sleep\n",
  696. agmt_get_long_name(prp->agmt), e1 ? event2name(EVENT_WINDOW_CLOSED) :
  697. event2name(EVENT_BACKOFF_EXPIRED), state2name(current_state));
  698. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  699. } else {
  700. /* wait until window opens or an event occurs */
  701. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  702. "%s: Incremental protocol: "
  703. "waiting for update window to open\n", agmt_get_long_name(prp->agmt));
  704. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  705. }
  706. break;
  707. case STATE_WAIT_CHANGES:
  708. /*
  709. * We're in a replication window, but we're waiting for more
  710. * changes to accumulate before we actually hook up and send
  711. * them.
  712. */
  713. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES)");
  714. if (PROTOCOL_IS_SHUTDOWN(prp)){
  715. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): PROTOCOL_IS_SHUTING_DOWN -> end repl5_inc_run\n");
  716. done = 1;
  717. break;
  718. } else if (event_occurred(prp, EVENT_REPLICATE_NOW)){
  719. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_REPLICATE_NOW received -> STATE_READY_TO_ACQUIRE\n");
  720. next_state = STATE_READY_TO_ACQUIRE;
  721. wait_change_timer_set = 0;
  722. } else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  723. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_AGMT_CHANGED received -> STATE_START\n");
  724. next_state = STATE_START;
  725. conn_set_agmt_changed(prp->conn);
  726. wait_change_timer_set = 0;
  727. } else if (event_occurred(prp, EVENT_WINDOW_CLOSED)){
  728. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_WINDOW_CLOSED received -> STATE_WAIT_WINDOW_OPEN\n");
  729. next_state = STATE_WAIT_WINDOW_OPEN;
  730. wait_change_timer_set = 0;
  731. } else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET)){
  732. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_TRIGGERING_CRITERIA_MET received -> STATE_READY_TO_ACQUIRE\n");
  733. next_state = STATE_READY_TO_ACQUIRE;
  734. wait_change_timer_set = 0;
  735. } else if ((e1 = event_occurred(prp, EVENT_WINDOW_OPENED)) || event_occurred(prp, EVENT_BACKOFF_EXPIRED)){
  736. /* this events - should not occur - log a warning and clear the event */
  737. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name, "%s: Incremental protocol: "
  738. "event %s should not occur in state %s\n",agmt_get_long_name(prp->agmt),
  739. e1 ? event2name(EVENT_WINDOW_OPENED) : event2name(EVENT_BACKOFF_EXPIRED),
  740. state2name(current_state));
  741. wait_change_timer_set = 0;
  742. } else {
  743. if (wait_change_timer_set){
  744. /* We are here because our timer expired */
  745. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): wait_change_timer_set expired -> STATE_START\n");
  746. next_state = STATE_START;
  747. wait_change_timer_set = 0;
  748. } else {
  749. /*
  750. * We are here because the last replication session
  751. * finished or aborted.
  752. */
  753. wait_change_timer_set = 1;
  754. protocol_sleep(prp, MAX_WAIT_BETWEEN_SESSIONS);
  755. }
  756. }
  757. break;
  758. case STATE_READY_TO_ACQUIRE:
  759. dev_debug("repl5_inc_run(STATE_READY_TO_ACQUIRE)");
  760. if (PROTOCOL_IS_SHUTDOWN(prp)){
  761. done = 1;
  762. break;
  763. }
  764. /* ONREPL - at this state we unconditionally acquire the replica
  765. ignoring all events. Not sure if this is good */
  766. object_acquire(prp->replica_object);
  767. rc = acquire_replica(prp, REPL_NSDS50_INCREMENTAL_PROTOCOL_OID, &ruv);
  768. use_busy_backoff_timer = PR_FALSE; /* default */
  769. if (rc == ACQUIRE_SUCCESS){
  770. next_state = STATE_SENDING_UPDATES;
  771. } else if (rc == ACQUIRE_REPLICA_BUSY){
  772. next_state = STATE_BACKOFF_START;
  773. use_busy_backoff_timer = PR_TRUE;
  774. } else if (rc == ACQUIRE_CONSUMER_WAS_UPTODATE){
  775. next_state = STATE_WAIT_CHANGES;
  776. } else if (rc == ACQUIRE_TRANSIENT_ERROR){
  777. next_state = STATE_BACKOFF_START;
  778. } else if (rc == ACQUIRE_FATAL_ERROR){
  779. next_state = STATE_STOP_FATAL_ERROR;
  780. }
  781. object_release(prp->replica_object);
  782. break;
  783. case STATE_BACKOFF_START:
  784. dev_debug("repl5_inc_run(STATE_BACKOFF_START)");
  785. if (PROTOCOL_IS_SHUTDOWN(prp)){
  786. done = 1;
  787. break;
  788. }
  789. if (event_occurred(prp, EVENT_REPLICATE_NOW)){
  790. next_state = STATE_READY_TO_ACQUIRE;
  791. } else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  792. next_state = STATE_START;
  793. conn_set_agmt_changed(prp->conn);
  794. } else if (event_occurred (prp, EVENT_WINDOW_CLOSED)){
  795. next_state = STATE_WAIT_WINDOW_OPEN;
  796. } else if (event_occurred (prp, EVENT_TRIGGERING_CRITERIA_MET)){
  797. /* consume and ignore */
  798. } else if ((e1 = event_occurred (prp, EVENT_WINDOW_OPENED)) ||
  799. event_occurred (prp, EVENT_BACKOFF_EXPIRED)){
  800. /* This should never happen */
  801. /* this events - should not occur - log a warning and go to sleep */
  802. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  803. "%s: Incremental protocol: event %s should not occur in state %s\n",
  804. agmt_get_long_name(prp->agmt),
  805. e1 ? event2name(EVENT_WINDOW_OPENED) : event2name(EVENT_BACKOFF_EXPIRED),
  806. state2name(current_state));
  807. } else {
  808. /* Set up the backoff timer to wake us up at the appropriate time */
  809. /* if previous backoff set up, delete it. */
  810. if (prp_priv->backoff) {
  811. backoff_delete(&prp_priv->backoff);
  812. }
  813. if (use_busy_backoff_timer){
  814. /* we received a busy signal from the consumer, wait for a while */
  815. if (!busywaittime){
  816. busywaittime = repl5_get_backoff_min(prp);
  817. }
  818. prp_priv->backoff = backoff_new(BACKOFF_FIXED, busywaittime , busywaittime);
  819. } else {
  820. prp_priv->backoff = backoff_new(BACKOFF_EXPONENTIAL, repl5_get_backoff_min(prp),
  821. repl5_get_backoff_max(prp));
  822. }
  823. next_state = STATE_BACKOFF;
  824. backoff_reset(prp_priv->backoff, repl5_inc_backoff_expired, (void *)prp);
  825. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  826. use_busy_backoff_timer = PR_FALSE;
  827. }
  828. break;
  829. case STATE_BACKOFF:
  830. /*
  831. * We're in a backoff state.
  832. */
  833. dev_debug("repl5_inc_run(STATE_BACKOFF)");
  834. if (PROTOCOL_IS_SHUTDOWN(prp)){
  835. if (prp_priv->backoff)
  836. backoff_delete(&prp_priv->backoff);
  837. done = 1;
  838. break;
  839. } else if (event_occurred(prp, EVENT_REPLICATE_NOW)){
  840. next_state = STATE_READY_TO_ACQUIRE;
  841. } else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  842. next_state = STATE_START;
  843. conn_set_agmt_changed(prp->conn);
  844. /* Destroy the backoff timer, since we won't need it anymore */
  845. if (prp_priv->backoff)
  846. backoff_delete(&prp_priv->backoff);
  847. } else if (event_occurred(prp, EVENT_WINDOW_CLOSED)){
  848. next_state = STATE_WAIT_WINDOW_OPEN;
  849. /* Destroy the backoff timer, since we won't need it anymore */
  850. if (prp_priv->backoff)
  851. backoff_delete(&prp_priv->backoff);
  852. } else if (event_occurred(prp, EVENT_BACKOFF_EXPIRED)){
  853. rc = acquire_replica(prp, REPL_NSDS50_INCREMENTAL_PROTOCOL_OID, &ruv);
  854. use_busy_backoff_timer = PR_FALSE;
  855. if (rc == ACQUIRE_SUCCESS){
  856. next_state = STATE_SENDING_UPDATES;
  857. } else if (rc == ACQUIRE_REPLICA_BUSY){
  858. next_state = STATE_BACKOFF;
  859. use_busy_backoff_timer = PR_TRUE;
  860. } else if (rc == ACQUIRE_CONSUMER_WAS_UPTODATE){
  861. next_state = STATE_WAIT_CHANGES;
  862. } else if (rc == ACQUIRE_TRANSIENT_ERROR){
  863. next_state = STATE_BACKOFF;
  864. } else if (rc == ACQUIRE_FATAL_ERROR){
  865. next_state = STATE_STOP_FATAL_ERROR;
  866. }
  867. /*
  868. * We either need to step the backoff timer, or
  869. * destroy it if we don't need it anymore
  870. */
  871. if (STATE_BACKOFF == next_state){
  872. /* Step the backoff timer */
  873. time(&now);
  874. next_fire_time = backoff_step(prp_priv->backoff);
  875. /* And go back to sleep */
  876. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  877. "%s: Replication session backing off for %ld seconds\n",
  878. agmt_get_long_name(prp->agmt),next_fire_time - now);
  879. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  880. } else {
  881. /* Destroy the backoff timer, since we won't need it anymore */
  882. backoff_delete(&prp_priv->backoff);
  883. }
  884. } else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET)){
  885. /* changes are available */
  886. if ( prp_priv->backoff == NULL || backoff_expired (prp_priv->backoff, 60)){
  887. /*
  888. * Have seen cases that the agmt stuck here forever since
  889. * somehow the backoff timer was not in event queue anymore.
  890. * If the backoff timer has expired more than 60 seconds, destroy it.
  891. */
  892. if ( prp_priv->backoff )
  893. backoff_delete(&prp_priv->backoff);
  894. next_state = STATE_READY_TO_ACQUIRE;
  895. } else {
  896. /* ignore changes and go to sleep */
  897. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  898. }
  899. } else if (event_occurred(prp, EVENT_WINDOW_OPENED)){
  900. /* this should never happen - log an error and go to sleep */
  901. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name, "%s: Incremental protocol: "
  902. "event %s should not occur in state %s; going to sleep\n",
  903. agmt_get_long_name(prp->agmt), event2name(EVENT_WINDOW_OPENED),
  904. state2name(current_state));
  905. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  906. }
  907. break;
  908. case STATE_SENDING_UPDATES:
  909. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES)");
  910. num_changes_sent = 0;
  911. /*
  912. * We've acquired the replica, and are ready to send any needed updates.
  913. */
  914. if (PROTOCOL_IS_SHUTDOWN(prp)){
  915. release_replica (prp);
  916. done = 1;
  917. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  918. agmt_set_last_update_end(prp->agmt, current_time());
  919. /* MAB: I don't find the following status correct. How do we know it has
  920. * been stopped by an admin and not by a total update request, for instance?
  921. * In any case, how is this protocol shutdown situation different from all the
  922. * other ones that are present in this state machine? */
  923. /* richm: We at least need to let monitors know that the protocol has been
  924. * shutdown - maybe they can figure out why */
  925. agmt_set_last_update_status(prp->agmt, 0, 0, "Protocol stopped");
  926. agmt_update_done(prp->agmt, 0);
  927. break;
  928. }
  929. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update started");
  930. /* ONREPL - in this state we send changes no matter what other events occur.
  931. * This is because we can get because of the REPLICATE_NOW event which
  932. * has high priority. Is this ok? */
  933. /* First, push new schema to the consumer if needed */
  934. /* ONREPL - should we push schema after we examine the RUV? */
  935. /*
  936. * GGOOREPL - I don't see why we should wait until we've
  937. * examined the RUV. The schema entry has its own CSN that is
  938. * used to decide if the remote schema needs to be updated.
  939. */
  940. cons_schema_csn = agmt_get_consumer_schema_csn ( prp->agmt );
  941. rc = conn_push_schema(prp->conn, &cons_schema_csn);
  942. if ( cons_schema_csn != agmt_get_consumer_schema_csn ( prp->agmt )){
  943. agmt_set_consumer_schema_csn ( prp->agmt, cons_schema_csn );
  944. }
  945. if (CONN_SCHEMA_UPDATED != rc && CONN_SCHEMA_NO_UPDATE_NEEDED != rc){
  946. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  947. "%s: Warning: unable to replicate schema: rc=%d\n", agmt_get_long_name(prp->agmt), rc);
  948. /* But keep going */
  949. }
  950. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> examine_update_vector");
  951. rc = examine_update_vector(prp, ruv);
  952. /*
  953. * Decide what to do next - proceed with incremental, backoff, or total update
  954. */
  955. switch (rc){
  956. case EXAMINE_RUV_PARAM_ERROR:
  957. /* this is really bad - we have NULL prp! */
  958. next_state = STATE_STOP_FATAL_ERROR;
  959. break;
  960. case EXAMINE_RUV_PRISTINE_REPLICA:
  961. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  962. "%s: Replica has no update vector. It has never been initialized.\n",
  963. agmt_get_long_name(prp->agmt));
  964. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_RUV_ERROR,
  965. "Replica is not initialized");
  966. next_state = STATE_BACKOFF_START;
  967. break;
  968. case EXAMINE_RUV_GENERATION_MISMATCH:
  969. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  970. "%s: The remote replica has a different database generation ID than "
  971. "the local database. You may have to reinitialize the remote replica, "
  972. "or the local replica.\n", agmt_get_long_name(prp->agmt));
  973. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_RUV_ERROR,
  974. "Replica has different database generation ID, remote "
  975. "replica may need to be initialized");
  976. next_state = STATE_BACKOFF_START;
  977. break;
  978. case EXAMINE_RUV_REPLICA_TOO_OLD:
  979. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  980. "%s: Replica update vector is too out of date to bring "
  981. "into sync using the incremental protocol. The replica "
  982. "must be reinitialized.\n", agmt_get_long_name(prp->agmt));
  983. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_RUV_ERROR,
  984. "Replica needs to be reinitialized");
  985. next_state = STATE_BACKOFF_START;
  986. break;
  987. case EXAMINE_RUV_OK:
  988. /* update our csn generator state with the consumer's ruv data */
  989. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> examine_update_vector OK");
  990. object_acquire(prp->replica_object);
  991. replica = object_get_data(prp->replica_object);
  992. rc = replica_update_csngen_state (replica, ruv);
  993. object_release (prp->replica_object);
  994. replica = NULL;
  995. if (rc == CSN_LIMIT_EXCEEDED) /* too much skew */ {
  996. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  997. "%s: Incremental protocol: fatal error - too much time skew between replicas!\n",
  998. agmt_get_long_name(prp->agmt));
  999. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_EXCESSIVE_CLOCK_SKEW,
  1000. "fatal error - too much time skew between replicas");
  1001. next_state = STATE_STOP_FATAL_ERROR;
  1002. } else if (rc != 0) /* internal error */ {
  1003. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1004. "%s: Incremental protocol: fatal internal error updating the CSN generator!\n",
  1005. agmt_get_long_name(prp->agmt));
  1006. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_INTERNAL_ERROR,
  1007. "fatal internal error updating the CSN generator");
  1008. next_state = STATE_STOP_FATAL_ERROR;
  1009. } else {
  1010. /*
  1011. * Reset our update times and status
  1012. */
  1013. agmt_set_last_update_start(prp->agmt, current_time());
  1014. agmt_set_last_update_end(prp->agmt, 0);
  1015. agmt_set_update_in_progress(prp->agmt, PR_TRUE);
  1016. /*
  1017. * Send the updates
  1018. */
  1019. rc = send_updates(prp, ruv, &num_changes_sent);
  1020. if (rc == UPDATE_NO_MORE_UPDATES){
  1021. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_NO_MORE_UPDATES -> STATE_WAIT_CHANGES");
  1022. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update succeeded");
  1023. next_state = STATE_WAIT_CHANGES;
  1024. } else if (rc == UPDATE_YIELD){
  1025. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_YIELD -> STATE_BACKOFF_START");
  1026. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update succeeded and yielded");
  1027. use_busy_backoff_timer = PR_TRUE;
  1028. next_state = STATE_BACKOFF_START;
  1029. } else if (rc == UPDATE_TRANSIENT_ERROR){
  1030. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_TRANSIENT_ERROR -> STATE_BACKOFF_START");
  1031. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_TRANSIENT_ERROR,
  1032. "Incremental update transient error. Backing off, will retry update later.");
  1033. next_state = STATE_BACKOFF_START;
  1034. } else if (rc == UPDATE_FATAL_ERROR){
  1035. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_FATAL_ERROR -> STATE_STOP_FATAL_ERROR");
  1036. next_state = STATE_STOP_FATAL_ERROR;
  1037. } else if (rc == UPDATE_SCHEDULE_WINDOW_CLOSED){
  1038. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_SCHEDULE_WINDOW_CLOSED -> STATE_WAIT_WINDOW_OPEN");
  1039. /*
  1040. * ONREPL - I don't think we should check this. We might be
  1041. * here because of replicate_now event - so we don't care
  1042. * about the schedule
  1043. */
  1044. next_state = STATE_WAIT_WINDOW_OPEN;
  1045. /* ONREPL - do we need to release the replica here ? */
  1046. conn_disconnect (prp->conn);
  1047. } else if (rc == UPDATE_CONNECTION_LOST){
  1048. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_CONNECTION_LOST -> STATE_BACKOFF_START");
  1049. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_CONN_ERROR,
  1050. "Incremental update connection error. Backing off, will retry update later.");
  1051. next_state = STATE_BACKOFF_START;
  1052. } else if (rc == UPDATE_TIMEOUT){
  1053. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_TIMEOUT -> STATE_BACKOFF_START");
  1054. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_CONN_TIMEOUT,
  1055. "Incremental update timeout error. Backing off, will retry update later.");
  1056. next_state = STATE_BACKOFF_START;
  1057. }
  1058. /* Set the updates times based off the result of send_updates() */
  1059. if(rc == UPDATE_NO_MORE_UPDATES){
  1060. /* update successful, set the end time */
  1061. agmt_set_last_update_end(prp->agmt, current_time());
  1062. } else {
  1063. /* Failed to send updates, reset the start time to zero */
  1064. agmt_set_last_update_start(prp->agmt, 0);
  1065. }
  1066. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  1067. }
  1068. break;
  1069. }
  1070. if (NULL != ruv){
  1071. ruv_destroy(&ruv); ruv = NULL;
  1072. }
  1073. agmt_update_done(prp->agmt, 0);
  1074. /* If timed out, close the connection after released the replica */
  1075. release_replica(prp);
  1076. if (rc == UPDATE_TIMEOUT) {
  1077. conn_disconnect(prp->conn);
  1078. }
  1079. if (rc == UPDATE_NO_MORE_UPDATES && num_changes_sent > 0){
  1080. if (pausetime > 0){
  1081. /* richm - 20020219 - If we have acquired the consumer, and another master has gone
  1082. * into backoff waiting for us to release it, we may acquire the replica sooner
  1083. * than the other master has a chance to, and the other master may not be able
  1084. * to acquire the consumer for a long time (hours, days?) if this server is
  1085. * under a heavy load (see reliab06 et. al. system tests)
  1086. * So, this sleep gives the other master(s) a chance to acquire the consumer replica */
  1087. loops = pausetime;
  1088. /* the while loop is so that we don't just sleep and sleep if an
  1089. * event comes in that we should handle immediately (like shutdown) */
  1090. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1091. "%s: Pausing updates for %ld seconds to allow other suppliers to update consumer\n",
  1092. agmt_get_long_name(prp->agmt), pausetime);
  1093. while (loops-- && !(PROTOCOL_IS_SHUTDOWN(prp))){
  1094. DS_Sleep(PR_SecondsToInterval(1));
  1095. }
  1096. } else if (num_changes_sent > 10){
  1097. /* wait for consumer to write its ruv if the replication was busy */
  1098. /* When asked, consumer sends its ruv in cache to the supplier. */
  1099. /* DS_Sleep ( PR_SecondsToInterval(1) ); */
  1100. }
  1101. }
  1102. break;
  1103. case STATE_STOP_FATAL_ERROR:
  1104. /*
  1105. * We encountered some sort of a fatal error. Suspend.
  1106. */
  1107. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR)");
  1108. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1109. "%s: Incremental update failed and requires administrator action\n",
  1110. agmt_get_long_name(prp->agmt));
  1111. next_state = STATE_STOP_FATAL_ERROR_PART2;
  1112. break;
  1113. case STATE_STOP_FATAL_ERROR_PART2:
  1114. if (PROTOCOL_IS_SHUTDOWN(prp)){
  1115. done = 1;
  1116. break;
  1117. }
  1118. /* MAB: This state is the FATAL state where we are supposed to get
  1119. * as a result of a FATAL error on send_updates. But, as bug
  1120. * states, send_updates was always returning TRANSIENT errors and never
  1121. * FATAL... In other words, this code has never been tested before...
  1122. *
  1123. * As of 01/16/01, this piece of code was in a very dangerous state. In particular,
  1124. * 1) it does not catch any events
  1125. * 2) it is a terminal state (once reached it never transitions to a different state)
  1126. *
  1127. * Both things combined make this state to become a consuming infinite loop
  1128. * that is useless after all (we are in a fatal place requiring manual admin jobs */
  1129. /* MAB: The following lines fix problem number 1 above... When the code gets
  1130. * into this state, it should only get a chance to get out of it by an
  1131. * EVENT_AGMT_CHANGED event... All other events should be ignored */
  1132. else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  1133. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR): EVENT_AGMT_CHANGED received\n");
  1134. /* Chance to recover for the EVENT_AGMT_CHANGED event.
  1135. * This is not mandatory, but fixes problem 2 above */
  1136. next_state = STATE_STOP_NORMAL_TERMINATION;
  1137. } else {
  1138. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR): Event received. Clearing it\n");
  1139. reset_events (prp);
  1140. }
  1141. protocol_sleep (prp, PR_INTERVAL_NO_TIMEOUT);
  1142. break;
  1143. case STATE_STOP_NORMAL_TERMINATION:
  1144. /*
  1145. * We encountered some sort of a fatal error. Return.
  1146. */
  1147. /* XXXggood update state in replica */
  1148. dev_debug("repl5_inc_run(STATE_STOP_NORMAL_TERMINATION)");
  1149. done = 1;
  1150. break;
  1151. }
  1152. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,"%s: State: %s -> %s\n",
  1153. agmt_get_long_name(prp->agmt),state2name(current_state), state2name(next_state));
  1154. current_state = next_state;
  1155. } while (!done);
  1156. /* remove_protocol_callbacks(prp); */
  1157. prp->stopped = 1;
  1158. /* Cancel any linger timer that might be in effect... */
  1159. conn_cancel_linger(prp->conn);
  1160. /* ... and disconnect, if currently connected */
  1161. conn_disconnect(prp->conn);
  1162. }
  1163. /*
  1164. * Go to sleep until awakened.
  1165. */
  1166. static void
  1167. protocol_sleep(Private_Repl_Protocol *prp, PRIntervalTime duration)
  1168. {
  1169. PR_ASSERT(NULL != prp);
  1170. PR_Lock(prp->lock);
  1171. /* we should not go to sleep if there are events available to be processed.
  1172. Otherwise, we can miss the event that suppose to wake us up */
  1173. if (prp->eventbits == 0)
  1174. PR_WaitCondVar(prp->cvar, duration);
  1175. else
  1176. {
  1177. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1178. "%s: Incremental protocol: can't go to sleep: event bits - %x\n",
  1179. agmt_get_long_name(prp->agmt), prp->eventbits);
  1180. }
  1181. PR_Unlock(prp->lock);
  1182. }
  1183. /*
  1184. * Notify the protocol about some event. Signal the condition
  1185. * variable in case the protocol is sleeping. Multiple occurences
  1186. * of a single event type are not remembered (e.g. no stack
  1187. * of events is maintained).
  1188. */
  1189. static void
  1190. event_notify(Private_Repl_Protocol *prp, PRUint32 event)
  1191. {
  1192. PR_ASSERT(NULL != prp);
  1193. PR_Lock(prp->lock);
  1194. prp->eventbits |= event;
  1195. PR_NotifyCondVar(prp->cvar);
  1196. PR_Unlock(prp->lock);
  1197. }
  1198. /*
  1199. * Test to see if an event occurred. The event is cleared when
  1200. * read.
  1201. */
  1202. static PRUint32
  1203. event_occurred(Private_Repl_Protocol *prp, PRUint32 event)
  1204. {
  1205. PRUint32 return_value;
  1206. PR_ASSERT(NULL != prp);
  1207. PR_Lock(prp->lock);
  1208. return_value = (prp->eventbits & event);
  1209. prp->eventbits &= ~event; /* Clear event */
  1210. PR_Unlock(prp->lock);
  1211. return return_value;
  1212. }
  1213. static void
  1214. reset_events (Private_Repl_Protocol *prp)
  1215. {
  1216. PR_ASSERT(NULL != prp);
  1217. PR_Lock(prp->lock);
  1218. prp->eventbits = 0;
  1219. PR_Unlock(prp->lock);
  1220. }
  1221. /*
  1222. * Replay the actual update to the consumer. Construct an appropriate LDAP
  1223. * operation, attach the baggage LDAPv3 control that contains the CSN, etc.,
  1224. * and send the operation to the consumer.
  1225. */
  1226. ConnResult
  1227. replay_update(Private_Repl_Protocol *prp, slapi_operation_parameters *op, int *message_id)
  1228. {
  1229. ConnResult return_value = CONN_OPERATION_FAILED;
  1230. LDAPControl *update_control;
  1231. char *parentuniqueid;
  1232. LDAPMod **modrdn_mods = NULL;
  1233. char csn_str[CSN_STRSIZE]; /* For logging only */
  1234. if (message_id) {
  1235. /* if we get out of this function without setting message_id, it means
  1236. we didn't send an op, so no result needs to be processed */
  1237. *message_id = 0;
  1238. }
  1239. /* Construct the replication info control that accompanies the operation */
  1240. if (SLAPI_OPERATION_ADD == op->operation_type)
  1241. {
  1242. parentuniqueid = op->p.p_add.parentuniqueid;
  1243. }
  1244. else if (SLAPI_OPERATION_MODRDN == op->operation_type)
  1245. {
  1246. /*
  1247. * For modrdn operations, we need to send along modified attributes, e.g.
  1248. * modifytimestamp.
  1249. * And the superior_uniqueid !
  1250. */
  1251. modrdn_mods = op->p.p_modrdn.modrdn_mods;
  1252. parentuniqueid = op->p.p_modrdn.modrdn_newsuperior_address.uniqueid;
  1253. }
  1254. else
  1255. {
  1256. parentuniqueid = NULL;
  1257. }
  1258. if (create_NSDS50ReplUpdateInfoControl(op->target_address.uniqueid,
  1259. parentuniqueid, op->csn, modrdn_mods, &update_control) != LDAP_SUCCESS)
  1260. {
  1261. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1262. "%s: replay_update: Unable to create NSDS50ReplUpdateInfoControl "
  1263. "for operation with csn %s. Skipping update.\n",
  1264. agmt_get_long_name(prp->agmt), csn_as_string(op->csn, PR_FALSE, csn_str));
  1265. }
  1266. else
  1267. {
  1268. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1269. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1270. "%s: replay_update: Sending %s operation (dn=\"%s\" csn=%s)\n",
  1271. agmt_get_long_name(prp->agmt),
  1272. op2string(op->operation_type), REPL_GET_DN(&op->target_address),
  1273. csn_as_string(op->csn, PR_FALSE, csn_str));
  1274. }
  1275. /* What type of operation is it? */
  1276. switch (op->operation_type)
  1277. {
  1278. case SLAPI_OPERATION_ADD:
  1279. {
  1280. LDAPMod **entryattrs;
  1281. /* Convert entry to mods */
  1282. (void)slapi_entry2mods (op->p.p_add.target_entry,
  1283. NULL /* &entrydn : We don't need it */,
  1284. &entryattrs);
  1285. if (NULL == entryattrs)
  1286. {
  1287. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1288. "%s: replay_update: Cannot convert entry to LDAPMods.\n",
  1289. agmt_get_long_name(prp->agmt));
  1290. return_value = CONN_LOCAL_ERROR;
  1291. }
  1292. else
  1293. {
  1294. /* If fractional agreement, trim down the entry */
  1295. if (agmt_is_fractional(prp->agmt))
  1296. {
  1297. repl5_strip_fractional_mods(prp->agmt,entryattrs);
  1298. }
  1299. if (MODS_ARE_EMPTY(entryattrs)) {
  1300. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1301. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1302. "%s: replay_update: %s operation (dn=\"%s\" csn=%s) "
  1303. "not sent - empty\n",
  1304. agmt_get_long_name(prp->agmt),
  1305. op2string(op->operation_type),
  1306. REPL_GET_DN(&op->target_address),
  1307. csn_as_string(op->csn, PR_FALSE, csn_str));
  1308. }
  1309. return_value = CONN_OPERATION_SUCCESS;
  1310. } else {
  1311. return_value = conn_send_add(prp->conn, REPL_GET_DN(&op->target_address),
  1312. entryattrs, update_control, message_id);
  1313. }
  1314. ldap_mods_free(entryattrs, 1);
  1315. }
  1316. break;
  1317. }
  1318. case SLAPI_OPERATION_MODIFY:
  1319. /* If fractional agreement, trim down the mods */
  1320. if (agmt_is_fractional(prp->agmt))
  1321. {
  1322. repl5_strip_fractional_mods(prp->agmt,op->p.p_modify.modify_mods);
  1323. }
  1324. if (MODS_ARE_EMPTY(op->p.p_modify.modify_mods)) {
  1325. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1326. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1327. "%s: replay_update: %ss operation (dn=\"%s\" csn=%s) "
  1328. "not sent - empty\n",
  1329. agmt_get_long_name(prp->agmt),
  1330. op2string(op->operation_type),
  1331. REPL_GET_DN(&op->target_address),
  1332. csn_as_string(op->csn, PR_FALSE, csn_str));
  1333. }
  1334. return_value = CONN_OPERATION_SUCCESS;
  1335. } else {
  1336. return_value = conn_send_modify(prp->conn, REPL_GET_DN(&op->target_address),
  1337. op->p.p_modify.modify_mods, update_control, message_id);
  1338. }
  1339. break;
  1340. case SLAPI_OPERATION_DELETE:
  1341. return_value = conn_send_delete(prp->conn, REPL_GET_DN(&op->target_address),
  1342. update_control, message_id);
  1343. break;
  1344. case SLAPI_OPERATION_MODRDN:
  1345. /* XXXggood need to pass modrdn mods in update control! */
  1346. return_value = conn_send_rename(prp->conn, REPL_GET_DN(&op->target_address),
  1347. op->p.p_modrdn.modrdn_newrdn,
  1348. REPL_GET_DN(&op->p.p_modrdn.modrdn_newsuperior_address),
  1349. op->p.p_modrdn.modrdn_deloldrdn,
  1350. update_control, message_id);
  1351. break;
  1352. default:
  1353. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name, "%s: replay_update: Unknown "
  1354. "operation type %lu found in changelog - skipping change.\n",
  1355. agmt_get_long_name(prp->agmt), op->operation_type);
  1356. }
  1357. destroy_NSDS50ReplUpdateInfoControl(&update_control);
  1358. }
  1359. if (CONN_OPERATION_SUCCESS == return_value)
  1360. {
  1361. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1362. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1363. "%s: replay_update: Consumer successfully sent operation with csn %s\n",
  1364. agmt_get_long_name(prp->agmt), csn_as_string(op->csn, PR_FALSE, csn_str));
  1365. }
  1366. }
  1367. else
  1368. {
  1369. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1370. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1371. "%s: replay_update: Consumer could not replay operation with csn %s\n",
  1372. agmt_get_long_name(prp->agmt), csn_as_string(op->csn, PR_FALSE, csn_str));
  1373. }
  1374. }
  1375. return return_value;
  1376. }
  1377. static PRBool
  1378. is_dummy_operation (const slapi_operation_parameters *op)
  1379. {
  1380. return (strcmp (op->target_address.uniqueid, START_ITERATION_ENTRY_UNIQUEID) == 0);
  1381. }
  1382. void
  1383. cl5_operation_parameters_done (struct slapi_operation_parameters *sop)
  1384. {
  1385. if(sop!=NULL) {
  1386. switch(sop->operation_type)
  1387. {
  1388. case SLAPI_OPERATION_BIND:
  1389. slapi_ch_free((void **)&(sop->p.p_bind.bind_saslmechanism));
  1390. if (sop->p.p_bind.bind_creds)
  1391. ber_bvecfree((struct berval**)&(sop->p.p_bind.bind_creds));
  1392. if (sop->p.p_bind.bind_ret_saslcreds)
  1393. ber_bvecfree((struct berval**)&(sop->p.p_bind.bind_ret_saslcreds));
  1394. sop->p.p_bind.bind_creds = NULL;
  1395. sop->p.p_bind.bind_ret_saslcreds = NULL;
  1396. break;
  1397. case SLAPI_OPERATION_COMPARE:
  1398. ava_done((struct ava *)&(sop->p.p_compare.compare_ava));
  1399. break;
  1400. case SLAPI_OPERATION_SEARCH:
  1401. slapi_ch_free((void **)&(sop->p.p_search.search_strfilter));
  1402. charray_free(sop->p.p_search.search_attrs);
  1403. slapi_filter_free(sop->p.p_search.search_filter,1);
  1404. break;
  1405. case SLAPI_OPERATION_MODRDN:
  1406. sop->p.p_modrdn.modrdn_deloldrdn = 0;
  1407. break;
  1408. case SLAPI_OPERATION_EXTENDED:
  1409. slapi_ch_free((void **)&(sop->p.p_extended.exop_oid));
  1410. if (sop->p.p_extended.exop_value)
  1411. ber_bvecfree((struct berval**)&(sop->p.p_extended.exop_value));
  1412. sop->p.p_extended.exop_value = NULL;
  1413. break;
  1414. default:
  1415. break;
  1416. }
  1417. }
  1418. operation_parameters_done(sop);
  1419. }
  1420. /* Helper to update the agreement state based on a the result of a replay operation */
  1421. static int
  1422. repl5_inc_update_from_op_result(Private_Repl_Protocol *prp, ConnResult replay_crc, int connection_error, char *csn_str, char *uniqueid, ReplicaId replica_id, int* finished, PRUint32 *num_changes_sent)
  1423. {
  1424. int return_value = 0;
  1425. if (CONN_OPERATION_SUCCESS != replay_crc)
  1426. {
  1427. /* Figure out what to do next */
  1428. if (CONN_OPERATION_FAILED == replay_crc)
  1429. {
  1430. /* Map ldap error code to return value */
  1431. if (!ignore_error_and_keep_going(connection_error))
  1432. {
  1433. return_value = UPDATE_TRANSIENT_ERROR;
  1434. *finished = 1;
  1435. }
  1436. else
  1437. {
  1438. agmt_inc_last_update_changecount (prp->agmt, replica_id, 1 /*skipped*/);
  1439. }
  1440. slapi_log_error(*finished ? SLAPI_LOG_FATAL : slapi_log_urp,
  1441. *finished ? LOG_ERR : LOG_DEBUG,
  1442. repl_plugin_name,
  1443. "%s: Consumer failed to replay change (uniqueid %s, CSN %s): %s (%d). %s.\n",
  1444. agmt_get_long_name(prp->agmt),
  1445. uniqueid, csn_str,
  1446. ldap_err2string(connection_error), connection_error,
  1447. *finished ? "Will retry later" : "Skipping");
  1448. }
  1449. else if (CONN_NOT_CONNECTED == replay_crc)
  1450. {
  1451. /* We lost the connection - enter backoff state */
  1452. return_value = UPDATE_CONNECTION_LOST;
  1453. *finished = 1;
  1454. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1455. "%s: Consumer failed to replay change (uniqueid %s, CSN %s): "
  1456. "%s(%d). Will retry later.\n",
  1457. agmt_get_long_name(prp->agmt),
  1458. uniqueid, csn_str,
  1459. connection_error ? ldap_err2string(connection_error) : "Connection lost",
  1460. connection_error);
  1461. }
  1462. else if (CONN_TIMEOUT == replay_crc)
  1463. {
  1464. return_value = UPDATE_TIMEOUT;
  1465. *finished = 1;
  1466. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1467. "%s: Consumer timed out to replay change (uniqueid %s, CSN %s): "
  1468. "%s.\n",
  1469. agmt_get_long_name(prp->agmt),
  1470. uniqueid, csn_str,
  1471. connection_error ? ldap_err2string(connection_error) : "Timeout");
  1472. }
  1473. else if (CONN_LOCAL_ERROR == replay_crc)
  1474. {
  1475. /*
  1476. * Something bad happened on the local server - enter
  1477. * backoff state.
  1478. */
  1479. return_value = UPDATE_TRANSIENT_ERROR;
  1480. *finished = 1;
  1481. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1482. "%s: Failed to replay change (uniqueid %s, CSN %s): "
  1483. "Local error. Will retry later.\n",
  1484. agmt_get_long_name(prp->agmt),
  1485. uniqueid, csn_str);
  1486. }
  1487. if (*finished){
  1488. /*
  1489. * A serious error has occurred, the consumer might have closed
  1490. * the connection already, but we need to close the conn on the
  1491. * supplier side to properly set the conn structure as closed.
  1492. */
  1493. conn_disconnect(prp->conn);
  1494. }
  1495. }
  1496. else
  1497. {
  1498. /* Positive response received */
  1499. (*num_changes_sent)++;
  1500. agmt_inc_last_update_changecount (prp->agmt, replica_id, 0 /*replayed*/);
  1501. }
  1502. return return_value;
  1503. }
  1504. /*
  1505. * Send a set of updates to the replica. Assumes that (1) the replica
  1506. * has already been acquired, (2) that the consumer's update vector has
  1507. * been checked and (3) that it's ok to send incremental updates.
  1508. * Returns:
  1509. * UPDATE_NO_MORE_UPDATES - all updates were sent successfully
  1510. * UPDATE_TRANSIENT_ERROR - some non-permanent error occurred. Try again later.
  1511. * UPDATE_FATAL_ERROR - some bad, permanent error occurred.
  1512. * UPDATE_SCHEDULE_WINDOW_CLOSED - the schedule window closed on us.
  1513. */
  1514. static int
  1515. send_updates(Private_Repl_Protocol *prp, RUV *remote_update_vector, PRUint32 *num_changes_sent)
  1516. {
  1517. CL5Entry entry;
  1518. slapi_operation_parameters op;
  1519. int return_value = 0;
  1520. int rc;
  1521. CL5ReplayIterator *changelog_iterator;
  1522. int message_id = 0;
  1523. result_data *rd = NULL;
  1524. *num_changes_sent = 0;
  1525. /*
  1526. * Iterate over the changelog. Retrieve each update,
  1527. * construct an appropriate LDAP operation,
  1528. * attaching the CSN, and send the change.
  1529. */
  1530. rc = cl5CreateReplayIterator(prp, remote_update_vector, &changelog_iterator);
  1531. if (CL5_SUCCESS != rc)
  1532. {
  1533. switch (rc)
  1534. {
  1535. case CL5_BAD_DATA: /* invalid parameter passed to the function */
  1536. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1537. "%s: Invalid parameter passed to cl5CreateReplayIterator\n",
  1538. agmt_get_long_name(prp->agmt));
  1539. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_CL_ERROR,
  1540. "Invalid parameter passed to cl5CreateReplayIterator");
  1541. return_value = UPDATE_FATAL_ERROR;
  1542. break;
  1543. case CL5_BAD_FORMAT: /* db data has unexpected format */
  1544. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1545. "%s: Unexpected format encountered in changelog database\n",
  1546. agmt_get_long_name(prp->agmt));
  1547. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_CL_ERROR,
  1548. "Unexpected format encountered in changelog database");
  1549. return_value = UPDATE_FATAL_ERROR;
  1550. break;
  1551. case CL5_BAD_STATE: /* changelog is in an incorrect state for attempted operation */
  1552. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1553. "%s: Changelog database was in an incorrect state\n",
  1554. agmt_get_long_name(prp->agmt));
  1555. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_CL_ERROR,
  1556. "Changelog database was in an incorrect state");
  1557. return_value = UPDATE_FATAL_ERROR;
  1558. break;
  1559. case CL5_BAD_DBVERSION: /* changelog has invalid dbversion */
  1560. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1561. "%s: Incorrect dbversion found in changelog database\n",
  1562. agmt_get_long_name(prp->agmt));
  1563. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_CL_ERROR,
  1564. "Incorrect dbversion found in changelog database");
  1565. return_value = UPDATE_FATAL_ERROR;
  1566. break;
  1567. case CL5_DB_ERROR: /* database error */
  1568. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1569. "%s: A changelog database error was encountered\n",
  1570. agmt_get_long_name(prp->agmt));
  1571. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_CL_ERROR,
  1572. "Changelog database error was encountered");
  1573. return_value = UPDATE_FATAL_ERROR;
  1574. break;
  1575. case CL5_NOTFOUND: /* we have no changes to send */
  1576. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1577. "%s: No changes to send\n",
  1578. agmt_get_long_name(prp->agmt));
  1579. return_value = UPDATE_NO_MORE_UPDATES;
  1580. break;
  1581. case CL5_MEMORY_ERROR: /* memory allocation failed */
  1582. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1583. "%s: Memory allocation error occurred\n",
  1584. agmt_get_long_name(prp->agmt));
  1585. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_CL_ERROR,
  1586. "changelog memory allocation error occurred");
  1587. return_value = UPDATE_FATAL_ERROR;
  1588. break;
  1589. case CL5_SYSTEM_ERROR: /* NSPR error occurred: use PR_GetError for further info */
  1590. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1591. "%s: An NSPR error (%d) occurred\n",
  1592. agmt_get_long_name(prp->agmt), PR_GetError());
  1593. return_value = UPDATE_TRANSIENT_ERROR;
  1594. break;
  1595. case CL5_CSN_ERROR: /* CSN API failed */
  1596. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1597. "%s: A CSN API failure was encountered\n",
  1598. agmt_get_long_name(prp->agmt));
  1599. return_value = UPDATE_TRANSIENT_ERROR;
  1600. break;
  1601. case CL5_RUV_ERROR: /* RUV API failed */
  1602. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1603. "%s: An RUV API failure occurred\n",
  1604. agmt_get_long_name(prp->agmt));
  1605. return_value = UPDATE_TRANSIENT_ERROR;
  1606. break;
  1607. case CL5_OBJSET_ERROR: /* namedobjset api failed */
  1608. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1609. "%s: A namedobject API failure occurred\n",
  1610. agmt_get_long_name(prp->agmt));
  1611. return_value = UPDATE_TRANSIENT_ERROR;
  1612. break;
  1613. case CL5_PURGED_DATA: /* requested data has been purged */
  1614. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1615. "%s: Data required to update replica has been purged from the changelog. "
  1616. "The replica must be reinitialized.\n",
  1617. agmt_get_long_name(prp->agmt));
  1618. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_CL_ERROR,
  1619. "Data required to update replica has been purged from the changelog. "
  1620. "The replica must be reinitialized.");
  1621. return_value = UPDATE_FATAL_ERROR;
  1622. break;
  1623. case CL5_MISSING_DATA: /* data should be in the changelog, but is missing */
  1624. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1625. "%s: Missing data encountered\n",
  1626. agmt_get_long_name(prp->agmt));
  1627. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_CL_ERROR,
  1628. "Changelog data is missing");
  1629. return_value = UPDATE_FATAL_ERROR;
  1630. break;
  1631. case CL5_UNKNOWN_ERROR: /* unclassified error */
  1632. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1633. "%s: An unknown error was encountered\n",
  1634. agmt_get_long_name(prp->agmt));
  1635. return_value = UPDATE_TRANSIENT_ERROR;
  1636. break;
  1637. default:
  1638. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1639. "%s: An unknown error (%d) occurred "
  1640. "(cl5CreateReplayIterator)\n",
  1641. agmt_get_long_name(prp->agmt), rc);
  1642. return_value = UPDATE_TRANSIENT_ERROR;
  1643. }
  1644. }
  1645. else
  1646. {
  1647. ConnResult replay_crc;
  1648. Replica *replica = (Replica*) object_get_data(prp->replica_object);
  1649. PRBool subentry_update_needed = PR_FALSE;
  1650. PRUint64 release_timeout = replica_get_release_timeout(replica);
  1651. char csn_str[CSN_STRSIZE];
  1652. int skipped_updates = 0;
  1653. int fractional_repl;
  1654. int finished = 0;
  1655. #define FRACTIONAL_SKIPPED_THRESHOLD 100
  1656. /* Start the results reading thread */
  1657. rd = repl5_inc_rd_new(prp);
  1658. if (!prp->repl50consumer)
  1659. {
  1660. rc = repl5_inc_create_async_result_thread(rd);
  1661. if (rc) {
  1662. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name, "%s: repl5_inc_run: "
  1663. "repl5_inc_create_async_result_thread failed; error - %d\n",
  1664. agmt_get_long_name(prp->agmt), rc);
  1665. agmt_set_last_update_status(prp->agmt, 0, rc, "Failed to create result thread");
  1666. return_value = UPDATE_FATAL_ERROR;
  1667. }
  1668. }
  1669. memset ( (void*)&op, 0, sizeof (op) );
  1670. entry.op = &op;
  1671. fractional_repl = agmt_is_fractional(prp->agmt);
  1672. do {
  1673. cl5_operation_parameters_done ( entry.op );
  1674. memset ( (void*)entry.op, 0, sizeof (op) );
  1675. rc = cl5GetNextOperationToReplay(changelog_iterator, &entry);
  1676. switch (rc)
  1677. {
  1678. case CL5_SUCCESS:
  1679. /* check that we don't return dummy entries */
  1680. if (is_dummy_operation (entry.op))
  1681. {
  1682. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1683. "%s: changelog iteration code returned a dummy entry with csn %s, "
  1684. "skipping ...\n",
  1685. agmt_get_long_name(prp->agmt), csn_as_string(entry.op->csn, PR_FALSE, csn_str));
  1686. continue;
  1687. }
  1688. replay_crc = replay_update(prp, entry.op, &message_id);
  1689. if (message_id)
  1690. {
  1691. rd->last_message_id_sent = message_id;
  1692. }
  1693. /* If we're talking to an old non-async replica, we need to pick up the response here */
  1694. if (CONN_OPERATION_SUCCESS != replay_crc)
  1695. {
  1696. int operation, error;
  1697. conn_get_error(prp->conn, &operation, &error);
  1698. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1699. /* Figure out what to do next */
  1700. if (CONN_OPERATION_FAILED == replay_crc)
  1701. {
  1702. /* Map ldap error code to return value */
  1703. if (!ignore_error_and_keep_going(error))
  1704. {
  1705. return_value = UPDATE_TRANSIENT_ERROR;
  1706. finished = 1;
  1707. }
  1708. else
  1709. {
  1710. agmt_inc_last_update_changecount (prp->agmt, csn_get_replicaid(entry.op->csn), 1 /*skipped*/);
  1711. }
  1712. slapi_log_error(finished ? SLAPI_LOG_FATAL : slapi_log_urp,
  1713. finished ? LOG_ERR : LOG_DEBUG, repl_plugin_name,
  1714. "%s: Failed to send update operation to consumer (uniqueid %s, CSN %s): %s. %s.\n",
  1715. agmt_get_long_name(prp->agmt),
  1716. entry.op->target_address.uniqueid, csn_str,
  1717. ldap_err2string(error),
  1718. finished ? "Will retry later" : "Skipping");
  1719. }
  1720. else if (CONN_NOT_CONNECTED == replay_crc)
  1721. {
  1722. /* We lost the connection - enter backoff state */
  1723. return_value = UPDATE_CONNECTION_LOST;
  1724. finished = 1;
  1725. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1726. "%s: Failed to send update operation to consumer (uniqueid %s, CSN %s): "
  1727. "%s. Will retry later.\n",
  1728. agmt_get_long_name(prp->agmt),
  1729. entry.op->target_address.uniqueid, csn_str,
  1730. error ? ldap_err2string(error) : "Connection lost");
  1731. }
  1732. else if (CONN_TIMEOUT == replay_crc)
  1733. {
  1734. return_value = UPDATE_TIMEOUT;
  1735. finished = 1;
  1736. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1737. "%s: Timed out sending update operation to consumer (uniqueid %s, CSN %s): "
  1738. "%s.\n",
  1739. agmt_get_long_name(prp->agmt),
  1740. entry.op->target_address.uniqueid, csn_str,
  1741. error ? ldap_err2string(error) : "Timeout");
  1742. }
  1743. else if (CONN_LOCAL_ERROR == replay_crc)
  1744. {
  1745. /*
  1746. * Something bad happened on the local server - enter
  1747. * backoff state.
  1748. */
  1749. return_value = UPDATE_TRANSIENT_ERROR;
  1750. finished = 1;
  1751. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1752. "%s: Failed to send update operation to consumer (uniqueid %s, CSN %s): "
  1753. "Local error. Will retry later.\n",
  1754. agmt_get_long_name(prp->agmt),
  1755. entry.op->target_address.uniqueid, csn_str);
  1756. }
  1757. }
  1758. else
  1759. {
  1760. char *uniqueid = NULL;
  1761. ReplicaId replica_id = 0;
  1762. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1763. replica_id = csn_get_replicaid(entry.op->csn);
  1764. uniqueid = entry.op->target_address.uniqueid;
  1765. if (fractional_repl && message_id)
  1766. {
  1767. /* This update was sent no need to update the subentry
  1768. * and restart counting the skipped updates
  1769. */
  1770. subentry_update_needed = PR_FALSE;
  1771. skipped_updates = 0;
  1772. }
  1773. if (prp->repl50consumer && message_id)
  1774. {
  1775. int operation, error = 0;
  1776. conn_get_error(prp->conn, &operation, &error);
  1777. /* Get the response here */
  1778. replay_crc = repl5_inc_get_next_result(rd);
  1779. conn_get_error(prp->conn, &operation, &error);
  1780. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1781. return_value = repl5_inc_update_from_op_result(prp, replay_crc, error, csn_str, uniqueid, replica_id, &finished, num_changes_sent);
  1782. }
  1783. else if (message_id) {
  1784. /* Queue the details for pickup later in the response thread */
  1785. repl5_inc_operation *sop = NULL;
  1786. sop = repl5_inc_operation_new();
  1787. PL_strncpyz(sop->csn_str, csn_str, sizeof(sop->csn_str));
  1788. sop->ldap_message_id = message_id;
  1789. sop->operation_type = entry.op->operation_type;
  1790. sop->replica_id = replica_id;
  1791. PL_strncpyz(sop->uniqueid, uniqueid, sizeof(sop->uniqueid));
  1792. repl5_int_push_operation(rd,sop);
  1793. repl5_inc_flow_control_results(prp->agmt, rd);
  1794. } else {
  1795. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1796. "%s: Skipping update operation with no message_id (uniqueid %s, CSN %s):\n",
  1797. agmt_get_long_name(prp->agmt),
  1798. entry.op->target_address.uniqueid, csn_str);
  1799. agmt_inc_last_update_changecount (prp->agmt, csn_get_replicaid(entry.op->csn), 1 /*skipped*/);
  1800. if (fractional_repl)
  1801. {
  1802. skipped_updates++;
  1803. if (skipped_updates > FRACTIONAL_SKIPPED_THRESHOLD) {
  1804. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1805. "%s: skipped updates is too high (%d) if no other update is sent we will update the subentry\n",
  1806. agmt_get_long_name(prp->agmt), skipped_updates);
  1807. subentry_update_needed = PR_TRUE;
  1808. }
  1809. }
  1810. }
  1811. }
  1812. break;
  1813. case CL5_BAD_DATA:
  1814. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1815. "%s: Invalid parameter passed to cl5GetNextOperationToReplay\n",
  1816. agmt_get_long_name(prp->agmt));
  1817. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_CL_ERROR,
  1818. "Invalid parameter passed to cl5GetNextOperationToReplay");
  1819. return_value = UPDATE_FATAL_ERROR;
  1820. finished = 1;
  1821. break;
  1822. case CL5_NOTFOUND:
  1823. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1824. "%s: No more updates to send (cl5GetNextOperationToReplay)\n",
  1825. agmt_get_long_name(prp->agmt));
  1826. return_value = UPDATE_NO_MORE_UPDATES;
  1827. finished = 1;
  1828. break;
  1829. case CL5_DB_ERROR:
  1830. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1831. "%s: A database error occurred (cl5GetNextOperationToReplay)\n",
  1832. agmt_get_long_name(prp->agmt));
  1833. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_CL_ERROR,
  1834. "Database error occurred while getting the next operation to replay");
  1835. return_value = UPDATE_FATAL_ERROR;
  1836. finished = 1;
  1837. break;
  1838. case CL5_BAD_FORMAT:
  1839. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1840. "%s: A malformed changelog entry was encountered (cl5GetNextOperationToReplay)\n",
  1841. agmt_get_long_name(prp->agmt));
  1842. break;
  1843. case CL5_MEMORY_ERROR:
  1844. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1845. "%s: A memory allocation error occurred (cl5GetNextOperationToReplay)\n",
  1846. agmt_get_long_name(prp->agmt));
  1847. agmt_set_last_update_status(prp->agmt, 0, NSDS50_REPL_CL_ERROR,
  1848. "Memory allocation error occurred (cl5GetNextOperationToReplay)");
  1849. return_value = UPDATE_FATAL_ERROR;
  1850. break;
  1851. case CL5_IGNORE_OP:
  1852. break;
  1853. default:
  1854. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1855. "%s: Unknown error code (%d) returned from cl5GetNextOperationToReplay\n",
  1856. agmt_get_long_name(prp->agmt), rc);
  1857. return_value = UPDATE_TRANSIENT_ERROR;
  1858. break;
  1859. }
  1860. /* Check for protocol shutdown */
  1861. if (prp->terminate)
  1862. {
  1863. return_value = UPDATE_NO_MORE_UPDATES;
  1864. finished = 1;
  1865. }
  1866. if (*num_changes_sent >= MAX_CHANGES_PER_SESSION)
  1867. {
  1868. return_value = UPDATE_YIELD;
  1869. finished = 1;
  1870. }
  1871. PR_Lock(rd->lock);
  1872. /* See if the result thread has hit a problem */
  1873. if(!finished && rd->abort_time){
  1874. time_t current_time = time ( NULL );
  1875. if ((current_time - rd->abort_time) >= release_timeout){
  1876. rd->result = UPDATE_YIELD;
  1877. return_value = UPDATE_YIELD;
  1878. finished = 1;
  1879. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1880. "Aborting send_updates...(%s)\n",
  1881. agmt_get_long_name(rd->prp->agmt));
  1882. }
  1883. }
  1884. if (!finished && rd->abort == ABORT_SESSION)
  1885. {
  1886. return_value = rd->result;
  1887. finished = 1;
  1888. }
  1889. PR_Unlock(rd->lock);
  1890. } while (!finished);
  1891. if (fractional_repl && subentry_update_needed)
  1892. {
  1893. ReplicaId rid = -1; /* Used to create the replica keep alive subentry */
  1894. Slapi_DN *replarea_sdn = NULL;
  1895. if (replica)
  1896. {
  1897. rid = replica_get_rid(replica);
  1898. }
  1899. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1900. "%s: skipped updates was definitely too high (%d) update the subentry now\n",
  1901. agmt_get_long_name(prp->agmt), skipped_updates);
  1902. replarea_sdn = agmt_get_replarea(prp->agmt);
  1903. if (!replarea_sdn) {
  1904. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name,
  1905. "send_updates: Unknown replication area due to agreement not found.");
  1906. agmt_set_last_update_status(prp->agmt, 0, -1, "Agreement is corrupted: missing suffix");
  1907. return_value = UPDATE_FATAL_ERROR;
  1908. } else {
  1909. replica_subentry_update(replarea_sdn, rid);
  1910. }
  1911. }
  1912. /* Terminate the results reading thread */
  1913. if (!prp->repl50consumer)
  1914. {
  1915. /* We need to ensure that we wait until all the responses have been received from our operations */
  1916. if (return_value != UPDATE_CONNECTION_LOST) {
  1917. /*
  1918. * If we already have an error, there is no need to check the
  1919. * async result thread anymore.
  1920. */
  1921. if (return_value == UPDATE_NO_MORE_UPDATES || return_value == UPDATE_YIELD)
  1922. {
  1923. /*
  1924. * We need to double check that an error hasn't popped up from
  1925. * the async result thread since our last check.
  1926. */
  1927. int final_result;
  1928. rd->WaitForAsyncResults = agmt_get_WaitForAsyncResults(prp->agmt);
  1929. if((final_result = repl5_inc_waitfor_async_results(rd))){
  1930. return_value = final_result;
  1931. }
  1932. }
  1933. }
  1934. rc = repl5_inc_destroy_async_result_thread(rd);
  1935. if (rc) {
  1936. slapi_log_error(SLAPI_LOG_FATAL, LOG_ERR, repl_plugin_name, "%s: repl5_inc_run: "
  1937. "repl5_tot_destroy_async_result_thread failed; error - %d\n",
  1938. agmt_get_long_name(prp->agmt), rc);
  1939. }
  1940. *num_changes_sent = rd->num_changes_sent;
  1941. }
  1942. PR_Lock(rd->lock);
  1943. if (rd->flowcontrol_detection) {
  1944. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1945. "%s: Incremental update flow control triggered %d times\n"
  1946. "You may increase %s and/or decrease %s in the replica agreement configuration\n",
  1947. agmt_get_long_name(prp->agmt),
  1948. rd->flowcontrol_detection,
  1949. type_nsds5ReplicaFlowControlPause,
  1950. type_nsds5ReplicaFlowControlWindow);
  1951. }
  1952. PR_Unlock(rd->lock);
  1953. repl5_inc_rd_destroy(&rd);
  1954. cl5_operation_parameters_done ( entry.op );
  1955. cl5DestroyReplayIterator(&changelog_iterator);
  1956. }
  1957. return return_value;
  1958. }
  1959. /*
  1960. * XXXggood this should probably be in the superclass, since the full update
  1961. * protocol is going to need it too.
  1962. */
  1963. static int
  1964. repl5_inc_stop(Private_Repl_Protocol *prp)
  1965. {
  1966. PRIntervalTime start, maxwait, now;
  1967. Replica *replica = NULL;
  1968. PRUint64 timeout;
  1969. int return_value;
  1970. if((timeout = agmt_get_protocol_timeout(prp->agmt)) == 0){
  1971. timeout = DEFAULT_PROTOCOL_TIMEOUT;
  1972. if(prp->replica_object){
  1973. object_acquire(prp->replica_object);
  1974. replica = object_get_data(prp->replica_object);
  1975. if((timeout = replica_get_protocol_timeout(replica)) == 0){
  1976. timeout = DEFAULT_PROTOCOL_TIMEOUT;
  1977. }
  1978. object_release(prp->replica_object);
  1979. }
  1980. }
  1981. maxwait = PR_SecondsToInterval(timeout);
  1982. prp->terminate = 1;
  1983. event_notify(prp, EVENT_PROTOCOL_SHUTDOWN);
  1984. start = PR_IntervalNow();
  1985. now = start;
  1986. while (!prp->stopped && ((now - start) < maxwait))
  1987. {
  1988. DS_Sleep(PR_MillisecondsToInterval(100));
  1989. now = PR_IntervalNow();
  1990. }
  1991. if (!prp->stopped)
  1992. {
  1993. /* Isn't listening. Do something drastic. */
  1994. return_value = -1;
  1995. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  1996. "%s: repl5_inc_stop: protocol does not stop after %" NSPRIu64 " seconds\n",
  1997. agmt_get_long_name(prp->agmt), timeout);
  1998. }
  1999. else
  2000. {
  2001. return_value = 0;
  2002. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  2003. "%s: repl5_inc_stop: protocol stopped after %d seconds\n",
  2004. agmt_get_long_name(prp->agmt),
  2005. PR_IntervalToSeconds(now-start));
  2006. }
  2007. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  2008. if (NULL == prp->replica_object) {
  2009. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  2010. "%s: repl5_inc_stop: protocol replica_object is NULL\n",
  2011. agmt_get_long_name(prp->agmt));
  2012. } else {
  2013. Replica *replica;
  2014. object_acquire(prp->replica_object);
  2015. replica = object_get_data(prp->replica_object);
  2016. if (NULL == replica) {
  2017. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  2018. "%s: repl5_inc_stop: replica is NULL\n",
  2019. agmt_get_long_name(prp->agmt));
  2020. } else {
  2021. Object *ruv_obj = replica_get_ruv(replica);
  2022. if (NULL == ruv_obj) {
  2023. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  2024. "%s: repl5_inc_stop: ruv_obj is NULL\n",
  2025. agmt_get_long_name(prp->agmt));
  2026. } else {
  2027. RUV *ruv;
  2028. object_acquire(ruv_obj);
  2029. ruv = (RUV*)object_get_data (ruv_obj);
  2030. if (NULL == ruv) {
  2031. slapi_log_error(SLAPI_LOG_REPL, LOG_DEBUG, repl_plugin_name,
  2032. "%s: repl5_inc_stop: ruv is NULL\n",
  2033. agmt_get_long_name(prp->agmt));
  2034. } else {
  2035. ruv_dump(ruv, "Database RUV", NULL);
  2036. }
  2037. object_release(ruv_obj);
  2038. }
  2039. }
  2040. object_release(prp->replica_object);
  2041. }
  2042. }
  2043. return return_value;
  2044. }
  2045. static int
  2046. repl5_inc_status(Private_Repl_Protocol *prp)
  2047. {
  2048. int return_value = 0;
  2049. return return_value;
  2050. }
  2051. static void
  2052. repl5_inc_notify_update(Private_Repl_Protocol *prp)
  2053. {
  2054. event_notify(prp, EVENT_TRIGGERING_CRITERIA_MET);
  2055. }
  2056. static void
  2057. repl5_inc_update_now(Private_Repl_Protocol *prp)
  2058. {
  2059. event_notify(prp, EVENT_REPLICATE_NOW);
  2060. }
  2061. static void
  2062. repl5_inc_notify_agmt_changed(Private_Repl_Protocol *prp)
  2063. {
  2064. event_notify(prp, EVENT_AGMT_CHANGED);
  2065. }
  2066. static void
  2067. repl5_inc_notify_window_opened (Private_Repl_Protocol *prp)
  2068. {
  2069. event_notify(prp, EVENT_WINDOW_OPENED);
  2070. }
  2071. static void
  2072. repl5_inc_notify_window_closed (Private_Repl_Protocol *prp)
  2073. {
  2074. event_notify(prp, EVENT_WINDOW_CLOSED);
  2075. }
  2076. Private_Repl_Protocol *
  2077. Repl_5_Inc_Protocol_new(Repl_Protocol *rp)
  2078. {
  2079. repl5_inc_private *rip = NULL;
  2080. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)slapi_ch_malloc(sizeof(Private_Repl_Protocol));
  2081. prp->delete = repl5_inc_delete;
  2082. prp->run = repl5_inc_run;
  2083. prp->stop = repl5_inc_stop;
  2084. prp->status = repl5_inc_status;
  2085. prp->notify_update = repl5_inc_notify_update;
  2086. prp->notify_agmt_changed = repl5_inc_notify_agmt_changed;
  2087. prp->notify_window_opened = repl5_inc_notify_window_opened;
  2088. prp->notify_window_closed = repl5_inc_notify_window_closed;
  2089. prp->update_now = repl5_inc_update_now;
  2090. prp->replica_object = prot_get_replica_object(rp);
  2091. if ((prp->lock = PR_NewLock()) == NULL)
  2092. {
  2093. goto loser;
  2094. }
  2095. if ((prp->cvar = PR_NewCondVar(prp->lock)) == NULL)
  2096. {
  2097. goto loser;
  2098. }
  2099. prp->stopped = 0;
  2100. prp->terminate = 0;
  2101. prp->eventbits = 0;
  2102. prp->conn = prot_get_connection(rp);
  2103. prp->agmt = prot_get_agreement(rp);
  2104. prp->last_acquire_response_code = NSDS50_REPL_REPLICA_READY;
  2105. rip = (void *)slapi_ch_malloc(sizeof(repl5_inc_private));
  2106. rip->ruv = NULL;
  2107. rip->backoff = NULL;
  2108. rip->rp = rp;
  2109. prp->private = (void *)rip;
  2110. prp->replica_acquired = PR_FALSE;
  2111. prp->repl50consumer = 0;
  2112. prp->repl71consumer = 0;
  2113. prp->repl90consumer = 0;
  2114. return prp;
  2115. loser:
  2116. repl5_inc_delete(&prp);
  2117. return NULL;
  2118. }
  2119. static void
  2120. repl5_inc_backoff_expired(time_t timer_fire_time, void *arg)
  2121. {
  2122. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)arg;
  2123. PR_ASSERT(NULL != prp);
  2124. event_notify(prp, EVENT_BACKOFF_EXPIRED);
  2125. }
  2126. /*
  2127. * Examine the update vector and determine our course of action.
  2128. * There are 3 different possibilities, plus a catch-all error:
  2129. * 1 - no update vector (ruv is NULL). The consumer's replica is
  2130. * pristine, so it needs to be initialized. Return
  2131. * EXAMINE_RUV_PRISTINE_REPLICA.
  2132. * 2 - ruv is present, but its database generation ID doesn't
  2133. * match the local generation ID. This means that either
  2134. * the local replica must be reinitialized from the remote
  2135. * replica or vice-versa. Return
  2136. * EXAMINE_RUV_GENERATION_MISMATCH.
  2137. * 3 - ruv is present, and we have all updates needed to bring
  2138. * the replica up to date using the incremental protocol.
  2139. * return EXAMINE_RUV_OK.
  2140. * 4 - parameter error. Return EXAMINE_RUV_PARAM_ERROR
  2141. */
  2142. static int
  2143. examine_update_vector(Private_Repl_Protocol *prp, RUV *remote_ruv)
  2144. {
  2145. int return_value;
  2146. PR_ASSERT(NULL != prp);
  2147. if (NULL == prp)
  2148. {
  2149. return_value = EXAMINE_RUV_PARAM_ERROR;
  2150. }
  2151. else if (NULL == remote_ruv)
  2152. {
  2153. return_value = EXAMINE_RUV_PRISTINE_REPLICA;
  2154. }
  2155. else
  2156. {
  2157. char *local_gen = NULL;
  2158. char *remote_gen = ruv_get_replica_generation(remote_ruv);
  2159. Object *local_ruv_obj;
  2160. RUV *local_ruv;
  2161. Replica *replica;
  2162. PR_ASSERT(NULL != prp->replica_object);
  2163. replica = object_get_data(prp->replica_object);
  2164. PR_ASSERT(NULL != replica);
  2165. local_ruv_obj = replica_get_ruv (replica);
  2166. if (NULL != local_ruv_obj)
  2167. {
  2168. local_ruv = (RUV*)object_get_data (local_ruv_obj);
  2169. PR_ASSERT (local_ruv);
  2170. local_gen = ruv_get_replica_generation(local_ruv);
  2171. object_release (local_ruv_obj);
  2172. }
  2173. if (NULL == remote_gen || NULL == local_gen || strcmp(remote_gen, local_gen) != 0)
  2174. {
  2175. return_value = EXAMINE_RUV_GENERATION_MISMATCH;
  2176. }
  2177. else
  2178. {
  2179. return_value = EXAMINE_RUV_OK;
  2180. }
  2181. slapi_ch_free((void**)&remote_gen);
  2182. slapi_ch_free((void**)&local_gen);
  2183. }
  2184. return return_value;
  2185. }
  2186. /*
  2187. * When we get an error from an LDAP operation, we call this
  2188. * function to decide if we should just keep replaying
  2189. * updates, or if we should stop, back off, and try again
  2190. * later.
  2191. * Returns PR_TRUE if we shoould keep going, PR_FALSE if
  2192. * we should back off and try again later.
  2193. *
  2194. * In general, we keep going if the return code is consistent
  2195. * with some sort of bug in URP that causes the consumer to
  2196. * emit an error code that it shouldn't have, e.g. LDAP_ALREADY_EXISTS.
  2197. *
  2198. * We stop if there's some indication that the server just completely
  2199. * failed to process the operation, e.g. LDAP_OPERATIONS_ERROR.
  2200. */
  2201. PRBool
  2202. ignore_error_and_keep_going(int error)
  2203. {
  2204. int return_value = PR_FALSE;
  2205. switch (error)
  2206. {
  2207. /* Cases where we keep going */
  2208. case LDAP_SUCCESS:
  2209. case LDAP_NO_SUCH_ATTRIBUTE:
  2210. case LDAP_UNDEFINED_TYPE:
  2211. case LDAP_CONSTRAINT_VIOLATION:
  2212. case LDAP_TYPE_OR_VALUE_EXISTS:
  2213. case LDAP_INVALID_SYNTAX:
  2214. case LDAP_NO_SUCH_OBJECT:
  2215. case LDAP_INVALID_DN_SYNTAX:
  2216. case LDAP_IS_LEAF:
  2217. case LDAP_INSUFFICIENT_ACCESS:
  2218. case LDAP_NAMING_VIOLATION:
  2219. case LDAP_OBJECT_CLASS_VIOLATION:
  2220. case LDAP_NOT_ALLOWED_ON_NONLEAF:
  2221. case LDAP_NOT_ALLOWED_ON_RDN:
  2222. case LDAP_ALREADY_EXISTS:
  2223. case LDAP_NO_OBJECT_CLASS_MODS:
  2224. return_value = PR_TRUE;
  2225. break;
  2226. /* Cases where we stop and retry */
  2227. case LDAP_OPERATIONS_ERROR:
  2228. case LDAP_PROTOCOL_ERROR:
  2229. case LDAP_TIMELIMIT_EXCEEDED:
  2230. case LDAP_SIZELIMIT_EXCEEDED:
  2231. case LDAP_STRONG_AUTH_NOT_SUPPORTED:
  2232. case LDAP_STRONG_AUTH_REQUIRED:
  2233. case LDAP_PARTIAL_RESULTS:
  2234. case LDAP_REFERRAL:
  2235. case LDAP_ADMINLIMIT_EXCEEDED:
  2236. case LDAP_UNAVAILABLE_CRITICAL_EXTENSION:
  2237. case LDAP_CONFIDENTIALITY_REQUIRED:
  2238. case LDAP_SASL_BIND_IN_PROGRESS:
  2239. case LDAP_INAPPROPRIATE_MATCHING:
  2240. case LDAP_ALIAS_PROBLEM:
  2241. case LDAP_ALIAS_DEREF_PROBLEM:
  2242. case LDAP_INAPPROPRIATE_AUTH:
  2243. case LDAP_INVALID_CREDENTIALS:
  2244. case LDAP_BUSY:
  2245. case LDAP_UNAVAILABLE:
  2246. case LDAP_UNWILLING_TO_PERFORM:
  2247. case LDAP_LOOP_DETECT:
  2248. case LDAP_SORT_CONTROL_MISSING:
  2249. case LDAP_INDEX_RANGE_ERROR:
  2250. case LDAP_RESULTS_TOO_LARGE:
  2251. case LDAP_AFFECTS_MULTIPLE_DSAS:
  2252. case LDAP_OTHER:
  2253. case LDAP_SERVER_DOWN:
  2254. case LDAP_LOCAL_ERROR:
  2255. case LDAP_ENCODING_ERROR:
  2256. case LDAP_DECODING_ERROR:
  2257. case LDAP_TIMEOUT:
  2258. case LDAP_AUTH_UNKNOWN:
  2259. case LDAP_FILTER_ERROR:
  2260. case LDAP_USER_CANCELLED:
  2261. case LDAP_PARAM_ERROR:
  2262. case LDAP_NO_MEMORY:
  2263. case LDAP_CONNECT_ERROR:
  2264. case LDAP_NOT_SUPPORTED:
  2265. case LDAP_CONTROL_NOT_FOUND:
  2266. case LDAP_NO_RESULTS_RETURNED:
  2267. case LDAP_MORE_RESULTS_TO_RETURN:
  2268. case LDAP_CLIENT_LOOP:
  2269. case LDAP_REFERRAL_LIMIT_EXCEEDED:
  2270. return_value = PR_FALSE;
  2271. break;
  2272. }
  2273. return return_value;
  2274. }
  2275. /* this function converts a state to its name - for debug output */
  2276. static const char*
  2277. state2name (int state)
  2278. {
  2279. switch (state)
  2280. {
  2281. case STATE_START: return "start";
  2282. case STATE_WAIT_WINDOW_OPEN: return "wait_for_window_to_open";
  2283. case STATE_WAIT_CHANGES: return "wait_for_changes";
  2284. case STATE_READY_TO_ACQUIRE: return "ready_to_acquire_replica";
  2285. case STATE_BACKOFF_START: return "start_backoff";
  2286. case STATE_BACKOFF: return "backoff";
  2287. case STATE_SENDING_UPDATES: return "sending_updates";
  2288. case STATE_STOP_FATAL_ERROR: return "stop_fatal_error";
  2289. case STATE_STOP_FATAL_ERROR_PART2: return "stop_fatal_error";
  2290. case STATE_STOP_NORMAL_TERMINATION: return "stop_normal_termination";
  2291. default: return "invalid_state";
  2292. }
  2293. }
  2294. /* this function convert s an event to its name - for debug output */
  2295. static const char*
  2296. event2name (int event)
  2297. {
  2298. switch (event)
  2299. {
  2300. case EVENT_WINDOW_OPENED: return "update_window_opened";
  2301. case EVENT_WINDOW_CLOSED: return "update_window_closed";
  2302. case EVENT_TRIGGERING_CRITERIA_MET: return "data_modified";
  2303. case EVENT_BACKOFF_EXPIRED: return "backoff_timer_expired";
  2304. case EVENT_REPLICATE_NOW: return "replicate_now";
  2305. case EVENT_PROTOCOL_SHUTDOWN: return "protocol_shutdown";
  2306. case EVENT_AGMT_CHANGED: return "agreement_changed";
  2307. default: return "invalid_event";
  2308. }
  2309. }
  2310. static const char*
  2311. op2string(int op)
  2312. {
  2313. switch (op) {
  2314. case SLAPI_OPERATION_ADD:
  2315. return "add";
  2316. case SLAPI_OPERATION_MODIFY:
  2317. return "modify";
  2318. case SLAPI_OPERATION_DELETE:
  2319. return "delete";
  2320. case SLAPI_OPERATION_MODRDN:
  2321. return "rename";
  2322. case SLAPI_OPERATION_EXTENDED:
  2323. return "extended";
  2324. }
  2325. return "unknown";
  2326. }
  2327. void
  2328. repl5_set_backoff_min(Private_Repl_Protocol *prp, int min)
  2329. {
  2330. Replica *replica;
  2331. replica = (Replica *)object_get_data(prp->replica_object);
  2332. if(replica){
  2333. replica_set_backoff_min(replica, min);
  2334. }
  2335. }
  2336. void
  2337. repl5_set_backoff_max(Private_Repl_Protocol *prp, int max)
  2338. {
  2339. Replica *replica;
  2340. replica = object_get_data(prp->replica_object);
  2341. if(replica){
  2342. replica_set_backoff_max(replica, max);
  2343. }
  2344. }
  2345. int
  2346. repl5_get_backoff_min(Private_Repl_Protocol *prp)
  2347. {
  2348. Replica *replica;
  2349. replica = object_get_data(prp->replica_object);
  2350. if(replica){
  2351. return (int)replica_get_backoff_min(replica);
  2352. }
  2353. return PROTOCOL_BACKOFF_MINIMUM;
  2354. }
  2355. int
  2356. repl5_get_backoff_max(Private_Repl_Protocol *prp)
  2357. {
  2358. Replica *replica;
  2359. replica = object_get_data(prp->replica_object);
  2360. if(replica){
  2361. return (int)replica_get_backoff_max(replica);
  2362. }
  2363. return PROTOCOL_BACKOFF_MAXIMUM;
  2364. }