windows_inc_protocol.c 60 KB

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