windows_inc_protocol.c 60 KB

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