windows_inc_protocol.c 61 KB

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