windows_inc_protocol.c 61 KB

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