repl5_inc_protocol.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /* repl5_inc_protocol.c */
  7. /*
  8. The Prot_Incremental object implements the DS 5.0 multi-master incremental
  9. replication protocol.
  10. Stuff to do:
  11. - Need to figure out how asynchronous events end up in here. They are:
  12. - entry updated in replicated area.
  13. - backoff timeout
  14. - enter/leave.
  15. Perhaps these events should be properties of the main protocol.
  16. */
  17. #include "repl.h"
  18. #include "repl5.h"
  19. #include "repl5_ruv.h"
  20. #include "repl5_prot_private.h"
  21. #include "cl5_api.h"
  22. extern int slapi_log_urp;
  23. /*** from proto-slap.h ***/
  24. void ava_done(struct ava *ava);
  25. typedef struct repl5_inc_private
  26. {
  27. char *ruv; /* RUV on remote replica (use diff type for this? - ggood */
  28. Backoff_Timer *backoff;
  29. Repl_Protocol *rp;
  30. PRLock *lock;
  31. PRUint32 eventbits;
  32. } repl5_inc_private;
  33. /* Various states the incremental protocol can pass through */
  34. #define STATE_START 0 /* ONREPL - should we rename this - we don't use it just to start up? */
  35. #define STATE_WAIT_WINDOW_OPEN 1
  36. #define STATE_WAIT_CHANGES 2
  37. #define STATE_READY_TO_ACQUIRE 3
  38. #define STATE_BACKOFF_START 4 /* ONREPL - can we combine BACKOFF_START and BACKOFF states? */
  39. #define STATE_BACKOFF 5
  40. #define STATE_SENDING_UPDATES 6
  41. #define STATE_STOP_FATAL_ERROR 7
  42. #define STATE_STOP_FATAL_ERROR_PART2 8
  43. #define STATE_STOP_NORMAL_TERMINATION 9
  44. /* Events (synchronous and asynchronous; these are bits) */
  45. #define EVENT_WINDOW_OPENED 1
  46. #define EVENT_WINDOW_CLOSED 2
  47. #define EVENT_TRIGGERING_CRITERIA_MET 4 /* ONREPL - should we rename this to EVENT_CHANGE_AVAILABLE */
  48. #define EVENT_BACKOFF_EXPIRED 8
  49. #define EVENT_REPLICATE_NOW 16
  50. #define EVENT_PROTOCOL_SHUTDOWN 32
  51. #define EVENT_AGMT_CHANGED 64
  52. #define UPDATE_NO_MORE_UPDATES 201
  53. #define UPDATE_TRANSIENT_ERROR 202
  54. #define UPDATE_FATAL_ERROR 203
  55. #define UPDATE_SCHEDULE_WINDOW_CLOSED 204
  56. #define UPDATE_CONNECTION_LOST 205
  57. #define UPDATE_TIMEOUT 206
  58. #define UPDATE_YIELD 207
  59. /* Return codes from examine_update_vector */
  60. #define EXAMINE_RUV_PRISTINE_REPLICA 401
  61. #define EXAMINE_RUV_GENERATION_MISMATCH 402
  62. #define EXAMINE_RUV_REPLICA_TOO_OLD 403
  63. #define EXAMINE_RUV_OK 404
  64. #define EXAMINE_RUV_PARAM_ERROR 405
  65. #define MAX_CHANGES_PER_SESSION 10000
  66. /*
  67. * Maximum time to wait between replication sessions. If we
  68. * don't see any updates for a period equal to this interval,
  69. * we go ahead and start a replication session, just to be safe
  70. */
  71. #define MAX_WAIT_BETWEEN_SESSIONS PR_SecondsToInterval(60 * 5) /* 5 minutes */
  72. /*
  73. * tests if the protocol has been shutdown and we need to quit
  74. * event_occurred resets the bits in the bit flag, so whoever tests for shutdown
  75. * resets the flags, so the next one who tests for shutdown won't get it, so we
  76. * also look at the terminate flag
  77. */
  78. #define PROTOCOL_IS_SHUTDOWN(prp) (event_occurred(prp, EVENT_PROTOCOL_SHUTDOWN) || prp->terminate)
  79. /* Forward declarations */
  80. static PRUint32 event_occurred(Private_Repl_Protocol *prp, PRUint32 event);
  81. static void reset_events (Private_Repl_Protocol *prp);
  82. static void protocol_sleep(Private_Repl_Protocol *prp, PRIntervalTime duration);
  83. static int send_updates(Private_Repl_Protocol *prp, RUV *ruv, PRUint32 *num_changes_sent);
  84. static void repl5_inc_backoff_expired(time_t timer_fire_time, void *arg);
  85. static int examine_update_vector(Private_Repl_Protocol *prp, RUV *ruv);
  86. static PRBool ignore_error_and_keep_going(int error);
  87. static const char* state2name (int state);
  88. static const char* event2name (int event);
  89. static const char* op2string (int op);
  90. /*
  91. * It's specifically ok to delete a protocol instance that
  92. * is currently running. The instance will be shut down, and
  93. * then resources will be freed. Since a graceful shutdown is
  94. * attempted, this function may take some time to complete.
  95. */
  96. static void
  97. repl5_inc_delete(Private_Repl_Protocol **prpp)
  98. {
  99. /* First, stop the protocol if it isn't already stopped */
  100. /* Then, delete all resources used by the protocol */
  101. }
  102. /* helper function */
  103. void
  104. set_pause_and_busy_time(long *pausetime, long *busywaittime)
  105. {
  106. /* If neither are set, set busy time to its default */
  107. if (!*pausetime && !*busywaittime)
  108. {
  109. *busywaittime = PROTOCOL_BUSY_BACKOFF_MINIMUM;
  110. }
  111. /* pause time must be at least 1 more than the busy backoff time */
  112. if (*pausetime && !*busywaittime)
  113. {
  114. /*
  115. * user specified a pause time but no busy wait time - must
  116. * set busy wait time to 1 less than pause time - if pause
  117. * time is 1, we must set it to 2
  118. */
  119. if (*pausetime < 2)
  120. {
  121. *pausetime = 2;
  122. }
  123. *busywaittime = *pausetime - 1;
  124. }
  125. else if (!*pausetime && *busywaittime)
  126. {
  127. /*
  128. * user specified a busy wait time but no pause time - must
  129. * set pause time to 1 more than busy wait time
  130. */
  131. *pausetime = *busywaittime + 1;
  132. }
  133. else if (*pausetime && *busywaittime && *pausetime <= *busywaittime)
  134. {
  135. /*
  136. * user specified both pause and busy wait times, but the pause
  137. * time was <= busy wait time - pause time must be at least
  138. * 1 more than the busy wait time
  139. */
  140. *pausetime = *busywaittime + 1;
  141. }
  142. }
  143. /*
  144. * Do the incremental protocol.
  145. *
  146. * What's going on here? This thing is a state machine. It has the
  147. * following states:
  148. *
  149. * State transition table:
  150. *
  151. * Curr State Condition/Event Next State
  152. * ---------- ------------ -----------
  153. * START schedule window is open ACQUIRE_REPLICA
  154. * schedule window is closed WAIT_WINDOW_OPEN
  155. * WAIT_WINDOW_OPEN schedule change START
  156. * replicate now ACQUIRE_REPLICA
  157. * schedule window opens ACQUIRE_REPLICA
  158. * ACQUIRE_REPLICA acquired replica SEND_CHANGES
  159. * failed to acquire - transient error START_BACKOFF
  160. * failed to acquire - fatal error STOP_FATAL_ERROR
  161. * SEND_CHANGES can't update CONSUMER_NEEDS_REINIT
  162. * no changes to send WAIT_CHANGES
  163. * can't send - thransient error START_BACKOF
  164. * can't send - window closed WAIT_WINDOW_OPEN
  165. * can'r send - fatal error STOP_FATAL_ERROR
  166. * START_BACKOF replicate now ACQUIRE_REPLICA
  167. * schedule changes START
  168. * schedule window closes WAIT_WINDOW_OPEN
  169. * backoff expires & can acquire SEND_CHANGES
  170. * backoff expires & can't acquire-trans BACKOFF
  171. * backoff expires & can't acquire-fatal STOP_FATAL_ERROR
  172. * BACKOF replicate now ACQUIRE_REPLICA
  173. * schedule changes START
  174. * schedule window closes WAIT_WINDOW_OPEN
  175. * backoff expires & can acquire SEND_CHANGES
  176. * backoff expires & can't acquire-trans BACKOFF
  177. * backoff expires & can't acquire-fatal STOP_FATAL_ERROR
  178. * WAIT_CHANGES schedule window closes WAIT_WINDOW_OPEN
  179. * replicate_now ACQUIRE_REPLICA
  180. * change available ACQUIRE_REPLICA
  181. * schedule_change START
  182. */
  183. /*
  184. * Main state machine for the incremental protocol. This routine will,
  185. * under normal circumstances, not return until the protocol is shut
  186. * down.
  187. */
  188. static void
  189. repl5_inc_run(Private_Repl_Protocol *prp)
  190. {
  191. int current_state = STATE_START;
  192. int next_state = STATE_START;
  193. repl5_inc_private *prp_priv = (repl5_inc_private *)prp->private;
  194. int done;
  195. int e1;
  196. RUV *ruv = NULL;
  197. CSN *cons_schema_csn;
  198. Replica *replica;
  199. int wait_change_timer_set = 0;
  200. time_t last_start_time;
  201. PRUint32 num_changes_sent;
  202. char *hostname = NULL;
  203. int portnum = 0;
  204. /* use a different backoff timer strategy for ACQUIRE_REPLICA_BUSY errors */
  205. PRBool use_busy_backoff_timer = PR_FALSE;
  206. long pausetime = 0;
  207. long busywaittime = 0;
  208. prp->stopped = 0;
  209. prp->terminate = 0;
  210. hostname = agmt_get_hostname(prp->agmt);
  211. portnum = agmt_get_port(prp->agmt);
  212. /* establish_protocol_callbacks(prp); */
  213. done = 0;
  214. do {
  215. int rc;
  216. /* Take action, based on current state, and compute new state. */
  217. switch (current_state)
  218. {
  219. case STATE_START:
  220. dev_debug("repl5_inc_run(STATE_START)");
  221. if (PROTOCOL_IS_SHUTDOWN(prp))
  222. {
  223. done = 1;
  224. break;
  225. }
  226. /*
  227. * Our initial state. See if we're in a schedule window. If
  228. * so, then we're ready to acquire the replica and see if it
  229. * needs any updates from us. If not, then wait for the window
  230. * to open.
  231. */
  232. if (agmt_schedule_in_window_now(prp->agmt))
  233. {
  234. next_state = STATE_READY_TO_ACQUIRE;
  235. }
  236. else
  237. {
  238. next_state = STATE_WAIT_WINDOW_OPEN;
  239. }
  240. /* we can get here from other states because some events happened and were
  241. not cleared. For instance when we wake up in STATE_WAIT_CHANGES state.
  242. Since this is a fresh start state, we should clear all events */
  243. /* ONREPL - this does not feel right - we should take another look
  244. at this state machine */
  245. reset_events (prp);
  246. /* Cancel any linger timer that might be in effect... */
  247. conn_cancel_linger(prp->conn);
  248. /* ... and disconnect, if currently connected */
  249. conn_disconnect(prp->conn);
  250. /* get the new pause time, if any */
  251. pausetime = agmt_get_pausetime(prp->agmt);
  252. /* get the new busy wait time, if any */
  253. busywaittime = agmt_get_busywaittime(prp->agmt);
  254. if (pausetime || busywaittime)
  255. {
  256. /* helper function to make sure they are set correctly */
  257. set_pause_and_busy_time(&pausetime, &busywaittime);
  258. }
  259. break;
  260. case STATE_WAIT_WINDOW_OPEN:
  261. /*
  262. * We're waiting for a schedule window to open. If one did,
  263. * or we receive a "replicate now" event, then start a protocol
  264. * session immediately. If the replication schedule changed, go
  265. * back to start. Otherwise, go back to sleep.
  266. */
  267. dev_debug("repl5_inc_run(STATE_WAIT_WINDOW_OPEN)");
  268. if (PROTOCOL_IS_SHUTDOWN(prp))
  269. {
  270. done = 1;
  271. break;
  272. }
  273. else if (event_occurred(prp, EVENT_WINDOW_OPENED))
  274. {
  275. next_state = STATE_READY_TO_ACQUIRE;
  276. }
  277. else if (event_occurred(prp, EVENT_REPLICATE_NOW))
  278. {
  279. next_state = STATE_READY_TO_ACQUIRE;
  280. }
  281. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  282. {
  283. next_state = STATE_START;
  284. conn_set_agmt_changed(prp->conn);
  285. }
  286. else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET)) /* change available */
  287. {
  288. /* just ignore it and go to sleep */
  289. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  290. }
  291. else if (e1 = event_occurred(prp, EVENT_WINDOW_CLOSED) ||
  292. event_occurred(prp, EVENT_BACKOFF_EXPIRED))
  293. {
  294. /* this events - should not occur - log a warning and go to sleep */
  295. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  296. "%s: Incremental protocol: "
  297. "event %s should not occur in state %s; going to sleep\n",
  298. agmt_get_long_name(prp->agmt),
  299. e1 ? event2name(EVENT_WINDOW_CLOSED) : event2name(EVENT_BACKOFF_EXPIRED),
  300. state2name(current_state));
  301. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  302. }
  303. else
  304. {
  305. /* wait until window opens or an event occurs */
  306. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  307. "%s: Incremental protocol: "
  308. "waiting for update window to open\n", agmt_get_long_name(prp->agmt));
  309. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  310. }
  311. break;
  312. case STATE_WAIT_CHANGES:
  313. /*
  314. * We're in a replication window, but we're waiting for more
  315. * changes to accumulate before we actually hook up and send
  316. * them.
  317. */
  318. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES)");
  319. if (PROTOCOL_IS_SHUTDOWN(prp))
  320. {
  321. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): PROTOCOL_IS_SHUTING_DOWN -> end repl5_inc_run\n");
  322. done = 1;
  323. break;
  324. }
  325. else if (event_occurred(prp, EVENT_REPLICATE_NOW))
  326. {
  327. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_REPLICATE_NOW received -> STATE_READY_TO_ACQUIRE\n");
  328. next_state = STATE_READY_TO_ACQUIRE;
  329. wait_change_timer_set = 0;
  330. }
  331. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  332. {
  333. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_AGMT_CHANGED received -> STATE_START\n");
  334. next_state = STATE_START;
  335. conn_set_agmt_changed(prp->conn);
  336. wait_change_timer_set = 0;
  337. }
  338. else if (event_occurred(prp, EVENT_WINDOW_CLOSED))
  339. {
  340. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_WINDOW_CLOSED received -> STATE_WAIT_WINDOW_OPEN\n");
  341. next_state = STATE_WAIT_WINDOW_OPEN;
  342. wait_change_timer_set = 0;
  343. }
  344. else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET))
  345. {
  346. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_TRIGGERING_CRITERIA_MET received -> STATE_READY_TO_ACQUIRE\n");
  347. next_state = STATE_READY_TO_ACQUIRE;
  348. wait_change_timer_set = 0;
  349. }
  350. else if (e1 = event_occurred(prp, EVENT_WINDOW_OPENED) ||
  351. event_occurred(prp, EVENT_BACKOFF_EXPIRED))
  352. {
  353. /* this events - should not occur - log a warning and clear the event */
  354. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: Incremental protocol: "
  355. "event %s should not occur in state %s\n",
  356. agmt_get_long_name(prp->agmt),
  357. e1 ? event2name(EVENT_WINDOW_OPENED) : event2name(EVENT_BACKOFF_EXPIRED),
  358. state2name(current_state));
  359. wait_change_timer_set = 0;
  360. }
  361. else
  362. {
  363. if (wait_change_timer_set)
  364. {
  365. /* We are here because our timer expired */
  366. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): wait_change_timer_set expired -> STATE_START\n");
  367. next_state = STATE_START;
  368. wait_change_timer_set = 0;
  369. }
  370. else
  371. {
  372. /* We are here because the last replication session
  373. * finished or aborted.
  374. */
  375. wait_change_timer_set = 1;
  376. protocol_sleep(prp, MAX_WAIT_BETWEEN_SESSIONS);
  377. }
  378. }
  379. break;
  380. case STATE_READY_TO_ACQUIRE:
  381. dev_debug("repl5_inc_run(STATE_READY_TO_ACQUIRE)");
  382. if (PROTOCOL_IS_SHUTDOWN(prp))
  383. {
  384. done = 1;
  385. break;
  386. }
  387. /* ONREPL - at this state we unconditionally acquire the replica
  388. ignoring all events. Not sure if this is good */
  389. object_acquire(prp->replica_object);
  390. replica = object_get_data(prp->replica_object);
  391. rc = acquire_replica(prp, REPL_NSDS50_INCREMENTAL_PROTOCOL_OID, &ruv);
  392. use_busy_backoff_timer = PR_FALSE; /* default */
  393. if (rc == ACQUIRE_SUCCESS)
  394. {
  395. next_state = STATE_SENDING_UPDATES;
  396. }
  397. else if (rc == ACQUIRE_REPLICA_BUSY)
  398. {
  399. next_state = STATE_BACKOFF_START;
  400. use_busy_backoff_timer = PR_TRUE;
  401. }
  402. else if (rc == ACQUIRE_CONSUMER_WAS_UPTODATE)
  403. {
  404. next_state = STATE_WAIT_CHANGES;
  405. }
  406. else if (rc == ACQUIRE_TRANSIENT_ERROR)
  407. {
  408. next_state = STATE_BACKOFF_START;
  409. }
  410. else if (rc == ACQUIRE_FATAL_ERROR)
  411. {
  412. next_state = STATE_STOP_FATAL_ERROR;
  413. }
  414. if (rc != ACQUIRE_SUCCESS)
  415. {
  416. int optype, ldaprc;
  417. conn_get_error(prp->conn, &optype, &ldaprc);
  418. agmt_set_last_update_status(prp->agmt, ldaprc,
  419. prp->last_acquire_response_code, NULL);
  420. }
  421. object_release(prp->replica_object); replica = NULL;
  422. break;
  423. case STATE_BACKOFF_START:
  424. dev_debug("repl5_inc_run(STATE_BACKOFF_START)");
  425. if (PROTOCOL_IS_SHUTDOWN(prp))
  426. {
  427. done = 1;
  428. break;
  429. }
  430. if (event_occurred(prp, EVENT_REPLICATE_NOW))
  431. {
  432. next_state = STATE_READY_TO_ACQUIRE;
  433. }
  434. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  435. {
  436. next_state = STATE_START;
  437. conn_set_agmt_changed(prp->conn);
  438. }
  439. else if (event_occurred (prp, EVENT_WINDOW_CLOSED))
  440. {
  441. next_state = STATE_WAIT_WINDOW_OPEN;
  442. }
  443. else if (event_occurred (prp, EVENT_TRIGGERING_CRITERIA_MET))
  444. {
  445. /* consume and ignore */
  446. }
  447. else if (e1 = event_occurred (prp, EVENT_WINDOW_OPENED) ||
  448. event_occurred (prp, EVENT_BACKOFF_EXPIRED))
  449. {
  450. /* This should never happen */
  451. /* this events - should not occur - log a warning and go to sleep */
  452. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  453. "%s: Incremental protocol: event %s should not occur in state %s\n",
  454. agmt_get_long_name(prp->agmt),
  455. e1 ? event2name(EVENT_WINDOW_OPENED) : event2name(EVENT_BACKOFF_EXPIRED),
  456. state2name(current_state));
  457. }
  458. else
  459. {
  460. /* Set up the backoff timer to wake us up at the appropriate time */
  461. if (use_busy_backoff_timer)
  462. {
  463. /* we received a busy signal from the consumer, wait for a while */
  464. if (!busywaittime)
  465. {
  466. busywaittime = PROTOCOL_BUSY_BACKOFF_MINIMUM;
  467. }
  468. prp_priv->backoff = backoff_new(BACKOFF_FIXED, busywaittime,
  469. busywaittime);
  470. }
  471. else
  472. {
  473. prp_priv->backoff = backoff_new(BACKOFF_EXPONENTIAL, PROTOCOL_BACKOFF_MINIMUM,
  474. PROTOCOL_BACKOFF_MAXIMUM);
  475. }
  476. next_state = STATE_BACKOFF;
  477. backoff_reset(prp_priv->backoff, repl5_inc_backoff_expired, (void *)prp);
  478. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  479. use_busy_backoff_timer = PR_FALSE;
  480. }
  481. break;
  482. case STATE_BACKOFF:
  483. /*
  484. * We're in a backoff state.
  485. */
  486. dev_debug("repl5_inc_run(STATE_BACKOFF)");
  487. if (PROTOCOL_IS_SHUTDOWN(prp))
  488. {
  489. if (prp_priv->backoff)
  490. backoff_delete(&prp_priv->backoff);
  491. done = 1;
  492. break;
  493. }
  494. else if (event_occurred(prp, EVENT_REPLICATE_NOW))
  495. {
  496. next_state = STATE_READY_TO_ACQUIRE;
  497. }
  498. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  499. {
  500. next_state = STATE_START;
  501. conn_set_agmt_changed(prp->conn);
  502. /* Destroy the backoff timer, since we won't need it anymore */
  503. if (prp_priv->backoff)
  504. backoff_delete(&prp_priv->backoff);
  505. }
  506. else if (event_occurred(prp, EVENT_WINDOW_CLOSED))
  507. {
  508. next_state = STATE_WAIT_WINDOW_OPEN;
  509. /* Destroy the backoff timer, since we won't need it anymore */
  510. if (prp_priv->backoff)
  511. backoff_delete(&prp_priv->backoff);
  512. }
  513. else if (event_occurred(prp, EVENT_BACKOFF_EXPIRED))
  514. {
  515. rc = acquire_replica(prp, REPL_NSDS50_INCREMENTAL_PROTOCOL_OID, &ruv);
  516. use_busy_backoff_timer = PR_FALSE;
  517. if (rc == ACQUIRE_SUCCESS)
  518. {
  519. next_state = STATE_SENDING_UPDATES;
  520. }
  521. else if (rc == ACQUIRE_REPLICA_BUSY)
  522. {
  523. next_state = STATE_BACKOFF;
  524. use_busy_backoff_timer = PR_TRUE;
  525. }
  526. else if (rc == ACQUIRE_CONSUMER_WAS_UPTODATE)
  527. {
  528. next_state = STATE_WAIT_CHANGES;
  529. }
  530. else if (rc == ACQUIRE_TRANSIENT_ERROR)
  531. {
  532. next_state = STATE_BACKOFF;
  533. }
  534. else if (rc == ACQUIRE_FATAL_ERROR)
  535. {
  536. next_state = STATE_STOP_FATAL_ERROR;
  537. }
  538. if (rc != ACQUIRE_SUCCESS)
  539. {
  540. int optype, ldaprc;
  541. conn_get_error(prp->conn, &optype, &ldaprc);
  542. agmt_set_last_update_status(prp->agmt, ldaprc,
  543. prp->last_acquire_response_code, NULL);
  544. }
  545. /*
  546. * We either need to step the backoff timer, or
  547. * destroy it if we don't need it anymore.
  548. */
  549. if (STATE_BACKOFF == next_state)
  550. {
  551. time_t next_fire_time;
  552. time_t now;
  553. /* Step the backoff timer */
  554. time(&now);
  555. next_fire_time = backoff_step(prp_priv->backoff);
  556. /* And go back to sleep */
  557. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  558. "%s: Replication session backing off for %d seconds\n",
  559. agmt_get_long_name(prp->agmt),
  560. next_fire_time - now);
  561. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  562. }
  563. else
  564. {
  565. /* Destroy the backoff timer, since we won't need it anymore */
  566. backoff_delete(&prp_priv->backoff);
  567. }
  568. use_busy_backoff_timer = PR_FALSE;
  569. }
  570. else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET))
  571. {
  572. /* changes are available */
  573. if ( prp_priv->backoff == NULL || backoff_expired (prp_priv->backoff, 60) )
  574. {
  575. /*
  576. * Have seen cases that the agmt stuck here forever since
  577. * somehow the backoff timer was not in event queue anymore.
  578. * If the backoff timer has expired more than 60 seconds,
  579. * destroy it.
  580. */
  581. if ( prp_priv->backoff )
  582. backoff_delete(&prp_priv->backoff);
  583. next_state = STATE_READY_TO_ACQUIRE;
  584. }
  585. else
  586. {
  587. /* ignore changes and go to sleep */
  588. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  589. }
  590. }
  591. else if (event_occurred(prp, EVENT_WINDOW_OPENED))
  592. {
  593. /* this should never happen - log an error and go to sleep */
  594. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: Incremental protocol: "
  595. "event %s should not occur in state %s; going to sleep\n",
  596. agmt_get_long_name(prp->agmt),
  597. event2name(EVENT_WINDOW_OPENED), state2name(current_state));
  598. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  599. }
  600. break;
  601. case STATE_SENDING_UPDATES:
  602. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES)");
  603. agmt_set_update_in_progress(prp->agmt, PR_TRUE);
  604. num_changes_sent = 0;
  605. last_start_time = current_time();
  606. agmt_set_last_update_start(prp->agmt, last_start_time);
  607. /*
  608. * We've acquired the replica, and are ready to send any
  609. * needed updates.
  610. */
  611. if (PROTOCOL_IS_SHUTDOWN(prp))
  612. {
  613. release_replica (prp);
  614. done = 1;
  615. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  616. agmt_set_last_update_end(prp->agmt, current_time());
  617. /* MAB: I don't find the following status correct. How do we know it has
  618. been stopped by an admin and not by a total update request, for instance?
  619. In any case, how is this protocol shutdown situation different from all the
  620. other ones that are present in this state machine? */
  621. /* richm: We at least need to let monitors know that the protocol has been
  622. shutdown - maybe they can figure out why */
  623. agmt_set_last_update_status(prp->agmt, 0, 0, "Protocol stopped");
  624. break;
  625. }
  626. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update started");
  627. /* ONREPL - in this state we send changes no matter what other events occur.
  628. This is because we can get because of the REPLICATE_NOW event which
  629. has high priority. Is this ok? */
  630. /* First, push new schema to the consumer if needed */
  631. /* ONREPL - should we push schema after we examine the RUV? */
  632. /*
  633. * GGOOREPL - I don't see why we should wait until we've
  634. * examined the RUV. The schema entry has its own CSN that is
  635. * used to decide if the remote schema needs to be updated.
  636. */
  637. cons_schema_csn = agmt_get_consumer_schema_csn ( prp->agmt );
  638. rc = conn_push_schema(prp->conn, &cons_schema_csn);
  639. if ( cons_schema_csn != agmt_get_consumer_schema_csn ( prp->agmt ))
  640. {
  641. agmt_set_consumer_schema_csn ( prp->agmt, cons_schema_csn );
  642. }
  643. if (CONN_SCHEMA_UPDATED != rc && CONN_SCHEMA_NO_UPDATE_NEEDED != rc)
  644. {
  645. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  646. "%s: Warning: unable to replicate schema: rc=%d\n",
  647. agmt_get_long_name(prp->agmt), rc);
  648. /* But keep going */
  649. }
  650. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> examine_update_vector");
  651. rc = examine_update_vector(prp, ruv);
  652. /*
  653. * Decide what to do next - proceed with incremental,
  654. * backoff, or total update
  655. */
  656. switch (rc)
  657. {
  658. case EXAMINE_RUV_PARAM_ERROR:
  659. /* this is really bad - we have NULL prp! */
  660. next_state = STATE_STOP_FATAL_ERROR;
  661. break;
  662. case EXAMINE_RUV_PRISTINE_REPLICA:
  663. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  664. "%s: Replica has no update vector. It has never been initialized.\n",
  665. agmt_get_long_name(prp->agmt));
  666. next_state = STATE_BACKOFF_START;
  667. break;
  668. case EXAMINE_RUV_GENERATION_MISMATCH:
  669. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  670. "%s: Replica has a different generation ID than the local data.\n",
  671. agmt_get_long_name(prp->agmt));
  672. next_state = STATE_BACKOFF_START;
  673. break;
  674. case EXAMINE_RUV_REPLICA_TOO_OLD:
  675. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  676. "%s: Replica update vector is too out of date to bring "
  677. "into sync using the incremental protocol. The replica "
  678. "must be reinitialized.\n", agmt_get_long_name(prp->agmt));
  679. next_state = STATE_BACKOFF_START;
  680. break;
  681. case EXAMINE_RUV_OK:
  682. /* update our csn generator state with the consumer's ruv data */
  683. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> examine_update_vector OK");
  684. object_acquire(prp->replica_object);
  685. replica = object_get_data(prp->replica_object);
  686. rc = replica_update_csngen_state (replica, ruv);
  687. object_release (prp->replica_object);
  688. replica = NULL;
  689. if (rc != 0) /* too much skew */
  690. {
  691. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  692. "%s: Incremental protocol: fatal error - too much time skew between replicas!\n",
  693. agmt_get_long_name(prp->agmt));
  694. next_state = STATE_STOP_FATAL_ERROR;
  695. }
  696. else
  697. {
  698. rc = send_updates(prp, ruv, &num_changes_sent);
  699. if (rc == UPDATE_NO_MORE_UPDATES)
  700. {
  701. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_NO_MORE_UPDATES -> STATE_WAIT_CHANGES");
  702. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update succeeded");
  703. next_state = STATE_WAIT_CHANGES;
  704. }
  705. else if (rc == UPDATE_YIELD)
  706. {
  707. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_YIELD -> STATE_BACKOFF_START");
  708. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update succeeded and yielded");
  709. next_state = STATE_BACKOFF_START;
  710. }
  711. else if (rc == UPDATE_TRANSIENT_ERROR)
  712. {
  713. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_TRANSIENT_ERROR -> STATE_BACKOFF_START");
  714. next_state = STATE_BACKOFF_START;
  715. }
  716. else if (rc == UPDATE_FATAL_ERROR)
  717. {
  718. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_FATAL_ERROR -> STATE_STOP_FATAL_ERROR");
  719. next_state = STATE_STOP_FATAL_ERROR;
  720. }
  721. else if (rc == UPDATE_SCHEDULE_WINDOW_CLOSED)
  722. {
  723. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_SCHEDULE_WINDOW_CLOSED -> STATE_WAIT_WINDOW_OPEN");
  724. /* ONREPL - I don't think we should check this. We might be
  725. here because of replicate_now event - so we don't care
  726. about the schedule */
  727. next_state = STATE_WAIT_WINDOW_OPEN;
  728. /* ONREPL - do we need to release the replica here ? */
  729. conn_disconnect (prp->conn);
  730. }
  731. else if (rc == UPDATE_CONNECTION_LOST)
  732. {
  733. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_CONNECTION_LOST -> STATE_BACKOFF_START");
  734. next_state = STATE_BACKOFF_START;
  735. }
  736. else if (rc == UPDATE_TIMEOUT)
  737. {
  738. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_TIMEOUT -> STATE_BACKOFF_START");
  739. next_state = STATE_BACKOFF_START;
  740. }
  741. }
  742. last_start_time = 0UL;
  743. break;
  744. }
  745. if (NULL != ruv)
  746. {
  747. ruv_destroy(&ruv); ruv = NULL;
  748. }
  749. agmt_set_last_update_end(prp->agmt, current_time());
  750. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  751. /* If timed out, close the connection after released the replica */
  752. release_replica(prp);
  753. if (rc == UPDATE_TIMEOUT) {
  754. conn_disconnect(prp->conn);
  755. }
  756. if (rc == UPDATE_NO_MORE_UPDATES && num_changes_sent > 0)
  757. {
  758. if (pausetime > 0)
  759. {
  760. /* richm - 20020219 - If we have acquired the consumer, and another master has gone
  761. into backoff waiting for us to release it, we may acquire the replica sooner
  762. than the other master has a chance to, and the other master may not be able
  763. to acquire the consumer for a long time (hours, days?) if this server is
  764. under a heavy load (see reliab06 et. al. system tests)
  765. So, this sleep gives the other master(s) a chance to acquire the consumer
  766. replica */
  767. long loops = pausetime;
  768. /* the while loop is so that we don't just sleep and sleep if an
  769. event comes in that we should handle immediately (like shutdown) */
  770. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  771. "%s: Pausing updates for %ld seconds to allow other suppliers to update consumer\n",
  772. agmt_get_long_name(prp->agmt), pausetime);
  773. while (loops-- && !(PROTOCOL_IS_SHUTDOWN(prp)))
  774. {
  775. DS_Sleep(PR_SecondsToInterval(1));
  776. }
  777. }
  778. else if (num_changes_sent > 10)
  779. {
  780. /* wait for consumer to write its ruv if the replication was busy */
  781. /* When asked, consumer sends its ruv in cache to the supplier. */
  782. /* DS_Sleep ( PR_SecondsToInterval(1) ); */
  783. }
  784. }
  785. break;
  786. case STATE_STOP_FATAL_ERROR:
  787. /*
  788. * We encountered some sort of a fatal error. Suspend.
  789. */
  790. /* XXXggood update state in replica */
  791. agmt_set_last_update_status(prp->agmt, -1, 0, "Incremental update has failed and requires administrator action");
  792. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR)");
  793. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  794. "%s: Incremental update failed and requires administrator action\n",
  795. agmt_get_long_name(prp->agmt));
  796. next_state = STATE_STOP_FATAL_ERROR_PART2;
  797. break;
  798. case STATE_STOP_FATAL_ERROR_PART2:
  799. if (PROTOCOL_IS_SHUTDOWN(prp))
  800. {
  801. done = 1;
  802. break;
  803. }
  804. /* MAB: This state is the FATAL state where we are supposed to get
  805. as a result of a FATAL error on send_updates. But, as bug
  806. states, send_updates was always returning TRANSIENT errors and never
  807. FATAL... In other words, this code has never been tested before...
  808. As of 01/16/01, this piece of code was in a very dangerous state. In particular,
  809. 1) it does not catch any events
  810. 2) it is a terminal state (once reached it never transitions to a different state)
  811. Both things combined make this state to become a consuming infinite loop
  812. that is useless after all (we are in a fatal place requiring manual admin jobs */
  813. /* MAB: The following lines fix problem number 1 above... When the code gets
  814. into this state, it should only get a chance to get out of it by an
  815. EVENT_AGMT_CHANGED event... All other events should be ignored */
  816. else if (event_occurred(prp, EVENT_AGMT_CHANGED))
  817. {
  818. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR): EVENT_AGMT_CHANGED received\n");
  819. /* Chance to recover for the EVENT_AGMT_CHANGED event.
  820. This is not mandatory, but fixes problem 2 above */
  821. next_state = STATE_STOP_NORMAL_TERMINATION;
  822. }
  823. else
  824. {
  825. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR): Event received. Clearing it\n");
  826. reset_events (prp);
  827. }
  828. protocol_sleep (prp, PR_INTERVAL_NO_TIMEOUT);
  829. break;
  830. case STATE_STOP_NORMAL_TERMINATION:
  831. /*
  832. * We encountered some sort of a fatal error. Return.
  833. */
  834. /* XXXggood update state in replica */
  835. dev_debug("repl5_inc_run(STATE_STOP_NORMAL_TERMINATION)");
  836. done = 1;
  837. break;
  838. }
  839. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  840. "%s: State: %s -> %s\n",
  841. agmt_get_long_name(prp->agmt),
  842. state2name(current_state), state2name(next_state));
  843. current_state = next_state;
  844. } while (!done);
  845. slapi_ch_free((void**)&hostname);
  846. /* remove_protocol_callbacks(prp); */
  847. prp->stopped = 1;
  848. /* Cancel any linger timer that might be in effect... */
  849. conn_cancel_linger(prp->conn);
  850. /* ... and disconnect, if currently connected */
  851. conn_disconnect(prp->conn);
  852. }
  853. /*
  854. * Go to sleep until awakened.
  855. */
  856. static void
  857. protocol_sleep(Private_Repl_Protocol *prp, PRIntervalTime duration)
  858. {
  859. PR_ASSERT(NULL != prp);
  860. PR_Lock(prp->lock);
  861. /* we should not go to sleep if there are events available to be processed.
  862. Otherwise, we can miss the event that suppose to wake us up */
  863. if (prp->eventbits == 0)
  864. PR_WaitCondVar(prp->cvar, duration);
  865. else
  866. {
  867. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  868. "%s: Incremental protocol: can't go to sleep: event bits - %x\n",
  869. agmt_get_long_name(prp->agmt), prp->eventbits);
  870. }
  871. PR_Unlock(prp->lock);
  872. }
  873. /*
  874. * Notify the protocol about some event. Signal the condition
  875. * variable in case the protocol is sleeping. Multiple occurences
  876. * of a single event type are not remembered (e.g. no stack
  877. * of events is maintained).
  878. */
  879. static void
  880. event_notify(Private_Repl_Protocol *prp, PRUint32 event)
  881. {
  882. PR_ASSERT(NULL != prp);
  883. PR_Lock(prp->lock);
  884. prp->eventbits |= event;
  885. PR_NotifyCondVar(prp->cvar);
  886. PR_Unlock(prp->lock);
  887. }
  888. /*
  889. * Test to see if an event occurred. The event is cleared when
  890. * read.
  891. */
  892. static PRUint32
  893. event_occurred(Private_Repl_Protocol *prp, PRUint32 event)
  894. {
  895. PRUint32 return_value;
  896. PR_ASSERT(NULL != prp);
  897. PR_Lock(prp->lock);
  898. return_value = (prp->eventbits & event);
  899. prp->eventbits &= ~event; /* Clear event */
  900. PR_Unlock(prp->lock);
  901. return return_value;
  902. }
  903. static void
  904. reset_events (Private_Repl_Protocol *prp)
  905. {
  906. PR_ASSERT(NULL != prp);
  907. PR_Lock(prp->lock);
  908. prp->eventbits = 0;
  909. PR_Unlock(prp->lock);
  910. }
  911. /*
  912. * Replay the actual update to the consumer. Construct an appropriate LDAP
  913. * operation, attach the baggage LDAPv3 control that contains the CSN, etc.,
  914. * and send the operation to the consumer.
  915. */
  916. ConnResult
  917. replay_update(Private_Repl_Protocol *prp, slapi_operation_parameters *op)
  918. {
  919. ConnResult return_value;
  920. LDAPControl *update_control;
  921. char *parentuniqueid;
  922. LDAPMod **modrdn_mods = NULL;
  923. char csn_str[CSN_STRSIZE]; /* For logging only */
  924. csn_as_string(op->csn, PR_FALSE, csn_str);
  925. /* Construct the replication info control that accompanies the operation */
  926. if (SLAPI_OPERATION_ADD == op->operation_type)
  927. {
  928. parentuniqueid = op->p.p_add.parentuniqueid;
  929. }
  930. else if (SLAPI_OPERATION_MODRDN == op->operation_type)
  931. {
  932. /*
  933. * For modrdn operations, we need to send along modified attributes, e.g.
  934. * modifytimestamp.
  935. * And the superior_uniqueid !
  936. */
  937. modrdn_mods = op->p.p_modrdn.modrdn_mods;
  938. parentuniqueid = op->p.p_modrdn.modrdn_newsuperior_address.uniqueid;
  939. }
  940. else
  941. {
  942. parentuniqueid = NULL;
  943. }
  944. if (create_NSDS50ReplUpdateInfoControl(op->target_address.uniqueid,
  945. parentuniqueid, op->csn, modrdn_mods, &update_control) != LDAP_SUCCESS)
  946. {
  947. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  948. "%s: replay_update: Unable to create NSDS50ReplUpdateInfoControl "
  949. "for operation with csn %s. Skipping update.\n",
  950. agmt_get_long_name(prp->agmt), csn_str);
  951. }
  952. else
  953. {
  954. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  955. "%s: replay_update: Sending %s operation (dn=\"%s\" csn=%s)\n",
  956. agmt_get_long_name(prp->agmt),
  957. op2string(op->operation_type), op->target_address.dn, csn_str);
  958. /* What type of operation is it? */
  959. switch (op->operation_type)
  960. {
  961. case SLAPI_OPERATION_ADD:
  962. {
  963. LDAPMod **entryattrs;
  964. /* Convert entry to mods */
  965. (void)slapi_entry2mods (op->p.p_add.target_entry,
  966. NULL /* &entrydn : We don't need it */,
  967. &entryattrs);
  968. if (NULL == entryattrs)
  969. {
  970. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  971. "%s: replay_update: Cannot convert entry to LDAPMods.\n",
  972. agmt_get_long_name(prp->agmt));
  973. return_value = CONN_LOCAL_ERROR;
  974. }
  975. else
  976. {
  977. return_value = conn_send_add(prp->conn, op->target_address.dn,
  978. entryattrs, update_control, NULL /* returned controls */);
  979. ldap_mods_free(entryattrs, 1);
  980. }
  981. break;
  982. }
  983. case SLAPI_OPERATION_MODIFY:
  984. return_value = conn_send_modify(prp->conn, op->target_address.dn,
  985. op->p.p_modify.modify_mods, update_control,
  986. NULL /* returned controls */);
  987. break;
  988. case SLAPI_OPERATION_DELETE:
  989. return_value = conn_send_delete(prp->conn, op->target_address.dn,
  990. update_control, NULL /* returned controls */);
  991. break;
  992. case SLAPI_OPERATION_MODRDN:
  993. /* XXXggood need to pass modrdn mods in update control! */
  994. return_value = conn_send_rename(prp->conn, op->target_address.dn,
  995. op->p.p_modrdn.modrdn_newrdn,
  996. op->p.p_modrdn.modrdn_newsuperior_address.dn,
  997. op->p.p_modrdn.modrdn_deloldrdn,
  998. update_control, NULL /* returned controls */);
  999. break;
  1000. default:
  1001. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: replay_update: Unknown "
  1002. "operation type %d found in changelog - skipping change.\n",
  1003. agmt_get_long_name(prp->agmt), op->operation_type);
  1004. }
  1005. destroy_NSDS50ReplUpdateInfoControl(&update_control);
  1006. }
  1007. if (CONN_OPERATION_SUCCESS == return_value)
  1008. {
  1009. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1010. "%s: replay_update: Consumer successfully replayed operation with csn %s\n",
  1011. agmt_get_long_name(prp->agmt), csn_str);
  1012. }
  1013. else
  1014. {
  1015. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1016. "%s: replay_update: Consumer could not replay operation with csn %s\n",
  1017. agmt_get_long_name(prp->agmt), csn_str);
  1018. }
  1019. return return_value;
  1020. }
  1021. static PRBool
  1022. is_dummy_operation (const slapi_operation_parameters *op)
  1023. {
  1024. return (strcmp (op->target_address.uniqueid, START_ITERATION_ENTRY_UNIQUEID) == 0);
  1025. }
  1026. void
  1027. cl5_operation_parameters_done (struct slapi_operation_parameters *sop)
  1028. {
  1029. if(sop!=NULL) {
  1030. switch(sop->operation_type)
  1031. {
  1032. case SLAPI_OPERATION_BIND:
  1033. slapi_ch_free((void **)&(sop->p.p_bind.bind_saslmechanism));
  1034. if (sop->p.p_bind.bind_creds)
  1035. ber_bvecfree((struct berval**)&(sop->p.p_bind.bind_creds));
  1036. if (sop->p.p_bind.bind_ret_saslcreds)
  1037. ber_bvecfree((struct berval**)&(sop->p.p_bind.bind_ret_saslcreds));
  1038. sop->p.p_bind.bind_creds = NULL;
  1039. sop->p.p_bind.bind_ret_saslcreds = NULL;
  1040. break;
  1041. case SLAPI_OPERATION_COMPARE:
  1042. ava_done((struct ava *)&(sop->p.p_compare.compare_ava));
  1043. break;
  1044. case SLAPI_OPERATION_SEARCH:
  1045. slapi_ch_free((void **)&(sop->p.p_search.search_strfilter));
  1046. charray_free(sop->p.p_search.search_attrs);
  1047. slapi_filter_free(sop->p.p_search.search_filter,1);
  1048. break;
  1049. case SLAPI_OPERATION_MODRDN:
  1050. sop->p.p_modrdn.modrdn_deloldrdn = 0;
  1051. break;
  1052. case SLAPI_OPERATION_EXTENDED:
  1053. slapi_ch_free((void **)&(sop->p.p_extended.exop_oid));
  1054. if (sop->p.p_extended.exop_value)
  1055. ber_bvecfree((struct berval**)&(sop->p.p_extended.exop_value));
  1056. sop->p.p_extended.exop_value = NULL;
  1057. break;
  1058. default:
  1059. break;
  1060. }
  1061. }
  1062. operation_parameters_done(sop);
  1063. }
  1064. /*
  1065. * Send a set of updates to the replica. Assumes that (1) the replica
  1066. * has already been acquired, (2) that the consumer's update vector has
  1067. * been checked and (3) that it's ok to send incremental updates.
  1068. * Returns:
  1069. * UPDATE_NO_MORE_UPDATES - all updates were sent succussfully
  1070. * UPDATE_TRANSIENT_ERROR - some non-permanent error occurred. Try again later.
  1071. * UPDATE_FATAL_ERROR - some bad, permanent error occurred.
  1072. * UPDATE_SCHEDULE_WINDOW_CLOSED - the schedule window closed on us.
  1073. */
  1074. static int
  1075. send_updates(Private_Repl_Protocol *prp, RUV *remote_update_vector, PRUint32 *num_changes_sent)
  1076. {
  1077. CL5Entry entry;
  1078. slapi_operation_parameters op;
  1079. int return_value;
  1080. int rc;
  1081. CL5ReplayIterator *changelog_iterator;
  1082. *num_changes_sent = 0;
  1083. /*
  1084. * Iterate over the changelog. Retrieve each update,
  1085. * construct an appropriate LDAP operation,
  1086. * attaching the CSN, and send the change.
  1087. */
  1088. rc = cl5CreateReplayIterator(prp, remote_update_vector, &changelog_iterator);
  1089. if (CL5_SUCCESS != rc)
  1090. {
  1091. switch (rc)
  1092. {
  1093. case CL5_BAD_DATA: /* invalid parameter passed to the function */
  1094. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1095. "%s: Invalid parameter passed to cl5CreateReplayIterator\n",
  1096. agmt_get_long_name(prp->agmt));
  1097. return_value = UPDATE_FATAL_ERROR;
  1098. break;
  1099. case CL5_BAD_FORMAT: /* db data has unexpected format */
  1100. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1101. "%s: Unexpected format encountered in changelog database\n",
  1102. agmt_get_long_name(prp->agmt));
  1103. return_value = UPDATE_FATAL_ERROR;
  1104. break;
  1105. case CL5_BAD_STATE: /* changelog is in an incorrect state for attempted operation */
  1106. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1107. "%s: Changelog database was in an incorrect state\n",
  1108. agmt_get_long_name(prp->agmt));
  1109. return_value = UPDATE_FATAL_ERROR;
  1110. break;
  1111. case CL5_BAD_DBVERSION: /* changelog has invalid dbversion */
  1112. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1113. "%s: Incorrect dbversion found in changelog database\n",
  1114. agmt_get_long_name(prp->agmt));
  1115. return_value = UPDATE_FATAL_ERROR;
  1116. break;
  1117. case CL5_DB_ERROR: /* database error */
  1118. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1119. "%s: A changelog database error was encountered\n",
  1120. agmt_get_long_name(prp->agmt));
  1121. return_value = UPDATE_FATAL_ERROR;
  1122. break;
  1123. case CL5_NOTFOUND: /* we have no changes to send */
  1124. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1125. "%s: No changes to send\n",
  1126. agmt_get_long_name(prp->agmt));
  1127. return_value = UPDATE_NO_MORE_UPDATES;
  1128. break;
  1129. case CL5_MEMORY_ERROR: /* memory allocation failed */
  1130. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1131. "%s: Memory allocation error occurred\n",
  1132. agmt_get_long_name(prp->agmt));
  1133. return_value = UPDATE_FATAL_ERROR;
  1134. break;
  1135. case CL5_SYSTEM_ERROR: /* NSPR error occurred: use PR_GetError for furhter info */
  1136. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1137. "%s: An NSPR error (%d) occurred\n",
  1138. agmt_get_long_name(prp->agmt), PR_GetError());
  1139. return_value = UPDATE_TRANSIENT_ERROR;
  1140. break;
  1141. case CL5_CSN_ERROR: /* CSN API failed */
  1142. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1143. "%s: A CSN API failure was encountered\n",
  1144. agmt_get_long_name(prp->agmt));
  1145. return_value = UPDATE_TRANSIENT_ERROR;
  1146. break;
  1147. case CL5_RUV_ERROR: /* RUV API failed */
  1148. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1149. "%s: An RUV API failure occurred\n",
  1150. agmt_get_long_name(prp->agmt));
  1151. return_value = UPDATE_TRANSIENT_ERROR;
  1152. break;
  1153. case CL5_OBJSET_ERROR: /* namedobjset api failed */
  1154. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1155. "%s: A namedobject API failure occurred\n",
  1156. agmt_get_long_name(prp->agmt));
  1157. return_value = UPDATE_TRANSIENT_ERROR;
  1158. break;
  1159. case CL5_PURGED_DATA: /* requested data has been purged */
  1160. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1161. "%s: Data required to update replica has been purged. "
  1162. "The replica must be reinitialized.\n",
  1163. agmt_get_long_name(prp->agmt));
  1164. return_value = UPDATE_FATAL_ERROR;
  1165. break;
  1166. case CL5_MISSING_DATA: /* data should be in the changelog, but is missing */
  1167. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1168. "%s: Missing data encountered\n",
  1169. agmt_get_long_name(prp->agmt));
  1170. return_value = UPDATE_FATAL_ERROR;
  1171. break;
  1172. case CL5_UNKNOWN_ERROR: /* unclassified error */
  1173. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1174. "%s: An unknown error was ecountered\n",
  1175. agmt_get_long_name(prp->agmt));
  1176. return_value = UPDATE_TRANSIENT_ERROR;
  1177. break;
  1178. default:
  1179. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1180. "%s: An unknown error (%d) occurred "
  1181. "(cl5CreateReplayIterator)\n",
  1182. agmt_get_long_name(prp->agmt), rc);
  1183. return_value = UPDATE_TRANSIENT_ERROR;
  1184. }
  1185. }
  1186. else
  1187. {
  1188. int finished = 0;
  1189. ConnResult replay_crc;
  1190. char csn_str[CSN_STRSIZE];
  1191. memset ( (void*)&op, 0, sizeof (op) );
  1192. entry.op = &op;
  1193. do {
  1194. cl5_operation_parameters_done ( entry.op );
  1195. memset ( (void*)entry.op, 0, sizeof (op) );
  1196. rc = cl5GetNextOperationToReplay(changelog_iterator, &entry);
  1197. switch (rc)
  1198. {
  1199. case CL5_SUCCESS:
  1200. /* check that we don't return dummy entries */
  1201. if (is_dummy_operation (entry.op))
  1202. {
  1203. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1204. "%s: changelog iteration code returned a dummy entry with csn %s, "
  1205. "skipping ...\n",
  1206. agmt_get_long_name(prp->agmt), csn_as_string(entry.op->csn, PR_FALSE, csn_str));
  1207. continue;
  1208. }
  1209. replay_crc = replay_update(prp, entry.op);
  1210. if (CONN_OPERATION_SUCCESS != replay_crc)
  1211. {
  1212. int operation, error;
  1213. conn_get_error(prp->conn, &operation, &error);
  1214. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1215. /* Figure out what to do next */
  1216. if (CONN_OPERATION_FAILED == replay_crc)
  1217. {
  1218. /* Map ldap error code to return value */
  1219. if (!ignore_error_and_keep_going(error))
  1220. {
  1221. return_value = UPDATE_TRANSIENT_ERROR;
  1222. finished = 1;
  1223. }
  1224. else
  1225. {
  1226. agmt_inc_last_update_changecount (prp->agmt, csn_get_replicaid(entry.op->csn), 1 /*skipped*/);
  1227. }
  1228. slapi_log_error(finished ? SLAPI_LOG_FATAL : slapi_log_urp, repl_plugin_name,
  1229. "%s: Consumer failed to replay change (uniqueid %s, CSN %s): %s. %s.\n",
  1230. agmt_get_long_name(prp->agmt),
  1231. entry.op->target_address.uniqueid, csn_str,
  1232. ldap_err2string(error),
  1233. finished ? "Will retry later" : "Skipping");
  1234. }
  1235. else if (CONN_NOT_CONNECTED == replay_crc)
  1236. {
  1237. /* We lost the connection - enter backoff state */
  1238. return_value = UPDATE_TRANSIENT_ERROR;
  1239. finished = 1;
  1240. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1241. "%s: Consumer failed to replay change (uniqueid %s, CSN %s): "
  1242. "%s. Will retry later.\n",
  1243. agmt_get_long_name(prp->agmt),
  1244. entry.op->target_address.uniqueid, csn_str,
  1245. error ? ldap_err2string(error) : "Connection lost");
  1246. }
  1247. else if (CONN_TIMEOUT == replay_crc)
  1248. {
  1249. return_value = UPDATE_TIMEOUT;
  1250. finished = 1;
  1251. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1252. "%s: Consumer timed out to replay change (uniqueid %s, CSN %s): "
  1253. "%s.\n",
  1254. agmt_get_long_name(prp->agmt),
  1255. entry.op->target_address.uniqueid, csn_str,
  1256. error ? ldap_err2string(error) : "Timeout");
  1257. }
  1258. else if (CONN_LOCAL_ERROR == replay_crc)
  1259. {
  1260. /*
  1261. * Something bad happened on the local server - enter
  1262. * backoff state.
  1263. */
  1264. return_value = UPDATE_TRANSIENT_ERROR;
  1265. finished = 1;
  1266. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1267. "%s: Failed to replay change (uniqueid %s, CSN %s): "
  1268. "Local error. Will retry later.\n",
  1269. agmt_get_long_name(prp->agmt),
  1270. entry.op->target_address.uniqueid, csn_str);
  1271. }
  1272. }
  1273. else
  1274. {
  1275. /* Positive response received */
  1276. (*num_changes_sent)++;
  1277. agmt_inc_last_update_changecount (prp->agmt, csn_get_replicaid(entry.op->csn), 0 /*replayed*/);
  1278. }
  1279. break;
  1280. case CL5_BAD_DATA:
  1281. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1282. "%s: Invalid parameter passed to cl5GetNextOperationToReplay\n",
  1283. agmt_get_long_name(prp->agmt));
  1284. return_value = UPDATE_FATAL_ERROR;
  1285. finished = 1;
  1286. break;
  1287. case CL5_NOTFOUND:
  1288. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1289. "%s: No more updates to send (cl5GetNextOperationToReplay)\n",
  1290. agmt_get_long_name(prp->agmt));
  1291. return_value = UPDATE_NO_MORE_UPDATES;
  1292. finished = 1;
  1293. break;
  1294. case CL5_DB_ERROR:
  1295. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1296. "%s: A database error occurred (cl5GetNextOperationToReplay)\n",
  1297. agmt_get_long_name(prp->agmt));
  1298. return_value = UPDATE_FATAL_ERROR;
  1299. finished = 1;
  1300. break;
  1301. case CL5_BAD_FORMAT:
  1302. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1303. "%s: A malformed changelog entry was encountered (cl5GetNextOperationToReplay)\n",
  1304. agmt_get_long_name(prp->agmt));
  1305. break;
  1306. case CL5_MEMORY_ERROR:
  1307. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1308. "%s: A memory allocation error occurred (cl5GetNextOperationToRepla)\n",
  1309. agmt_get_long_name(prp->agmt));
  1310. return_value = UPDATE_FATAL_ERROR;
  1311. break;
  1312. default:
  1313. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1314. "%s: Unknown error code (%d) returned from cl5GetNextOperationToReplay\n",
  1315. agmt_get_long_name(prp->agmt), rc);
  1316. return_value = UPDATE_TRANSIENT_ERROR;
  1317. break;
  1318. }
  1319. /* Check for protocol shutdown */
  1320. if (prp->terminate)
  1321. {
  1322. return_value = UPDATE_NO_MORE_UPDATES;
  1323. finished = 1;
  1324. }
  1325. if (*num_changes_sent >= MAX_CHANGES_PER_SESSION)
  1326. {
  1327. return_value = UPDATE_YIELD;
  1328. finished = 1;
  1329. }
  1330. } while (!finished);
  1331. cl5_operation_parameters_done ( entry.op );
  1332. cl5DestroyReplayIterator(&changelog_iterator);
  1333. }
  1334. return return_value;
  1335. }
  1336. /*
  1337. * XXXggood this should probably be in the superclass, since the full update
  1338. * protocol is going to need it too.
  1339. */
  1340. static int
  1341. repl5_inc_stop(Private_Repl_Protocol *prp)
  1342. {
  1343. int return_value;
  1344. PRIntervalTime start, maxwait, now;
  1345. int seconds = 1200;
  1346. maxwait = PR_SecondsToInterval(seconds);
  1347. prp->terminate = 1;
  1348. event_notify(prp, EVENT_PROTOCOL_SHUTDOWN);
  1349. start = PR_IntervalNow();
  1350. now = start;
  1351. while (!prp->stopped && ((now - start) < maxwait))
  1352. {
  1353. DS_Sleep(PR_SecondsToInterval(1));
  1354. now = PR_IntervalNow();
  1355. }
  1356. if (!prp->stopped)
  1357. {
  1358. /* Isn't listening. Do something drastic. */
  1359. return_value = -1;
  1360. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1361. "%s: repl5_inc_stop: protocol does not stop after %d seconds\n",
  1362. agmt_get_long_name(prp->agmt), seconds);
  1363. }
  1364. else
  1365. {
  1366. return_value = 0;
  1367. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1368. "%s: repl5_inc_stop: protocol stopped after %d seconds\n",
  1369. agmt_get_long_name(prp->agmt),
  1370. PR_IntervalToSeconds(now-start));
  1371. }
  1372. return return_value;
  1373. }
  1374. static int
  1375. repl5_inc_status(Private_Repl_Protocol *prp)
  1376. {
  1377. int return_value = 0;
  1378. return return_value;
  1379. }
  1380. static void
  1381. repl5_inc_notify_update(Private_Repl_Protocol *prp)
  1382. {
  1383. event_notify(prp, EVENT_TRIGGERING_CRITERIA_MET);
  1384. }
  1385. static void
  1386. repl5_inc_update_now(Private_Repl_Protocol *prp)
  1387. {
  1388. event_notify(prp, EVENT_REPLICATE_NOW);
  1389. }
  1390. static void
  1391. repl5_inc_notify_agmt_changed(Private_Repl_Protocol *prp)
  1392. {
  1393. event_notify(prp, EVENT_AGMT_CHANGED);
  1394. }
  1395. static void
  1396. repl5_inc_notify_window_opened (Private_Repl_Protocol *prp)
  1397. {
  1398. event_notify(prp, EVENT_WINDOW_OPENED);
  1399. }
  1400. static void
  1401. repl5_inc_notify_window_closed (Private_Repl_Protocol *prp)
  1402. {
  1403. event_notify(prp, EVENT_WINDOW_CLOSED);
  1404. }
  1405. Private_Repl_Protocol *
  1406. Repl_5_Inc_Protocol_new(Repl_Protocol *rp)
  1407. {
  1408. repl5_inc_private *rip = NULL;
  1409. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)slapi_ch_malloc(sizeof(Private_Repl_Protocol));
  1410. prp->delete = repl5_inc_delete;
  1411. prp->run = repl5_inc_run;
  1412. prp->stop = repl5_inc_stop;
  1413. prp->status = repl5_inc_status;
  1414. prp->notify_update = repl5_inc_notify_update;
  1415. prp->notify_agmt_changed = repl5_inc_notify_agmt_changed;
  1416. prp->notify_window_opened = repl5_inc_notify_window_opened;
  1417. prp->notify_window_closed = repl5_inc_notify_window_closed;
  1418. prp->update_now = repl5_inc_update_now;
  1419. prp->replica_object = prot_get_replica_object(rp);
  1420. if ((prp->lock = PR_NewLock()) == NULL)
  1421. {
  1422. goto loser;
  1423. }
  1424. if ((prp->cvar = PR_NewCondVar(prp->lock)) == NULL)
  1425. {
  1426. goto loser;
  1427. }
  1428. prp->stopped = 0;
  1429. prp->terminate = 0;
  1430. prp->eventbits = 0;
  1431. prp->conn = prot_get_connection(rp);
  1432. prp->agmt = prot_get_agreement(rp);
  1433. prp->last_acquire_response_code = NSDS50_REPL_REPLICA_READY;
  1434. rip = (void *)slapi_ch_malloc(sizeof(repl5_inc_private));
  1435. rip->ruv = NULL;
  1436. rip->backoff = NULL;
  1437. rip->rp = rp;
  1438. prp->private = (void *)rip;
  1439. prp->replica_acquired = PR_FALSE;
  1440. return prp;
  1441. loser:
  1442. repl5_inc_delete(&prp);
  1443. return NULL;
  1444. }
  1445. static void
  1446. repl5_inc_backoff_expired(time_t timer_fire_time, void *arg)
  1447. {
  1448. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)arg;
  1449. PR_ASSERT(NULL != prp);
  1450. event_notify(prp, EVENT_BACKOFF_EXPIRED);
  1451. }
  1452. /*
  1453. * Examine the update vector and determine our course of action.
  1454. * There are 3 different possibilities, plus a catch-all error:
  1455. * 1 - no update vector (ruv is NULL). The consumer's replica is
  1456. * pristine, so it needs to be initialized. Return
  1457. * EXAMINE_RUV_PRISTINE_REPLICA.
  1458. * 2 - ruv is present, but its database generation ID doesn't
  1459. * match the local generation ID. This means that either
  1460. * the local replica must be reinitialized from the remote
  1461. * replica or vice-versa. Return
  1462. * EXAMINE_RUV_GENERATION_MISMATCH.
  1463. * 3 - ruv is present, and we have all updates needed to bring
  1464. * the replica up to date using the incremental protocol.
  1465. * return EXAMINE_RUV_OK.
  1466. * 4 - parameter error. Return EXAMINE_RUV_PARAM_ERROR
  1467. */
  1468. static int
  1469. examine_update_vector(Private_Repl_Protocol *prp, RUV *remote_ruv)
  1470. {
  1471. int return_value;
  1472. PR_ASSERT(NULL != prp);
  1473. if (NULL == prp)
  1474. {
  1475. return_value = EXAMINE_RUV_PARAM_ERROR;
  1476. }
  1477. else if (NULL == remote_ruv)
  1478. {
  1479. return_value = EXAMINE_RUV_PRISTINE_REPLICA;
  1480. }
  1481. else
  1482. {
  1483. char *local_gen = NULL;
  1484. char *remote_gen = ruv_get_replica_generation(remote_ruv);
  1485. Object *local_ruv_obj;
  1486. RUV *local_ruv;
  1487. Replica *replica;
  1488. PR_ASSERT(NULL != prp->replica_object);
  1489. replica = object_get_data(prp->replica_object);
  1490. PR_ASSERT(NULL != replica);
  1491. local_ruv_obj = replica_get_ruv (replica);
  1492. if (NULL != local_ruv_obj)
  1493. {
  1494. local_ruv = (RUV*)object_get_data (local_ruv_obj);
  1495. PR_ASSERT (local_ruv);
  1496. local_gen = ruv_get_replica_generation(local_ruv);
  1497. object_release (local_ruv_obj);
  1498. }
  1499. if (NULL == remote_gen || NULL == local_gen || strcmp(remote_gen, local_gen) != 0)
  1500. {
  1501. return_value = EXAMINE_RUV_GENERATION_MISMATCH;
  1502. }
  1503. else
  1504. {
  1505. return_value = EXAMINE_RUV_OK;
  1506. }
  1507. slapi_ch_free((void**)&remote_gen);
  1508. slapi_ch_free((void**)&local_gen);
  1509. }
  1510. return return_value;
  1511. }
  1512. /*
  1513. * When we get an error from an LDAP operation, we call this
  1514. * function to decide if we should just keep replaying
  1515. * updates, or if we should stop, back off, and try again
  1516. * later.
  1517. * Returns PR_TRUE if we shoould keep going, PR_FALSE if
  1518. * we should back off and try again later.
  1519. *
  1520. * In general, we keep going if the return code is consistent
  1521. * with some sort of bug in URP that causes the consumer to
  1522. * emit an error code that it shouldn't have, e.g. LDAP_ALREADY_EXISTS.
  1523. *
  1524. * We stop if there's some indication that the server just completely
  1525. * failed to process the operation, e.g. LDAP_OPERATIONS_ERROR.
  1526. */
  1527. static PRBool
  1528. ignore_error_and_keep_going(int error)
  1529. {
  1530. int return_value;
  1531. switch (error)
  1532. {
  1533. /* Cases where we keep going */
  1534. case LDAP_SUCCESS:
  1535. case LDAP_NO_SUCH_ATTRIBUTE:
  1536. case LDAP_UNDEFINED_TYPE:
  1537. case LDAP_CONSTRAINT_VIOLATION:
  1538. case LDAP_TYPE_OR_VALUE_EXISTS:
  1539. case LDAP_INVALID_SYNTAX:
  1540. case LDAP_NO_SUCH_OBJECT:
  1541. case LDAP_INVALID_DN_SYNTAX:
  1542. case LDAP_IS_LEAF:
  1543. case LDAP_INSUFFICIENT_ACCESS:
  1544. case LDAP_NAMING_VIOLATION:
  1545. case LDAP_OBJECT_CLASS_VIOLATION:
  1546. case LDAP_NOT_ALLOWED_ON_NONLEAF:
  1547. case LDAP_NOT_ALLOWED_ON_RDN:
  1548. case LDAP_ALREADY_EXISTS:
  1549. case LDAP_NO_OBJECT_CLASS_MODS:
  1550. return_value = PR_TRUE;
  1551. break;
  1552. /* Cases where we stop and retry */
  1553. case LDAP_OPERATIONS_ERROR:
  1554. case LDAP_PROTOCOL_ERROR:
  1555. case LDAP_TIMELIMIT_EXCEEDED:
  1556. case LDAP_SIZELIMIT_EXCEEDED:
  1557. case LDAP_STRONG_AUTH_NOT_SUPPORTED:
  1558. case LDAP_STRONG_AUTH_REQUIRED:
  1559. case LDAP_PARTIAL_RESULTS:
  1560. case LDAP_REFERRAL:
  1561. case LDAP_ADMINLIMIT_EXCEEDED:
  1562. case LDAP_UNAVAILABLE_CRITICAL_EXTENSION:
  1563. case LDAP_CONFIDENTIALITY_REQUIRED:
  1564. case LDAP_SASL_BIND_IN_PROGRESS:
  1565. case LDAP_INAPPROPRIATE_MATCHING:
  1566. case LDAP_ALIAS_PROBLEM:
  1567. case LDAP_ALIAS_DEREF_PROBLEM:
  1568. case LDAP_INAPPROPRIATE_AUTH:
  1569. case LDAP_INVALID_CREDENTIALS:
  1570. case LDAP_BUSY:
  1571. case LDAP_UNAVAILABLE:
  1572. case LDAP_UNWILLING_TO_PERFORM:
  1573. case LDAP_LOOP_DETECT:
  1574. case LDAP_SORT_CONTROL_MISSING:
  1575. case LDAP_INDEX_RANGE_ERROR:
  1576. case LDAP_RESULTS_TOO_LARGE:
  1577. case LDAP_AFFECTS_MULTIPLE_DSAS:
  1578. case LDAP_OTHER:
  1579. case LDAP_SERVER_DOWN:
  1580. case LDAP_LOCAL_ERROR:
  1581. case LDAP_ENCODING_ERROR:
  1582. case LDAP_DECODING_ERROR:
  1583. case LDAP_TIMEOUT:
  1584. case LDAP_AUTH_UNKNOWN:
  1585. case LDAP_FILTER_ERROR:
  1586. case LDAP_USER_CANCELLED:
  1587. case LDAP_PARAM_ERROR:
  1588. case LDAP_NO_MEMORY:
  1589. case LDAP_CONNECT_ERROR:
  1590. case LDAP_NOT_SUPPORTED:
  1591. case LDAP_CONTROL_NOT_FOUND:
  1592. case LDAP_NO_RESULTS_RETURNED:
  1593. case LDAP_MORE_RESULTS_TO_RETURN:
  1594. case LDAP_CLIENT_LOOP:
  1595. case LDAP_REFERRAL_LIMIT_EXCEEDED:
  1596. return_value = PR_FALSE;
  1597. break;
  1598. }
  1599. return return_value;
  1600. }
  1601. /* this function converts a state to its name - for debug output */
  1602. static const char*
  1603. state2name (int state)
  1604. {
  1605. switch (state)
  1606. {
  1607. case STATE_START: return "start";
  1608. case STATE_WAIT_WINDOW_OPEN: return "wait_for_window_to_open";
  1609. case STATE_WAIT_CHANGES: return "wait_for_changes";
  1610. case STATE_READY_TO_ACQUIRE: return "ready_to_acquire_replica";
  1611. case STATE_BACKOFF_START: return "start_backoff";
  1612. case STATE_BACKOFF: return "backoff";
  1613. case STATE_SENDING_UPDATES: return "sending_updates";
  1614. case STATE_STOP_FATAL_ERROR: return "stop_fatal_error";
  1615. case STATE_STOP_FATAL_ERROR_PART2: return "stop_fatal_error";
  1616. case STATE_STOP_NORMAL_TERMINATION: return "stop_normal_termination";
  1617. default: return "invalid_state";
  1618. }
  1619. }
  1620. /* this function convert s an event to its name - for debug output */
  1621. static const char*
  1622. event2name (int event)
  1623. {
  1624. switch (event)
  1625. {
  1626. case EVENT_WINDOW_OPENED: return "update_window_opened";
  1627. case EVENT_WINDOW_CLOSED: return "update_window_closed";
  1628. case EVENT_TRIGGERING_CRITERIA_MET: return "data_modified";
  1629. case EVENT_BACKOFF_EXPIRED: return "backoff_timer_expired";
  1630. case EVENT_REPLICATE_NOW: return "replicate_now";
  1631. case EVENT_PROTOCOL_SHUTDOWN: return "protocol_shutdown";
  1632. case EVENT_AGMT_CHANGED: return "agreement_changed";
  1633. default: return "invalid_event";
  1634. }
  1635. }
  1636. static const char*
  1637. op2string(int op)
  1638. {
  1639. switch (op) {
  1640. case SLAPI_OPERATION_ADD:
  1641. return "add";
  1642. case SLAPI_OPERATION_MODIFY:
  1643. return "modify";
  1644. case SLAPI_OPERATION_DELETE:
  1645. return "delete";
  1646. case SLAPI_OPERATION_MODRDN:
  1647. return "rename";
  1648. case SLAPI_OPERATION_EXTENDED:
  1649. return "extended";
  1650. }
  1651. return "unknown";
  1652. }