windows_inc_protocol.c 60 KB

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