windows_inc_protocol.c 62 KB

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