windows_inc_protocol.c 60 KB

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