windows_inc_protocol.c 62 KB

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