windows_inc_protocol.c 73 KB

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