windows_inc_protocol.c 60 KB

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