windows_inc_protocol.c 62 KB

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