windows_inc_protocol.c 59 KB

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