windows_inc_protocol.c 62 KB

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