repl5_inc_protocol.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  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 <plstr.h>
  53. #include "repl.h"
  54. #include "repl5.h"
  55. #include "repl5_ruv.h"
  56. #include "repl5_prot_private.h"
  57. #include "cl5_api.h"
  58. extern int slapi_log_urp;
  59. /*** from proto-slap.h ***/
  60. void ava_done(struct ava *ava);
  61. typedef struct repl5_inc_private
  62. {
  63. char *ruv; /* RUV on remote replica (use diff type for this? - ggood */
  64. Backoff_Timer *backoff;
  65. Repl_Protocol *rp;
  66. PRLock *lock;
  67. PRUint32 eventbits;
  68. } repl5_inc_private;
  69. /* Structures used to communicate with the result reading thread */
  70. #ifndef UIDSTR_SIZE
  71. #define UIDSTR_SIZE 35 /* size of the string representation of the id */
  72. #endif
  73. typedef struct repl5_inc_operation
  74. {
  75. int ldap_message_id;
  76. unsigned long operation_type;
  77. char csn_str[CSN_STRSIZE];
  78. char uniqueid[UIDSTR_SIZE+1];
  79. ReplicaId replica_id;
  80. struct repl5_inc_operation *next;
  81. } repl5_inc_operation;
  82. typedef struct result_data
  83. {
  84. Private_Repl_Protocol *prp;
  85. int rc;
  86. PRLock *lock; /* Lock to protect access to this structure, the message id list and to force memory barriers */
  87. PRThread *result_tid; /* The async result thread */
  88. repl5_inc_operation *operation_list_head; /* List of IDs for outstanding operations */
  89. repl5_inc_operation *operation_list_tail; /* List of IDs for outstanding operations */
  90. int abort; /* Flag used to tell the sending thread asyncronously that it should abort (because an error came up in a result) */
  91. PRUint32 num_changes_sent;
  92. int stop_result_thread; /* Flag used to tell the result thread to exit */
  93. int last_message_id_sent;
  94. int last_message_id_received;
  95. int result; /* The UPDATE_TRANSIENT_ERROR etc */
  96. } result_data;
  97. /* Various states the incremental protocol can pass through */
  98. #define STATE_START 0 /* ONREPL - should we rename this - we don't use it just to start up? */
  99. #define STATE_WAIT_WINDOW_OPEN 1
  100. #define STATE_WAIT_CHANGES 2
  101. #define STATE_READY_TO_ACQUIRE 3
  102. #define STATE_BACKOFF_START 4 /* ONREPL - can we combine BACKOFF_START and BACKOFF states? */
  103. #define STATE_BACKOFF 5
  104. #define STATE_SENDING_UPDATES 6
  105. #define STATE_STOP_FATAL_ERROR 7
  106. #define STATE_STOP_FATAL_ERROR_PART2 8
  107. #define STATE_STOP_NORMAL_TERMINATION 9
  108. /* Events (synchronous and asynchronous; these are bits) */
  109. #define EVENT_WINDOW_OPENED 1
  110. #define EVENT_WINDOW_CLOSED 2
  111. #define EVENT_TRIGGERING_CRITERIA_MET 4 /* ONREPL - should we rename this to EVENT_CHANGE_AVAILABLE */
  112. #define EVENT_BACKOFF_EXPIRED 8
  113. #define EVENT_REPLICATE_NOW 16
  114. #define EVENT_PROTOCOL_SHUTDOWN 32
  115. #define EVENT_AGMT_CHANGED 64
  116. #define UPDATE_NO_MORE_UPDATES 201
  117. #define UPDATE_TRANSIENT_ERROR 202
  118. #define UPDATE_FATAL_ERROR 203
  119. #define UPDATE_SCHEDULE_WINDOW_CLOSED 204
  120. #define UPDATE_CONNECTION_LOST 205
  121. #define UPDATE_TIMEOUT 206
  122. #define UPDATE_YIELD 207
  123. /* Return codes from examine_update_vector */
  124. #define EXAMINE_RUV_PRISTINE_REPLICA 401
  125. #define EXAMINE_RUV_GENERATION_MISMATCH 402
  126. #define EXAMINE_RUV_REPLICA_TOO_OLD 403
  127. #define EXAMINE_RUV_OK 404
  128. #define EXAMINE_RUV_PARAM_ERROR 405
  129. #define MAX_CHANGES_PER_SESSION 10000
  130. /*
  131. * Maximum time to wait between replication sessions. If we
  132. * don't see any updates for a period equal to this interval,
  133. * we go ahead and start a replication session, just to be safe
  134. */
  135. #define MAX_WAIT_BETWEEN_SESSIONS PR_SecondsToInterval(60 * 5) /* 5 minutes */
  136. /*
  137. * tests if the protocol has been shutdown and we need to quit
  138. * event_occurred resets the bits in the bit flag, so whoever tests for shutdown
  139. * resets the flags, so the next one who tests for shutdown won't get it, so we
  140. * also look at the terminate flag
  141. */
  142. #define PROTOCOL_IS_SHUTDOWN(prp) (event_occurred(prp, EVENT_PROTOCOL_SHUTDOWN) || prp->terminate)
  143. /* mods should be LDAPMod **mods */
  144. #define MODS_ARE_EMPTY(mods) ((mods == NULL) || (mods[0] == NULL))
  145. /* Forward declarations */
  146. static PRUint32 event_occurred(Private_Repl_Protocol *prp, PRUint32 event);
  147. static void reset_events (Private_Repl_Protocol *prp);
  148. static void protocol_sleep(Private_Repl_Protocol *prp, PRIntervalTime duration);
  149. static int send_updates(Private_Repl_Protocol *prp, RUV *ruv, PRUint32 *num_changes_sent);
  150. static void repl5_inc_backoff_expired(time_t timer_fire_time, void *arg);
  151. static int examine_update_vector(Private_Repl_Protocol *prp, RUV *ruv);
  152. static PRBool ignore_error_and_keep_going(int error);
  153. static const char* state2name (int state);
  154. static const char* event2name (int event);
  155. static const char* op2string (int op);
  156. static int repl5_inc_update_from_op_result(Private_Repl_Protocol *prp, ConnResult replay_crc, int connection_error, char *csn_str, char *uniqueid, ReplicaId replica_id, int* finished, PRUint32 *num_changes_sent);
  157. /* Push a newly sent operation onto the tail of the list */
  158. static void repl5_int_push_operation(result_data *rd, repl5_inc_operation *it)
  159. {
  160. repl5_inc_operation *tail = NULL;
  161. PR_Lock(rd->lock);
  162. tail = rd->operation_list_tail;
  163. if (tail)
  164. {
  165. tail->next = it;
  166. }
  167. if (NULL == rd->operation_list_head)
  168. {
  169. rd->operation_list_head = it;
  170. }
  171. rd->operation_list_tail = it;
  172. PR_Unlock(rd->lock);
  173. }
  174. /* Pop the next operation in line to respond from the list */
  175. /* The caller is expected to free the operation item */
  176. static repl5_inc_operation *repl5_inc_pop_operation(result_data *rd)
  177. {
  178. repl5_inc_operation *head = NULL;
  179. repl5_inc_operation *ret = NULL;
  180. PR_Lock(rd->lock);
  181. head = rd->operation_list_head;
  182. if (head)
  183. {
  184. ret = head;
  185. rd->operation_list_head = head->next;
  186. if (rd->operation_list_tail == head)
  187. {
  188. rd->operation_list_tail = NULL;
  189. }
  190. }
  191. PR_Unlock(rd->lock);
  192. return ret;
  193. }
  194. static void
  195. repl5_inc_op_free(repl5_inc_operation *op)
  196. {
  197. slapi_ch_free((void**)&op);
  198. }
  199. static repl5_inc_operation *repl5_inc_operation_new()
  200. {
  201. repl5_inc_operation *ret = NULL;
  202. ret = (repl5_inc_operation *) slapi_ch_calloc(1,sizeof(repl5_inc_operation));
  203. return ret;
  204. }
  205. /* Called when in compatibility mode, to get the next result from the wire
  206. * The operation thread will not send a second operation until it has read the
  207. * previous result. */
  208. static int
  209. repl5_inc_get_next_result(result_data *rd)
  210. {
  211. ConnResult conres = 0;
  212. int message_id = 0;
  213. /* Wait on the next result */
  214. conres = conn_read_result(rd->prp->conn, &message_id);
  215. /* Return it to the caller */
  216. return conres;
  217. }
  218. #if NEEDED_FOR_DEBUGGING
  219. static void
  220. repl5_inc_log_operation_failure(int operation_code, int ldap_error, char* ldap_error_string, const char *agreement_name)
  221. {
  222. char *op_string = slapi_op_type_to_string(operation_code);
  223. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  224. "%s: Received error %d: %s for %s operation\n",
  225. agreement_name,
  226. ldap_error, ldap_error_string ? ldap_error_string : "NULL",
  227. op_string ? op_string : "NULL");
  228. }
  229. #endif
  230. /* Thread that collects results from async operations sent to the consumer */
  231. static void repl5_inc_result_threadmain(void *param)
  232. {
  233. result_data *rd = (result_data*) param;
  234. ConnResult conres = 0;
  235. Repl_Connection *conn = rd->prp->conn;
  236. int finished = 0;
  237. int message_id = 0;
  238. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain starting\n");
  239. while (!finished)
  240. {
  241. repl5_inc_operation *op = NULL;
  242. int connection_error = 0;
  243. char *csn_str = NULL;
  244. char *uniqueid = NULL;
  245. ReplicaId replica_id = 0;
  246. int operation_code = 0;
  247. char *ldap_error_string = NULL;
  248. time_t time_now = 0;
  249. time_t start_time = time( NULL );
  250. int backoff_time = 1;
  251. /* Read the next result */
  252. /* We call the get result function with a short timeout (non-blocking)
  253. * this is so we don't block here forever, and can stop this thread when
  254. * the time comes. However, we do need to implement blocking with timeout
  255. * semantics here instead.
  256. */
  257. while (!finished)
  258. {
  259. conres = conn_read_result_ex(conn, NULL, NULL, NULL, LDAP_RES_ANY, &message_id, 0);
  260. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain: read result for message_id %d\n", message_id);
  261. /* Timeout here means that we didn't block, not a real timeout */
  262. if (CONN_TIMEOUT == conres)
  263. {
  264. /* We need to a) check that the 'real' timeout hasn't expired and
  265. * b) implement a backoff sleep to avoid spinning */
  266. /* Did the connection's timeout expire ? */
  267. time_now = time( NULL );
  268. if (conn_get_timeout(conn) <= ( time_now - start_time ))
  269. {
  270. /* We timed out */
  271. conres = CONN_TIMEOUT;
  272. break;
  273. }
  274. /* Otherwise we backoff */
  275. DS_Sleep(PR_MillisecondsToInterval(backoff_time));
  276. if (backoff_time < 1000)
  277. {
  278. backoff_time <<= 1;
  279. }
  280. /* Should we stop ? */
  281. PR_Lock(rd->lock);
  282. if (rd->stop_result_thread)
  283. {
  284. finished = 1;
  285. }
  286. PR_Unlock(rd->lock);
  287. } else
  288. {
  289. /* Something other than a timeout, so we exit the loop */
  290. break;
  291. }
  292. }
  293. if (conres != CONN_TIMEOUT)
  294. {
  295. int return_value;
  296. int should_finish = 0;
  297. if (message_id)
  298. {
  299. rd->last_message_id_received = message_id;
  300. }
  301. /* Handle any error etc */
  302. /* Get the stored operation details from the queue, unless we timed out... */
  303. op = repl5_inc_pop_operation(rd);
  304. if (op)
  305. {
  306. csn_str = op->csn_str;
  307. replica_id = op->replica_id;
  308. uniqueid = op->uniqueid;
  309. }
  310. conn_get_error_ex(conn, &operation_code, &connection_error, &ldap_error_string);
  311. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain: result %d, %d, %d, %d, %s\n", operation_code,connection_error,conres,message_id,ldap_error_string);
  312. return_value = repl5_inc_update_from_op_result(rd->prp, conres, connection_error, csn_str, uniqueid, replica_id, &should_finish, &(rd->num_changes_sent));
  313. if (return_value || should_finish)
  314. {
  315. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain: got op result %d should finish %d\n", return_value, should_finish);
  316. /* If so then we need to take steps to abort the update process */
  317. PR_Lock(rd->lock);
  318. rd->result = return_value;
  319. rd->abort = 1;
  320. PR_Unlock(rd->lock);
  321. /* We also need to log the error, including details stored from when the operation was sent */
  322. /* we cannot finish yet - we still need to waitfor the pending results, then
  323. the main repl code will shut down this thread */
  324. /* we can finish if we have disconnected - in that case, there will be nothing
  325. to read */
  326. if (return_value == UPDATE_CONNECTION_LOST) {
  327. finished = 1;
  328. }
  329. } else {
  330. /* old semantics had result set outside of lock */
  331. rd->result = return_value;
  332. }
  333. }
  334. /* Should we stop ? */
  335. PR_Lock(rd->lock);
  336. if (rd->stop_result_thread)
  337. {
  338. finished = 1;
  339. }
  340. PR_Unlock(rd->lock);
  341. if (op)
  342. {
  343. repl5_inc_op_free(op);
  344. }
  345. }
  346. slapi_log_error(SLAPI_LOG_REPL, NULL, "repl5_inc_result_threadmain exiting\n");
  347. }
  348. static result_data *
  349. repl5_inc_rd_new(Private_Repl_Protocol *prp)
  350. {
  351. result_data *res = NULL;
  352. res = (result_data *) slapi_ch_calloc(1,sizeof(result_data));
  353. if (res) {
  354. res->prp = prp;
  355. res->lock = PR_NewLock();
  356. if (NULL == res->lock) {
  357. slapi_ch_free((void **)&res);
  358. res = NULL;
  359. }
  360. }
  361. return res;
  362. }
  363. static void
  364. repl5_inc_rd_list_destroy(repl5_inc_operation *op)
  365. {
  366. while (op) {
  367. repl5_inc_operation *next = op->next;
  368. repl5_inc_op_free(op);
  369. op = next;
  370. }
  371. }
  372. static void
  373. repl5_inc_rd_destroy(result_data **pres)
  374. {
  375. result_data *res = *pres;
  376. if (res->lock) {
  377. PR_DestroyLock(res->lock);
  378. }
  379. /* Delete the linked list if we have one */
  380. /* Begin at the head */
  381. repl5_inc_rd_list_destroy(res->operation_list_head);
  382. slapi_ch_free((void **)pres);
  383. }
  384. static int
  385. repl5_inc_create_async_result_thread(result_data *rd)
  386. {
  387. int retval = 0;
  388. PRThread *tid = NULL;
  389. /* Create a thread that reads results from the connection and stores status in the callback_data structure */
  390. tid = PR_CreateThread(PR_USER_THREAD,
  391. repl5_inc_result_threadmain, (void*)rd,
  392. PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD,
  393. SLAPD_DEFAULT_THREAD_STACKSIZE);
  394. if (NULL == tid)
  395. {
  396. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  397. "repl5_tot_create_async_result_thread failed. "
  398. SLAPI_COMPONENT_NAME_NSPR " error %d (%s)\n",
  399. PR_GetError(), slapd_pr_strerror( PR_GetError() ));
  400. retval = -1;
  401. } else {
  402. rd->result_tid = tid;
  403. }
  404. return retval;
  405. }
  406. static int
  407. repl5_inc_destroy_async_result_thread(result_data *rd)
  408. {
  409. int retval = 0;
  410. PRThread *tid = rd->result_tid;
  411. if (tid) {
  412. PR_Lock(rd->lock);
  413. rd->stop_result_thread = 1;
  414. PR_Unlock(rd->lock);
  415. (void)PR_JoinThread(tid);
  416. }
  417. return retval;
  418. }
  419. static void
  420. repl5_inc_waitfor_async_results(result_data *rd)
  421. {
  422. int done = 0;
  423. int loops = 0;
  424. /* Keep pulling results off the LDAP connection until we catch up to the last message id stored in the rd */
  425. while (!done)
  426. {
  427. /* Lock the structure to force memory barrier */
  428. PR_Lock(rd->lock);
  429. /* Are we caught up ? */
  430. slapi_log_error(SLAPI_LOG_REPL, NULL,
  431. "repl5_inc_waitfor_async_results: %d %d\n",
  432. rd->last_message_id_received, rd->last_message_id_sent);
  433. if (rd->last_message_id_received >= rd->last_message_id_sent)
  434. {
  435. /* If so then we're done */
  436. done = 1;
  437. }
  438. if (rd->abort && (rd->result == UPDATE_CONNECTION_LOST))
  439. {
  440. done = 1; /* no connection == no more results */
  441. }
  442. PR_Unlock(rd->lock);
  443. /* If not then sleep a bit */
  444. DS_Sleep(PR_SecondsToInterval(1));
  445. loops++;
  446. /* If we sleep forever then we can conclude that something bad happened, and bail... */
  447. /* Arbitrary 30 second delay : basically we should only expect to wait as long as it takes to process a few operations, which should be on the order of a second at most */
  448. if (!done && (loops > 300))
  449. {
  450. /* Log a warning */
  451. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  452. "repl5_inc_waitfor_async_results timed out waiting for responses: %d %d\n",
  453. rd->last_message_id_received, rd->last_message_id_sent);
  454. done = 1;
  455. }
  456. }
  457. }
  458. /*
  459. * It's specifically ok to delete a protocol instance that
  460. * is currently running. The instance will be shut down, and
  461. * then resources will be freed. Since a graceful shutdown is
  462. * attempted, this function may take some time to complete.
  463. */
  464. static void
  465. repl5_inc_delete(Private_Repl_Protocol **prpp)
  466. {
  467. /* First, stop the protocol if it isn't already stopped */
  468. if (!(*prpp)->stopped) {
  469. (*prpp)->stopped = 1;
  470. (*prpp)->stop(*prpp);
  471. }
  472. /* Then, delete all resources used by the protocol */
  473. if ((*prpp)->lock) {
  474. PR_DestroyLock((*prpp)->lock);
  475. (*prpp)->lock = NULL;
  476. }
  477. if ((*prpp)->cvar) {
  478. PR_DestroyCondVar((*prpp)->cvar);
  479. (*prpp)->cvar = NULL;
  480. }
  481. slapi_ch_free((void **)&(*prpp)->private);
  482. slapi_ch_free((void **)prpp);
  483. }
  484. /* helper function */
  485. void
  486. set_pause_and_busy_time(long *pausetime, long *busywaittime)
  487. {
  488. /* If neither are set, set busy time to its default */
  489. if (!*pausetime && !*busywaittime)
  490. {
  491. *busywaittime = PROTOCOL_BUSY_BACKOFF_MINIMUM;
  492. }
  493. /* pause time must be at least 1 more than the busy backoff time */
  494. if (*pausetime && !*busywaittime)
  495. {
  496. /*
  497. * user specified a pause time but no busy wait time - must
  498. * set busy wait time to 1 less than pause time - if pause
  499. * time is 1, we must set it to 2
  500. */
  501. if (*pausetime < 2)
  502. {
  503. *pausetime = 2;
  504. }
  505. *busywaittime = *pausetime - 1;
  506. }
  507. else if (!*pausetime && *busywaittime)
  508. {
  509. /*
  510. * user specified a busy wait time but no pause time - must
  511. * set pause time to 1 more than busy wait time
  512. */
  513. *pausetime = *busywaittime + 1;
  514. }
  515. else if (*pausetime && *busywaittime && *pausetime <= *busywaittime)
  516. {
  517. /*
  518. * user specified both pause and busy wait times, but the pause
  519. * time was <= busy wait time - pause time must be at least
  520. * 1 more than the busy wait time
  521. */
  522. *pausetime = *busywaittime + 1;
  523. }
  524. }
  525. /*
  526. * Do the incremental protocol.
  527. *
  528. * What's going on here? This thing is a state machine. It has the
  529. * following states:
  530. *
  531. * State transition table:
  532. *
  533. * Curr State Condition/Event Next State
  534. * ---------- ------------ -----------
  535. * START schedule window is open ACQUIRE_REPLICA
  536. * schedule window is closed WAIT_WINDOW_OPEN
  537. * WAIT_WINDOW_OPEN schedule change START
  538. * replicate now ACQUIRE_REPLICA
  539. * schedule window opens ACQUIRE_REPLICA
  540. * ACQUIRE_REPLICA acquired replica SEND_CHANGES
  541. * failed to acquire - transient error START_BACKOFF
  542. * failed to acquire - fatal error STOP_FATAL_ERROR
  543. * SEND_CHANGES can't update CONSUMER_NEEDS_REINIT
  544. * no changes to send WAIT_CHANGES
  545. * can't send - thransient error START_BACKOF
  546. * can't send - window closed WAIT_WINDOW_OPEN
  547. * can'r send - fatal error STOP_FATAL_ERROR
  548. * START_BACKOF replicate now ACQUIRE_REPLICA
  549. * schedule changes START
  550. * schedule window closes WAIT_WINDOW_OPEN
  551. * backoff expires & can acquire SEND_CHANGES
  552. * backoff expires & can't acquire-trans BACKOFF
  553. * backoff expires & can't acquire-fatal STOP_FATAL_ERROR
  554. * BACKOF replicate now ACQUIRE_REPLICA
  555. * schedule changes START
  556. * schedule window closes WAIT_WINDOW_OPEN
  557. * backoff expires & can acquire SEND_CHANGES
  558. * backoff expires & can't acquire-trans BACKOFF
  559. * backoff expires & can't acquire-fatal STOP_FATAL_ERROR
  560. * WAIT_CHANGES schedule window closes WAIT_WINDOW_OPEN
  561. * replicate_now ACQUIRE_REPLICA
  562. * change available ACQUIRE_REPLICA
  563. * schedule_change START
  564. */
  565. /*
  566. * Main state machine for the incremental protocol. This routine will,
  567. * under normal circumstances, not return until the protocol is shut
  568. * down.
  569. */
  570. static void
  571. repl5_inc_run(Private_Repl_Protocol *prp)
  572. {
  573. int current_state = STATE_START;
  574. int next_state = STATE_START;
  575. repl5_inc_private *prp_priv = (repl5_inc_private *)prp->private;
  576. int done;
  577. int e1;
  578. RUV *ruv = NULL;
  579. CSN *cons_schema_csn;
  580. Replica *replica = NULL;
  581. int wait_change_timer_set = 0;
  582. time_t last_start_time;
  583. PRUint32 num_changes_sent;
  584. /* use a different backoff timer strategy for ACQUIRE_REPLICA_BUSY errors */
  585. PRBool use_busy_backoff_timer = PR_FALSE;
  586. long pausetime = 0;
  587. long busywaittime = 0;
  588. long loops = 0;
  589. int optype, ldaprc;
  590. time_t next_fire_time;
  591. time_t now;
  592. prp->stopped = 0;
  593. prp->terminate = 0;
  594. /* establish_protocol_callbacks(prp); */
  595. done = 0;
  596. do {
  597. int rc;
  598. /* Take action, based on current state, and compute new state. */
  599. switch (current_state)
  600. {
  601. case STATE_START:
  602. dev_debug("repl5_inc_run(STATE_START)");
  603. if (PROTOCOL_IS_SHUTDOWN(prp)){
  604. done = 1;
  605. break;
  606. }
  607. /*
  608. * Our initial state. See if we're in a schedule window. If
  609. * so, then we're ready to acquire the replica and see if it
  610. * needs any updates from us. If not, then wait for the window
  611. * to open.
  612. */
  613. if (agmt_schedule_in_window_now(prp->agmt)){
  614. next_state = STATE_READY_TO_ACQUIRE;
  615. } else {
  616. next_state = STATE_WAIT_WINDOW_OPEN;
  617. }
  618. /* we can get here from other states because some events happened and were
  619. * not cleared. For instance when we wake up in STATE_WAIT_CHANGES state.
  620. * Since this is a fresh start state, we should clear all events */
  621. /* ONREPL - this does not feel right - we should take another look
  622. * at this state machine */
  623. reset_events (prp);
  624. /* Cancel any linger timer that might be in effect... */
  625. conn_cancel_linger(prp->conn);
  626. /* ... and disconnect, if currently connected */
  627. conn_disconnect(prp->conn);
  628. /* get the new pause time, if any */
  629. pausetime = agmt_get_pausetime(prp->agmt);
  630. /* get the new busy wait time, if any */
  631. busywaittime = agmt_get_busywaittime(prp->agmt);
  632. if (pausetime || busywaittime){
  633. /* helper function to make sure they are set correctly */
  634. set_pause_and_busy_time(&pausetime, &busywaittime);
  635. }
  636. break;
  637. case STATE_WAIT_WINDOW_OPEN:
  638. /*
  639. * We're waiting for a schedule window to open. If one did,
  640. * or we receive a "replicate now" event, then start a protocol
  641. * session immediately. If the replication schedule changed, go
  642. * back to start. Otherwise, go back to sleep.
  643. */
  644. dev_debug("repl5_inc_run(STATE_WAIT_WINDOW_OPEN)");
  645. if (PROTOCOL_IS_SHUTDOWN(prp)){
  646. done = 1;
  647. break;
  648. } else if (event_occurred(prp, EVENT_WINDOW_OPENED)){
  649. next_state = STATE_READY_TO_ACQUIRE;
  650. } else if (event_occurred(prp, EVENT_REPLICATE_NOW)){
  651. next_state = STATE_READY_TO_ACQUIRE;
  652. } else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  653. next_state = STATE_START;
  654. conn_set_agmt_changed(prp->conn);
  655. } else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET)){ /* change available */
  656. /* just ignore it and go to sleep */
  657. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  658. } else if ((e1 = event_occurred(prp, EVENT_WINDOW_CLOSED)) ||
  659. event_occurred(prp, EVENT_BACKOFF_EXPIRED)){
  660. /* this events - should not occur - log a warning and go to sleep */
  661. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  662. "%s: Incremental protocol: "
  663. "event %s should not occur in state %s; going to sleep\n",
  664. agmt_get_long_name(prp->agmt), e1 ? event2name(EVENT_WINDOW_CLOSED) :
  665. event2name(EVENT_BACKOFF_EXPIRED), state2name(current_state));
  666. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  667. } else {
  668. /* wait until window opens or an event occurs */
  669. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  670. "%s: Incremental protocol: "
  671. "waiting for update window to open\n", agmt_get_long_name(prp->agmt));
  672. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  673. }
  674. break;
  675. case STATE_WAIT_CHANGES:
  676. /*
  677. * We're in a replication window, but we're waiting for more
  678. * changes to accumulate before we actually hook up and send
  679. * them.
  680. */
  681. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES)");
  682. if (PROTOCOL_IS_SHUTDOWN(prp)){
  683. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): PROTOCOL_IS_SHUTING_DOWN -> end repl5_inc_run\n");
  684. done = 1;
  685. break;
  686. } else if (event_occurred(prp, EVENT_REPLICATE_NOW)){
  687. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_REPLICATE_NOW received -> STATE_READY_TO_ACQUIRE\n");
  688. next_state = STATE_READY_TO_ACQUIRE;
  689. wait_change_timer_set = 0;
  690. } else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  691. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_AGMT_CHANGED received -> STATE_START\n");
  692. next_state = STATE_START;
  693. conn_set_agmt_changed(prp->conn);
  694. wait_change_timer_set = 0;
  695. } else if (event_occurred(prp, EVENT_WINDOW_CLOSED)){
  696. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_WINDOW_CLOSED received -> STATE_WAIT_WINDOW_OPEN\n");
  697. next_state = STATE_WAIT_WINDOW_OPEN;
  698. wait_change_timer_set = 0;
  699. } else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET)){
  700. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): EVENT_TRIGGERING_CRITERIA_MET received -> STATE_READY_TO_ACQUIRE\n");
  701. next_state = STATE_READY_TO_ACQUIRE;
  702. wait_change_timer_set = 0;
  703. } else if ((e1 = event_occurred(prp, EVENT_WINDOW_OPENED)) || event_occurred(prp, EVENT_BACKOFF_EXPIRED)){
  704. /* this events - should not occur - log a warning and clear the event */
  705. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: Incremental protocol: "
  706. "event %s should not occur in state %s\n",agmt_get_long_name(prp->agmt),
  707. e1 ? event2name(EVENT_WINDOW_OPENED) : event2name(EVENT_BACKOFF_EXPIRED),
  708. state2name(current_state));
  709. wait_change_timer_set = 0;
  710. } else {
  711. if (wait_change_timer_set){
  712. /* We are here because our timer expired */
  713. dev_debug("repl5_inc_run(STATE_WAIT_CHANGES): wait_change_timer_set expired -> STATE_START\n");
  714. next_state = STATE_START;
  715. wait_change_timer_set = 0;
  716. } else {
  717. /*
  718. * We are here because the last replication session
  719. * finished or aborted.
  720. */
  721. wait_change_timer_set = 1;
  722. protocol_sleep(prp, MAX_WAIT_BETWEEN_SESSIONS);
  723. }
  724. }
  725. break;
  726. case STATE_READY_TO_ACQUIRE:
  727. dev_debug("repl5_inc_run(STATE_READY_TO_ACQUIRE)");
  728. if (PROTOCOL_IS_SHUTDOWN(prp)){
  729. done = 1;
  730. break;
  731. }
  732. /* ONREPL - at this state we unconditionally acquire the replica
  733. ignoring all events. Not sure if this is good */
  734. object_acquire(prp->replica_object);
  735. rc = acquire_replica(prp, REPL_NSDS50_INCREMENTAL_PROTOCOL_OID, &ruv);
  736. use_busy_backoff_timer = PR_FALSE; /* default */
  737. if (rc == ACQUIRE_SUCCESS){
  738. next_state = STATE_SENDING_UPDATES;
  739. } else if (rc == ACQUIRE_REPLICA_BUSY){
  740. next_state = STATE_BACKOFF_START;
  741. use_busy_backoff_timer = PR_TRUE;
  742. } else if (rc == ACQUIRE_CONSUMER_WAS_UPTODATE){
  743. next_state = STATE_WAIT_CHANGES;
  744. } else if (rc == ACQUIRE_TRANSIENT_ERROR){
  745. next_state = STATE_BACKOFF_START;
  746. } else if (rc == ACQUIRE_FATAL_ERROR){
  747. next_state = STATE_STOP_FATAL_ERROR;
  748. }
  749. if (rc != ACQUIRE_SUCCESS){
  750. int optype, ldaprc;
  751. conn_get_error(prp->conn, &optype, &ldaprc);
  752. agmt_set_last_update_status(prp->agmt, ldaprc,
  753. prp->last_acquire_response_code, NULL);
  754. }
  755. object_release(prp->replica_object);
  756. break;
  757. case STATE_BACKOFF_START:
  758. dev_debug("repl5_inc_run(STATE_BACKOFF_START)");
  759. if (PROTOCOL_IS_SHUTDOWN(prp)){
  760. done = 1;
  761. break;
  762. }
  763. if (event_occurred(prp, EVENT_REPLICATE_NOW)){
  764. next_state = STATE_READY_TO_ACQUIRE;
  765. } else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  766. next_state = STATE_START;
  767. conn_set_agmt_changed(prp->conn);
  768. } else if (event_occurred (prp, EVENT_WINDOW_CLOSED)){
  769. next_state = STATE_WAIT_WINDOW_OPEN;
  770. } else if (event_occurred (prp, EVENT_TRIGGERING_CRITERIA_MET)){
  771. /* consume and ignore */
  772. } else if ((e1 = event_occurred (prp, EVENT_WINDOW_OPENED)) ||
  773. event_occurred (prp, EVENT_BACKOFF_EXPIRED)){
  774. /* This should never happen */
  775. /* this events - should not occur - log a warning and go to sleep */
  776. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  777. "%s: Incremental protocol: event %s should not occur in state %s\n",
  778. agmt_get_long_name(prp->agmt),
  779. e1 ? event2name(EVENT_WINDOW_OPENED) : event2name(EVENT_BACKOFF_EXPIRED),
  780. state2name(current_state));
  781. } else {
  782. /* Set up the backoff timer to wake us up at the appropriate time */
  783. if (use_busy_backoff_timer){
  784. /* we received a busy signal from the consumer, wait for a while */
  785. if (!busywaittime){
  786. busywaittime = PROTOCOL_BUSY_BACKOFF_MINIMUM;
  787. }
  788. prp_priv->backoff = backoff_new(BACKOFF_FIXED, busywaittime, busywaittime);
  789. } else {
  790. prp_priv->backoff = backoff_new(BACKOFF_EXPONENTIAL, PROTOCOL_BACKOFF_MINIMUM,
  791. PROTOCOL_BACKOFF_MAXIMUM);
  792. }
  793. next_state = STATE_BACKOFF;
  794. backoff_reset(prp_priv->backoff, repl5_inc_backoff_expired, (void *)prp);
  795. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  796. use_busy_backoff_timer = PR_FALSE;
  797. }
  798. break;
  799. case STATE_BACKOFF:
  800. /*
  801. * We're in a backoff state.
  802. */
  803. dev_debug("repl5_inc_run(STATE_BACKOFF)");
  804. if (PROTOCOL_IS_SHUTDOWN(prp)){
  805. if (prp_priv->backoff)
  806. backoff_delete(&prp_priv->backoff);
  807. done = 1;
  808. break;
  809. } else if (event_occurred(prp, EVENT_REPLICATE_NOW)){
  810. next_state = STATE_READY_TO_ACQUIRE;
  811. } else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  812. next_state = STATE_START;
  813. conn_set_agmt_changed(prp->conn);
  814. /* Destroy the backoff timer, since we won't need it anymore */
  815. if (prp_priv->backoff)
  816. backoff_delete(&prp_priv->backoff);
  817. } else if (event_occurred(prp, EVENT_WINDOW_CLOSED)){
  818. next_state = STATE_WAIT_WINDOW_OPEN;
  819. /* Destroy the backoff timer, since we won't need it anymore */
  820. if (prp_priv->backoff)
  821. backoff_delete(&prp_priv->backoff);
  822. } else if (event_occurred(prp, EVENT_BACKOFF_EXPIRED)){
  823. rc = acquire_replica(prp, REPL_NSDS50_INCREMENTAL_PROTOCOL_OID, &ruv);
  824. use_busy_backoff_timer = PR_FALSE;
  825. if (rc == ACQUIRE_SUCCESS){
  826. next_state = STATE_SENDING_UPDATES;
  827. } else if (rc == ACQUIRE_REPLICA_BUSY){
  828. next_state = STATE_BACKOFF;
  829. use_busy_backoff_timer = PR_TRUE;
  830. } else if (rc == ACQUIRE_CONSUMER_WAS_UPTODATE){
  831. next_state = STATE_WAIT_CHANGES;
  832. } else if (rc == ACQUIRE_TRANSIENT_ERROR){
  833. next_state = STATE_BACKOFF;
  834. } else if (rc == ACQUIRE_FATAL_ERROR){
  835. next_state = STATE_STOP_FATAL_ERROR;
  836. }
  837. if (rc != ACQUIRE_SUCCESS){
  838. conn_get_error(prp->conn, &optype, &ldaprc);
  839. agmt_set_last_update_status(prp->agmt, ldaprc, prp->last_acquire_response_code, NULL);
  840. }
  841. /*
  842. * We either need to step the backoff timer, or
  843. * destroy it if we don't need it anymore
  844. */
  845. if (STATE_BACKOFF == next_state){
  846. /* Step the backoff timer */
  847. time(&now);
  848. next_fire_time = backoff_step(prp_priv->backoff);
  849. /* And go back to sleep */
  850. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  851. "%s: Replication session backing off for %ld seconds\n",
  852. agmt_get_long_name(prp->agmt),next_fire_time - now);
  853. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  854. } else {
  855. /* Destroy the backoff timer, since we won't need it anymore */
  856. backoff_delete(&prp_priv->backoff);
  857. }
  858. use_busy_backoff_timer = PR_FALSE;
  859. } else if (event_occurred(prp, EVENT_TRIGGERING_CRITERIA_MET)){
  860. /* changes are available */
  861. if ( prp_priv->backoff == NULL || backoff_expired (prp_priv->backoff, 60)){
  862. /*
  863. * Have seen cases that the agmt stuck here forever since
  864. * somehow the backoff timer was not in event queue anymore.
  865. * If the backoff timer has expired more than 60 seconds, destroy it.
  866. */
  867. if ( prp_priv->backoff )
  868. backoff_delete(&prp_priv->backoff);
  869. next_state = STATE_READY_TO_ACQUIRE;
  870. } else {
  871. /* ignore changes and go to sleep */
  872. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  873. }
  874. } else if (event_occurred(prp, EVENT_WINDOW_OPENED)){
  875. /* this should never happen - log an error and go to sleep */
  876. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: Incremental protocol: "
  877. "event %s should not occur in state %s; going to sleep\n",
  878. agmt_get_long_name(prp->agmt), event2name(EVENT_WINDOW_OPENED),
  879. state2name(current_state));
  880. protocol_sleep(prp, PR_INTERVAL_NO_TIMEOUT);
  881. }
  882. break;
  883. case STATE_SENDING_UPDATES:
  884. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES)");
  885. agmt_set_update_in_progress(prp->agmt, PR_TRUE);
  886. num_changes_sent = 0;
  887. last_start_time = current_time();
  888. agmt_set_last_update_start(prp->agmt, last_start_time);
  889. /*
  890. * We've acquired the replica, and are ready to send any needed updates.
  891. */
  892. if (PROTOCOL_IS_SHUTDOWN(prp)){
  893. release_replica (prp);
  894. done = 1;
  895. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  896. agmt_set_last_update_end(prp->agmt, current_time());
  897. /* MAB: I don't find the following status correct. How do we know it has
  898. * been stopped by an admin and not by a total update request, for instance?
  899. * In any case, how is this protocol shutdown situation different from all the
  900. * other ones that are present in this state machine? */
  901. /* richm: We at least need to let monitors know that the protocol has been
  902. * shutdown - maybe they can figure out why */
  903. agmt_set_last_update_status(prp->agmt, 0, 0, "Protocol stopped");
  904. break;
  905. }
  906. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update started");
  907. /* ONREPL - in this state we send changes no matter what other events occur.
  908. * This is because we can get because of the REPLICATE_NOW event which
  909. * has high priority. Is this ok? */
  910. /* First, push new schema to the consumer if needed */
  911. /* ONREPL - should we push schema after we examine the RUV? */
  912. /*
  913. * GGOOREPL - I don't see why we should wait until we've
  914. * examined the RUV. The schema entry has its own CSN that is
  915. * used to decide if the remote schema needs to be updated.
  916. */
  917. cons_schema_csn = agmt_get_consumer_schema_csn ( prp->agmt );
  918. rc = conn_push_schema(prp->conn, &cons_schema_csn);
  919. if ( cons_schema_csn != agmt_get_consumer_schema_csn ( prp->agmt )){
  920. agmt_set_consumer_schema_csn ( prp->agmt, cons_schema_csn );
  921. }
  922. if (CONN_SCHEMA_UPDATED != rc && CONN_SCHEMA_NO_UPDATE_NEEDED != rc){
  923. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  924. "%s: Warning: unable to replicate schema: rc=%d\n", agmt_get_long_name(prp->agmt), rc);
  925. /* But keep going */
  926. }
  927. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> examine_update_vector");
  928. rc = examine_update_vector(prp, ruv);
  929. /*
  930. * Decide what to do next - proceed with incremental, backoff, or total update
  931. */
  932. switch (rc){
  933. case EXAMINE_RUV_PARAM_ERROR:
  934. /* this is really bad - we have NULL prp! */
  935. next_state = STATE_STOP_FATAL_ERROR;
  936. break;
  937. case EXAMINE_RUV_PRISTINE_REPLICA:
  938. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  939. "%s: Replica has no update vector. It has never been initialized.\n",
  940. agmt_get_long_name(prp->agmt));
  941. next_state = STATE_BACKOFF_START;
  942. break;
  943. case EXAMINE_RUV_GENERATION_MISMATCH:
  944. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  945. "%s: Replica has a different generation ID than the local data.\n",
  946. agmt_get_long_name(prp->agmt));
  947. next_state = STATE_BACKOFF_START;
  948. break;
  949. case EXAMINE_RUV_REPLICA_TOO_OLD:
  950. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  951. "%s: Replica update vector is too out of date to bring "
  952. "into sync using the incremental protocol. The replica "
  953. "must be reinitialized.\n", agmt_get_long_name(prp->agmt));
  954. next_state = STATE_BACKOFF_START;
  955. break;
  956. case EXAMINE_RUV_OK:
  957. /* update our csn generator state with the consumer's ruv data */
  958. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> examine_update_vector OK");
  959. object_acquire(prp->replica_object);
  960. replica = object_get_data(prp->replica_object);
  961. rc = replica_update_csngen_state (replica, ruv);
  962. object_release (prp->replica_object);
  963. replica = NULL;
  964. if (rc == CSN_LIMIT_EXCEEDED) /* too much skew */ {
  965. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  966. "%s: Incremental protocol: fatal error - too much time skew between replicas!\n",
  967. agmt_get_long_name(prp->agmt));
  968. next_state = STATE_STOP_FATAL_ERROR;
  969. } else if (rc != 0) /* internal error */ {
  970. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  971. "%s: Incremental protocol: fatal internal error updating the CSN generator!\n",
  972. agmt_get_long_name(prp->agmt));
  973. next_state = STATE_STOP_FATAL_ERROR;
  974. } else {
  975. rc = send_updates(prp, ruv, &num_changes_sent);
  976. if (rc == UPDATE_NO_MORE_UPDATES){
  977. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_NO_MORE_UPDATES -> STATE_WAIT_CHANGES");
  978. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update succeeded");
  979. next_state = STATE_WAIT_CHANGES;
  980. } else if (rc == UPDATE_YIELD){
  981. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_YIELD -> STATE_BACKOFF_START");
  982. agmt_set_last_update_status(prp->agmt, 0, 0, "Incremental update succeeded and yielded");
  983. next_state = STATE_BACKOFF_START;
  984. } else if (rc == UPDATE_TRANSIENT_ERROR){
  985. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_TRANSIENT_ERROR -> STATE_BACKOFF_START");
  986. next_state = STATE_BACKOFF_START;
  987. } else if (rc == UPDATE_FATAL_ERROR){
  988. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_FATAL_ERROR -> STATE_STOP_FATAL_ERROR");
  989. next_state = STATE_STOP_FATAL_ERROR;
  990. } else if (rc == UPDATE_SCHEDULE_WINDOW_CLOSED){
  991. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_SCHEDULE_WINDOW_CLOSED -> STATE_WAIT_WINDOW_OPEN");
  992. /*
  993. * ONREPL - I don't think we should check this. We might be
  994. * here because of replicate_now event - so we don't care
  995. * about the schedule
  996. */
  997. next_state = STATE_WAIT_WINDOW_OPEN;
  998. /* ONREPL - do we need to release the replica here ? */
  999. conn_disconnect (prp->conn);
  1000. } else if (rc == UPDATE_CONNECTION_LOST){
  1001. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_CONNECTION_LOST -> STATE_BACKOFF_START");
  1002. next_state = STATE_BACKOFF_START;
  1003. } else if (rc == UPDATE_TIMEOUT){
  1004. dev_debug("repl5_inc_run(STATE_SENDING_UPDATES) -> send_updates = UPDATE_TIMEOUT -> STATE_BACKOFF_START");
  1005. next_state = STATE_BACKOFF_START;
  1006. }
  1007. }
  1008. last_start_time = 0UL;
  1009. break;
  1010. }
  1011. if (NULL != ruv){
  1012. ruv_destroy(&ruv); ruv = NULL;
  1013. }
  1014. agmt_set_last_update_end(prp->agmt, current_time());
  1015. agmt_set_update_in_progress(prp->agmt, PR_FALSE);
  1016. /* If timed out, close the connection after released the replica */
  1017. release_replica(prp);
  1018. if (rc == UPDATE_TIMEOUT) {
  1019. conn_disconnect(prp->conn);
  1020. }
  1021. if (rc == UPDATE_NO_MORE_UPDATES && num_changes_sent > 0){
  1022. if (pausetime > 0){
  1023. /* richm - 20020219 - If we have acquired the consumer, and another master has gone
  1024. * into backoff waiting for us to release it, we may acquire the replica sooner
  1025. * than the other master has a chance to, and the other master may not be able
  1026. * to acquire the consumer for a long time (hours, days?) if this server is
  1027. * under a heavy load (see reliab06 et. al. system tests)
  1028. * So, this sleep gives the other master(s) a chance to acquire the consumer replica */
  1029. loops = pausetime;
  1030. /* the while loop is so that we don't just sleep and sleep if an
  1031. * event comes in that we should handle immediately (like shutdown) */
  1032. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1033. "%s: Pausing updates for %ld seconds to allow other suppliers to update consumer\n",
  1034. agmt_get_long_name(prp->agmt), pausetime);
  1035. while (loops-- && !(PROTOCOL_IS_SHUTDOWN(prp))){
  1036. DS_Sleep(PR_SecondsToInterval(1));
  1037. }
  1038. } else if (num_changes_sent > 10){
  1039. /* wait for consumer to write its ruv if the replication was busy */
  1040. /* When asked, consumer sends its ruv in cache to the supplier. */
  1041. /* DS_Sleep ( PR_SecondsToInterval(1) ); */
  1042. }
  1043. }
  1044. break;
  1045. case STATE_STOP_FATAL_ERROR:
  1046. /*
  1047. * We encountered some sort of a fatal error. Suspend.
  1048. */
  1049. /* XXXggood update state in replica */
  1050. agmt_set_last_update_status(prp->agmt, -1, 0, "Incremental update has failed and requires administrator action");
  1051. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR)");
  1052. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1053. "%s: Incremental update failed and requires administrator action\n",
  1054. agmt_get_long_name(prp->agmt));
  1055. next_state = STATE_STOP_FATAL_ERROR_PART2;
  1056. break;
  1057. case STATE_STOP_FATAL_ERROR_PART2:
  1058. if (PROTOCOL_IS_SHUTDOWN(prp)){
  1059. done = 1;
  1060. break;
  1061. }
  1062. /* MAB: This state is the FATAL state where we are supposed to get
  1063. * as a result of a FATAL error on send_updates. But, as bug
  1064. * states, send_updates was always returning TRANSIENT errors and never
  1065. * FATAL... In other words, this code has never been tested before...
  1066. *
  1067. * As of 01/16/01, this piece of code was in a very dangerous state. In particular,
  1068. * 1) it does not catch any events
  1069. * 2) it is a terminal state (once reached it never transitions to a different state)
  1070. *
  1071. * Both things combined make this state to become a consuming infinite loop
  1072. * that is useless after all (we are in a fatal place requiring manual admin jobs */
  1073. /* MAB: The following lines fix problem number 1 above... When the code gets
  1074. * into this state, it should only get a chance to get out of it by an
  1075. * EVENT_AGMT_CHANGED event... All other events should be ignored */
  1076. else if (event_occurred(prp, EVENT_AGMT_CHANGED)){
  1077. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR): EVENT_AGMT_CHANGED received\n");
  1078. /* Chance to recover for the EVENT_AGMT_CHANGED event.
  1079. * This is not mandatory, but fixes problem 2 above */
  1080. next_state = STATE_STOP_NORMAL_TERMINATION;
  1081. } else {
  1082. dev_debug("repl5_inc_run(STATE_STOP_FATAL_ERROR): Event received. Clearing it\n");
  1083. reset_events (prp);
  1084. }
  1085. protocol_sleep (prp, PR_INTERVAL_NO_TIMEOUT);
  1086. break;
  1087. case STATE_STOP_NORMAL_TERMINATION:
  1088. /*
  1089. * We encountered some sort of a fatal error. Return.
  1090. */
  1091. /* XXXggood update state in replica */
  1092. dev_debug("repl5_inc_run(STATE_STOP_NORMAL_TERMINATION)");
  1093. done = 1;
  1094. break;
  1095. }
  1096. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,"%s: State: %s -> %s\n",
  1097. agmt_get_long_name(prp->agmt),state2name(current_state), state2name(next_state));
  1098. current_state = next_state;
  1099. } while (!done);
  1100. /* remove_protocol_callbacks(prp); */
  1101. prp->stopped = 1;
  1102. /* Cancel any linger timer that might be in effect... */
  1103. conn_cancel_linger(prp->conn);
  1104. /* ... and disconnect, if currently connected */
  1105. conn_disconnect(prp->conn);
  1106. }
  1107. /*
  1108. * Go to sleep until awakened.
  1109. */
  1110. static void
  1111. protocol_sleep(Private_Repl_Protocol *prp, PRIntervalTime duration)
  1112. {
  1113. PR_ASSERT(NULL != prp);
  1114. PR_Lock(prp->lock);
  1115. /* we should not go to sleep if there are events available to be processed.
  1116. Otherwise, we can miss the event that suppose to wake us up */
  1117. if (prp->eventbits == 0)
  1118. PR_WaitCondVar(prp->cvar, duration);
  1119. else
  1120. {
  1121. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1122. "%s: Incremental protocol: can't go to sleep: event bits - %x\n",
  1123. agmt_get_long_name(prp->agmt), prp->eventbits);
  1124. }
  1125. PR_Unlock(prp->lock);
  1126. }
  1127. /*
  1128. * Notify the protocol about some event. Signal the condition
  1129. * variable in case the protocol is sleeping. Multiple occurences
  1130. * of a single event type are not remembered (e.g. no stack
  1131. * of events is maintained).
  1132. */
  1133. static void
  1134. event_notify(Private_Repl_Protocol *prp, PRUint32 event)
  1135. {
  1136. PR_ASSERT(NULL != prp);
  1137. PR_Lock(prp->lock);
  1138. prp->eventbits |= event;
  1139. PR_NotifyCondVar(prp->cvar);
  1140. PR_Unlock(prp->lock);
  1141. }
  1142. /*
  1143. * Test to see if an event occurred. The event is cleared when
  1144. * read.
  1145. */
  1146. static PRUint32
  1147. event_occurred(Private_Repl_Protocol *prp, PRUint32 event)
  1148. {
  1149. PRUint32 return_value;
  1150. PR_ASSERT(NULL != prp);
  1151. PR_Lock(prp->lock);
  1152. return_value = (prp->eventbits & event);
  1153. prp->eventbits &= ~event; /* Clear event */
  1154. PR_Unlock(prp->lock);
  1155. return return_value;
  1156. }
  1157. static void
  1158. reset_events (Private_Repl_Protocol *prp)
  1159. {
  1160. PR_ASSERT(NULL != prp);
  1161. PR_Lock(prp->lock);
  1162. prp->eventbits = 0;
  1163. PR_Unlock(prp->lock);
  1164. }
  1165. /*
  1166. * Replay the actual update to the consumer. Construct an appropriate LDAP
  1167. * operation, attach the baggage LDAPv3 control that contains the CSN, etc.,
  1168. * and send the operation to the consumer.
  1169. */
  1170. ConnResult
  1171. replay_update(Private_Repl_Protocol *prp, slapi_operation_parameters *op, int *message_id)
  1172. {
  1173. ConnResult return_value = CONN_OPERATION_FAILED;
  1174. LDAPControl *update_control;
  1175. char *parentuniqueid;
  1176. LDAPMod **modrdn_mods = NULL;
  1177. char csn_str[CSN_STRSIZE]; /* For logging only */
  1178. if (message_id) {
  1179. /* if we get out of this function without setting message_id, it means
  1180. we didn't send an op, so no result needs to be processed */
  1181. *message_id = 0;
  1182. }
  1183. /* Construct the replication info control that accompanies the operation */
  1184. if (SLAPI_OPERATION_ADD == op->operation_type)
  1185. {
  1186. parentuniqueid = op->p.p_add.parentuniqueid;
  1187. }
  1188. else if (SLAPI_OPERATION_MODRDN == op->operation_type)
  1189. {
  1190. /*
  1191. * For modrdn operations, we need to send along modified attributes, e.g.
  1192. * modifytimestamp.
  1193. * And the superior_uniqueid !
  1194. */
  1195. modrdn_mods = op->p.p_modrdn.modrdn_mods;
  1196. parentuniqueid = op->p.p_modrdn.modrdn_newsuperior_address.uniqueid;
  1197. }
  1198. else
  1199. {
  1200. parentuniqueid = NULL;
  1201. }
  1202. if (create_NSDS50ReplUpdateInfoControl(op->target_address.uniqueid,
  1203. parentuniqueid, op->csn, modrdn_mods, &update_control) != LDAP_SUCCESS)
  1204. {
  1205. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1206. "%s: replay_update: Unable to create NSDS50ReplUpdateInfoControl "
  1207. "for operation with csn %s. Skipping update.\n",
  1208. agmt_get_long_name(prp->agmt), csn_as_string(op->csn, PR_FALSE, csn_str));
  1209. }
  1210. else
  1211. {
  1212. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1213. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1214. "%s: replay_update: Sending %s operation (dn=\"%s\" csn=%s)\n",
  1215. agmt_get_long_name(prp->agmt),
  1216. op2string(op->operation_type), REPL_GET_DN(&op->target_address),
  1217. csn_as_string(op->csn, PR_FALSE, csn_str));
  1218. }
  1219. /* What type of operation is it? */
  1220. switch (op->operation_type)
  1221. {
  1222. case SLAPI_OPERATION_ADD:
  1223. {
  1224. LDAPMod **entryattrs;
  1225. /* Convert entry to mods */
  1226. (void)slapi_entry2mods (op->p.p_add.target_entry,
  1227. NULL /* &entrydn : We don't need it */,
  1228. &entryattrs);
  1229. if (NULL == entryattrs)
  1230. {
  1231. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1232. "%s: replay_update: Cannot convert entry to LDAPMods.\n",
  1233. agmt_get_long_name(prp->agmt));
  1234. return_value = CONN_LOCAL_ERROR;
  1235. }
  1236. else
  1237. {
  1238. /* If fractional agreement, trim down the entry */
  1239. if (agmt_is_fractional(prp->agmt))
  1240. {
  1241. repl5_strip_fractional_mods(prp->agmt,entryattrs);
  1242. }
  1243. if (MODS_ARE_EMPTY(entryattrs)) {
  1244. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1245. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1246. "%s: replay_update: %s operation (dn=\"%s\" csn=%s) "
  1247. "not sent - empty\n",
  1248. agmt_get_long_name(prp->agmt),
  1249. op2string(op->operation_type),
  1250. REPL_GET_DN(&op->target_address),
  1251. csn_as_string(op->csn, PR_FALSE, csn_str));
  1252. }
  1253. return_value = CONN_OPERATION_SUCCESS;
  1254. } else {
  1255. return_value = conn_send_add(prp->conn, REPL_GET_DN(&op->target_address),
  1256. entryattrs, update_control, message_id);
  1257. }
  1258. ldap_mods_free(entryattrs, 1);
  1259. }
  1260. break;
  1261. }
  1262. case SLAPI_OPERATION_MODIFY:
  1263. /* If fractional agreement, trim down the mods */
  1264. if (agmt_is_fractional(prp->agmt))
  1265. {
  1266. repl5_strip_fractional_mods(prp->agmt,op->p.p_modify.modify_mods);
  1267. }
  1268. if (MODS_ARE_EMPTY(op->p.p_modify.modify_mods)) {
  1269. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1270. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1271. "%s: replay_update: %ss operation (dn=\"%s\" csn=%s) "
  1272. "not sent - empty\n",
  1273. agmt_get_long_name(prp->agmt),
  1274. op2string(op->operation_type),
  1275. REPL_GET_DN(&op->target_address),
  1276. csn_as_string(op->csn, PR_FALSE, csn_str));
  1277. }
  1278. return_value = CONN_OPERATION_SUCCESS;
  1279. } else {
  1280. return_value = conn_send_modify(prp->conn, REPL_GET_DN(&op->target_address),
  1281. op->p.p_modify.modify_mods, update_control, message_id);
  1282. }
  1283. break;
  1284. case SLAPI_OPERATION_DELETE:
  1285. return_value = conn_send_delete(prp->conn, REPL_GET_DN(&op->target_address),
  1286. update_control, message_id);
  1287. break;
  1288. case SLAPI_OPERATION_MODRDN:
  1289. /* XXXggood need to pass modrdn mods in update control! */
  1290. return_value = conn_send_rename(prp->conn, REPL_GET_DN(&op->target_address),
  1291. op->p.p_modrdn.modrdn_newrdn,
  1292. REPL_GET_DN(&op->p.p_modrdn.modrdn_newsuperior_address),
  1293. op->p.p_modrdn.modrdn_deloldrdn,
  1294. update_control, message_id);
  1295. break;
  1296. default:
  1297. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name, "%s: replay_update: Unknown "
  1298. "operation type %lu found in changelog - skipping change.\n",
  1299. agmt_get_long_name(prp->agmt), op->operation_type);
  1300. }
  1301. destroy_NSDS50ReplUpdateInfoControl(&update_control);
  1302. }
  1303. if (CONN_OPERATION_SUCCESS == return_value)
  1304. {
  1305. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1306. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1307. "%s: replay_update: Consumer successfully sent operation with csn %s\n",
  1308. agmt_get_long_name(prp->agmt), csn_as_string(op->csn, PR_FALSE, csn_str));
  1309. }
  1310. }
  1311. else
  1312. {
  1313. if (slapi_is_loglevel_set(SLAPI_LOG_REPL)) {
  1314. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1315. "%s: replay_update: Consumer could not replay operation with csn %s\n",
  1316. agmt_get_long_name(prp->agmt), csn_as_string(op->csn, PR_FALSE, csn_str));
  1317. }
  1318. }
  1319. return return_value;
  1320. }
  1321. static PRBool
  1322. is_dummy_operation (const slapi_operation_parameters *op)
  1323. {
  1324. return (strcmp (op->target_address.uniqueid, START_ITERATION_ENTRY_UNIQUEID) == 0);
  1325. }
  1326. void
  1327. cl5_operation_parameters_done (struct slapi_operation_parameters *sop)
  1328. {
  1329. if(sop!=NULL) {
  1330. switch(sop->operation_type)
  1331. {
  1332. case SLAPI_OPERATION_BIND:
  1333. slapi_ch_free((void **)&(sop->p.p_bind.bind_saslmechanism));
  1334. if (sop->p.p_bind.bind_creds)
  1335. ber_bvecfree((struct berval**)&(sop->p.p_bind.bind_creds));
  1336. if (sop->p.p_bind.bind_ret_saslcreds)
  1337. ber_bvecfree((struct berval**)&(sop->p.p_bind.bind_ret_saslcreds));
  1338. sop->p.p_bind.bind_creds = NULL;
  1339. sop->p.p_bind.bind_ret_saslcreds = NULL;
  1340. break;
  1341. case SLAPI_OPERATION_COMPARE:
  1342. ava_done((struct ava *)&(sop->p.p_compare.compare_ava));
  1343. break;
  1344. case SLAPI_OPERATION_SEARCH:
  1345. slapi_ch_free((void **)&(sop->p.p_search.search_strfilter));
  1346. charray_free(sop->p.p_search.search_attrs);
  1347. slapi_filter_free(sop->p.p_search.search_filter,1);
  1348. break;
  1349. case SLAPI_OPERATION_MODRDN:
  1350. sop->p.p_modrdn.modrdn_deloldrdn = 0;
  1351. break;
  1352. case SLAPI_OPERATION_EXTENDED:
  1353. slapi_ch_free((void **)&(sop->p.p_extended.exop_oid));
  1354. if (sop->p.p_extended.exop_value)
  1355. ber_bvecfree((struct berval**)&(sop->p.p_extended.exop_value));
  1356. sop->p.p_extended.exop_value = NULL;
  1357. break;
  1358. default:
  1359. break;
  1360. }
  1361. }
  1362. operation_parameters_done(sop);
  1363. }
  1364. /* Helper to update the agreement state based on a the result of a replay operation */
  1365. static int
  1366. repl5_inc_update_from_op_result(Private_Repl_Protocol *prp, ConnResult replay_crc, int connection_error, char *csn_str, char *uniqueid, ReplicaId replica_id, int* finished, PRUint32 *num_changes_sent)
  1367. {
  1368. int return_value = 0;
  1369. /* Indentation is wrong here so we can get a sensible cvs diff */
  1370. if (CONN_OPERATION_SUCCESS != replay_crc)
  1371. {
  1372. /* Figure out what to do next */
  1373. if (CONN_OPERATION_FAILED == replay_crc)
  1374. {
  1375. /* Map ldap error code to return value */
  1376. if (!ignore_error_and_keep_going(connection_error))
  1377. {
  1378. return_value = UPDATE_TRANSIENT_ERROR;
  1379. *finished = 1;
  1380. }
  1381. else
  1382. {
  1383. agmt_inc_last_update_changecount (prp->agmt, replica_id, 1 /*skipped*/);
  1384. }
  1385. slapi_log_error(*finished ? SLAPI_LOG_FATAL : slapi_log_urp, repl_plugin_name,
  1386. "%s: Consumer failed to replay change (uniqueid %s, CSN %s): %s. %s.\n",
  1387. agmt_get_long_name(prp->agmt),
  1388. uniqueid, csn_str,
  1389. ldap_err2string(connection_error),
  1390. *finished ? "Will retry later" : "Skipping");
  1391. }
  1392. else if (CONN_NOT_CONNECTED == replay_crc)
  1393. {
  1394. /* We lost the connection - enter backoff state */
  1395. return_value = UPDATE_CONNECTION_LOST;
  1396. *finished = 1;
  1397. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1398. "%s: Consumer failed to replay change (uniqueid %s, CSN %s): "
  1399. "%s. Will retry later.\n",
  1400. agmt_get_long_name(prp->agmt),
  1401. uniqueid, csn_str,
  1402. connection_error ? ldap_err2string(connection_error) : "Connection lost");
  1403. }
  1404. else if (CONN_TIMEOUT == replay_crc)
  1405. {
  1406. return_value = UPDATE_TIMEOUT;
  1407. *finished = 1;
  1408. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1409. "%s: Consumer timed out to replay change (uniqueid %s, CSN %s): "
  1410. "%s.\n",
  1411. agmt_get_long_name(prp->agmt),
  1412. uniqueid, csn_str,
  1413. connection_error ? ldap_err2string(connection_error) : "Timeout");
  1414. }
  1415. else if (CONN_LOCAL_ERROR == replay_crc)
  1416. {
  1417. /*
  1418. * Something bad happened on the local server - enter
  1419. * backoff state.
  1420. */
  1421. return_value = UPDATE_TRANSIENT_ERROR;
  1422. *finished = 1;
  1423. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1424. "%s: Failed to replay change (uniqueid %s, CSN %s): "
  1425. "Local error. Will retry later.\n",
  1426. agmt_get_long_name(prp->agmt),
  1427. uniqueid, csn_str);
  1428. }
  1429. }
  1430. else
  1431. {
  1432. /* Positive response received */
  1433. (*num_changes_sent)++;
  1434. agmt_inc_last_update_changecount (prp->agmt, replica_id, 0 /*replayed*/);
  1435. }
  1436. return return_value;
  1437. }
  1438. /*
  1439. * Send a set of updates to the replica. Assumes that (1) the replica
  1440. * has already been acquired, (2) that the consumer's update vector has
  1441. * been checked and (3) that it's ok to send incremental updates.
  1442. * Returns:
  1443. * UPDATE_NO_MORE_UPDATES - all updates were sent succussfully
  1444. * UPDATE_TRANSIENT_ERROR - some non-permanent error occurred. Try again later.
  1445. * UPDATE_FATAL_ERROR - some bad, permanent error occurred.
  1446. * UPDATE_SCHEDULE_WINDOW_CLOSED - the schedule window closed on us.
  1447. */
  1448. static int
  1449. send_updates(Private_Repl_Protocol *prp, RUV *remote_update_vector, PRUint32 *num_changes_sent)
  1450. {
  1451. CL5Entry entry;
  1452. slapi_operation_parameters op;
  1453. int return_value;
  1454. int rc;
  1455. CL5ReplayIterator *changelog_iterator;
  1456. int message_id = 0;
  1457. result_data *rd = NULL;
  1458. *num_changes_sent = 0;
  1459. /*
  1460. * Iterate over the changelog. Retrieve each update,
  1461. * construct an appropriate LDAP operation,
  1462. * attaching the CSN, and send the change.
  1463. */
  1464. rc = cl5CreateReplayIterator(prp, remote_update_vector, &changelog_iterator);
  1465. if (CL5_SUCCESS != rc)
  1466. {
  1467. switch (rc)
  1468. {
  1469. case CL5_BAD_DATA: /* invalid parameter passed to the function */
  1470. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1471. "%s: Invalid parameter passed to cl5CreateReplayIterator\n",
  1472. agmt_get_long_name(prp->agmt));
  1473. return_value = UPDATE_FATAL_ERROR;
  1474. break;
  1475. case CL5_BAD_FORMAT: /* db data has unexpected format */
  1476. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1477. "%s: Unexpected format encountered in changelog database\n",
  1478. agmt_get_long_name(prp->agmt));
  1479. return_value = UPDATE_FATAL_ERROR;
  1480. break;
  1481. case CL5_BAD_STATE: /* changelog is in an incorrect state for attempted operation */
  1482. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1483. "%s: Changelog database was in an incorrect state\n",
  1484. agmt_get_long_name(prp->agmt));
  1485. return_value = UPDATE_FATAL_ERROR;
  1486. break;
  1487. case CL5_BAD_DBVERSION: /* changelog has invalid dbversion */
  1488. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1489. "%s: Incorrect dbversion found in changelog database\n",
  1490. agmt_get_long_name(prp->agmt));
  1491. return_value = UPDATE_FATAL_ERROR;
  1492. break;
  1493. case CL5_DB_ERROR: /* database error */
  1494. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1495. "%s: A changelog database error was encountered\n",
  1496. agmt_get_long_name(prp->agmt));
  1497. return_value = UPDATE_FATAL_ERROR;
  1498. break;
  1499. case CL5_NOTFOUND: /* we have no changes to send */
  1500. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1501. "%s: No changes to send\n",
  1502. agmt_get_long_name(prp->agmt));
  1503. return_value = UPDATE_NO_MORE_UPDATES;
  1504. break;
  1505. case CL5_MEMORY_ERROR: /* memory allocation failed */
  1506. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1507. "%s: Memory allocation error occurred\n",
  1508. agmt_get_long_name(prp->agmt));
  1509. return_value = UPDATE_FATAL_ERROR;
  1510. break;
  1511. case CL5_SYSTEM_ERROR: /* NSPR error occurred: use PR_GetError for furhter info */
  1512. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1513. "%s: An NSPR error (%d) occurred\n",
  1514. agmt_get_long_name(prp->agmt), PR_GetError());
  1515. return_value = UPDATE_TRANSIENT_ERROR;
  1516. break;
  1517. case CL5_CSN_ERROR: /* CSN API failed */
  1518. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1519. "%s: A CSN API failure was encountered\n",
  1520. agmt_get_long_name(prp->agmt));
  1521. return_value = UPDATE_TRANSIENT_ERROR;
  1522. break;
  1523. case CL5_RUV_ERROR: /* RUV API failed */
  1524. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1525. "%s: An RUV API failure occurred\n",
  1526. agmt_get_long_name(prp->agmt));
  1527. return_value = UPDATE_TRANSIENT_ERROR;
  1528. break;
  1529. case CL5_OBJSET_ERROR: /* namedobjset api failed */
  1530. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1531. "%s: A namedobject API failure occurred\n",
  1532. agmt_get_long_name(prp->agmt));
  1533. return_value = UPDATE_TRANSIENT_ERROR;
  1534. break;
  1535. case CL5_PURGED_DATA: /* requested data has been purged */
  1536. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1537. "%s: Data required to update replica has been purged. "
  1538. "The replica must be reinitialized.\n",
  1539. agmt_get_long_name(prp->agmt));
  1540. return_value = UPDATE_FATAL_ERROR;
  1541. break;
  1542. case CL5_MISSING_DATA: /* data should be in the changelog, but is missing */
  1543. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1544. "%s: Missing data encountered\n",
  1545. agmt_get_long_name(prp->agmt));
  1546. return_value = UPDATE_FATAL_ERROR;
  1547. break;
  1548. case CL5_UNKNOWN_ERROR: /* unclassified error */
  1549. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1550. "%s: An unknown error was ecountered\n",
  1551. agmt_get_long_name(prp->agmt));
  1552. return_value = UPDATE_TRANSIENT_ERROR;
  1553. break;
  1554. default:
  1555. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1556. "%s: An unknown error (%d) occurred "
  1557. "(cl5CreateReplayIterator)\n",
  1558. agmt_get_long_name(prp->agmt), rc);
  1559. return_value = UPDATE_TRANSIENT_ERROR;
  1560. }
  1561. }
  1562. else
  1563. {
  1564. int finished = 0;
  1565. ConnResult replay_crc;
  1566. char csn_str[CSN_STRSIZE];
  1567. /* Start the results reading thread */
  1568. rd = repl5_inc_rd_new(prp);
  1569. if (!prp->repl50consumer)
  1570. {
  1571. rc = repl5_inc_create_async_result_thread(rd);
  1572. if (rc) {
  1573. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name, "%s: repl5_inc_run: "
  1574. "repl5_tot_create_async_result_thread failed; error - %d\n",
  1575. agmt_get_long_name(prp->agmt), rc);
  1576. return_value = UPDATE_FATAL_ERROR;
  1577. }
  1578. }
  1579. memset ( (void*)&op, 0, sizeof (op) );
  1580. entry.op = &op;
  1581. do {
  1582. cl5_operation_parameters_done ( entry.op );
  1583. memset ( (void*)entry.op, 0, sizeof (op) );
  1584. rc = cl5GetNextOperationToReplay(changelog_iterator, &entry);
  1585. switch (rc)
  1586. {
  1587. case CL5_SUCCESS:
  1588. /* check that we don't return dummy entries */
  1589. if (is_dummy_operation (entry.op))
  1590. {
  1591. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1592. "%s: changelog iteration code returned a dummy entry with csn %s, "
  1593. "skipping ...\n",
  1594. agmt_get_long_name(prp->agmt), csn_as_string(entry.op->csn, PR_FALSE, csn_str));
  1595. continue;
  1596. }
  1597. replay_crc = replay_update(prp, entry.op, &message_id);
  1598. if (message_id)
  1599. {
  1600. rd->last_message_id_sent = message_id;
  1601. }
  1602. /* If we're talking to an old non-async replica, we need to pick up the response here */
  1603. if (CONN_OPERATION_SUCCESS != replay_crc)
  1604. {
  1605. int operation, error;
  1606. conn_get_error(prp->conn, &operation, &error);
  1607. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1608. /* Figure out what to do next */
  1609. if (CONN_OPERATION_FAILED == replay_crc)
  1610. {
  1611. /* Map ldap error code to return value */
  1612. if (!ignore_error_and_keep_going(error))
  1613. {
  1614. return_value = UPDATE_TRANSIENT_ERROR;
  1615. finished = 1;
  1616. }
  1617. else
  1618. {
  1619. agmt_inc_last_update_changecount (prp->agmt, csn_get_replicaid(entry.op->csn), 1 /*skipped*/);
  1620. }
  1621. slapi_log_error(finished ? SLAPI_LOG_FATAL : slapi_log_urp, repl_plugin_name,
  1622. "%s: Failed to send update operation to consumer (uniqueid %s, CSN %s): %s. %s.\n",
  1623. agmt_get_long_name(prp->agmt),
  1624. entry.op->target_address.uniqueid, csn_str,
  1625. ldap_err2string(error),
  1626. finished ? "Will retry later" : "Skipping");
  1627. }
  1628. else if (CONN_NOT_CONNECTED == replay_crc)
  1629. {
  1630. /* We lost the connection - enter backoff state */
  1631. return_value = UPDATE_CONNECTION_LOST;
  1632. finished = 1;
  1633. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1634. "%s: Failed to send update operation to consumer (uniqueid %s, CSN %s): "
  1635. "%s. Will retry later.\n",
  1636. agmt_get_long_name(prp->agmt),
  1637. entry.op->target_address.uniqueid, csn_str,
  1638. error ? ldap_err2string(error) : "Connection lost");
  1639. }
  1640. else if (CONN_TIMEOUT == replay_crc)
  1641. {
  1642. return_value = UPDATE_TIMEOUT;
  1643. finished = 1;
  1644. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1645. "%s: Timed out sending update operation to consumer (uniqueid %s, CSN %s): "
  1646. "%s.\n",
  1647. agmt_get_long_name(prp->agmt),
  1648. entry.op->target_address.uniqueid, csn_str,
  1649. error ? ldap_err2string(error) : "Timeout");
  1650. }
  1651. else if (CONN_LOCAL_ERROR == replay_crc)
  1652. {
  1653. /*
  1654. * Something bad happened on the local server - enter
  1655. * backoff state.
  1656. */
  1657. return_value = UPDATE_TRANSIENT_ERROR;
  1658. finished = 1;
  1659. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1660. "%s: Failed to send update operation to consumer (uniqueid %s, CSN %s): "
  1661. "Local error. Will retry later.\n",
  1662. agmt_get_long_name(prp->agmt),
  1663. entry.op->target_address.uniqueid, csn_str);
  1664. }
  1665. }
  1666. else
  1667. {
  1668. char *uniqueid = NULL;
  1669. ReplicaId replica_id = 0;
  1670. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1671. replica_id = csn_get_replicaid(entry.op->csn);
  1672. uniqueid = entry.op->target_address.uniqueid;
  1673. if (prp->repl50consumer && message_id)
  1674. {
  1675. int operation, error = 0;
  1676. conn_get_error(prp->conn, &operation, &error);
  1677. /* Get the response here */
  1678. replay_crc = repl5_inc_get_next_result(rd);
  1679. conn_get_error(prp->conn, &operation, &error);
  1680. csn_as_string(entry.op->csn, PR_FALSE, csn_str);
  1681. return_value = repl5_inc_update_from_op_result(prp, replay_crc, error, csn_str, uniqueid, replica_id, &finished, num_changes_sent);
  1682. }
  1683. else if (message_id) {
  1684. /* Queue the details for pickup later in the response thread */
  1685. repl5_inc_operation *sop = NULL;
  1686. sop = repl5_inc_operation_new();
  1687. PL_strncpyz(sop->csn_str, csn_str, sizeof(sop->csn_str));
  1688. sop->ldap_message_id = message_id;
  1689. sop->operation_type = entry.op->operation_type;
  1690. sop->replica_id = replica_id;
  1691. PL_strncpyz(sop->uniqueid, uniqueid, sizeof(sop->uniqueid));
  1692. repl5_int_push_operation(rd,sop);
  1693. } else {
  1694. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1695. "%s: Skipping update operation with no message_id (uniqueid %s, CSN %s):\n",
  1696. agmt_get_long_name(prp->agmt),
  1697. entry.op->target_address.uniqueid, csn_str);
  1698. agmt_inc_last_update_changecount (prp->agmt, csn_get_replicaid(entry.op->csn), 1 /*skipped*/);
  1699. }
  1700. }
  1701. break;
  1702. case CL5_BAD_DATA:
  1703. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1704. "%s: Invalid parameter passed to cl5GetNextOperationToReplay\n",
  1705. agmt_get_long_name(prp->agmt));
  1706. return_value = UPDATE_FATAL_ERROR;
  1707. finished = 1;
  1708. break;
  1709. case CL5_NOTFOUND:
  1710. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1711. "%s: No more updates to send (cl5GetNextOperationToReplay)\n",
  1712. agmt_get_long_name(prp->agmt));
  1713. return_value = UPDATE_NO_MORE_UPDATES;
  1714. finished = 1;
  1715. break;
  1716. case CL5_DB_ERROR:
  1717. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1718. "%s: A database error occurred (cl5GetNextOperationToReplay)\n",
  1719. agmt_get_long_name(prp->agmt));
  1720. return_value = UPDATE_FATAL_ERROR;
  1721. finished = 1;
  1722. break;
  1723. case CL5_BAD_FORMAT:
  1724. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1725. "%s: A malformed changelog entry was encountered (cl5GetNextOperationToReplay)\n",
  1726. agmt_get_long_name(prp->agmt));
  1727. break;
  1728. case CL5_MEMORY_ERROR:
  1729. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1730. "%s: A memory allocation error occurred (cl5GetNextOperationToRepla)\n",
  1731. agmt_get_long_name(prp->agmt));
  1732. return_value = UPDATE_FATAL_ERROR;
  1733. break;
  1734. default:
  1735. slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
  1736. "%s: Unknown error code (%d) returned from cl5GetNextOperationToReplay\n",
  1737. agmt_get_long_name(prp->agmt), rc);
  1738. return_value = UPDATE_TRANSIENT_ERROR;
  1739. break;
  1740. }
  1741. /* Check for protocol shutdown */
  1742. if (prp->terminate)
  1743. {
  1744. return_value = UPDATE_NO_MORE_UPDATES;
  1745. finished = 1;
  1746. }
  1747. if (*num_changes_sent >= MAX_CHANGES_PER_SESSION)
  1748. {
  1749. return_value = UPDATE_YIELD;
  1750. finished = 1;
  1751. }
  1752. PR_Lock(rd->lock);
  1753. /* See if the result thread has hit a problem */
  1754. if (!finished && rd->abort)
  1755. {
  1756. return_value = rd->result;
  1757. finished = 1;
  1758. }
  1759. PR_Unlock(rd->lock);
  1760. } while (!finished);
  1761. /* Terminate the results reading thread */
  1762. if (!prp->repl50consumer)
  1763. {
  1764. /* We need to ensure that we wait until all the responses have been recived from our operations */
  1765. if (return_value != UPDATE_CONNECTION_LOST) {
  1766. /* if connection was lost/closed, there will be nothing to read */
  1767. repl5_inc_waitfor_async_results(rd);
  1768. }
  1769. rc = repl5_inc_destroy_async_result_thread(rd);
  1770. if (rc) {
  1771. slapi_log_error (SLAPI_LOG_FATAL, repl_plugin_name, "%s: repl5_inc_run: "
  1772. "repl5_tot_destroy_async_result_thread failed; error - %d\n",
  1773. agmt_get_long_name(prp->agmt), rc);
  1774. }
  1775. *num_changes_sent = rd->num_changes_sent;
  1776. }
  1777. repl5_inc_rd_destroy(&rd);
  1778. cl5_operation_parameters_done ( entry.op );
  1779. cl5DestroyReplayIterator(&changelog_iterator);
  1780. }
  1781. return return_value;
  1782. }
  1783. /*
  1784. * XXXggood this should probably be in the superclass, since the full update
  1785. * protocol is going to need it too.
  1786. */
  1787. static int
  1788. repl5_inc_stop(Private_Repl_Protocol *prp)
  1789. {
  1790. int return_value;
  1791. PRIntervalTime start, maxwait, now;
  1792. int seconds = 1200;
  1793. maxwait = PR_SecondsToInterval(seconds);
  1794. prp->terminate = 1;
  1795. event_notify(prp, EVENT_PROTOCOL_SHUTDOWN);
  1796. start = PR_IntervalNow();
  1797. now = start;
  1798. while (!prp->stopped && ((now - start) < maxwait))
  1799. {
  1800. DS_Sleep(PR_MillisecondsToInterval(100));
  1801. now = PR_IntervalNow();
  1802. }
  1803. if (!prp->stopped)
  1804. {
  1805. /* Isn't listening. Do something drastic. */
  1806. return_value = -1;
  1807. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1808. "%s: repl5_inc_stop: protocol does not stop after %d seconds\n",
  1809. agmt_get_long_name(prp->agmt), seconds);
  1810. }
  1811. else
  1812. {
  1813. return_value = 0;
  1814. slapi_log_error(SLAPI_LOG_REPL, repl_plugin_name,
  1815. "%s: repl5_inc_stop: protocol stopped after %d seconds\n",
  1816. agmt_get_long_name(prp->agmt),
  1817. PR_IntervalToSeconds(now-start));
  1818. }
  1819. return return_value;
  1820. }
  1821. static int
  1822. repl5_inc_status(Private_Repl_Protocol *prp)
  1823. {
  1824. int return_value = 0;
  1825. return return_value;
  1826. }
  1827. static void
  1828. repl5_inc_notify_update(Private_Repl_Protocol *prp)
  1829. {
  1830. event_notify(prp, EVENT_TRIGGERING_CRITERIA_MET);
  1831. }
  1832. static void
  1833. repl5_inc_update_now(Private_Repl_Protocol *prp)
  1834. {
  1835. event_notify(prp, EVENT_REPLICATE_NOW);
  1836. }
  1837. static void
  1838. repl5_inc_notify_agmt_changed(Private_Repl_Protocol *prp)
  1839. {
  1840. event_notify(prp, EVENT_AGMT_CHANGED);
  1841. }
  1842. static void
  1843. repl5_inc_notify_window_opened (Private_Repl_Protocol *prp)
  1844. {
  1845. event_notify(prp, EVENT_WINDOW_OPENED);
  1846. }
  1847. static void
  1848. repl5_inc_notify_window_closed (Private_Repl_Protocol *prp)
  1849. {
  1850. event_notify(prp, EVENT_WINDOW_CLOSED);
  1851. }
  1852. Private_Repl_Protocol *
  1853. Repl_5_Inc_Protocol_new(Repl_Protocol *rp)
  1854. {
  1855. repl5_inc_private *rip = NULL;
  1856. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)slapi_ch_malloc(sizeof(Private_Repl_Protocol));
  1857. prp->delete = repl5_inc_delete;
  1858. prp->run = repl5_inc_run;
  1859. prp->stop = repl5_inc_stop;
  1860. prp->status = repl5_inc_status;
  1861. prp->notify_update = repl5_inc_notify_update;
  1862. prp->notify_agmt_changed = repl5_inc_notify_agmt_changed;
  1863. prp->notify_window_opened = repl5_inc_notify_window_opened;
  1864. prp->notify_window_closed = repl5_inc_notify_window_closed;
  1865. prp->update_now = repl5_inc_update_now;
  1866. prp->replica_object = prot_get_replica_object(rp);
  1867. if ((prp->lock = PR_NewLock()) == NULL)
  1868. {
  1869. goto loser;
  1870. }
  1871. if ((prp->cvar = PR_NewCondVar(prp->lock)) == NULL)
  1872. {
  1873. goto loser;
  1874. }
  1875. prp->stopped = 0;
  1876. prp->terminate = 0;
  1877. prp->eventbits = 0;
  1878. prp->conn = prot_get_connection(rp);
  1879. prp->agmt = prot_get_agreement(rp);
  1880. prp->last_acquire_response_code = NSDS50_REPL_REPLICA_READY;
  1881. rip = (void *)slapi_ch_malloc(sizeof(repl5_inc_private));
  1882. rip->ruv = NULL;
  1883. rip->backoff = NULL;
  1884. rip->rp = rp;
  1885. prp->private = (void *)rip;
  1886. prp->replica_acquired = PR_FALSE;
  1887. prp->repl50consumer = 0;
  1888. prp->repl71consumer = 0;
  1889. prp->repl90consumer = 0;
  1890. return prp;
  1891. loser:
  1892. repl5_inc_delete(&prp);
  1893. return NULL;
  1894. }
  1895. static void
  1896. repl5_inc_backoff_expired(time_t timer_fire_time, void *arg)
  1897. {
  1898. Private_Repl_Protocol *prp = (Private_Repl_Protocol *)arg;
  1899. PR_ASSERT(NULL != prp);
  1900. event_notify(prp, EVENT_BACKOFF_EXPIRED);
  1901. }
  1902. /*
  1903. * Examine the update vector and determine our course of action.
  1904. * There are 3 different possibilities, plus a catch-all error:
  1905. * 1 - no update vector (ruv is NULL). The consumer's replica is
  1906. * pristine, so it needs to be initialized. Return
  1907. * EXAMINE_RUV_PRISTINE_REPLICA.
  1908. * 2 - ruv is present, but its database generation ID doesn't
  1909. * match the local generation ID. This means that either
  1910. * the local replica must be reinitialized from the remote
  1911. * replica or vice-versa. Return
  1912. * EXAMINE_RUV_GENERATION_MISMATCH.
  1913. * 3 - ruv is present, and we have all updates needed to bring
  1914. * the replica up to date using the incremental protocol.
  1915. * return EXAMINE_RUV_OK.
  1916. * 4 - parameter error. Return EXAMINE_RUV_PARAM_ERROR
  1917. */
  1918. static int
  1919. examine_update_vector(Private_Repl_Protocol *prp, RUV *remote_ruv)
  1920. {
  1921. int return_value;
  1922. PR_ASSERT(NULL != prp);
  1923. if (NULL == prp)
  1924. {
  1925. return_value = EXAMINE_RUV_PARAM_ERROR;
  1926. }
  1927. else if (NULL == remote_ruv)
  1928. {
  1929. return_value = EXAMINE_RUV_PRISTINE_REPLICA;
  1930. }
  1931. else
  1932. {
  1933. char *local_gen = NULL;
  1934. char *remote_gen = ruv_get_replica_generation(remote_ruv);
  1935. Object *local_ruv_obj;
  1936. RUV *local_ruv;
  1937. Replica *replica;
  1938. PR_ASSERT(NULL != prp->replica_object);
  1939. replica = object_get_data(prp->replica_object);
  1940. PR_ASSERT(NULL != replica);
  1941. local_ruv_obj = replica_get_ruv (replica);
  1942. if (NULL != local_ruv_obj)
  1943. {
  1944. local_ruv = (RUV*)object_get_data (local_ruv_obj);
  1945. PR_ASSERT (local_ruv);
  1946. local_gen = ruv_get_replica_generation(local_ruv);
  1947. object_release (local_ruv_obj);
  1948. }
  1949. if (NULL == remote_gen || NULL == local_gen || strcmp(remote_gen, local_gen) != 0)
  1950. {
  1951. return_value = EXAMINE_RUV_GENERATION_MISMATCH;
  1952. }
  1953. else
  1954. {
  1955. return_value = EXAMINE_RUV_OK;
  1956. }
  1957. slapi_ch_free((void**)&remote_gen);
  1958. slapi_ch_free((void**)&local_gen);
  1959. }
  1960. return return_value;
  1961. }
  1962. /*
  1963. * When we get an error from an LDAP operation, we call this
  1964. * function to decide if we should just keep replaying
  1965. * updates, or if we should stop, back off, and try again
  1966. * later.
  1967. * Returns PR_TRUE if we shoould keep going, PR_FALSE if
  1968. * we should back off and try again later.
  1969. *
  1970. * In general, we keep going if the return code is consistent
  1971. * with some sort of bug in URP that causes the consumer to
  1972. * emit an error code that it shouldn't have, e.g. LDAP_ALREADY_EXISTS.
  1973. *
  1974. * We stop if there's some indication that the server just completely
  1975. * failed to process the operation, e.g. LDAP_OPERATIONS_ERROR.
  1976. */
  1977. static PRBool
  1978. ignore_error_and_keep_going(int error)
  1979. {
  1980. int return_value = PR_FALSE;
  1981. switch (error)
  1982. {
  1983. /* Cases where we keep going */
  1984. case LDAP_SUCCESS:
  1985. case LDAP_NO_SUCH_ATTRIBUTE:
  1986. case LDAP_UNDEFINED_TYPE:
  1987. case LDAP_CONSTRAINT_VIOLATION:
  1988. case LDAP_TYPE_OR_VALUE_EXISTS:
  1989. case LDAP_INVALID_SYNTAX:
  1990. case LDAP_NO_SUCH_OBJECT:
  1991. case LDAP_INVALID_DN_SYNTAX:
  1992. case LDAP_IS_LEAF:
  1993. case LDAP_INSUFFICIENT_ACCESS:
  1994. case LDAP_NAMING_VIOLATION:
  1995. case LDAP_OBJECT_CLASS_VIOLATION:
  1996. case LDAP_NOT_ALLOWED_ON_NONLEAF:
  1997. case LDAP_NOT_ALLOWED_ON_RDN:
  1998. case LDAP_ALREADY_EXISTS:
  1999. case LDAP_NO_OBJECT_CLASS_MODS:
  2000. return_value = PR_TRUE;
  2001. break;
  2002. /* Cases where we stop and retry */
  2003. case LDAP_OPERATIONS_ERROR:
  2004. case LDAP_PROTOCOL_ERROR:
  2005. case LDAP_TIMELIMIT_EXCEEDED:
  2006. case LDAP_SIZELIMIT_EXCEEDED:
  2007. case LDAP_STRONG_AUTH_NOT_SUPPORTED:
  2008. case LDAP_STRONG_AUTH_REQUIRED:
  2009. case LDAP_PARTIAL_RESULTS:
  2010. case LDAP_REFERRAL:
  2011. case LDAP_ADMINLIMIT_EXCEEDED:
  2012. case LDAP_UNAVAILABLE_CRITICAL_EXTENSION:
  2013. case LDAP_CONFIDENTIALITY_REQUIRED:
  2014. case LDAP_SASL_BIND_IN_PROGRESS:
  2015. case LDAP_INAPPROPRIATE_MATCHING:
  2016. case LDAP_ALIAS_PROBLEM:
  2017. case LDAP_ALIAS_DEREF_PROBLEM:
  2018. case LDAP_INAPPROPRIATE_AUTH:
  2019. case LDAP_INVALID_CREDENTIALS:
  2020. case LDAP_BUSY:
  2021. case LDAP_UNAVAILABLE:
  2022. case LDAP_UNWILLING_TO_PERFORM:
  2023. case LDAP_LOOP_DETECT:
  2024. case LDAP_SORT_CONTROL_MISSING:
  2025. case LDAP_INDEX_RANGE_ERROR:
  2026. case LDAP_RESULTS_TOO_LARGE:
  2027. case LDAP_AFFECTS_MULTIPLE_DSAS:
  2028. case LDAP_OTHER:
  2029. case LDAP_SERVER_DOWN:
  2030. case LDAP_LOCAL_ERROR:
  2031. case LDAP_ENCODING_ERROR:
  2032. case LDAP_DECODING_ERROR:
  2033. case LDAP_TIMEOUT:
  2034. case LDAP_AUTH_UNKNOWN:
  2035. case LDAP_FILTER_ERROR:
  2036. case LDAP_USER_CANCELLED:
  2037. case LDAP_PARAM_ERROR:
  2038. case LDAP_NO_MEMORY:
  2039. case LDAP_CONNECT_ERROR:
  2040. case LDAP_NOT_SUPPORTED:
  2041. case LDAP_CONTROL_NOT_FOUND:
  2042. case LDAP_NO_RESULTS_RETURNED:
  2043. case LDAP_MORE_RESULTS_TO_RETURN:
  2044. case LDAP_CLIENT_LOOP:
  2045. case LDAP_REFERRAL_LIMIT_EXCEEDED:
  2046. return_value = PR_FALSE;
  2047. break;
  2048. }
  2049. return return_value;
  2050. }
  2051. /* this function converts a state to its name - for debug output */
  2052. static const char*
  2053. state2name (int state)
  2054. {
  2055. switch (state)
  2056. {
  2057. case STATE_START: return "start";
  2058. case STATE_WAIT_WINDOW_OPEN: return "wait_for_window_to_open";
  2059. case STATE_WAIT_CHANGES: return "wait_for_changes";
  2060. case STATE_READY_TO_ACQUIRE: return "ready_to_acquire_replica";
  2061. case STATE_BACKOFF_START: return "start_backoff";
  2062. case STATE_BACKOFF: return "backoff";
  2063. case STATE_SENDING_UPDATES: return "sending_updates";
  2064. case STATE_STOP_FATAL_ERROR: return "stop_fatal_error";
  2065. case STATE_STOP_FATAL_ERROR_PART2: return "stop_fatal_error";
  2066. case STATE_STOP_NORMAL_TERMINATION: return "stop_normal_termination";
  2067. default: return "invalid_state";
  2068. }
  2069. }
  2070. /* this function convert s an event to its name - for debug output */
  2071. static const char*
  2072. event2name (int event)
  2073. {
  2074. switch (event)
  2075. {
  2076. case EVENT_WINDOW_OPENED: return "update_window_opened";
  2077. case EVENT_WINDOW_CLOSED: return "update_window_closed";
  2078. case EVENT_TRIGGERING_CRITERIA_MET: return "data_modified";
  2079. case EVENT_BACKOFF_EXPIRED: return "backoff_timer_expired";
  2080. case EVENT_REPLICATE_NOW: return "replicate_now";
  2081. case EVENT_PROTOCOL_SHUTDOWN: return "protocol_shutdown";
  2082. case EVENT_AGMT_CHANGED: return "agreement_changed";
  2083. default: return "invalid_event";
  2084. }
  2085. }
  2086. static const char*
  2087. op2string(int op)
  2088. {
  2089. switch (op) {
  2090. case SLAPI_OPERATION_ADD:
  2091. return "add";
  2092. case SLAPI_OPERATION_MODIFY:
  2093. return "modify";
  2094. case SLAPI_OPERATION_DELETE:
  2095. return "delete";
  2096. case SLAPI_OPERATION_MODRDN:
  2097. return "rename";
  2098. case SLAPI_OPERATION_EXTENDED:
  2099. return "extended";
  2100. }
  2101. return "unknown";
  2102. }